| 1 |
<?php |
| 2 |
namespace FileBird\Classes; |
| 3 |
|
| 4 |
use FileBird\Controller\Convert as ConvertController; |
| 5 |
use FileBird\Model\Folder as FolderModel; |
| 6 |
use FileBird\Classes\Helpers; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
class Convert { |
| 11 |
|
| 12 |
protected static $instance = null; |
| 13 |
public static function getInstance() { |
| 14 |
if ( null == self::$instance ) { |
| 15 |
self::$instance = new self(); |
| 16 |
self::$instance->doHooks(); |
| 17 |
} |
| 18 |
return self::$instance; |
| 19 |
} |
| 20 |
|
| 21 |
public function __construct() { |
| 22 |
} |
| 23 |
|
| 24 |
private function doHooks() { |
| 25 |
add_action( 'rest_api_init', array( $this, 'registerRestFields' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
public function registerRestFields() { |
| 29 |
register_rest_route( |
| 30 |
NJFB_REST_URL, |
| 31 |
'fb-import', |
| 32 |
array( |
| 33 |
'methods' => 'POST', |
| 34 |
'callback' => array( $this, 'ajaxImport' ), |
| 35 |
'permission_callback' => array( $this, 'resPermissionsCheck' ), |
| 36 |
) |
| 37 |
); |
| 38 |
register_rest_route( |
| 39 |
NJFB_REST_URL, |
| 40 |
'fb-import-insert-folder', |
| 41 |
array( |
| 42 |
'methods' => 'POST', |
| 43 |
'callback' => array( $this, 'ajaxImportInsertFolder' ), |
| 44 |
'permission_callback' => array( $this, 'resPermissionsCheck' ), |
| 45 |
) |
| 46 |
); |
| 47 |
register_rest_route( |
| 48 |
NJFB_REST_URL, |
| 49 |
'fb-import-after-inserting', |
| 50 |
array( |
| 51 |
'methods' => 'POST', |
| 52 |
'callback' => array( $this, 'ajaxImportAfterInserting' ), |
| 53 |
'permission_callback' => array( $this, 'resPermissionsCheck' ), |
| 54 |
) |
| 55 |
); |
| 56 |
register_rest_route( |
| 57 |
NJFB_REST_URL, |
| 58 |
'fb-no-thanks', |
| 59 |
array( |
| 60 |
'methods' => 'POST', |
| 61 |
'callback' => array( $this, 'ajaxNoThanks' ), |
| 62 |
'permission_callback' => array( $this, 'resPermissionsCheck' ), |
| 63 |
) |
| 64 |
); |
| 65 |
//get old data |
| 66 |
register_rest_route( |
| 67 |
NJFB_REST_URL, |
| 68 |
'fb-get-old-data', |
| 69 |
array( |
| 70 |
'methods' => 'POST', |
| 71 |
'callback' => array( $this, 'ajaxGetOldData' ), |
| 72 |
'permission_callback' => array( $this, 'resPermissionsCheck' ), |
| 73 |
) |
| 74 |
); |
| 75 |
//insert old data |
| 76 |
register_rest_route( |
| 77 |
NJFB_REST_URL, |
| 78 |
'fb-insert-old-data', |
| 79 |
array( |
| 80 |
'methods' => 'POST', |
| 81 |
'callback' => array( $this, 'ajaxInsertOldData' ), |
| 82 |
'permission_callback' => array( $this, 'resPermissionsCheck' ), |
| 83 |
) |
| 84 |
); |
| 85 |
//wipe old data |
| 86 |
register_rest_route( |
| 87 |
NJFB_REST_URL, |
| 88 |
'fb-wipe-old-data', |
| 89 |
array( |
| 90 |
'methods' => 'POST', |
| 91 |
'callback' => array( $this, 'ajaxWipeOldData' ), |
| 92 |
'permission_callback' => array( $this, 'resPermissionsCheck' ), |
| 93 |
) |
| 94 |
); |
| 95 |
//wipe old data |
| 96 |
register_rest_route( |
| 97 |
NJFB_REST_URL, |
| 98 |
'fb-wipe-clear-all-data', |
| 99 |
array( |
| 100 |
'methods' => 'POST', |
| 101 |
'callback' => array( $this, 'ajaxClearAllData' ), |
| 102 |
'permission_callback' => array( $this, 'resPermissionsCheck' ), |
| 103 |
) |
| 104 |
); |
| 105 |
} |
| 106 |
public function resPermissionsCheck() { |
| 107 |
return current_user_can( 'upload_files' ); |
| 108 |
} |
| 109 |
|
| 110 |
public function plugin_import_description( $counter, $pluginName ) { |
| 111 |
return wp_kses_post( sprintf( __( 'We found you have %1$s categories you created from <strong>%2$s</strong> plugin. Would you like to import it to <strong>FileBird</strong>?', 'filebird' ), $counter, $pluginName ) ); |
| 112 |
} |
| 113 |
|
| 114 |
public function get_plugin3rd_folders_to_import() { |
| 115 |
global $pagenow; |
| 116 |
|
| 117 |
$sites = array(); |
| 118 |
|
| 119 |
if ( $pagenow !== 'upload.php' ) { |
| 120 |
return $sites; |
| 121 |
} |
| 122 |
|
| 123 |
$oldEnhancedFolders = array(); |
| 124 |
if ( ! $this->isUpdated( 'enhanced' ) && ! $this->isNoThanks( 'enhanced' ) ) { |
| 125 |
$oldEnhancedFolders = $this->getOldFolders( 'enhanced', true ); |
| 126 |
} |
| 127 |
|
| 128 |
$oldWpmlfFolders = array(); |
| 129 |
if ( ! $this->isUpdated( 'wpmlf' ) && ! $this->isNoThanks( 'wpmlf' ) ) { |
| 130 |
$oldWpmlfFolders = $this->getOldFolders( 'wpmlf', true ); |
| 131 |
} |
| 132 |
|
| 133 |
$oldWpmfFolders = array(); |
| 134 |
if ( ! $this->isUpdated( 'wpmf' ) && ! $this->isNoThanks( 'wpmf' ) ) { |
| 135 |
$oldWpmfFolders = $this->getOldFolders( 'wpmf', true ); |
| 136 |
} |
| 137 |
|
| 138 |
$oldRealMediaFolders = array(); |
| 139 |
if ( ! $this->isUpdated( 'realmedia' ) && ! $this->isNoThanks( 'realmedia' ) ) { |
| 140 |
$oldRealMediaFolders = $this->getOldFolders( 'realmedia', true ); |
| 141 |
} |
| 142 |
|
| 143 |
$oldHappyFilesFolders = array(); |
| 144 |
if ( ! $this->isUpdated( 'happyfiles' ) && ! $this->isNoThanks( 'happyfiles' ) ) { |
| 145 |
$oldHappyFilesFolders = $this->getOldFolders( 'happyfiles', true ); |
| 146 |
} |
| 147 |
|
| 148 |
$oldPremioFolders = array(); |
| 149 |
if ( ! $this->isUpdated( 'premio' ) && ! $this->isNoThanks( 'premio' ) ) { |
| 150 |
$oldPremioFolders = $this->getOldFolders( 'premio', true ); |
| 151 |
} |
| 152 |
|
| 153 |
$oldFemlFolders = array(); |
| 154 |
if ( ! $this->isUpdated( 'feml' ) && ! $this->isNoThanks( 'feml' ) ) { |
| 155 |
$oldFemlFolders = $this->getOldFolders( 'feml', true ); |
| 156 |
} |
| 157 |
|
| 158 |
if ( count( $oldEnhancedFolders ) > 3 ) { |
| 159 |
$sites[] = array( |
| 160 |
'site' => 'enhanced', |
| 161 |
'title' => 'Enhanced Media Library', |
| 162 |
'desc' => $this->plugin_import_description( count( $oldEnhancedFolders ), 'Enhanced Media Library' ), |
| 163 |
); |
| 164 |
} |
| 165 |
if ( count( $oldWpmlfFolders ) > 3 ) { |
| 166 |
$sites[] = array( |
| 167 |
'site' => 'wpmlf', |
| 168 |
'title' => 'Media Library Folders', |
| 169 |
'desc' => $this->plugin_import_description( count( $oldWpmlfFolders ), 'Media Library Folders' ), |
| 170 |
); |
| 171 |
} |
| 172 |
if ( count( $oldWpmfFolders ) > 3 ) { |
| 173 |
$sites[] = array( |
| 174 |
'site' => 'wpmf', |
| 175 |
'title' => 'WP Media folder', |
| 176 |
'desc' => $this->plugin_import_description( count( $oldWpmfFolders ), 'WP Media folder' ), |
| 177 |
); |
| 178 |
} |
| 179 |
if ( count( $oldRealMediaFolders ) > 3 ) { |
| 180 |
$sites[] = array( |
| 181 |
'site' => 'realmedia', |
| 182 |
'title' => 'WP Real Media Library', |
| 183 |
'desc' => $this->plugin_import_description( count( $oldRealMediaFolders ), 'WP Real Media Library' ), |
| 184 |
); |
| 185 |
} |
| 186 |
if ( count( $oldHappyFilesFolders ) > 3 ) { |
| 187 |
$sites[] = array( |
| 188 |
'site' => 'happyfiles', |
| 189 |
'title' => 'HappyFiles', |
| 190 |
'desc' => $this->plugin_import_description( count( $oldHappyFilesFolders ), 'HappyFiles' ), |
| 191 |
); |
| 192 |
} |
| 193 |
if ( count( $oldPremioFolders ) > 3 ) { |
| 194 |
$sites[] = array( |
| 195 |
'site' => 'premio', |
| 196 |
'title' => 'premio', |
| 197 |
'desc' => $this->plugin_import_description( count( $oldPremioFolders ), 'Folders' ), |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
if ( count( $oldFemlFolders ) > 3 ) { |
| 202 |
$sites[] = array( |
| 203 |
'site' => 'feml', |
| 204 |
'title' => 'feml', |
| 205 |
'desc' => $this->plugin_import_description( count( $oldFemlFolders ), 'WP Media Folders' ), |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
return $sites; |
| 210 |
} |
| 211 |
|
| 212 |
public function adminNotice() { |
| 213 |
global $pagenow; |
| 214 |
|
| 215 |
$sites = array(); |
| 216 |
|
| 217 |
if ( $pagenow !== 'upload.php' ) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
$oldEnhancedFolders = array(); |
| 222 |
if ( ! $this->isUpdated( 'enhanced' ) && ! $this->isNoThanks( 'enhanced' ) ) { |
| 223 |
$oldEnhancedFolders = $this->getOldFolders( 'enhanced', true ); |
| 224 |
} |
| 225 |
|
| 226 |
$oldWpmlfFolders = array(); |
| 227 |
if ( ! $this->isUpdated( 'wpmlf' ) && ! $this->isNoThanks( 'wpmlf' ) ) { |
| 228 |
$oldWpmlfFolders = $this->getOldFolders( 'wpmlf', true ); |
| 229 |
} |
| 230 |
|
| 231 |
$oldWpmfFolders = array(); |
| 232 |
if ( ! $this->isUpdated( 'wpmf' ) && ! $this->isNoThanks( 'wpmf' ) ) { |
| 233 |
$oldWpmfFolders = $this->getOldFolders( 'wpmf', true ); |
| 234 |
} |
| 235 |
|
| 236 |
$oldRealMediaFolders = array(); |
| 237 |
if ( ! $this->isUpdated( 'realmedia' ) && ! $this->isNoThanks( 'realmedia' ) ) { |
| 238 |
$oldRealMediaFolders = $this->getOldFolders( 'realmedia', true ); |
| 239 |
} |
| 240 |
|
| 241 |
$oldHappyFilesFolders = array(); |
| 242 |
if ( ! $this->isUpdated( 'happyfiles' ) && ! $this->isNoThanks( 'happyfiles' ) ) { |
| 243 |
$oldHappyFilesFolders = $this->getOldFolders( 'happyfiles', true ); |
| 244 |
} |
| 245 |
|
| 246 |
if ( count( $oldEnhancedFolders ) > 3 ) { |
| 247 |
$sites[] = array( |
| 248 |
'site' => 'enhanced', |
| 249 |
'title' => 'Enhanced Media Library', |
| 250 |
); |
| 251 |
} |
| 252 |
if ( count( $oldWpmlfFolders ) > 3 ) { |
| 253 |
$sites[] = array( |
| 254 |
'site' => 'wpmlf', |
| 255 |
'title' => 'Media Library Folders', |
| 256 |
); |
| 257 |
} |
| 258 |
if ( count( $oldWpmfFolders ) > 3 ) { |
| 259 |
$sites[] = array( |
| 260 |
'site' => 'wpmf', |
| 261 |
'title' => 'WP Media folder', |
| 262 |
); |
| 263 |
} |
| 264 |
if ( count( $oldRealMediaFolders ) > 3 ) { |
| 265 |
$sites[] = array( |
| 266 |
'site' => 'realmedia', |
| 267 |
'title' => 'WP Real Media Library', |
| 268 |
); |
| 269 |
} |
| 270 |
if ( count( $oldHappyFilesFolders ) > 3 ) { |
| 271 |
$sites[] = array( |
| 272 |
'site' => 'happyfiles', |
| 273 |
'title' => 'HappyFiles', |
| 274 |
); |
| 275 |
} |
| 276 |
foreach ( $sites as $k => $site ) : |
| 277 |
$c = 0; |
| 278 |
if ( $site['site'] == 'enhanced' ) { |
| 279 |
$c = count( $oldEnhancedFolders ); |
| 280 |
} elseif ( $site['site'] == 'wpmlf' ) { |
| 281 |
$c = count( $oldWpmlfFolders ); |
| 282 |
} elseif ( $site['site'] == 'wpmf' ) { |
| 283 |
$c = count( $oldWpmfFolders ); |
| 284 |
} elseif ( $site['site'] == 'realmedia' ) { |
| 285 |
$c = count( $oldRealMediaFolders ); |
| 286 |
} elseif ( $site['site'] == 'happyfiles' ) { |
| 287 |
$c = count( $oldHappyFilesFolders ); |
| 288 |
} |
| 289 |
?> |
| 290 |
<div class="njt notice notice-warning <?php echo esc_attr( $site['site'] ); ?> is-dismissible"> |
| 291 |
<p> |
| 292 |
<strong><?php esc_html_e( 'Import categories to FileBird', 'filebird' ); ?></strong> |
| 293 |
</p> |
| 294 |
<p> |
| 295 |
<?php _e( sprintf( __( 'We found you have %1$s categories you created from <strong>%2$s</strong> plugin. Would you like to import it to <strong>FileBird</strong>?', 'filebird' ), $c, $site['title'] ) ); ?> |
| 296 |
</p> |
| 297 |
<a target="_blank" href=" |
| 298 |
<?php |
| 299 |
echo esc_url( |
| 300 |
add_query_arg( |
| 301 |
array( |
| 302 |
'page' => 'filebird-settings', |
| 303 |
'tab' => 'import', |
| 304 |
), |
| 305 |
admin_url( 'admin.php' ) |
| 306 |
) |
| 307 |
); |
| 308 |
?> |
| 309 |
" class="button button-primary"><?php esc_html_e( 'Import Now', 'filebird' ); ?></a> |
| 310 |
<button class="button njt_fb_no_thanks_btn" data-site="<?php echo esc_attr( $site['site'] ); ?>"><?php esc_html_e( 'No, thanks', 'filebird' ); ?></button> |
| 311 |
</p> |
| 312 |
</div> |
| 313 |
<?php endforeach; ?> |
| 314 |
<?php |
| 315 |
} |
| 316 |
public function ajaxNoThanks( $request ) { |
| 317 |
$site = $request->get_param( 'site' ); |
| 318 |
|
| 319 |
$site = isset( $site ) ? sanitize_text_field( $site ) : ''; |
| 320 |
// if ( ! wp_verify_nonce( $nonce, 'fbv_nonce' ) ){ |
| 321 |
// wp_send_json_error(array('mess' => __('Nonce error'))); |
| 322 |
// exit(); |
| 323 |
// } |
| 324 |
if ( $site == 'enhanced' ) { |
| 325 |
update_option( 'njt_fb_enhanced_no_thanks', '1' ); |
| 326 |
} elseif ( $site == 'wpmlf' ) { |
| 327 |
update_option( 'njt_fb_wpmlf_no_thanks', '1' ); |
| 328 |
} elseif ( $site == 'wpmf' ) { |
| 329 |
update_option( 'njt_fb_wpmf_no_thanks', '1' ); |
| 330 |
} elseif ( $site == 'realmedia' ) { |
| 331 |
update_option( 'njt_fb_realmedia_no_thanks', '1' ); |
| 332 |
} elseif ( $site == 'happyfiles' ) { |
| 333 |
update_option( 'njt_fb_happyfiles_no_thanks', '1' ); |
| 334 |
} elseif ( $site == 'premio' ) { |
| 335 |
update_option( 'njt_fb_premio_no_thanks', '1' ); |
| 336 |
} elseif ( $site == 'feml' ) { |
| 337 |
update_option( 'njt_fb_feml_no_thanks', '1' ); |
| 338 |
} elseif ( $site == 'all' ) { |
| 339 |
update_option( 'njt_fb_happyfiles_no_thanks', '1' ); |
| 340 |
update_option( 'njt_fb_realmedia_no_thanks', '1' ); |
| 341 |
update_option( 'njt_fb_wpmf_no_thanks', '1' ); |
| 342 |
update_option( 'njt_fb_enhanced_no_thanks', '1' ); |
| 343 |
update_option( 'njt_fb_wpmlf_no_thanks', '1' ); |
| 344 |
update_option( 'njt_fb_premio_no_thanks', '1' ); |
| 345 |
update_option( 'njt_fb_feml_no_thanks', '1' ); |
| 346 |
} |
| 347 |
|
| 348 |
wp_send_json_success( |
| 349 |
array( |
| 350 |
'mess' => __( 'Success', 'filebird' ), |
| 351 |
) |
| 352 |
); |
| 353 |
} |
| 354 |
|
| 355 |
public function ajaxGetOldData() { |
| 356 |
$folders = ConvertController::getOldFolers(); |
| 357 |
$folders_chunk = array_chunk( $folders, 20 ); |
| 358 |
wp_send_json_success( |
| 359 |
array( |
| 360 |
'folders' => $folders_chunk, |
| 361 |
) |
| 362 |
); |
| 363 |
} |
| 364 |
public function ajaxInsertOldData( $request ) { |
| 365 |
$folders = isset( $request ) ? $request->get_params()['folders'] : ''; |
| 366 |
if ( $folders != '' ) { |
| 367 |
ConvertController::insertToNewTable( $folders ); |
| 368 |
update_option( 'fbv_old_data_updated_to_v4', '1' ); |
| 369 |
wp_send_json_success( array( 'mess' => __( 'success', 'filebird' ) ) ); |
| 370 |
} else { |
| 371 |
wp_send_json_error( array( 'mess' => __( 'validation failed', 'filebird' ) ) ); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
public function ajaxWipeOldData() { |
| 376 |
global $wpdb; |
| 377 |
$queries = array( |
| 378 |
'DELETE FROM ' . $wpdb->prefix . 'termmeta WHERE `term_id` IN (SELECT `term_id` FROM ' . $wpdb->prefix . "term_taxonomy WHERE `taxonomy` = 'nt_wmc_folder')", |
| 379 |
'DELETE FROM ' . $wpdb->prefix . 'term_relationships WHERE `term_taxonomy_id` IN (SELECT `term_taxonomy_id` FROM ' . $wpdb->prefix . "term_taxonomy WHERE `taxonomy` = 'nt_wmc_folder')", |
| 380 |
'DELETE FROM ' . $wpdb->prefix . 'terms WHERE `term_id` IN (SELECT `term_id` FROM ' . $wpdb->prefix . "term_taxonomy WHERE `taxonomy` = 'nt_wmc_folder')", |
| 381 |
'DELETE FROM ' . $wpdb->prefix . "term_taxonomy WHERE `taxonomy` = 'nt_wmc_folder'", |
| 382 |
); |
| 383 |
foreach ( $queries as $k => $query ) { |
| 384 |
$wpdb->query( $query ); |
| 385 |
} |
| 386 |
wp_send_json_success( |
| 387 |
array( |
| 388 |
'mess' => __( 'Successfully wiped.', 'filebird' ), |
| 389 |
) |
| 390 |
); |
| 391 |
} |
| 392 |
public function ajaxClearAllData() { |
| 393 |
global $wpdb; |
| 394 |
$table_name = $wpdb->prefix . 'fbv'; |
| 395 |
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) ) ) == $table_name ) { |
| 396 |
FolderModel::deleteAll(); |
| 397 |
|
| 398 |
update_option( 'njt_fb_updated_from_enhanced', '0' ); |
| 399 |
update_option( 'njt_fb_updated_from_wpmlf', '0' ); |
| 400 |
update_option( 'njt_fb_updated_from_wpmf', '0' ); |
| 401 |
update_option( 'njt_fb_updated_from_realmedia', '0' ); |
| 402 |
update_option( 'njt_fb_updated_from_happyfiles', '0' ); |
| 403 |
update_option( 'njt_fb_updated_from_feml', '0' ); |
| 404 |
|
| 405 |
wp_send_json_success( |
| 406 |
array( |
| 407 |
'mess' => __( 'Successfully cleared.', 'filebird' ), |
| 408 |
) |
| 409 |
); |
| 410 |
} else { |
| 411 |
wp_send_json_error( |
| 412 |
array( |
| 413 |
'mess' => __( 'Please try again.', 'filebird' ), |
| 414 |
) |
| 415 |
); |
| 416 |
} |
| 417 |
|
| 418 |
} |
| 419 |
public function isUpdated( $site ) { |
| 420 |
global $wpdb; |
| 421 |
$is = false; |
| 422 |
if ( $site == 'enhanced' ) { |
| 423 |
$is = get_option( 'njt_fb_updated_from_enhanced', '0' ) === '1'; |
| 424 |
} elseif ( $site == 'wpmlf' ) { |
| 425 |
$is = get_option( 'njt_fb_updated_from_wpmlf', '0' ) === '1'; |
| 426 |
} elseif ( $site == 'wpmf' ) { |
| 427 |
$is = get_option( 'njt_fb_updated_from_wpmf', '0' ) === '1'; |
| 428 |
} elseif ( $site == 'realmedia' ) { |
| 429 |
$is = get_option( 'njt_fb_updated_from_realmedia', '0' ) === '1'; |
| 430 |
} elseif ( $site == 'happyfiles' ) { |
| 431 |
$is = get_option( 'njt_fb_updated_from_happyfiles', '0' ) === '1'; |
| 432 |
} elseif ( $site == 'premio' ) { |
| 433 |
$is = get_option( 'njt_fb_updated_from_premio', '0' ) === '1'; |
| 434 |
} elseif ( $site == 'feml' ) { |
| 435 |
$is = get_option( 'njt_fb_updated_from_feml', '0' ) === '1'; |
| 436 |
} |
| 437 |
|
| 438 |
return $is; |
| 439 |
} |
| 440 |
public function isNoThanks( $site ) { |
| 441 |
if ( $site == 'enhanced' ) { |
| 442 |
return get_option( 'njt_fb_enhanced_no_thanks', '0' ) === '1'; |
| 443 |
} elseif ( $site == 'wpmlf' ) { |
| 444 |
return get_option( 'njt_fb_wpmlf_no_thanks', '0' ) === '1'; |
| 445 |
} elseif ( $site == 'wpmf' ) { |
| 446 |
return get_option( 'njt_fb_wpmf_no_thanks', '0' ) === '1'; |
| 447 |
} elseif ( $site == 'realmedia' ) { |
| 448 |
return get_option( 'njt_fb_realmedia_no_thanks', '0' ) === '1'; |
| 449 |
} elseif ( $site == 'happyfiles' ) { |
| 450 |
return get_option( 'njt_fb_happyfiles_no_thanks', '0' ) === '1'; |
| 451 |
} elseif ( $site == 'premio' ) { |
| 452 |
return get_option( 'njt_fb_premio_no_thanks', '0' ) === '1'; |
| 453 |
} elseif ( $site == 'feml' ) { |
| 454 |
return get_option( 'njt_fb_feml_no_thanks', '0' ) === '1'; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
public function ajaxImport( $request ) { |
| 459 |
$site = $request->get_param( 'site' ); |
| 460 |
|
| 461 |
$site = isset( $site ) ? sanitize_text_field( $site ) : ''; |
| 462 |
|
| 463 |
$this->beforeGettingNewFolders( $site ); |
| 464 |
$folders = $this->getOldFolders( $site, true ); |
| 465 |
$count = count( $folders ); |
| 466 |
$folders = array_chunk( $folders, 20 ); |
| 467 |
|
| 468 |
wp_send_json_success( |
| 469 |
array( |
| 470 |
'folders' => $folders, |
| 471 |
'count' => $count, |
| 472 |
'site' => $site, |
| 473 |
) |
| 474 |
); |
| 475 |
exit(); |
| 476 |
} |
| 477 |
public function ajaxImportInsertFolder( $request ) { |
| 478 |
$site = $request->get_param( 'site' ); |
| 479 |
$folders = $request->get_param( 'folders' ); |
| 480 |
|
| 481 |
$site = isset( $site ) ? sanitize_text_field( $site ) : ''; |
| 482 |
$folders = isset( $folders ) ? $this->sanitize_arr( $folders ) : ''; |
| 483 |
|
| 484 |
$this->insertFolderAndItsAtt( $site, $folders ); |
| 485 |
|
| 486 |
wp_send_json_success(); |
| 487 |
exit(); |
| 488 |
} |
| 489 |
public function ajaxImportAfterInserting( $request ) { |
| 490 |
$site = $request->get_param( 'site' ); |
| 491 |
$count = $request->get_param( 'count' ); |
| 492 |
|
| 493 |
$site = isset( $site ) ? sanitize_text_field( $site ) : ''; |
| 494 |
$count = isset( $count ) ? sanitize_text_field( $count ) : ''; |
| 495 |
$this->afterInsertingNewFolders( $site ); |
| 496 |
$this->updateUpdated( $site ); |
| 497 |
|
| 498 |
$mess = sprintf( __( 'Congratulations! We imported successfully %d folders into <strong>FileBird.</strong>', 'filebird' ), $count ); |
| 499 |
wp_send_json_success( |
| 500 |
array( |
| 501 |
'mess' => $mess, |
| 502 |
) |
| 503 |
); |
| 504 |
exit(); |
| 505 |
} |
| 506 |
private function beforeGettingNewFolders( $site ) { |
| 507 |
if ( $site == 'enhanced' ) { |
| 508 |
if ( get_option( 'njt_fb_updated_from_enhanced', '0' ) == '1' ) { |
| 509 |
wp_send_json_success( |
| 510 |
array( |
| 511 |
'mess' => __( 'Already Updated', 'filebird' ), |
| 512 |
) |
| 513 |
); |
| 514 |
exit(); |
| 515 |
} |
| 516 |
} elseif ( $site == 'wpmlf' ) { |
| 517 |
if ( get_option( 'njt_fb_updated_from_wpmlf', '0' ) == '1' ) { |
| 518 |
wp_send_json_success( |
| 519 |
array( |
| 520 |
'mess' => __( 'Already Updated', 'filebird' ), |
| 521 |
) |
| 522 |
); |
| 523 |
exit(); |
| 524 |
} |
| 525 |
} elseif ( $site == 'wpmf' ) { |
| 526 |
if ( get_option( 'njt_fb_updated_from_wpmf', '0' ) == '1' ) { |
| 527 |
wp_send_json_success( |
| 528 |
array( |
| 529 |
'mess' => __( 'Already Updated', 'filebird' ), |
| 530 |
) |
| 531 |
); |
| 532 |
exit(); |
| 533 |
} |
| 534 |
} elseif ( $site == 'realmedia' ) { |
| 535 |
if ( get_option( 'njt_fb_updated_from_realmedia', '0' ) == '1' ) { |
| 536 |
wp_send_json_success( |
| 537 |
array( |
| 538 |
'mess' => __( 'Already Updated', 'filebird' ), |
| 539 |
) |
| 540 |
); |
| 541 |
exit(); |
| 542 |
} |
| 543 |
} elseif ( $site == 'happyfiles' ) { |
| 544 |
if ( get_option( 'njt_fb_updated_from_happyfiles', '0' ) == '1' ) { |
| 545 |
wp_send_json_success( |
| 546 |
array( |
| 547 |
'mess' => __( 'Already Updated', 'filebird' ), |
| 548 |
) |
| 549 |
); |
| 550 |
exit(); |
| 551 |
} |
| 552 |
} elseif ( $site == 'premio' ) { |
| 553 |
if ( get_option( 'njt_fb_updated_from_premio', '0' ) == '1' ) { |
| 554 |
wp_send_json_success( |
| 555 |
array( |
| 556 |
'mess' => __( 'Already Updated', 'filebird' ), |
| 557 |
) |
| 558 |
); |
| 559 |
exit(); |
| 560 |
} |
| 561 |
} elseif ( $site == 'feml' ) { |
| 562 |
if ( get_option( 'njt_fb_updated_from_feml', '0' ) == '1' ) { |
| 563 |
wp_send_json_success( |
| 564 |
array( |
| 565 |
'mess' => __( 'Already Updated', 'filebird' ), |
| 566 |
) |
| 567 |
); |
| 568 |
exit(); |
| 569 |
} |
| 570 |
} |
| 571 |
} |
| 572 |
public function getOldFolders( $site, $flat = false ) { |
| 573 |
$folders = array(); |
| 574 |
if ( $site == 'enhanced' ) { |
| 575 |
$folders = Helpers::foldersFromEnhanced( 0, $flat ); |
| 576 |
} elseif ( $site == 'wpmlf' ) { |
| 577 |
$folders = Helpers::foldersFromWpmlf( 0, $flat ); |
| 578 |
} elseif ( $site == 'wpmf' ) { |
| 579 |
$folders = Helpers::foldersFromWpmf( 0, $flat ); |
| 580 |
} elseif ( $site == 'realmedia' ) { |
| 581 |
$folders = Helpers::foldersFromRealMedia( -1, $flat ); |
| 582 |
foreach ( $folders as $k => $folder ) { |
| 583 |
$folders[ $k ]->parent = $folder->parent == '-1' ? 0 : $folder->parent; |
| 584 |
} |
| 585 |
} elseif ( $site == 'happyfiles' ) { |
| 586 |
$folders = Helpers::foldersFromHappyFiles( 0, $flat ); |
| 587 |
} elseif ( $site == 'premio' ) { |
| 588 |
$folders = Helpers::foldersFromPremio( 0, $flat ); |
| 589 |
} elseif ( $site == 'feml' ) { |
| 590 |
$folders = Helpers::foldersFromWpfeml( 0, $flat ); |
| 591 |
} |
| 592 |
return $folders; |
| 593 |
} |
| 594 |
public function insertFolderAndItsAtt( $site, $folders ) { |
| 595 |
foreach ( $folders as $k => $folder ) { |
| 596 |
if ( \is_array( $folder ) ) { |
| 597 |
$folder = json_decode( wp_json_encode( $folder ) ); |
| 598 |
} |
| 599 |
$new_parent = $folder->parent; |
| 600 |
if ( $new_parent > 0 ) { |
| 601 |
$new_parent = get_option( 'njt_new_term_id_' . $new_parent ); |
| 602 |
} |
| 603 |
$inserted = FolderModel::newOrGet( $folder->title, $new_parent ); |
| 604 |
update_option( 'njt_new_term_id_' . $folder->id, $inserted ); |
| 605 |
$atts = $this->getAttOfFolder( $site, $folder ); |
| 606 |
FolderModel::setFoldersForPosts( $atts, $inserted ); |
| 607 |
} |
| 608 |
} |
| 609 |
public function getAttOfFolder( $site, $folder ) { |
| 610 |
global $wpdb; |
| 611 |
$att = array(); |
| 612 |
if ( is_array( $folder ) ) { |
| 613 |
$folder = json_decode( wp_json_encode( $folder ) ); |
| 614 |
} |
| 615 |
|
| 616 |
if ( $site == 'enhanced' ) { |
| 617 |
$att = $wpdb->get_col( $wpdb->prepare( 'SELECT object_id FROM %1$s WHERE term_taxonomy_id = %2$d', $wpdb->term_relationships, $folder->term_taxonomy_id ) ); |
| 618 |
} elseif ( $site == 'wpmlf' ) { |
| 619 |
$folder_table = $wpdb->prefix . 'mgmlp_folders'; |
| 620 |
$sql = $wpdb->prepare( |
| 621 |
"select ID from {$wpdb->prefix}posts |
| 622 |
LEFT JOIN $folder_table ON({$wpdb->prefix}posts.ID = $folder_table.post_id) |
| 623 |
LEFT JOIN {$wpdb->prefix}postmeta AS pm ON (pm.post_id = {$wpdb->prefix}posts.ID) |
| 624 |
where post_type = 'attachment' |
| 625 |
and folder_id = %s |
| 626 |
AND pm.meta_key = '_wp_attached_file' |
| 627 |
order by post_date desc", |
| 628 |
$folder->id |
| 629 |
); |
| 630 |
$att = $wpdb->get_col( $sql ); |
| 631 |
} elseif ( $site == 'wpmf' ) { |
| 632 |
$att = $wpdb->get_col( $wpdb->prepare( 'SELECT object_id FROM %1$s WHERE term_taxonomy_id = %2$d', $wpdb->term_relationships, $folder->term_taxonomy_id ) ); |
| 633 |
} elseif ( $site == 'realmedia' ) { |
| 634 |
$folder_table = $wpdb->prefix . 'realmedialibrary_posts'; |
| 635 |
$att = $wpdb->get_col( $wpdb->prepare( 'SELECT attachment FROM %1$s WHERE fid = %2$d', $folder_table, $folder->id ) ); |
| 636 |
} elseif ( $site == 'happyfiles' ) { |
| 637 |
$att = $wpdb->get_col( $wpdb->prepare( 'SELECT object_id FROM %1$s WHERE term_taxonomy_id = %2$d', $wpdb->term_relationships, $folder->term_taxonomy_id ) ); |
| 638 |
} elseif ( $site == 'premio' ) { |
| 639 |
$att = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $folder->term_taxonomy_id ) ); |
| 640 |
} elseif ( $site == 'feml' ) { |
| 641 |
$att = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $folder->term_taxonomy_id ) ); |
| 642 |
} |
| 643 |
return $att; |
| 644 |
} |
| 645 |
private function afterInsertingNewFolders( $site ) { |
| 646 |
global $wpdb; |
| 647 |
$wpdb->delete( $wpdb->termmeta, array( 'meta_key' => 'njt_old_term_id' ) ); |
| 648 |
} |
| 649 |
private function updateUpdated( $site ) { |
| 650 |
if ( $site == 'enhanced' ) { |
| 651 |
update_option( 'njt_fb_updated_from_enhanced', '1' ); |
| 652 |
} elseif ( $site == 'wpmlf' ) { |
| 653 |
update_option( 'njt_fb_updated_from_wpmlf', '1' ); |
| 654 |
} elseif ( $site == 'wpmf' ) { |
| 655 |
update_option( 'njt_fb_updated_from_wpmf', '1' ); |
| 656 |
} elseif ( $site == 'realmedia' ) { |
| 657 |
update_option( 'njt_fb_updated_from_realmedia', '1' ); |
| 658 |
} elseif ( $site == 'happyfiles' ) { |
| 659 |
update_option( 'njt_fb_updated_from_happyfiles', '1' ); |
| 660 |
} elseif ( $site == 'premio' ) { |
| 661 |
update_option( 'njt_fb_updated_from_premio', '1' ); |
| 662 |
} elseif ( 'feml' === $site ) { |
| 663 |
update_option( 'njt_fb_updated_from_feml', '1' ); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
private function sanitize_arr( $arr ) { |
| 668 |
if ( is_array( $arr ) ) { |
| 669 |
return array_map( array( $this, 'sanitize_arr' ), $arr ); |
| 670 |
} else { |
| 671 |
return sanitize_text_field( $arr ); |
| 672 |
} |
| 673 |
} |
| 674 |
} |
| 675 |
|