prefix . 'ninetytwo_notify_subscribers'; $charset_collate = $wpdb->get_charset_collate(); return "CREATE TABLE $table_name ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, email varchar(255) NOT NULL DEFAULT '', product_id bigint(20) unsigned NOT NULL DEFAULT 0, variation_id bigint(20) unsigned NOT NULL DEFAULT 0, product_name varchar(255) NOT NULL DEFAULT '', product_url varchar(255) NOT NULL DEFAULT '', tags varchar(255) NOT NULL DEFAULT '', varieties varchar(100) NOT NULL DEFAULT '', status tinyint(1) NOT NULL DEFAULT 1, unsubscribe_token varchar(64) NOT NULL DEFAULT '', notified_at datetime NULL DEFAULT NULL, metadata longtext NULL, created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY unsubscribe_token (unsubscribe_token), UNIQUE KEY email_product (email, product_id, variation_id), KEY product_lookup (product_id, variation_id, status), KEY status (status) ) $charset_collate;"; } /** * Runs dbDelta() against the current schema. Safe to call repeatedly — * dbDelta only issues the ALTER/CREATE statements actually needed. */ function ninetytwo_notify_create_or_upgrade_table() { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( ninetytwo_notify_get_subscribers_table_schema() ); } register_activation_hook( NINETYTWO_NOTIFY_PLUGIN_FILE, 'ninetytwo_notify_create_or_upgrade_table' ); /** * One-time data fixups that dbDelta can't express, run before the schema * migration that depends on them (e.g. de-duplicating rows before adding a * UNIQUE key across them, or backfilling a new NOT-NULL-in-spirit column). * Each step is idempotent and safe to run more than once. * * @param string $from_version The previously stored ninetytwo_notify_db_version, or '0' if none. */ function ninetytwo_notify_run_data_migrations( $from_version ) { global $wpdb; $table_name = $wpdb->prefix . 'ninetytwo_notify_subscribers'; if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) ) !== $table_name ) { return; } if ( version_compare( $from_version, '2.0.0', '<' ) ) { // Remove exact duplicate subscriptions (same email/product/variation) // before the new UNIQUE KEY is added below, keeping the earliest row. $wpdb->query( $wpdb->prepare( 'DELETE t1 FROM %i t1 INNER JOIN %i t2 ON t1.email = t2.email AND t1.product_id = t2.product_id AND t1.variation_id = t2.variation_id AND t1.id > t2.id', $table_name, $table_name ) ); // Backfill a secure unsubscribe token for rows created before tokens existed. $rows_needing_token = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM %i WHERE unsubscribe_token = '' OR unsubscribe_token IS NULL", $table_name ) ); foreach ( $rows_needing_token as $row_id ) { $wpdb->update( $table_name, array( 'unsubscribe_token' => ninetytwo_notify_generate_unsubscribe_token() ), array( 'id' => $row_id ), array( '%s' ), array( '%d' ) ); } ninetytwo_notify_migrate_category_option_to_term_ids(); // The SMS/Twilio feature (and its phone/sms_count columns) has been // removed entirely. dbDelta never drops columns, so do it explicitly; // guarded so it's a no-op on a table that's already been migrated. $existing_columns = $wpdb->get_col( $wpdb->prepare( 'SHOW COLUMNS FROM %i', $table_name ), 0 ); foreach ( array( 'phone', 'sms_count' ) as $legacy_column ) { if ( in_array( $legacy_column, $existing_columns, true ) ) { $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i DROP COLUMN %i', $table_name, $legacy_column ) ); } } } } /** * Generates a cryptographically secure, unique unsubscribe token. * * @return string */ function ninetytwo_notify_generate_unsubscribe_token() { global $wpdb; $table_name = $wpdb->prefix . 'ninetytwo_notify_subscribers'; do { $token = bin2hex( random_bytes( 24 ) ); $exists = $wpdb->get_var( $wpdb->prepare( 'SELECT id FROM %i WHERE unsubscribe_token = %s', $table_name, $token ) ); } while ( $exists ); return $token; } /** * The category filter setting used to store term slugs (`ninetytwo_notify_category_slug_label`). * Slugs can change, so this migrates it once to term IDs stored under * `ninetytwo_notify_category_ids_label`, then removes the legacy option. */ function ninetytwo_notify_migrate_category_option_to_term_ids() { if ( false !== get_option( 'ninetytwo_notify_category_ids_label', false ) ) { return; } $legacy_slugs = get_option( 'ninetytwo_notify_category_slug_label' ); if ( empty( $legacy_slugs ) || ! is_array( $legacy_slugs ) ) { delete_option( 'ninetytwo_notify_category_slug_label' ); return; } $term_ids = array(); foreach ( $legacy_slugs as $slug ) { $term = get_term_by( 'slug', $slug, 'product_cat' ); if ( $term && ! is_wp_error( $term ) ) { $term_ids[] = (int) $term->term_id; } } update_option( 'ninetytwo_notify_category_ids_label', array_values( array_unique( $term_ids ) ) ); delete_option( 'ninetytwo_notify_category_slug_label' ); } /** * One-time, idempotent migration off the pre-1.0.0 unprefixed `ntn_*` table * and option names used only during this plugin's pending-review development * period. Renames the subscriber table (preserving its data) and copies each * legacy option's value to its new `ninetytwo_notify_` name, then removes the * legacy name, so no unprefixed identifiers are left orphaned going forward. * Safe to run on every request: each step only acts when the legacy name is * still present and the new name isn't already set. */ function ninetytwo_notify_migrate_legacy_identifiers() { global $wpdb; $old_table = $wpdb->prefix . 'ntn_subscribers'; $new_table = $wpdb->prefix . 'ninetytwo_notify_subscribers'; $old_table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $old_table ) ) === $old_table; $new_table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $new_table ) ) === $new_table; if ( $old_table_exists && ! $new_table_exists ) { $wpdb->query( $wpdb->prepare( 'RENAME TABLE %i TO %i', $old_table, $new_table ) ); } $legacy_options = array( 'ntn_delete_data_on_uninstall' => 'ninetytwo_notify_delete_data_on_uninstall', 'ntn_category_ids_label' => 'ninetytwo_notify_category_ids_label', 'ntn_category_slug_label' => 'ninetytwo_notify_category_slug_label', 'ntn_auto_notify_label' => 'ninetytwo_notify_auto_notify_label', 'ntn_similar_products_label' => 'ninetytwo_notify_similar_products_label', 'ntn_db_version' => 'ninetytwo_notify_db_version', ); foreach ( $legacy_options as $old_key => $new_key ) { if ( false === get_option( $new_key, false ) ) { $legacy_value = get_option( $old_key, false ); if ( false !== $legacy_value ) { update_option( $new_key, $legacy_value ); } } delete_option( $old_key ); } } add_action( 'plugins_loaded', 'ninetytwo_notify_migrate_legacy_identifiers', 5 ); /** * Checks the stored schema version on every load and runs the upgrade path * when it's behind, so updates applied without a deactivate/reactivate cycle * (e.g. via auto-update) still get their schema and data migrations. */ function ninetytwo_notify_maybe_upgrade_db() { $installed_version = get_option( 'ninetytwo_notify_db_version', '0' ); if ( version_compare( $installed_version, NINETYTWO_NOTIFY_DB_VERSION, '>=' ) ) { return; } ninetytwo_notify_create_or_upgrade_table(); ninetytwo_notify_run_data_migrations( $installed_version ); update_option( 'ninetytwo_notify_db_version', NINETYTWO_NOTIFY_DB_VERSION ); } add_action( 'plugins_loaded', 'ninetytwo_notify_maybe_upgrade_db' );