← All changes
|
compatibility/class-wpo-kd-submissions-compatibility.php
+107
-31
4.5.5
→
4.7.0
View file →
| @@ -4,38 +4,75 @@ | ||
| 4 | 4 | |
| 5 | 5 | if (!class_exists('WPO_KD_Submissions_Compatibility')) : |
| 6 | 6 | |
| 7 | 7 | /** |
| 8 | - * Class to handle compatibility with KD Submissions plugin | |
| 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. | |
| 9 | 13 | */ |
| 10 | 14 | class WPO_KD_Submissions_Compatibility { |
| 11 | 15 | |
| 12 | 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 | + /** | |
| 13 | 31 | * Constructor |
| 14 | 32 | */ |
| 15 | 33 | private function __construct() { |
| 16 | 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')); | |
| 17 | 36 | } |
| 18 | 37 | |
| 19 | 38 | /** |
| 20 | - * Checks if KD Submissions tables should be marked as Elementor tables. If Elementor Pro is installed or active and KD Submissions tables are not marked as Elementor tables, then mark them as Elementor tables. This is needed to make sure that if Elementor Pro is installed, then KD Submissions tables are optimized with Elementor tables. | |
| 39 | + * Associates KD Submissions tables with Elementor Pro when it is present. | |
| 21 | 40 | * |
| 22 | - * @param array $tables | |
| 23 | - * @return array | |
| 41 | + * @param array<int, stdClass> $tables List of database table objects. | |
| 42 | + * @return array<int, stdClass> Updated list with corrected plugin associations. | |
| 24 | 43 | */ |
| 25 | 44 | public function check_kd_submissions_tables($tables) { |
| 26 | - $is_elementor_pro_installed_or_active = $this->is_elementor_pro_installed_or_active(); | |
| 45 | + $kd_submissions_status = $this->get_kd_submissions_status(); | |
| 27 | 46 | $elementor_pro_status = $this->get_elementor_pro_status(); |
| 47 | + $is_elementor_pro_available = $elementor_pro_status['installed'] || $elementor_pro_status['active']; | |
| 28 | 48 | |
| 29 | 49 | foreach ($tables as $key => $table) { |
| 30 | - if (preg_match('/e_submissions_actions_log$/', $table->Name) || (false !== strpos($table->Name, '_e_') && $this->has_plugin_in_list('kd-submissions', $table->plugin_status))) { | |
| 31 | - if ($is_elementor_pro_installed_or_active && !$this->has_plugin_in_list('elementor-pro', $table->plugin_status)) { | |
| 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)) { | |
| 32 | 65 | $tables[$key]->plugin_status[] = array( |
| 33 | - 'plugin' => 'elementor-pro', | |
| 34 | - 'status' => $elementor_pro_status, | |
| 66 | + 'plugin' => self::KD_SUBMISSIONS_PLUGIN, | |
| 67 | + 'status' => $kd_submissions_status, | |
| 35 | 68 | ); |
| 69 | + | |
| 70 | + // Allow to remove the table when KD Submissions is inactive. | |
| 71 | + $tables[$key]->can_be_removed = !$kd_submissions_status['active']; | |
| 36 | 72 | } |
| 37 | 73 | } |
| 74 | + | |
| 38 | 75 | } |
| 39 | 76 | |
| 40 | 77 | return $tables; |
| 41 | 78 | } |
| @@ -40,22 +77,62 @@ | ||
| 40 | 77 | return $tables; |
| 41 | 78 | } |
| 42 | 79 | |
| 43 | 80 | /** |
| 44 | - * Checks plugin status array and checks if selected plugin is already in the list. | |
| 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. | |
| 45 | 83 | * |
| 46 | - * @param string $plugin | |
| 47 | - * @param array $plugin_status | |
| 48 | - * [ | |
| 49 | - * [ | |
| 50 | - * [plugin] => woocommerce | |
| 51 | - * [status] => Array | |
| 52 | - * ( | |
| 53 | - * [installed] => (bool) | |
| 54 | - * [active] => (bool) | |
| 55 | - * ) | |
| 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. | |
| 56 | 107 | * @return bool |
| 57 | 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 | + */ | |
| 58 | 135 | private function has_plugin_in_list($plugin, $plugin_status) { |
| 59 | 136 | foreach ($plugin_status as $plugin_info) { |
| 60 | 137 | if ($plugin === $plugin_info['plugin']) return true; |
| 61 | 138 | } |
| @@ -63,29 +140,28 @@ | ||
| 63 | 140 | return false; |
| 64 | 141 | } |
| 65 | 142 | |
| 66 | 143 | /** |
| 67 | - * Checks if Elementor Pro is installed or active. This is needed to check if KD Submissions tables should be marked as Elementor tables. | |
| 144 | + * Retrieves the installation and activation status of Elementor Pro. | |
| 68 | 145 | * |
| 69 | - * @return boolean | |
| 146 | + * @return array{installed: bool, active: bool} | |
| 70 | 147 | */ |
| 71 | - private function is_elementor_pro_installed_or_active() { | |
| 72 | - $status = $this->get_elementor_pro_status(); | |
| 73 | - | |
| 74 | - return $status['installed'] || $status['active']; | |
| 148 | + private function get_elementor_pro_status() { | |
| 149 | + return WP_Optimize()->get_db_info()->get_plugin_status(self::ELEMENTOR_PRO_PLUGIN); | |
| 75 | 150 | } |
| 76 | 151 | |
| 77 | 152 | /** |
| 78 | - * Get Elementor Pro status | |
| 153 | + * Retrieves the installation and activation status of KD Submissions. | |
| 79 | 154 | * |
| 80 | - * @return array | |
| 155 | + * @return array{installed: bool, active: bool} | |
| 81 | 156 | */ |
| 82 | - private function get_elementor_pro_status() { | |
| 83 | - return WP_Optimize_Database_Information::instance()->get_plugin_status('elementor-pro'); | |
| 157 | + private function get_kd_submissions_status() { | |
| 158 | + return WP_Optimize()->get_db_info()->get_plugin_status(self::KD_SUBMISSIONS_PLUGIN); | |
| 84 | 159 | } |
| 85 | 160 | |
| 161 | + | |
| 86 | 162 | /** |
| 87 | - * Returns singleton instance | |
| 163 | + * Returns the singleton instance of this class. | |
| 88 | 164 | * |
| 89 | 165 | * @return WPO_KD_Submissions_Compatibility |
| 90 | 166 | */ |
| 91 | 167 | public static function instance() { |