| 1 |
<?php |
| 2 |
|
| 3 |
namespace FileBird\Controller\Import; |
| 4 |
|
| 5 |
use FileBird\Admin\Settings; |
| 6 |
|
| 7 |
defined( 'ABSPATH' ) || exit; |
| 8 |
|
| 9 |
class ImportController { |
| 10 |
const IMPORT_ENDPOINT = 'import'; |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); |
| 14 |
|
| 15 |
$this->add( |
| 16 |
'happyfiles', |
| 17 |
array( |
| 18 |
'name' => 'HappyFiles', |
| 19 |
'author' => 'Codeer', |
| 20 |
'taxonomy' => 'happyfiles_category', |
| 21 |
) |
| 22 |
); |
| 23 |
$this->add( |
| 24 |
'enhanced', |
| 25 |
array( |
| 26 |
'name' => 'Enhanced Media Library', |
| 27 |
'author' => 'wpUXsolutions', |
| 28 |
'taxonomy' => 'media_category', |
| 29 |
) |
| 30 |
); |
| 31 |
$this->add( |
| 32 |
'wpmf', |
| 33 |
array( |
| 34 |
'name' => 'WP Media folder', |
| 35 |
'author' => 'Joomunited', |
| 36 |
'taxonomy' => 'wpmf-category', |
| 37 |
) |
| 38 |
); |
| 39 |
$this->add( |
| 40 |
'premio', |
| 41 |
array( |
| 42 |
'name' => 'Folders', |
| 43 |
'author' => 'Premio', |
| 44 |
'taxonomy' => 'media_folder', |
| 45 |
) |
| 46 |
); |
| 47 |
$this->add( |
| 48 |
'wf', |
| 49 |
array( |
| 50 |
'name' => 'Wicked Folders', |
| 51 |
'author' => 'Wicked Plugins', |
| 52 |
'taxonomy' => 'wf_attachment_folders', |
| 53 |
) |
| 54 |
); |
| 55 |
$this->add( |
| 56 |
'feml', |
| 57 |
array( |
| 58 |
'name' => 'WP Media Folders', |
| 59 |
'author' => 'Damien Barrère', |
| 60 |
'taxonomy' => 'feml-folder', |
| 61 |
) |
| 62 |
); |
| 63 |
$this->add( |
| 64 |
'wpmlf', |
| 65 |
array( |
| 66 |
'name' => 'WordPress Media Library Folders', |
| 67 |
'author' => 'Max Foundry', |
| 68 |
) |
| 69 |
); |
| 70 |
$this->add( |
| 71 |
'mla', |
| 72 |
array( |
| 73 |
'name' => 'Media Library Assistant', |
| 74 |
'author' => 'David Lingren', |
| 75 |
'taxonomy' => 'attachment_category', |
| 76 |
) |
| 77 |
); |
| 78 |
|
| 79 |
$this->add( |
| 80 |
'realmedia', |
| 81 |
array( |
| 82 |
'name' => 'WP Real Media Library', |
| 83 |
'author' => 'devowl.io GmbH', |
| 84 |
) |
| 85 |
); |
| 86 |
|
| 87 |
$this->add( |
| 88 |
'mediamatic', |
| 89 |
array( |
| 90 |
'name' => 'Mediamatic', |
| 91 |
'author' => 'Plugincraft', |
| 92 |
'taxonomy' => 'mediamatic_wpfolder', |
| 93 |
) |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
public function add( $prefix, $attributes ) { |
| 98 |
new DataImport( $prefix, $attributes ); |
| 99 |
} |
| 100 |
|
| 101 |
public function get_all() { |
| 102 |
return DataImport::get(); |
| 103 |
} |
| 104 |
|
| 105 |
public static function get_all_plugins_import() { |
| 106 |
$data_import = DataImport::get(); |
| 107 |
$total_folder_import = 0; |
| 108 |
foreach ( $data_import as $prefix => $data ) { |
| 109 |
$data_import[ $prefix ]->counter = self::get_counters( $prefix ); |
| 110 |
$data_import[ $prefix ]->completed = get_option( 'njt_fb_updated_from_' . $data->prefix ) === '1'; |
| 111 |
$data_import[ $prefix ]->noThanks = get_option( "njt_fb_{$data->prefix}_no_thanks" ) === '1'; |
| 112 |
$data_import[ $prefix ]->description = sprintf( esc_html__( 'We found you have %1$s categories you created from %2$s plugin. Would you like to import it to %3$s?', 'filebird' ), '<strong>(' . esc_html( $data_import[ $prefix ]->counter ) . ')</strong>', '<strong>' . esc_html( $data->name ) . '</strong>', '<strong>FileBird</strong>' ); |
| 113 |
|
| 114 |
$total_folder_import += $data_import[ $prefix ]->counter; |
| 115 |
} |
| 116 |
return (object) array( |
| 117 |
'plugins' => array_filter( |
| 118 |
$data_import, |
| 119 |
function( $data ) { |
| 120 |
return $data->counter !== 0; |
| 121 |
} |
| 122 |
), |
| 123 |
'total_folder_import' => $total_folder_import, |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
public static function get_notice_import( $screen ) { |
| 128 |
if ( $screen !== 'upload.php' && $screen !== Settings::getInstance()->getSettingHookSuffix() ) { |
| 129 |
return (object) array( 'plugins' => new \stdClass() ); |
| 130 |
} |
| 131 |
|
| 132 |
return self::get_all_plugins_import(); |
| 133 |
} |
| 134 |
|
| 135 |
public function register_rest_routes() { |
| 136 |
register_rest_route( |
| 137 |
NJFB_REST_URL, |
| 138 |
self::IMPORT_ENDPOINT . '/get-folders/(?P<prefix>[a-zA-Z]+)', |
| 139 |
array( |
| 140 |
'methods' => \WP_REST_Server::READABLE, |
| 141 |
'callback' => array( $this, 'get_folders' ), |
| 142 |
'permission_callback' => array( $this, 'permission_callback' ), |
| 143 |
) |
| 144 |
); |
| 145 |
|
| 146 |
register_rest_route( |
| 147 |
NJFB_REST_URL, |
| 148 |
self::IMPORT_ENDPOINT . '/get-attachments/(?P<prefix>[a-zA-Z]+)', |
| 149 |
array( |
| 150 |
'methods' => \WP_REST_Server::READABLE, |
| 151 |
'callback' => array( $this, 'get_attachments' ), |
| 152 |
'permission_callback' => array( $this, 'permission_callback' ), |
| 153 |
) |
| 154 |
); |
| 155 |
|
| 156 |
register_rest_route( |
| 157 |
NJFB_REST_URL, |
| 158 |
self::IMPORT_ENDPOINT . '/run/(?P<prefix>[a-zA-Z]+)', |
| 159 |
array( |
| 160 |
'methods' => \WP_REST_Server::READABLE, |
| 161 |
'callback' => array( $this, 'run_import' ), |
| 162 |
'permission_callback' => array( $this, 'permission_callback' ), |
| 163 |
) |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
public function permission_callback() { |
| 168 |
return current_user_can( 'upload_files' ); |
| 169 |
} |
| 170 |
|
| 171 |
public static function get_counters( $prefix ) { |
| 172 |
$import_method = ImportFactory::getImportMethod( $prefix ); |
| 173 |
$data = DataImport::get( $prefix ); |
| 174 |
|
| 175 |
return $import_method->get_counters( $data ); |
| 176 |
} |
| 177 |
|
| 178 |
public function get_folders( $request ) { |
| 179 |
$prefix = sanitize_key( $request->get_param( 'prefix' ) ); |
| 180 |
|
| 181 |
$import_method = ImportFactory::getImportMethod( $prefix ); |
| 182 |
$data = DataImport::get( $prefix ); |
| 183 |
|
| 184 |
return $import_method->get_folders( $data ); |
| 185 |
} |
| 186 |
|
| 187 |
public function get_attachments( $request ) { |
| 188 |
$prefix = sanitize_key( $request->get_param( 'prefix' ) ); |
| 189 |
|
| 190 |
$import_method = ImportFactory::getImportMethod( $prefix ); |
| 191 |
$data = DataImport::get( $prefix ); |
| 192 |
|
| 193 |
return $import_method->get_attachments( $data ); |
| 194 |
} |
| 195 |
|
| 196 |
public function run_import( $request ) { |
| 197 |
$prefix = sanitize_key( $request->get_param( 'prefix' ) ); |
| 198 |
|
| 199 |
$import_method = ImportFactory::getImportMethod( $prefix ); |
| 200 |
$data = DataImport::get( $prefix ); |
| 201 |
|
| 202 |
return $import_method->run( $data ); |
| 203 |
} |
| 204 |
} |