-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth-diagnosis-plugin.php
executable file
·443 lines (373 loc) · 16.6 KB
/
health-diagnosis-plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
/**
* Plugin Name: Health Diagnosis Plugin
* Description: A WordPress plugin for health diagnosis.
* Version: 1.0.0
* Author: Mohammed Foud
* Author URI: https://mfoud4444.web.app
*/
if (!defined('ABSPATH')) {
exit;
}
define('HEALTH_DIAGNOSIS_PLUGIN_VERSION', '1.0.0');
define('HEALTH_DIAGNOSIS_PLUGIN_PATH', plugin_dir_path(__FILE__));
define('HEALTH_DIAGNOSIS_PLUGIN_URL', plugin_dir_url(__FILE__));
require_once HEALTH_DIAGNOSIS_PLUGIN_PATH . 'hd-plugin/admin.php';
require_once HEALTH_DIAGNOSIS_PLUGIN_PATH . 'hd-plugin/client.php';
global $jal_db_version;
$jal_db_version = '1.0';
/*Creating or Updating the Table*/
function jal_install()
{
global $wpdb;
global $jal_db_version;
$table_name_plant = $wpdb->prefix . 'plant_ai';
$table_name_disease = $wpdb->prefix . 'disease_ai';
$table_name_modelai = $wpdb->prefix . 'model_ai';
$table_name_history = $wpdb->prefix . 'history_user_detection';
$table_name_translations = $wpdb->prefix . 'translations';
$table_name_product_disease = $wpdb->prefix . 'product_disease';
// Execute queries
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$charset_collate = $wpdb->get_charset_collate();
// Check if plant table already exists
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name_plant'") != $table_name_plant) {
// Plant table creation query...
$sql_plant = "CREATE TABLE $table_name_plant (
id mediumint(9) NOT NULL AUTO_INCREMENT,
picture varchar(255) ,
status boolean NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
dbDelta($sql_plant);
}
// Check if disease table already exists
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name_disease'") != $table_name_disease) {
// Disease table creation query...
$sql_disease = "CREATE TABLE $table_name_disease (
id mediumint(9) NOT NULL AUTO_INCREMENT,
plant_id mediumint(9) NOT NULL,
key_label INT NOT NULL,
pictures text,
status boolean NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (plant_id) REFERENCES $table_name_plant(id) ON DELETE CASCADE
) $charset_collate;";
dbDelta($sql_disease);
}
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name_translations'") != $table_name_translations) {
$sql_translations = "CREATE TABLE $table_name_translations (
id mediumint(9) NOT NULL AUTO_INCREMENT,
language_code varchar(5) NOT NULL,
translated_text text NOT NULL,
entity_type varchar(50) NOT NULL,
entity_id mediumint(9) NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY (entity_type, entity_id, language_code)
) $charset_collate;";
dbDelta($sql_translations);
}
// Check if model AI table already exists
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name_modelai'") != $table_name_modelai) {
// Model AI table creation query...
$sql_model_ai = "CREATE TABLE $table_name_modelai (
id mediumint(9) NOT NULL AUTO_INCREMENT,
plant_id mediumint(9) NOT NULL,
name varchar(255) NOT NULL,
description text DEFAULT NULL,
github_url varchar(255) NOT NULL,
model_json_file varchar(255) NOT NULL,
model_weights_file varchar(255) NOT NULL,
model_meta_file varchar(255) NOT NULL,
model_class_file varchar(255) NOT NULL,
version varchar(255) DEFAULT NULL,
status boolean NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (plant_id) REFERENCES $table_name_plant(id) ON DELETE CASCADE
) $charset_collate;";
dbDelta($sql_model_ai);
}
// Check if history table already exists
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name_history'") != $table_name_history) {
// History table creation query...
$sql_history = "CREATE TABLE $table_name_history (
id mediumint(9) NOT NULL AUTO_INCREMENT,
id_user bigint(20) UNSIGNED NOT NULL,
picture varchar(255) NOT NULL,
prediction_result_value varchar(255) NOT NULL,
plant_id mediumint(9) NOT NULL,
correct_disease_id mediumint(9) NOT NULL,
model_id mediumint(9) NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (id_user) REFERENCES {$wpdb->users}(ID),
FOREIGN KEY (plant_id) REFERENCES $table_name_plant(id) ON DELETE CASCADE,
FOREIGN KEY (correct_disease_id) REFERENCES $table_name_disease(id) ON DELETE CASCADE,
FOREIGN KEY (model_id) REFERENCES $table_name_modelai(id) ON DELETE CASCADE
) $charset_collate;";
dbDelta($sql_history);
}
// Create the product_disease table
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name_product_disease'") != $table_name_product_disease) {
$sql_product_disease = "CREATE TABLE $table_name_product_disease (
id mediumint(9) NOT NULL AUTO_INCREMENT,
disease_id mediumint(9) NOT NULL,
product_id bigint(20) UNSIGNED NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (disease_id) REFERENCES $table_name_disease(id) ON DELETE CASCADE,
FOREIGN KEY (product_id) REFERENCES {$wpdb->prefix}posts(ID) ON DELETE CASCADE
) $charset_collate;";
dbDelta($sql_product_disease);
}
// Update plugin version
add_option('jal_db_version', $jal_db_version);
}
function jal_install_data() {
global $wpdb;
$table_name_plant = $wpdb->prefix . 'plant_ai';
$table_name_disease = $wpdb->prefix . 'disease_ai';
$table_name_modelai = $wpdb->prefix . 'model_ai';
$table_name_translations = $wpdb->prefix . 'translations';
// Read JSON file
$json_data = file_get_contents( plugin_dir_path(__FILE__) . '/data.json');
// Decode JSON data
$data = json_decode($json_data, true);
// Define plant folders and labels
$plant_folder = array_keys($data);
$plant_labels = array();
foreach ($data as $key => $value) {
array_push($plant_labels, $value['label']['en']);
}
// Insert plants, diseases, and models
foreach ($plant_labels as $key => $label) {
// Insert plant
$wpdb->insert(
$table_name_plant,
array(
'status' => 1,
'created_at' => current_time('mysql'),
'updated_at' => current_time('mysql')
),
array('%d', '%s', '%s')
);
$plant_id = $wpdb->insert_id;
// Insert plant translation
$wpdb->insert(
$table_name_translations,
array(
'language_code' => 'en',
'translated_text' => $label,
'entity_type' => 'plant',
'entity_id' => $plant_id,
'created_at' => current_time('mysql'),
'updated_at' => current_time('mysql')
),
array('%s', '%s', '%s', '%d', '%s', '%s')
);
// Insert Arabic plant translation
$wpdb->insert(
$table_name_translations,
array(
'language_code' => 'ar',
'translated_text' => $data[$plant_folder[$key]]['label']['ar'],
'entity_type' => 'plant',
'entity_id' => $plant_id,
'created_at' => current_time('mysql'),
'updated_at' => current_time('mysql')
),
array('%s', '%s', '%s', '%d', '%s', '%s')
);
// Insert diseases
$disease_labels = array_keys($data[$plant_folder[$key]]['diseases']);
foreach ($disease_labels as $disease_key => $disease_name) {
$description = $data[$plant_folder[$key]]['diseases'][$disease_name]['description']['en'];
$health_condition = $data[$plant_folder[$key]]['diseases'][$disease_name]['health_condition']['en'];
// Insert disease
$wpdb->insert(
$table_name_disease,
array(
'plant_id' => $plant_id,
'key_label' => $disease_key,
'status' => 1,
'created_at' => current_time('mysql'),
'updated_at' => current_time('mysql')
),
array('%d', '%d', '%d', '%s', '%s')
);
$disease_id = $wpdb->insert_id;
// Insert disease translation
$wpdb->insert(
$table_name_translations,
array(
'language_code' => 'en',
'translated_text' => $disease_name,
'entity_type' => 'disease',
'entity_id' => $disease_id,
'created_at' => current_time('mysql'),
'updated_at' => current_time('mysql')
),
array('%s', '%s', '%s', '%d', '%s', '%s')
);
// Insert Arabic disease translation
$wpdb->insert(
$table_name_translations,
array(
'language_code' => 'ar',
'translated_text' => $data[$plant_folder[$key]]['diseases'][$disease_name]['disease_name']['ar'],
'entity_type' => 'disease',
'entity_id' => $disease_id,
'created_at' => current_time('mysql'),
'updated_at' => current_time('mysql')
),
array('%s', '%s', '%s', '%d', '%s', '%s')
);
// Insert disease description translation
$wpdb->insert(
$table_name_translations,
array(
'language_code' => 'en',
'translated_text' => $description,
'entity_type' => 'disease_description',
'entity_id' => $disease_id,
'created_at' => current_time('mysql'),
'updated_at' => current_time('mysql')
),
array('%s', '%s', '%s', '%d', '%s', '%s')
);
// Insert Arabic disease description translation
$wpdb->insert(
$table_name_translations,
array(
'language_code' => 'ar',
'translated_text' => $data[$plant_folder[$key]]['diseases'][$disease_name]['description']['ar'],
'entity_type' => 'disease_description',
'entity_id' => $disease_id,
'created_at' => current_time('mysql'),
'updated_at' => current_time('mysql')
),
array('%s', '%s', '%s', '%d', '%s', '%s')
);
// Insert disease health condition translation
$wpdb->insert(
$table_name_translations,
array(
'language_code' => 'en',
'translated_text' => $health_condition,
'entity_type' => 'disease_health_condition',
'entity_id' => $disease_id,
'created_at' => current_time('mysql'),
'updated_at' => current_time('mysql')
),
array('%s', '%s', '%s', '%d', '%s', '%s')
);
// Insert Arabic disease health condition translation
$wpdb->insert(
$table_name_translations,
array(
'language_code' => 'ar',
'translated_text' => $data[$plant_folder[$key]]['diseases'][$disease_name]['health_condition']['ar'],
'entity_type' => 'disease_health_condition',
'entity_id' => $disease_id,
'created_at' => current_time('mysql'),
'updated_at' => current_time('mysql')
),
array('%s', '%s', '%s', '%d', '%s', '%s')
);
}
$github_url = '/~https://github.com/shahd1995913/models_world_of_plants_2022/blob/main/'.$plant_folder[$key].'/model';
$github_url = str_replace('/blob', '', $github_url);
// Generate random characters for the folder name
$random_chars = uniqid('');
// Generate random folder name starting with "model"
$random_folder_name = 'model_' . $random_chars;
// Download files from GitHub and save them in the same random folder
$model_json_file_path = download_and_save_file($github_url, 'model.json', $random_folder_name);
if (is_wp_error($model_json_file_path)) {
return $model_json_file_path;
}
$model_weights_file_path = download_and_save_file($github_url, 'weights.bin', $random_folder_name);
if (is_wp_error($model_weights_file_path)) {
return $model_weights_file_path;
}
$model_meta_file_path = download_and_save_file($github_url, 'metadata.json', $random_folder_name);
if (is_wp_error($model_meta_file_path)) {
return $model_meta_file_path;
}
$model_class_file_path = download_and_save_file($github_url, 'class.json', $random_folder_name);
if (is_wp_error($model_class_file_path)) {
return $model_class_file_path;
}
// Insert model for the plant
$model_data = array(
'plant_id' => $plant_id,
'name' => $label . ' Model',
'description' => 'A machine learning model trained to identify diseases and pests affecting ' . $label . ' plants.',
'github_url' => $github_url,
'model_json_file' => $model_json_file_path,
'model_weights_file' => $model_weights_file_path,
'model_meta_file' => $model_meta_file_path,
'model_class_file' => $model_class_file_path,
'version' => '1.0',
'status' => 1,
'created_at' => current_time('mysql'),
'updated_at' => current_time('mysql')
);
$wpdb->insert(
$table_name_modelai,
$model_data,
array(
'%d',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%d',
'%s',
'%s'
)
);
}
}
register_activation_hook(__FILE__, 'jal_install');
register_activation_hook( __FILE__, 'jal_install_data');
function health_diagnosis_plugin_uninstall() {
global $wpdb;
$table_name_plant = $wpdb->prefix . 'plant_ai';
$table_name_disease = $wpdb->prefix . 'disease_ai';
$table_name_modelai = $wpdb->prefix . 'model_ai';
$table_name_history = $wpdb->prefix . 'history_user_detection';
$table_name_translations = $wpdb->prefix . 'translations';
$table_name_product_disease = $wpdb->prefix . 'product_disease';
// Drop the tables
$wpdb->query("DROP TABLE IF EXISTS $table_name_history");
$wpdb->query("DROP TABLE IF EXISTS $table_name_modelai");
$wpdb->query("DROP TABLE IF EXISTS $table_name_product_disease");
$wpdb->query("DROP TABLE IF EXISTS $table_name_disease");
$wpdb->query("DROP TABLE IF EXISTS $table_name_translations");
$wpdb->query("DROP TABLE IF EXISTS $table_name_plant");
// Delete plugin options
delete_option('jal_db_version');
}
register_uninstall_hook(__FILE__, 'health_diagnosis_plugin_uninstall');
function myplugin_update_db_check()
{
global $jal_db_version;
if (get_site_option('jal_db_version') != $jal_db_version) {
jal_install();
}
}
add_action('plugins_loaded', 'myplugin_update_db_check');