| 1 |
<?php |
| 2 |
|
| 3 |
class Meow_DBCLNR_Support |
| 4 |
{ |
| 5 |
public $core = null; |
| 6 |
public $admin = null; |
| 7 |
public $is_rest = false; |
| 8 |
public $is_cli = false; |
| 9 |
public $site_url = null; |
| 10 |
public $prefix = ""; |
| 11 |
public $active_plugins = null; |
| 12 |
|
| 13 |
static public $core_tables = [ 'postmeta', 'posts', |
| 14 |
'terms', 'termmeta', 'term_relationships', 'term_taxonomy', |
| 15 |
'usermeta', 'users', 'commentmeta', 'comments', 'links', 'options' ]; |
| 16 |
static public $core_post_types = [ 'attachment', 'page', 'post', 'revision', 'custom_css', |
| 17 |
'nav_menu_item', 'oembed_cache', 'wp_block', 'customize_changeset', 'wp_global_styles' ]; |
| 18 |
|
| 19 |
private $support_loaded = false; |
| 20 |
private $table_to_plugin = null; |
| 21 |
private $post_type_to_plugin = null; |
| 22 |
private $option_to_plugin = null; |
| 23 |
private $cron_to_plugin = null; |
| 24 |
private $metadata_to_plugin = null; |
| 25 |
|
| 26 |
public function __construct( $core ) { |
| 27 |
$this->core = $core; |
| 28 |
add_filter( 'dbclnr_check_table_info', array( $this, 'check_table_info' ), 10, 1 ); |
| 29 |
add_filter( 'dbclnr_check_post_type_info', array( $this, 'check_post_type_info' ), 10, 1 ); |
| 30 |
add_filter( 'dbclnr_check_option_info', array( $this, 'check_option_info' ), 10, 1 ); |
| 31 |
add_filter( 'dbclnr_check_cron_info', array( $this, 'check_cron_info' ), 10, 1 ); |
| 32 |
add_filter( 'dbclnr_check_metadata_info', array( $this, 'check_metadata_info' ), 10, 1 ); |
| 33 |
} |
| 34 |
|
| 35 |
function load_support_db() { |
| 36 |
if ( !$this->support_loaded ) { |
| 37 |
$this->support_loaded = new Meow_DBCLNR_Support_Core(); |
| 38 |
do_action( 'dbclnr_support_db_loaded' ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
function get_table_to_plugin() { |
| 43 |
if ( $this->table_to_plugin === null ) { |
| 44 |
$this->load_support_db(); |
| 45 |
$this->table_to_plugin = apply_filters( 'dbclnr_table_to_plugin', [] ); |
| 46 |
} |
| 47 |
return $this->table_to_plugin; |
| 48 |
} |
| 49 |
|
| 50 |
function get_post_type_to_plugin() { |
| 51 |
if ( $this->post_type_to_plugin === null ) { |
| 52 |
$this->load_support_db(); |
| 53 |
$this->post_type_to_plugin = apply_filters( 'dbclnr_post_type_to_plugin', [] ); |
| 54 |
} |
| 55 |
return $this->post_type_to_plugin; |
| 56 |
} |
| 57 |
|
| 58 |
function get_option_to_plugin() { |
| 59 |
if ( $this->option_to_plugin === null ) { |
| 60 |
$this->load_support_db(); |
| 61 |
$this->option_to_plugin = apply_filters( 'dbclnr_option_to_plugin', [] ); |
| 62 |
} |
| 63 |
return $this->option_to_plugin; |
| 64 |
} |
| 65 |
|
| 66 |
function get_cron_to_plugin() { |
| 67 |
if ( $this->cron_to_plugin === null ) { |
| 68 |
$this->load_support_db(); |
| 69 |
$this->cron_to_plugin = apply_filters( 'dbclnr_cron_to_plugin', [] ); |
| 70 |
} |
| 71 |
return $this->cron_to_plugin; |
| 72 |
} |
| 73 |
|
| 74 |
function get_metadata_to_plugin() { |
| 75 |
if ( $this->metadata_to_plugin === null ) { |
| 76 |
$this->load_support_db(); |
| 77 |
$this->metadata_to_plugin = apply_filters( 'dbclnr_metadata_to_plugin', [] ); |
| 78 |
} |
| 79 |
return $this->metadata_to_plugin; |
| 80 |
} |
| 81 |
|
| 82 |
function get_active_plugins_list() { |
| 83 |
|
| 84 |
if ( $this->active_plugins === null ) { |
| 85 |
$this->active_plugins = wp_get_active_and_valid_plugins(); |
| 86 |
|
| 87 |
foreach ( $this->active_plugins as &$plugin ) { |
| 88 |
$exploded = explode( '/wp-content/plugins/', $plugin ); |
| 89 |
$name = explode( '/', $exploded[1] )[0]; |
| 90 |
|
| 91 |
$plugin = preg_replace( '/\-pro$/', '', $name ); |
| 92 |
} |
| 93 |
|
| 94 |
$themes = wp_get_themes(); |
| 95 |
foreach ( $themes as $path => $data ) { |
| 96 |
$dir = explode( '/', $path ); |
| 97 |
$this->active_plugins[] = $dir[0]; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return $this->active_plugins; |
| 102 |
} |
| 103 |
|
| 104 |
function check_support( $item_name, $support_table, $kind = 'table' ) { |
| 105 |
$active_plugins = $this->get_active_plugins_list(); |
| 106 |
$status = [ 'status' => 'n/a', 'usedBy' => "" ]; |
| 107 |
$hasFound = false; |
| 108 |
if ( key_exists( $item_name, $support_table ) ) { |
| 109 |
$plugins = $support_table[$item_name]; |
| 110 |
if ( $plugins === 'WP' ) { |
| 111 |
$status = [ 'status' => 'ok', 'usedBy' => "WordPress" ]; |
| 112 |
$hasFound = true; |
| 113 |
} |
| 114 |
else { |
| 115 |
$main_plugin = null; |
| 116 |
$hasFound = false; |
| 117 |
foreach ( $plugins as $plugin ) { |
| 118 |
if ( $hasFound ) { |
| 119 |
// I don't like this double break! Maybe we could make this more beautiful (in terms of coding). |
| 120 |
break; |
| 121 |
} |
| 122 |
$main_plugin = ( empty( $main_plugin ) || isset( $plugin['native']) ) ? $plugin : $main_plugin; |
| 123 |
if ( is_array( $plugin ) ) { |
| 124 |
foreach ( $plugin['slugs'] as $slug ) { |
| 125 |
if ( $slug === '*' || in_array( $slug, $active_plugins ) ) { |
| 126 |
$status = [ |
| 127 |
'status' => 'ok', |
| 128 |
'usedBy' => $plugin['plugin'], |
| 129 |
'custom' => isset( $plugin['custom'] ) ? $plugin['custom'] : null |
| 130 |
]; |
| 131 |
$hasFound = true; |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
else { |
| 137 |
error_log( 'Database Cleaner: Plugin support was badly set up (' . $plugin . ')' ); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
if ( !$hasFound ) { |
| 142 |
$status = [ |
| 143 |
'status' => 'warn', |
| 144 |
'usedBy' => $main_plugin['plugin'], |
| 145 |
'custom' => isset( $main_plugin['custom'] ) ? $main_plugin['custom'] : null |
| 146 |
]; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// if ( $item_name === 'edd_sl_4a863a79162200176337a215508377b7' ) { |
| 151 |
// error_log( "Let's look into item : " . $item_name ); |
| 152 |
// } |
| 153 |
|
| 154 |
|
| 155 |
// If custom, should be returned right away |
| 156 |
if ( isset( $status['custom'] ) && $status['custom'] ) { |
| 157 |
return $status; |
| 158 |
} |
| 159 |
|
| 160 |
// If not found, let's look a bit more |
| 161 |
if ( !$hasFound ) { |
| 162 |
$status = apply_filters( "dbclnr_check_support_for_{$kind}", $status, $item_name, $active_plugins ); |
| 163 |
} |
| 164 |
|
| 165 |
return $status; |
| 166 |
} |
| 167 |
|
| 168 |
function check_table_info( $table_name ) |
| 169 |
{ |
| 170 |
// Remove the prefix (to original the real table name) |
| 171 |
if ( strpos( $table_name, $this->core->prefix ) === 0 ) { |
| 172 |
$table_name = substr( $table_name, strlen( $this->core->prefix ) ); |
| 173 |
} |
| 174 |
return $this->check_support( $table_name, $this->get_table_to_plugin(), 'table' ); |
| 175 |
} |
| 176 |
|
| 177 |
function check_post_type_info( $post_type ) { |
| 178 |
return $this->check_support( $post_type, $this->get_post_type_to_plugin(), 'post_type' ); |
| 179 |
} |
| 180 |
|
| 181 |
function check_option_info( $option ) { |
| 182 |
return $this->check_support( $option, $this->get_option_to_plugin(), 'option' ); |
| 183 |
} |
| 184 |
|
| 185 |
function check_cron_info( $cron ) { |
| 186 |
return $this->check_support( $cron, $this->get_cron_to_plugin(), 'cron' ); |
| 187 |
} |
| 188 |
|
| 189 |
function check_metadata_info( $metadata_key ) { |
| 190 |
return $this->check_support( $metadata_key, $this->get_metadata_to_plugin(), 'metadata' ); |
| 191 |
} |
| 192 |
} |