| 1 |
<?php |
| 2 |
/** |
| 3 |
* Update to 1.8.12 |
| 4 |
* |
| 5 |
* Remove duplicate postmeta rows on POS-touched posts. |
| 6 |
* |
| 7 |
* Since v1.4.0, the REST API response hooks called save_meta_data() on every |
| 8 |
* read request. On non-HPOS sites this triggered wp_update_post(), which fired |
| 9 |
* save_post — giving third-party plugins (Jetpack, Astra, Xero, etc.) an |
| 10 |
* opportunity to add duplicate meta rows via add_post_meta(). Over 2+ years |
| 11 |
* this accumulated thousands of junk rows per post on some stores. |
| 12 |
* |
| 13 |
* This migration keeps the most recent row (highest meta_id) for each |
| 14 |
* (post_id, meta_key) pair that has more than 20 identical entries, and |
| 15 |
* deletes the rest. Scoped to posts with _woocommerce_pos_uuid meta. |
| 16 |
* |
| 17 |
* @author Paul Kilmurray <paul@kilbot.com> |
| 18 |
* |
| 19 |
* @see http://wcpos.com |
| 20 |
* @package WCPOS\WooCommercePOS |
| 21 |
*/ |
| 22 |
|
| 23 |
namespace WCPOS\WooCommercePOS; |
| 24 |
|
| 25 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 26 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching |
| 27 |
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Update script with file-scoped variables. |
| 28 |
|
| 29 |
global $wpdb; |
| 30 |
|
| 31 |
$wcpos_deleted = $wpdb->query( |
| 32 |
"DELETE pm FROM {$wpdb->postmeta} pm |
| 33 |
INNER JOIN ( |
| 34 |
SELECT post_id, meta_key, MAX(meta_id) AS keep_id |
| 35 |
FROM {$wpdb->postmeta} |
| 36 |
WHERE post_id IN ( |
| 37 |
SELECT DISTINCT post_id FROM {$wpdb->postmeta} |
| 38 |
WHERE meta_key = '_woocommerce_pos_uuid' |
| 39 |
) |
| 40 |
GROUP BY post_id, meta_key |
| 41 |
HAVING COUNT(*) > 20 |
| 42 |
) dups ON pm.post_id = dups.post_id |
| 43 |
AND pm.meta_key = dups.meta_key |
| 44 |
AND pm.meta_id != dups.keep_id" |
| 45 |
); |
| 46 |
|
| 47 |
if ( \function_exists( 'wc_get_logger' ) ) { |
| 48 |
$wcpos_logger = wc_get_logger(); |
| 49 |
if ( false === $wcpos_deleted ) { |
| 50 |
$wcpos_logger->error( |
| 51 |
'WCPOS 1.8.12 migration: database query failed when removing duplicate meta rows.', |
| 52 |
array( 'source' => 'woocommerce-pos' ) |
| 53 |
); |
| 54 |
} else { |
| 55 |
$wcpos_logger->info( |
| 56 |
sprintf( 'WCPOS 1.8.12 migration: removed %d duplicate meta rows from POS-touched posts.', (int) $wcpos_deleted ), |
| 57 |
array( 'source' => 'woocommerce-pos' ) |
| 58 |
); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// phpcs:enable |
| 63 |
|