-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclass-plugin.php
More file actions
147 lines (129 loc) · 3.61 KB
/
Copy pathclass-plugin.php
File metadata and controls
147 lines (129 loc) · 3.61 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
<?php
/**
* Plugin functionality for AAA Option Optimizer.
*
* @package Emilia\OptionOptimizer
*/
namespace Emilia\OptionOptimizer;
/**
* Core functionality of AAA Option Optimizer.
*/
class Plugin {
/**
* The instance of the plugin.
*
* @var Plugin
*/
public static $instance;
/**
* Holds the names of the options accessed during the request.
*
* @var string[]
*/
protected $accessed_options = [];
/**
* Whether the plugin should reset the option_optimizer data.
*
* @var boolean
*/
protected $should_reset = false;
/**
* Initializes the plugin.
*
* @return void
*/
public function __construct() {
self::$instance = $this;
}
/**
* Gets the instance of the plugin.
*
* @return Plugin
*/
public static function get_instance() {
// @phpstan-ignore-next-line -- The 'instance' property is set in the constructor.
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Registers hooks.
*
* @return void
*/
public function register_hooks() {
$this->accessed_options = \get_option( 'option_optimizer', [ 'used_options' => [] ] )['used_options'];
// Hook into all actions and filters to monitor option accesses.
// @phpstan-ignore-next-line -- The 'all' hook does not need a return.
\add_filter( 'all', [ $this, 'monitor_option_accesses' ] );
// Use the shutdown action to update the option with tracked data.
\add_action( 'shutdown', [ $this, 'update_tracked_options' ] );
// Register the REST routes.
$rest = new REST();
$rest->register_hooks();
if ( \is_admin() ) {
// Register the admin page.
$admin_page = new Admin_Page();
$admin_page->register_hooks();
}
}
/**
* Sets the 'should_reset' property.
*
* @param boolean $should_reset Whether the plugin should reset the option_optimizer data.
*
* @return void
*/
public function reset( $should_reset = true ) {
$this->should_reset = $should_reset;
}
/**
* Monitor all actions and filters for option accesses.
*
* @param string $tag The current action or filter tag being executed.
*
* @return void
*/
public function monitor_option_accesses( $tag ) {
// Check if the tag is related to an option access.
if ( str_starts_with( $tag, 'option_' ) || str_starts_with( $tag, 'default_option_' ) ) {
$option_name = preg_replace( '#^(default_)?option_#', '', $tag );
$this->add_option_usage( $option_name );
}
}
/**
* Add an option to the list of used options if it's not already there.
*
* @param string $option_name Name of the option being accessed.
*
* @return void
*/
protected function add_option_usage( $option_name ) {
// Check if this option hasn't been tracked yet and add it to the array.
if ( ! array_key_exists( $option_name, $this->accessed_options ) ) {
$this->accessed_options[ $option_name ] = 1;
return;
}
++$this->accessed_options[ $option_name ];
}
/**
* Update the 'option_optimizer' option with the list of used options at the end of the page load.
*
* @return void
*/
public function update_tracked_options() {
// phpcs:ignore WordPress.Security.NonceVerification -- not doing anything.
if ( isset( $_GET['page'] ) && $_GET['page'] === 'aaa-option-optimizer' ) {
return;
}
// Retrieve the existing option_optimizer data.
$option_optimizer = get_option( 'option_optimizer', [ 'used_options' => [] ] );
$option_optimizer['used_options'] = $this->accessed_options;
if ( $this->should_reset ) {
$option_optimizer['used_options'] = [];
}
// Update the 'option_optimizer' option with the new list.
update_option( 'option_optimizer', $option_optimizer, false );
}
}