class-affiliate.php
2 years ago
class-polylang.php
2 years ago
class-review-box.php
2 years ago
class-upgrade-box.php
2 years ago
class-wpml.php
2 years ago
folders.class.php
2 years ago
form.class.php
2 years ago
media.replace.php
2 years ago
plugins.class.php
2 years ago
tree.class.php
2 years ago
plugins.class.php
814 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class Folders Plugins import/export |
| 4 | * |
| 5 | * @author : Premio <contact@premio.io> |
| 6 | * @license : GPL2 |
| 7 | * */ |
| 8 | |
| 9 | if (! defined('ABSPATH')) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | // Free/Pro Class name change |
| 14 | class WCP_Folder_Plugins |
| 15 | { |
| 16 | |
| 17 | /** |
| 18 | * Collection of Plugins to import Data |
| 19 | * |
| 20 | * @var array $plugins Collection of Plugins |
| 21 | * @since 1.0.0 |
| 22 | * @access public |
| 23 | */ |
| 24 | public $plugins = []; |
| 25 | |
| 26 | /** |
| 27 | * Collection of Post types to Import |
| 28 | * |
| 29 | * @var array $post_types Post types to import |
| 30 | * @since 1.0.0 |
| 31 | * @access public |
| 32 | */ |
| 33 | public $post_types = []; |
| 34 | |
| 35 | /** |
| 36 | * Check is there any data to import |
| 37 | * |
| 38 | * @var integer $is_exists 0/1 |
| 39 | * @since 1.0.0 |
| 40 | * @access public |
| 41 | */ |
| 42 | public $is_exists = 0; |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Define the core functionality of the import data functionality. |
| 47 | * Define the core functionality of the import data functionality. |
| 48 | * |
| 49 | * Import data from other plugins |
| 50 | * Remove data from other plugins |
| 51 | * |
| 52 | * @since 1.0.0 |
| 53 | */ |
| 54 | public function __construct() |
| 55 | { |
| 56 | |
| 57 | // Import plugin data |
| 58 | add_action('wp_ajax_wcp_import_plugin_folders_data', [$this, 'import_plugin_folders_data']); |
| 59 | add_action('wp_ajax_wcp_remove_plugin_folders_data', [$this, 'remove_plugin_folders_data']); |
| 60 | |
| 61 | }//end __construct() |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Remove data from other plugins |
| 66 | * |
| 67 | * @since 1.0.0 |
| 68 | * @access public |
| 69 | * @return $response |
| 70 | */ |
| 71 | public function remove_plugin_folders_data() |
| 72 | { |
| 73 | global $wpdb; |
| 74 | $postData = filter_input_array(INPUT_POST); |
| 75 | |
| 76 | $plugin = isset($postData['plugin']) ? sanitize_text_field($postData['plugin']) : ""; |
| 77 | $nonce = isset($postData['nonce']) ? sanitize_text_field($postData['nonce']) : ""; |
| 78 | $response = []; |
| 79 | $response['status'] = 0; |
| 80 | $response['message'] = esc_html__("Invalid request", "folders"); |
| 81 | $response['data'] = []; |
| 82 | $response['data']['plugin'] = $plugin; |
| 83 | if (wp_verify_nonce($nonce, "import_data_from_".$plugin)) { |
| 84 | $this->get_plugin_information(); |
| 85 | $folders = isset($this->plugins[$plugin]['folders']) ? $this->plugins[$plugin]['folders'] : []; |
| 86 | $attachments = isset($this->plugins[$plugin]['attachments']) ? $this->plugins[$plugin]['attachments'] : []; |
| 87 | |
| 88 | if ($plugin != 'filebird' && $plugin != 'real-media-library' && $plugin != 'catfolders') { |
| 89 | $deleted = []; |
| 90 | |
| 91 | foreach ($folders as $folder) { |
| 92 | $term_id = intval($folder->term_id); |
| 93 | |
| 94 | if ($term_id) { |
| 95 | $deleted[$term_id]['term_relationships'] = $wpdb->delete($wpdb->prefix.'term_relationships', ['term_taxonomy_id' => $term_id]); |
| 96 | $deleted[$term_id]['term_taxonomy'] = $wpdb->delete($wpdb->prefix.'term_taxonomy', ['term_id' => $term_id]); |
| 97 | $deleted[$term_id]['terms'] = $wpdb->delete($wpdb->prefix.'terms', ['term_id' => $term_id]); |
| 98 | |
| 99 | if ($plugin === 'folders') { |
| 100 | $deleted[$term_id]['termmeta'] = $wpdb->delete($wpdb->prefix.'termmeta', ['term_id' => $term_id]); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } else { |
| 105 | if (count($folders)) { |
| 106 | if ($plugin === 'filebird') { |
| 107 | $wpdb->query('DELETE FROM '.$wpdb->prefix.'fbv'); |
| 108 | } |
| 109 | |
| 110 | if ($plugin === 'real-media-library') { |
| 111 | $wpdb->query('DELETE FROM '.$wpdb->prefix.'realmedialibrary'); |
| 112 | |
| 113 | $wpdb->query('DELETE FROM '.$wpdb->prefix.'realmedialibrary_meta'); |
| 114 | } |
| 115 | |
| 116 | if ($plugin === 'catfolders') { |
| 117 | $wpdb->query('DELETE FROM '.$wpdb->prefix.'catfolders'); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if (count($attachments)) { |
| 122 | if ($plugin === 'filebird') { |
| 123 | $wpdb->query('DELETE FROM '.$wpdb->prefix.'fbv_attachment_folder'); |
| 124 | } |
| 125 | |
| 126 | if ($plugin === 'real-media-library') { |
| 127 | $wpdb->query('DELETE FROM '.$wpdb->prefix.'realmedialibrary_posts'); |
| 128 | } |
| 129 | |
| 130 | if ($plugin === 'catfolders') { |
| 131 | $wpdb->query('DELETE FROM '.$wpdb->prefix.'catfolders_posts'); |
| 132 | } |
| 133 | } |
| 134 | }//end if |
| 135 | |
| 136 | $response['status'] = 1; |
| 137 | }//end if |
| 138 | |
| 139 | echo wp_json_encode($response); |
| 140 | exit; |
| 141 | |
| 142 | }//end remove_plugin_folders_data() |
| 143 | |
| 144 | |
| 145 | public function filter_categories_by_term_id($attachments) |
| 146 | { |
| 147 | $termsData = []; |
| 148 | foreach ($attachments as $attachment) { |
| 149 | $termsData[$attachment->folder_id][] = $attachment->attachment_id; |
| 150 | } |
| 151 | |
| 152 | return $termsData; |
| 153 | |
| 154 | }//end filter_categories_by_term_id() |
| 155 | |
| 156 | |
| 157 | public function import_plugin_folders_data() |
| 158 | { |
| 159 | |
| 160 | global $wpdb; |
| 161 | |
| 162 | $postData = filter_input_array(INPUT_POST); |
| 163 | |
| 164 | $paged = isset($postData['paged']) && is_numeric($postData['paged']) && $postData['paged'] > 0 ? intval(sanitize_text_field($postData['paged'])) : 1; |
| 165 | $attachedItems = isset($postData['attached']) && is_numeric($postData['attached']) && $postData['attached'] > 0 ? intval(sanitize_text_field($postData['attached'])) : 0; |
| 166 | $startFrom = (10 * ($paged - 1)); |
| 167 | $endFolder = (10 * $paged); |
| 168 | $totalFolders = 0; |
| 169 | |
| 170 | $plugin = isset($postData['plugin']) ? $postData['plugin'] : ""; |
| 171 | $nonce = isset($postData['nonce']) ? $postData['nonce'] : ""; |
| 172 | |
| 173 | $dataSet = []; |
| 174 | $response = []; |
| 175 | $response['status'] = 0; |
| 176 | $response['message'] = esc_html__("Invalid request", "folders"); |
| 177 | $response['data'] = []; |
| 178 | $response['data']['plugin'] = $plugin; |
| 179 | if (wp_verify_nonce($nonce, "import_data_from_".$plugin)) { |
| 180 | $this->get_plugin_information(); |
| 181 | $folders = isset($this->plugins[$plugin]['folders']) ? $this->plugins[$plugin]['folders'] : []; |
| 182 | $totalFolders = count($folders); |
| 183 | $attachments = isset($this->plugins[$plugin]['attachments']) ? $this->plugins[$plugin]['attachments'] : []; |
| 184 | |
| 185 | $categoryByID = []; |
| 186 | $foldersImported = []; |
| 187 | $attachmentsImported = []; |
| 188 | if ($plugin != 'filebird' && $plugin != 'real-media-library' && $plugin != "catfolders") { |
| 189 | $currentFolder = -1; |
| 190 | foreach ($folders as $folder) { |
| 191 | $currentFolder++; |
| 192 | $folder_id = $folder->term_id; |
| 193 | $parent = intval($folder->parent); |
| 194 | |
| 195 | $taxonomy = 'media_folder'; |
| 196 | |
| 197 | foreach ($this->post_types as $post_type) { |
| 198 | if (strpos($folder->taxonomy, $post_type) !== false) { |
| 199 | if ($post_type == "post") { |
| 200 | $taxonomy = "post_folder"; |
| 201 | } else if ($post_type == "page") { |
| 202 | $taxonomy = "folder"; |
| 203 | } else if ($post_type == "attachment") { |
| 204 | $taxonomy = "media_folder"; |
| 205 | } else { |
| 206 | $taxonomy = $post_type.'_folder'; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if ($parent && isset($categoryByID[$parent]['term_id'])) { |
| 212 | $parent = intval($categoryByID[$parent]['term_id']); |
| 213 | } |
| 214 | |
| 215 | $new_term = wp_insert_term($folder->name, $taxonomy, ['parent' => $parent]); |
| 216 | |
| 217 | if (is_wp_error($new_term)) { |
| 218 | if (isset($new_term->errors['term_exists']) && $new_term->error_data['term_exists']) { |
| 219 | $termId = $new_term->error_data['term_exists']; |
| 220 | $termData = get_term($termId, "", ARRAY_A); |
| 221 | if (!empty($termData)) { |
| 222 | $new_term = $termData; |
| 223 | } else { |
| 224 | continue; |
| 225 | } |
| 226 | } else { |
| 227 | continue; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | $arg = [ |
| 232 | 'taxonomy' => $taxonomy, |
| 233 | 'hide_empty' => false, |
| 234 | 'parent' => $parent, |
| 235 | 'hierarchical' => false, |
| 236 | 'update_count_callback' => '_update_generic_term_count', |
| 237 | ]; |
| 238 | $terms = get_terms($arg); |
| 239 | $position = count($terms); |
| 240 | |
| 241 | if ($plugin == 'mediamatic' || $plugin == 'happyfiles') { |
| 242 | $meta_key = ""; |
| 243 | if ($plugin == 'mediamatic') { |
| 244 | $meta_key = "folder_position"; |
| 245 | } else if ($plugin == 'happyfiles') { |
| 246 | $meta_key = "happyfiles_position"; |
| 247 | } |
| 248 | |
| 249 | if (!empty($meta_key)) { |
| 250 | $folder_position = get_term_meta($new_term['term_id'], $meta_key, true); |
| 251 | if (empty($folder_position)) { |
| 252 | $position = intval($folder_position); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | update_term_meta($new_term['term_id'], 'wcp_custom_order', $position); |
| 258 | |
| 259 | $foldersImported[] = $new_term; |
| 260 | |
| 261 | $categoryByID[$folder_id] = [ |
| 262 | 'term_id' => $new_term['term_id'], |
| 263 | 'parent' => $parent, |
| 264 | 'name' => $folder->name, |
| 265 | ]; |
| 266 | |
| 267 | $newTermID = $new_term['term_id']; |
| 268 | $folderItems = 0; |
| 269 | $failedItems = 0; |
| 270 | $query = "SELECT object_id FROM ".$wpdb->term_relationships." WHERE term_taxonomy_id = %d AND object_id != 1"; |
| 271 | $query = $wpdb->prepare($query, $folder->term_taxonomy_id); |
| 272 | $results = $wpdb->get_results($query); |
| 273 | $found_results = count($results); |
| 274 | if ($currentFolder >= $startFrom && $currentFolder < $endFolder) { |
| 275 | if (count($results)) { |
| 276 | foreach ($results as $result) { |
| 277 | $term_set = wp_set_object_terms($result->object_id, $newTermID, $taxonomy, true); |
| 278 | |
| 279 | if (is_wp_error($term_set)) { |
| 280 | $failedItems++; |
| 281 | continue; |
| 282 | } |
| 283 | |
| 284 | $folderItems++; |
| 285 | |
| 286 | $attachmentsImported[] = [ |
| 287 | 'cat_id' => $newTermID, |
| 288 | 'term_ids' => $result->object_id, |
| 289 | 'set' => $term_set, |
| 290 | ]; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | $dataSet[] = [ |
| 295 | 'id' => $newTermID, |
| 296 | 'old_id' => $folder->term_taxonomy_id, |
| 297 | 'name' => $folder->name, |
| 298 | 'items' => $folderItems, |
| 299 | 'failed' => $failedItems, |
| 300 | 'results' => $found_results, |
| 301 | ]; |
| 302 | }//end if |
| 303 | }//end foreach |
| 304 | } else { |
| 305 | $attachments = $this->filter_categories_by_term_id($attachments); |
| 306 | $currentFolder = -1; |
| 307 | foreach ($folders as $folder) { |
| 308 | $currentFolder++; |
| 309 | $parent = intval($folder->parent); |
| 310 | if ($parent == -1) { |
| 311 | $parent = 0; |
| 312 | } |
| 313 | |
| 314 | $parentID = 0; |
| 315 | |
| 316 | if ($parent && isset($categoryByID[$parent]['term_id'])) { |
| 317 | $parentID = $categoryByID[$parent]['term_id']; |
| 318 | } |
| 319 | |
| 320 | $new_term = wp_insert_term($folder->name, "media_folder", ['parent' => $parentID]); |
| 321 | |
| 322 | if (is_wp_error($new_term)) { |
| 323 | if (isset($new_term->errors['term_exists']) && $new_term->error_data['term_exists']) { |
| 324 | $termId = $new_term->error_data['term_exists']; |
| 325 | $termData = get_term($termId, "", ARRAY_A); |
| 326 | if (!empty($termData)) { |
| 327 | $new_term = $termData; |
| 328 | } else { |
| 329 | continue; |
| 330 | } |
| 331 | } else { |
| 332 | continue; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | $taxonomy = 'media_folder'; |
| 337 | |
| 338 | $arg = [ |
| 339 | 'taxonomy' => $taxonomy, |
| 340 | 'hide_empty' => false, |
| 341 | 'parent' => $parentID, |
| 342 | 'hierarchical' => false, |
| 343 | 'update_count_callback' => '_update_generic_term_count', |
| 344 | ]; |
| 345 | $terms = get_terms($arg); |
| 346 | $position = count($terms); |
| 347 | |
| 348 | update_term_meta($new_term['term_id'], 'wcp_custom_order', intval($position)); |
| 349 | |
| 350 | $foldersImported[] = $new_term; |
| 351 | |
| 352 | $categoryByID[$folder->id] = [ |
| 353 | 'name' => $folder->name, |
| 354 | 'parent' => $parent, |
| 355 | 'term_id' => $new_term['term_id'], |
| 356 | ]; |
| 357 | |
| 358 | $newTermID = $new_term['term_id']; |
| 359 | $folderItems = 0; |
| 360 | $failedItems = 0; |
| 361 | $found_results = 0; |
| 362 | if ($currentFolder >= $startFrom && $currentFolder < $endFolder) { |
| 363 | if (isset($attachments[$folder->id]) && count($attachments[$folder->id]) > 0) { |
| 364 | foreach ($attachments[$folder->id] as $result) { |
| 365 | $term_set = wp_set_object_terms($result, $newTermID, $taxonomy, true); |
| 366 | |
| 367 | if (is_wp_error($term_set)) { |
| 368 | $failedItems++; |
| 369 | continue; |
| 370 | } |
| 371 | |
| 372 | $folderItems++; |
| 373 | |
| 374 | $attachmentsImported[] = [ |
| 375 | 'cat_id' => $newTermID, |
| 376 | 'term_ids' => $result->object_id, |
| 377 | 'set' => $term_set, |
| 378 | ]; |
| 379 | } |
| 380 | |
| 381 | $dataSet[] = [ |
| 382 | 'id' => $newTermID, |
| 383 | 'old_id' => $folder->id, |
| 384 | 'name' => $folder->name, |
| 385 | 'items' => $folderItems, |
| 386 | 'failed' => $failedItems, |
| 387 | 'results' => $found_results, |
| 388 | ]; |
| 389 | }//end if |
| 390 | }//end if |
| 391 | }//end foreach |
| 392 | }//end if |
| 393 | |
| 394 | delete_transient("premio_folders_without_trash"); |
| 395 | |
| 396 | $totalPages = ceil($totalFolders / 10); |
| 397 | $response['status'] = 1; |
| 398 | $response['data']['imported'] = count($foldersImported); |
| 399 | $response['data']['attachments'] = (count($attachmentsImported) + $attachedItems); |
| 400 | $response['data']['data_set'] = $dataSet; |
| 401 | $response['data']['folders'] = $totalFolders; |
| 402 | $response['data']['pages'] = $totalPages; |
| 403 | $response['data']['current'] = $paged; |
| 404 | $response['data']['plugin'] = $plugin; |
| 405 | $response['message'] = sprintf(esc_html__("%1\$s folders imported and %2\$s attachments categorized.", 'folders'), count($foldersImported), (count($attachmentsImported) + $attachedItems)); |
| 406 | }//end if |
| 407 | |
| 408 | echo wp_json_encode($response); |
| 409 | exit; |
| 410 | |
| 411 | }//end import_plugin_folders_data() |
| 412 | |
| 413 | |
| 414 | /** |
| 415 | * Get installed Plugins list |
| 416 | * |
| 417 | * @since 1.0.0 |
| 418 | * @access public |
| 419 | * @return $plugins |
| 420 | */ |
| 421 | public function get_plugin_information() |
| 422 | { |
| 423 | $this->get_other_plugins_data(); |
| 424 | return $this->plugins; |
| 425 | |
| 426 | }//end get_plugin_information() |
| 427 | |
| 428 | |
| 429 | /** |
| 430 | * Get installed Plugins list to Import data |
| 431 | * |
| 432 | * @since 1.0.0 |
| 433 | * @access public |
| 434 | * @return $plugins |
| 435 | */ |
| 436 | public function get_other_plugins_data() |
| 437 | { |
| 438 | if (!empty($this->plugins)) { |
| 439 | return $this->plugins; |
| 440 | } |
| 441 | |
| 442 | $this->plugins = [ |
| 443 | // FileBird |
| 444 | 'filebird' => [ |
| 445 | 'name' => 'FileBird (v4)', |
| 446 | 'taxonomy' => 'filebird', |
| 447 | // has custom DB table |
| 448 | 'folders' => [], |
| 449 | 'attachments' => [], |
| 450 | 'total_folders' => 0, |
| 451 | 'total_attachments' => 0, |
| 452 | 'is_exists' => 0, |
| 453 | ], |
| 454 | 'enhanced-media-library' => [ |
| 455 | // Enhanced Media Library |
| 456 | 'name' => 'Enhanced Media Library', |
| 457 | 'taxonomy' => 'media_category', |
| 458 | 'folders' => [], |
| 459 | 'attachments' => [], |
| 460 | 'total_folders' => 0, |
| 461 | 'total_attachments' => 0, |
| 462 | 'is_exists' => 0, |
| 463 | ], |
| 464 | 'wicked-folders' => [ |
| 465 | // Wicked Folders |
| 466 | 'name' => 'Wicked Folders', |
| 467 | 'taxonomy' => 'wf_attachment_folders', |
| 468 | 'folders' => [], |
| 469 | 'attachments' => [], |
| 470 | 'total_folders' => 0, |
| 471 | 'total_attachments' => 0, |
| 472 | 'is_exists' => 0, |
| 473 | ], |
| 474 | 'real-media-library' => [ |
| 475 | // Real Media Library |
| 476 | 'name' => 'Real Media Library (by DevOwl)', |
| 477 | 'taxonomy' => 'rml', |
| 478 | // has custom DB table |
| 479 | 'folders' => [], |
| 480 | 'attachments' => [], |
| 481 | 'total_folders' => 0, |
| 482 | 'total_attachments' => 0, |
| 483 | 'is_exists' => 0, |
| 484 | ], |
| 485 | 'wp-media-folder' => [ |
| 486 | // WP Media Folder |
| 487 | 'name' => 'WP Media Folder (by JoomUnited)', |
| 488 | 'taxonomy' => 'wpmf-category', |
| 489 | 'folders' => [], |
| 490 | 'attachments' => [], |
| 491 | 'total_folders' => 0, |
| 492 | 'total_attachments' => 0, |
| 493 | 'is_exists' => 0, |
| 494 | ], |
| 495 | 'mediamatic' => [ |
| 496 | // Mediamatic |
| 497 | 'name' => 'WordPress Media Library Folders | Mediamatic', |
| 498 | 'taxonomy' => 'mediamatic_wpfolder', |
| 499 | 'folders' => [], |
| 500 | 'attachments' => [], |
| 501 | 'total_folders' => 0, |
| 502 | 'total_attachments' => 0, |
| 503 | 'is_exists' => 0, |
| 504 | ], |
| 505 | 'happyfiles' => [ |
| 506 | // HappyFiles |
| 507 | 'name' => 'HappyFiles', |
| 508 | 'taxonomy' => 'happyfiles_category', |
| 509 | 'folders' => [], |
| 510 | 'attachments' => [], |
| 511 | 'total_folders' => 0, |
| 512 | 'total_attachments' => 0, |
| 513 | 'is_exists' => 0, |
| 514 | ], |
| 515 | 'catfolders' => [ |
| 516 | 'name' => 'CatFolders Lite - WP Media Folders', |
| 517 | 'taxonomy' => 'catfolders', |
| 518 | 'folders' => [], |
| 519 | 'attachments' => [], |
| 520 | 'total_folders' => 0, |
| 521 | 'total_attachments' => 0, |
| 522 | 'is_exists' => 0, |
| 523 | ], |
| 524 | 'media_library_categories' => [ |
| 525 | 'name' => 'Media Library Categories', |
| 526 | 'taxonomy' => 'category', |
| 527 | 'folders' => [], |
| 528 | 'attachments' => [], |
| 529 | 'total_folders' => 0, |
| 530 | 'total_attachments' => 0, |
| 531 | 'is_exists' => 0, |
| 532 | ] |
| 533 | ]; |
| 534 | $DS = DIRECTORY_SEPARATOR; |
| 535 | $dirName = ABSPATH."wp-content{$DS}plugins{$DS}wp-media-library-categories{$DS}"; |
| 536 | if (is_dir($dirName)) { |
| 537 | $settings = get_option("wpmlc_settings"); |
| 538 | $category = isset($settings['wpmediacategory_taxonomy'])&&!empty($settings['wpmediacategory_taxonomy'])?$settings['wpmediacategory_taxonomy']:'category'; |
| 539 | $this->plugins['media_library_categories']['category'] = esc_attr($category); |
| 540 | } |
| 541 | |
| 542 | $post_types = get_post_types([]); |
| 543 | $this->post_types = array_keys($post_types); |
| 544 | |
| 545 | foreach ($this->plugins as $slug => $plugin_data) { |
| 546 | $taxonomy = $plugin_data['taxonomy']; |
| 547 | |
| 548 | if ($slug === 'wicked-folders') { |
| 549 | // Run for all registered post types |
| 550 | $folders = []; |
| 551 | |
| 552 | foreach ($this->post_types as $post_type) { |
| 553 | $wicked_folders = $this->get_plugin_folders('wf_'.$post_type.'_folders', $slug); |
| 554 | |
| 555 | if (is_array($wicked_folders)) { |
| 556 | $folders = array_merge($folders, $wicked_folders); |
| 557 | } |
| 558 | } |
| 559 | } else { |
| 560 | $folders = $this->get_plugin_folders($taxonomy, $slug); |
| 561 | } |
| 562 | |
| 563 | if (in_array($taxonomy, ['filebird', 'rml','catfolders'])) { |
| 564 | $folders = is_array($folders) && count($folders) ? $this->map_plugin_folders($taxonomy, $folders) : []; |
| 565 | } |
| 566 | |
| 567 | $this->plugins[$slug]['folders'] = $folders; |
| 568 | |
| 569 | $attachments = is_array($folders) && count($folders) ? $this->get_plugin_attachments($taxonomy, $folders) : []; |
| 570 | |
| 571 | if (in_array($taxonomy, ['filebird', 'rml','catfolders'])) { |
| 572 | $attachments = $this->map_plugin_attachments($taxonomy, $attachments); |
| 573 | } |
| 574 | |
| 575 | $this->plugins[$slug]['attachments'] = $attachments; |
| 576 | }//end foreach |
| 577 | |
| 578 | foreach ($this->plugins as $key => $plugin) { |
| 579 | $folders = isset($plugin['folders'])&&is_array($plugin['folders']) ? $plugin['folders'] : []; |
| 580 | $this->plugins[$key]['total_folders'] = count($folders); |
| 581 | |
| 582 | $attachments = isset($plugin['attachments'])&&is_array($plugin['attachments']) ? $plugin['attachments'] : []; |
| 583 | $this->plugins[$key]['total_attachments'] = count($attachments); |
| 584 | |
| 585 | if (count($folders) > 0 || count($attachments) > 0) { |
| 586 | $this->plugins[$key]['is_exists'] = 1; |
| 587 | $this->is_exists = 1; |
| 588 | } |
| 589 | } |
| 590 | }//end get_other_plugins_data() |
| 591 | |
| 592 | |
| 593 | /** |
| 594 | * Get Folders Data From Imported Plugins |
| 595 | * |
| 596 | * @since 1.0.0 |
| 597 | * @access public |
| 598 | * @return $taxonomy |
| 599 | */ |
| 600 | public function get_plugin_folders($taxonomy, $slug) |
| 601 | { |
| 602 | global $wpdb; |
| 603 | |
| 604 | // FileBird has its own db table |
| 605 | if ($taxonomy === 'filebird') { |
| 606 | $filebird_folders_table = $wpdb->prefix.'fbv'; |
| 607 | |
| 608 | // Get FileBird folders (order by 'parent' to create parent categories first) |
| 609 | if ($wpdb->get_var("SHOW TABLES LIKE '$filebird_folders_table'") == $filebird_folders_table) { |
| 610 | return $wpdb->get_results("SELECT * FROM $filebird_folders_table ORDER BY parent ASC"); |
| 611 | } else { |
| 612 | $taxonomy = "nt_wmc_folder"; |
| 613 | |
| 614 | $query = "SELECT * FROM ".$wpdb->term_taxonomy." |
| 615 | LEFT JOIN ".$wpdb->terms." |
| 616 | ON ".$wpdb->term_taxonomy.".term_id = ".$wpdb->terms.".term_id |
| 617 | WHERE ".$wpdb->term_taxonomy.".taxonomy = '%s' |
| 618 | ORDER BY parent ASC"; |
| 619 | $query = $wpdb->prepare($query, $taxonomy); |
| 620 | $folders = $wpdb->get_results($query); |
| 621 | |
| 622 | // WP Media Folder (JoomUnited): Remove root folder |
| 623 | if ($slug === 'wp-media-folder') { |
| 624 | foreach ($folders as $index => $folder) { |
| 625 | if ($folder->slug === 'wp-media-folder-root') { |
| 626 | unset($folders[$index]); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | return array_values($folders); |
| 632 | } |
| 633 | } else if ($taxonomy === 'catfolders') { |
| 634 | $filebird_folders_table = $wpdb->prefix.'catfolders'; |
| 635 | |
| 636 | // Get FileBird folders (order by 'parent' to create parent categories first) |
| 637 | if ($wpdb->get_var("SHOW TABLES LIKE '$filebird_folders_table'") == $filebird_folders_table) { |
| 638 | return $wpdb->get_results("SELECT * FROM $filebird_folders_table ORDER BY parent ASC"); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | // Real Media Library has its own db table |
| 643 | else if ($taxonomy === 'rml') { |
| 644 | $rml_folders_table = $wpdb->prefix.'realmedialibrary'; |
| 645 | |
| 646 | // Get FileBird folders (order by 'parent' to create parent categories first) |
| 647 | if ($wpdb->get_var("SHOW TABLES LIKE '$rml_folders_table'") == $rml_folders_table) { |
| 648 | return $wpdb->get_results("SELECT * FROM $rml_folders_table ORDER BY parent ASC"); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | // Default: Plugins with custom taxonomy terms |
| 653 | else { |
| 654 | $query = "SELECT * FROM ".$wpdb->term_taxonomy." |
| 655 | LEFT JOIN ".$wpdb->terms." |
| 656 | ON ".$wpdb->term_taxonomy.".term_id = ".$wpdb->terms.".term_id |
| 657 | WHERE ".$wpdb->term_taxonomy.".taxonomy = '%s' |
| 658 | ORDER BY parent ASC"; |
| 659 | $query = $wpdb->prepare($query, $taxonomy); |
| 660 | $folders = $wpdb->get_results($query); |
| 661 | |
| 662 | // WP Media Folder (JoomUnited): Remove root folder |
| 663 | if ($slug === 'wp-media-folder') { |
| 664 | foreach ($folders as $index => $folder) { |
| 665 | if ($folder->slug === 'wp-media-folder-root') { |
| 666 | unset($folders[$index]); |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | return array_values($folders); |
| 672 | } |
| 673 | |
| 674 | }//end get_plugin_folders() |
| 675 | |
| 676 | |
| 677 | /** |
| 678 | * Get Folders Data From Imported Plugins |
| 679 | * |
| 680 | * @since 1.0.0 |
| 681 | * @access public |
| 682 | * @return $taxonomy |
| 683 | */ |
| 684 | public function map_plugin_folders($taxonomy, $folders) |
| 685 | { |
| 686 | $mapped_folders = []; |
| 687 | |
| 688 | foreach ($folders as $folder) { |
| 689 | // FileBird, Real Media Library |
| 690 | if ($taxonomy === 'filebird' || $taxonomy === 'rml' || $taxonomy === "catfolders") { |
| 691 | $folderObj = new \stdClass(); |
| 692 | |
| 693 | if($taxonomy == "catfolders") { |
| 694 | $folderObj->name = $folder->title; |
| 695 | } else { |
| 696 | $folderObj->name = $folder->name; |
| 697 | } |
| 698 | $folderObj->id = intval($folder->id); |
| 699 | $folderObj->parent = intval($folder->parent); |
| 700 | $folderObj->position = intval($folder->ord); |
| 701 | |
| 702 | $mapped_folders[] = $folderObj; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return $mapped_folders; |
| 707 | |
| 708 | }//end map_plugin_folders() |
| 709 | |
| 710 | |
| 711 | /** |
| 712 | * Save Folders and it's attachment Data From Imported Plugins |
| 713 | * |
| 714 | * @since 1.0.0 |
| 715 | * @access public |
| 716 | * @return $files |
| 717 | */ |
| 718 | public function map_plugin_attachments($taxonomy, $attachments) |
| 719 | { |
| 720 | $mapped_attachments = []; |
| 721 | |
| 722 | foreach ($attachments as $folder) { |
| 723 | // FileBird |
| 724 | if ($taxonomy === 'filebird') { |
| 725 | $folderObj = new \stdClass(); |
| 726 | |
| 727 | $folderObj->folder_id = intval($folder->folder_id); |
| 728 | $folderObj->attachment_id = intval($folder->attachment_id); |
| 729 | |
| 730 | $mapped_attachments[] = $folderObj; |
| 731 | } |
| 732 | |
| 733 | // Real Media Library |
| 734 | if ($taxonomy === 'rml') { |
| 735 | $folderObj = new \stdClass(); |
| 736 | |
| 737 | $folderObj->folder_id = intval($folder->fid); |
| 738 | $folderObj->attachment_id = intval($folder->attachment); |
| 739 | |
| 740 | $mapped_attachments[] = $folderObj; |
| 741 | } |
| 742 | |
| 743 | if($taxonomy === "catfolders") { |
| 744 | $folderObj = new \stdClass(); |
| 745 | |
| 746 | $folderObj->folder_id = intval($folder->folder_id); |
| 747 | $folderObj->attachment_id = intval($folder->post_id); |
| 748 | |
| 749 | $mapped_attachments[] = $folderObj; |
| 750 | } |
| 751 | }//end foreach |
| 752 | |
| 753 | return $mapped_attachments; |
| 754 | |
| 755 | }//end map_plugin_attachments() |
| 756 | |
| 757 | |
| 758 | /** |
| 759 | * Get Folders and it's attachment Data From Imported Plugins |
| 760 | * |
| 761 | * @since 1.0.0 |
| 762 | * @access public |
| 763 | * @return $files |
| 764 | */ |
| 765 | public function get_plugin_attachments($taxonomy, $folders) |
| 766 | { |
| 767 | global $wpdb; |
| 768 | |
| 769 | // FileBird has its own db table |
| 770 | if ($taxonomy === 'filebird') { |
| 771 | $filebirdTable = $wpdb->prefix.'fbv_attachment_folder'; |
| 772 | |
| 773 | // Get FileBird attachments |
| 774 | if ($wpdb->get_var("SHOW TABLES LIKE '{$filebirdTable}'") == $filebirdTable) { |
| 775 | return $wpdb->get_results("SELECT * FROM {$filebirdTable} ORDER BY folder_id ASC"); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | else if ($taxonomy === 'catfolders') { |
| 780 | $filebirdTable = $wpdb->prefix.'catfolders_posts'; |
| 781 | |
| 782 | // Get FileBird attachments |
| 783 | if ($wpdb->get_var("SHOW TABLES LIKE '{$filebirdTable}'") == $filebirdTable) { |
| 784 | return $wpdb->get_results("SELECT * FROM {$filebirdTable} ORDER BY folder_id ASC"); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | // Real Media Library has its own db table |
| 789 | else if ($taxonomy === 'rml') { |
| 790 | $rmlTable = $wpdb->prefix.'realmedialibrary_posts'; |
| 791 | |
| 792 | // Get Data from Real Media Library DB Table |
| 793 | if ($wpdb->get_var("SHOW TABLES LIKE '{$rmlTable}'") == $rmlTable) { |
| 794 | return $wpdb->get_results("SELECT * FROM {$rmlTable} ORDER BY fid ASC"); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | // Default: Plugins with custom taxonomy terms |
| 799 | else { |
| 800 | return $wpdb->get_results( |
| 801 | "SELECT TR.object_id, |
| 802 | TR.term_taxonomy_id |
| 803 | FROM ".$wpdb->term_relationships." AS TR |
| 804 | WHERE TR.object_id != 1 && TR.term_taxonomy_id IN (".implode(',', array_column($folders, 'term_taxonomy_id')).")" |
| 805 | ); |
| 806 | } |
| 807 | |
| 808 | }//end get_plugin_attachments() |
| 809 | |
| 810 | |
| 811 | }//end class |
| 812 | |
| 813 | $WCP_Folder_Plugins = new WCP_Folder_Plugins(); |
| 814 |