| 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 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Associates KD Submissions tables with Elementor Pro when it is present. |
| 39 |
* |
| 40 |
* @param array<int, stdClass> $tables List of database table objects. |
| 41 |
* @return array<int, stdClass> Updated list with corrected plugin associations. |
| 42 |
*/ |
| 43 |
public function check_kd_submissions_tables($tables) { |
| 44 |
$elementor_pro_status = $this->get_elementor_pro_status(); |
| 45 |
$is_elementor_pro_available = $elementor_pro_status['installed'] || $elementor_pro_status['active']; |
| 46 |
|
| 47 |
foreach ($tables as $key => $table) { |
| 48 |
if (!$this->is_kd_submissions_table($table)) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
if ($is_elementor_pro_available && !$this->has_plugin_in_list(self::ELEMENTOR_PRO_PLUGIN, $table->plugin_status)) { |
| 53 |
$tables[$key]->plugin_status[] = array( |
| 54 |
'plugin' => self::ELEMENTOR_PRO_PLUGIN, |
| 55 |
'status' => $elementor_pro_status, |
| 56 |
); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
return $tables; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Determines whether the given table belongs to KD Submissions. |
| 65 |
* |
| 66 |
* A table is identified as a KD Submissions table if its name matches |
| 67 |
* the Elementor submissions actions log pattern, or if it contains the |
| 68 |
* '_e_' infix and is already tagged with the KD Submissions plugin. |
| 69 |
* |
| 70 |
* @param stdClass $table A database table object with a Name property and plugin_status array. |
| 71 |
* @return bool |
| 72 |
*/ |
| 73 |
private function is_kd_submissions_table($table) { |
| 74 |
if (preg_match('/e_submissions_actions_log$/', $table->Name)) { |
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
return false !== strpos($table->Name, '_e_') |
| 79 |
&& $this->has_plugin_in_list(self::KD_SUBMISSIONS_PLUGIN, $table->plugin_status); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Checks whether a given plugin is already present in the plugin status list. |
| 84 |
* |
| 85 |
* @param string $plugin The plugin slug to search for. |
| 86 |
* @param array<int, array{plugin: string, status: array{installed: bool, active: bool}}> $plugin_status The list of plugin status entries. |
| 87 |
* @return bool |
| 88 |
*/ |
| 89 |
private function has_plugin_in_list($plugin, $plugin_status) { |
| 90 |
foreach ($plugin_status as $plugin_info) { |
| 91 |
if ($plugin === $plugin_info['plugin']) return true; |
| 92 |
} |
| 93 |
|
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Retrieves the installation and activation status of Elementor Pro. |
| 99 |
* |
| 100 |
* @return array{installed: bool, active: bool} |
| 101 |
*/ |
| 102 |
private function get_elementor_pro_status() { |
| 103 |
return WP_Optimize()->get_db_info()->get_plugin_status(self::ELEMENTOR_PRO_PLUGIN); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns the singleton instance of this class. |
| 108 |
* |
| 109 |
* @return WPO_KD_Submissions_Compatibility |
| 110 |
*/ |
| 111 |
public static function instance() { |
| 112 |
static $_instance = null; |
| 113 |
if (null === $_instance) { |
| 114 |
$_instance = new self(); |
| 115 |
} |
| 116 |
return $_instance; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
endif; |
| 121 |
|