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
OptimizerElementor.php
337 lines
| 1 | <?php |
| 2 | |
| 3 | namespace TenWebOptimizer; |
| 4 | |
| 5 | use Elementor\Core\Settings\Manager as SettingsManager; |
| 6 | |
| 7 | /** |
| 8 | * Class OptimizerElementor |
| 9 | */ |
| 10 | class OptimizerElementor |
| 11 | { |
| 12 | public function __construct() |
| 13 | { |
| 14 | add_action('elementor/editor/after_enqueue_scripts', [ $this, 'two_scripts_styles' ]); |
| 15 | add_action('elementor/init', [ $this, 'two_add_panel_tab' ]); |
| 16 | add_action('elementor/documents/register_controls', [ $this, 'two_register_document_controls' ]); |
| 17 | } |
| 18 | |
| 19 | /* Enque scripts/styles for Elementor editor */ |
| 20 | public function two_scripts_styles() |
| 21 | { |
| 22 | global $post; |
| 23 | $page_url = get_permalink($post->ID); |
| 24 | |
| 25 | if (!current_user_can('administrator')) { |
| 26 | return; |
| 27 | } |
| 28 | wp_register_style('two-open-sans', 'https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,600,700,800&display=swap'); |
| 29 | wp_enqueue_style('two_speed_css', TENWEB_SO_URL . '/assets/css/speed.css', [ 'two-open-sans' ], TENWEB_SO_VERSION); |
| 30 | wp_enqueue_style('two_speed_dark_css', TENWEB_SO_URL . '/assets/css/speed_elementor_dark.css', [ 'two-open-sans', 'elementor-editor-dark-mode' ], TENWEB_SO_VERSION); |
| 31 | wp_enqueue_script('two_circle_js', TENWEB_SO_URL . '/assets/js/circle-progress.js', [ 'jquery' ], TENWEB_SO_VERSION); |
| 32 | wp_enqueue_script('two_speed_js', TENWEB_SO_URL . '/assets/js/speed.js', [ |
| 33 | 'jquery', |
| 34 | 'two_circle_js' |
| 35 | ], TENWEB_SO_VERSION); |
| 36 | wp_localize_script('two_speed_js', 'two_speed', [ |
| 37 | 'nonce' => wp_create_nonce('two_ajax_nonce'), |
| 38 | 'ajax_url' => admin_url('admin-ajax.php'), |
| 39 | 'clearing' => __('Clearing...', 'tenweb-speed-optimizer'), |
| 40 | 'cleared' => __('Cleared cache', 'tenweb-speed-optimizer'), |
| 41 | 'clear' => __('Clear cache', 'tenweb-speed-optimizer'), |
| 42 | 'title' => __('10Web Booster', 'tenweb-speed-optimizer'), |
| 43 | 'optimize_entire_website' => \TenWebOptimizer\OptimizerOnInit::two_reached_limit(), |
| 44 | 'post_type' => $post->post_type, |
| 45 | 'post_status' => get_post_status($post->ID), |
| 46 | 'post_optimizable' => \TenWebOptimizer\OptimizerUrl::urlIsOptimizable($page_url), |
| 47 | ]); |
| 48 | } |
| 49 | |
| 50 | /* Register new tab in page settings */ |
| 51 | public function two_add_panel_tab() |
| 52 | { |
| 53 | if (!current_user_can('administrator')) { |
| 54 | return; |
| 55 | } |
| 56 | \Elementor\Controls_Manager::add_tab( |
| 57 | 'two_optimize', |
| 58 | esc_html__('10Web Booster', 'tenweb-speed-optimizer') |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Register additional document controls. |
| 64 | * |
| 65 | * @param \Elementor\Core\DocumentTypes\PageBase $document the PageBase document instance |
| 66 | */ |
| 67 | public function two_register_document_controls($document) |
| 68 | { |
| 69 | if (!current_user_can('administrator')) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | if (! $document instanceof \Elementor\Core\DocumentTypes\PageBase || ! $document::get_property('has_elements')) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | $document->start_controls_section( |
| 78 | 'two_optimize_section', |
| 79 | [ |
| 80 | 'tab' => 'two_optimize', |
| 81 | ] |
| 82 | ); |
| 83 | |
| 84 | global $post; |
| 85 | $post_id = $post->ID; |
| 86 | |
| 87 | /* Check home page */ |
| 88 | if (get_option('page_on_front') == $post_id) { |
| 89 | $post_id = 'front_page'; |
| 90 | $page_score = get_option('two-front-page-speed'); |
| 91 | } else { |
| 92 | $page_score = get_post_meta($post_id, 'two_page_speed', true); |
| 93 | } |
| 94 | $ui_theme = 'auto'; |
| 95 | |
| 96 | if (class_exists('Elementor\Core\Settings\Manager')) { |
| 97 | $ui_theme = SettingsManager::get_settings_managers('editorPreferences')->get_model()->get_settings('ui_theme'); |
| 98 | } |
| 99 | $classname = 'two_elementor_' . $ui_theme . ' '; |
| 100 | $status = 'optimized'; |
| 101 | $critical_pages = \TenWebOptimizer\OptimizerUtils::getCriticalPages(); |
| 102 | |
| 103 | if (\TenWebWpTransients\OptimizerTransients::get('two_optimize_inprogress_' . $post_id)) { |
| 104 | $status = 'optimizing'; |
| 105 | } elseif (!array_key_exists($post_id, $critical_pages)) { |
| 106 | $status = 'notOptimized'; |
| 107 | } |
| 108 | |
| 109 | $reach_limit = \TenWebOptimizer\OptimizerOnInit::two_reached_limit(); |
| 110 | |
| 111 | if ($status != 'optimized') { |
| 112 | if ($reach_limit != false) { |
| 113 | $label = '<p class="two_elementor_control_title' . ($status == 'optimizing' ? ' two-hidden' : '') . '">' . esc_html__('You’ve reached the Free Plan limit', 'tenweb-speed-optimizer') . '</p>'; |
| 114 | } else { |
| 115 | $label = '<p class="two_elementor_control_title' . ($status == 'optimizing' ? ' two-hidden' : '') . '">' . esc_html__('Optimize with 10Web Booster', 'tenweb-speed-optimizer') . '</p>'; |
| 116 | } |
| 117 | $content = $this->two_elementor_not_optimized_content($status, $post_id); |
| 118 | $classname = $classname . 'two_elementor_settings_content' . ($status == 'optimizing' ? ' two-optimizing' : ''); |
| 119 | } else { |
| 120 | $page_title = get_the_title($post_id); |
| 121 | $label = ''; |
| 122 | $content = $this->two_elementor_optimized_content($page_title, $page_score, $status, $post_id); |
| 123 | $classname = $classname . 'two_elementor_settings_content two_optimized'; |
| 124 | } |
| 125 | |
| 126 | $document->add_control( |
| 127 | 'raw_html', |
| 128 | [ |
| 129 | 'label' => $label, |
| 130 | 'type' => \Elementor\Controls_Manager::RAW_HTML, |
| 131 | 'raw' => $content, |
| 132 | 'content_classes' => $classname, |
| 133 | ] |
| 134 | ); |
| 135 | |
| 136 | $document->end_controls_section(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Elementor editor booster info in case of page not optimized |
| 141 | * |
| 142 | * @param $status bool |
| 143 | * @param $post_id integer |
| 144 | * |
| 145 | * @return string html data |
| 146 | */ |
| 147 | public function two_elementor_not_optimized_content($status, $post_id) |
| 148 | { |
| 149 | $reach_limit = \TenWebOptimizer\OptimizerOnInit::two_reached_limit(); |
| 150 | ob_start(); |
| 151 | |
| 152 | if ($reach_limit != false) { ?> |
| 153 | <div class="two_elementor_control_container<?php echo $status == 'optimizing' ? ' two-hidden' : ''; ?>"> |
| 154 | <p class="two_elementor_control_container_description"><?php echo esc_html__('Upgrade to 10Web Booster Pro to optimize all pages and enable Cloudflare Enterprise CDN.', 'textdomain'); ?></p> |
| 155 | <a href="<?php echo esc_url($reach_limit . '?two_comes_from=ElementorAfterLimit'); ?>" target="_blank" data-post-id="<?php echo esc_attr($post_id); ?>" |
| 156 | data-initiator="elementor" class="two_optimize_button_elementor two_optimize_button"><?php _e('Upgrade', 'tenweb-speed-optimizer'); ?> |
| 157 | </a> |
| 158 | </div> |
| 159 | <?php } else { ?> |
| 160 | <div class="two_elementor_control_container<?php echo $status == 'optimizing' ? ' two-hidden' : ''; ?>"> |
| 161 | <p><?php echo esc_html__('Get a 90+ PageSpeed score', 'textdomain'); ?></p> |
| 162 | <a onclick="<?php echo 'two_optimize_page(this)'; ?>" data-post-id="<?php echo esc_attr($post_id); ?>" |
| 163 | data-initiator="elementor" class="two_optimize_button_elementor two_optimize_button"><?php _e('Optimize now', 'tenweb-speed-optimizer'); ?> |
| 164 | </a> |
| 165 | </div> |
| 166 | <span class="two-page-speed two-optimizing two-loading-bg <?php echo $status == 'optimizing' ? '' : ' two-hidden'; ?>"> |
| 167 | <?php _e('Optimizing...', 'tenweb-speed-optimizer'); ?> |
| 168 | <p class="two-description"><?php _e('Reload in 2 minutes to see<br> the new score', 'tenweb-speed-optimizer'); ?></p> |
| 169 | </span> |
| 170 | <?php } |
| 171 | |
| 172 | return ob_get_clean(); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Elementor editor booster info in case of page optimized |
| 177 | * |
| 178 | * @param $page_title string |
| 179 | * @param $score_data array |
| 180 | * |
| 181 | * @return string html data |
| 182 | */ |
| 183 | public function two_elementor_optimized_content($page_title, $score_data, $status, $post_id) |
| 184 | { |
| 185 | $date = 0; |
| 186 | |
| 187 | if (!empty($score_data) && isset($score_data['current_score'])) { |
| 188 | $optimized_pages = \TenWebOptimizer\OptimizerUtils::getCriticalPages(); |
| 189 | |
| 190 | if (isset($optimized_pages[$post_id]) && isset($optimized_pages[$post_id]['critical_date'])) { |
| 191 | $date = $optimized_pages[$post_id]['critical_date']; |
| 192 | } elseif (isset($score_data['current_score']['date'])) { |
| 193 | $date = strtotime($score_data['current_score']['date']); |
| 194 | } |
| 195 | } |
| 196 | $modified_date = get_the_modified_date('d.m.Y h:i:s a', $post_id); |
| 197 | $modified_date = strtotime($modified_date); |
| 198 | $reanalyze_button_status_previous = false; |
| 199 | $reanalyze_button_status_current = false; |
| 200 | |
| 201 | if (!empty($score_data)) { |
| 202 | if (isset($score_data['current_score']) && isset($score_data['current_score']['status']) |
| 203 | && $score_data['current_score']['status'] == 'inprogress') { |
| 204 | $reanalyze_button_status_current = true; |
| 205 | } |
| 206 | |
| 207 | if (isset($score_data['previous_score']) && isset($score_data['previous_score']['status']) |
| 208 | && $score_data['previous_score']['status'] == 'inprogress') { |
| 209 | $reanalyze_button_status_previous = true; |
| 210 | } |
| 211 | } |
| 212 | ob_start(); ?> |
| 213 | <script> |
| 214 | jQuery('.two-score-circle').each(function () { |
| 215 | two_draw_score_circle(this); |
| 216 | }); |
| 217 | </script> |
| 218 | <p class="two-elementor-container-title"><?php echo '<span>' . esc_html__($page_title) . '</span>' . esc_html__(' page is successfully optimized', 'textdomain'); ?></p> |
| 219 | <div class="two-score-section two-any-reanalyzing-score-section" data-id="<?php echo esc_attr($post_id); ?>"> |
| 220 | <div class="two-score-container-both"> |
| 221 | <div class="two-score-container-old"> |
| 222 | <div class="two-score-header"><?php _e('Before optimization', 'tenweb-speed-optimizer'); ?></div> |
| 223 | <?php if (empty($score_data) || !isset($score_data['previous_score']) || $reanalyze_button_status_previous) { |
| 224 | $no_old_scores = 'two-no-scores'; |
| 225 | } ?> |
| 226 | <div class="two-old-scores <?php echo isset($no_old_scores) ? esc_html($no_old_scores) : ''; ?>" |
| 227 | data-no-score-for="<?php echo $reanalyze_button_status_previous ? esc_attr($post_id) : ''; ?>"> |
| 228 | <div class="two-score-mobile"> |
| 229 | <div class="two-score-circle two_circle_with_bg" |
| 230 | data-id="mobile" |
| 231 | data-thickness="2" |
| 232 | data-size="40" |
| 233 | data-score="<?php echo (!isset($no_old_scores) && isset($score_data['previous_score']) |
| 234 | && isset($score_data['previous_score']['mobile_score'])) ? (int) $score_data['previous_score']['mobile_score'] : ''; ?>" |
| 235 | data-loading-time="<?php echo (!isset($no_old_scores) && isset($score_data['previous_score']) |
| 236 | && isset($score_data['previous_score']['mobile_tti'])) ? esc_html($score_data['previous_score']['mobile_tti']) : ''; ?>"> |
| 237 | <span class="two-score-circle-animated"></span> |
| 238 | </div> |
| 239 | <div class="two-score-text"> |
| 240 | <span class="two-score-text-name"><?php _e('Mobile score', 'tenweb-speed-optimizer'); ?></span> |
| 241 | <span class="two-load-text-time"><?php _e('Load time: ', 'tenweb-speed-optimizer'); ?><span class="two-load-time"></span><?php _e('s', 'tenweb-speed-optimizer'); ?></span> |
| 242 | </div> |
| 243 | </div> |
| 244 | <div class="two-score-desktop"> |
| 245 | <div class="two-score-circle two_circle_with_bg" |
| 246 | data-id="desktop" |
| 247 | data-thickness="2" |
| 248 | data-size="40" |
| 249 | data-score="<?php echo (!isset($no_old_scores) && isset($score_data['previous_score']) |
| 250 | && isset($score_data['previous_score']['desktop_score'])) ? (int) $score_data['previous_score']['desktop_score'] : ''; ?>" |
| 251 | data-loading-time="<?php echo (!isset($no_old_scores) && isset($score_data['previous_score']) |
| 252 | && isset($score_data['previous_score']['desktop_tti'])) ? esc_html($score_data['previous_score']['desktop_tti']) : ''; ?>"> |
| 253 | <span class="two-score-circle-animated"></span> |
| 254 | </div> |
| 255 | <div class="two-score-text"> |
| 256 | <span class="two-score-text-name"><?php _e('Desktop score', 'tenweb-speed-optimizer'); ?></span> |
| 257 | <span class="two-load-text-time"><?php _e('Load time: ', 'tenweb-speed-optimizer'); ?><span class="two-load-time"></span><?php _e('s', 'tenweb-speed-optimizer'); ?></span> |
| 258 | </div> |
| 259 | </div> |
| 260 | <a onclick="<?php echo 'two_reanalyze_score(this)'; ?>" data-from-elementor="1" data-post_id="<?php echo esc_attr($post_id); ?>" target="_blank" |
| 261 | data-initiator="elementor" class="two_reanalyze_link <?php echo $reanalyze_button_status_previous ? 'two-hidden' : ''; ?>"> |
| 262 | </a> |
| 263 | <span class="two-page-speed two-optimizing <?php echo $reanalyze_button_status_previous ? '' : 'two-hidden'; ?>"></span> |
| 264 | </div> |
| 265 | </div> |
| 266 | <div class="two-score-container-new"> |
| 267 | <div class="two-score-header"><?php _e('After optimization', 'tenweb-speed-optimizer'); ?></div> |
| 268 | <?php if (empty($score_data) || !isset($score_data['current_score']) || $reanalyze_button_status_current) { |
| 269 | $no_new_scores = 'two-no-scores'; |
| 270 | } ?> |
| 271 | <div class="two-new-scores <?php echo isset($no_new_scores) ? esc_html($no_new_scores) : ''; ?>" |
| 272 | data-no-score-for="<?php echo $reanalyze_button_status_current ? esc_attr($post_id) : ''; ?>"> |
| 273 | <div class="two-score-mobile"> |
| 274 | <div class="two-score-circle two_circle_with_bg" |
| 275 | data-id="mobile" |
| 276 | data-thickness="2" |
| 277 | data-size="40" |
| 278 | data-score="<?php echo (!isset($no_new_scores) && isset($score_data['current_score']) |
| 279 | && isset($score_data['current_score']['mobile_score'])) ? (int) $score_data['current_score']['mobile_score'] : ''; ?>" |
| 280 | data-loading-time="<?php echo (!isset($no_new_scores) && isset($score_data['current_score']) |
| 281 | && isset($score_data['current_score']['mobile_tti'])) ? esc_html($score_data['current_score']['mobile_tti']) : ''; ?>"> |
| 282 | <span class="two-score-circle-animated"></span> |
| 283 | </div> |
| 284 | <div class="two-score-text"> |
| 285 | <span class="two-score-text-name"><?php _e('Mobile score', 'tenweb-speed-optimizer'); ?></span> |
| 286 | <span class="two-load-text-time"><?php _e('Load time: ', 'tenweb-speed-optimizer'); ?> |
| 287 | <span class="two-load-time"></span><?php _e('s', 'tenweb-speed-optimizer'); ?></span> |
| 288 | </div> |
| 289 | </div> |
| 290 | <div class="two-score-desktop"> |
| 291 | <div class="two-score-circle two_circle_with_bg" |
| 292 | data-id="desktop" |
| 293 | data-thickness="2" |
| 294 | data-size="40" |
| 295 | data-score="<?php echo (!isset($no_new_scores) && isset($score_data['current_score']) |
| 296 | && isset($score_data['current_score']['desktop_score'])) ? (int) $score_data['current_score']['desktop_score'] : ''; ?>" |
| 297 | data-loading-time="<?php echo (!isset($no_new_scores) && isset($score_data['current_score']) |
| 298 | && isset($score_data['current_score']['desktop_tti'])) ? esc_html($score_data['current_score']['desktop_tti']) : ''; ?>"> |
| 299 | <span class="two-score-circle-animated"></span> |
| 300 | </div> |
| 301 | <div class="two-score-text"> |
| 302 | <span class="two-score-text-name"><?php _e('Desktop score', 'tenweb-speed-optimizer'); ?></span> |
| 303 | <span class="two-load-text-time"><?php _e('Load time: ', 'tenweb-speed-optimizer'); ?><span class="two-load-time"></span><?php _e('s', 'tenweb-speed-optimizer'); ?></span> |
| 304 | </div> |
| 305 | </div> |
| 306 | <a onclick="<?php echo 'two_reanalyze_score(this)'; ?>" data-from-elementor="1" data-post_id="<?php echo esc_attr($post_id); ?>" target="_blank" |
| 307 | data-initiator="elementor" class="two_reanalyze_link <?php echo $reanalyze_button_status_current ? 'two-hidden' : ''; ?>"> |
| 308 | </a> |
| 309 | <span class="two-page-speed two-optimizing <?php echo $reanalyze_button_status_current ? '' : 'two-hidden'; ?>"></span> |
| 310 | </div> |
| 311 | </div> |
| 312 | </div> |
| 313 | <div class="two_reanalyze_container"> |
| 314 | <span class="two-page-speed two-optimizing <?php echo $reanalyze_button_status_current ? '' : 'two-hidden'; ?>"></span> |
| 315 | <a onclick="two_reanalyze_score(this)" data-from-elementor="1" data-post_id="<?php echo esc_attr($post_id); ?>" |
| 316 | data-initiator="elementor" class="two_reanalyze_button"> |
| 317 | <?php $reanalyze_button_status_current ? _e('Reanalyzing...', 'tenweb-speed-optimizer') : _e('Reanalyze', 'tenweb-speed-optimizer'); ?> |
| 318 | </a> |
| 319 | </div> |
| 320 | <div class="two_elementor_control_container<?php echo ($modified_date > $date && $date != 0) ? '' : ' two-hidden'; ?>"> |
| 321 | <a onclick="two_optimize_page(this)" |
| 322 | data-post-id="<?php echo esc_attr($post_id); ?>" |
| 323 | data-initiator="elementor" |
| 324 | class="two_optimize_button_elementor two_optimize_button <?php echo (!$reanalyze_button_status_current && !$reanalyze_button_status_previous) ? '' : ' two-button-disabled'; ?>"> |
| 325 | <?php _e('Re-optimize', 'tenweb-speed-optimizer'); ?> |
| 326 | </a> |
| 327 | </div> |
| 328 | </div> |
| 329 | <span class="two-page-speed two-optimizing two-loading-bg <?php echo $status == 'optimizing' ? '' : ' two-hidden'; ?>"> |
| 330 | <?php _e('Re-optimizing...', 'tenweb-speed-optimizer'); ?> |
| 331 | <p class="two-description"><?php _e('Reload in 2 minutes to see<br> the new score', 'tenweb-speed-optimizer'); ?></p> |
| 332 | </span> |
| 333 | <?php |
| 334 | return ob_get_clean(); |
| 335 | } |
| 336 | } |
| 337 |