PluginProbe
CatFolders – WordPress Media Library Folders & Categories / trunk
CatFolders – WordPress Media Library Folders & Categories vtrunk
2.5.6 2.5.5 trunk 1.6.1 1.8 1.9 2.0 2.2 2.3.1 2.3.2 2.4.4 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4
catfolders / includes / Rest / Controllers / ImportController.php

ImportController.php in CatFolders – WordPress Media Library Folders & Categories trunk, at includes/Rest/Controllers/ImportController.php

538 lines 16.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace CatFolders\Rest\Controllers;
3
4 use CatFolders\Models\FolderModel;
5 use CatFolders\Classes\Helpers;
6
7 defined( 'ABSPATH' ) || exit;
8
9 class ImportController {
10 private $plugins_to_import = array();
11
12 public function __construct() {
13 $this->plugins_to_import = array(
14 'FB' => array(
15 'plugin_name' => 'FileBird',
16 'author' => 'NinjaTeam',
17 'folder_counter' => 0,
18 'custom_db' => 'fbv',
19 'imported' => false,
20 ),
21 'WF' => array(
22 'plugin_name' => 'Wicked Folders',
23 'author' => 'Wicked Plugins',
24 'folder_counter' => 0,
25 'taxonomy' => 'wf_attachment_folders',
26 'imported' => false,
27 ),
28 'EML' => array(
29 'plugin_name' => 'Enhanced Media Library',
30 'author' => 'wpUXsolutions',
31 'folder_counter' => 0,
32 'taxonomy' => 'media_category',
33 'imported' => false,
34 ),
35 'MLA' => array(
36 'plugin_name' => 'Media Library Assistant',
37 'author' => 'David Lingren',
38 'folder_counter' => 0,
39 'taxonomy' => 'attachment_category',
40 'imported' => false,
41 ),
42
43 'WMLF' => array(
44 'plugin_name' => 'WordPress Media Library Folders',
45 'author' => 'Max Foundry',
46 'folder_counter' => 0,
47 'post_type' => 'mgmlp_media_folder',
48 'imported' => false,
49 ),
50
51 'WPMF' => array(
52 'plugin_name' => 'WP Media folder',
53 'author' => 'Joomunited',
54 'folder_counter' => 0,
55 'taxonomy' => 'wpmf-category',
56 'imported' => false,
57 ),
58
59 'RML' => array(
60 'plugin_name' => 'WP Real Media Library',
61 'author' => 'devowl.io',
62 'folder_counter' => 0,
63 'custom_db' => 'realmedialibrary',
64 'imported' => false,
65 ),
66
67 'HP' => array(
68 'plugin_name' => 'HappyFiles',
69 'author' => 'Codeer',
70 'prefix' => 'HP',
71 'folder_counter' => 0,
72 'taxonomy' => 'happyfiles_category',
73 'imported' => false,
74 ),
75
76 'Folders' => array(
77 'plugin_name' => 'Folders',
78 'author' => 'Premio',
79 'folder_counter' => 0,
80 'taxonomy' => 'media_folder',
81 'imported' => false,
82 ),
83 );
84 }
85
86 public function detect_import() {
87 $plugin_can_import = array();
88
89 foreach ( $this->plugins_to_import as $key => $plugin ) {
90 $has_import = get_option( $this->get_prefix_option( $key, 'success_import' ) );
91
92 $counter = $this->get_folder_counter( $key );
93 if ( \intval( $counter ) > 0 ) {
94 $this->plugins_to_import[ $key ]['folder_counter'] = $has_import ? -1 : \intval( $counter );
95 $this->plugins_to_import[ $key ]['prefix'] = $key;
96 array_push( $plugin_can_import, $this->plugins_to_import[ $key ] );
97 }
98 }
99
100 return $plugin_can_import;
101 }
102
103 public function get_folder_counter( $prefix ) {
104 global $wpdb;
105 if ( in_array( $prefix, array( 'EML', 'HP', 'Folders', 'WPMF', 'MLA', 'WF' ), true ) ) {
106 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(term_taxonomy_id) FROM $wpdb->term_taxonomy WHERE taxonomy = %s", $this->plugins_to_import[ $prefix ]['taxonomy'] ) );
107 }
108
109 if ( 'WMLF' === $prefix ) {
110 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = %s", $this->plugins_to_import[ $prefix ]['post_type'] ) );
111 }
112
113 if ( 'RML' === $prefix || 'FB' === $prefix ) {
114 $table = $wpdb->prefix . $this->plugins_to_import[ $prefix ]['custom_db'];
115 if ( $this->table_exist( $table ) ) {
116 return $wpdb->get_var( 'SELECT COUNT(id) FROM ' . $table );
117 }
118 return 0;
119 }
120 }
121
122 public function register_routes() {
123 register_rest_route(
124 CATF_ROUTE_NAMESPACE,
125 '/import/get-folder-structure/(?P<prefix>[a-zA-Z]+)',
126 array(
127 'methods' => \WP_REST_Server::READABLE,
128 'callback' => array( $this, 'get_folder_structure' ),
129 'permission_callback' => array( $this, 'permission_callback' ),
130 )
131 );
132
133 register_rest_route(
134 CATF_ROUTE_NAMESPACE,
135 '/import/get-attachment-in-folder/(?P<prefix>[a-zA-Z]+)',
136 array(
137 'methods' => \WP_REST_Server::READABLE,
138 'callback' => array( $this, 'get_attachment_in_folder' ),
139 'permission_callback' => array( $this, 'permission_callback' ),
140 )
141 );
142
143 register_rest_route(
144 CATF_ROUTE_NAMESPACE,
145 '/import/process',
146 array(
147 'methods' => \WP_REST_Server::CREATABLE,
148 'callback' => array( $this, 'process' ),
149 'permission_callback' => array( $this, 'permission_callback' ),
150 )
151 );
152
153 register_rest_route(
154 CATF_ROUTE_NAMESPACE,
155 '/clean-db',
156 array(
157 'methods' => \WP_REST_Server::CREATABLE,
158 'callback' => array( $this, 'clean_db' ),
159 'permission_callback' => array( $this, 'permission_callback' ),
160 )
161 );
162
163 register_rest_route(
164 CATF_ROUTE_NAMESPACE,
165 '/import-csv',
166 array(
167 'methods' => \WP_REST_Server::CREATABLE,
168 'callback' => array( $this, 'import_csv' ),
169 'permission_callback' => array( $this, 'permission_callback' ),
170 )
171 );
172
173 }
174
175 public function permission_callback() {
176 return current_user_can( 'upload_files' );
177 }
178
179 public function import_csv( \WP_REST_Request $request ) {
180 if( ! current_user_can( 'manage_options' ) ) {
181 return new \WP_Error( 403, __( 'You are not allowed to import CSV.', 'catfolders' ) );
182 }
183 $params = $request->get_file_params();
184 $handle = \fopen( $params['file']['tmp_name'], 'r' );
185 $data = array();
186 $columns = array();
187 $bom = "\xef\xbb\xbf";
188 if ( false !== $handle ) {
189 $count = 1;
190 while ( 1 ) {
191 $row = fgetcsv( $handle, 0 );
192 if ( 1 === $count ) {
193 $columns = $row;
194 $count++;
195 continue;
196 }
197 if ( false === $row ) {
198 break;
199 }
200 foreach ( $columns as $key => $col ) {
201 $tmp[ $col ] = $row[ $key ];
202 }
203 $data[] = $tmp;
204 }
205 }
206 \fclose( $handle );
207
208 $this->restore_folders( $data );
209
210 return new \WP_REST_Response( array( 'success' => true ) );
211 }
212
213 public function get_prefix_option( $prefix = '', $option_name = '' ) {
214 $catfPrefix = '_catf_';
215
216 return $catfPrefix . $prefix . '_' . $option_name;
217 }
218
219 public function get_folder_structure( \WP_REST_Request $request ) {
220 if( ! current_user_can( 'manage_options' ) ) {
221 return new \WP_Error( 403, __( 'You are not allowed to get folder structure.', 'catfolders' ) );
222 }
223 $prefix = Helpers::sanitize_array( $request->get_param( 'prefix' ) );
224
225 if ( isset( $this->plugins_to_import[ $prefix ] ) ) {
226 if ( in_array( $prefix, array( 'EML', 'HP', 'Folders', 'WPMF', 'MLA', 'WF' ), true ) ) {
227 $folders = $this->get_term_folders( 0, $this->plugins_to_import[ $prefix ]['taxonomy'] );
228 }
229
230 if ( 'WMLF' === $prefix ) {
231 $folders = $this->get_wmlf_folders( 0, $this->plugins_to_import[ $prefix ]['post_type'] );
232 }
233
234 if ( 'RML' === $prefix ) {
235 $folders = $this->get_rml_folders( -1, $this->plugins_to_import[ $prefix ]['custom_db'] );
236 }
237
238 if ( 'FB' === $prefix ) {
239 $folders = $this->get_fb_folders( 0, $this->plugins_to_import[ $prefix ]['custom_db'] );
240 }
241
242 update_option( $this->get_prefix_option( $prefix, 'folders_import' ), $folders, 'no' );
243 return new \WP_REST_Response( array( 'result' => true ) );
244 }
245
246 return new \WP_Error( '500', __( 'Plugin does not support!', 'catfolders' ) );
247 }
248
249 public function get_fb_folders( $parent, $custom_db, $check_table_exist = true ) {
250 global $wpdb;
251 if ( $check_table_exist && ! $this->table_exist( $wpdb->prefix . $custom_db ) ) {
252 return array();
253 }
254 $query = $wpdb->prepare( "SELECT id AS term_id, name FROM {$wpdb->prefix}$custom_db WHERE parent = %d", $parent );
255
256 $folders = $wpdb->get_results( $query, ARRAY_A );
257
258 foreach ( $folders as $key => $folder ) {
259 $folders[ $key ]['children'] = $this->get_fb_folders( $folder['term_id'], $custom_db, false );
260 }
261
262 return $folders;
263 }
264
265 public function get_rml_folders( $parent, $custom_db, $check_table_exist = true ) {
266 global $wpdb;
267
268 if ( $check_table_exist && ! $this->table_exist( $wpdb->prefix . $custom_db ) ) {
269 return array();
270 }
271
272 $query = $wpdb->prepare( "SELECT id AS term_id, name FROM {$wpdb->prefix}$custom_db WHERE parent = %d", $parent );
273
274 $folders = $wpdb->get_results( $query, ARRAY_A );
275
276 foreach ( $folders as $key => $folder ) {
277 $folders[ $key ]['children'] = $this->get_rml_folders( $folder['term_id'], $custom_db, false );
278 }
279
280 return $folders;
281 }
282
283 public function get_wmlf_folders( $parent, $post_type, $check_table_exist = true ) {
284 global $wpdb;
285
286 $table = $wpdb->prefix . 'mgmlp_folders';
287
288 if ( $check_table_exist && ! $this->table_exist( $table ) ) {
289 return array();
290 }
291
292 $query = $wpdb->prepare(
293 "SELECT ID as term_id, post_title as name
294 from {$wpdb->prefix}posts
295 LEFT JOIN {$table} ON( {$wpdb->prefix}posts.ID = {$table}.post_id )
296 where post_type = %s and {$table}.folder_id = %d
297 order by folder_id",
298 $post_type,
299 $parent
300 );
301
302 $folders = $wpdb->get_results( $query, ARRAY_A );
303
304 foreach ( $folders as $key => $folder ) {
305 $folders[ $key ]['children'] = $this->get_wmlf_folders( $folder['term_id'], $post_type, false );
306 }
307
308 return $folders;
309 }
310
311 public function get_term_folders( $parent = 0, $taxonomy = '' ) {
312 global $wpdb;
313
314 $query = $wpdb->prepare(
315 "SELECT term_taxonomy.term_id, terms.name, term_taxonomy.term_taxonomy_id FROM $wpdb->term_taxonomy as `term_taxonomy`
316 JOIN $wpdb->terms as `terms`
317 ON term_taxonomy.term_taxonomy_id = terms.term_id
318 WHERE taxonomy = %s and parent = %d",
319 $taxonomy,
320 $parent
321 );
322
323 $folders = $wpdb->get_results( $query, ARRAY_A );
324
325 foreach ( $folders as $key => $folder ) {
326 $folders[ $key ]['children'] = $this->get_term_folders( $folder['term_id'], $taxonomy );
327 }
328
329 return $folders;
330 }
331
332 public function get_wmlf_attachments( $folders, $post_type ) {
333 global $wpdb;
334
335 $query = "SELECT {$wpdb->prefix}posts.ID FROM {$wpdb->prefix}posts
336 LEFT JOIN {$wpdb->prefix}postmeta as pm ON pm.post_id = {$wpdb->prefix}posts.ID
337 LEFT JOIN {$wpdb->prefix}mgmlp_folders ON( {$wpdb->prefix}posts.ID = {$wpdb->prefix}mgmlp_folders.post_id )
338 WHERE post_type = 'attachment'
339 and pm.meta_key = '_wp_attached_file'
340 and folder_id = %d";
341
342 foreach ( $folders as $folder ) {
343 $attachments[ $folder['term_id'] ] = $wpdb->get_col( $wpdb->prepare( $query, $folder['term_id'] ) );
344 if ( count( $folder['children'] ) > 0 ) {
345 $attachments = $attachments + $this->get_wmlf_attachments( $folder['children'], $post_type );
346 }
347 }
348
349 return $attachments;
350 }
351
352 public function get_term_attachments( $folders, $taxonomy ) {
353 global $wpdb;
354 $attachments = array();
355
356 $query = "SELECT term_relationships.object_id
357 FROM $wpdb->term_relationships as `term_relationships`
358 JOIN $wpdb->term_taxonomy as `term_taxonomy`
359 ON term_relationships.term_taxonomy_id = term_taxonomy.term_taxonomy_id
360 WHERE taxonomy = %s and term_id = %d";
361
362 foreach ( $folders as $folder ) {
363 $attachments[ $folder['term_id'] ] = $wpdb->get_col( $wpdb->prepare( $query, $taxonomy, $folder['term_id'] ) );
364
365 if ( count( $folder['children'] ) > 0 ) {
366 $attachments = $attachments + $this->get_term_attachments( $folder['children'], $taxonomy );
367 }
368 }
369
370 return $attachments;
371 }
372
373 public function get_fb_attachments( $folders ) {
374 global $wpdb;
375 $attachments = array();
376
377 $query = "SELECT attachment_id FROM {$wpdb->prefix}fbv_attachment_folder WHERE folder_id = %d";
378
379 foreach ( $folders as $folder ) {
380 $attachments[ $folder['term_id'] ] = $wpdb->get_col( $wpdb->prepare( $query, $folder['term_id'] ) );
381
382 if ( count( $folder['children'] ) > 0 ) {
383 $attachments = $attachments + $this->get_fb_attachments( $folder['children'] );
384 }
385 }
386
387 return $attachments;
388 }
389
390 public function get_rml_attachments( $folders, $check_table_exist = true ) {
391 global $wpdb;
392
393 $table = $wpdb->prefix . 'realmedialibrary_posts';
394
395 $attachments = array();
396
397 if ( $check_table_exist && ! $this->table_exist( $table ) ) {
398 return $attachments;
399 }
400 $query = "SELECT attachment FROM {$table} WHERE fid = %d";
401
402 foreach ( $folders as $folder ) {
403 $attachments[ $folder['term_id'] ] = $wpdb->get_col( $wpdb->prepare( $query, $folder['term_id'] ) );
404
405 if ( count( $folder['children'] ) > 0 ) {
406 $attachments = $attachments + $this->get_rml_attachments( $folder['children'], false );
407 }
408 }
409
410 return $attachments;
411 }
412
413 public function get_attachment_in_folder( \WP_REST_Request $request ) {
414 if( ! current_user_can( 'manage_options' ) ) {
415 return new \WP_Error( 403, __( 'You are not allowed to get attachment in folder.', 'catfolders' ) );
416 }
417 $prefix = Helpers::sanitize_array( $request->get_param( 'prefix' ) );
418 if ( isset( $this->plugins_to_import[ $prefix ] ) ) {
419 $folders = get_option( $this->get_prefix_option( $prefix, 'folders_import' ), array() );
420 if ( in_array( $prefix, array( 'EML', 'HP', 'Folders', 'WPMF', 'MLA', 'WF' ), true ) ) {
421 $attachments_in_folder = $this->get_term_attachments( $folders, $this->plugins_to_import[ $prefix ]['taxonomy'] );
422 }
423
424 if ( 'WMLF' === $prefix ) {
425 $attachments_in_folder = $this->get_wmlf_attachments( $folders, $this->plugins_to_import[ $prefix ]['post_type'] );
426 }
427
428 if ( 'RML' === $prefix ) {
429 $attachments_in_folder = $this->get_rml_attachments( $folders );
430 }
431
432 if ( 'FB' === $prefix ) {
433 $attachments_in_folder = $this->get_fb_attachments( $folders );
434 }
435
436 update_option( $this->get_prefix_option( $prefix, 'attachments_import' ), $attachments_in_folder, 'no' );
437 return new \WP_REST_Response( array( 'result' => true ) );
438 }
439 return new \WP_Error( '500', __( 'Plugin does not support!', 'catfolders' ) );
440 }
441
442 public function create_recursive_tree( $folders = array(), $attachments = array(), $parent = 0 ) {
443 $folders_created = array();
444
445 foreach ( $folders as $folder ) {
446 $new_folder = FolderModel::createFolder(
447 array(
448 'title' => $folder['name'],
449 'parent' => $parent,
450 'type' => 'attachment',
451 )
452 );
453 array_push( $folders_created, $folder['term_id'] );
454
455 if ( isset( $attachments[ $folder['term_id'] ] )
456 && count( $attachments[ $folder['term_id'] ] ) > 0
457 && false !== $new_folder ) {
458 FolderModel::set_attachments( $new_folder['id'], $attachments[ $folder['term_id'] ], false );
459 }
460
461 if ( count( $folder['children'] ) > 0 ) {
462 $new_child_folders = $this->create_recursive_tree( $folder['children'], $attachments, $new_folder['id'] );
463 $folders_created = array_merge( $folders_created, $new_child_folders );
464 }
465 }
466
467 return $folders_created;
468 }
469
470 public function process( \WP_REST_Request $request ) {
471 if( ! current_user_can( 'manage_options' ) ) {
472 return new \WP_Error( 403, __( 'You are not allowed to process import.', 'catfolders' ) );
473 }
474 $plugin_prefix = Helpers::sanitize_array( $request->get_param( 'prefix' ) );
475
476 $folders = get_option( '_catf_' . $plugin_prefix . '_folders_import' );
477 $attachments = get_option( '_catf_' . $plugin_prefix . '_attachments_import' );
478
479 $folders_created = $this->create_recursive_tree( $folders, $attachments );
480 update_option( $this->get_prefix_option( $plugin_prefix, 'folder_created' ), $folders_created, 'no' );
481 update_option( $this->get_prefix_option( $plugin_prefix, 'success_import' ), 1 );
482
483 return new \WP_REST_Response( array( 'result' => true ) );
484 }
485
486 public function clean_db() {
487 if( ! current_user_can( 'manage_options' ) ) {
488 return new \WP_Error( 403, __( 'You are not allowed to clean database.', 'catfolders' ) );
489 }
490 global $wpdb;
491 // Delete all import folders
492 foreach ( $this->plugins_to_import as $key => $value ) {
493 delete_option( $this->get_prefix_option( $key, 'success_import' ) );
494 delete_option( $this->get_prefix_option( $key, 'folders_import' ) );
495 delete_option( $this->get_prefix_option( $key, 'folder_created' ) );
496 delete_option( $this->get_prefix_option( $key, 'attachments_import' ) );
497 }
498
499 //Clear Cat folder && Cat Relationships Post
500
501 $cat_folder = $wpdb->prefix . FolderModel::TABLE;
502 $cat_attachment_folder = $wpdb->prefix . FolderModel::TABLE_RELATIONSHIP;
503 $wpdb->query( "DELETE FROM {$cat_folder}" );
504 $wpdb->query( "DELETE FROM {$cat_attachment_folder}" );
505
506 return new \WP_REST_Response( array( 'result' => true ) );
507 }
508
509 public function restore_folders( $data ) {
510 global $wpdb;
511 $cat_folder = $wpdb->prefix . FolderModel::TABLE;
512 $cat_attachment_folder = $wpdb->prefix . FolderModel::TABLE_RELATIONSHIP;
513 $wpdb->query( "DELETE FROM {$cat_folder}" );
514 $wpdb->query( "DELETE FROM {$cat_attachment_folder}" );
515
516 foreach ( $data as $key => $folder ) {
517 $new_folder = FolderModel::createFolder(
518 array(
519 'title' => $folder['title'],
520 'parent' => $folder['parent'],
521 'type' => $folder['type'],
522 'ord' => $folder['ord'],
523 'created_by' => $folder['created_by'],
524 'id' => $folder['id'],
525 )
526 );
527 if ( '' !== $folder['attachments'] && false !== $new_folder ) {
528 FolderModel::set_attachments( $new_folder['id'], explode( ',', $folder['attachments'] ), false );
529 }
530 }
531
532 }
533 private function table_exist( $table ) {
534 global $wpdb;
535 return $wpdb->get_var( "show tables like '$table'" ) == $table;
536 }
537 }
538