Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
caspahouzer committed Apr 17, 2023
1 parent b9e09c3 commit 097fab3
Show file tree
Hide file tree
Showing 16 changed files with 412 additions and 75 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Overall, outsourcing WordPress custom post types to separate tables offers a ran
- Woocommerce
- YOAST etc.
- Advanced Custom Fields
- … and all other registered active custom post types

## Settings

Expand Down
50 changes: 44 additions & 6 deletions core.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ class WPCPT_Tables_Core
*/
private $helper;

/**
* @var LightApps_Connector
*/
private $connector;

/**
* @var string
*/
public $version = '1.0.0';
public $version = '';

/**
* The plugin constructor
Expand Down Expand Up @@ -48,6 +53,10 @@ public function initConfig()
'default_post_table' => $wpdb->prefix . 'posts',
'default_meta_table' => $wpdb->prefix . 'postmeta',
];

$plugin_data = get_plugin_data(dirname(__FILE__) . '/wp-cpt-tables.php');
$this->version = $plugin_data['Version'];
$this->config['version'] = $this->version;
}

/**
Expand All @@ -61,17 +70,31 @@ public function load()
add_filter('network_admin_plugin_action_links_cpt-tables/wp-cpt-tables.php', [$this, 'addActionLinksNetwork'], 10, 2);
add_filter('plugin_row_meta', array($this, 'filterPluginRowMeta'), 10, 2);

$self->setupConnector();
$self->setupAdminFilters();
$self->setupQueryFilters();
$self->setupSettingsPage();
$self->setupSettings();
$self->setupHelper();
$self->checkVersion();

// Check for triggers on existing cpt tables
if (count($this->config['post_types']) > 0) {
$self->checkExistingTriggers();
}
}

/**
* Check the new version
*/
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);
}
}

