Skip to content

Commit

Permalink
refactoring to avoid function and class names collisions, removed ext…
Browse files Browse the repository at this point in the history
…ernal css and js, raised version to 1.0.3
  • Loading branch information
caspahouzer committed Mar 28, 2023
1 parent 5da214d commit 9412ae4
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 187 deletions.
62 changes: 47 additions & 15 deletions core.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php

class Core
class WPCPT_Tables_Core
{
/**
* @var array
*/
private $config = [];

/**
* @var Db
* @var WPCPT_Tables_Db
*/
private $db;

/**
* @var array
* @var WPCPT_Tables_Helper
*/
private $helper;

Expand All @@ -24,7 +24,7 @@ class Core

public function __construct()
{
$this->db = new Db;
$this->db = new WPCPT_Tables_Db;

$this->initConfig();
}
Expand All @@ -51,8 +51,9 @@ public function load()
{
$self = new self();

add_filter('plugin_action_links_' . plugin_basename(__FILE__), [$this, 'add_action_links'], 10, 2);
add_filter('network_admin_plugin_action_links_' . plugin_basename(__FILE__), [$this, 'add_action_links_network'], 10, 2);
add_filter('plugin_action_links_wp-cpt-tables/wp-cpt-tables.php', [$this, 'add_action_links'], 10, 2);
add_filter('network_admin_plugin_action_links_wp-cpt-tables/wp-cpt-tables.php', [$this, 'add_action_links_network'], 10, 2);
add_filter('plugin_row_meta', array($this, 'filter_plugin_row_meta'), 10, 2);

$self->setupAdminFilters();
$self->setupQueryFilters();
Expand All @@ -65,9 +66,7 @@ public function load()
*/
public function enqueue_scripts_styles()
{
wp_enqueue_style($this->config['plugin_slug'] . '-css-select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css', [], $this->version, 'all');
wp_enqueue_style($this->config['plugin_slug'] . '-css', plugin_dir_url(__FILE__) . 'assets/css/styles.css', [], $this->version, 'all');
wp_enqueue_script($this->config['plugin_slug'] . '-js-select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js', ['jquery'], $this->version, false);
wp_enqueue_script($this->config['plugin_slug'] . '-js', plugin_dir_url(__FILE__) . 'assets/js/scripts.js', ['jquery'], $this->version, false);
}

Expand All @@ -76,32 +75,32 @@ public function enqueue_scripts_styles()
*/
private function setupAdminFilters()
{
new AdminFilters;
new WPCPT_Tables_AdminFilters;
}

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

/**
* @return void
*/
private function setupSettingsPage()
{
new SettingsPage(
new Table($this->db, $this->config),
new Triggers($this->db, $this->config),
new WPCPT_Tables_SettingsPage(
new WPCPT_Tables_Table($this->db, $this->config),
new WPCPT_Tables_Triggers($this->db, $this->config),
$this->config
);
}

private function setupHelper()
{
new Helper;
new WPCPT_Tables_Helper;
}

/**
Expand All @@ -120,6 +119,38 @@ public function deactivate()
flush_rewrite_rules();
}

/**
* Gets the option that stores enabled post type tables and unserializes it
*
* @return array
*/
private function getEnabledPostTypes(): array
{
return array_values(get_option($this->config['tables_enabled'], []));
}

/**
* Filters the array of row meta for each plugin in the Plugins list table.
*
* @param string[] $plugin_meta An array of the plugin's metadata.
* @param string $plugin_file Path to the plugin file relative to the plugins directory.
* @return string[] An array of the plugin's metadata.
*/
public function filter_plugin_row_meta(array $plugin_meta, $plugin_file)
{
if ('wp-cpt-tables/wp-cpt-tables.php' !== $plugin_file) {
return $plugin_meta;
}

$plugin_meta[] = sprintf(
'<a href="%1$s" target="_blank"><span class="dashicons dashicons-star-filled" aria-hidden="true" style="font-size:14px;line-height:1.3"></span>%2$s</a>',
'https://www.paypal.com/donate/?hosted_button_id=JNA8L66BWE2AA',
esc_html_x('Sponsor', 'verb', 'query-monitor')
);

return $plugin_meta;
}

/**
* @param array $links
* @param string $file
Expand All @@ -143,7 +174,8 @@ public function add_action_links($links, $file)
*/
public function add_action_links_network($links, $file)
{
$links[] = '<a href="' . admin_url('settings.php?page=' . $this->config['plugin_slug']) . '">' . __('Settings', $this->config['plugin_slug']) . '</a>';
$settings = '<a href="' . admin_url('options-general.php?page=' . $this->config['plugin_slug']) . '">' . __('Settings') . '</a>';
array_unshift($links, $settings);

return $links;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/AdminFilters.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class AdminFilters
class WPCPT_Tables_AdminFilters
{
/**
* Bind the method that updates the admin redirect url to the admin_url
Expand Down
2 changes: 1 addition & 1 deletion lib/Db.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class Db
class WPCPT_Tables_Db
{
/**
* @var object wpdb
Expand Down
8 changes: 6 additions & 2 deletions lib/Helper.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<?php

class Helper
class WPCPT_Tables_Helper
{
/**
* @var WPCPT_Tables_Db
*/
private $db;

/**
* @return void
*/
public function __construct()
{
$this->db = new Db;
$this->db = new WPCPT_Tables_Db;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/Notices.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class Notices
class WPCPT_Tables_Notices
{

/**
Expand Down Expand Up @@ -64,8 +64,8 @@ public function displayFlashNotices()
foreach ($notices as $notice) {
printf(
'<div class="notice notice-%1$s %2$s"><p>%3$s</p></div>',
$notice['type'],
$notice['dismissible'],
esc_attr($notice['type']),
esc_attr($notice['dismissible']),
$notice['notice']
);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/QueryFilters.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

class QueryFilters
class WPCPT_Tables_QueryFilters
{
/**
* @var Db
* @var WPCPT_Tables_Db
*/
private $db;

Expand All @@ -20,7 +20,7 @@ class QueryFilters
* @param Db $db
* @param array $config
*/
public function __construct(Db $db, array $config)
public function __construct(WPCPT_Tables_Db $db, array $config)
{
$this->db = $db;
$this->config = $config;
Expand Down
44 changes: 29 additions & 15 deletions lib/SettingsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

class SettingsPage
class WPCPT_Tables_SettingsPage
{
/**
* The menu and page name for this settings page
Expand All @@ -21,25 +21,25 @@ class SettingsPage

/**
* The table class
* @var Table
* @var WPCPT_Tables_Table
*/
private $table;

/**
* The triggers class
* @var Triggers
* @var WPCPT_Tables_Triggers
*/
private $triggers;

/**
* The notices class
* @var Notices
* @var WPCPT_Tables_Notices
*/
private $notices;

/**
* The helper class
* @var Helper
* @var WPCPT_Tables_Helper
*/
private $helper;

Expand Down Expand Up @@ -69,14 +69,14 @@ class SettingsPage
/**
* Add settings page. If form has been submitted, route to save method.
*
* @param Table $table
* @param Triggers $triggers
* @param WPCPT_Tables_Table $table
* @param WPCPT_Tables_Triggers $triggers
*/
public function __construct(Table $table, Triggers $triggers, $config)
public function __construct(WPCPT_Tables_Table $table, WPCPT_Tables_Triggers $triggers, $config)
{
// $this->enqueue_styles();
$this->notices = new Notices;
$this->helper = new Helper;
$this->notices = new WPCPT_Tables_Notices;
$this->helper = new WPCPT_Tables_Helper;

$this->config = $config;

Expand Down Expand Up @@ -127,7 +127,6 @@ public function showSettingsPage()

$postTypes = $this->getAllPostTypes();
$unpublicPostTypes = $this->getAllPostTypes(false);
error_log(print_r($unpublicPostTypes, true));
$enabled = $this->getEnabledPostTypes();
$settingsClass = $this;

Expand All @@ -142,10 +141,13 @@ public function showSettingsPage()
private function startRevertCustomPostType($postType)
{
$enabledPostTypes = $this->getEnabledPostTypes();
error_log(print_r($enabledPostTypes, true));
if (($key = array_search($postType, $enabledPostTypes)) !== false) {
unset($enabledPostTypes[$key]);
update_option($this->config['tables_enabled'], array_values($enabledPostTypes), true);
$enabledPostTypes = array_values($enabledPostTypes);
update_option($this->config['tables_enabled'], $enabledPostTypes, true);
}
error_log(print_r($enabledPostTypes, true));

$this->triggers->create($enabledPostTypes);

Expand All @@ -158,13 +160,18 @@ private function startRevertCustomPostType($postType)
}

/**
* Revert the custom post type
* Revert the custom post types
*
* @return array
*/
private function revertCustomPostType($postType)
private function revertCustomPostType($postTypes)
{
$this->table->revert($postType);
if (!is_array($postTypes)) {
$postTypes = [$postTypes];
}
foreach ($postTypes as $postType) {
$this->table->revert($postType);
}
}

/**
Expand All @@ -191,6 +198,13 @@ private function startMigrateCustomPostType($postType)
wp_safe_redirect($this->redirect_uri);
}

/**
* Migrate all existing custom post types to the new tables
*
* @param string|array $newPostTypes
*
* @return array
*/
private function migrateCustomPostTypes($newPostTypes)
{
if (!is_array($newPostTypes)) {
Expand Down
12 changes: 6 additions & 6 deletions lib/Table.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php


class Table
class WPCPT_Tables_Table
{
/**
* @var Db
* @var WPCPT_Tables_Db
*/
private $db;

Expand All @@ -15,19 +15,19 @@ class Table

/**
* The notices class
* @var Notices
* @var WPCPT_Tables_Notices
*/
private $notices;

/**
* Triggers the create methods for tables
* @param Db $db
* @param WPCPT_Tables_Db $db
* @param array $config
* @param string $table
*/
public function __construct(Db $db, array $config)
public function __construct(WPCPT_Tables_Db $db, array $config)
{
$this->notices = new Notices();
$this->notices = new WPCPT_Tables_Notices();

$this->db = $db;
$this->config = $config;
Expand Down
8 changes: 4 additions & 4 deletions lib/Triggers.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

class Triggers
class WPCPT_Tables_Triggers
{
/**
* @var Db
* @var WPCPT_Tables_Db
*/
private $db;

Expand Down Expand Up @@ -35,10 +35,10 @@ class Triggers
/**
* Triggers the methods that create the post and post meta triggers
*
* @param Db $db
* @param WPCPT_Tables_Db $db
* @param array $config
*/
public function __construct(Db $db, array $config)
public function __construct(WPCPT_Tables_Db $db, array $config)
{
$this->db = $db;
$this->config = $config;
Expand Down
Loading

0 comments on commit 9412ae4

Please sign in to comment.