PluginProbe
Cache Warmer / trunk
Cache Warmer vtrunk
1.3.10 trunk 1.0.1 1.0.17 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.3 1.0.34 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.44 1.0.51 1.1.2 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 All 41 releases
cache-warmer / cache-warmer.php

cache-warmer.php in Cache Warmer trunk, at cache-warmer.php

330 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Cache Warmer
4 * Description: Visits website pages to warm (create) the cache if you have any caching solutions configured.
5 * Version: 1.3.10
6 * Text Domain: cache-warmer
7 * Author: TMM Technology
8 * Author URI: https://tmm.ventures/
9 * Requires PHP: 7.4
10 * License: GPLv3
11 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
12 *
13 * @package Cache-Warmer
14 */
15
16 namespace Cache_Warmer;
17
18 /**
19 * Main Cache_Warmer class.
20 */
21 final class Cache_Warmer {
22
23 /**
24 * Cache warmer process hook name.
25 */
26 const HOOK_NAME = 'cache_warmer_process';
27
28 /**
29 * Cache warmer process hook name for interval.
30 */
31 const INTERVAL_HOOK_NAME = 'cache_warmer_process_interval';
32
33 /**
34 * AJAX class.
35 *
36 * @var AJAX
37 */
38 public static $ajax;
39
40 /**
41 * Options.
42 *
43 * @var Options
44 */
45 public static $options;
46
47 /**
48 * Plugin name.
49 *
50 * @var string
51 */
52 public static $name;
53
54 /**
55 * Plugin slug.
56 *
57 * @var string
58 */
59 public static $slug;
60
61 /**
62 * Plugin version.
63 *
64 * @var string
65 */
66 public static $version;
67
68 /**
69 * The list of extensions that are polyfilled and therefore not required to run the plugin.
70 *
71 * Some of them can be fakely polyfilled (just to allow to start the plugin).
72 *
73 * @var string[]
74 */
75 public static $polyfilled_extensions = [
76 // Faked.
77 'simplexml',
78 ];
79
80 /**
81 * Constructor.
82 */
83 public function __construct() {
84 $this->define_constants();
85 $this->import_plugin_files();
86
87 // Schedule intervals.
88 new Intervals_Scheduler();
89
90 add_action(
91 'plugins_loaded',
92 function() {
93 new \WP_Plugins_Core\WP_Plugins_Core( $this, false );
94
95 // Load translations (on `init` or later, per WP 6.7+).
96 if ( did_action( 'init' ) ) {
97 $this->load_plugin_textdomain();
98 } else {
99 add_action( 'init', [ $this, 'load_plugin_textdomain' ] );
100 }
101
102 // Options.
103 self::$options = new Options();
104
105 // Handle version update.
106 $this->handle_version_update();
107
108 // The plugin is being updated notice.
109 if ( self::$version !== self::$options->get( 'last-handled-version-update' ) ) {
110 add_action(
111 'admin_notices',
112 function() {
113 ?>
114 <div class="notice notice-info">
115 <p>
116 <b><?php echo esc_html( self::$name ); ?>: </b>
117 <?php
118 esc_html_e(
119 'The plugin is being updated in the background.',
120 'cache-warmer'
121 );
122 ?>
123 </p>
124 </div>
125 <?php
126 }
127 );
128 }
129
130 // Interval.
131 new Interval();
132
133 // Posts Warming Interval.
134 new Posts_Warming_Interval();
135
136 // Clear old actions.
137 new Clear_Old_Actions();
138
139 // Assets.
140 new Assets\Assets();
141
142 // Post publish box.
143 new Representation\Publish_Box();
144
145 // Server IP detection logic.
146 new Server_IP_Detection();
147
148 // Adds dashboard widget.
149 add_action( 'wp_dashboard_setup', [ $this, 'setup_dashboard_widget' ] );
150
151 // Adds plugin settings links to plugins admin screen.
152 add_filter( 'plugin_action_links_' . CACHE_WARMER_BASENAME, [ $this, 'plugin_action_links' ] );
153
154 add_action( self::HOOK_NAME, [ 'Cache_Warmer\Warm_Up', 'process' ] );
155 add_action( self::INTERVAL_HOOK_NAME, [ 'Cache_Warmer\AJAX', 'start_warm_up' ] );
156
157 add_action(
158 'init',
159 function () {
160
161 // Posts enqueue.
162 new Posts_Enqueue();
163
164 // External warmer.
165 new External_Warmer();
166
167 // Menu.
168 new Admin_Menu();
169
170 // AJAX Handler.
171 self::$ajax = new AJAX();
172
173 // Extend WP CLI.
174 new Extend_WP_CLI();
175 }
176 );
177 }
178 );
179 }
180
181 /**
182 * Defines constants.
183 */
184 private function define_constants() {
185 require_once __DIR__ . '/data/constants.php';
186
187 /**
188 * Plugin name.
189 */
190 self::$name = get_file_data( CACHE_WARMER_FILE, [ 'Plugin Name' ] )[0];
191
192 /**
193 * Plugin slug.
194 */
195 $dir_parts = explode( DIRECTORY_SEPARATOR, CACHE_WARMER_DIR );
196 self::$slug = $dir_parts[ array_search( 'plugins', $dir_parts, true ) + 1 ];
197
198 /**
199 * Plugin version.
200 */
201 self::$version = CACHE_WARMER_VERSION;
202 }
203
204 /**
205 * Imports plugin files.
206 */
207 private function import_plugin_files() {
208 $src_files = [
209 'assets/class-assets',
210 'assets/screens/class-assets-logs-screen',
211 'assets/screens/class-assets-main-screen',
212 'assets/screens/class-assets-settings-screen',
213 'assets/screens/class-assets-post-screen',
214 'assets/screens/class-assets-dashboard',
215 'class-admin-menu',
216 'class-warm-up',
217 'class-ajax',
218 'class-summary',
219 'class-logging',
220 'class-databases',
221 'class-options',
222 'class-settings-export',
223 'class-interval',
224 'class-content-parsing',
225 'class-tree',
226 'class-leaf-only-subtree',
227 'class-url-formatting',
228 'class-url-parsing',
229 'class-url-validation',
230 'class-debug',
231 'class-utils',
232 'class-object-cache',
233 'class-server-ip-detection',
234 'class-migrations',
235 'class-clear-old-actions',
236 'class-external-warmer',
237 'class-intervals-scheduler',
238 'class-extend-wp-cli',
239 'posts-warming/class-posts-enqueue',
240 'posts-warming/class-posts-warming-interval',
241 'integrations/class-cache-plugins-integration',
242 'integrations/class-wp-super-cache',
243 'representation/class-publish-box',
244 ];
245 foreach ( $src_files as $file ) {
246 require_once CACHE_WARMER_DIR . 'src/' . $file . '.php';
247 }
248
249 $files = [
250 'libs/phpuri/phpuri',
251 'vendor/autoload_packages',
252 'vendor/tmmtech/wp-plugins-core/wp-plugins-core',
253 'vendor/woocommerce/action-scheduler/action-scheduler',
254 ];
255 foreach ( $files as $file ) {
256 require_once CACHE_WARMER_DIR . $file . '.php';
257 }
258 }
259
260 /**
261 * Loads textdomain.
262 */
263 public function load_plugin_textdomain() {
264 load_plugin_textdomain(
265 'cache-warmer',
266 false,
267 dirname( CACHE_WARMER_BASENAME ) . '/languages'
268 );
269 }
270
271 /**
272 * Show settings link on the plugin screen.
273 *
274 * @param mixed $links Plugin Action links.
275 *
276 * @return array
277 */
278 public function plugin_action_links( $links ) {
279 $action_links = array(
280 'settings' => '<a href="' . admin_url( 'admin.php?page=cache-warmer-settings' ) . '" aria-label="' .
281 esc_attr__( 'View Cache Warmer settings', 'cache-warmer' ) . '">' . esc_html__( 'Settings', 'cache-warmer' ) . '</a>',
282 );
283
284 return array_merge( $action_links, $links );
285 }
286
287 /**
288 * Handles version update.
289 */
290 private function handle_version_update() {
291 if ( time() - MINUTE_IN_SECONDS > (int) get_option( 'cache-warmer-updating' ) ) {
292 $last_handled_version_update = self::$options->get( 'last-handled-version-update' );
293 if ( self::$version !== $last_handled_version_update ) {
294 update_option( 'cache-warmer-updating', time() );
295
296 DB::do_migrations();
297 new Migrations( $last_handled_version_update );
298
299 self::$options->set( 'last-handled-version-update', self::$version );
300 delete_option( 'cache-warmer-updating' );
301 }
302 }
303 }
304
305 /**
306 * Adds the dashboard metrics widget.
307 */
308 public function setup_dashboard_widget() {
309 wp_add_dashboard_widget(
310 'dashboard_cache_warmer',
311 __( 'Cache Warmer', 'cache-warmer' ),
312 [ $this, 'show_dashboard_widget' ],
313 null,
314 null,
315 'normal',
316 'high'
317 );
318 }
319
320 /**
321 * Displays the dashboard widget
322 *
323 * @return void
324 */
325 public function show_dashboard_widget() {
326 require_once CACHE_WARMER_DIR . 'src/templates/admin/widget.php';
327 }
328 }
329 new Cache_Warmer();
330