refactor: move Settings and Tools tabs to dedicated classes
- Created WP_Allstars_Settings_Manager class to handle General and Advanced tabs - Created WP_Allstars_Tools_Manager class to handle Tools tab functionality - Moved inline CSS to properly enqueued styles - Further reduced code duplication in settings.php - Improved code organization with OOP principles
This commit is contained in:
168
admin/includes/class-settings-manager.php
Normal file
168
admin/includes/class-settings-manager.php
Normal file
@ -0,0 +1,168 @@
|
||||
<?php
|
||||
/**
|
||||
* Settings Manager Class
|
||||
*
|
||||
* Handles the display and management of plugin settings tabs (General and Advanced).
|
||||
*/
|
||||
|
||||
// If this file is called directly, abort.
|
||||
if ( ! defined( 'WPINC' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
class WP_Allstars_Settings_Manager {
|
||||
|
||||
/**
|
||||
* Initialize the class
|
||||
*/
|
||||
public static function init() {
|
||||
// Register settings
|
||||
add_action('admin_init', array(self::class, 'register_settings'));
|
||||
|
||||
// Enqueue scripts and styles if needed
|
||||
add_action('admin_enqueue_scripts', array(self::class, 'enqueue_scripts'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register plugin settings
|
||||
*/
|
||||
public static function register_settings() {
|
||||
// General settings
|
||||
register_setting('wp_allstars_settings', 'wp_allstars_simple_setting');
|
||||
|
||||
// Advanced settings
|
||||
register_setting('wp_allstars_settings', 'wp_allstars_auto_upload_images');
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue scripts and styles needed for settings
|
||||
*/
|
||||
public static function enqueue_scripts($hook) {
|
||||
// Only load on the plugin settings page
|
||||
if (strpos($hook, 'wp-allstars') === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Add inline JS for toggle functionality
|
||||
$toggle_js = '
|
||||
jQuery(document).ready(function($) {
|
||||
// Toggle expandable settings panels
|
||||
$(".wp-allstars-toggle-header").on("click", function() {
|
||||
var $this = $(this);
|
||||
var $settings = $this.next(".wp-allstars-toggle-settings");
|
||||
var isExpanded = $this.attr("aria-expanded") === "true";
|
||||
|
||||
// Toggle aria-expanded attribute
|
||||
$this.attr("aria-expanded", !isExpanded);
|
||||
|
||||
// Toggle settings visibility
|
||||
$settings.slideToggle(200);
|
||||
});
|
||||
});
|
||||
';
|
||||
|
||||
wp_add_inline_script('wp-allstars-admin', $toggle_js);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the general tab content
|
||||
*/
|
||||
public static function display_general_tab() {
|
||||
?>
|
||||
<div class="wp-allstars-settings-section">
|
||||
<div class="wp-allstars-settings-grid">
|
||||
<!-- Example of a simple toggle setting (no panel) -->
|
||||
<div class="wp-setting-row">
|
||||
<div class="wp-setting-header">
|
||||
<div class="wp-setting-main">
|
||||
<div class="wp-setting-left">
|
||||
<div class="wp-toggle-switch">
|
||||
<input type="checkbox"
|
||||
id="wp_allstars_simple_setting"
|
||||
name="wp_allstars_simple_setting"
|
||||
value="1"
|
||||
<?php checked(get_option('wp_allstars_simple_setting', false)); ?>
|
||||
/>
|
||||
<span class="wp-toggle-slider"></span>
|
||||
</div>
|
||||
<label for="wp_allstars_simple_setting" class="wp-setting-label">
|
||||
<?php esc_html_e('Example: Simple Toggle', 'wp-allstars'); ?>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<p class="wp-setting-description">
|
||||
<?php esc_html_e('This is an example of a simple toggle setting without an expandable panel. Currently for demonstration purposes only.', 'wp-allstars'); ?>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the advanced tab content
|
||||
*/
|
||||
public static function display_advanced_tab() {
|
||||
?>
|
||||
<div class="wp-allstars-settings-section">
|
||||
<div class="wp-allstars-settings-grid">
|
||||
<!-- Example of an expandable panel setting -->
|
||||
<div class="wp-allstars-toggle">
|
||||
<div class="wp-allstars-toggle-header" aria-expanded="false">
|
||||
<div class="wp-allstars-toggle-main">
|
||||
<div class="wp-allstars-toggle-left">
|
||||
<div class="wp-toggle-switch">
|
||||
<input type="checkbox"
|
||||
id="wp_allstars_auto_upload_images"
|
||||
name="wp_allstars_auto_upload_images"
|
||||
value="1"
|
||||
<?php checked(get_option('wp_allstars_auto_upload_images', false)); ?>
|
||||
/>
|
||||
<span class="wp-toggle-slider"></span>
|
||||
</div>
|
||||
<label for="wp_allstars_auto_upload_images">
|
||||
<?php esc_html_e('Example: Expandable Panel', 'wp-allstars'); ?>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<p class="wp-setting-description">
|
||||
<?php esc_html_e('This is an example of an expandable panel setting. Currently for demonstration purposes only - no actual functionality.', 'wp-allstars'); ?>
|
||||
</p>
|
||||
</div>
|
||||
<div class="wp-allstars-toggle-settings">
|
||||
<div class="wp-allstars-setting-row">
|
||||
<label for="example_text"><?php esc_html_e('Example Text Field', 'wp-allstars'); ?></label>
|
||||
<input type="text"
|
||||
id="example_text"
|
||||
name="example_text"
|
||||
value="Example value"
|
||||
/>
|
||||
<p class="description"><?php esc_html_e('This is an example text field for demonstration purposes.', 'wp-allstars'); ?></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Save settings
|
||||
*/
|
||||
public static function save_settings() {
|
||||
// Check for nonce
|
||||
if (!isset($_POST['wp_allstars_settings_nonce']) || !wp_verify_nonce($_POST['wp_allstars_settings_nonce'], 'wp_allstars_save_settings')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Save general settings
|
||||
update_option('wp_allstars_simple_setting', isset($_POST['wp_allstars_simple_setting']) ? 1 : 0);
|
||||
|
||||
// Save advanced settings
|
||||
update_option('wp_allstars_auto_upload_images', isset($_POST['wp_allstars_auto_upload_images']) ? 1 : 0);
|
||||
|
||||
// Add settings saved notice
|
||||
add_settings_error('wp_allstars_settings', 'settings_updated', __('Settings saved.', 'wp-allstars'), 'updated');
|
||||
}
|
||||
}
|
191
admin/includes/class-tools-manager.php
Normal file
191
admin/includes/class-tools-manager.php
Normal file
@ -0,0 +1,191 @@
|
||||
<?php
|
||||
/**
|
||||
* Tools Manager Class
|
||||
*
|
||||
* Handles the display and management of recommended tools.
|
||||
*/
|
||||
|
||||
// If this file is called directly, abort.
|
||||
if ( ! defined( 'WPINC' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
class WP_Allstars_Tools_Manager {
|
||||
|
||||
/**
|
||||
* Initialize the class
|
||||
*/
|
||||
public static function init() {
|
||||
// Hook into WordPress if needed
|
||||
add_action('admin_enqueue_scripts', array(self::class, 'enqueue_styles'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all tools
|
||||
*
|
||||
* @return array Tools configuration
|
||||
*/
|
||||
public static function get_tools() {
|
||||
// Using the existing function to maintain compatibility
|
||||
return wp_allstars_get_tools();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the tools tab content
|
||||
*/
|
||||
public static function display_tab_content() {
|
||||
$tools = self::get_tools();
|
||||
|
||||
// Sort tools alphabetically by name
|
||||
uasort($tools, function($a, $b) {
|
||||
return strcasecmp($a['name'], $b['name']);
|
||||
});
|
||||
|
||||
// Output the HTML for the tools tab
|
||||
?>
|
||||
<div class="wpa-tools-grid">
|
||||
<?php
|
||||
foreach ($tools as $tool) {
|
||||
self::display_tool_card($tool);
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a single tool card
|
||||
*
|
||||
* @param array $tool Tool configuration
|
||||
*/
|
||||
public static function display_tool_card($tool) {
|
||||
?>
|
||||
<div class="wpa-tool-card">
|
||||
<h3><?php echo esc_html($tool['name']); ?></h3>
|
||||
<p><?php echo esc_html($tool['description']); ?></p>
|
||||
<?php if (isset($tool['button_group'])): ?>
|
||||
<div class="button-group">
|
||||
<?php foreach ($tool['button_group'] as $button): ?>
|
||||
<a href="<?php echo esc_url($button['url']); ?>" target="_blank" class="button <?php echo isset($button['primary']) && $button['primary'] ? 'button-primary' : ''; ?>">
|
||||
<?php echo esc_html($button['text']); ?>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue styles specific to tools
|
||||
*/
|
||||
public static function enqueue_styles($hook) {
|
||||
// Only load on the plugin settings page
|
||||
if (strpos($hook, 'wp-allstars') === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Add inline CSS for tools
|
||||
$custom_css = '
|
||||
.wpa-tools-grid {
|
||||
padding: 20px;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(450px, 1fr));
|
||||
gap: 24px;
|
||||
max-width: 1920px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.wpa-tool-card {
|
||||
background: #fff;
|
||||
border: 1px solid #ddd;
|
||||
padding: 24px;
|
||||
border-radius: 8px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
transition: all 0.2s ease;
|
||||
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
||||
}
|
||||
.wpa-tool-card:hover {
|
||||
border-color: #2271b1;
|
||||
box-shadow: 0 2px 6px rgba(0,0,0,0.15);
|
||||
}
|
||||
.wpa-tool-card h3 {
|
||||
margin: 0 0 12px;
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: #1d2327;
|
||||
line-height: 1.4;
|
||||
}
|
||||
.wpa-tool-card p {
|
||||
margin: 0 0 16px;
|
||||
color: #50575e;
|
||||
font-size: 14px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
.wpa-tool-card .button-group {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
margin-top: auto;
|
||||
}
|
||||
.wpa-tool-card .button {
|
||||
text-decoration: none;
|
||||
min-width: 120px;
|
||||
text-align: center;
|
||||
height: 30px;
|
||||
line-height: 28px;
|
||||
padding: 0 12px;
|
||||
font-size: 13px;
|
||||
font-weight: normal;
|
||||
margin: 0;
|
||||
border: 1px solid #0071a1 !important;
|
||||
border-radius: 3px !important;
|
||||
background: #f6f7f7;
|
||||
color: #0071a1;
|
||||
display: inline-block;
|
||||
vertical-align: top;
|
||||
box-shadow: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
.wpa-tool-card .button:hover {
|
||||
background: #f0f0f1;
|
||||
border-color: #0071a1;
|
||||
color: #0071a1;
|
||||
}
|
||||
.wpa-tool-card .button.button-primary {
|
||||
background: #2271b1;
|
||||
border-color: #2271b1 !important;
|
||||
color: #fff;
|
||||
}
|
||||
.wpa-tool-card .button.button-primary:hover {
|
||||
background: #135e96;
|
||||
border-color: #135e96 !important;
|
||||
}
|
||||
@media screen and (max-width: 960px) {
|
||||
.wpa-tools-grid {
|
||||
grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
|
||||
gap: 20px;
|
||||
padding: 16px;
|
||||
}
|
||||
.wpa-tool-card {
|
||||
padding: 20px;
|
||||
}
|
||||
}
|
||||
@media screen and (max-width: 782px) {
|
||||
.wpa-tools-grid {
|
||||
grid-template-columns: 1fr;
|
||||
gap: 16px;
|
||||
padding: 12px;
|
||||
}
|
||||
.wpa-tool-card {
|
||||
padding: 16px;
|
||||
}
|
||||
.wpa-tool-card .button {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
';
|
||||
|
||||
wp_add_inline_style('wp-allstars-admin', $custom_css);
|
||||
}
|
||||
}
|
@ -41,15 +41,17 @@ require_once dirname(__FILE__) . '/data/hosting-providers.php';
|
||||
// Include recommended plugins data
|
||||
require_once dirname(__FILE__) . '/data/recommended-plugins.php';
|
||||
|
||||
// Include the Plugin Manager class
|
||||
// Include manager classes
|
||||
require_once dirname(__FILE__) . '/includes/class-plugin-manager.php';
|
||||
|
||||
// Include the Pro Plugins Manager class
|
||||
require_once dirname(__FILE__) . '/includes/class-pro-plugins-manager.php';
|
||||
require_once dirname(__FILE__) . '/includes/class-settings-manager.php';
|
||||
require_once dirname(__FILE__) . '/includes/class-tools-manager.php';
|
||||
|
||||
// Initialize the managers
|
||||
WP_Allstars_Plugin_Manager::init();
|
||||
WP_Allstars_Pro_Plugins_Manager::init();
|
||||
WP_Allstars_Settings_Manager::init();
|
||||
WP_Allstars_Tools_Manager::init();
|
||||
|
||||
|
||||
// Remove the old plugins API filter since we're handling everything in the AJAX endpoint
|
||||
@ -659,74 +661,11 @@ function wp_allstars_settings_page() {
|
||||
</div>
|
||||
<?php elseif ($active_tab == 'general'): ?>
|
||||
<div class="tab-content" id="general">
|
||||
<div class="wp-allstars-settings-section">
|
||||
<div class="wp-allstars-settings-grid">
|
||||
<!-- Example of a simple toggle setting (no panel) -->
|
||||
<div class="wp-setting-row">
|
||||
<div class="wp-setting-header">
|
||||
<div class="wp-setting-main">
|
||||
<div class="wp-setting-left">
|
||||
<div class="wp-toggle-switch">
|
||||
<input type="checkbox"
|
||||
id="wp_allstars_simple_setting"
|
||||
name="wp_allstars_simple_setting"
|
||||
value="1"
|
||||
<?php checked(get_option('wp_allstars_simple_setting', false)); ?>
|
||||
/>
|
||||
<span class="wp-toggle-slider"></span>
|
||||
</div>
|
||||
<label for="wp_allstars_simple_setting" class="wp-setting-label">
|
||||
<?php esc_html_e('Example: Simple Toggle', 'wp-allstars'); ?>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<p class="wp-setting-description">
|
||||
<?php esc_html_e('This is an example of a simple toggle setting without an expandable panel. Currently for demonstration purposes only.', 'wp-allstars'); ?>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php WP_Allstars_Settings_Manager::display_general_tab(); ?>
|
||||
</div>
|
||||
<?php elseif ($active_tab == 'advanced'): ?>
|
||||
<div class="tab-content" id="advanced">
|
||||
<div class="wp-allstars-settings-section">
|
||||
<div class="wp-allstars-settings-grid">
|
||||
<!-- Example of an expandable panel setting -->
|
||||
<div class="wp-allstars-toggle">
|
||||
<div class="wp-allstars-toggle-header" aria-expanded="false">
|
||||
<div class="wp-allstars-toggle-main">
|
||||
<div class="wp-allstars-toggle-left">
|
||||
<div class="wp-toggle-switch">
|
||||
<input type="checkbox"
|
||||
id="wp_allstars_auto_upload_images"
|
||||
name="wp_allstars_auto_upload_images"
|
||||
value="1"
|
||||
<?php checked(get_option('wp_allstars_auto_upload_images', false)); ?>
|
||||
/>
|
||||
<span class="wp-toggle-slider"></span>
|
||||
</div>
|
||||
<label for="wp_allstars_auto_upload_images">
|
||||
<?php esc_html_e('Example: Expandable Panel', 'wp-allstars'); ?>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<p class="wp-setting-description">
|
||||
<?php esc_html_e('This is an example of an expandable panel setting. Currently for demonstration purposes only - no actual functionality.', 'wp-allstars'); ?>
|
||||
</p>
|
||||
</div>
|
||||
<div class="wp-allstars-toggle-settings">
|
||||
<div class="wp-allstars-setting-row">
|
||||
<label for="example_text"><?php esc_html_e('Example Text Field', 'wp-allstars'); ?></label>
|
||||
<input type="text"
|
||||
id="example_text"
|
||||
name="example_text"
|
||||
value="Example value"
|
||||
/>
|
||||
<p class="description"><?php esc_html_e('This is an example text field for demonstration purposes.', 'wp-allstars'); ?></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php WP_Allstars_Settings_Manager::display_advanced_tab(); ?>
|
||||
</div>
|
||||
<?php elseif ($active_tab == 'theme'): ?>
|
||||
<div class="tab-content" id="theme">
|
||||
@ -823,108 +762,7 @@ function wp_allstars_settings_page() {
|
||||
</div>
|
||||
<?php elseif ($active_tab == 'tools'): ?>
|
||||
<div class="tab-content" id="tools">
|
||||
<style>
|
||||
.wpa-pro-plugins {
|
||||
padding: 20px;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(450px, 1fr));
|
||||
gap: 24px;
|
||||
max-width: 1920px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.wpa-pro-plugin {
|
||||
background: #fff;
|
||||
border: 1px solid #ddd;
|
||||
padding: 24px;
|
||||
border-radius: 8px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
transition: all 0.2s ease;
|
||||
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
||||
}
|
||||
.wpa-pro-plugin:hover {
|
||||
border-color: #2271b1;
|
||||
box-shadow: 0 2px 6px rgba(0,0,0,0.15);
|
||||
}
|
||||
.wpa-pro-plugin h3 {
|
||||
margin: 0 0 12px;
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: #1d2327;
|
||||
line-height: 1.4;
|
||||
}
|
||||
.wpa-pro-plugin p {
|
||||
margin: 0 0 16px;
|
||||
color: #50575e;
|
||||
font-size: 14px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
.wpa-pro-plugin .button-group {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
margin-top: auto;
|
||||
}
|
||||
.wpa-pro-plugin .button {
|
||||
text-decoration: none;
|
||||
min-width: 120px;
|
||||
text-align: center;
|
||||
height: 30px;
|
||||
line-height: 28px;
|
||||
padding: 0 12px;
|
||||
font-size: 13px;
|
||||
font-weight: normal;
|
||||
margin: 0;
|
||||
border: 1px solid #0071a1 !important;
|
||||
border-radius: 3px !important;
|
||||
background: #f6f7f7;
|
||||
color: #0071a1;
|
||||
display: inline-block;
|
||||
vertical-align: top;
|
||||
box-shadow: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
.wpa-pro-plugin .button:hover {
|
||||
background: #f0f0f1;
|
||||
border-color: #0071a1;
|
||||
color: #0071a1;
|
||||
}
|
||||
.wpa-pro-plugin .button.button-primary {
|
||||
background: #2271b1;
|
||||
border-color: #2271b1 !important;
|
||||
color: #fff;
|
||||
}
|
||||
.wpa-pro-plugin .button.button-primary:hover {
|
||||
background: #135e96;
|
||||
border-color: #135e96 !important;
|
||||
}
|
||||
</style>
|
||||
<div class="wpa-pro-plugins">
|
||||
<?php
|
||||
$tools = wp_allstars_get_tools();
|
||||
// Sort tools alphabetically by name
|
||||
uasort($tools, function($a, $b) {
|
||||
return strcasecmp($a['name'], $b['name']);
|
||||
});
|
||||
foreach ($tools as $tool) {
|
||||
?>
|
||||
<div class="wpa-pro-plugin">
|
||||
<h3><?php echo esc_html($tool['name']); ?></h3>
|
||||
<p><?php echo esc_html($tool['description']); ?></p>
|
||||
<?php if (isset($tool['button_group'])): ?>
|
||||
<div class="button-group">
|
||||
<?php foreach ($tool['button_group'] as $button): ?>
|
||||
<a href="<?php echo esc_url($button['url']); ?>" target="_blank" class="button <?php echo isset($button['primary']) && $button['primary'] ? 'button-primary' : ''; ?>">
|
||||
<?php echo esc_html($button['text']); ?>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<?php WP_Allstars_Tools_Manager::display_tab_content(); ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
Reference in New Issue
Block a user