|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Plugin compatibility layer. |
| 4 | + * |
| 5 | + * Provides string-level translation fallbacks for complex third-party plugin queries |
| 6 | + * that are incompatible with the pure AST SQLite evaluator (e.g. Action Scheduler). |
| 7 | + */ |
| 8 | + |
| 9 | +if ( ! defined( 'ABSPATH' ) ) { |
| 10 | + exit; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Filter SQL queries early to fix plugin compatibility issues. |
| 15 | + * |
| 16 | + * @param string $query The SQL query. |
| 17 | + * @return string Modified query. |
| 18 | + */ |
| 19 | +function wp_sqlite_integration_plugin_compat( $query ) { |
| 20 | + if ( ! is_string( $query ) ) { |
| 21 | + return $query; |
| 22 | + } |
| 23 | + |
| 24 | + // 1. Heavy cleaning of unsupported MySQL locking clauses. |
| 25 | + // SQLite doesn't support FOR UPDATE, SKIP LOCKED, or NOWAIT anywhere. |
| 26 | + // We strip these globally (case-insensitive, multi-line) to prevent syntax errors in subqueries. |
| 27 | + if ( stripos( $query, 'FOR UPDATE' ) !== false ) { |
| 28 | + $query = preg_replace( '/\s+FOR\s+UPDATE(?:\s+(?:SKIP\s+LOCKED|NOWAIT))?\b/is', '', $query ); |
| 29 | + } |
| 30 | + |
| 31 | + // 2. Action Scheduler specific compatibility fixes. |
| 32 | + if ( stripos( $query, 'actionscheduler' ) !== false ) { |
| 33 | + // Escape the 'group' keyword safely. |
| 34 | + // Action Scheduler sometimes queries an unquoted `group` column. |
| 35 | + $query = preg_replace( '/(?<![\'"`])\bgroup\b(?!\s+by)(?![\'"`])/i', '`group`', $query ); |
| 36 | + |
| 37 | + // Fix 'INSERT wp_actionscheduler...' syntax to include 'INTO'. |
| 38 | + $query = preg_replace( '/INSERT\s+(?!INTO\s+)(wp_actionscheduler_[a-zA-Z0-9_]+)/i', 'INSERT INTO $1', $query ); |
| 39 | + |
| 40 | + // Fix 'UPDATE ... JOIN' syntax manually. |
| 41 | + // Handles variants like "UPDATE table t1 JOIN" and "UPDATE table AS t1 JOIN". |
| 42 | + $pattern = '/UPDATE\s+([^\s]+)\s+(?:AS\s+)?t1\s+JOIN\s*\((.*?)\)\s*(?:AS\s+)?t2\s*ON\s*t1\.action_id\s*=\s*t2\.action_id\s*SET\s*(.*)/is'; |
| 43 | + if ( preg_match( $pattern, $query, $matches ) ) { |
| 44 | + $set_clause = str_ireplace( 't1.', '', $matches[3] ); |
| 45 | + // Extract the SELECT logic from the join to use in an IN clause. |
| 46 | + $query = "UPDATE {$matches[1]} SET {$set_clause} WHERE action_id IN ({$matches[2]})"; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return $query; |
| 51 | +} |
| 52 | + |
| 53 | +if ( function_exists( 'add_filter' ) ) { |
| 54 | + // Execute the compatibility fixes at priority 0 (before other generic manipulations). |
| 55 | + add_filter( 'query', 'wp_sqlite_integration_plugin_compat', 0 ); |
| 56 | +} |
0 commit comments