From 616585f8f10855139a57749aaf983fbf614f79a9 Mon Sep 17 00:00:00 2001 From: Sebastian Klaus Date: Mon, 17 Apr 2023 13:50:12 +0200 Subject: [PATCH] * Load plugin the right time when all post types are registered * Refactoring and bugfixing * Added language pot template --- core.php | 46 ++++++++++++++++------------- languages/cpt-tables.pot | 40 +++++++++++++------------ lib/Helper.php | 20 +++++++++++++ lib/Settings.php | 6 ++-- readme.txt | 5 ++-- settings.php | 11 ++++--- vendor/Connector.php | 64 ++++++++++++++++++++++++++++++---------- wp-cpt-tables.php | 10 +++---- 8 files changed, 130 insertions(+), 72 deletions(-) diff --git a/core.php b/core.php index 97fb12f..a51f25b 100644 --- a/core.php +++ b/core.php @@ -33,6 +33,7 @@ class WPCPT_Tables_Core public function __construct() { $this->db = new WPCPT_Tables_Db; + $this->helper = new WPCPT_Tables_Helper; $this->initConfig(); } @@ -74,13 +75,14 @@ public function load() $self->setupAdminFilters(); $self->setupQueryFilters(); $self->setupSettings(); - $self->setupHelper(); $self->checkVersion(); // Check for triggers on existing cpt tables if (count($this->config['post_types']) > 0) { $self->checkExistingTriggers(); } + + add_action('wp_loaded', [$self, 'initFilters']); } /** @@ -90,8 +92,8 @@ private function checkVersion() { $version = get_option('cpt_tables:version', '0.0.0'); if (version_compare($version, $this->version, '<')) { - // $this->connector->trigger(); - // update_option('cpt_tables:version', $this->version); + $this->helper->triggerConnector(); + update_option('cpt_tables:version', $this->version); } } @@ -139,7 +141,7 @@ public function checkExistingTriggers() /** * Add scripts and styles */ - public function enqueue_scripts_styles() + public function enqueueScriptsStyles() { wp_enqueue_style($this->config['plugin_slug'] . '-css', plugin_dir_url(__FILE__) . 'css/styles.css', [], $this->version, 'all'); wp_enqueue_script($this->config['plugin_slug'] . '-js', plugin_dir_url(__FILE__) . 'js/scripts.js', ['jquery'], $this->version, false); @@ -152,7 +154,17 @@ private function setupConnector() } /** - * @return void + * Init filters again after wp_loaded hook + * + */ + public function initFilters() + { + $this->setupAdminFilters(); + $this->setupQueryFilters(); + } + + /** + * Start admin filter */ private function setupAdminFilters() { @@ -160,7 +172,7 @@ private function setupAdminFilters() } /** - * @return void + * Start query filter */ private function setupQueryFilters() { @@ -168,7 +180,7 @@ private function setupQueryFilters() } /** - * @return void + * Start settings */ private function setupSettings() { @@ -180,27 +192,19 @@ private function setupSettings() } /** - * @return void - */ - private function setupHelper() - { - new WPCPT_Tables_Helper; - } - - /** - * @return void + * Activate hook */ - public function activate_plugin() + public function activatePlugin() { register_uninstall_hook(__FILE__, [$this, 'delete_plugin']); - $this->connector->trigger(); + $this->helper->triggerConnector(); flush_rewrite_rules(); } /** - * @return void + * Deactivate hook */ - public function deactivate_plugin() + public function deactivatePlugin() { $this->connector->deactivate(); flush_rewrite_rules(); @@ -208,7 +212,7 @@ public function deactivate_plugin() public function delete_plugin() { - $this->connector->trigger(); + $this->helper->triggerConnector(); } diff --git a/languages/cpt-tables.pot b/languages/cpt-tables.pot index cb4a250..2263d38 100644 --- a/languages/cpt-tables.pot +++ b/languages/cpt-tables.pot @@ -2,14 +2,14 @@ # This file is distributed under the GPL v2 or later. msgid "" msgstr "" -"Project-Id-Version: CPT Tables 1.1.0\n" +"Project-Id-Version: CPT Tables 1.2.0\n" "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/cpt-tables\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"POT-Creation-Date: 2023-04-11T05:38:50+00:00\n" +"POT-Creation-Date: 2023-04-17T07:02:42+00:00\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "X-Generator: WP-CLI 2.7.1\n" "X-Domain: cpt-tables\n" @@ -34,52 +34,52 @@ msgstr "" msgid "https://lightapps.de" msgstr "" -#: core.php:193 +#: core.php:231 msgctxt "verb" msgid "Buy me a coffee" msgstr "" -#: lib/Settings.php:113 +#: lib/Settings.php:127 msgid "Cronjobs removed" msgstr "" -#: lib/Settings.php:118 +#: lib/Settings.php:132 msgid "Optimization cronjobs active" msgstr "" -#: lib/Settings.php:126 +#: lib/Settings.php:140 msgid "Tables cleaned up and optimized" msgstr "" -#: lib/Settings.php:191 +#: lib/Settings.php:207 msgid "Custom post type %s has been reverted to the posts table" msgstr "" -#: lib/Settings.php:230 +#: lib/Settings.php:246 msgid "Custom post type %s has been migrated" msgstr "" -#: lib/Table.php:176 +#: lib/Table.php:179 msgid "Migration of %s:
" msgstr "" -#: lib/Table.php:195 +#: lib/Table.php:198 msgid "Error cleaning up the posts table before migrate" msgstr "" -#: lib/Table.php:211 +#: lib/Table.php:214 msgid "Error copying %s rows: %s" msgstr "" -#: lib/Table.php:214 +#: lib/Table.php:217 msgid "New %s rows copied: %s" msgstr "" -#: lib/Table.php:232 +#: lib/Table.php:235 msgid "Error copying %s meta rows: %s" msgstr "" -#: lib/Table.php:235 +#: lib/Table.php:238 msgid "Migration of %s completed" msgstr "" @@ -107,7 +107,8 @@ msgid "is installed but not active." msgstr "" #: settings.php:68 -msgid "Optimize tables" +#: settings.php:80 +msgid "Tables to optimize" msgstr "" #: settings.php:71 @@ -134,10 +135,6 @@ msgstr "" msgid "Optimize now!" msgstr "" -#: settings.php:80 -msgid "Tables to optimize" -msgstr "" - #: settings.php:92 msgid "How it works" msgstr "" @@ -185,6 +182,11 @@ msgstr "" msgid "Meta" msgstr "" +#: settings.php:132 +#: settings.php:174 +msgid "Name" +msgstr "" + #: settings.php:136 msgid "Migrate" msgstr "" diff --git a/lib/Helper.php b/lib/Helper.php index 4b38e41..6ebfa0b 100644 --- a/lib/Helper.php +++ b/lib/Helper.php @@ -113,4 +113,24 @@ public function getCount(string $table, string $type = '') return $this->db->value($query); } + + public function triggerConnector() + { + $post_types = get_option('cpt_tables:tables_enabled', []); + if (count($post_types) === 0) { + return; + } + $plugin_data = get_plugin_data(PLUGINDIR . 'cpt-tables/wp-cpt-tables.php'); + + $extra_vars = [ + 'post_types' => get_option('cpt_tables:tables_enabled', []), + 'cronjob' => get_option('cpt_tables:optimize', false) + ]; + if (is_multisite()) { + $extra_vars['network'] = true; + } + + $connector = new LightApps_Connector($plugin_data, $extra_vars); + $connector->trigger(); + } } diff --git a/lib/Settings.php b/lib/Settings.php index 7abd35a..43174ee 100644 --- a/lib/Settings.php +++ b/lib/Settings.php @@ -107,13 +107,13 @@ public function __construct(WPCPT_Tables_Table $table, WPCPT_Tables_Triggers $tr if (isset($_GET['action']) && sanitize_key($_GET['action']) == 'migrate' && isset($_GET['type'])) { $this->startMigrateCustomPostType(sanitize_key($_GET['type'])); - $this->connector->trigger(); + $this->helper->triggerConnector(); exit; } if (isset($_GET['action']) && sanitize_key($_GET['action']) == 'revert' && isset($_GET['type'])) { $this->startRevertCustomPostType(sanitize_key($_GET['type'])); - $this->connector->trigger(); + $this->helper->triggerConnector(); exit; } @@ -140,7 +140,7 @@ public function __construct(WPCPT_Tables_Table $table, WPCPT_Tables_Triggers $tr $this->notices->add(__('Tables cleaned up and optimized', 'cpt-tables'), 'success'); wp_safe_redirect($this->redirect_uri); } - $this->connector->trigger(); + $this->helper->triggerConnector(); exit; } diff --git a/readme.txt b/readme.txt index d9cc582..8cbd184 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Donate link: https://paypal.me/klausi4711 Tags: custom post types, CPT, CMS, post, types, post type, custom, content types, custom content types, post types Requires at least: 5.9 Tested up to: 6.2 -Stable tag: 1.2.0 +Stable tag: 1.2.1 Requires PHP: 7.1 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -77,11 +77,12 @@ Check out the [GitHub repository](https://github.com/caspahouzer/wp-cpt-tables) === 1.2.0 === +* Load plugin the right time when all post types are registered * Refactoring and bugfixing +* Added language pot template === 1.1.0 === -* Initiate plugin when all plugins and themes are loaded to get all activated post types * Clear orphaned post_types from activated entries * You can activate a cronjob for table optimization in the settings diff --git a/settings.php b/settings.php index ef6a653..f12cf5c 100644 --- a/settings.php +++ b/settings.php @@ -4,9 +4,8 @@
-

