| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
|
| 5 |
if (!class_exists('WPO_KD_Submissions_Compatibility')) : |
| 6 |
|
| 7 |
/** |
| 8 |
* Handles compatibility with the KD Submissions plugin. |
| 9 |
* |
| 10 |
* Ensures KD Submissions database tables are associated with Elementor Pro |
| 11 |
* when both plugins are present, allowing them to be optimized alongside |
| 12 |
* Elementor tables. |
| 13 |
*/ |
| 14 |
class WPO_KD_Submissions_Compatibility { |
| 15 |
|
| 16 |
/** |
| 17 |
* Elementor Pro plugin identifier. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
const ELEMENTOR_PRO_PLUGIN = 'elementor-pro'; |
| 22 |
|
| 23 |
/** |
| 24 |
* KD Submissions plugin identifier. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
const KD_SUBMISSIONS_PLUGIN = 'kd-submissions'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor |
| 32 |
*/ |
| 33 |
private function __construct() { |
| 34 |
add_filter('wp_optimize_get_tables', array($this, 'check_kd_submissions_tables')); |
| 35 |
add_filter('wp_optimize_get_table', array($this, 'check_kd_submissions_table')); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Associates KD Submissions tables with Elementor Pro when it is present. |
| 40 |
* |
| 41 |
* @param array<int, stdClass> $tables List of database table objects. |
| 42 |
* @return array<int, stdClass> Updated list with corrected plugin associations. |
| 43 |
*/ |
| 44 |
public function check_kd_submissions_tables($tables) { |
| 45 |
$kd_submissions_status = $this->get_kd_submissions_status(); |
| 46 |
$elementor_pro_status = $this->get_elementor_pro_status(); |
| 47 |
$is_elementor_pro_available = $elementor_pro_status['installed'] || $elementor_pro_status['active']; |
| 48 |
|
| 49 |
foreach ($tables as $key => $table) { |
| 50 |
if (!$this->is_kd_submissions_table($table) && !$this->is_elementor_pro_table($table)) { |
| 51 |
continue; |
| 52 |
} |
| 53 |
|
| 54 |
if (($is_elementor_pro_available || $this->is_elementor_pro_table($table)) && !$this->has_plugin_in_list(self::ELEMENTOR_PRO_PLUGIN, $table->plugin_status)) { |
| 55 |
$tables[$key]->plugin_status[] = array( |
| 56 |
'plugin' => self::ELEMENTOR_PRO_PLUGIN, |
| 57 |
'status' => $elementor_pro_status, |
| 58 |
); |
| 59 |
|
| 60 |
// Allow to remove the table when Elementor Pro and KD Submissions are inactive. |
| 61 |
$tables[$key]->can_be_removed = !$elementor_pro_status['active'] && !$kd_submissions_status['active']; |
| 62 |
} else { |
| 63 |
// If Elementor Pro is not available, ensure KD Submissions is associated |
| 64 |
if (!$this->has_plugin_in_list(self::KD_SUBMISSIONS_PLUGIN, $table->plugin_status)) { |
| 65 |
$tables[$key]->plugin_status[] = array( |
| 66 |
'plugin' => self::KD_SUBMISSIONS_PLUGIN, |
| 67 |
'status' => $kd_submissions_status, |
| 68 |
); |
| 69 |
|
| 70 |
// Allow to remove the table when KD Submissions is inactive. |
| 71 |
$tables[$key]->can_be_removed = !$kd_submissions_status['active']; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
return $tables; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Associates a single KD Submissions table with Elementor Pro, if present. |
| 82 |
* If the provided table is not a KD Submissions table, it is returned unchanged. |
| 83 |
* |
| 84 |
* @param stdClass $table A database table object with a Name property and plugin_status array. |
| 85 |
* @return stdClass |
| 86 |
*/ |
| 87 |
public function check_kd_submissions_table($table) { |
| 88 |
if (!property_exists($table, 'Name')) return $table; |
| 89 |
|
| 90 |
$tables = array( |
| 91 |
$table->Name => $table, |
| 92 |
); |
| 93 |
|
| 94 |
$tables = $this->check_kd_submissions_tables($tables); |
| 95 |
|
| 96 |
return $tables[$table->Name]; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Determines whether the given table belongs to KD Submissions. |
| 101 |
* |
| 102 |
* A table is identified as a KD Submissions table if its name matches |
| 103 |
* the Elementor submissions actions log pattern, or if it contains the |
| 104 |
* '_e_' infix and is already tagged with the KD Submissions plugin. |
| 105 |
* |
| 106 |
* @param stdClass $table A database table object with a Name property and plugin_status array. |
| 107 |
* @return bool |
| 108 |
*/ |
| 109 |
private function is_kd_submissions_table($table) { |
| 110 |
return false !== strpos($table->Name, '_e_') |
| 111 |
&& $this->has_plugin_in_list(self::KD_SUBMISSIONS_PLUGIN, $table->plugin_status); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Determines whether the given table belongs to Elementor Pro |
| 116 |
* |
| 117 |
* @param stdClass $table A database table object with a Name property and plugin_status array. |
| 118 |
* @return boolean |
| 119 |
*/ |
| 120 |
private function is_elementor_pro_table($table) { |
| 121 |
if (preg_match('/e_submissions_actions_log$/', $table->Name)) { |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Checks whether a given plugin is already present in the plugin status list. |
| 130 |
* |
| 131 |
* @param string $plugin The plugin slug to search for. |
| 132 |
* @param array<int, array{plugin: string, status: array{installed: bool, active: bool}}> $plugin_status The list of plugin status entries. |
| 133 |
* @return bool |
| 134 |
*/ |
| 135 |
private function has_plugin_in_list($plugin, $plugin_status) { |
| 136 |
foreach ($plugin_status as $plugin_info) { |
| 137 |
if ($plugin === $plugin_info['plugin']) return true; |
| 138 |
} |
| 139 |
|
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Retrieves the installation and activation status of Elementor Pro. |
| 145 |
* |
| 146 |
* @return array{installed: bool, active: bool} |
| 147 |
*/ |
| 148 |
private function get_elementor_pro_status() { |
| 149 |
return WP_Optimize()->get_db_info()->get_plugin_status(self::ELEMENTOR_PRO_PLUGIN); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Retrieves the installation and activation status of KD Submissions. |
| 154 |
* |
| 155 |
* @return array{installed: bool, active: bool} |
| 156 |
*/ |
| 157 |
private function get_kd_submissions_status() { |
| 158 |
return WP_Optimize()->get_db_info()->get_plugin_status(self::KD_SUBMISSIONS_PLUGIN); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
/** |
| 163 |
* Returns the singleton instance of this class. |
| 164 |
* |
| 165 |
* @return WPO_KD_Submissions_Compatibility |
| 166 |
*/ |
| 167 |
public static function instance() { |
| 168 |
static $_instance = null; |
| 169 |
if (null === $_instance) { |
| 170 |
$_instance = new self(); |
| 171 |
} |
| 172 |
return $_instance; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
endif; |
| 177 |
|