plugins_to_import = array( [ "id" => "filebird", "name" => "FileBird", "author" => "Ninja Team", "avatar" => "https://ps.w.org/filebird/assets/icon-128x128.gif", 'folder_db' => 'fbv', 'attachment_db' => 'fbv_attachment_folder' ], [ "id" => "catfolders", "name" => "CatFolders - WP Media Folders", "author" => "CatFolders", 'folder_db' => 'catfolders', 'attachment_db' => 'catfolders_posts', "avatar" => "https://ps.w.org/catfolders/assets/icon-256x256.png", ], [ 'id' => 'enhanced', 'name' => 'Enhanced Media Library', 'author' => 'wpUXsolutions', 'color' => '#099ee8', 'taxonomy' => 'media_category' ], [ 'id' => 'wpmf', 'name' => 'WP Media folder', 'author' => 'Joomunited', 'color' => '#a6ce39', 'taxonomy' => 'wpmf-category' ], [ 'id' => 'folders', 'name' => 'Folders – Unlimited Folders to Organize', 'author' => 'Premio', 'color' => '#CE256D', 'taxonomy' => 'media_folder', ], [ 'id' => 'wicked', 'name' => 'Wicked Folders', 'author' => 'Wicked Plugins', "avatar" => "https://ps.w.org/wicked-folders/assets/icon-256x256.png", 'taxonomy' => 'wf_attachment_folders', ], [ 'id' => 'wpmlf', 'name' => 'Media Library Folders', 'author' => 'Max Foundry', 'post_type' => 'mgmlp_media_folder', "avatar" => "https://ps.w.org/media-library-plus/assets/icon-128x128.jpg", "color" => "#EB4237" ], [ 'id' => 'happyfiles', 'name' => 'HappyFiles', 'author' => 'Codeer', 'color' => '#EC008C', 'taxonomy' => 'happyfiles_category', ], [ 'id' => 'mla', 'name' => 'Media Library Assistant', 'author' => 'David Lingren', "avatar" => "https://ps.w.org/media-library-assistant/assets/icon-256x256.png", 'taxonomy' => 'attachment_category', ], [ 'id' => 'realmedia', 'name' => 'WP Real Media Library', "avatar" => "https://ps.w.org/real-media-library-lite/assets/icon-256x256.gif", 'author' => 'devowl.io GmbH', 'folder_db' => 'realmedialibrary', 'attachment_db' => 'realmedialibrary_posts' ] ); } /** * Get all files from other plugins * */ public function detect_import( ){ $plugin_can_import = array(); foreach( $this->plugins_to_import as $plugin ){ $has_import = Config::get_option( $plugin['id'].'_imported' ); $counter = $this->get_folder_counter($plugin); if( !$counter ) continue; $row = [ 'id' => $plugin['id'], 'name' => $plugin['name'], 'author' => $plugin['author'], 'folders' => $counter, 'imported' => $has_import ? 1 : 0, ]; if( isset($plugin['avatar']) && $plugin['avatar'] ){ $row['avatar'] = $plugin['avatar']; } else if( isset($plugin['color']) && $plugin['color'] ){ $row['color'] = $plugin['color']; }else{ $row['color'] = '#666666'; } $plugin_can_import[] = $row; } return $plugin_can_import; } /** * Count all folders and files in other plugin * */ public function get_folder_counter($plugin){ global $wpdb; // check with custom db if( isset($plugin['folder_db']) && $plugin['folder_db'] ){ // check table exists $table = Helpers::check_table_exists($plugin['folder_db']); if( !$table ) return 0; // phpcs:ignore WordPress.DB return $wpdb->get_var("SELECT COUNT(id) FROM $table"); } // check with plugin use taxonomy if( isset($plugin['taxonomy']) && $plugin['taxonomy'] ){ return Helpers::query( "SELECT COUNT(term_taxonomy_id) FROM $wpdb->term_taxonomy WHERE taxonomy = %s", $plugin['taxonomy'], 'get_var' ); } // check with plugin use post type if( isset($plugin['post_type']) && $plugin['post_type'] ){ return Helpers::query( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = %s", $plugin['post_type'], 'get_var' ); } return 0; } /** * Get plugin by id */ public function get_plugin_by_id($plugin_id){ if( !$plugin_id || !is_string($plugin_id) ) return false; $plugin = array_values(array_filter($this->plugins_to_import, function($plugin) use ($plugin_id) { return $plugin['id'] === $plugin_id; })); if( empty($plugin) ) return false; return $plugin[0]; } /** * Change status imported of plugin */ public function set_imported($plugin_id, $status = 1){ $plugin = $this->get_plugin_by_id($plugin_id); if( !$plugin ) return false; Config::set_option( $plugin_id.'_imported', $status ); return true; } /** * Get all folders and posts from other plugins */ public function getFoldersPosts($plugin_id){ $plugin = $this->get_plugin_by_id($plugin_id); if( !$plugin ) return false; $importFolder = new ImportFolder(); $importPost = new ImportPost(); $folders = []; $posts = []; // import from plugin use table database if( isset($plugin['folder_db']) && $plugin['folder_db'] ){ $folders = $importFolder->get_folders_by_table($plugin['folder_db']); $posts = $importPost->get_posts_by_table($plugin['attachment_db']); } else if( isset($plugin['taxonomy']) && $plugin['taxonomy'] ){ $folders = $importFolder->get_folders_by_taxonomy($plugin['taxonomy']); $posts = $importPost->get_posts_by_taxonomy($plugin['taxonomy']); } else if( isset($plugin['post_type']) && $plugin['post_type'] ){ $folders = $importFolder->get_folders_by_post_type($plugin['post_type']); $posts = $importPost->get_posts_by_post_type($plugin['post_type']); } $data = [ 'folders' => $folders, 'posts' => $posts, ]; return $data; } /** * Import folders, posts from array * * @param array $folders * @param array $posts * * @return bool */ public function importArray($folders, $posts, $parent_id = 0){ if( !$folders || count($folders) == 0 ) return true; // Loop through each row and import the folder $i = -1; foreach ( $folders as $row ) { $i++; if( !$row['id'] || !$row['title'] || $row['parent_id'] != $parent_id ) continue; $old_folder_id = $row['id']; unset($row['id']); $folder_id = FolderModel::createFolder($row); if( !$folder_id ) continue; // get all posts in folder $posts_in_folder = array_filter($posts, function($post) use ($old_folder_id) { return $post['folder_id'] == $old_folder_id; }); if( $posts_in_folder && is_array($posts_in_folder) && count($posts_in_folder) > 0 ){ $j = -1; foreach( $posts_in_folder as $post ){ $j++; $post['folder_id'] = $folder_id; PostModel::addOrUpdate($post); } } // delete folder added unset($folders[$i]); // update new folder_id to parent_id of array folders foreach( $folders as $key => $folder ){ if( $folder['parent_id'] == $old_folder_id ){ $folders[$key]['parent_id'] = $folder_id; } } $this->importArray($folders, $posts, $folder_id); } return true; } } ?>