Skip to content

Instantly share code, notes, and snippets.

@madeincosmos
Created February 13, 2018 15:47
Show Gist options
  • Save madeincosmos/24d0addbaec01941a1aa79d623ee8537 to your computer and use it in GitHub Desktop.
Save madeincosmos/24d0addbaec01941a1aa79d623ee8537 to your computer and use it in GitHub Desktop.
Fix outdated Stripe tokens in WooCommerce
<?php
/*
* Plugin Name: Fix customer tokens for WooCommerce Stripe
* Plugin URI:
* Description: Fixes outdated customer tokens saved on customers registered before mid 2014
* Author: Maria Gorska
* Author URI:
* License: GPLv3
* Version: 0.1.0
* Version: 0.1.0
* Requires at least: 4.0
* Tested up to: 4.9.4
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package Fix customer tokens for WooCommerce Stripe
* @author Maria Gorska
* @since 1.0
*/
class WC_Tools_Custom_Button {
/**
* __construct function.
*
* @access public
* @return void
*/
function __construct() {
add_filter( 'woocommerce_debug_tools', array( $this,'debug_button' ) );
}
/**
* debug_button function.
*
* @access public
* @param mixed $old
* @return void
*/
function debug_button( $old ) {
$new = array(
'my_custom_button' => array(
'name' => __( 'Fix customer Stripe tokens', '' ),
'button' => __( 'Fix tokens', '' ),
'desc' => __( 'Resets Stripe tokens saved on customer accounts into correct format', '' ),
'callback' => array( $this, 'update_customer_tokens' ),
),
);
$tools = array_merge( $old, $new );
return $tools;
}
/**
* update_customer_tokens function.
*
* @access public
* @return void
*/
function update_customer_tokens( ){
global $wpdb;
$modified_rows = 0;
$query = "SELECT * FROM {$wpdb->prefix}usermeta WHERE meta_key = '_stripe_customer_id' AND meta_value LIKE 'a:%'";
$results = $wpdb->get_results($query);
foreach( $results as $result ) {
$token = unserialize($result->meta_value);
$wpdb->update( "{$wpdb->prefix}usermeta", array( 'meta_value' => $token["customer_id"] ), array( 'umeta_id' => $result->umeta_id ), array( '%s', '%s'), array( '%d' ) );
$modified_rows++;
}
echo '<div class="updated"><p>' . __( $modified_rows . ' customer tokens updated. ' , '' ) . '</p></div>';
}
}
$GLOBALS['WC_Tools_Custom_Button'] = new WC_Tools_Custom_Button();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment