Added upgrade routine to sanitize all coupon codes

This commit is contained in:
Claudio Sanches 2020-07-27 16:50:23 -03:00
parent 6ee47b0356
commit be106af910
2 changed files with 36 additions and 0 deletions

View File

@ -149,6 +149,10 @@ class WC_Install {
'wc_update_400_reset_action_scheduler_migration_status',
'wc_update_400_db_version',
),
'4.5.0' => array(
'wc_update_450_sanitize_coupons_code',
'wc_update_450_db_version',
),
);
/**

View File

@ -2110,3 +2110,35 @@ function wc_update_400_reset_action_scheduler_migration_status() {
function wc_update_400_db_version() {
WC_Install::update_db_version( '4.0.0' );
}
/**
* Update DB version to 4.5.0.
*/
function wc_update_450_db_version() {
WC_Install::update_db_version( '4.5.0' );
}
/**
* Sanitize all coupons code.
*/
function wc_update_450_sanitize_coupons_code() {
global $wpdb;
$coupons = $wpdb->get_results( "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = 'shop_coupon';" );
if ( $coupons ) {
foreach ( $coupons as $data ) {
$code = trim( wp_filter_kses( $data->post_title ) );
if ( ! empty( $code ) ) {
$wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->posts} SET post_title = %s WHERE ID = %d",
$code,
$data->ID
)
);
}
}
}
}