Skip to content

Commit

Permalink
* Load plugin the right time when all post types are registered
Browse files Browse the repository at this point in the history
* Refactoring and bugfixing
* Added language pot template
  • Loading branch information
caspahouzer committed Apr 17, 2023
1 parent 097fab3 commit 616585f
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 72 deletions.
46 changes: 25 additions & 21 deletions core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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']);
}

/**
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -152,23 +154,33 @@ 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()
{
new WPCPT_Tables_AdminFilters;
}

/**
* @return void
* Start query filter
*/
private function setupQueryFilters()
{
new WPCPT_Tables_QueryFilters($this->db, $this->config);
}

/**
* @return void
* Start settings
*/
private function setupSettings()
{
Expand All @@ -180,35 +192,27 @@ 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();
}

public function delete_plugin()
{
$this->connector->trigger();
$this->helper->triggerConnector();
}


Expand Down
40 changes: 21 additions & 19 deletions languages/cpt-tables.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\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"
Expand All @@ -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 <strong>%s</strong> has been reverted to the posts table"
msgstr ""

#: lib/Settings.php:230
#: lib/Settings.php:246
msgid "Custom post type <strong>%s</strong> has been migrated"
msgstr ""

#: lib/Table.php:176
#: lib/Table.php:179
msgid "Migration of <strong>%s</strong>:<br/>"
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 ""

Expand Down Expand Up @@ -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
Expand All @@ -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 ""
Expand Down Expand Up @@ -185,6 +182,11 @@ msgstr ""
msgid "Meta"
msgstr ""

#: settings.php:132
#: settings.php:174
msgid "Name"
msgstr ""

#: settings.php:136
msgid "Migrate"
msgstr ""
Expand Down
20 changes: 20 additions & 0 deletions lib/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
6 changes: 3 additions & 3 deletions lib/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
11 changes: 5 additions & 6 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

<?php
$show_multisite_warning = false;

if (is_multisite()) {
if (defined('SITE_ID_CURRENT_SITE') && get_current_blog_id() === SITE_ID_CURRENT_SITE) {
if (is_multisite() && defined('BLOG_ID_CURRENT_SITE')) {
if (defined('BLOG_ID_CURRENT_SITE') && get_current_blog_id() === BLOG_ID_CURRENT_SITE) {
$show_multisite_warning = true;
}
}
Expand All @@ -29,7 +28,7 @@

<div id="postimagediv" class="postbox ">
<div class="postbox-header">
<h2 class="hndle ui-sortable-handle"><?php echo __('No CPT found', 'cpt-tables') ?></h2>
<h2><?php echo __('No CPT found', 'cpt-tables') ?></h2>
</div>
<div class="inside">
<?php if ($this->helper->checkPluginInstalled('custom-post-type-ui/custom-post-type-ui.php')) : ?>
Expand Down Expand Up @@ -65,7 +64,7 @@
<?php $cron_installed = get_option('cpt_tables:optimize', false); ?>
<div class="postbox ">
<div class="postbox-header">
<h2 class="hndle ui-sortable-handle"><?php echo __('Optimize tables', 'cpt-tables') ?></h2>
<h2><?php echo __('Tables to optimize', 'cpt-tables') ?></h2>
</div>
<div class="inside">
<p><?php echo __('The use of meta tables often leaves residual data that is not needed and bloats the database.', 'cpt-tables') ?></p>
Expand All @@ -89,7 +88,7 @@

<div class="postbox ">
<div class="postbox-header">
<h2 class="hndle ui-sortable-handle"><?php echo __('How it works', 'cpt-tables') ?></h2>
<h2><?php echo __('How it works', 'cpt-tables') ?></h2>
</div>
<div class="inside">
<p><?php echo __('As soon as you select a Custom Post Type, a new table is created in the database. This newly created table will be used for all entries of this custom post type.', 'cpt-tables') ?></p>
Expand Down
Loading

0 comments on commit 616585f

Please sign in to comment.