Skip to content

Instantly share code, notes, and snippets.

@xnau
Created November 10, 2024 21:02
Show Gist options
  • Save xnau/65ba550cdd96dfcaff5f567d6cba43a6 to your computer and use it in GitHub Desktop.
Save xnau/65ba550cdd96dfcaff5f567d6cba43a6 to your computer and use it in GitHub Desktop.
Performs some simple compatibility fixes for running WordPress using SQLite
<?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();
@xnau
Copy link
Author

xnau commented Nov 22, 2024

This plugin is best installed as an MU-Plugin so that it can be first in line to apply its compatibility fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment