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