-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkl-pc-preset-bulk-generator.php
More file actions
153 lines (133 loc) · 5.27 KB
/
mkl-pc-preset-bulk-generator.php
File metadata and controls
153 lines (133 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Plugin Name: Product Configurator - Bulk Preset Generator
* Plugin URI: http://wc-product-configurator.com/
* Description: Automatically generate all valid configuration presets based on conditional logic rules
* Version: 1.1.4
* Author: Happy Webs Limited
* Author URI: https://happywebs.co.uk
* Text Domain: mkl-pc-preset-generator
* Domain Path: /languages
*/
// If this file is called directly, abort.
if (! defined('ABSPATH')) {
die;
}
define('MKL_PC_PRESET_GENERATOR_VERSION', '1.1.4');
define('MKL_PC_PRESET_GENERATOR_PATH', plugin_dir_path(__FILE__));
define('MKL_PC_PRESET_GENERATOR_URL', plugin_dir_url(__FILE__));
/**
* Main plugin class
*/
class MKL_PC_Preset_Bulk_Generator
{
private static $instance = null;
/**
* Get singleton instance
*/
public static function instance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct()
{
add_action('plugins_loaded', [$this, 'init']);
}
/**
* Initialise plugin
*/
public function init()
{
// Check dependencies
if (! $this->check_dependencies()) {
add_action('admin_notices', [$this, 'dependency_notice']);
return;
}
// Prevent MKL PC from loading all presets into memory on preset admin page
// The preset admin loads ALL presets into JavaScript via wp_localize_script
// which causes 512MB+ memory exhaustion with thousands of presets
// We intercept the WP_Query before it executes and return empty array
add_action('mkl_pc_scripts_product_page_after', [$this, 'prevent_preset_loading'], 1);
// Load includes
require_once MKL_PC_PRESET_GENERATOR_PATH . 'includes/class-combination-generator.php';
require_once MKL_PC_PRESET_GENERATOR_PATH . 'includes/class-smart-combination-generator.php';
require_once MKL_PC_PRESET_GENERATOR_PATH . 'includes/class-conditional-validator.php';
require_once MKL_PC_PRESET_GENERATOR_PATH . 'includes/class-configuration-builder.php';
require_once MKL_PC_PRESET_GENERATOR_PATH . 'includes/class-preset-saver.php';
require_once MKL_PC_PRESET_GENERATOR_PATH . 'includes/class-layer-variation-expander.php';
require_once MKL_PC_PRESET_GENERATOR_PATH . 'includes/class-image-diagnostics.php';
require_once MKL_PC_PRESET_GENERATOR_PATH . 'includes/class-image-generator.php';
require_once MKL_PC_PRESET_GENERATOR_PATH . 'includes/class-admin-ui.php';
// Initialise components
MKL_PC_Preset_Generator_Admin_UI::instance();
}
/**
* Prevent MKL PC from loading all presets into JavaScript
* This causes memory issues with thousands of presets
*/
public function prevent_preset_loading()
{
if (! isset($_REQUEST['pc-presets-admin'])) {
return;
}
// Intercept the get_posts query that loads all presets
// and return empty array to prevent memory exhaustion
add_filter('posts_pre_query', function ($posts, $query) {
// Only intercept if this is a preset configuration query
if (isset($query->query_vars['post_type']) &&
$query->query_vars['post_type'] === 'mkl_pc_configuration' &&
isset($query->query_vars['post_status']) &&
(is_array($query->query_vars['post_status']) && in_array('preset', $query->query_vars['post_status'], true) ||
$query->query_vars['post_status'] === 'preset')) {
// Check if this is being called from the MKL PC preset admin script enqueue
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10);
foreach ($backtrace as $trace) {
if (isset($trace['function']) && $trace['function'] === 'add_scripts' &&
isset($trace['class']) && $trace['class'] === 'MKL_PC_Presets_Admin') {
// Return empty array to prevent loading all presets
return [];
}
}
}
return $posts;
}, 10, 2);
}
/**
* Check required dependencies are active
*/
private function check_dependencies()
{
// Check if the product configurator is present
$required_functions = [
'mkl_pc', // Product Configurator helper function
];
foreach ($required_functions as $func) {
if (! function_exists($func)) {
return false;
}
}
return true;
}
/**
* Show dependency notice
*/
public function dependency_notice()
{
?>
<div class="notice notice-error">
<p>
<strong><?php esc_html_e('Product Configurator - Bulk Preset Generator', 'mkl-pc-preset-generator'); ?></strong>
<?php esc_html_e('requires Product Configurator, Save Your Design, and Conditional Logic plugins to be active.', 'mkl-pc-preset-generator'); ?>
</p>
</div>
<?php
}
}
// Initialise plugin
MKL_PC_Preset_Bulk_Generator::instance();