PluginProbe ʕ •ᴥ•ʔ
10Web Booster – Website speed optimization, Cache & Page Speed optimizer / trunk
10Web Booster – Website speed optimization, Cache & Page Speed optimizer vtrunk
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 7 months ago external 2 months ago ExcludeJsFromDelay.php 2 years ago OptimizerBanner.php 11 months ago OptimizerBase.php 2 years ago OptimizerCSSMin.php 2 years ago OptimizerCache.php 1 year ago OptimizerCacheStructure.php 2 years ago OptimizerCriticalCss.php 1 year ago OptimizerElementor.php 2 years ago OptimizerFonts.php 2 years ago OptimizerImages.php 1 year ago OptimizerLogger.php 1 year ago OptimizerMain.php 2 months ago OptimizerOnInit.php 11 months ago OptimizerScripts.php 2 years ago OptimizerSettings.php 2 months ago OptimizerStyles.php 2 years ago OptimizerUrl.php 1 year ago OptimizerUtils.php 2 months ago OptimizerWhiteLabel.php 2 years ago
OptimizerOnInit.php
405 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 && ! get_option('two_update_default_excluded_css_list')) {
51 $newValue = 'ds-gravity-forms-for-divi';
52 $old_value = $TwoSettings->get_settings('two_exclude_css');
53
54 if ($old_value && ! str_contains($old_value, $newValue)) {
55 $old_value .= ',' . $newValue;
56 }
57 $TwoSettings->update_setting(
58 'two_exclude_css',
59 $old_value
60 );
61 update_option('two_update_default_excluded_css_list', '1');
62 }
63 }
64
65 public function two_register_meta()
66 {
67 $allowed_post_types = ['post', 'page'];
68
69 foreach ($allowed_post_types as $type) {
70 register_post_meta($type, 'two_page_speed', [
71 'show_in_rest' => [
72 'schema' => [
73 'type' => 'object',
74 'properties' => [
75 'previous_score' => [
76 'type' => 'object',
77 'properties' => [
78 'desktop_score' => [
79 'type' => 'number',
80 ],
81 'desktop_tti' => [
82 'type' => 'string',
83 ],
84 'mobile_score' => [
85 'type' => 'number',
86 ],
87 'mobile_tti' => [
88 'type' => 'string',
89 ],
90 'date' => [
91 'type' => 'string',
92 ],
93 'status' => [
94 'type' => 'string',
95 ],
96 ],
97 ],
98 'current_score' => [
99 'type' => 'object',
100 'properties' => [
101 'desktop_score' => [
102 'type' => 'number',
103 ],
104 'desktop_tti' => [
105 'type' => 'string',
106 ],
107 'mobile_score' => [
108 'type' => 'number',
109 ],
110 'mobile_tti' => [
111 'type' => 'string',
112 ],
113 'date' => [
114 'type' => 'string',
115 ],
116 'status' => [
117 'type' => 'string',
118 ],
119 ],
120 ],
121 ],
122 ],
123 ],
124 'single' => true,
125 'type' => 'object'
126 ]);
127 }
128 }
129
130 public function two_plugin_add_new_image_size()
131 {
132 add_image_size('tenweb_optimizer_mobile', 600, 600, false);
133 add_image_size('tenweb_optimizer_tablet', 768, 1024, false);
134 }
135
136 /* Run admin bar functionality */
137 public function two_admin_bar($wp_admin_bar)
138 {
139 /* post status not to show the admin bar */
140 $post_status = [
141 'private',
142 'future',
143 'draft'
144 ];
145
146 if ((isset($_GET['post']) && isset($_GET['action']) && sanitize_text_field($_GET['action']) == 'edit') // phpcs:ignore
147 || (strtolower(TWO_SO_ORGANIZATION_NAME) != '10web')
148 || (get_the_ID() && array_search(get_post(get_the_ID())->post_status, $post_status) !== false)
149 /* remove admin bar for mailpoet plugin(it is blocked all other plugins styles and scripts)*/
150 || (isset($_GET['page']) && (strpos($_GET['page'], 'mailpoet') !== false)) // phpcs:ignore
151 // Do not show admin topbar on some pages that break it. (Gravity Forms)
152 || (isset($_GET['page']) && (0 === strpos($_GET['page'], 'gf_'))) // phpcs:ignore
153 || !current_user_can('administrator')) {
154 // Do not show admin topbar on Booster page.
155 return false;
156 }
157
158 require_once TENWEB_SO_PLUGIN_DIR . '/OptimizerAdminBar.php';
159 new OptimizerAdminBar($wp_admin_bar);
160 }
161
162 public function two_block_editor_assets()
163 {
164 //check non-cached pages
165 $urlIsOptimizable = false;
166
167 if (get_the_ID()) {
168 $page_url = get_permalink(get_the_ID());
169 $urlIsOptimizable = \TenWebOptimizer\OptimizerUrl::urlIsOptimizable($page_url);
170 }
171
172 if ($urlIsOptimizable && current_user_can('administrator')) {
173 wp_enqueue_script('two-sidebar-plugin', TENWEB_SO_URL . '/assets/js/gutenberg/sidebar-plugin-compiled.js', [
174 'wp-plugins',
175 'wp-edit-post'
176 ], TENWEB_SO_VERSION);
177 wp_localize_script('two-sidebar-plugin', 'two_speed', [
178 'nonce' => wp_create_nonce('two_ajax_nonce'),
179 'ajax_url' => admin_url('admin-ajax.php'),
180 'optimize_entire_website' => self::two_reached_limit(),
181 'critical_pages' => \TenWebOptimizer\OptimizerUtils::getCriticalPages(),
182 ]);
183 wp_enqueue_style('two_speed_css', TENWEB_SO_URL . '/assets/css/speed.css', ['two-open-sans'], TENWEB_SO_VERSION);
184 }
185 }
186
187 public function two_add_column_to_posts($columns)
188 {
189 if (\TenWebOptimizer\OptimizerUtils::is_paid_user()) {
190 return $columns;
191 }
192
193 $offset = array_search('author', array_keys($columns));
194
195 return array_merge(array_slice($columns, 0, $offset), [ 'two-speed' => '<b>' . TWO_SO_ORGANIZATION_NAME . ' Booster' . '</b>' ], array_slice($columns, $offset, null));
196 }
197
198 public function two_admin_banners()
199 {
200 $banners = new \TenWebOptimizer\OptimizerBanner();
201 $banners->printBanners();
202 }
203
204 public function two_page_optimized($post_id)
205 {
206 \TenWebSC\TWScoreChecker::twsc_check_score($post_id);
207
208 if ($post_id == 'front_page') {
209 $post_id = url_to_postid(get_home_url()); // phpcs:ignore
210 }
211 \TenWebWpTransients\OptimizerTransients::delete('two_optimize_inprogress_' . $post_id);
212 /* Keeping all posts statuses which is optimized and notif popup view is not shown yet */
213 $two_optimization_notif_status = get_option('two_optimization_notif_status', []);
214 $two_optimization_notif_status[$post_id] = 'optimized_not_closed';
215 update_option('two_optimization_notif_status', $two_optimization_notif_status, 1);
216 }
217
218 public function two_page_optimized_removed($post_id)
219 {
220 if ($post_id == 'front_page') {
221 delete_option('two-front-page-speed');
222 // If front page is a page and has ID, check and save the score also as post meta.
223 $post_id = url_to_postid(get_home_url()); // phpcs:ignore
224
225 if ($post_id) {
226 delete_post_meta($post_id, 'two_page_speed');
227 }
228 } else {
229 delete_post_meta($post_id, 'two_page_speed');
230 }
231 \TenWebWpTransients\OptimizerTransients::delete('two_optimize_inprogress_' . $post_id);
232 }
233
234 public function two_optimized_notif_closed()
235 {
236 $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
237
238 if (!wp_verify_nonce($nonce, 'two_ajax_nonce') || !OptimizerUtils::check_admin_capabilities()) {
239 die('Permission Denied.');
240 }
241
242 $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
243 /* Keeping all posts statuses which is in progress or optimized to manage notif popup view one time for each case */
244 $two_optimization_notif_status = get_option('two_optimization_notif_status');
245 unset($two_optimization_notif_status[$post_id]);
246 update_option('two_optimization_notif_status', $two_optimization_notif_status, 1);
247 wp_send_json_success(['status' => 'success']);
248 }
249
250 public function two_is_page_optimized()
251 {
252 $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
253
254 if (!wp_verify_nonce($nonce, 'two_ajax_nonce') || !OptimizerUtils::check_admin_capabilities()) {
255 die('Permission Denied.');
256 }
257
258 $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
259
260 $page_score = get_post_meta($post_id, 'two_page_speed', true);
261
262 if (!\TenWebWpTransients\OptimizerTransients::get('two_optimize_inprogress_' . $post_id) && !empty($page_score)) {
263 wp_send_json_success($page_score);
264 }
265
266 wp_send_json_error(['status' => 'pending']);
267 }
268
269 public function two_recount_score()
270 {
271 $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
272
273 if (!wp_verify_nonce($nonce, 'two_ajax_nonce') || !OptimizerUtils::check_admin_capabilities()) {
274 die('Permission Denied.');
275 }
276
277 $post_id = isset($_POST['post_id']) ? sanitize_text_field($_POST['post_id']) : 0;
278 $reanalyze_score_for = isset($_POST['reanalyze_score_for']) ? sanitize_text_field($_POST['reanalyze_score_for']) : '';
279
280 $page_score = \TenWebSC\TWScoreChecker::twsc_recount_score($post_id, $reanalyze_score_for);
281 wp_send_json_success($page_score);
282 }
283
284 /* Get page score info for js */
285 public function two_get_page_score()
286 {
287 $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
288
289 if (!wp_verify_nonce($nonce, 'two_ajax_nonce') || ! OptimizerUtils::check_admin_capabilities()) {
290 die('Permission Denied.');
291 }
292
293 $post_id = isset($_POST['post_id']) ? sanitize_text_field($_POST['post_id']) : 0;
294
295 if ($post_id == 'front_page' || $post_id == get_option('page_on_front')) {
296 $page_score = get_option('two-front-page-speed');
297 } else {
298 $page_score = get_post_meta($post_id, 'two_page_speed', true);
299 }
300
301 wp_send_json_success($page_score);
302 }
303
304 /* Get website images total count and optimized images count from endpoint */
305 public function two_get_optimized_images()
306 {
307 $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
308
309 if (!wp_verify_nonce($nonce, 'two_ajax_nonce') || !OptimizerUtils::check_admin_capabilities()) {
310 die('Permission Denied.');
311 }
312
313 $two_images_count = \TenWebWpTransients\OptimizerTransients::get('two_images_count');
314
315 if (!empty($two_images_count)) {
316 return;
317 }
318 $workspace_id = (int) get_site_option(TENWEBIO_MANAGER_PREFIX . '_workspace_id', 0);
319 $domain_id = (int) get_option(TENWEBIO_MANAGER_PREFIX . '_domain_id', 0);
320 $access_token = get_site_option('tenweb_access_token');
321 $url = TENWEBIO_API_URL . '/compress/workspaces/' . $workspace_id . '/domains/' . $domain_id . '/stat';
322 $args = [
323 'timeout' => 15, // phpcs:ignore
324 'headers' => [
325 'accept' => 'application/x.10weboptimizer.v3+json',
326 'authorization' => 'Bearer ' . $access_token,
327 ],
328 ];
329 $response = wp_remote_get($url, $args); // phpcs:ignore
330 $images_data = [];
331
332 if (!is_wp_error($response) && is_array($response)) {
333 $body = json_decode($response['body'], 1);
334
335 if (isset($body['status']) && $body['status'] == 200) {
336 $data = $body['data'];
337
338 $total_not_compressed_images_count = (int) ($data['not_compressed']['full'] + $data['not_compressed']['thumbs'] + $data['not_compressed']['other']);
339 $total_compressed_images_count = (int) ($data['compressed']['full'] + $data['compressed']['thumbs'] + $data['compressed']['other']);
340 $total_images_count = (int) ($total_not_compressed_images_count + $total_compressed_images_count);
341 $pages_compressed = $data['pages_compressed'];
342 $count = 0;
343
344 foreach ($pages_compressed as $page) {
345 $count += $page['images_count'];
346 }
347 $images_data = ['total_images_count' => (int) $total_images_count, 'optimized_images_count' => (int) $count];
348 \TenWebWpTransients\OptimizerTransients::set('two_images_count', $images_data, DAY_IN_SECONDS);
349 }
350 } else {
351 $images_data = ['total_images_count' => 0, 'optimized_images_count' => 0];
352 \TenWebWpTransients\OptimizerTransients::set('two_images_count', $images_data, DAY_IN_SECONDS);
353 }
354
355 wp_send_json_success($images_data);
356 }
357
358 // Check if optimized pages limit reached.
359 public static function two_reached_limit()
360 {
361 if (!\TenWebOptimizer\OptimizerUtils::is_paid_user() && count(\TenWebOptimizer\OptimizerUtils::getCriticalPages()) >= 6) {
362 $domain_id = intval(get_option(TENWEBIO_MANAGER_PREFIX . '_domain_id', 0));
363
364 return TENWEB_DASHBOARD . '/websites/' . $domain_id . '/booster/pro';
365 }
366
367 return false;
368 }
369
370 public function two_enqueue_editor_scripts()
371 {
372 wp_enqueue_script(
373 'two-elementor-editor-scripts',
374 TENWEB_SO_URL . '/assets/js/two_elementor_editor.js',
375 ['jquery'],
376 TENWEB_SO_VERSION
377 );
378 wp_localize_script('two-elementor-editor-scripts', 'two_elementor_vars', [
379 'nonce' => wp_create_nonce('two_elementor_regenerate_ccss'),
380 'ajax_url' => admin_url('admin-ajax.php')
381 ]);
382 }
383
384 public function maybeRegenerateCCSS($et_option_name, $et_option_new_value)
385 {
386 //set old divi_custom_css
387 //divi_custom_css option is not set as other settings
388 $oldCustomCSS = get_option('et_divi')['divi_custom_css'];
389 add_option(TW_OPTIMIZE_PREFIX . '_old_divi_custom_css', $oldCustomCSS, false);
390 $old_value = et_get_option($et_option_name, []);
391 $diviOptionNames = [
392 'divi_dynamic_css',
393 'divi_critical_css',
394 'divi_critical_threshold_height',
395 'divi_dynamic_js_libraries',
396 ];
397
398 if (in_array($et_option_name, $diviOptionNames) && $et_option_new_value !== $old_value
399 && !get_option(TW_OPTIMIZE_PREFIX . '_clear_cache_after_divi')) {
400 //clear cache right here is causing issues with divi
401 update_option(TW_OPTIMIZE_PREFIX . '_clear_cache_after_divi', 1, false);
402 }
403 }
404 }
405