-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamigo-performance.php
More file actions
334 lines (290 loc) · 10.9 KB
/
Copy pathamigo-performance.php
File metadata and controls
334 lines (290 loc) · 10.9 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* Plugin Name: Amigo Performance
* Plugin URI: https://github.com/AmigoDheena/Amigo-Performance
* Description: A lightweight performance optimization plugin that boosts website speed by removing query strings, disabling emojis, enabling script defer, and implementing lazy loading for images and iframes to improve scores in Google PageSpeed Insights and GTmetrix.
* Version: 3.2
* Author: Amigo Dheena
* Author URI: https://github.com/AmigoDheena
* Text Domain: amigo-performance
* Domain Path: /languages
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Requires at least: 6.0
* Tested up to: 6.9
* Requires PHP: 8.0
*
* @package amigo-performance
* @author Amigo Dheena
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Include WordPress core files
include_once(ABSPATH . 'wp-admin/includes/file.php');
include_once(ABSPATH . 'wp-admin/includes/plugin.php');
// Define plugin constants
if (!defined('AMIGOPERF_PLUGIN_VERSION')) {
define('AMIGOPERF_PLUGIN_VERSION', '3.2');
}
if (!defined('AMIGOPERF_PLUGIN_PATH')) {
define('AMIGOPERF_PLUGIN_PATH', plugin_dir_path(__FILE__));
}
if (!defined('AMIGOPERF_PLUGIN_URL')) {
define('AMIGOPERF_PLUGIN_URL', plugin_dir_url(__FILE__));
}
// Define debug mode constant - MUST be set to false in production
if (!defined('AMIGOPERF_DEBUG')) {
/**
* Controls debug logging for Amigo Performance plugin
*
* When set to true AND WP_DEBUG is true, will enable debug logging
* This should ALWAYS be set to false in production environments
*
* To enable debugging, add this to wp-config.php:
* define('AMIGOPERF_DEBUG', true);
*/
define('AMIGOPERF_DEBUG', false);
}
// Load text domain
function amigoperf_load_textdomain() {
load_plugin_textdomain(
'amigo-performance',
false,
dirname(plugin_basename(__FILE__)) . '/languages/'
);
}
add_action('plugins_loaded', 'amigoperf_load_textdomain', 1);
// Include class files
require_once AMIGOPERF_PLUGIN_PATH . 'includes/class-amigo-performance-logger.php';
require_once AMIGOPERF_PLUGIN_PATH . 'includes/class-amigo-performance-core.php';
require_once AMIGOPERF_PLUGIN_PATH . 'includes/class-amigo-performance-lazyload.php';
require_once AMIGOPERF_PLUGIN_PATH . 'includes/class-amigo-performance-minify.php';
require_once AMIGOPERF_PLUGIN_PATH . 'includes/class-amigo-performance-asset-manager.php';
require_once AMIGOPERF_PLUGIN_PATH . 'includes/class-amigo-performance-admin.php';
require_once AMIGOPERF_PLUGIN_PATH . 'includes/class-amigo-performance-settings.php';
/**
* Main Plugin Class
*/
class AmigoPerformancePlugin {
// Plugin properties
public $amigoPerf_hfn = 'amigoPerf_hfn';
public $amigoPerf_PluginName = 'Amigo Performance';
public $amigoPerf_PluginVersion = 'amigoPerf_plugin_version';
public $version = AMIGOPERF_PLUGIN_VERSION;
// Settings properties
public $amigoPerf_rqs;
public $amigoPerf_rqs_opt;
public $amigoPerf_rqs_val;
public $amigoPerf_remoji;
public $amigoPerf_remoji_opt;
public $amigoPerf_remoji_val;
public $amigoPerf_defer;
public $amigoPerf_defer_opt;
public $amigoPerf_defer_val;
public $amigoPerf_iframelazy;
public $amigoPerf_iframelazy_opt;
public $amigoPerf_iframelazy_val;
public $amigoPerf_lazyload;
public $amigoPerf_lazyload_opt;
public $amigoPerf_lazyload_val;
public $amigoPerf_minify_css;
public $amigoPerf_minify_css_opt;
public $amigoPerf_minify_css_val;
public $amigoPerf_minify_js;
public $amigoPerf_minify_js_opt;
public $amigoPerf_minify_js_val;
// Component instances
private $core;
private $lazyload;
private $minify;
private $asset_manager;
private $admin;
private $settings;
private $addons;
// Asset Manager properties
public $asset_manager_enabled = true;
/**
* Constructor
*/
public function __construct() {
$this->init_components();
$this->init_hooks();
}
/**
* Initialize component classes
*/
private function init_components() {
$this->core = new AmigoPerformance_Core($this);
$this->lazyload = new AmigoPerformance_LazyLoad($this);
$this->minify = new AmigoPerformance_Minify($this);
$this->asset_manager = new AmigoPerformance_AssetManager($this);
$this->admin = new AmigoPerformance_Admin($this);
$this->settings = new AmigoPerformance_Settings($this);
}
/**
* Initialize WordPress hooks
*/
private function init_hooks() {
// Initialize components
$this->settings->init();
$this->admin->init();
if ($this->asset_manager_enabled) {
add_action('init', array($this->asset_manager, 'init'));
}
// Performance optimizations
add_action('init', array($this, 'init_performance_features'));
// Version checker
add_action('init', array($this->settings, 'check_version_and_update'));
// Version tracking for WordPress.org statistics
add_action('init', array($this, 'track_version_usage'), 20);
}
/**
* Initialize performance features
*/
public function init_performance_features() {
$this->core->execute_query_strings_removal();
$this->core->execute_emoji_removal();
$this->core->execute_defer_javascript();
$this->lazyload->init_image_lazy_loading();
$this->lazyload->init_iframe_lazy_loading();
$this->minify->init();
}
/**
* Track version usage for WordPress.org statistics
*/
public function track_version_usage() {
$current_version = AMIGOPERF_PLUGIN_VERSION;
$stored_version = get_option($this->amigoPerf_PluginVersion, '1.0');
// Update version if it's different (important for statistics)
if ($stored_version !== $current_version) {
update_option($this->amigoPerf_PluginVersion, $current_version);
update_option('amigoperf_version_updated', time());
// Clear any version-related cache
if (function_exists('wp_cache_delete')) {
wp_cache_delete('amigoperf_version', 'options');
}
}
// Ensure WordPress knows about our current version
if (!defined('AMIGOPERF_VERSION_TRACKED')) {
define('AMIGOPERF_VERSION_TRACKED', $current_version);
}
}
/**
* Plugin activation
*/
public function activate() {
if ($this->asset_manager_enabled) {
$this->asset_manager->create_table();
delete_option('amigoperf_asset_cache');
}
// Properly store the version in database with correct option name
update_option($this->amigoPerf_PluginVersion, AMIGOPERF_PLUGIN_VERSION);
// Also update WordPress version tracking for statistics
if (function_exists('wp_get_current_user')) {
update_option('amigoperf_activation_version', AMIGOPERF_PLUGIN_VERSION);
update_option('amigoperf_last_version_check', time());
}
return AMIGOPERF_PLUGIN_VERSION;
}
/**
* Plugin deactivation
*/
public function deactivate() {
flush_rewrite_rules();
delete_option('amigoPerf_nq_script');
delete_option('amigoPerf_nq_style');
}
/**
* Get asset manager stats (for backward compatibility)
*/
public function get_asset_manager_stats() {
if ($this->asset_manager_enabled && $this->asset_manager) {
return $this->asset_manager->get_stats();
}
return array(
'total_assets' => 0,
'dequeued_assets' => 0,
'unique_pages' => 0,
'css_assets' => 0,
'js_assets' => 0
);
}
/**
* Get all managed assets (for backward compatibility)
*/
public function get_all_managed_assets() {
if ($this->asset_manager_enabled && $this->asset_manager) {
return $this->asset_manager->get_all_assets();
}
return array();
}
/**
* Add asset manager admin bar items (for backward compatibility)
*/
public function add_asset_manager_admin_bar_items($admin_bar) {
// This method is now handled by the Admin class
// Keeping for backward compatibility
}
/**
* Register addon (for backward compatibility)
*/
public function register_addon($addon_id, $addon_data) {
return $this->addons->register_addon($addon_id, $addon_data);
}
/**
* Check if addon is active (for backward compatibility)
*/
public function is_addon_active($addon_id) {
return $this->addons->is_addon_active($addon_id);
}
/**
* Get active addons (for backward compatibility)
*/
public function get_active_addons() {
return $this->addons->get_active_addons();
}
}
// Initialize the plugin
if (class_exists('AmigoPerformancePlugin')) {
// Create plugin instances
$amigoperformanceplugin = new AmigoPerformancePlugin();
// Make the main plugin instance globally accessible
global $amigo_performance_instance, $amigo_performance_addons;
$amigo_performance_instance = $amigoperformanceplugin;
$amigo_performance_addons = $amigoperformanceplugin->addons ?? null;
// Trigger add-on system hook
do_action('amigoperf_addons_loaded');
}
// Activation and deactivation hooks
register_activation_hook(__FILE__, array($amigoperformanceplugin, 'activate'));
register_deactivation_hook(__FILE__, array($amigoperformanceplugin, 'deactivate'));
// Legacy helper functions for add-ons (maintained for backward compatibility)
if (!function_exists('amigoperf_register_addon')) {
function amigoperf_register_addon($addon_id, $addon_data) {
global $amigo_performance_instance;
if ($amigo_performance_instance && method_exists($amigo_performance_instance, 'register_addon')) {
return $amigo_performance_instance->register_addon($addon_id, $addon_data);
}
return false;
}
}
if (!function_exists('amigoperf_is_addon_active')) {
function amigoperf_is_addon_active($addon_id) {
global $amigo_performance_instance;
if ($amigo_performance_instance && method_exists($amigo_performance_instance, 'is_addon_active')) {
return $amigo_performance_instance->is_addon_active($addon_id);
}
return false;
}
}
if (!function_exists('amigoperf_get_active_addons')) {
function amigoperf_get_active_addons() {
global $amigo_performance_instance;
if ($amigo_performance_instance && method_exists($amigo_performance_instance, 'get_active_addons')) {
return $amigo_performance_instance->get_active_addons();
}
return array();
}
}