| 1 |
<?php |
| 2 |
namespace FileBird\Classes; |
| 3 |
|
| 4 |
use FileBird\Controller\Convert as ConvertController; |
| 5 |
use FileBird\Model\Folder as FolderModel; |
| 6 |
use FileBird\Controller\Import\DataImport; |
| 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 |
//get old data |
| 30 |
register_rest_route( |
| 31 |
NJFB_REST_URL, |
| 32 |
'fb-get-old-data', |
| 33 |
array( |
| 34 |
'methods' => 'POST', |
| 35 |
'callback' => array( $this, 'ajaxGetOldData' ), |
| 36 |
'permission_callback' => array( $this, 'resPermissionsCheck' ), |
| 37 |
) |
| 38 |
); |
| 39 |
//insert old data |
| 40 |
register_rest_route( |
| 41 |
NJFB_REST_URL, |
| 42 |
'fb-insert-old-data', |
| 43 |
array( |
| 44 |
'methods' => 'POST', |
| 45 |
'callback' => array( $this, 'ajaxInsertOldData' ), |
| 46 |
'permission_callback' => array( $this, 'resPermissionsCheck' ), |
| 47 |
) |
| 48 |
); |
| 49 |
//wipe old data |
| 50 |
register_rest_route( |
| 51 |
NJFB_REST_URL, |
| 52 |
'fb-wipe-old-data', |
| 53 |
array( |
| 54 |
'methods' => 'POST', |
| 55 |
'callback' => array( $this, 'ajaxWipeOldData' ), |
| 56 |
'permission_callback' => array( $this, 'resPermissionsCheck' ), |
| 57 |
) |
| 58 |
); |
| 59 |
//wipe old data |
| 60 |
register_rest_route( |
| 61 |
NJFB_REST_URL, |
| 62 |
'fb-wipe-clear-all-data', |
| 63 |
array( |
| 64 |
'methods' => 'POST', |
| 65 |
'callback' => array( $this, 'ajaxClearAllData' ), |
| 66 |
'permission_callback' => array( $this, 'resPermissionsCheck' ), |
| 67 |
) |
| 68 |
); |
| 69 |
register_rest_route( |
| 70 |
NJFB_REST_URL, |
| 71 |
'fb-no-thanks', |
| 72 |
array( |
| 73 |
'methods' => 'POST', |
| 74 |
'callback' => array( $this, 'ajaxNoThanks' ), |
| 75 |
'permission_callback' => array( $this, 'resPermissionsCheck' ), |
| 76 |
) |
| 77 |
); |
| 78 |
} |
| 79 |
public function resPermissionsCheck() { |
| 80 |
return current_user_can( 'upload_files' ); |
| 81 |
} |
| 82 |
|
| 83 |
public function ajaxGetOldData() { |
| 84 |
$folders = ConvertController::getOldFolders(); |
| 85 |
$folders_chunk = array_chunk( $folders, 20 ); |
| 86 |
wp_send_json_success( |
| 87 |
array( |
| 88 |
'folders' => $folders_chunk, |
| 89 |
) |
| 90 |
); |
| 91 |
} |
| 92 |
public function ajaxInsertOldData( $request ) { |
| 93 |
$folders = isset( $request ) ? $request->get_params()['folders'] : ''; |
| 94 |
if ( $folders != '' ) { |
| 95 |
ConvertController::insertToNewTable( $folders ); |
| 96 |
update_option( 'fbv_old_data_updated_to_v4', '1' ); |
| 97 |
wp_send_json_success( array( 'mess' => __( 'success', 'filebird' ) ) ); |
| 98 |
} else { |
| 99 |
wp_send_json_error( array( 'mess' => __( 'validation failed', 'filebird' ) ) ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
public function ajaxWipeOldData() { |
| 104 |
global $wpdb; |
| 105 |
$queries = array( |
| 106 |
'DELETE FROM ' . $wpdb->prefix . 'termmeta WHERE `term_id` IN (SELECT `term_id` FROM ' . $wpdb->prefix . 'term_taxonomy WHERE `taxonomy` = %s)', |
| 107 |
'DELETE FROM ' . $wpdb->prefix . 'term_relationships WHERE `term_taxonomy_id` IN (SELECT `term_taxonomy_id` FROM ' . $wpdb->prefix . 'term_taxonomy WHERE `taxonomy` = %s)', |
| 108 |
'DELETE FROM ' . $wpdb->prefix . 'terms WHERE `term_id` IN (SELECT `term_id` FROM ' . $wpdb->prefix . 'term_taxonomy WHERE `taxonomy` = %s)', |
| 109 |
'DELETE FROM ' . $wpdb->prefix . 'term_taxonomy WHERE `taxonomy` = %s', |
| 110 |
); |
| 111 |
foreach ( $queries as $k => $query ) { |
| 112 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared |
| 113 |
$wpdb->query( $wpdb->prepare( $query, 'nt_wmc_folder' ) ); |
| 114 |
} |
| 115 |
wp_send_json_success( |
| 116 |
array( |
| 117 |
'mess' => __( 'Successfully wiped.', 'filebird' ), |
| 118 |
) |
| 119 |
); |
| 120 |
} |
| 121 |
public function ajaxClearAllData() { |
| 122 |
global $wpdb; |
| 123 |
$table_name = $wpdb->prefix . 'fbv'; |
| 124 |
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) ) ) == $table_name ) { |
| 125 |
FolderModel::deleteAll(); |
| 126 |
|
| 127 |
foreach ( DataImport::get() as $data ) { |
| 128 |
update_option( "njt_fb_updated_from_{$data->prefix}", '0' ); |
| 129 |
} |
| 130 |
|
| 131 |
wp_send_json_success( |
| 132 |
array( |
| 133 |
'mess' => __( 'Successfully cleared!', 'filebird' ), |
| 134 |
) |
| 135 |
); |
| 136 |
} else { |
| 137 |
wp_send_json_error( |
| 138 |
array( |
| 139 |
'mess' => __( 'Please try again.', 'filebird' ), |
| 140 |
) |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
public function ajaxNoThanks( $request ) { |
| 147 |
$site = $request->get_param( 'site' ); |
| 148 |
|
| 149 |
$site = isset( $site ) ? sanitize_text_field( $site ) : ''; |
| 150 |
|
| 151 |
if ( $site === 'all' ) { |
| 152 |
foreach ( DataImport::get() as $data ) { |
| 153 |
update_option( "njt_fb_{$data->prefix}_no_thanks", '1' ); |
| 154 |
} |
| 155 |
} else { |
| 156 |
update_option( "njt_fb_{$site}_no_thanks", '1' ); |
| 157 |
} |
| 158 |
|
| 159 |
return new \WP_REST_Response( |
| 160 |
array( |
| 161 |
'mess' => __( 'Success', 'filebird' ), |
| 162 |
) |
| 163 |
); |
| 164 |
} |
| 165 |
} |