| @@ -105,4 +105,46 @@ | ||
| 105 | 105 | |
| 106 | 106 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 107 | 107 | dbDelta( $sql ); |
| 108 | 108 | } |
| 109 | + | |
| 110 | + | |
| 111 | +/** | |
| 112 | + * Add an index on (funnel_id, status) to the stats table to avoid full | |
| 113 | + * table scans in funnel-level analytics lookups (e.g. the funnel canvas). | |
| 114 | + * | |
| 115 | + * Guarded with SHOW INDEX so it stays idempotent on sites where the index | |
| 116 | + * may already have been applied manually. | |
| 117 | + * | |
| 118 | + * Return value follows the batched-migration contract documented on | |
| 119 | + * Wpfnl_Activator::run_update_callback_end(): true means "there is more work | |
| 120 | + * left, queue me again", false means "finished". This callback completes in a | |
| 121 | + * single pass, so it always returns false. Returning true here is what caused | |
| 122 | + * the runaway ActionScheduler loop fixed in 3.12.13 — do not change it back. | |
| 123 | + * | |
| 124 | + * @return bool Always false; this migration never needs a second pass. | |
| 125 | + * | |
| 126 | + * @since 3.12.12 | |
| 127 | + */ | |
| 128 | +function wpf_add_31212_stats_funnel_status_index() { | |
| 129 | + global $wpdb; | |
| 130 | + $table = $wpdb->prefix . 'wpfnl_stats'; | |
| 131 | + | |
| 132 | + // SHOW INDEX errors outright on a missing table, so confirm it exists | |
| 133 | + // first. Sites that predate the stats table get it from the 3.5.0 | |
| 134 | + // callback, which ActionScheduler runs ahead of this one. | |
| 135 | + $table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ); // phpcs:ignore | |
| 136 | + if ( ! $table_exists ) { | |
| 137 | + return false; | |
| 138 | + } | |
| 139 | + | |
| 140 | + $exists = $wpdb->get_var( $wpdb->prepare( | |
| 141 | + 'SHOW INDEX FROM ' . $table . ' WHERE Key_name = %s', | |
| 142 | + 'idx_funnel_status' | |
| 143 | + ) ); // phpcs:ignore | |
| 144 | + | |
| 145 | + if ( ! $exists ) { | |
| 146 | + $wpdb->query( "ALTER TABLE {$table} ADD INDEX idx_funnel_status (funnel_id, status)" ); // phpcs:ignore | |
| 147 | + } | |
| 148 | + | |
| 149 | + return false; | |
| 150 | +} | |