1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
<?php
defined( 'ABSPATH' ) || exit();
class WC_Braintree_Controller_Data_Migration extends WC_Braintree_Rest_Controller {
protected $namespace = 'data-migration/';
public function register_routes() {
register_rest_route(
$this->rest_uri(),
'plugin',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'migrate_data' ),
'permission_callback' => array( $this, 'admin_permission_check' ),
)
);
}
public function migrate_data( $request ) {
global $wpdb;
wc_set_time_limit( 0 );
$plugin = $request->get_param( 'plugin_id' );
$message = '';
switch ( $plugin ) {
case 'paypal_powered_by_braintree':
if ( null != $request->get_param( 'users' ) ) {
$this->create_temp_table( 'wc_temp_usermeta', $wpdb->usermeta, 'wc_braintree_customer_id' );
$this->update_temp_table( 'wc_temp_usermeta', 'braintree_production_vault_id' );
$this->insert_from_temp_table( 'wc_temp_usermeta', $wpdb->usermeta, array(
'user_id',
'meta_key',
'meta_value'
), 'user_id', 'meta_key' );
wc_braintree_log_info( sprintf( '%s rows inserted in to usermeta table.', $wpdb->rows_affected ) );
$message .= sprintf( '%s rows updated in usermeta table.', $wpdb->rows_affected );
$this->drop_temp_table( 'wc_temp_usermeta' );
}
if ( null != $request->get_param( 'orders' ) ) {
$query = $wpdb->prepare( $this->get_update_orders_statement( $wpdb->postmeta ), 'braintree_cc', 'braintree_credit_card' );
$wpdb->query( $query );
$message .= sprintf( '%s orders updated in postmeta table.', $wpdb->rows_affected );
}
if ( null != $request->get_param( 'subscriptions' ) ) {
$args = array(
array( '_wc_paypal_braintree_payment_method_token', '_payment_method_token' ),
array( '_wc_braintree_paypal_payment_token', '_payment_method_token' ),
array( '_wc_braintree_cc_token', '_payment_method_token' ),
);
$count = 0;
foreach ( $args as $arg ) {
$this->create_temp_table( 'wc_temp_postmeta', $wpdb->postmeta, $arg[0] );
$this->update_temp_table( 'wc_temp_postmeta', $arg[1] );
$this->insert_from_temp_table( 'wc_temp_postmeta', $wpdb->postmeta, array(
'post_id',
'meta_key',
'meta_value'
), 'post_id', 'meta_key' );
$count += $wpdb->rows_affected;
$this->drop_temp_table( 'wc_temp_postmeta' );
}
wc_braintree_log_info( sprintf( '%s rows inserted into postmeta table.', $count ) );
$message .= sprintf( ' %s subscriptions updated in postmeta table.', $count );
}
if ( empty( $message ) ) {
$message = 'No data updated.';
}
break;
}
return rest_ensure_response( array( 'message' => trim( $message ) ) );
}
private function create_temp_table( $temp_table, $table, $args = array() ) {
global $wpdb;
$wpdb->query( $wpdb->prepare( "CREATE TEMPORARY TABLE {$temp_table} SELECT * FROM {$table} as maintable WHERE maintable.meta_key = %s", $args ) );
}
private function drop_temp_table( $temp_table ) {
global $wpdb;
$wpdb->query( "DROP TEMPORARY TABLE IF EXISTS {$temp_table}" );
}
private function update_temp_table( $temp_table, $args = array() ) {
global $wpdb;
$wpdb->query( $wpdb->prepare( "UPDATE {$temp_table} SET meta_key = %s", $args ) );
}
private function insert_from_temp_table( $temp_table, $table, $columns, $key1, $key2 ) {
global $wpdb;
$wpdb->query(
"INSERT INTO {$table} (" . implode( ', ', $columns ) . ') SELECT ' . implode( ', ', $columns ) .
" FROM {$temp_table} t2 WHERE NOT EXISTS (SELECT 1 FROM {$table} t1 WHERE t1.{$key1} = t2.{$key1} AND t1.{$key2} = t2.{$key2})"
);
}
private function get_update_orders_statement( $table ) {
return "UPDATE {$table} AS postmeta SET postmeta.meta_value = %s WHERE postmeta.meta_value = %s;";
}
}