admin
3 years ago
cache
3 years ago
css
3 years ago
images
3 years ago
javascript
3 years ago
vendor
3 years ago
cwvpsb_iframe.css
3 years ago
cwvpsb_iframe.js
3 years ago
functions.php
3 years ago
gravatar.php
3 years ago
helper-section.php
3 years ago
style-sanitizer.php
3 years ago
functions.php
389 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; // Exit if accessed directly. |
| 4 | } |
| 5 | |
| 6 | // Add Settings Page |
| 7 | require_once CWVPSB_PLUGIN_DIR."includes/admin/settings.php"; |
| 8 | require_once CWVPSB_PLUGIN_DIR."includes/gravatar.php"; |
| 9 | |
| 10 | add_filter('plugin_action_links_core-web-vitals-page-speed-booster/core-web-vitals-page-speed-booster.php', 'cwvpsb_add_settings_link'); |
| 11 | function cwvpsb_add_settings_link( $links ) { |
| 12 | $links[] = '<a href="' . |
| 13 | admin_url( 'admin.php?page=cwvpsb-images' ) . |
| 14 | '">' . 'Settings' . '</a>'; |
| 15 | return $links; |
| 16 | } |
| 17 | |
| 18 | function cwvpsb_complete_html_after_dom_loaded( $content ) { |
| 19 | if(function_exists('is_feed')&& is_feed()){return $content;} |
| 20 | $content = apply_filters('cwvpsb_complete_html_after_dom_loaded', $content); |
| 21 | return $content; |
| 22 | } |
| 23 | add_action('wp', function(){ |
| 24 | |
| 25 | if ( cwvpsb_amp_support_enabled() ) { return; } |
| 26 | ob_start('cwvpsb_complete_html_after_dom_loaded'); }, 999); |
| 27 | |
| 28 | $settings = cwvpsb_defaults(); |
| 29 | |
| 30 | if(isset($settings['cache_support'])){ |
| 31 | add_action( |
| 32 | 'plugins_loaded', |
| 33 | array( |
| 34 | 'CWVPSB_Cache', |
| 35 | 'instance' |
| 36 | ) |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | register_activation_hook( |
| 41 | __FILE__, |
| 42 | array( |
| 43 | 'CWVPSB_Cache', |
| 44 | 'on_activation' |
| 45 | ) |
| 46 | ); |
| 47 | register_deactivation_hook( |
| 48 | __FILE__, |
| 49 | array( |
| 50 | 'CWVPSB_Cache', |
| 51 | 'on_deactivation' |
| 52 | ) |
| 53 | ); |
| 54 | register_uninstall_hook( |
| 55 | __FILE__, |
| 56 | array( |
| 57 | 'CWVPSB_Cache', |
| 58 | 'on_uninstall' |
| 59 | ) |
| 60 | ); |
| 61 | // autoload register |
| 62 | spl_autoload_register('cwvpsb_cache_autoload'); |
| 63 | |
| 64 | // autoload function |
| 65 | function cwvpsb_cache_autoload($class) { |
| 66 | require_once( |
| 67 | sprintf( |
| 68 | '%s/includes/cache/cache-class.php', |
| 69 | CWVPSB_DIR, |
| 70 | strtolower($class) |
| 71 | ) |
| 72 | ); |
| 73 | require_once( |
| 74 | sprintf( |
| 75 | '%s/includes/cache/disk-cache-class.php', |
| 76 | CWVPSB_DIR, |
| 77 | strtolower($class) |
| 78 | ) |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | //Load plugin textdomain |
| 83 | add_action( 'init', 'cwvpsb_load_textdomain' ); |
| 84 | function cwvpsb_load_textdomain() { |
| 85 | load_plugin_textdomain( 'cwvpsb_textdomain', false, dirname( CWVPSB_BASE ) . '/languages' ); |
| 86 | } |
| 87 | |
| 88 | add_action('wp_ajax_cwvpsb_clear_cached_css', 'cwvpsb_clear_cached_css'); |
| 89 | function cwvpsb_clear_cached_css(){ |
| 90 | if(isset($_POST['nonce_verify']) && !wp_verify_nonce($_POST['nonce_verify'],'cwv-security-nonce')){ |
| 91 | echo json_encode(array("status"=> 400, "msg"=>esc_html__("Security verification failed, Refresh the page", 'cwvpsb') ));die; |
| 92 | } |
| 93 | $clean_types = array('css'); |
| 94 | if(!in_array($_POST['cleaning'], $clean_types)){ |
| 95 | echo json_encode(array("status"=> 400, "msg"=>esc_html__("Cache type not found", 'cwvpsb') ));die; |
| 96 | } |
| 97 | $cleaning = $_POST['cleaning']; |
| 98 | |
| 99 | $upload_dir = wp_upload_dir(); |
| 100 | |
| 101 | //Clean css |
| 102 | if($cleaning == 'css'){ |
| 103 | $user_dirname = $upload_dir['basedir'] . '/' . 'web_vital'; |
| 104 | $dir_handle = opendir($user_dirname); |
| 105 | if (!$dir_handle){ |
| 106 | echo json_encode(array("status"=> 400, "msg"=>esc_html__("cache not found", 'cwvpsb') ));die; |
| 107 | } |
| 108 | while($file = readdir($dir_handle)) { |
| 109 | if (strpos($file, '.css') !== false){ |
| 110 | unlink($user_dirname."/".$file); |
| 111 | } |
| 112 | } |
| 113 | closedir($dir_handle); |
| 114 | } |
| 115 | echo json_encode(array("status"=> 200, "msg"=>esc_html__("CSS Cleared", 'cwvpsb') ));die; |
| 116 | } |
| 117 | |
| 118 | function cwvpsb_admin_link($tab = '', $args = array()){ |
| 119 | $page = 'cwvpsb'; |
| 120 | if ( ! is_multisite() ) { |
| 121 | $link = admin_url( 'admin.php?page=' . $page ); |
| 122 | } |
| 123 | else { |
| 124 | $link = get_dashboard_url(0,'admin.php?page=' . $page ); |
| 125 | } |
| 126 | |
| 127 | if ( $tab ) { |
| 128 | $link .= '&tab=' . $tab; |
| 129 | } |
| 130 | |
| 131 | if ( $args ) { |
| 132 | foreach ( $args as $arg => $value ) { |
| 133 | $link .= '&' . $arg . '=' . urlencode( $value ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return esc_url($link); |
| 138 | } |
| 139 | |
| 140 | function cwvpsb_get_tab( $default = '', $available = array() ) { |
| 141 | |
| 142 | $tab = isset( $_GET['tab'] ) ? sanitize_text_field($_GET['tab']) : $default; |
| 143 | if ( ! in_array( $tab, $available ) ) { |
| 144 | $tab = $default; |
| 145 | } |
| 146 | return $tab; |
| 147 | } |
| 148 | |
| 149 | function cwvpsb_defaults(){ |
| 150 | $defaults = array( |
| 151 | 'image_optimization' =>1, |
| 152 | 'webp_support' => 'auto', |
| 153 | 'lazyload_support' => 1, |
| 154 | 'minification_support' => 1, |
| 155 | 'unused_css_support' => 0, |
| 156 | 'google_fonts_support' => 1, |
| 157 | 'js_optimization' => 1, |
| 158 | 'delay_js' => 'php', |
| 159 | 'whitelist_css'=>array(), |
| 160 | 'critical_css_support'=>1, |
| 161 | 'cache_support_method'=>'Highly Optimized', |
| 162 | 'cache_support'=>1, |
| 163 | 'advance_support'=>'', |
| 164 | 'exclude_delay_js'=>'', |
| 165 | 'critical_css_on_home' => 1, |
| 166 | 'critical_css_on_cp_type' => array( |
| 167 | 'post' => 1 |
| 168 | ) |
| 169 | ); |
| 170 | if ( is_multisite() && is_plugin_active_for_network(CWVPSB_BASE) ) { |
| 171 | $settings = get_site_option( 'cwvpsb_get_settings', $defaults ); |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | $settings = get_option( 'cwvpsb_get_settings', $defaults ); |
| 176 | } |
| 177 | |
| 178 | $settings['unused_css_support'] = 0; |
| 179 | return $settings; |
| 180 | } |
| 181 | |
| 182 | add_action('admin_enqueue_scripts','cwvpsb_admin_enqueue'); |
| 183 | function cwvpsb_admin_enqueue($check) { |
| 184 | if ( !is_admin() ) { |
| 185 | return; |
| 186 | } |
| 187 | if($check != 'toplevel_page_cwvpsb'){ |
| 188 | return; |
| 189 | } |
| 190 | wp_enqueue_script('cwvpsb-datatable-script', CWVPSB_PLUGIN_DIR_URI . '/includes/admin/js/jquery.dataTables.min.js', ['jquery']); |
| 191 | wp_enqueue_style( 'cwvpsb-datatable-style', CWVPSB_PLUGIN_DIR_URI . '/includes/admin/js/jquery.dataTables.min.css' ); |
| 192 | |
| 193 | wp_register_style( 'cwvpsb-admin-css', CWVPSB_PLUGIN_DIR_URI . '/includes/admin/style.css', false, CWVPSB_VERSION ); |
| 194 | wp_enqueue_style( 'cwvpsb-admin-css' ); |
| 195 | |
| 196 | $data = array( |
| 197 | 'cwvpsb_security_nonce' => wp_create_nonce('cwvpsb_ajax_check_nonce') , |
| 198 | 'cwvpsb_showdetails_data_nonce' => wp_create_nonce('cwvpsb_showdetails_data_nonce') , |
| 199 | ); |
| 200 | wp_register_script( 'cwvpsb-admin-js', CWVPSB_PLUGIN_DIR_URI . 'includes/admin/script.js', array('cwvpsb-datatable-script'), CWVPSB_VERSION , true ); |
| 201 | wp_localize_script( 'cwvpsb-admin-js', 'cwvpsb_localize_data', $data ); |
| 202 | wp_enqueue_script( 'cwvpsb-admin-js' ); |
| 203 | } |
| 204 | |
| 205 | add_filter('cwvpsb_complete_html_after_dom_loaded','cwvpsb_google_fonts_swap'); |
| 206 | function cwvpsb_google_fonts_swap( $html ) { |
| 207 | $html = str_replace("&display=swap", "", $html); |
| 208 | $html = str_replace("googleapis.com/css?family", "googleapis.com/css?display=swap&family", $html); |
| 209 | $html = str_replace("googleapis.com/css2?family", "googleapis.com/css2?display=swap&family", $html); |
| 210 | $html = preg_replace("/(WebFontConfig\['google'\])(.+[\w])(.+};)/", '$1$2&display=swap$3', $html); |
| 211 | return $html; |
| 212 | } |
| 213 | |
| 214 | add_filter('cwvpsb_complete_html_after_dom_loaded','web_vitals_changes'); |
| 215 | function web_vitals_changes($html){ |
| 216 | if(!$html){ return $html; } |
| 217 | if(function_exists('is_feed')&& is_feed()){return $html;} |
| 218 | $settings = cwvpsb_defaults(); |
| 219 | if (is_admin()) { |
| 220 | return $html; |
| 221 | } |
| 222 | if($settings['webp_support'] == 'auto'){ |
| 223 | return $html; |
| 224 | } |
| 225 | if ( function_exists('is_checkout') && is_checkout() ) { |
| 226 | return $html; |
| 227 | } |
| 228 | $guessurl = site_url(); |
| 229 | if ( ! $guessurl ) { |
| 230 | $guessurl = wp_guess_url(); |
| 231 | } |
| 232 | $base_url = untrailingslashit( $guessurl ); |
| 233 | $upload = wp_upload_dir(); |
| 234 | |
| 235 | $tmpDoc = new DOMDocument(); |
| 236 | libxml_use_internal_errors(true); |
| 237 | $tmpDoc->loadHTML($html); |
| 238 | |
| 239 | $xpath = new DOMXPath( $tmpDoc ); |
| 240 | $domImg = $xpath->query( "//img[@src]"); |
| 241 | |
| 242 | if(count($domImg)>0){ |
| 243 | foreach ($domImg as $key => $element) { |
| 244 | $srcupdate = $element->getAttribute("src"); |
| 245 | if(strpos($srcupdate, $base_url)!==false){ |
| 246 | //test page exists or not |
| 247 | $srcupdatePath = str_replace($upload['baseurl'], $upload['basedir'].'/web-vital-webp', $srcupdate); |
| 248 | $srcupdatePath = "$srcupdatePath.webp"; |
| 249 | if(file_exists($srcupdatePath)){ |
| 250 | $srcupdate = str_replace($upload['baseurl'], $upload['baseurl'].'/web-vital-webp', $srcupdate); |
| 251 | $srcupdate = "$srcupdate.webp"; |
| 252 | $element->setAttribute("src", $srcupdate); |
| 253 | } |
| 254 | |
| 255 | } |
| 256 | if($element->hasAttribute('srcset')){ |
| 257 | $attrValue = $element->getAttribute("srcset"); |
| 258 | |
| 259 | $srcsetArr = explode(',', $attrValue); |
| 260 | foreach ($srcsetArr as $i => $srcSetEntry) { |
| 261 | // $srcSetEntry is ie "image.jpg 520w", but can also lack width, ie just "image.jpg" |
| 262 | // it can also be ie "image.jpg 2x" |
| 263 | $srcSetEntry = trim($srcSetEntry); |
| 264 | $entryParts = preg_split('/\s+/', $srcSetEntry, 2); |
| 265 | if (count($entryParts) == 2) { |
| 266 | list($src, $descriptors) = $entryParts; |
| 267 | } else { |
| 268 | $src = $srcSetEntry; |
| 269 | $descriptors = null; |
| 270 | } |
| 271 | |
| 272 | if(strpos($src, $base_url)!==false){ |
| 273 | //test page exists or not |
| 274 | $srcupdatePath = str_replace($upload['baseurl'], $upload['basedir'].'/web-vital-webp', $src); |
| 275 | $srcupdatePath = "$srcupdatePath.webp"; |
| 276 | if(file_exists($srcupdatePath)){ |
| 277 | $webpUrl = str_replace($upload['baseurl'], $upload['baseurl'].'/web-vital-webp', $src); |
| 278 | $webpUrl .= '.webp'; |
| 279 | }else{ $webpUrl = $src; } |
| 280 | }else{ $webpUrl = $src; } |
| 281 | if ($webpUrl !== false) { |
| 282 | $srcsetArr[$i] = $webpUrl . (isset($descriptors) ? ' ' . $descriptors : ''); |
| 283 | } |
| 284 | } |
| 285 | $newSrcsetArr = implode(', ', $srcsetArr); |
| 286 | $attrValue = $element->setAttribute("srcset", $newSrcsetArr); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | $html = $tmpDoc->saveHTML(); |
| 291 | return $html; |
| 292 | } |
| 293 | |
| 294 | add_action( 'current_screen', 'cwvpsb_remove_wp_footer_notice' ); |
| 295 | function cwvpsb_remove_wp_footer_notice() { |
| 296 | if ( is_admin() ) { |
| 297 | $my_current_screen = get_current_screen(); |
| 298 | if ( isset( $my_current_screen->base ) && 'toplevel_page_cwvpsb' === $my_current_screen->base ) { |
| 299 | add_filter( 'admin_footer_text', '__return_empty_string', 11 ); |
| 300 | add_filter( 'update_footer', '__return_empty_string', 11 ); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | add_action('pre_amp_render_post','cwvpsb_amp_support'); |
| 306 | function cwvpsb_amp_support(){ |
| 307 | remove_all_filters( 'cwvpsb_complete_html_after_dom_loaded' ); |
| 308 | } |
| 309 | |
| 310 | add_action('wp' , 'cwvpsb_on_specific_url'); |
| 311 | function cwvpsb_on_specific_url(){ |
| 312 | $settings = cwvpsb_defaults(); |
| 313 | $url = $settings['advance_support']; |
| 314 | if (empty($url)) { |
| 315 | return; |
| 316 | } |
| 317 | $url_id = url_to_postid( $url ); |
| 318 | $id = get_the_ID(); |
| 319 | if (is_home() && $url == home_url( '/' )) { |
| 320 | if ( is_multisite() && is_plugin_active_for_network(CWVPSB_BASE) ) { |
| 321 | $page_for_posts = get_site_option( 'page_for_posts' ); |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | $page_for_posts = get_option( 'page_for_posts' ); |
| 326 | } |
| 327 | |
| 328 | $post = get_post($page_for_posts); |
| 329 | $url_id = $post->ID; |
| 330 | } |
| 331 | if ($url_id != $id ) { |
| 332 | add_filter( 'cwvpsb_complete_html_after_dom_loaded', '__return_false' ); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | add_filter('the_content', 'cwvpsb_iframe_delay'); |
| 337 | |
| 338 | function cwvpsb_iframe_delay($content) { |
| 339 | if(function_exists('is_feed')&& is_feed()){return $content;} |
| 340 | $content = preg_replace('/<iframe(?!iframe)(.+)youtube\.com\/embed\/(?!iframe)(?!videoseries)(.+?)\?(.+)<\/iframe>/', '<div class="cwvpsb_iframe"><div class="iframe_wrap"><div class="iframe_player" data-embed="${2}" id="player_${2}"><div class="play-button"></div></div></div></div>', $content); |
| 341 | |
| 342 | global $iframe_check; |
| 343 | $iframe_check = preg_match( '/iframe_player/i', $content, $result ); |
| 344 | return $content; |
| 345 | } |
| 346 | |
| 347 | add_action("wp_footer", "cwvpsb_iframe_delay_enqueue"); |
| 348 | |
| 349 | function cwvpsb_iframe_delay_enqueue(){ |
| 350 | |
| 351 | global $iframe_check; |
| 352 | if ( $iframe_check == 1 ) { |
| 353 | |
| 354 | |
| 355 | wp_enqueue_script( 'cwvpsb_iframe', plugin_dir_url(__FILE__) . 'cwvpsb_iframe.js', array(), NULL); |
| 356 | wp_enqueue_style( 'cwvpsb_iframe', plugin_dir_url(__FILE__) . 'cwvpsb_iframe.css', array(), NULL); |
| 357 | |
| 358 | echo '<style>.cwvpsb_iframe {max-width:600px !important}</style>'; |
| 359 | |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | function cwvpsb_amp_support_enabled(){ |
| 364 | if(function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint()){ |
| 365 | return true; |
| 366 | }elseif(function_exists('is_amp_endpoint') && is_amp_endpoint()){ |
| 367 | return true; |
| 368 | }elseif(function_exists('get_post_type') && in_array(get_post_type(), array('web-stories', |
| 369 | 'web-story'))){ |
| 370 | return true; |
| 371 | }elseif(function_exists('get_post_type') && get_post_type()=='ampforwp_story'){ |
| 372 | return true; |
| 373 | } |
| 374 | if(function_exists('vp_metabox')){ |
| 375 | $amp_story_activated = vp_metabox('amp_story_vp_metabox.amp_story_tg'); |
| 376 | $amp_story_primary = vp_metabox('amp_story_vp_metabox.amp_story_tg_primary'); |
| 377 | if ($amp_story_primary == 1 && $amp_story_activated == 1 && !is_admin() && (is_single() || is_page() ) ) { |
| 378 | return true; |
| 379 | } |
| 380 | if (sanitize_text_field($_GET['amp'] == 1) && $amp_story_activated == 1 ) { |
| 381 | return true; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | |
| 389 |