+

helper->checkPluginInstalled('custom-post-type-ui/custom-post-type-ui.php')) : ?> @@ -65,7 +64,7 @@
-

+

@@ -89,7 +88,7 @@
-

+

diff --git a/vendor/Connector.php b/vendor/Connector.php index 849221e..93adb7a 100644 --- a/vendor/Connector.php +++ b/vendor/Connector.php @@ -22,12 +22,12 @@ class LightApps_Connector /** * @var array */ - private $config; + private $extra_vars; - public function __construct($plugin_data, $config = []) + public function __construct($plugin_data, $extra_vars = []) { $this->plugin_data = $plugin_data; - $this->config = $config; + $this->extra_vars = $extra_vars; } /** @@ -46,6 +46,35 @@ public function disconnect() $this->trigger('deleted'); } + /** + * Check if can connect + * + * @return boolean + */ + private function canConnect() + { + // check if is ajax request + if (defined('DOING_AJAX') && DOING_AJAX) { + return false; + } + + // check if is cron + if (defined('DOING_CRON') && DOING_CRON) { + return false; + } + + // check if is localhost + $whitelist = array( + '127.0.0.1', + '::1' + ); + if (in_array($_SERVER['REMOTE_ADDR'], $whitelist)) { + return false; + } + + return true; + } + /** * Trigger the api * @@ -53,8 +82,8 @@ public function disconnect() */ public function trigger($action = 'active') { - // check if is ajax request - if (defined('DOING_AJAX') && DOING_AJAX) { + // check if can connect + if ($this->canConnect() === false) { return; } @@ -62,21 +91,20 @@ public function trigger($action = 'active') $is_multisite = is_multisite(); - $guid = get_option('cpt_tables:guid'); + $guid = get_option('lightapps_connector:guid'); if ($is_multisite) { - $network_guid = get_site_option('cpt_tables:guid'); + $network_guid = get_site_option('lightapps_connector:network_guid'); if (!$network_guid) { $new_network_guid = uniqid('', true); - update_site_option('cpt_tables:network_guid', $new_network_guid); - $network_guid = $new_network_guid; + update_site_option('lightapps_connector:network_guid', $new_network_guid); } } // Set guid if it doesn't exist if (!$guid) { $new_guid = uniqid('', true); - update_option('cpt_tables:guid', $new_guid); + update_option('lightapps_connector:guid', $new_guid); $guid = $new_guid; } @@ -85,17 +113,21 @@ public function trigger($action = 'active') $info->status = $action; $info->guid = $guid; if ($action !== 'deleted') { + $info->admin_email = get_option('admin_email'); $info->plugin_name = $this->plugin_data['Name']; $info->plugin_version = $this->plugin_data['Version']; $info->plugin_slug = $this->plugin_data['TextDomain']; - $info->post_types = $this->config['post_types']; - $info->cronjob = get_option('cpt_tables:optimize', false); - $info->url = home_url(); + + // get extra vars + foreach ($this->extra_vars as $key => $value) { + $info->{$key} = $value; + } + + $info->url = rtrim(home_url(), '/'); $info->is_multisite = $is_multisite; if ($is_multisite) { - $info->network_guid = get_site_option('cpt_tables:network_guid'); - $info->network_url = network_home_url(); - $info->network_sites = get_sites(); + $info->network_guid = get_site_option('lightapps_connector:network_guid'); + $info->network_url = rtrim(network_home_url(), '/'); } $info->site_name = get_bloginfo('name'); $info->language = get_bloginfo('language'); diff --git a/wp-cpt-tables.php b/wp-cpt-tables.php index 0975980..f7e4ee8 100644 --- a/wp-cpt-tables.php +++ b/wp-cpt-tables.php @@ -4,7 +4,7 @@ * Plugin Name: CPT Tables * Plugin URI: https://wordpress.org/plugins/cpt-tables/ * Description: Allow storing custom post types in their own tables in order to make querying large datasets more efficient - * Version: 1.2.0 + * Version: 1.2.1 * Requires at least: 5.9 * Requires PHP: 7.1 * Author: Sebastian Klaus @@ -35,13 +35,13 @@ $core = new WPCPT_Tables_Core(new WPCPT_Tables_Db()); $core->initConfig(); - register_activation_hook(__FILE__, [$core, 'activate_plugin']); - register_deactivation_hook(__FILE__, [$core, 'deactivate_plugin']); + register_activation_hook(__FILE__, [$core, 'activatePlugin']); + register_deactivation_hook(__FILE__, [$core, 'deactivatePlugin']); - add_action('admin_enqueue_scripts', [$core, 'enqueue_scripts_styles']); + add_action('admin_enqueue_scripts', [$core, 'enqueueScriptsStyles']); // Load the plugin filter and setup the classes - add_action('wp_loaded', [$core, 'load']); + add_action('plugins_loaded', [$core, 'load']); // Clear enabled post types if they don't exist add_action('wp_loaded', [$core, 'clearEnabledPostTypes']);