prefix ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned queue/privacy tables and transaction locks require current uncached database state; all values/identifiers are prepared separately. if ( '1' !== (string) $wpdb->get_var( $wpdb->prepare( 'SELECT GET_LOCK(%s, 0)', $lock ) ) ) { return false; } try { $old = get_option( 'oacw_settings', false ); if ( is_array( $old ) ) { $moves = array(); foreach ( array( 'carts', 'events', 'rates' ) as $suffix ) { $source = $wpdb->prefix . 'oacw_' . $suffix; $target = $wpdb->prefix . 'acpw_' . $suffix; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned queue/privacy tables and transaction locks require current uncached database state; all values/identifiers are prepared separately. $has_source = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $source ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned queue/privacy tables and transaction locks require current uncached database state; all values/identifiers are prepared separately. $has_target = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $target ) ) ); if ( $has_source && $has_target ) { update_option( 'acpw_migration_error', 'duplicate_tables', false ); return false; } if ( $has_source ) { // Identifiers are built solely from the trusted WordPress table prefix and fixed suffixes. $moves[] = $wpdb->prepare( '%i TO %i', $source, $target ); } } // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Every rename pair is prepared with %i; only fixed comma delimiters are joined. Schema migration requires uncached plugin-owned table access. if ( $moves && false === $wpdb->query( 'RENAME TABLE ' . implode( ', ', $moves ) ) ) { update_option( 'acpw_migration_error', 'rename_failed', false ); return false; } if ( false === get_option( 'acpw_settings', false ) ) { $old['enabled'] = 0; $old['recovery_enabled'] = 0; if ( ! add_option( 'acpw_settings', $old, '', false ) ) { return false; } } foreach ( array( 'last_cleanup', 'last_sweep' ) as $key ) { $value = get_option( 'oacw_' . $key, false ); if ( false !== $value ) { update_option( 'acpw_' . $key, $value, false ); } } foreach ( array( 'settings', 'db_version', 'last_cleanup', 'last_sweep' ) as $key ) { delete_option( 'oacw_' . $key ); } if ( did_action( 'init' ) ) { self::retire_legacy_schedule(); } else { add_action( 'init', array( __CLASS__, 'retire_legacy_schedule' ), 20 ); } wp_clear_scheduled_hook( 'oacw_sweep' ); update_option( 'acpw_upgrade_notice', 1, false ); } delete_option( 'acpw_migration_error' ); update_option( 'acpw_migrated_legacy', 1, false ); return true; } finally { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin-owned queue/privacy tables and transaction locks require current uncached database state; all values/identifiers are prepared separately. $wpdb->get_var( $wpdb->prepare( 'SELECT RELEASE_LOCK(%s)', $lock ) ); } } public static function migration_error_message() { $code = get_option( 'acpw_migration_error', '' ); if ( 'duplicate_tables' === $code ) { return __( 'Both previous and current storage tables exist. Migration stopped to protect stored carts. Resolve the duplicate storage before enabling tracking.', 'abandoned-cart-webhooks-for-woocommerce' ); } if ( 'rename_failed' === $code ) { return __( 'Storage migration failed. Existing data was not discarded. Check database rename permissions before enabling tracking.', 'abandoned-cart-webhooks-for-woocommerce' ); } return is_string( $code ) ? $code : ''; } public static function retire_legacy_schedule() { if ( function_exists( 'as_unschedule_all_actions' ) ) { as_unschedule_all_actions( 'oacw_sweep' ); as_unschedule_all_actions( 'oacw_deliver' ); } } public static function retire_previous_schedule() { if ( ! did_action( 'init' ) ) { add_action( 'init', array( __CLASS__, 'retire_previous_schedule' ), 20 ); return; } if ( function_exists( 'as_unschedule_all_actions' ) ) { as_unschedule_all_actions( 'acpw_sweep', null, 'abandoned-cart-plugin-for-woocommerce' ); as_unschedule_all_actions( 'acpw_deliver', null, 'abandoned-cart-plugin-for-woocommerce' ); } } public static function session() { if ( ! function_exists( 'WC' ) || ! WC()->session ) { return; } foreach ( array( 'cart_uuid', 'contact', 'submitted_basket', 'capture_token', 'contact_saved_at' ) as $key ) { $value = WC()->session->get( 'oacw_' . $key ); if ( null !== $value ) { if ( null === WC()->session->get( 'acpw_' . $key ) ) { WC()->session->set( 'acpw_' . $key, $value ); } WC()->session->__unset( 'oacw_' . $key ); } } } public static function order_uuid( $order ) { return $order->get_meta( '_acpw_cart_uuid', true ) ?: $order->get_meta( '_oacw_cart_uuid', true ); } /** Recovery tokens use a plugin-specific secret, never WordPress authentication material. */ public static function recovery_token( $uuid ) { $secret = ACPW_Store::recovery_secret(); return hash_hmac( 'sha256', 'acpw-recovery:' . $uuid, $secret ); } }