Files

71 lines
2.3 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* Admin settings and configuration
*/
if (!defined('ABSPATH')) {
exit;
}
if (!class_exists('WC_TPP_Admin')) {
class WC_TPP_Admin {
private static $instance = null;
private static $settings_instance = null;
public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
add_filter('woocommerce_get_settings_pages', array($this, 'add_settings_page'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
}
/**
* Add settings page to WooCommerce settings
*/
public function add_settings_page($settings) {
if (null === self::$settings_instance) {
Release version 1.1.18 - Fix root cause of duplicate settings Identified and fixed the actual root cause of duplicate settings page: automatic instantiation in settings file being executed multiple times. Root Cause Analysis: - Settings file ended with: return new WC_TPP_Settings(); - File is in Composer's classmap for autoloading - When autoloader loads class, it executes the entire file including instantiation - Each include/autoload created a NEW instance despite admin singleton pattern - Admin singleton prevented multiple admin instances but not multiple settings instances The Fix: - Removed automatic instantiation (return new) from settings file - Settings file now only contains class definition - Admin class explicitly creates instance: new WC_TPP_Settings() - Changed from include to require_once to prevent multiple file loads - Settings instance now created exactly once, when filter needs it Fixes: - Settings page rendering twice in WooCommerce backend - Multiple WC_TPP_Settings instances being created - Composer autoload triggering unintended instantiation Changes: - Removed line 145 from class-wc-tpp-settings.php (return new WC_TPP_Settings()) - Modified add_settings_page() to use require_once + explicit new - Settings file now side-effect free, safe for autoloading Technical Details: - Composer classmap autoloader includes files when class is referenced - Previous code had side effect (instantiation) on every include - New code separates class definition from instantiation - Instance creation now controlled explicitly by admin class - require_once ensures file loaded once even if accessed multiple times Updated Files: - includes/class-wc-tpp-settings.php (removed return new line) - includes/class-wc-tpp-admin.php (explicit instantiation) - wc-tier-and-package-prices.php (version 1.1.18) - composer.json (version 1.1.18) - CHANGELOG.md (v1.1.18 section) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-22 23:29:35 +01:00
require_once WC_TPP_PLUGIN_DIR . 'includes/class-wc-tpp-settings.php';
self::$settings_instance = new WC_TPP_Settings();
}
// Check if our settings page is already in the array to prevent duplicates
Release version 1.1.19 - Enhanced duplicate detection Fixed persistent duplicate settings page by improving duplicate detection to check by class type and ID instead of strict instance comparison. Issue: - Strict instance comparison (===) failed to detect duplicates - Different object instances of same class not caught by previous check - Settings page still appearing twice despite singleton and instance caching Solution: - Changed duplicate detection from instance comparison to type/ID checks - Added instanceof WC_TPP_Settings check - Added get_id() method check for ID 'tier_package_prices' - Added direct id property check as fallback - Multiple layers of detection to catch duplicates regardless of how created Fixes: - Settings page rendering twice in WooCommerce backend - Duplicate detection failing for different instances of same class Changes: - Enhanced add_settings_page() with multi-layer duplicate detection - Checks: instanceof, get_id(), and id property - Returns early if any existing page matches criteria Technical Details: - Strict comparison only works if exact same object instance - Different instances (even of same class/ID) fail === check - instanceof checks class type regardless of which instance - ID checks ensure no duplicate pages with same identifier - Fallback checks handle different WooCommerce versions/implementations Updated Files: - includes/class-wc-tpp-admin.php (enhanced duplicate detection) - wc-tier-and-package-prices.php (version 1.1.19) - composer.json (version 1.1.19) - CHANGELOG.md (v1.1.19 section) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-22 23:33:12 +01:00
// Check by class type and ID instead of instance comparison
foreach ($settings as $settings_page) {
Release version 1.1.19 - Enhanced duplicate detection Fixed persistent duplicate settings page by improving duplicate detection to check by class type and ID instead of strict instance comparison. Issue: - Strict instance comparison (===) failed to detect duplicates - Different object instances of same class not caught by previous check - Settings page still appearing twice despite singleton and instance caching Solution: - Changed duplicate detection from instance comparison to type/ID checks - Added instanceof WC_TPP_Settings check - Added get_id() method check for ID 'tier_package_prices' - Added direct id property check as fallback - Multiple layers of detection to catch duplicates regardless of how created Fixes: - Settings page rendering twice in WooCommerce backend - Duplicate detection failing for different instances of same class Changes: - Enhanced add_settings_page() with multi-layer duplicate detection - Checks: instanceof, get_id(), and id property - Returns early if any existing page matches criteria Technical Details: - Strict comparison only works if exact same object instance - Different instances (even of same class/ID) fail === check - instanceof checks class type regardless of which instance - ID checks ensure no duplicate pages with same identifier - Fallback checks handle different WooCommerce versions/implementations Updated Files: - includes/class-wc-tpp-admin.php (enhanced duplicate detection) - wc-tier-and-package-prices.php (version 1.1.19) - composer.json (version 1.1.19) - CHANGELOG.md (v1.1.19 section) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-22 23:33:12 +01:00
if ($settings_page instanceof WC_TPP_Settings) {
return $settings;
}
// Also check by ID property if it's a WC_Settings_Page
if (is_object($settings_page) &&
method_exists($settings_page, 'get_id') &&
$settings_page->get_id() === 'tier_package_prices') {
return $settings;
}
// Check id property directly
if (is_object($settings_page) &&
isset($settings_page->id) &&
$settings_page->id === 'tier_package_prices') {
return $settings;
}
}
$settings[] = self::$settings_instance;
return $settings;
}
public function enqueue_admin_scripts($hook) {
if ('woocommerce_page_wc-settings' === $hook || 'post.php' === $hook || 'post-new.php' === $hook) {
wp_enqueue_style('wc-tpp-admin', WC_TPP_PLUGIN_URL . 'assets/css/admin.css', array(), WC_TPP_VERSION);
wp_enqueue_script('wc-tpp-admin', WC_TPP_PLUGIN_URL . 'assets/js/admin.js', array('jquery'), WC_TPP_VERSION, true);
}
}
}
WC_TPP_Admin::get_instance();
}