Created
November 10, 2024 21:02
-
-
Save xnau/65ba550cdd96dfcaff5f567d6cba43a6 to your computer and use it in GitHub Desktop.
Performs some simple compatibility fixes for running WordPress using SQLite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Participants Database SQLite Fix | |
* Description: Fixes certain syntax issues with plugin db queries | |
* Author: Roland Barker, xnau webdesign | |
* Version: 1.1 | |
* Author URI: https://xnau.com | |
* License: GPL3 | |
*/ | |
class xnau_sqlite_query_fix { | |
/** | |
* @var array of database table names to target | |
* | |
* these can be partial names | |
*/ | |
private $db_tables = ['wp_participants_database']; | |
/** | |
* @var array of keywords to check | |
*/ | |
private $keywords = ['default','order','group']; | |
/** | |
* @var string the regex pattern used to escape keywords | |
*/ | |
private $escape_pattern; | |
/** | |
* sets up the filter | |
*/ | |
public function __construct() | |
{ | |
add_filter( 'query', [$this, 'fix_queries'] ); | |
$this->set_escape_pattern(); | |
} | |
/** | |
* fixes syntax issues | |
* | |
* @param string $query | |
*/ | |
public function fix_queries( $query ) | |
{ | |
if ( $this->is_target_query( $query ) ) | |
{ | |
$query = $this->fix_quotes($query); | |
$query = $this->escape_sqlite_keywords($query); | |
// error_log(__METHOD__.' query: '. $query ); | |
} | |
return $query; | |
} | |
/** | |
* tells if the query target one of our tables | |
* | |
* @param string $query | |
* @return bool | |
*/ | |
private function is_target_query( $query ) | |
{ | |
foreach ( $this->db_tables as $table ) | |
{ | |
if ( strpos( $query, $table ) !== false ) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* replaces double quotes with single quotes | |
* | |
* @param string $query | |
* @return string | |
*/ | |
private function fix_quotes( $query ) | |
{ | |
$query = preg_replace( '/"/', "'", $query ); | |
return $query; | |
} | |
/** | |
* fixes unescaped keywords | |
* | |
* @param string $query | |
* @return string | |
*/ | |
private function escape_sqlite_keywords( $query ) | |
{ | |
$query = preg_replace( $this->escape_pattern, '.`$1`', $query ); | |
return $query; | |
} | |
/** | |
* sets up the escape pattern | |
*/ | |
private function set_escape_pattern() | |
{ | |
$this->escape_pattern = '/(?:\.)(' . implode( '|', $this->keywords ) . ')/'; | |
} | |
} | |
new xnau_sqlite_query_fix(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This plugin is best installed as an MU-Plugin so that it can be first in line to apply its compatibility fixes.