PluginProbe ʕ •ᴥ•ʔ
10Web Booster – Website speed optimization, Cache & Page Speed optimizer / 2.33.6
10Web Booster – Website speed optimization, Cache & Page Speed optimizer v2.33.6
2.33.6 2.33.5 2.33.0 2.30.5 2.30.7 2.30.9 2.31.10 2.31.8 2.32.11 2.32.21 2.32.3 2.32.4 2.32.7 2.6.31 2.6.40 2.6.42 2.6.7 2.7.37 2.7.44 2.7.47 2.8.18 2.8.19 2.8.32 2.8.34 2.8.35 2.9.23 2.9.24 2.9.25 2.9.27 v2.27.4 trunk 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.17 2.0.18 2.0.21 2.0.22 2.0.25 2.0.26 2.0.27 2.0.3 2.0.7 2.0.9 2.10.46 2.10.65 2.10.66 2.10.68 2.11.41 2.11.42 2.11.43 2.12.15 2.12.21 2.12.22 2.12.23 2.12.26 2.13.37 2.13.40 2.13.41 2.13.42 2.13.44 2.13.45 2.13.47 2.14.49 2.14.50 2.15.18 2.17.21 2.17.23 2.18.17 2.19.44 2.19.45 2.19.46 2.19.49 2.2.12 2.2.15 2.2.16 2.2.18 2.2.8 2.20.31 2.20.32 2.20.33 2.21.11 2.21.12 2.21.16 2.21.25 2.22.32 2.23.13 2.23.15 2.23.16 2.23.18 2.24.12 2.24.14 2.24.18 2.25.14 2.26.6 2.28.10 2.28.13 2.28.14 2.28.7 2.29.1 2.29.2 2.29.3 2.3.0 2.3.1 2.3.2 2.3.3 2.30.18
tenweb-speed-optimizer / includes / OptimizerOnInit.php
tenweb-speed-optimizer / includes Last commit date
WebPageCache 2 weeks ago external 1 week ago ExcludeJsFromDelay.php 2 years ago OptimizerBanner.php 1 year ago OptimizerBase.php 2 years ago OptimizerCSSMin.php 2 years ago OptimizerCache.php 2 weeks ago OptimizerCacheStructure.php 2 years ago OptimizerCriticalCss.php 2 weeks ago OptimizerElementor.php 2 years ago OptimizerFonts.php 2 years ago OptimizerImages.php 1 year ago OptimizerLogger.php 1 year ago OptimizerMain.php 2 weeks ago OptimizerOnInit.php 2 weeks ago OptimizerScripts.php 2 years ago OptimizerSettings.php 2 weeks ago OptimizerStyles.php 2 weeks ago OptimizerUrl.php 1 year ago OptimizerUtils.php 2 weeks ago OptimizerWhiteLabel.php 2 years ago
OptimizerOnInit.php
422 lines
1 <?php
2
3 namespace TenWebOptimizer;
4
5 class OptimizerOnInit
6 {
7 public function __construct()
8 {
9 add_action('init', [ $this, 'two_register_meta']);
10 add_action('init', [ $this, 'two_plugin_add_new_image_size']);
11 add_action('admin_bar_menu', [ $this, 'two_admin_bar'], 71);
12
13 if (strtolower(TWO_SO_ORGANIZATION_NAME) == '10web' && !\TenWebOptimizer\OptimizerUtils::is_paid_user()) {
14 add_action('enqueue_block_editor_assets', [$this, 'two_block_editor_assets']);
15
16 if (is_plugin_active('elementor/elementor.php')) {
17 require_once __DIR__ . '/OptimizerElementor.php';
18 new OptimizerElementor();
19 }
20 }
21 // Add Optimize column to the posts list table.
22 add_filter('manage_post_posts_columns', [ $this, 'two_add_column_to_posts']);
23 add_filter('manage_page_posts_columns', [ $this, 'two_add_column_to_posts']);
24
25 add_action('admin_notices', [ $this, 'two_admin_banners' ]);
26 add_action('wp_ajax_two_set_autoupdate_from_banner', [ '\TenWebOptimizer\OptimizerBanner', 'two_set_autoupdate_from_banner' ]);
27 // Call the action on finishing the given page optimization.
28 add_action('two_page_optimized', [ $this, 'two_page_optimized']);
29 // Call the action on removing the page critical CSS.
30 add_action('two_page_optimized_removed', [ $this, 'two_page_optimized_removed']);
31
32 add_action('wp_ajax_two_optimized_notif_closed', [ $this, 'two_optimized_notif_closed' ]);
33 add_action('wp_ajax_two_is_page_optimized', [ $this, 'two_is_page_optimized' ]);
34 add_action('wp_ajax_two_recount_score', [ $this, 'two_recount_score' ]);
35 add_action('wp_ajax_two_get_page_score', [ $this, 'two_get_page_score' ]);
36 add_action('wp_ajax_two_get_optimized_images', [ $this, 'two_get_optimized_images' ]);
37 add_action('wp_ajax_two_sign_up_dashboard_magic_link', [ $this, 'two_sign_up_dashboard_magic_link' ]);
38 add_action('wp_ajax_two_setFlowIdNotificationId', [ '\TenWebOptimizer\OptimizerAdmin', 'setFlowIdNotificationId' ]);
39 add_action('elementor/editor/before_enqueue_scripts', [ $this, 'two_enqueue_editor_scripts' ]);
40 /*
41 * Fires before updating an DIVI ePanel option in the database.
42 */
43 add_action('et_epanel_update_option', [ $this, 'maybeRegenerateCCSS' ], 10, 2);
44 }
45
46 public static function maybe_update_excluded_css_list()
47 {
48 global $TwoSettings;
49
50 if (!$TwoSettings) {
51 return;
52 }
53
54 // Legacy one-time migration for ds-gravity-forms-for-divi.
55 if (!get_option('two_update_default_excluded_css_list')) {
56 $newValue = 'ds-gravity-forms-for-divi';
57 $old_value = $TwoSettings->get_settings('two_exclude_css');
58
59 if ($old_value && !str_contains($old_value, $newValue)) {
60 $old_value .= ',' . $newValue;
61 }
62 $TwoSettings->update_setting(
63 'two_exclude_css',
64 $old_value
65 );
66 update_option('two_update_default_excluded_css_list', '1');
67 }
68
69 // One-time migration for wvc-builds (existing clients).
70 if (!get_option('two_update_excluded_css_list_wvc_builds')) {
71 $newValue = 'wvc-builds';
72 $old_value = $TwoSettings->get_settings('two_exclude_css');
73
74 if ($old_value && !str_contains($old_value, $newValue)) {
75 $old_value .= ',' . $newValue;
76 $TwoSettings->update_setting('two_exclude_css', $old_value);
77 }
78 update_option('two_update_excluded_css_list_wvc_builds', '1');
79 }
80 }
81
82 public function two_register_meta()
83 {
84 $allowed_post_types = ['post', 'page'];
85
86 foreach ($allowed_post_types as $type) {
87 register_post_meta($type, 'two_page_speed', [
88 'show_in_rest' => [
89 'schema' => [
90 'type' => 'object',
91 'properties' => [
92 'previous_score' => [
93 'type' => 'object',
94 'properties' => [
95 'desktop_score' => [
96 'type' => 'number',
97 ],
98 'desktop_tti' => [
99 'type' => 'string',
100 ],
101 'mobile_score' => [
102 'type' => 'number',
103 ],
104 'mobile_tti' => [
105 'type' => 'string',
106 ],
107 'date' => [
108 'type' => 'string',
109 ],
110 'status' => [
111 'type' => 'string',
112 ],
113 ],
114 ],
115 'current_score' => [
116 'type' => 'object',
117 'properties' => [
118 'desktop_score' => [
119 'type' => 'number',
120 ],
121 'desktop_tti' => [
122 'type' => 'string',
123 ],
124 'mobile_score' => [
125 'type' => 'number',
126 ],
127 'mobile_tti' => [
128 'type' => 'string',
129 ],
130 'date' => [
131 'type' => 'string',
132 ],
133 'status' => [
134 'type' => 'string',
135 ],
136 ],
137 ],
138 ],
139 ],
140 ],
141 'single' => true,
142 'type' => 'object'
143 ]);
144 }
145 }
146
147 public function two_plugin_add_new_image_size()
148 {
149 add_image_size('tenweb_optimizer_mobile', 600, 600, false);
150 add_image_size('tenweb_optimizer_tablet', 768, 1024, false);
151 }
152
153 /* Run admin bar functionality */
154 public function two_admin_bar($wp_admin_bar)
155 {
156 /* post status not to show the admin bar */
157 $post_status = [
158 'private',
159 'future',
160 'draft'
161 ];
162
163 if ((isset($_GET['post']) && isset($_GET['action']) && sanitize_text_field($_GET['action']) == 'edit') // phpcs:ignore
164 || (strtolower(TWO_SO_ORGANIZATION_NAME) != '10web')
165 || (get_the_ID() && array_search(get_post(get_the_ID())->post_status, $post_status) !== false)
166 /* remove admin bar for mailpoet plugin(it is blocked all other plugins styles and scripts)*/
167 || (isset($_GET['page']) && (strpos($_GET['page'], 'mailpoet') !== false)) // phpcs:ignore
168 // Do not show admin topbar on some pages that break it. (Gravity Forms)
169 || (isset($_GET['page']) && (0 === strpos($_GET['page'], 'gf_'))) // phpcs:ignore
170 || !current_user_can('administrator')) {
171 // Do not show admin topbar on Booster page.
172 return false;
173 }
174
175 require_once TENWEB_SO_PLUGIN_DIR . '/OptimizerAdminBar.php';
176 new OptimizerAdminBar($wp_admin_bar);
177 }
178
179 public function two_block_editor_assets()
180 {
181 //check non-cached pages
182 $urlIsOptimizable = false;
183
184 if (get_the_ID()) {
185 $page_url = get_permalink(get_the_ID());
186 $urlIsOptimizable = \TenWebOptimizer\OptimizerUrl::urlIsOptimizable($page_url);
187 }
188
189 if ($urlIsOptimizable && current_user_can('administrator')) {
190 wp_enqueue_script('two-sidebar-plugin', TENWEB_SO_URL . '/assets/js/gutenberg/sidebar-plugin-compiled.js', [
191 'wp-plugins',
192 'wp-edit-post'
193 ], TENWEB_SO_VERSION);
194 wp_localize_script('two-sidebar-plugin', 'two_speed', [
195 'nonce' => wp_create_nonce('two_ajax_nonce'),
196 'ajax_url' => admin_url('admin-ajax.php'),
197 'optimize_entire_website' => self::two_reached_limit(),
198 'critical_pages' => \TenWebOptimizer\OptimizerUtils::getCriticalPages(),
199 ]);
200 wp_enqueue_style('two_speed_css', TENWEB_SO_URL . '/assets/css/speed.css', ['two-open-sans'], TENWEB_SO_VERSION);
201 }
202 }
203
204 public function two_add_column_to_posts($columns)
205 {
206 if (\TenWebOptimizer\OptimizerUtils::is_paid_user()) {
207 return $columns;
208 }
209
210 $offset = array_search('author', array_keys($columns));
211
212 return array_merge(array_slice($columns, 0, $offset), [ 'two-speed' => '<b>' . TWO_SO_ORGANIZATION_NAME . ' Booster' . '</b>' ], array_slice($columns, $offset, null));
213 }
214
215 public function two_admin_banners()
216 {
217 $banners = new \TenWebOptimizer\OptimizerBanner();
218 $banners->printBanners();
219 }
220
221 public function two_page_optimized($post_id)
222 {
223 \TenWebSC\TWScoreChecker::twsc_check_score($post_id);
224
225 if ($post_id == 'front_page') {
226 $post_id = url_to_postid(get_home_url()); // phpcs:ignore
227 }
228 \TenWebWpTransients\OptimizerTransients::delete('two_optimize_inprogress_' . $post_id);
229 /* Keeping all posts statuses which is optimized and notif popup view is not shown yet */
230 $two_optimization_notif_status = get_option('two_optimization_notif_status', []);
231 $two_optimization_notif_status[$post_id] = 'optimized_not_closed';
232 update_option('two_optimization_notif_status', $two_optimization_notif_status, 1);
233 }
234
235 public function two_page_optimized_removed($post_id)
236 {
237 if ($post_id == 'front_page') {
238 delete_option('two-front-page-speed');
239 // If front page is a page and has ID, check and save the score also as post meta.
240 $post_id = url_to_postid(get_home_url()); // phpcs:ignore
241
242 if ($post_id) {
243 delete_post_meta($post_id, 'two_page_speed');
244 }
245 } else {
246 delete_post_meta($post_id, 'two_page_speed');
247 }
248 \TenWebWpTransients\OptimizerTransients::delete('two_optimize_inprogress_' . $post_id);
249 }
250
251 public function two_optimized_notif_closed()
252 {
253 $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
254
255 if (!wp_verify_nonce($nonce, 'two_ajax_nonce') || !OptimizerUtils::check_admin_capabilities()) {
256 die('Permission Denied.');
257 }
258
259 $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
260 /* Keeping all posts statuses which is in progress or optimized to manage notif popup view one time for each case */
261 $two_optimization_notif_status = get_option('two_optimization_notif_status');
262 unset($two_optimization_notif_status[$post_id]);
263 update_option('two_optimization_notif_status', $two_optimization_notif_status, 1);
264 wp_send_json_success(['status' => 'success']);
265 }
266
267 public function two_is_page_optimized()
268 {
269 $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
270
271 if (!wp_verify_nonce($nonce, 'two_ajax_nonce') || !OptimizerUtils::check_admin_capabilities()) {
272 die('Permission Denied.');
273 }
274
275 $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
276
277 $page_score = get_post_meta($post_id, 'two_page_speed', true);
278
279 if (!\TenWebWpTransients\OptimizerTransients::get('two_optimize_inprogress_' . $post_id) && !empty($page_score)) {
280 wp_send_json_success($page_score);
281 }
282
283 wp_send_json_error(['status' => 'pending']);
284 }
285
286 public function two_recount_score()
287 {
288 $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
289
290 if (!wp_verify_nonce($nonce, 'two_ajax_nonce') || !OptimizerUtils::check_admin_capabilities()) {
291 die('Permission Denied.');
292 }
293
294 $post_id = isset($_POST['post_id']) ? sanitize_text_field($_POST['post_id']) : 0;
295 $reanalyze_score_for = isset($_POST['reanalyze_score_for']) ? sanitize_text_field($_POST['reanalyze_score_for']) : '';
296
297 $page_score = \TenWebSC\TWScoreChecker::twsc_recount_score($post_id, $reanalyze_score_for);
298 wp_send_json_success($page_score);
299 }
300
301 /* Get page score info for js */
302 public function two_get_page_score()
303 {
304 $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
305
306 if (!wp_verify_nonce($nonce, 'two_ajax_nonce') || ! OptimizerUtils::check_admin_capabilities()) {
307 die('Permission Denied.');
308 }
309
310 $post_id = isset($_POST['post_id']) ? sanitize_text_field($_POST['post_id']) : 0;
311
312 if ($post_id == 'front_page' || $post_id == get_option('page_on_front')) {
313 $page_score = get_option('two-front-page-speed');
314 } else {
315 $page_score = get_post_meta($post_id, 'two_page_speed', true);
316 }
317
318 wp_send_json_success($page_score);
319 }
320
321 /* Get website images total count and optimized images count from endpoint */
322 public function two_get_optimized_images()
323 {
324 $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
325
326 if (!wp_verify_nonce($nonce, 'two_ajax_nonce') || !OptimizerUtils::check_admin_capabilities()) {
327 die('Permission Denied.');
328 }
329
330 $two_images_count = \TenWebWpTransients\OptimizerTransients::get('two_images_count');
331
332 if (!empty($two_images_count)) {
333 return;
334 }
335 $workspace_id = (int) get_site_option(TENWEBIO_MANAGER_PREFIX . '_workspace_id', 0);
336 $domain_id = (int) get_option(TENWEBIO_MANAGER_PREFIX . '_domain_id', 0);
337 $access_token = get_site_option('tenweb_access_token');
338 $url = TENWEBIO_API_URL . '/compress/workspaces/' . $workspace_id . '/domains/' . $domain_id . '/stat';
339 $args = [
340 'timeout' => 15, // phpcs:ignore
341 'headers' => [
342 'accept' => 'application/x.10weboptimizer.v3+json',
343 'authorization' => 'Bearer ' . $access_token,
344 ],
345 ];
346 $response = wp_remote_get($url, $args); // phpcs:ignore
347 $images_data = [];
348
349 if (!is_wp_error($response) && is_array($response)) {
350 $body = json_decode($response['body'], 1);
351
352 if (isset($body['status']) && $body['status'] == 200) {
353 $data = $body['data'];
354
355 $total_not_compressed_images_count = (int) ($data['not_compressed']['full'] + $data['not_compressed']['thumbs'] + $data['not_compressed']['other']);
356 $total_compressed_images_count = (int) ($data['compressed']['full'] + $data['compressed']['thumbs'] + $data['compressed']['other']);
357 $total_images_count = (int) ($total_not_compressed_images_count + $total_compressed_images_count);
358 $pages_compressed = $data['pages_compressed'];
359 $count = 0;
360
361 foreach ($pages_compressed as $page) {
362 $count += $page['images_count'];
363 }
364 $images_data = ['total_images_count' => (int) $total_images_count, 'optimized_images_count' => (int) $count];
365 \TenWebWpTransients\OptimizerTransients::set('two_images_count', $images_data, DAY_IN_SECONDS);
366 }
367 } else {
368 $images_data = ['total_images_count' => 0, 'optimized_images_count' => 0];
369 \TenWebWpTransients\OptimizerTransients::set('two_images_count', $images_data, DAY_IN_SECONDS);
370 }
371
372 wp_send_json_success($images_data);
373 }
374
375 // Check if optimized pages limit reached.
376 public static function two_reached_limit()
377 {
378 if (!\TenWebOptimizer\OptimizerUtils::is_paid_user() && count(\TenWebOptimizer\OptimizerUtils::getCriticalPages()) >= 6) {
379 $domain_id = intval(get_option(TENWEBIO_MANAGER_PREFIX . '_domain_id', 0));
380
381 return TENWEB_DASHBOARD . '/websites/' . $domain_id . '/booster/pro';
382 }
383
384 return false;
385 }
386
387 public function two_enqueue_editor_scripts()
388 {
389 wp_enqueue_script(
390 'two-elementor-editor-scripts',
391 TENWEB_SO_URL . '/assets/js/two_elementor_editor.js',
392 ['jquery'],
393 TENWEB_SO_VERSION
394 );
395 wp_localize_script('two-elementor-editor-scripts', 'two_elementor_vars', [
396 'nonce' => wp_create_nonce('two_elementor_regenerate_ccss'),
397 'ajax_url' => admin_url('admin-ajax.php')
398 ]);
399 }
400
401 public function maybeRegenerateCCSS($et_option_name, $et_option_new_value)
402 {
403 //set old divi_custom_css
404 //divi_custom_css option is not set as other settings
405 $oldCustomCSS = get_option('et_divi')['divi_custom_css'];
406 add_option(TW_OPTIMIZE_PREFIX . '_old_divi_custom_css', $oldCustomCSS, false);
407 $old_value = et_get_option($et_option_name, []);
408 $diviOptionNames = [
409 'divi_dynamic_css',
410 'divi_critical_css',
411 'divi_critical_threshold_height',
412 'divi_dynamic_js_libraries',
413 ];
414
415 if (in_array($et_option_name, $diviOptionNames) && $et_option_new_value !== $old_value
416 && !get_option(TW_OPTIMIZE_PREFIX . '_clear_cache_after_divi')) {
417 //clear cache right here is causing issues with divi
418 update_option(TW_OPTIMIZE_PREFIX . '_clear_cache_after_divi', 1, false);
419 }
420 }
421 }
422