PluginProbe
Cache Warmer / 1.3.2
Cache Warmer v1.3.2
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 1.3.2, at cache-warmer.php

318 lines 8.8 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.2
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 add_action(
88 'plugins_loaded',
89 function() {
90 new \WP_Plugins_Core\WP_Plugins_Core( $this, false );
91
92 // Load translations.
93 $this->load_plugin_textdomain();
94
95 // Options.
96 self::$options = new Options();
97
98 // Handle version update.
99 $this->handle_version_update();
100
101 // The plugin is being updated notice.
102 if ( self::$version !== self::$options->get( 'last-handled-version-update' ) ) {
103 add_action(
104 'admin_notices',
105 function() {
106 ?>
107 <div class="notice notice-info">
108 <p>
109 <b><?php echo esc_html( self::$name ); ?>: </b>
110 <?php
111 esc_html_e(
112 'The plugin is being updated in the background.',
113 'cache-warmer'
114 );
115 ?>
116 </p>
117 </div>
118 <?php
119 }
120 );
121 }
122
123 // Interval.
124 new Interval();
125
126 // Posts Warming Interval.
127 new Posts_Warming_Interval();
128
129 // Clear old actions.
130 new Clear_Old_Actions();
131
132 // Assets.
133 new Assets\Assets();
134
135 // Post publish box.
136 new Representation\Publish_Box();
137
138 // Server IP detection logic.
139 new Server_IP_Detection();
140
141 // Adds dashboard widget.
142 add_action( 'wp_dashboard_setup', [ $this, 'setup_dashboard_widget' ] );
143
144 // Adds plugin settings links to plugins admin screen.
145 add_filter( 'plugin_action_links_' . CACHE_WARMER_BASENAME, [ $this, 'plugin_action_links' ] );
146
147 add_action( self::HOOK_NAME, [ 'Cache_Warmer\Warm_Up', 'process' ] );
148 add_action( self::INTERVAL_HOOK_NAME, [ 'Cache_Warmer\AJAX', 'start_warm_up' ] );
149
150 add_action(
151 'init',
152 function () {
153
154 // Posts enqueue.
155 new Posts_Enqueue();
156
157 // External warmer.
158 new External_Warmer();
159
160 // Menu.
161 new Admin_Menu();
162
163 // AJAX Handler.
164 self::$ajax = new AJAX();
165 }
166 );
167 }
168 );
169 }
170
171 /**
172 * Defines constants.
173 */
174 private function define_constants() {
175 require_once __DIR__ . '/data/constants.php';
176
177 /**
178 * Plugin name.
179 */
180 self::$name = get_file_data( CACHE_WARMER_FILE, [ 'Plugin Name' ] )[0];
181
182 /**
183 * Plugin slug.
184 */
185 $dir_parts = explode( DIRECTORY_SEPARATOR, CACHE_WARMER_DIR );
186 self::$slug = $dir_parts[ array_search( 'plugins', $dir_parts, true ) + 1 ];
187
188 /**
189 * Plugin version.
190 */
191 self::$version = CACHE_WARMER_VERSION;
192 }
193
194 /**
195 * Imports plugin files.
196 */
197 private function import_plugin_files() {
198 $src_files = [
199 'assets/class-assets',
200 'assets/screens/class-assets-logs-screen',
201 'assets/screens/class-assets-main-screen',
202 'assets/screens/class-assets-settings-screen',
203 'assets/screens/class-assets-post-screen',
204 'assets/screens/class-assets-dashboard',
205 'class-admin-menu',
206 'class-warm-up',
207 'class-ajax',
208 'class-summary',
209 'class-logging',
210 'class-databases',
211 'class-options',
212 'class-settings-export',
213 'class-interval',
214 'class-content-parsing',
215 'class-tree',
216 'class-leaf-only-subtree',
217 'class-url-formatting',
218 'class-url-parsing',
219 'class-url-validation',
220 'class-debug',
221 'class-utils',
222 'class-object-cache',
223 'class-server-ip-detection',
224 'class-migrations',
225 'class-clear-old-actions',
226 'class-external-warmer',
227 'posts-warming/class-posts-enqueue',
228 'posts-warming/class-posts-warming-interval',
229 'integrations/class-cache-plugins-integration',
230 'integrations/class-wp-super-cache',
231 'representation/class-publish-box',
232 ];
233 foreach ( $src_files as $file ) {
234 require_once CACHE_WARMER_DIR . 'src/' . $file . '.php';
235 }
236
237 $files = [
238 'libs/phpuri/phpuri',
239 'vendor/autoload_packages',
240 'vendor/tmmtech/wp-plugins-core/wp-plugins-core',
241 'vendor/woocommerce/action-scheduler/action-scheduler',
242 ];
243 foreach ( $files as $file ) {
244 require_once CACHE_WARMER_DIR . $file . '.php';
245 }
246 }
247
248 /**
249 * Loads textdomain.
250 */
251 private function load_plugin_textdomain() {
252 load_plugin_textdomain(
253 'cache-warmer',
254 false,
255 dirname( CACHE_WARMER_BASENAME ) . '/languages'
256 );
257 }
258
259 /**
260 * Show settings link on the plugin screen.
261 *
262 * @param mixed $links Plugin Action links.
263 *
264 * @return array
265 */
266 public function plugin_action_links( $links ) {
267 $action_links = array(
268 'settings' => '<a href="' . admin_url( 'admin.php?page=cache-warmer-settings' ) . '" aria-label="' .
269 esc_attr__( 'View Cache Warmer settings', 'cache-warmer' ) . '">' . esc_html__( 'Settings', 'cache-warmer' ) . '</a>',
270 );
271
272 return array_merge( $action_links, $links );
273 }
274
275 /**
276 * Handles version update.
277 */
278 private function handle_version_update() {
279 if ( time() - MINUTE_IN_SECONDS > (int) get_option( 'cache-warmer-updating' ) ) {
280 $last_handled_version_update = self::$options->get( 'last-handled-version-update' );
281 if ( self::$version !== $last_handled_version_update ) {
282 update_option( 'cache-warmer-updating', time() );
283
284 DB::do_migrations();
285 new Migrations( $last_handled_version_update );
286
287 self::$options->set( 'last-handled-version-update', self::$version );
288 delete_option( 'cache-warmer-updating' );
289 }
290 }
291 }
292
293 /**
294 * Adds the dashboard metrics widget.
295 */
296 public function setup_dashboard_widget() {
297 wp_add_dashboard_widget(
298 'dashboard_cache_warmer',
299 __( 'Cache Warmer', 'cache-warmer' ),
300 [ $this, 'show_dashboard_widget' ],
301 null,
302 null,
303 'normal',
304 'high'
305 );
306 }
307
308 /**
309 * Displays the dashboard widget
310 *
311 * @return void
312 */
313 public function show_dashboard_widget() {
314 require_once CACHE_WARMER_DIR . 'src/templates/admin/widget.php';
315 }
316 }
317 new Cache_Warmer();
318