/**
* Clear enabled post types if they don't exist
* This is needed because the plugin is not aware of post types that are deleted
Expand Down Expand Up @@ -122,6 +145,12 @@ public function enqueue_scripts_styles()
wp_enqueue_script($this->config['plugin_slug'] . '-js', plugin_dir_url(__FILE__) . 'js/scripts.js', ['jquery'], $this->version, false);
}

private function setupConnector()
{
$plugin_data = get_plugin_data(dirname(__FILE__) . '/wp-cpt-tables.php');
$this->connector = new LightApps_Connector($plugin_data, $this->config);
}

/**
* @return void
*/
Expand All @@ -141,9 +170,9 @@ private function setupQueryFilters()
/**
* @return void
*/
private function setupSettingsPage()
private function setupSettings()
{
new WPCPT_Tables_SettingsPage(
new WPCPT_Tables_Settings(
new WPCPT_Tables_Table($this->db, $this->config),
new WPCPT_Tables_Triggers($this->db, $this->config),
$this->config
Expand All @@ -161,19 +190,28 @@ private function setupHelper()
/**
* @return void
*/
public function activate()
public function activate_plugin()
{
register_uninstall_hook(__FILE__, [$this, 'delete_plugin']);
$this->connector->trigger();
flush_rewrite_rules();
}

/**
* @return void
*/
public function deactivate()
public function deactivate_plugin()
{
$this->connector->deactivate();
flush_rewrite_rules();
}

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


/**
* Filters the array of row meta for each plugin in the Plugins list table.
*
Expand Down
111 changes: 105 additions & 6 deletions languages/cpt-tables.pot
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2023-04-10T07:08:01+00:00\n"
"POT-Creation-Date: 2023-04-11T05:38:50+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 Down Expand Up @@ -39,23 +39,23 @@ msgctxt "verb"
msgid "Buy me a coffee"
msgstr ""

#: lib/SettingsPage.php:113
#: lib/Settings.php:113
msgid "Cronjobs removed"
msgstr ""

#: lib/SettingsPage.php:118
#: lib/Settings.php:118
msgid "Optimization cronjobs active"
msgstr ""

#: lib/SettingsPage.php:126
#: lib/Settings.php:126
msgid "Tables cleaned up and optimized"
msgstr ""

#: lib/SettingsPage.php:191
#: lib/Settings.php:191
msgid "Custom post type <strong>%s</strong> has been reverted to the posts table"
msgstr ""

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

Expand Down Expand Up @@ -105,3 +105,102 @@ msgstr ""
#: settings.php:48
msgid "is installed but not active."
msgstr ""

#: settings.php:68
msgid "Optimize tables"
msgstr ""

#: settings.php:71
msgid "The use of meta tables often leaves residual data that is not needed and bloats the database."
msgstr ""

#: settings.php:73
msgid "Cronjob is installed"
msgstr ""

#: settings.php:73
msgid "Remove Cronjob"
msgstr ""

#: settings.php:75
msgid "You can install this cron job to optimize the tables at regular intervals."
msgstr ""

#: settings.php:76
msgid "Install Cronjob"
msgstr ""

#: settings.php:78
msgid "Optimize now!"
msgstr ""

#: settings.php:80
msgid "Tables to optimize"
msgstr ""

#: settings.php:92
msgid "How it works"
msgstr ""

#: settings.php:95
msgid "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."
msgstr ""

#: settings.php:96
msgid "When you migrate the existing entries, the data from the old table is moved to the new table."
msgstr ""

#: settings.php:97
msgid "Backup"
msgstr ""

#: settings.php:97
msgid "This plugin modifies your WordPress database. Backup your database before migrating in case something doesn't work as you expect."
msgstr ""

#: settings.php:109
msgid "Custom Post Types"
msgstr ""

#: settings.php:113
msgid "All custom post types are already using custom tables."
msgstr ""

#: settings.php:119
#: settings.php:161
msgid "Custom Post Type"
msgstr ""

#: settings.php:120
msgid "New Table Name"
msgstr ""

#: settings.php:121
#: settings.php:163
msgid "Entries"
msgstr ""

#: settings.php:122
#: settings.php:164
msgid "Meta"
msgstr ""

#: settings.php:136
msgid "Migrate"
msgstr ""

#: settings.php:152
msgid "Migrated Custom Post Types"
msgstr ""

#: settings.php:156
msgid "No custom post types have been migrated yet."
msgstr ""

#: settings.php:162
msgid "Table Name"
msgstr ""

#: settings.php:178
msgid "Revert"
msgstr ""
4 changes: 4 additions & 0 deletions lib/AdminFilters.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

if (!defined('ABSPATH')) {
exit;
}

class WPCPT_Tables_AdminFilters
{
/**
Expand Down
4 changes: 4 additions & 0 deletions lib/Db.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

if (!defined('ABSPATH')) {
exit;
}

class WPCPT_Tables_Db
{
/**
Expand Down
4 changes: 4 additions & 0 deletions lib/Helper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

if (!defined('ABSPATH')) {
exit;
}

class WPCPT_Tables_Helper
{
/**
Expand Down
4 changes: 4 additions & 0 deletions lib/Notices.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

if (!defined('ABSPATH')) {
exit;
}

class WPCPT_Tables_Notices
{

Expand Down
5 changes: 2 additions & 3 deletions lib/Optimize.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

// If this file is called directly, abort.
if (!defined('WPINC')) {
die;
if (!defined('ABSPATH')) {
exit;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions lib/QueryFilters.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

if (!defined('ABSPATH')) {
exit;
}

class WPCPT_Tables_QueryFilters
{
/**
Expand Down
18 changes: 17 additions & 1 deletion lib/SettingsPage.php → lib/Settings.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?php

if (!defined('ABSPATH')) {
exit;
}

require_once __DIR__ . '/Optimize.php';

// Check if needed functions exists - if not, require them
if (!function_exists('get_plugins') || !function_exists('is_plugin_active')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

class WPCPT_Tables_SettingsPage
class WPCPT_Tables_Settings
{
/**
* The menu and page name for this settings page
Expand Down Expand Up @@ -45,6 +49,12 @@ class WPCPT_Tables_SettingsPage
*/
private $helper;

/**
* The connector class
* @var LightApps_Connector
*/
private $connector;

/**
* The config array
* @var array
Expand Down Expand Up @@ -88,18 +98,22 @@ public function __construct(WPCPT_Tables_Table $table, WPCPT_Tables_Triggers $tr

$this->config = $config;

$this->connector = new LightApps_Connector($this->config);

$this->table = $table;
$this->triggers = $triggers;

$this->redirect_uri = admin_url('options-general.php?page=' . $this->config['plugin_slug']);

if (isset($_GET['action']) && sanitize_key($_GET['action']) == 'migrate' && isset($_GET['type'])) {
$this->startMigrateCustomPostType(sanitize_key($_GET['type']));
$this->connector->trigger();
exit;
}

if (isset($_GET['action']) && sanitize_key($_GET['action']) == 'revert' && isset($_GET['type'])) {
$this->startRevertCustomPostType(sanitize_key($_GET['type']));
$this->connector->trigger();
exit;
}

Expand All @@ -126,6 +140,8 @@ 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();
exit;
}

add_filter('admin_menu', [$this, 'addSettingsPage']);
Expand Down
3 changes: 3 additions & 0 deletions lib/Table.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

if (!defined('ABSPATH')) {
exit;
}

class WPCPT_Tables_Table
{
Expand Down
Loading

0 comments on commit 097fab3

Please sign in to comment.