@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | * Handles autoptimizeExtra frontend features + admin options page |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 6 | +if (!defined('ABSPATH')) { |
|
| 7 | 7 | exit; |
| 8 | 8 | } |
| 9 | 9 | |
@@ -21,9 +21,9 @@ discard block |
||
| 21 | 21 | * |
| 22 | 22 | * @param array $options Optional. Allows overriding options without having to specify them via admin options page. |
| 23 | 23 | */ |
| 24 | - public function __construct( $options = array() ) |
|
| 24 | + public function __construct($options = array()) |
|
| 25 | 25 | { |
| 26 | - if ( empty( $options ) ) { |
|
| 26 | + if (empty($options)) { |
|
| 27 | 27 | $options = self::fetch_options(); |
| 28 | 28 | } |
| 29 | 29 | |
@@ -32,9 +32,9 @@ discard block |
||
| 32 | 32 | |
| 33 | 33 | public function run() |
| 34 | 34 | { |
| 35 | - if ( is_admin() ) { |
|
| 36 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
| 37 | - add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_extra_tab' ) ); |
|
| 35 | + if (is_admin()) { |
|
| 36 | + add_action('admin_menu', array($this, 'admin_menu')); |
|
| 37 | + add_filter('autoptimize_filter_settingsscreen_tabs', array($this, 'add_extra_tab')); |
|
| 38 | 38 | } else { |
| 39 | 39 | $this->run_on_frontend(); |
| 40 | 40 | } |
@@ -42,8 +42,8 @@ discard block |
||
| 42 | 42 | |
| 43 | 43 | public static function fetch_options() |
| 44 | 44 | { |
| 45 | - $value = get_option( 'autoptimize_extra_settings' ); |
|
| 46 | - if ( empty( $value ) ) { |
|
| 45 | + $value = get_option('autoptimize_extra_settings'); |
|
| 46 | + if (empty($value)) { |
|
| 47 | 47 | // Fallback to returning defaults when no stored option exists yet. |
| 48 | 48 | $value = autoptimizeConfig::get_ao_extra_default_options(); |
| 49 | 49 | } |
@@ -54,55 +54,55 @@ discard block |
||
| 54 | 54 | public function disable_emojis() |
| 55 | 55 | { |
| 56 | 56 | // Removing all actions related to emojis! |
| 57 | - remove_action( 'admin_print_styles', 'print_emoji_styles' ); |
|
| 58 | - remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); |
|
| 59 | - remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
|
| 60 | - remove_action( 'wp_print_styles', 'print_emoji_styles' ); |
|
| 61 | - remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); |
|
| 62 | - remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); |
|
| 63 | - remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); |
|
| 57 | + remove_action('admin_print_styles', 'print_emoji_styles'); |
|
| 58 | + remove_action('wp_head', 'print_emoji_detection_script', 7); |
|
| 59 | + remove_action('admin_print_scripts', 'print_emoji_detection_script'); |
|
| 60 | + remove_action('wp_print_styles', 'print_emoji_styles'); |
|
| 61 | + remove_filter('wp_mail', 'wp_staticize_emoji_for_email'); |
|
| 62 | + remove_filter('the_content_feed', 'wp_staticize_emoji'); |
|
| 63 | + remove_filter('comment_text_rss', 'wp_staticize_emoji'); |
|
| 64 | 64 | |
| 65 | 65 | // Removes TinyMCE emojis. |
| 66 | - add_filter( 'tiny_mce_plugins', array( $this, 'filter_disable_emojis_tinymce' ) ); |
|
| 66 | + add_filter('tiny_mce_plugins', array($this, 'filter_disable_emojis_tinymce')); |
|
| 67 | 67 | |
| 68 | 68 | // Removes emoji dns-preftech. |
| 69 | - add_filter( 'wp_resource_hints', array( $this, 'filter_remove_emoji_dns_prefetch' ), 10, 2 ); |
|
| 69 | + add_filter('wp_resource_hints', array($this, 'filter_remove_emoji_dns_prefetch'), 10, 2); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - public function filter_disable_emojis_tinymce( $plugins ) |
|
| 72 | + public function filter_disable_emojis_tinymce($plugins) |
|
| 73 | 73 | { |
| 74 | - if ( is_array( $plugins ) ) { |
|
| 75 | - return array_diff( $plugins, array( 'wpemoji' ) ); |
|
| 74 | + if (is_array($plugins)) { |
|
| 75 | + return array_diff($plugins, array('wpemoji')); |
|
| 76 | 76 | } else { |
| 77 | 77 | return array(); |
| 78 | 78 | } |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - public function filter_remove_qs( $src ) |
|
| 81 | + public function filter_remove_qs($src) |
|
| 82 | 82 | { |
| 83 | - if ( strpos( $src, '?ver=' ) ) { |
|
| 84 | - $src = remove_query_arg( 'ver', $src ); |
|
| 83 | + if (strpos($src, '?ver=')) { |
|
| 84 | + $src = remove_query_arg('ver', $src); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | return $src; |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | - public function extra_async_js( $in ) |
|
| 90 | + public function extra_async_js($in) |
|
| 91 | 91 | { |
| 92 | 92 | $exclusions = array(); |
| 93 | - if ( ! empty( $in ) ) { |
|
| 94 | - $exclusions = array_fill_keys( array_filter( array_map( 'trim', explode( ',', $in ) ) ), '' ); |
|
| 93 | + if (!empty($in)) { |
|
| 94 | + $exclusions = array_fill_keys(array_filter(array_map('trim', explode(',', $in))), ''); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | $settings = $this->options['autoptimize_extra_text_field_3']; |
| 98 | - $async = array_fill_keys( array_filter( array_map( 'trim', explode( ',', $settings ) ) ), '' ); |
|
| 99 | - $attr = apply_filters( 'autoptimize_filter_extra_async', 'async' ); |
|
| 100 | - foreach ( $async as $k => $v ) { |
|
| 101 | - $async[ $k ] = $attr; |
|
| 98 | + $async = array_fill_keys(array_filter(array_map('trim', explode(',', $settings))), ''); |
|
| 99 | + $attr = apply_filters('autoptimize_filter_extra_async', 'async'); |
|
| 100 | + foreach ($async as $k => $v) { |
|
| 101 | + $async[$k] = $attr; |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | // Merge exclusions & asyncs in one array and return to AO API. |
| 105 | - $merged = array_merge( $exclusions, $async ); |
|
| 105 | + $merged = array_merge($exclusions, $async); |
|
| 106 | 106 | |
| 107 | 107 | return $merged; |
| 108 | 108 | } |
@@ -112,56 +112,56 @@ discard block |
||
| 112 | 112 | $options = $this->options; |
| 113 | 113 | |
| 114 | 114 | // Disable emojis if specified. |
| 115 | - if ( ! empty( $options['autoptimize_extra_checkbox_field_1'] ) ) { |
|
| 115 | + if (!empty($options['autoptimize_extra_checkbox_field_1'])) { |
|
| 116 | 116 | $this->disable_emojis(); |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | // Remove version query parameters. |
| 120 | - if ( ! empty( $options['autoptimize_extra_checkbox_field_0'] ) ) { |
|
| 121 | - add_filter( 'script_loader_src', array( $this, 'filter_remove_qs' ), 15, 1 ); |
|
| 122 | - add_filter( 'style_loader_src', array( $this, 'filter_remove_qs' ), 15, 1 ); |
|
| 120 | + if (!empty($options['autoptimize_extra_checkbox_field_0'])) { |
|
| 121 | + add_filter('script_loader_src', array($this, 'filter_remove_qs'), 15, 1); |
|
| 122 | + add_filter('style_loader_src', array($this, 'filter_remove_qs'), 15, 1); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Avoiding conflicts of interest when async-javascript plugin is active! |
| 126 | - $async_js_plugin_active = autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' ); |
|
| 127 | - if ( ! empty( $options['autoptimize_extra_text_field_3'] ) && ! $async_js_plugin_active ) { |
|
| 128 | - add_filter( 'autoptimize_filter_js_exclude', array( $this, 'extra_async_js' ), 10, 1 ); |
|
| 126 | + $async_js_plugin_active = autoptimizeUtils::is_plugin_active('async-javascript/async-javascript.php'); |
|
| 127 | + if (!empty($options['autoptimize_extra_text_field_3']) && !$async_js_plugin_active) { |
|
| 128 | + add_filter('autoptimize_filter_js_exclude', array($this, 'extra_async_js'), 10, 1); |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | // Optimize google fonts! |
| 132 | - if ( ! empty( $options['autoptimize_extra_radio_field_4'] ) && ( '1' !== $options['autoptimize_extra_radio_field_4'] ) ) { |
|
| 133 | - add_filter( 'wp_resource_hints', array( $this, 'filter_remove_gfonts_dnsprefetch' ), 10, 2 ); |
|
| 134 | - add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_optimize_google_fonts' ), 10, 1 ); |
|
| 135 | - add_filter( 'autoptimize_extra_filter_tobepreconn', array( $this, 'filter_preconnect_google_fonts' ), 10, 1 ); |
|
| 132 | + if (!empty($options['autoptimize_extra_radio_field_4']) && ('1' !== $options['autoptimize_extra_radio_field_4'])) { |
|
| 133 | + add_filter('wp_resource_hints', array($this, 'filter_remove_gfonts_dnsprefetch'), 10, 2); |
|
| 134 | + add_filter('autoptimize_html_after_minify', array($this, 'filter_optimize_google_fonts'), 10, 1); |
|
| 135 | + add_filter('autoptimize_extra_filter_tobepreconn', array($this, 'filter_preconnect_google_fonts'), 10, 1); |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | // Preconnect! |
| 139 | - if ( ! empty( $options['autoptimize_extra_text_field_2'] ) || has_filter( 'autoptimize_extra_filter_tobepreconn' ) ) { |
|
| 140 | - add_filter( 'wp_resource_hints', array( $this, 'filter_preconnect' ), 10, 2 ); |
|
| 139 | + if (!empty($options['autoptimize_extra_text_field_2']) || has_filter('autoptimize_extra_filter_tobepreconn')) { |
|
| 140 | + add_filter('wp_resource_hints', array($this, 'filter_preconnect'), 10, 2); |
|
| 141 | 141 | } |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | - public function filter_remove_emoji_dns_prefetch( $urls, $relation_type ) |
|
| 144 | + public function filter_remove_emoji_dns_prefetch($urls, $relation_type) |
|
| 145 | 145 | { |
| 146 | - $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/' ); |
|
| 146 | + $emoji_svg_url = apply_filters('emoji_svg_url', 'https://s.w.org/images/core/emoji/'); |
|
| 147 | 147 | |
| 148 | - return $this->filter_remove_dns_prefetch( $urls, $relation_type, $emoji_svg_url ); |
|
| 148 | + return $this->filter_remove_dns_prefetch($urls, $relation_type, $emoji_svg_url); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | - public function filter_remove_gfonts_dnsprefetch( $urls, $relation_type ) |
|
| 151 | + public function filter_remove_gfonts_dnsprefetch($urls, $relation_type) |
|
| 152 | 152 | { |
| 153 | - return $this->filter_remove_dns_prefetch( $urls, $relation_type, 'fonts.googleapis.com' ); |
|
| 153 | + return $this->filter_remove_dns_prefetch($urls, $relation_type, 'fonts.googleapis.com'); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - public function filter_remove_dns_prefetch( $urls, $relation_type, $url_to_remove ) |
|
| 156 | + public function filter_remove_dns_prefetch($urls, $relation_type, $url_to_remove) |
|
| 157 | 157 | { |
| 158 | 158 | $url_to_remove = (string) $url_to_remove; |
| 159 | 159 | |
| 160 | - if ( ! empty( $url_to_remove ) && 'dns-prefetch' === $relation_type ) { |
|
| 160 | + if (!empty($url_to_remove) && 'dns-prefetch' === $relation_type) { |
|
| 161 | 161 | $cnt = 0; |
| 162 | - foreach ( $urls as $url ) { |
|
| 163 | - if ( false !== strpos( $url, $url_to_remove ) ) { |
|
| 164 | - unset( $urls[ $cnt ] ); |
|
| 162 | + foreach ($urls as $url) { |
|
| 163 | + if (false !== strpos($url, $url_to_remove)) { |
|
| 164 | + unset($urls[$cnt]); |
|
| 165 | 165 | } |
| 166 | 166 | $cnt++; |
| 167 | 167 | } |
@@ -170,131 +170,131 @@ discard block |
||
| 170 | 170 | return $urls; |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - public function filter_optimize_google_fonts( $in ) |
|
| 173 | + public function filter_optimize_google_fonts($in) |
|
| 174 | 174 | { |
| 175 | 175 | // Extract fonts, partly based on wp rocket's extraction code. |
| 176 | - $markup = preg_replace( '/<!--(.*)-->/Uis', '', $in ); |
|
| 177 | - preg_match_all( '#<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])((?:https?:)?\/\/fonts\.googleapis\.com\/css(?:(?!\1).)+)\1)(?:\s+[^>]*)?>#iU', $markup, $matches ); |
|
| 176 | + $markup = preg_replace('/<!--(.*)-->/Uis', '', $in); |
|
| 177 | + preg_match_all('#<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])((?:https?:)?\/\/fonts\.googleapis\.com\/css(?:(?!\1).)+)\1)(?:\s+[^>]*)?>#iU', $markup, $matches); |
|
| 178 | 178 | |
| 179 | 179 | $fonts_collection = array(); |
| 180 | - if ( ! $matches[2] ) { |
|
| 180 | + if (!$matches[2]) { |
|
| 181 | 181 | return $in; |
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | // Store them in $fonts array. |
| 185 | 185 | $i = 0; |
| 186 | - foreach ( $matches[2] as $font ) { |
|
| 187 | - if ( ! preg_match( '/rel=["\']dns-prefetch["\']/', $matches[0][ $i ] ) ) { |
|
| 186 | + foreach ($matches[2] as $font) { |
|
| 187 | + if (!preg_match('/rel=["\']dns-prefetch["\']/', $matches[0][$i])) { |
|
| 188 | 188 | // Get fonts name. |
| 189 | - $font = str_replace( array( '%7C', '%7c' ), '|', $font ); |
|
| 190 | - $font = explode( 'family=', $font ); |
|
| 191 | - $font = ( isset( $font[1] ) ) ? explode( '&', $font[1] ) : array(); |
|
| 189 | + $font = str_replace(array('%7C', '%7c'), '|', $font); |
|
| 190 | + $font = explode('family=', $font); |
|
| 191 | + $font = (isset($font[1])) ? explode('&', $font[1]) : array(); |
|
| 192 | 192 | // Add font to $fonts[$i] but make sure not to pollute with an empty family! |
| 193 | - $_thisfont = array_values( array_filter( explode( '|', reset( $font ) ) ) ); |
|
| 194 | - if ( ! empty( $_thisfont ) ) { |
|
| 195 | - $fonts_collection[ $i ]['fonts'] = $_thisfont; |
|
| 193 | + $_thisfont = array_values(array_filter(explode('|', reset($font)))); |
|
| 194 | + if (!empty($_thisfont)) { |
|
| 195 | + $fonts_collection[$i]['fonts'] = $_thisfont; |
|
| 196 | 196 | // And add subset if any! |
| 197 | - $subset = ( is_array( $font ) ) ? end( $font ) : ''; |
|
| 198 | - if ( false !== strpos( $subset, 'subset=' ) ) { |
|
| 199 | - $subset = str_replace( array( '%2C', '%2c' ), ',', $subset ); |
|
| 200 | - $subset = explode( 'subset=', $subset ); |
|
| 201 | - $fonts_collection[ $i ]['subsets'] = explode( ',', $subset[1] ); |
|
| 197 | + $subset = (is_array($font)) ? end($font) : ''; |
|
| 198 | + if (false !== strpos($subset, 'subset=')) { |
|
| 199 | + $subset = str_replace(array('%2C', '%2c'), ',', $subset); |
|
| 200 | + $subset = explode('subset=', $subset); |
|
| 201 | + $fonts_collection[$i]['subsets'] = explode(',', $subset[1]); |
|
| 202 | 202 | } |
| 203 | 203 | } |
| 204 | 204 | // And remove Google Fonts. |
| 205 | - $in = str_replace( $matches[0][ $i ], '', $in ); |
|
| 205 | + $in = str_replace($matches[0][$i], '', $in); |
|
| 206 | 206 | } |
| 207 | 207 | $i++; |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | $options = $this->options; |
| 211 | 211 | $fonts_markup = ''; |
| 212 | - if ( '2' === $options['autoptimize_extra_radio_field_4'] ) { |
|
| 212 | + if ('2' === $options['autoptimize_extra_radio_field_4']) { |
|
| 213 | 213 | // Remove Google Fonts. |
| 214 | - unset( $fonts_collection ); |
|
| 214 | + unset($fonts_collection); |
|
| 215 | 215 | return $in; |
| 216 | - } elseif ( '3' === $options['autoptimize_extra_radio_field_4'] || '5' === $options['autoptimize_extra_radio_field_4'] ) { |
|
| 216 | + } elseif ('3' === $options['autoptimize_extra_radio_field_4'] || '5' === $options['autoptimize_extra_radio_field_4']) { |
|
| 217 | 217 | // Aggregate & link! |
| 218 | 218 | $fonts_string = ''; |
| 219 | 219 | $subset_string = ''; |
| 220 | - foreach ( $fonts_collection as $font ) { |
|
| 221 | - $fonts_string .= '|' . trim( implode( '|', $font['fonts'] ), '|' ); |
|
| 222 | - if ( ! empty( $font['subsets'] ) ) { |
|
| 223 | - $subset_string .= ',' . trim( implode( ',', $font['subsets'] ), ',' ); |
|
| 220 | + foreach ($fonts_collection as $font) { |
|
| 221 | + $fonts_string .= '|'.trim(implode('|', $font['fonts']), '|'); |
|
| 222 | + if (!empty($font['subsets'])) { |
|
| 223 | + $subset_string .= ','.trim(implode(',', $font['subsets']), ','); |
|
| 224 | 224 | } |
| 225 | 225 | } |
| 226 | 226 | |
| 227 | - if ( ! empty( $subset_string ) ) { |
|
| 228 | - $subset_string = str_replace( ',', '%2C', ltrim( $subset_string, ',' ) ); |
|
| 229 | - $fonts_string = $fonts_string . '&subset=' . $subset_string; |
|
| 227 | + if (!empty($subset_string)) { |
|
| 228 | + $subset_string = str_replace(',', '%2C', ltrim($subset_string, ',')); |
|
| 229 | + $fonts_string = $fonts_string.'&subset='.$subset_string; |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | - $fonts_string = str_replace( '|', '%7C', ltrim( $fonts_string, '|' ) ); |
|
| 232 | + $fonts_string = str_replace('|', '%7C', ltrim($fonts_string, '|')); |
|
| 233 | 233 | |
| 234 | - if ( ! empty( $fonts_string ) ) { |
|
| 235 | - if ( '5' === $options['autoptimize_extra_radio_field_4'] ) { |
|
| 236 | - $rel_string = 'rel="preload" as="style" onload="' . autoptimizeConfig::get_ao_css_preload_onload() . '"'; |
|
| 234 | + if (!empty($fonts_string)) { |
|
| 235 | + if ('5' === $options['autoptimize_extra_radio_field_4']) { |
|
| 236 | + $rel_string = 'rel="preload" as="style" onload="'.autoptimizeConfig::get_ao_css_preload_onload().'"'; |
|
| 237 | 237 | } else { |
| 238 | 238 | $rel_string = 'rel="stylesheet"'; |
| 239 | 239 | } |
| 240 | - $fonts_markup = '<link ' . $rel_string . ' id="ao_optimized_gfonts" href="https://fonts.googleapis.com/css?family=' . $fonts_string . '" />'; |
|
| 240 | + $fonts_markup = '<link '.$rel_string.' id="ao_optimized_gfonts" href="https://fonts.googleapis.com/css?family='.$fonts_string.'" />'; |
|
| 241 | 241 | } |
| 242 | - } elseif ( '4' === $options['autoptimize_extra_radio_field_4'] ) { |
|
| 242 | + } elseif ('4' === $options['autoptimize_extra_radio_field_4']) { |
|
| 243 | 243 | // Aggregate & load async (webfont.js impl.)! |
| 244 | 244 | $fonts_array = array(); |
| 245 | - foreach ( $fonts_collection as $_fonts ) { |
|
| 246 | - if ( ! empty( $_fonts['subsets'] ) ) { |
|
| 247 | - $_subset = implode( ',', $_fonts['subsets'] ); |
|
| 248 | - foreach ( $_fonts['fonts'] as $key => $_one_font ) { |
|
| 249 | - $_one_font = $_one_font . ':' . $_subset; |
|
| 250 | - $_fonts['fonts'][ $key ] = $_one_font; |
|
| 245 | + foreach ($fonts_collection as $_fonts) { |
|
| 246 | + if (!empty($_fonts['subsets'])) { |
|
| 247 | + $_subset = implode(',', $_fonts['subsets']); |
|
| 248 | + foreach ($_fonts['fonts'] as $key => $_one_font) { |
|
| 249 | + $_one_font = $_one_font.':'.$_subset; |
|
| 250 | + $_fonts['fonts'][$key] = $_one_font; |
|
| 251 | 251 | } |
| 252 | 252 | } |
| 253 | - $fonts_array = array_merge( $fonts_array, $_fonts['fonts'] ); |
|
| 253 | + $fonts_array = array_merge($fonts_array, $_fonts['fonts']); |
|
| 254 | 254 | } |
| 255 | 255 | |
| 256 | - $fonts_array = array_map( 'urldecode', $fonts_array ); |
|
| 257 | - $fonts_markup = '<script data-cfasync="false" id="ao_optimized_gfonts_config" type="text/javascript">WebFontConfig={google:{families:' . wp_json_encode( $fonts_array ) . ' },classes:false, events:false, timeout:1500};</script>'; |
|
| 256 | + $fonts_array = array_map('urldecode', $fonts_array); |
|
| 257 | + $fonts_markup = '<script data-cfasync="false" id="ao_optimized_gfonts_config" type="text/javascript">WebFontConfig={google:{families:'.wp_json_encode($fonts_array).' },classes:false, events:false, timeout:1500};</script>'; |
|
| 258 | 258 | $fonts_library_markup = '<script data-cfasync="false" id="ao_optimized_gfonts_webfontloader" type="text/javascript">(function() {var wf = document.createElement(\'script\');wf.src=\'https://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js\';wf.type=\'text/javascript\';wf.async=\'true\';var s=document.getElementsByTagName(\'script\')[0];s.parentNode.insertBefore(wf, s);})();</script>'; |
| 259 | - $in = substr_replace( $in, $fonts_library_markup . '</head>', strpos( $in, '</head>' ), strlen( '</head>' ) ); |
|
| 259 | + $in = substr_replace($in, $fonts_library_markup.'</head>', strpos($in, '</head>'), strlen('</head>')); |
|
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | // Replace back in markup. |
| 263 | - $inject_point = apply_filters( 'autoptimize_filter_extra_gfont_injectpoint', '<link' ); |
|
| 264 | - $out = substr_replace( $in, $fonts_markup . $inject_point, strpos( $in, $inject_point ), strlen( $inject_point ) ); |
|
| 265 | - unset( $fonts_collection ); |
|
| 263 | + $inject_point = apply_filters('autoptimize_filter_extra_gfont_injectpoint', '<link'); |
|
| 264 | + $out = substr_replace($in, $fonts_markup.$inject_point, strpos($in, $inject_point), strlen($inject_point)); |
|
| 265 | + unset($fonts_collection); |
|
| 266 | 266 | |
| 267 | 267 | // and insert preload polyfill if "link preload" and if the polyfill isn't there yet (courtesy of inline&defer). |
| 268 | 268 | $preload_polyfill = autoptimizeConfig::get_ao_css_preload_polyfill(); |
| 269 | - if ( '5' === $options['autoptimize_extra_radio_field_4'] && strpos( $out, $preload_polyfill ) === false ) { |
|
| 270 | - $out = str_replace( '</body>', $preload_polyfill . '</body>', $out ); |
|
| 269 | + if ('5' === $options['autoptimize_extra_radio_field_4'] && strpos($out, $preload_polyfill) === false) { |
|
| 270 | + $out = str_replace('</body>', $preload_polyfill.'</body>', $out); |
|
| 271 | 271 | } |
| 272 | 272 | return $out; |
| 273 | 273 | } |
| 274 | 274 | |
| 275 | - public function filter_preconnect( $hints, $relation_type ) |
|
| 275 | + public function filter_preconnect($hints, $relation_type) |
|
| 276 | 276 | { |
| 277 | 277 | $options = $this->options; |
| 278 | 278 | |
| 279 | 279 | // Get settings and store in array. |
| 280 | - $preconns = array_filter( array_map( 'trim', explode( ',', $options['autoptimize_extra_text_field_2'] ) ) ); |
|
| 281 | - $preconns = apply_filters( 'autoptimize_extra_filter_tobepreconn', $preconns ); |
|
| 280 | + $preconns = array_filter(array_map('trim', explode(',', $options['autoptimize_extra_text_field_2']))); |
|
| 281 | + $preconns = apply_filters('autoptimize_extra_filter_tobepreconn', $preconns); |
|
| 282 | 282 | |
| 283 | 283 | // Walk array, extract domain and add to new array with crossorigin attribute. |
| 284 | - foreach ( $preconns as $preconn ) { |
|
| 285 | - $parsed = parse_url( $preconn ); |
|
| 286 | - if ( is_array( $parsed ) && empty( $parsed['scheme'] ) ) { |
|
| 287 | - $domain = '//' . $parsed['host']; |
|
| 288 | - } elseif ( is_array( $parsed ) ) { |
|
| 289 | - $domain = $parsed['scheme'] . '://' . $parsed['host']; |
|
| 284 | + foreach ($preconns as $preconn) { |
|
| 285 | + $parsed = parse_url($preconn); |
|
| 286 | + if (is_array($parsed) && empty($parsed['scheme'])) { |
|
| 287 | + $domain = '//'.$parsed['host']; |
|
| 288 | + } elseif (is_array($parsed)) { |
|
| 289 | + $domain = $parsed['scheme'].'://'.$parsed['host']; |
|
| 290 | 290 | } |
| 291 | 291 | |
| 292 | - if ( ! empty( $domain ) ) { |
|
| 293 | - $hint = array( 'href' => $domain ); |
|
| 292 | + if (!empty($domain)) { |
|
| 293 | + $hint = array('href' => $domain); |
|
| 294 | 294 | // Fonts don't get preconnected unless crossorigin flag is set, non-fonts don't get preconnected if origin flag is set |
| 295 | 295 | // so hardcode fonts.gstatic.com to come with crossorigin and have filter to add other domains if needed. |
| 296 | - $crossorigins = apply_filters( 'autoptimize_extra_filter_preconn_crossorigin', array( 'https://fonts.gstatic.com' ) ); |
|
| 297 | - if ( in_array( $domain, $crossorigins ) ) { |
|
| 296 | + $crossorigins = apply_filters('autoptimize_extra_filter_preconn_crossorigin', array('https://fonts.gstatic.com')); |
|
| 297 | + if (in_array($domain, $crossorigins)) { |
|
| 298 | 298 | $hint['crossorigin'] = 'anonymous'; |
| 299 | 299 | } |
| 300 | 300 | $new_hints[] = $hint; |
@@ -302,21 +302,21 @@ discard block |
||
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | // Merge in WP's preconnect hints. |
| 305 | - if ( 'preconnect' === $relation_type && ! empty( $new_hints ) ) { |
|
| 306 | - $hints = array_merge( $hints, $new_hints ); |
|
| 305 | + if ('preconnect' === $relation_type && !empty($new_hints)) { |
|
| 306 | + $hints = array_merge($hints, $new_hints); |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | return $hints; |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | - public function filter_preconnect_google_fonts( $in ) |
|
| 312 | + public function filter_preconnect_google_fonts($in) |
|
| 313 | 313 | { |
| 314 | - if ( '2' !== $this->options['autoptimize_extra_radio_field_4'] ) { |
|
| 314 | + if ('2' !== $this->options['autoptimize_extra_radio_field_4']) { |
|
| 315 | 315 | // Preconnect to fonts.gstatic.com unless we remove gfonts. |
| 316 | 316 | $in[] = 'https://fonts.gstatic.com'; |
| 317 | 317 | } |
| 318 | 318 | |
| 319 | - if ( '4' === $this->options['autoptimize_extra_radio_field_4'] ) { |
|
| 319 | + if ('4' === $this->options['autoptimize_extra_radio_field_4']) { |
|
| 320 | 320 | // Preconnect even more hosts for webfont.js! |
| 321 | 321 | $in[] = 'https://ajax.googleapis.com'; |
| 322 | 322 | $in[] = 'https://fonts.googleapis.com'; |
@@ -333,14 +333,14 @@ discard block |
||
| 333 | 333 | 'autoptimize_extra', |
| 334 | 334 | 'manage_options', |
| 335 | 335 | 'autoptimize_extra', |
| 336 | - array( $this, 'options_page' ) |
|
| 336 | + array($this, 'options_page') |
|
| 337 | 337 | ); |
| 338 | - register_setting( 'autoptimize_extra_settings', 'autoptimize_extra_settings' ); |
|
| 338 | + register_setting('autoptimize_extra_settings', 'autoptimize_extra_settings'); |
|
| 339 | 339 | } |
| 340 | 340 | |
| 341 | - public function add_extra_tab( $in ) |
|
| 341 | + public function add_extra_tab($in) |
|
| 342 | 342 | { |
| 343 | - $in = array_merge( $in, array( 'autoptimize_extra' => __( 'Extra', 'autoptimize' ) ) ); |
|
| 343 | + $in = array_merge($in, array('autoptimize_extra' => __('Extra', 'autoptimize'))); |
|
| 344 | 344 | |
| 345 | 345 | return $in; |
| 346 | 346 | } |
@@ -360,87 +360,87 @@ discard block |
||
| 360 | 360 | #autoptimize_extra_descr{font-size: 120%;} |
| 361 | 361 | </style> |
| 362 | 362 | <div class="wrap"> |
| 363 | - <h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1> |
|
| 363 | + <h1><?php _e('Autoptimize Settings', 'autoptimize'); ?></h1> |
|
| 364 | 364 | <?php echo autoptimizeConfig::ao_admin_tabs(); ?> |
| 365 | - <?php if ( 'on' !== get_option( 'autoptimize_js' ) && 'on' !== get_option( 'autoptimize_css' ) && 'on' !== get_option( 'autoptimize_html' ) && ! autoptimizeImages::imgopt_active() ) { ?> |
|
| 365 | + <?php if ('on' !== get_option('autoptimize_js') && 'on' !== get_option('autoptimize_css') && 'on' !== get_option('autoptimize_html') && !autoptimizeImages::imgopt_active()) { ?> |
|
| 366 | 366 | <div class="notice-warning notice"><p> |
| 367 | - <?php _e( 'Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize' ); ?> |
|
| 367 | + <?php _e('Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize'); ?> |
|
| 368 | 368 | </p></div> |
| 369 | 369 | <?php } ?> |
| 370 | 370 | |
| 371 | 371 | <form id='ao_settings_form' action='options.php' method='post'> |
| 372 | - <?php settings_fields( 'autoptimize_extra_settings' ); ?> |
|
| 373 | - <h2><?php _e( 'Extra Auto-Optimizations', 'autoptimize' ); ?></h2> |
|
| 374 | - <span id='autoptimize_extra_descr'><?php _e( 'The following settings can improve your site\'s performance even more.', 'autoptimize' ); ?></span> |
|
| 372 | + <?php settings_fields('autoptimize_extra_settings'); ?> |
|
| 373 | + <h2><?php _e('Extra Auto-Optimizations', 'autoptimize'); ?></h2> |
|
| 374 | + <span id='autoptimize_extra_descr'><?php _e('The following settings can improve your site\'s performance even more.', 'autoptimize'); ?></span> |
|
| 375 | 375 | <table class="form-table"> |
| 376 | 376 | <tr> |
| 377 | - <th scope="row"><?php _e( 'Google Fonts', 'autoptimize' ); ?></th> |
|
| 377 | + <th scope="row"><?php _e('Google Fonts', 'autoptimize'); ?></th> |
|
| 378 | 378 | <td> |
| 379 | - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="1" <?php if ( ! in_array( $gfonts, array( 2, 3, 4, 5 ) ) ) { echo 'checked'; } ?> ><?php _e( 'Leave as is', 'autoptimize' ); ?><br/> |
|
| 380 | - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="2" <?php checked( 2, $gfonts, true ); ?> ><?php _e( 'Remove Google Fonts', 'autoptimize' ); ?><br/> |
|
| 381 | - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="3" <?php checked( 3, $gfonts, true ); ?> ><?php _e( 'Combine and link in head (fonts load fast but are render-blocking)', 'autoptimize' ); ?><br/> |
|
| 382 | - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="5" <?php checked( 5, $gfonts, true ); ?> ><?php _e( 'Combine and preload in head (fonts load late, but are not render-blocking)', 'autoptimize' ); ?><br/> |
|
| 383 | - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="4" <?php checked( 4, $gfonts, true ); ?> ><?php _e( 'Combine and load fonts asynchronously with <a href="https://github.com/typekit/webfontloader#readme" target="_blank">webfont.js</a>', 'autoptimize' ); ?><br/> |
|
| 379 | + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="1" <?php if (!in_array($gfonts, array(2, 3, 4, 5))) { echo 'checked'; } ?> ><?php _e('Leave as is', 'autoptimize'); ?><br/> |
|
| 380 | + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="2" <?php checked(2, $gfonts, true); ?> ><?php _e('Remove Google Fonts', 'autoptimize'); ?><br/> |
|
| 381 | + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="3" <?php checked(3, $gfonts, true); ?> ><?php _e('Combine and link in head (fonts load fast but are render-blocking)', 'autoptimize'); ?><br/> |
|
| 382 | + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="5" <?php checked(5, $gfonts, true); ?> ><?php _e('Combine and preload in head (fonts load late, but are not render-blocking)', 'autoptimize'); ?><br/> |
|
| 383 | + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="4" <?php checked(4, $gfonts, true); ?> ><?php _e('Combine and load fonts asynchronously with <a href="https://github.com/typekit/webfontloader#readme" target="_blank">webfont.js</a>', 'autoptimize'); ?><br/> |
|
| 384 | 384 | </td> |
| 385 | 385 | </tr> |
| 386 | 386 | <tr> |
| 387 | - <th scope="row"><?php _e( 'Remove emojis', 'autoptimize' ); ?></th> |
|
| 387 | + <th scope="row"><?php _e('Remove emojis', 'autoptimize'); ?></th> |
|
| 388 | 388 | <td> |
| 389 | - <label><input type='checkbox' name='autoptimize_extra_settings[autoptimize_extra_checkbox_field_1]' <?php if ( ! empty( $options['autoptimize_extra_checkbox_field_1'] ) && '1' === $options['autoptimize_extra_checkbox_field_1'] ) { echo 'checked="checked"'; } ?> value='1'><?php _e( 'Removes WordPress\' core emojis\' inline CSS, inline JavaScript, and an otherwise un-autoptimized JavaScript file.', 'autoptimize' ); ?></label> |
|
| 389 | + <label><input type='checkbox' name='autoptimize_extra_settings[autoptimize_extra_checkbox_field_1]' <?php if (!empty($options['autoptimize_extra_checkbox_field_1']) && '1' === $options['autoptimize_extra_checkbox_field_1']) { echo 'checked="checked"'; } ?> value='1'><?php _e('Removes WordPress\' core emojis\' inline CSS, inline JavaScript, and an otherwise un-autoptimized JavaScript file.', 'autoptimize'); ?></label> |
|
| 390 | 390 | </td> |
| 391 | 391 | </tr> |
| 392 | 392 | <tr> |
| 393 | - <th scope="row"><?php _e( 'Remove query strings from static resources', 'autoptimize' ); ?></th> |
|
| 393 | + <th scope="row"><?php _e('Remove query strings from static resources', 'autoptimize'); ?></th> |
|
| 394 | 394 | <td> |
| 395 | - <label><input type='checkbox' name='autoptimize_extra_settings[autoptimize_extra_checkbox_field_0]' <?php if ( ! empty( $options['autoptimize_extra_checkbox_field_0'] ) && '1' === $options['autoptimize_extra_checkbox_field_0'] ) { echo 'checked="checked"'; } ?> value='1'><?php _e( 'Removing query strings (or more specifically the <code>ver</code> parameter) will not improve load time, but might improve performance scores.', 'autoptimize' ); ?></label> |
|
| 395 | + <label><input type='checkbox' name='autoptimize_extra_settings[autoptimize_extra_checkbox_field_0]' <?php if (!empty($options['autoptimize_extra_checkbox_field_0']) && '1' === $options['autoptimize_extra_checkbox_field_0']) { echo 'checked="checked"'; } ?> value='1'><?php _e('Removing query strings (or more specifically the <code>ver</code> parameter) will not improve load time, but might improve performance scores.', 'autoptimize'); ?></label> |
|
| 396 | 396 | </td> |
| 397 | 397 | </tr> |
| 398 | 398 | <tr> |
| 399 | - <th scope="row"><?php _e( 'Preconnect to 3rd party domains <em>(advanced users)</em>', 'autoptimize' ); ?></th> |
|
| 399 | + <th scope="row"><?php _e('Preconnect to 3rd party domains <em>(advanced users)</em>', 'autoptimize'); ?></th> |
|
| 400 | 400 | <td> |
| 401 | - <label><input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_2]' value='<?php if ( array_key_exists( 'autoptimize_extra_text_field_2', $options ) ) { echo esc_attr( $options['autoptimize_extra_text_field_2'] ); } ?>'><br /><?php _e( 'Add 3rd party domains you want the browser to <a href="https://www.keycdn.com/support/preconnect/#primary" target="_blank">preconnect</a> to, separated by comma\'s. Make sure to include the correct protocol (HTTP or HTTPS).', 'autoptimize' ); ?></label> |
|
| 401 | + <label><input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_2]' value='<?php if (array_key_exists('autoptimize_extra_text_field_2', $options)) { echo esc_attr($options['autoptimize_extra_text_field_2']); } ?>'><br /><?php _e('Add 3rd party domains you want the browser to <a href="https://www.keycdn.com/support/preconnect/#primary" target="_blank">preconnect</a> to, separated by comma\'s. Make sure to include the correct protocol (HTTP or HTTPS).', 'autoptimize'); ?></label> |
|
| 402 | 402 | </td> |
| 403 | 403 | </tr> |
| 404 | 404 | <tr> |
| 405 | - <th scope="row"><?php _e( 'Async Javascript-files <em>(advanced users)</em>', 'autoptimize' ); ?></th> |
|
| 405 | + <th scope="row"><?php _e('Async Javascript-files <em>(advanced users)</em>', 'autoptimize'); ?></th> |
|
| 406 | 406 | <td> |
| 407 | 407 | <?php |
| 408 | - if ( autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' ) ) { |
|
| 408 | + if (autoptimizeUtils::is_plugin_active('async-javascript/async-javascript.php')) { |
|
| 409 | 409 | // translators: link points Async Javascript settings page. |
| 410 | - printf( __( 'You have "Async JavaScript" installed, %1$sconfiguration of async javascript is best done there%2$s.', 'autoptimize' ), '<a href="' . 'options-general.php?page=async-javascript' . '">', '</a>' ); |
|
| 410 | + printf(__('You have "Async JavaScript" installed, %1$sconfiguration of async javascript is best done there%2$s.', 'autoptimize'), '<a href="'.'options-general.php?page=async-javascript'.'">', '</a>'); |
|
| 411 | 411 | } else { |
| 412 | 412 | ?> |
| 413 | - <input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_3]' value='<?php if ( array_key_exists( 'autoptimize_extra_text_field_3', $options ) ) { echo esc_attr( $options['autoptimize_extra_text_field_3'] ); } ?>'> |
|
| 413 | + <input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_3]' value='<?php if (array_key_exists('autoptimize_extra_text_field_3', $options)) { echo esc_attr($options['autoptimize_extra_text_field_3']); } ?>'> |
|
| 414 | 414 | <br /> |
| 415 | 415 | <?php |
| 416 | - _e( 'Comma-separated list of local or 3rd party JS-files that should loaded with the <code>async</code> flag. JS-files from your own site will be automatically excluded if added here. ', 'autoptimize' ); |
|
| 416 | + _e('Comma-separated list of local or 3rd party JS-files that should loaded with the <code>async</code> flag. JS-files from your own site will be automatically excluded if added here. ', 'autoptimize'); |
|
| 417 | 417 | // translators: %s will be replaced by a link to the "async javascript" plugin. |
| 418 | - echo sprintf( __( 'Configuration of async javascript is easier and more flexible using the %s plugin.', 'autoptimize' ), '"<a href="https://wordpress.org/plugins/async-javascript" target="_blank">Async Javascript</a>"' ); |
|
| 419 | - $asj_install_url = network_admin_url() . 'plugin-install.php?s=async+javascript&tab=search&type=term'; |
|
| 420 | - echo sprintf( ' <a href="' . $asj_install_url . '">%s</a>', __( 'Click here to install and activate it.', 'autoptimize' ) ); |
|
| 418 | + echo sprintf(__('Configuration of async javascript is easier and more flexible using the %s plugin.', 'autoptimize'), '"<a href="https://wordpress.org/plugins/async-javascript" target="_blank">Async Javascript</a>"'); |
|
| 419 | + $asj_install_url = network_admin_url().'plugin-install.php?s=async+javascript&tab=search&type=term'; |
|
| 420 | + echo sprintf(' <a href="'.$asj_install_url.'">%s</a>', __('Click here to install and activate it.', 'autoptimize')); |
|
| 421 | 421 | } |
| 422 | 422 | ?> |
| 423 | 423 | </td> |
| 424 | 424 | </tr> |
| 425 | 425 | <tr> |
| 426 | - <th scope="row"><?php _e( 'Optimize YouTube videos', 'autoptimize' ); ?></th> |
|
| 426 | + <th scope="row"><?php _e('Optimize YouTube videos', 'autoptimize'); ?></th> |
|
| 427 | 427 | <td> |
| 428 | 428 | <?php |
| 429 | - if ( autoptimizeUtils::is_plugin_active( 'wp-youtube-lyte/wp-youtube-lyte.php' ) ) { |
|
| 430 | - _e( 'Great, you have WP YouTube Lyte installed.', 'autoptimize' ); |
|
| 429 | + if (autoptimizeUtils::is_plugin_active('wp-youtube-lyte/wp-youtube-lyte.php')) { |
|
| 430 | + _e('Great, you have WP YouTube Lyte installed.', 'autoptimize'); |
|
| 431 | 431 | $lyte_config_url = 'options-general.php?page=lyte_settings_page'; |
| 432 | - echo sprintf( ' <a href="' . $lyte_config_url . '">%s</a>', __( 'Click here to configure it.', 'autoptimize' ) ); |
|
| 432 | + echo sprintf(' <a href="'.$lyte_config_url.'">%s</a>', __('Click here to configure it.', 'autoptimize')); |
|
| 433 | 433 | } else { |
| 434 | 434 | // translators: %s will be replaced by a link to "wp youtube lyte" plugin. |
| 435 | - echo sprintf( __( '%s allows you to “lazy load” your videos, by inserting responsive “Lite YouTube Embeds". ', 'autoptimize' ), '<a href="https://wordpress.org/plugins/wp-youtube-lyte" target="_blank">WP YouTube Lyte</a>' ); |
|
| 436 | - $lyte_install_url = network_admin_url() . 'plugin-install.php?s=lyte&tab=search&type=term'; |
|
| 437 | - echo sprintf( ' <a href="' . $lyte_install_url . '">%s</a>', __( 'Click here to install and activate it.', 'autoptimize' ) ); |
|
| 435 | + echo sprintf(__('%s allows you to “lazy load” your videos, by inserting responsive “Lite YouTube Embeds". ', 'autoptimize'), '<a href="https://wordpress.org/plugins/wp-youtube-lyte" target="_blank">WP YouTube Lyte</a>'); |
|
| 436 | + $lyte_install_url = network_admin_url().'plugin-install.php?s=lyte&tab=search&type=term'; |
|
| 437 | + echo sprintf(' <a href="'.$lyte_install_url.'">%s</a>', __('Click here to install and activate it.', 'autoptimize')); |
|
| 438 | 438 | } |
| 439 | 439 | ?> |
| 440 | 440 | </td> |
| 441 | 441 | </tr> |
| 442 | 442 | </table> |
| 443 | - <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e( 'Save Changes', 'autoptimize' ); ?>" /></p> |
|
| 443 | + <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e('Save Changes', 'autoptimize'); ?>" /></p> |
|
| 444 | 444 | </form> |
| 445 | 445 | <?php |
| 446 | 446 | } |
@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | * Handles optimizing images. |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 6 | +if (!defined('ABSPATH')) { |
|
| 7 | 7 | exit; |
| 8 | 8 | } |
| 9 | 9 | |
@@ -23,17 +23,17 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | protected static $instance = null; |
| 25 | 25 | |
| 26 | - public function __construct( array $options = array() ) |
|
| 26 | + public function __construct(array $options = array()) |
|
| 27 | 27 | { |
| 28 | 28 | // If options are not provided, fetch them. |
| 29 | - if ( empty( $options ) ) { |
|
| 29 | + if (empty($options)) { |
|
| 30 | 30 | $options = $this->fetch_options(); |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - $this->set_options( $options ); |
|
| 33 | + $this->set_options($options); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - public function set_options( array $options ) |
|
| 36 | + public function set_options(array $options) |
|
| 37 | 37 | { |
| 38 | 38 | $this->options = $options; |
| 39 | 39 | |
@@ -42,17 +42,17 @@ discard block |
||
| 42 | 42 | |
| 43 | 43 | public static function fetch_options() |
| 44 | 44 | { |
| 45 | - $value = get_option( 'autoptimize_imgopt_settings' ); |
|
| 46 | - if ( empty( $value ) ) { |
|
| 45 | + $value = get_option('autoptimize_imgopt_settings'); |
|
| 46 | + if (empty($value)) { |
|
| 47 | 47 | // Fallback to returning defaults when no stored option exists yet. |
| 48 | 48 | $value = autoptimizeConfig::get_ao_imgopt_default_options(); |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | // get service availability and add it to the options-array. |
| 52 | - $value['availabilities'] = get_option( 'autoptimize_service_availablity' ); |
|
| 52 | + $value['availabilities'] = get_option('autoptimize_service_availablity'); |
|
| 53 | 53 | |
| 54 | - if ( empty( $value['availabilities'] ) ) { |
|
| 55 | - $value['availabilities'] = autoptimizeUtils::check_service_availability( true ); |
|
| 54 | + if (empty($value['availabilities'])) { |
|
| 55 | + $value['availabilities'] = autoptimizeUtils::check_service_availability(true); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | return $value; |
@@ -65,9 +65,9 @@ discard block |
||
| 65 | 65 | // and does not use/ request the availablity data (which could slow things down). |
| 66 | 66 | static $imgopt_active = null; |
| 67 | 67 | |
| 68 | - if ( null === $imgopt_active ) { |
|
| 69 | - $opts = get_option( 'autoptimize_imgopt_settings', '' ); |
|
| 70 | - if ( ! empty( $opts ) && is_array( $opts ) && array_key_exists( 'autoptimize_imgopt_checkbox_field_1', $opts ) && ! empty( $opts['autoptimize_imgopt_checkbox_field_1'] ) && '1' === $opts['autoptimize_imgopt_checkbox_field_1'] ) { |
|
| 68 | + if (null === $imgopt_active) { |
|
| 69 | + $opts = get_option('autoptimize_imgopt_settings', ''); |
|
| 70 | + if (!empty($opts) && is_array($opts) && array_key_exists('autoptimize_imgopt_checkbox_field_1', $opts) && !empty($opts['autoptimize_imgopt_checkbox_field_1']) && '1' === $opts['autoptimize_imgopt_checkbox_field_1']) { |
|
| 71 | 71 | $imgopt_active = true; |
| 72 | 72 | } else { |
| 73 | 73 | $imgopt_active = false; |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | */ |
| 88 | 88 | public static function instance() |
| 89 | 89 | { |
| 90 | - if ( null === self::$instance ) { |
|
| 90 | + if (null === self::$instance) { |
|
| 91 | 91 | self::$instance = new self(); |
| 92 | 92 | } |
| 93 | 93 | |
@@ -96,26 +96,26 @@ discard block |
||
| 96 | 96 | |
| 97 | 97 | public function run() |
| 98 | 98 | { |
| 99 | - if ( is_admin() ) { |
|
| 100 | - add_action( 'admin_menu', array( $this, 'imgopt_admin_menu' ) ); |
|
| 101 | - add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_imgopt_tab' ), 9 ); |
|
| 99 | + if (is_admin()) { |
|
| 100 | + add_action('admin_menu', array($this, 'imgopt_admin_menu')); |
|
| 101 | + add_filter('autoptimize_filter_settingsscreen_tabs', array($this, 'add_imgopt_tab'), 9); |
|
| 102 | 102 | } else { |
| 103 | 103 | $this->run_on_frontend(); |
| 104 | 104 | } |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | public function run_on_frontend() { |
| 108 | - if ( ! $this->should_run() ) { |
|
| 109 | - if ( $this->should_lazyload() ) { |
|
| 108 | + if (!$this->should_run()) { |
|
| 109 | + if ($this->should_lazyload()) { |
|
| 110 | 110 | add_filter( |
| 111 | 111 | 'autoptimize_html_after_minify', |
| 112 | - array( $this, 'filter_lazyload_images' ), |
|
| 112 | + array($this, 'filter_lazyload_images'), |
|
| 113 | 113 | 10, |
| 114 | 114 | 1 |
| 115 | 115 | ); |
| 116 | 116 | add_action( |
| 117 | 117 | 'wp_footer', |
| 118 | - array( $this, 'add_lazyload_js_footer' ), |
|
| 118 | + array($this, 'add_lazyload_js_footer'), |
|
| 119 | 119 | 10, |
| 120 | 120 | 0 |
| 121 | 121 | ); |
@@ -125,39 +125,39 @@ discard block |
||
| 125 | 125 | |
| 126 | 126 | $active = false; |
| 127 | 127 | |
| 128 | - if ( apply_filters( 'autoptimize_filter_imgopt_do', true ) ) { |
|
| 128 | + if (apply_filters('autoptimize_filter_imgopt_do', true)) { |
|
| 129 | 129 | add_filter( |
| 130 | 130 | 'autoptimize_html_after_minify', |
| 131 | - array( $this, 'filter_optimize_images' ), |
|
| 131 | + array($this, 'filter_optimize_images'), |
|
| 132 | 132 | 10, |
| 133 | 133 | 1 |
| 134 | 134 | ); |
| 135 | 135 | $active = true; |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - if ( apply_filters( 'autoptimize_filter_imgopt_do_css', true ) ) { |
|
| 138 | + if (apply_filters('autoptimize_filter_imgopt_do_css', true)) { |
|
| 139 | 139 | add_filter( |
| 140 | 140 | 'autoptimize_filter_base_replace_cdn', |
| 141 | - array( $this, 'filter_optimize_css_images' ), |
|
| 141 | + array($this, 'filter_optimize_css_images'), |
|
| 142 | 142 | 10, |
| 143 | 143 | 1 |
| 144 | 144 | ); |
| 145 | 145 | $active = true; |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | - if ( $active ) { |
|
| 148 | + if ($active) { |
|
| 149 | 149 | add_filter( |
| 150 | 150 | 'autoptimize_extra_filter_tobepreconn', |
| 151 | - array( $this, 'filter_preconnect_imgopt_url' ), |
|
| 151 | + array($this, 'filter_preconnect_imgopt_url'), |
|
| 152 | 152 | 10, |
| 153 | 153 | 1 |
| 154 | 154 | ); |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - if ( $this->should_lazyload() ) { |
|
| 157 | + if ($this->should_lazyload()) { |
|
| 158 | 158 | add_action( |
| 159 | 159 | 'wp_footer', |
| 160 | - array( $this, 'add_lazyload_js_footer' ) |
|
| 160 | + array($this, 'add_lazyload_js_footer') |
|
| 161 | 161 | ); |
| 162 | 162 | } |
| 163 | 163 | } |
@@ -170,8 +170,8 @@ discard block |
||
| 170 | 170 | protected function should_run() |
| 171 | 171 | { |
| 172 | 172 | $opts = $this->options; |
| 173 | - $service_not_down = ( 'down' !== $opts['availabilities']['extra_imgopt']['status'] ); |
|
| 174 | - $not_launch_status = ( 'launch' !== $opts['availabilities']['extra_imgopt']['status'] ); |
|
| 173 | + $service_not_down = ('down' !== $opts['availabilities']['extra_imgopt']['status']); |
|
| 174 | + $not_launch_status = ('launch' !== $opts['availabilities']['extra_imgopt']['status']); |
|
| 175 | 175 | |
| 176 | 176 | $do_cdn = true; |
| 177 | 177 | $_userstatus = $this->get_imgopt_provider_userstatus(); |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | $this->imgopt_active() |
| 184 | 184 | && $do_cdn |
| 185 | 185 | && $service_not_down |
| 186 | - && ( $not_launch_status || $this->launch_ok() ) |
|
| 186 | + && ($not_launch_status || $this->launch_ok()) |
|
| 187 | 187 | ) { |
| 188 | 188 | return true; |
| 189 | 189 | } |
@@ -194,13 +194,13 @@ discard block |
||
| 194 | 194 | { |
| 195 | 195 | static $imgopt_host = null; |
| 196 | 196 | |
| 197 | - if ( null === $imgopt_host ) { |
|
| 197 | + if (null === $imgopt_host) { |
|
| 198 | 198 | $imgopt_host = 'https://cdn.shortpixel.ai/'; |
| 199 | 199 | $avail_imgopt = $this->options['availabilities']['extra_imgopt']; |
| 200 | - if ( ! empty( $avail_imgopt ) && array_key_exists( 'hosts', $avail_imgopt ) && is_array( $avail_imgopt['hosts'] ) ) { |
|
| 201 | - $imgopt_host = array_rand( array_flip( $avail_imgopt['hosts'] ) ); |
|
| 200 | + if (!empty($avail_imgopt) && array_key_exists('hosts', $avail_imgopt) && is_array($avail_imgopt['hosts'])) { |
|
| 201 | + $imgopt_host = array_rand(array_flip($avail_imgopt['hosts'])); |
|
| 202 | 202 | } |
| 203 | - $imgopt_host = apply_filters( 'autoptimize_filter_imgopt_host', $imgopt_host ); |
|
| 203 | + $imgopt_host = apply_filters('autoptimize_filter_imgopt_host', $imgopt_host); |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | return $imgopt_host; |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | |
| 216 | 216 | public static function get_service_url_suffix() |
| 217 | 217 | { |
| 218 | - $suffix = '/af/GWRGFLW109483/' . AUTOPTIMIZE_SITE_DOMAIN; |
|
| 218 | + $suffix = '/af/GWRGFLW109483/'.AUTOPTIMIZE_SITE_DOMAIN; |
|
| 219 | 219 | |
| 220 | 220 | return $suffix; |
| 221 | 221 | } |
@@ -224,12 +224,12 @@ discard block |
||
| 224 | 224 | { |
| 225 | 225 | static $quality = null; |
| 226 | 226 | |
| 227 | - if ( null === $quality ) { |
|
| 227 | + if (null === $quality) { |
|
| 228 | 228 | $q_array = $this->get_img_quality_array(); |
| 229 | 229 | $setting = $this->get_img_quality_setting(); |
| 230 | 230 | $quality = apply_filters( |
| 231 | 231 | 'autoptimize_filter_imgopt_quality', |
| 232 | - 'q_' . $q_array[ $setting ] |
|
| 232 | + 'q_'.$q_array[$setting] |
|
| 233 | 233 | ); |
| 234 | 234 | } |
| 235 | 235 | |
@@ -240,7 +240,7 @@ discard block |
||
| 240 | 240 | { |
| 241 | 241 | static $map = null; |
| 242 | 242 | |
| 243 | - if ( null === $map ) { |
|
| 243 | + if (null === $map) { |
|
| 244 | 244 | $map = array( |
| 245 | 245 | '1' => 'lossy', |
| 246 | 246 | '2' => 'glossy', |
@@ -259,12 +259,12 @@ discard block |
||
| 259 | 259 | { |
| 260 | 260 | static $q = null; |
| 261 | 261 | |
| 262 | - if ( null === $q ) { |
|
| 263 | - if ( is_array( $this->options ) && array_key_exists( 'autoptimize_imgopt_select_field_2', $this->options ) ) { |
|
| 262 | + if (null === $q) { |
|
| 263 | + if (is_array($this->options) && array_key_exists('autoptimize_imgopt_select_field_2', $this->options)) { |
|
| 264 | 264 | $setting = $this->options['autoptimize_imgopt_select_field_2']; |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | - if ( ! isset( $setting ) || empty( $setting ) || ( '1' !== $setting && '3' !== $setting ) ) { |
|
| 267 | + if (!isset($setting) || empty($setting) || ('1' !== $setting && '3' !== $setting)) { |
|
| 268 | 268 | // default image opt. value is 2 ("glossy"). |
| 269 | 269 | $q = '2'; |
| 270 | 270 | } else { |
@@ -275,10 +275,10 @@ discard block |
||
| 275 | 275 | return $q; |
| 276 | 276 | } |
| 277 | 277 | |
| 278 | - public function filter_preconnect_imgopt_url( array $in ) |
|
| 278 | + public function filter_preconnect_imgopt_url(array $in) |
|
| 279 | 279 | { |
| 280 | - $url_parts = parse_url( $this->get_imgopt_base_url() ); |
|
| 281 | - $in[] = $url_parts['scheme'] . '://' . $url_parts['host']; |
|
| 280 | + $url_parts = parse_url($this->get_imgopt_base_url()); |
|
| 281 | + $in[] = $url_parts['scheme'].'://'.$url_parts['host']; |
|
| 282 | 282 | |
| 283 | 283 | return $in; |
| 284 | 284 | } |
@@ -291,20 +291,20 @@ discard block |
||
| 291 | 291 | * |
| 292 | 292 | * @return string |
| 293 | 293 | */ |
| 294 | - private function normalize_img_url( $in ) |
|
| 294 | + private function normalize_img_url($in) |
|
| 295 | 295 | { |
| 296 | 296 | // Only parse the site url once. |
| 297 | 297 | static $parsed_site_url = null; |
| 298 | - if ( null === $parsed_site_url ) { |
|
| 299 | - $parsed_site_url = parse_url( site_url() ); |
|
| 298 | + if (null === $parsed_site_url) { |
|
| 299 | + $parsed_site_url = parse_url(site_url()); |
|
| 300 | 300 | } |
| 301 | 301 | |
| 302 | 302 | // get CDN domain once. |
| 303 | 303 | static $cdn_domain = null; |
| 304 | - if ( is_null( $cdn_domain ) ) { |
|
| 305 | - $cdn_url = apply_filters( 'autoptimize_filter_base_cdnurl', get_option( 'autoptimize_cdn_url', '' ) ); |
|
| 306 | - if ( ! empty( $cdn_url ) ) { |
|
| 307 | - $cdn_domain = parse_url( $cdn_url, PHP_URL_HOST ); |
|
| 304 | + if (is_null($cdn_domain)) { |
|
| 305 | + $cdn_url = apply_filters('autoptimize_filter_base_cdnurl', get_option('autoptimize_cdn_url', '')); |
|
| 306 | + if (!empty($cdn_url)) { |
|
| 307 | + $cdn_domain = parse_url($cdn_url, PHP_URL_HOST); |
|
| 308 | 308 | } else { |
| 309 | 309 | $cdn_domain = ''; |
| 310 | 310 | } |
@@ -320,39 +320,39 @@ discard block |
||
| 320 | 320 | * identical string operations). |
| 321 | 321 | */ |
| 322 | 322 | static $cache = null; |
| 323 | - if ( null === $cache ) { |
|
| 323 | + if (null === $cache) { |
|
| 324 | 324 | $cache = array(); |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | // Do the work on cache miss only. |
| 328 | - if ( ! isset( $cache[ $in ] ) ) { |
|
| 328 | + if (!isset($cache[$in])) { |
|
| 329 | 329 | // Default to what was given to us. |
| 330 | - $result = trim( $in ); |
|
| 331 | - if ( autoptimizeUtils::is_protocol_relative( $in ) ) { |
|
| 332 | - $result = $parsed_site_url['scheme'] . ':' . $in; |
|
| 333 | - } elseif ( 0 === strpos( $in, '/' ) ) { |
|
| 330 | + $result = trim($in); |
|
| 331 | + if (autoptimizeUtils::is_protocol_relative($in)) { |
|
| 332 | + $result = $parsed_site_url['scheme'].':'.$in; |
|
| 333 | + } elseif (0 === strpos($in, '/')) { |
|
| 334 | 334 | // Root-relative... |
| 335 | - $result = $parsed_site_url['scheme'] . '://' . $parsed_site_url['host']; |
|
| 335 | + $result = $parsed_site_url['scheme'].'://'.$parsed_site_url['host']; |
|
| 336 | 336 | $result .= $in; |
| 337 | - } elseif ( ! empty( $cdn_domain ) && strpos( $in, $cdn_domain ) !== 0 ) { |
|
| 338 | - $result = str_replace( $cdn_domain, $parsed_site_url['host'], $in ); |
|
| 337 | + } elseif (!empty($cdn_domain) && strpos($in, $cdn_domain) !== 0) { |
|
| 338 | + $result = str_replace($cdn_domain, $parsed_site_url['host'], $in); |
|
| 339 | 339 | } |
| 340 | 340 | |
| 341 | - $result = apply_filters( 'autoptimize_filter_imgopt_normalized_url', $result ); |
|
| 341 | + $result = apply_filters('autoptimize_filter_imgopt_normalized_url', $result); |
|
| 342 | 342 | |
| 343 | 343 | // Store in cache. |
| 344 | - $cache[ $in ] = $result; |
|
| 344 | + $cache[$in] = $result; |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | - return $cache[ $in ]; |
|
| 347 | + return $cache[$in]; |
|
| 348 | 348 | } |
| 349 | 349 | |
| 350 | - public function filter_optimize_css_images( $in ) |
|
| 350 | + public function filter_optimize_css_images($in) |
|
| 351 | 351 | { |
| 352 | - $in = $this->normalize_img_url( $in ); |
|
| 352 | + $in = $this->normalize_img_url($in); |
|
| 353 | 353 | |
| 354 | - if ( $this->can_optimize_image( $in ) ) { |
|
| 355 | - return $this->build_imgopt_url( $in, '', '' ); |
|
| 354 | + if ($this->can_optimize_image($in)) { |
|
| 355 | + return $this->build_imgopt_url($in, '', ''); |
|
| 356 | 356 | } else { |
| 357 | 357 | return $in; |
| 358 | 358 | } |
@@ -362,50 +362,50 @@ discard block |
||
| 362 | 362 | { |
| 363 | 363 | static $imgopt_base_url = null; |
| 364 | 364 | |
| 365 | - if ( null === $imgopt_base_url ) { |
|
| 365 | + if (null === $imgopt_base_url) { |
|
| 366 | 366 | $imgopt_host = $this->get_imgopt_host(); |
| 367 | 367 | $quality = $this->get_img_quality_string(); |
| 368 | - $ret_val = apply_filters( 'autoptimize_filter_imgopt_wait', 'ret_img' ); // values: ret_wait, ret_img, ret_json, ret_blank. |
|
| 369 | - $imgopt_base_url = $imgopt_host . 'client/' . $quality . ',' . $ret_val; |
|
| 370 | - $imgopt_base_url = apply_filters( 'autoptimize_filter_imgopt_base_url', $imgopt_base_url ); |
|
| 368 | + $ret_val = apply_filters('autoptimize_filter_imgopt_wait', 'ret_img'); // values: ret_wait, ret_img, ret_json, ret_blank. |
|
| 369 | + $imgopt_base_url = $imgopt_host.'client/'.$quality.','.$ret_val; |
|
| 370 | + $imgopt_base_url = apply_filters('autoptimize_filter_imgopt_base_url', $imgopt_base_url); |
|
| 371 | 371 | } |
| 372 | 372 | |
| 373 | 373 | return $imgopt_base_url; |
| 374 | 374 | } |
| 375 | 375 | |
| 376 | - private function can_optimize_image( $url ) |
|
| 376 | + private function can_optimize_image($url) |
|
| 377 | 377 | { |
| 378 | 378 | static $cdn_url = null; |
| 379 | 379 | static $nopti_images = null; |
| 380 | 380 | |
| 381 | - if ( null === $cdn_url ) { |
|
| 381 | + if (null === $cdn_url) { |
|
| 382 | 382 | $cdn_url = apply_filters( |
| 383 | 383 | 'autoptimize_filter_base_cdnurl', |
| 384 | - get_option( 'autoptimize_cdn_url', '' ) |
|
| 384 | + get_option('autoptimize_cdn_url', '') |
|
| 385 | 385 | ); |
| 386 | 386 | } |
| 387 | 387 | |
| 388 | - if ( null === $nopti_images ) { |
|
| 389 | - $nopti_images = apply_filters( 'autoptimize_filter_imgopt_noptimize', '' ); |
|
| 388 | + if (null === $nopti_images) { |
|
| 389 | + $nopti_images = apply_filters('autoptimize_filter_imgopt_noptimize', ''); |
|
| 390 | 390 | } |
| 391 | 391 | |
| 392 | 392 | $site_host = AUTOPTIMIZE_SITE_DOMAIN; |
| 393 | - $url = $this->normalize_img_url( $url ); |
|
| 394 | - $url_parsed = parse_url( $url ); |
|
| 393 | + $url = $this->normalize_img_url($url); |
|
| 394 | + $url_parsed = parse_url($url); |
|
| 395 | 395 | |
| 396 | - if ( array_key_exists( 'host', $url_parsed ) && $url_parsed['host'] !== $site_host && empty( $cdn_url ) ) { |
|
| 396 | + if (array_key_exists('host', $url_parsed) && $url_parsed['host'] !== $site_host && empty($cdn_url)) { |
|
| 397 | 397 | return false; |
| 398 | - } elseif ( ! empty( $cdn_url ) && strpos( $url, $cdn_url ) === false && array_key_exists( 'host', $url_parsed ) && $url_parsed['host'] !== $site_host ) { |
|
| 398 | + } elseif (!empty($cdn_url) && strpos($url, $cdn_url) === false && array_key_exists('host', $url_parsed) && $url_parsed['host'] !== $site_host) { |
|
| 399 | 399 | return false; |
| 400 | - } elseif ( strpos( $url, '.php' ) !== false ) { |
|
| 400 | + } elseif (strpos($url, '.php') !== false) { |
|
| 401 | 401 | return false; |
| 402 | - } elseif ( str_ireplace( array( '.png', '.gif', '.jpg', '.jpeg', '.webp' ), '', $url_parsed['path'] ) === $url_parsed['path'] ) { |
|
| 402 | + } elseif (str_ireplace(array('.png', '.gif', '.jpg', '.jpeg', '.webp'), '', $url_parsed['path']) === $url_parsed['path']) { |
|
| 403 | 403 | // fixme: better check against end of string. |
| 404 | 404 | return false; |
| 405 | - } elseif ( ! empty( $nopti_images ) ) { |
|
| 406 | - $nopti_images_array = array_filter( array_map( 'trim', explode( ',', $nopti_images ) ) ); |
|
| 407 | - foreach ( $nopti_images_array as $nopti_image ) { |
|
| 408 | - if ( strpos( $url, $nopti_image ) !== false ) { |
|
| 405 | + } elseif (!empty($nopti_images)) { |
|
| 406 | + $nopti_images_array = array_filter(array_map('trim', explode(',', $nopti_images))); |
|
| 407 | + foreach ($nopti_images_array as $nopti_image) { |
|
| 408 | + if (strpos($url, $nopti_image) !== false) { |
|
| 409 | 409 | return false; |
| 410 | 410 | } |
| 411 | 411 | } |
@@ -413,13 +413,13 @@ discard block |
||
| 413 | 413 | return true; |
| 414 | 414 | } |
| 415 | 415 | |
| 416 | - private function build_imgopt_url( $orig_url, $width = 0, $height = 0 ) |
|
| 416 | + private function build_imgopt_url($orig_url, $width = 0, $height = 0) |
|
| 417 | 417 | { |
| 418 | 418 | // sanitize width and height. |
| 419 | - if ( strpos( $width, '%' ) !== false ) { |
|
| 419 | + if (strpos($width, '%') !== false) { |
|
| 420 | 420 | $width = 0; |
| 421 | 421 | } |
| 422 | - if ( strpos( $height, '%' ) !== false ) { |
|
| 422 | + if (strpos($height, '%') !== false) { |
|
| 423 | 423 | $height = 0; |
| 424 | 424 | } |
| 425 | 425 | $width = (int) $width; |
@@ -433,43 +433,43 @@ discard block |
||
| 433 | 433 | ); |
| 434 | 434 | |
| 435 | 435 | // If filter modified the url, return that. |
| 436 | - if ( $filtered_url !== $orig_url ) { |
|
| 436 | + if ($filtered_url !== $orig_url) { |
|
| 437 | 437 | return $filtered_url; |
| 438 | 438 | } |
| 439 | 439 | |
| 440 | - $orig_url = $this->normalize_img_url( $orig_url ); |
|
| 440 | + $orig_url = $this->normalize_img_url($orig_url); |
|
| 441 | 441 | $imgopt_base_url = $this->get_imgopt_base_url(); |
| 442 | 442 | $imgopt_size = ''; |
| 443 | 443 | |
| 444 | - if ( $width && 0 !== $width ) { |
|
| 445 | - $imgopt_size = ',w_' . $width; |
|
| 444 | + if ($width && 0 !== $width) { |
|
| 445 | + $imgopt_size = ',w_'.$width; |
|
| 446 | 446 | } |
| 447 | 447 | |
| 448 | - if ( $height && 0 !== $height ) { |
|
| 449 | - $imgopt_size .= ',h_' . $height; |
|
| 448 | + if ($height && 0 !== $height) { |
|
| 449 | + $imgopt_size .= ',h_'.$height; |
|
| 450 | 450 | } |
| 451 | 451 | |
| 452 | - $url = $imgopt_base_url . $imgopt_size . '/' . $orig_url; |
|
| 452 | + $url = $imgopt_base_url.$imgopt_size.'/'.$orig_url; |
|
| 453 | 453 | |
| 454 | 454 | return $url; |
| 455 | 455 | } |
| 456 | 456 | |
| 457 | - public function replace_data_thumbs( $matches ) |
|
| 457 | + public function replace_data_thumbs($matches) |
|
| 458 | 458 | { |
| 459 | - return $this->replace_img_callback( $matches, 150, 150 ); |
|
| 459 | + return $this->replace_img_callback($matches, 150, 150); |
|
| 460 | 460 | } |
| 461 | 461 | |
| 462 | - public function replace_img_callback( $matches, $width = 0, $height = 0 ) |
|
| 462 | + public function replace_img_callback($matches, $width = 0, $height = 0) |
|
| 463 | 463 | { |
| 464 | - $_normalized_img_url = $this->normalize_img_url( $matches[1] ); |
|
| 465 | - if ( $this->can_optimize_image( $matches[1] ) ) { |
|
| 466 | - return str_replace( $matches[1], $this->build_imgopt_url( $_normalized_img_url, $width, $height ), $matches[0] ); |
|
| 464 | + $_normalized_img_url = $this->normalize_img_url($matches[1]); |
|
| 465 | + if ($this->can_optimize_image($matches[1])) { |
|
| 466 | + return str_replace($matches[1], $this->build_imgopt_url($_normalized_img_url, $width, $height), $matches[0]); |
|
| 467 | 467 | } else { |
| 468 | 468 | return $matches[0]; |
| 469 | 469 | } |
| 470 | 470 | } |
| 471 | 471 | |
| 472 | - public function filter_optimize_images( $in ) |
|
| 472 | + public function filter_optimize_images($in) |
|
| 473 | 473 | { |
| 474 | 474 | /* |
| 475 | 475 | * potential future functional improvements: |
@@ -480,7 +480,7 @@ discard block |
||
| 480 | 480 | $to_replace = array(); |
| 481 | 481 | |
| 482 | 482 | // hide noscript tags to avoid nesting noscript tags (as lazyloaded images add noscript). |
| 483 | - if ( $this->should_lazyload() ) { |
|
| 483 | + if ($this->should_lazyload()) { |
|
| 484 | 484 | $in = autoptimizeBase::replace_contents_with_marker_if_exists( |
| 485 | 485 | 'SCRIPT', |
| 486 | 486 | '<script', |
@@ -490,25 +490,25 @@ discard block |
||
| 490 | 490 | } |
| 491 | 491 | |
| 492 | 492 | // extract img tags. |
| 493 | - if ( preg_match_all( '#<img[^>]*src[^>]*>#Usmi', $in, $matches ) ) { |
|
| 494 | - foreach ( $matches[0] as $tag ) { |
|
| 493 | + if (preg_match_all('#<img[^>]*src[^>]*>#Usmi', $in, $matches)) { |
|
| 494 | + foreach ($matches[0] as $tag) { |
|
| 495 | 495 | $orig_tag = $tag; |
| 496 | 496 | $imgopt_w = ''; |
| 497 | 497 | $imgopt_h = ''; |
| 498 | 498 | |
| 499 | 499 | // first do (data-)srcsets. |
| 500 | - if ( preg_match_all( '#srcset=("|\')(.*)("|\')#Usmi', $tag, $allsrcsets, PREG_SET_ORDER ) ) { |
|
| 501 | - foreach ( $allsrcsets as $srcset ) { |
|
| 500 | + if (preg_match_all('#srcset=("|\')(.*)("|\')#Usmi', $tag, $allsrcsets, PREG_SET_ORDER)) { |
|
| 501 | + foreach ($allsrcsets as $srcset) { |
|
| 502 | 502 | $srcset = $srcset[2]; |
| 503 | - $srcsets = explode( ',', $srcset ); |
|
| 504 | - foreach ( $srcsets as $indiv_srcset ) { |
|
| 505 | - $indiv_srcset_parts = explode( ' ', trim( $indiv_srcset ) ); |
|
| 506 | - if ( isset( $indiv_srcset_parts[1] ) && rtrim( $indiv_srcset_parts[1], 'w' ) !== $indiv_srcset_parts[1] ) { |
|
| 507 | - $imgopt_w = rtrim( $indiv_srcset_parts[1], 'w' ); |
|
| 503 | + $srcsets = explode(',', $srcset); |
|
| 504 | + foreach ($srcsets as $indiv_srcset) { |
|
| 505 | + $indiv_srcset_parts = explode(' ', trim($indiv_srcset)); |
|
| 506 | + if (isset($indiv_srcset_parts[1]) && rtrim($indiv_srcset_parts[1], 'w') !== $indiv_srcset_parts[1]) { |
|
| 507 | + $imgopt_w = rtrim($indiv_srcset_parts[1], 'w'); |
|
| 508 | 508 | } |
| 509 | - if ( $this->can_optimize_image( $indiv_srcset_parts[0] ) ) { |
|
| 510 | - $imgopt_url = $this->build_imgopt_url( $indiv_srcset_parts[0], $imgopt_w, '' ); |
|
| 511 | - $tag = str_replace( $indiv_srcset_parts[0], $imgopt_url, $tag ); |
|
| 509 | + if ($this->can_optimize_image($indiv_srcset_parts[0])) { |
|
| 510 | + $imgopt_url = $this->build_imgopt_url($indiv_srcset_parts[0], $imgopt_w, ''); |
|
| 511 | + $tag = str_replace($indiv_srcset_parts[0], $imgopt_url, $tag); |
|
| 512 | 512 | } |
| 513 | 513 | } |
| 514 | 514 | } |
@@ -516,142 +516,142 @@ discard block |
||
| 516 | 516 | |
| 517 | 517 | // proceed with img src. |
| 518 | 518 | // get width and height and add to $imgopt_size. |
| 519 | - $_get_size = $this->get_size_from_tag( $tag ); |
|
| 519 | + $_get_size = $this->get_size_from_tag($tag); |
|
| 520 | 520 | $imgopt_w = $_get_size['width']; |
| 521 | 521 | $imgopt_h = $_get_size['height']; |
| 522 | 522 | |
| 523 | 523 | // then start replacing images src. |
| 524 | - if ( preg_match_all( '#src=(?:"|\')(?!data)(.*)(?:"|\')#Usmi', $tag, $urls, PREG_SET_ORDER ) ) { |
|
| 525 | - foreach ( $urls as $url ) { |
|
| 524 | + if (preg_match_all('#src=(?:"|\')(?!data)(.*)(?:"|\')#Usmi', $tag, $urls, PREG_SET_ORDER)) { |
|
| 525 | + foreach ($urls as $url) { |
|
| 526 | 526 | $full_src_orig = $url[0]; |
| 527 | 527 | $url = $url[1]; |
| 528 | - if ( $this->can_optimize_image( $url ) ) { |
|
| 529 | - $imgopt_url = $this->build_imgopt_url( $url, $imgopt_w, $imgopt_h ); |
|
| 530 | - $full_imgopt_src = str_replace( $url, $imgopt_url, $full_src_orig ); |
|
| 531 | - $tag = str_replace( $full_src_orig, $full_imgopt_src, $tag ); |
|
| 528 | + if ($this->can_optimize_image($url)) { |
|
| 529 | + $imgopt_url = $this->build_imgopt_url($url, $imgopt_w, $imgopt_h); |
|
| 530 | + $full_imgopt_src = str_replace($url, $imgopt_url, $full_src_orig); |
|
| 531 | + $tag = str_replace($full_src_orig, $full_imgopt_src, $tag); |
|
| 532 | 532 | } |
| 533 | 533 | } |
| 534 | 534 | } |
| 535 | 535 | |
| 536 | 536 | // do lazyload stuff. |
| 537 | - if ( $this->should_lazyload() && str_ireplace( $this->get_lazyload_exclusions(), '', $tag ) === $tag ) { |
|
| 538 | - $tag = $this->maybe_fix_missing_quotes( $tag ); |
|
| 539 | - $noscript_tag = '<noscript>' . $tag . '</noscript>'; |
|
| 540 | - $tag = str_replace( 'srcset=', 'data-srcset=', $tag ); |
|
| 537 | + if ($this->should_lazyload() && str_ireplace($this->get_lazyload_exclusions(), '', $tag) === $tag) { |
|
| 538 | + $tag = $this->maybe_fix_missing_quotes($tag); |
|
| 539 | + $noscript_tag = '<noscript>'.$tag.'</noscript>'; |
|
| 540 | + $tag = str_replace('srcset=', 'data-srcset=', $tag); |
|
| 541 | 541 | |
| 542 | 542 | // add lazyload class. |
| 543 | - $tag = $this->inject_classes_in_tag( $tag, 'lazyload ' ); |
|
| 543 | + $tag = $this->inject_classes_in_tag($tag, 'lazyload '); |
|
| 544 | 544 | |
| 545 | 545 | // set placeholder. |
| 546 | - if ( strpos( $url, $this->get_imgopt_host() ) === 0 ) { |
|
| 546 | + if (strpos($url, $this->get_imgopt_host()) === 0) { |
|
| 547 | 547 | // if all img src have been replaced during srcset, we have to extract the |
| 548 | 548 | // origin url from the imgopt one to be able to set a lqip placeholder. |
| 549 | - $_url = substr( $url, strpos( $url, '/http' ) + 1 ); |
|
| 549 | + $_url = substr($url, strpos($url, '/http') + 1); |
|
| 550 | 550 | } else { |
| 551 | 551 | $_url = $url; |
| 552 | 552 | } |
| 553 | - if ( $this->can_optimize_image( $_url ) && apply_filters( 'autoptimize_filter_imgopt_lazyload_dolqip', true ) ) { |
|
| 553 | + if ($this->can_optimize_image($_url) && apply_filters('autoptimize_filter_imgopt_lazyload_dolqip', true)) { |
|
| 554 | 554 | $lqip_w = ''; |
| 555 | 555 | $lqip_h = ''; |
| 556 | - if ( isset( $imgopt_w ) ) { |
|
| 557 | - $lqip_w = ',w_' . $imgopt_w; |
|
| 556 | + if (isset($imgopt_w)) { |
|
| 557 | + $lqip_w = ',w_'.$imgopt_w; |
|
| 558 | 558 | } |
| 559 | - if ( isset( $imgopt_h ) ) { |
|
| 560 | - $lqip_h = ',h_' . $imgopt_h; |
|
| 559 | + if (isset($imgopt_h)) { |
|
| 560 | + $lqip_h = ',h_'.$imgopt_h; |
|
| 561 | 561 | } |
| 562 | - $placeholder = $this->get_imgopt_host() . 'client/q_lqip,ret_wait' . $lqip_w . $lqip_h . '/' . $_url; |
|
| 562 | + $placeholder = $this->get_imgopt_host().'client/q_lqip,ret_wait'.$lqip_w.$lqip_h.'/'.$_url; |
|
| 563 | 563 | } else { |
| 564 | - $placeholder = $this->get_default_lazyload_placeholder( $imgopt_w, $imgopt_h ); |
|
| 564 | + $placeholder = $this->get_default_lazyload_placeholder($imgopt_w, $imgopt_h); |
|
| 565 | 565 | } |
| 566 | - $placeholder = ' src=\'' . apply_filters( 'autoptimize_filter_imgopt_lazyload_placeholder', $placeholder ); |
|
| 566 | + $placeholder = ' src=\''.apply_filters('autoptimize_filter_imgopt_lazyload_placeholder', $placeholder); |
|
| 567 | 567 | |
| 568 | 568 | // add min-heigth off by default as it can deform images, can be enabled with filter. |
| 569 | 569 | $min_height = ''; |
| 570 | - if ( apply_filters( 'autoptimize_filter_imgopt_lazyload_addminheight', false ) ) { |
|
| 571 | - $min_height = ' style="min-height:' . $imgopt_h . 'px;"'; |
|
| 570 | + if (apply_filters('autoptimize_filter_imgopt_lazyload_addminheight', false)) { |
|
| 571 | + $min_height = ' style="min-height:'.$imgopt_h.'px;"'; |
|
| 572 | 572 | } |
| 573 | 573 | |
| 574 | 574 | // move sizes to data-sizes unless filter says no. |
| 575 | - if ( apply_filters( 'autoptimize_filter_imgopt_lazyload_move_sizes', true ) ) { |
|
| 576 | - $tag = str_replace( 'sizes=', 'data-sizes=', $tag ); |
|
| 575 | + if (apply_filters('autoptimize_filter_imgopt_lazyload_move_sizes', true)) { |
|
| 576 | + $tag = str_replace('sizes=', 'data-sizes=', $tag); |
|
| 577 | 577 | } |
| 578 | 578 | |
| 579 | 579 | // add noscript & placeholder. |
| 580 | - $tag = $noscript_tag . str_replace( ' src=', $min_height . $placeholder . '\' data-src=', $tag ); |
|
| 581 | - $tag = apply_filters( 'autoptimize_filter_imgopt_lazyloaded_img', $tag ); |
|
| 580 | + $tag = $noscript_tag.str_replace(' src=', $min_height.$placeholder.'\' data-src=', $tag); |
|
| 581 | + $tag = apply_filters('autoptimize_filter_imgopt_lazyloaded_img', $tag); |
|
| 582 | 582 | } |
| 583 | 583 | |
| 584 | 584 | // add tag to array for later replacement. |
| 585 | - if ( $tag !== $orig_tag ) { |
|
| 586 | - $to_replace[ $orig_tag ] = $tag; |
|
| 585 | + if ($tag !== $orig_tag) { |
|
| 586 | + $to_replace[$orig_tag] = $tag; |
|
| 587 | 587 | } |
| 588 | 588 | } |
| 589 | 589 | } |
| 590 | 590 | |
| 591 | 591 | // and replace all. |
| 592 | - $out = str_replace( array_keys( $to_replace ), array_values( $to_replace ), $in ); |
|
| 592 | + $out = str_replace(array_keys($to_replace), array_values($to_replace), $in); |
|
| 593 | 593 | |
| 594 | 594 | // img thumbnails in e.g. woocommerce. |
| 595 | - if ( strpos( $out, 'data-thumb' ) !== false && apply_filters( 'autoptimize_filter_imgopt_datathumbs', true ) ) { |
|
| 595 | + if (strpos($out, 'data-thumb') !== false && apply_filters('autoptimize_filter_imgopt_datathumbs', true)) { |
|
| 596 | 596 | $out = preg_replace_callback( |
| 597 | 597 | '/\<div(?:[^>]?)\sdata-thumb\=(?:\"|\')(.+?)(?:\"|\')(?:[^>]*)?\>/s', |
| 598 | - array( $this, 'replace_data_thumbs' ), |
|
| 598 | + array($this, 'replace_data_thumbs'), |
|
| 599 | 599 | $out |
| 600 | 600 | ); |
| 601 | 601 | } |
| 602 | 602 | |
| 603 | 603 | // background-image in inline style. |
| 604 | - if ( strpos( $out, 'background-image:' ) !== false && apply_filters( 'autoptimize_filter_imgopt_backgroundimages', true ) ) { |
|
| 604 | + if (strpos($out, 'background-image:') !== false && apply_filters('autoptimize_filter_imgopt_backgroundimages', true)) { |
|
| 605 | 605 | $out = preg_replace_callback( |
| 606 | 606 | '/style=(?:"|\').*?background-image:\s?url\((?:"|\')?([^"\')]*)(?:"|\')?\)/', |
| 607 | - array( $this, 'replace_img_callback' ), |
|
| 607 | + array($this, 'replace_img_callback'), |
|
| 608 | 608 | $out |
| 609 | 609 | ); |
| 610 | 610 | } |
| 611 | 611 | |
| 612 | 612 | // lazyload: restore noscript tags + lazyload picture source tags. |
| 613 | - if ( $this->should_lazyload() ) { |
|
| 613 | + if ($this->should_lazyload()) { |
|
| 614 | 614 | $out = autoptimizeBase::restore_marked_content( |
| 615 | 615 | 'SCRIPT', |
| 616 | 616 | $out |
| 617 | 617 | ); |
| 618 | 618 | |
| 619 | - $out = $this->process_picture_tag( $out, true, true ); |
|
| 619 | + $out = $this->process_picture_tag($out, true, true); |
|
| 620 | 620 | } else { |
| 621 | - $out = $this->process_picture_tag( $out, true, false ); |
|
| 621 | + $out = $this->process_picture_tag($out, true, false); |
|
| 622 | 622 | } |
| 623 | 623 | |
| 624 | 624 | return $out; |
| 625 | 625 | } |
| 626 | 626 | |
| 627 | - public function get_size_from_tag( $tag ) { |
|
| 627 | + public function get_size_from_tag($tag) { |
|
| 628 | 628 | // reusable function to extract widht and height from an image tag |
| 629 | 629 | // enforcing a filterable maximum width and height (default 4999X4999). |
| 630 | 630 | $width = ''; |
| 631 | 631 | $height = ''; |
| 632 | 632 | |
| 633 | - if ( preg_match( '#width=("|\')(.*)("|\')#Usmi', $tag, $_width ) ) { |
|
| 634 | - if ( strpos( $_width[2], '%' ) === false ) { |
|
| 633 | + if (preg_match('#width=("|\')(.*)("|\')#Usmi', $tag, $_width)) { |
|
| 634 | + if (strpos($_width[2], '%') === false) { |
|
| 635 | 635 | $width = (int) $_width[2]; |
| 636 | 636 | } |
| 637 | 637 | } |
| 638 | - if ( preg_match( '#height=("|\')(.*)("|\')#Usmi', $tag, $_height ) ) { |
|
| 639 | - if ( strpos( $_height[2], '%' ) === false ) { |
|
| 638 | + if (preg_match('#height=("|\')(.*)("|\')#Usmi', $tag, $_height)) { |
|
| 639 | + if (strpos($_height[2], '%') === false) { |
|
| 640 | 640 | $height = (int) $_height[2]; |
| 641 | 641 | } |
| 642 | 642 | } |
| 643 | 643 | |
| 644 | 644 | // check for and enforce (filterable) max sizes. |
| 645 | - $_max_width = apply_filters( 'autoptimize_filter_imgopt_max_width', 4999 ); |
|
| 646 | - if ( $width > $_max_width ) { |
|
| 645 | + $_max_width = apply_filters('autoptimize_filter_imgopt_max_width', 4999); |
|
| 646 | + if ($width > $_max_width) { |
|
| 647 | 647 | $_width = $_max_width; |
| 648 | - $height = $_width / $width * $height; |
|
| 648 | + $height = $_width/$width*$height; |
|
| 649 | 649 | $width = $_width; |
| 650 | 650 | } |
| 651 | - $_max_height = apply_filters( 'autoptimize_filter_imgopt_max_height', 4999 ); |
|
| 652 | - if ( $height > $_max_height ) { |
|
| 651 | + $_max_height = apply_filters('autoptimize_filter_imgopt_max_height', 4999); |
|
| 652 | + if ($height > $_max_height) { |
|
| 653 | 653 | $_height = $_max_height; |
| 654 | - $width = $_height / $height * $width; |
|
| 654 | + $width = $_height/$height*$width; |
|
| 655 | 655 | $height = $_height; |
| 656 | 656 | } |
| 657 | 657 | |
@@ -665,12 +665,12 @@ discard block |
||
| 665 | 665 | * Lazyload functions |
| 666 | 666 | */ |
| 667 | 667 | public function should_lazyload() { |
| 668 | - if ( ! empty( $this->options['autoptimize_imgopt_checkbox_field_3'] ) ) { |
|
| 668 | + if (!empty($this->options['autoptimize_imgopt_checkbox_field_3'])) { |
|
| 669 | 669 | $lazyload_return = true; |
| 670 | 670 | } else { |
| 671 | 671 | $lazyload_return = false; |
| 672 | 672 | } |
| 673 | - $lazyload_return = apply_filters( 'autoptimize_filter_imgopt_should_lazyload', $lazyload_return ); |
|
| 673 | + $lazyload_return = apply_filters('autoptimize_filter_imgopt_should_lazyload', $lazyload_return); |
|
| 674 | 674 | |
| 675 | 675 | return $lazyload_return; |
| 676 | 676 | } |
@@ -681,7 +681,7 @@ discard block |
||
| 681 | 681 | return $self->should_lazyload(); |
| 682 | 682 | } |
| 683 | 683 | |
| 684 | - public function filter_lazyload_images( $in ) |
|
| 684 | + public function filter_lazyload_images($in) |
|
| 685 | 685 | { |
| 686 | 686 | // only used is image optimization is NOT active but lazyload is. |
| 687 | 687 | $to_replace = array(); |
@@ -695,15 +695,15 @@ discard block |
||
| 695 | 695 | ); |
| 696 | 696 | |
| 697 | 697 | // extract img tags and add lazyload attribs. |
| 698 | - if ( preg_match_all( '#<img[^>]*src[^>]*>#Usmi', $out, $matches ) ) { |
|
| 699 | - foreach ( $matches[0] as $tag ) { |
|
| 700 | - $to_replace[ $tag ] = $this->add_lazyload( $tag ); |
|
| 698 | + if (preg_match_all('#<img[^>]*src[^>]*>#Usmi', $out, $matches)) { |
|
| 699 | + foreach ($matches[0] as $tag) { |
|
| 700 | + $to_replace[$tag] = $this->add_lazyload($tag); |
|
| 701 | 701 | } |
| 702 | - $out = str_replace( array_keys( $to_replace ), array_values( $to_replace ), $out ); |
|
| 702 | + $out = str_replace(array_keys($to_replace), array_values($to_replace), $out); |
|
| 703 | 703 | } |
| 704 | 704 | |
| 705 | 705 | // and also lazyload picture tag. |
| 706 | - $out = $this->process_picture_tag( $out, false, true ); |
|
| 706 | + $out = $this->process_picture_tag($out, false, true); |
|
| 707 | 707 | |
| 708 | 708 | // restore noscript tags. |
| 709 | 709 | $out = autoptimizeBase::restore_marked_content( |
@@ -714,42 +714,42 @@ discard block |
||
| 714 | 714 | return $out; |
| 715 | 715 | } |
| 716 | 716 | |
| 717 | - public function add_lazyload( $tag ) { |
|
| 717 | + public function add_lazyload($tag) { |
|
| 718 | 718 | // adds actual lazyload-attributes to an image node. |
| 719 | - if ( str_ireplace( $this->get_lazyload_exclusions(), '', $tag ) === $tag ) { |
|
| 720 | - $tag = $this->maybe_fix_missing_quotes( $tag ); |
|
| 719 | + if (str_ireplace($this->get_lazyload_exclusions(), '', $tag) === $tag) { |
|
| 720 | + $tag = $this->maybe_fix_missing_quotes($tag); |
|
| 721 | 721 | |
| 722 | 722 | // store original tag for use in noscript version. |
| 723 | - $noscript_tag = '<noscript>' . $tag . '</noscript>'; |
|
| 723 | + $noscript_tag = '<noscript>'.$tag.'</noscript>'; |
|
| 724 | 724 | |
| 725 | 725 | // insert lazyload class. |
| 726 | - $tag = $this->inject_classes_in_tag( $tag, 'lazyload ' ); |
|
| 726 | + $tag = $this->inject_classes_in_tag($tag, 'lazyload '); |
|
| 727 | 727 | |
| 728 | 728 | // get image width & heigth for placeholder fun (and to prevent content reflow). |
| 729 | - $_get_size = $this->get_size_from_tag( $tag ); |
|
| 729 | + $_get_size = $this->get_size_from_tag($tag); |
|
| 730 | 730 | $width = $_get_size['width']; |
| 731 | 731 | $height = $_get_size['height']; |
| 732 | - if ( false === $width ) { |
|
| 732 | + if (false === $width) { |
|
| 733 | 733 | $widht = 210; // default width for SVG placeholder. |
| 734 | 734 | } |
| 735 | - if ( false === $height ) { |
|
| 736 | - $heigth = $width / 3 * 2; // if no height, base it on width using the 3/2 aspect ratio. |
|
| 735 | + if (false === $height) { |
|
| 736 | + $heigth = $width/3*2; // if no height, base it on width using the 3/2 aspect ratio. |
|
| 737 | 737 | } |
| 738 | 738 | |
| 739 | 739 | // insert the actual lazyload stuff. |
| 740 | 740 | // see https://css-tricks.com/preventing-content-reflow-from-lazy-loaded-images/ for great read on why we're using empty svg's. |
| 741 | - $placeholder = apply_filters( 'autoptimize_filter_imgopt_lazyload_placeholder', $this->get_default_lazyload_placeholder( $width, $height ) ); |
|
| 742 | - $tag = str_replace( ' src=', ' src=\'' . $placeholder . '\' data-src=', $tag ); |
|
| 743 | - $tag = str_replace( ' srcset=', ' data-srcset=', $tag ); |
|
| 741 | + $placeholder = apply_filters('autoptimize_filter_imgopt_lazyload_placeholder', $this->get_default_lazyload_placeholder($width, $height)); |
|
| 742 | + $tag = str_replace(' src=', ' src=\''.$placeholder.'\' data-src=', $tag); |
|
| 743 | + $tag = str_replace(' srcset=', ' data-srcset=', $tag); |
|
| 744 | 744 | |
| 745 | 745 | // move sizes to data-sizes unless filter says no. |
| 746 | - if ( apply_filters( 'autoptimize_filter_imgopt_lazyload_move_sizes', true ) ) { |
|
| 747 | - $tag = str_replace( 'sizes=', 'data-sizes=', $tag ); |
|
| 746 | + if (apply_filters('autoptimize_filter_imgopt_lazyload_move_sizes', true)) { |
|
| 747 | + $tag = str_replace('sizes=', 'data-sizes=', $tag); |
|
| 748 | 748 | } |
| 749 | 749 | |
| 750 | 750 | // add the noscript-tag from earlier. |
| 751 | - $tag = $noscript_tag . $tag; |
|
| 752 | - $tag = apply_filters( 'autoptimize_filter_imgopt_lazyloaded_img', $tag ); |
|
| 751 | + $tag = $noscript_tag.$tag; |
|
| 752 | + $tag = apply_filters('autoptimize_filter_imgopt_lazyloaded_img', $tag); |
|
| 753 | 753 | } |
| 754 | 754 | |
| 755 | 755 | return $tag; |
@@ -758,20 +758,20 @@ discard block |
||
| 758 | 758 | public function add_lazyload_js_footer() { |
| 759 | 759 | // The JS will by default be excluded form autoptimization but this can be changed with a filter. |
| 760 | 760 | $noptimize_flag = ''; |
| 761 | - if ( apply_filters( 'autoptimize_filter_imgopt_lazyload_js_noptimize', true ) ) { |
|
| 761 | + if (apply_filters('autoptimize_filter_imgopt_lazyload_js_noptimize', true)) { |
|
| 762 | 762 | $noptimize_flag = ' data-noptimize="1"'; |
| 763 | 763 | } |
| 764 | 764 | |
| 765 | 765 | // Adds lazyload CSS & JS to footer, using echo because wp_enqueue_script seems not to support pushing attributes (async). |
| 766 | - echo apply_filters( 'autoptimize_filter_imgopt_lazyload_cssoutput', '<style>.lazyload,.lazyloading{opacity:0;}.lazyloaded{opacity:1;transition:opacity 300ms;}</style><noscript><style>.lazyload{display:none;}</style></noscript>' ); |
|
| 767 | - echo apply_filters( 'autoptimize_filter_imgopt_lazyload_jsconfig', '<script' . $noptimize_flag . '>window.lazySizesConfig=window.lazySizesConfig||{};window.lazySizesConfig.loadMode=1;</script>' ); |
|
| 768 | - echo '<script async' . $noptimize_flag . ' src=\'' . plugins_url( 'external/js/lazysizes.min.js', __FILE__ ) . '\'></script>'; |
|
| 766 | + echo apply_filters('autoptimize_filter_imgopt_lazyload_cssoutput', '<style>.lazyload,.lazyloading{opacity:0;}.lazyloaded{opacity:1;transition:opacity 300ms;}</style><noscript><style>.lazyload{display:none;}</style></noscript>'); |
|
| 767 | + echo apply_filters('autoptimize_filter_imgopt_lazyload_jsconfig', '<script'.$noptimize_flag.'>window.lazySizesConfig=window.lazySizesConfig||{};window.lazySizesConfig.loadMode=1;</script>'); |
|
| 768 | + echo '<script async'.$noptimize_flag.' src=\''.plugins_url('external/js/lazysizes.min.js', __FILE__).'\'></script>'; |
|
| 769 | 769 | |
| 770 | 770 | // And add webp detection and loading JS. |
| 771 | - if ( $this->should_webp() ) { |
|
| 771 | + if ($this->should_webp()) { |
|
| 772 | 772 | $_webp_detect = "function c_webp(A){var n=new Image;n.onload=function(){var e=0<n.width&&0<n.height;A(e)},n.onerror=function(){A(!1)},n.src='data:image/webp;base64,UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA=='}function s_webp(e){window.supportsWebP=e}c_webp(s_webp);"; |
| 773 | 773 | $_webp_load = "document.addEventListener('lazybeforeunveil',function({target:c}){supportsWebP&&['data-src','data-srcset'].forEach(function(a){attr=c.getAttribute(a),null!==attr&&c.setAttribute(a,attr.replace(/\/client\//,'/client/to_webp,'))})});"; |
| 774 | - echo apply_filters( 'autoptimize_filter_imgopt_webp_js', '<script' . $noptimize_flag . '>' . $_webp_detect . $_webp_load . '</script>' ); |
|
| 774 | + echo apply_filters('autoptimize_filter_imgopt_webp_js', '<script'.$noptimize_flag.'>'.$_webp_detect.$_webp_load.'</script>'); |
|
| 775 | 775 | } |
| 776 | 776 | } |
| 777 | 777 | |
@@ -779,47 +779,47 @@ discard block |
||
| 779 | 779 | // returns array of strings that if found in an <img tag will stop the img from being lazy-loaded. |
| 780 | 780 | static $exclude_lazyload_array = null; |
| 781 | 781 | |
| 782 | - if ( null === $exclude_lazyload_array ) { |
|
| 782 | + if (null === $exclude_lazyload_array) { |
|
| 783 | 783 | $options = $this->options; |
| 784 | 784 | |
| 785 | 785 | // set default exclusions. |
| 786 | - $exclude_lazyload_array = array( 'skip-lazy', 'data-no-lazy', 'notlazy', 'data-src', 'data-srcset', 'data:image/', 'data-lazyload', 'rev-slidebg' ); |
|
| 786 | + $exclude_lazyload_array = array('skip-lazy', 'data-no-lazy', 'notlazy', 'data-src', 'data-srcset', 'data:image/', 'data-lazyload', 'rev-slidebg'); |
|
| 787 | 787 | |
| 788 | 788 | // add from setting. |
| 789 | - if ( array_key_exists( 'autoptimize_imgopt_text_field_5', $options ) ) { |
|
| 789 | + if (array_key_exists('autoptimize_imgopt_text_field_5', $options)) { |
|
| 790 | 790 | $exclude_lazyload_option = $options['autoptimize_imgopt_text_field_5']; |
| 791 | - if ( ! empty( $exclude_lazyload_option ) ) { |
|
| 792 | - $exclude_lazyload_array = array_merge( $exclude_lazyload_array, array_filter( array_map( 'trim', explode( ',', $options['autoptimize_imgopt_text_field_5'] ) ) ) ); |
|
| 791 | + if (!empty($exclude_lazyload_option)) { |
|
| 792 | + $exclude_lazyload_array = array_merge($exclude_lazyload_array, array_filter(array_map('trim', explode(',', $options['autoptimize_imgopt_text_field_5'])))); |
|
| 793 | 793 | } |
| 794 | 794 | } |
| 795 | 795 | |
| 796 | 796 | // and filter for developer-initiated changes. |
| 797 | - $exclude_lazyload_array = apply_filters( 'autoptimize_filter_imgopt_lazyload_exclude_array', $exclude_lazyload_array ); |
|
| 797 | + $exclude_lazyload_array = apply_filters('autoptimize_filter_imgopt_lazyload_exclude_array', $exclude_lazyload_array); |
|
| 798 | 798 | } |
| 799 | 799 | |
| 800 | 800 | return $exclude_lazyload_array; |
| 801 | 801 | } |
| 802 | 802 | |
| 803 | - public function inject_classes_in_tag( $tag, $target_class ) { |
|
| 804 | - if ( strpos( $tag, 'class=' ) !== false ) { |
|
| 805 | - $tag = preg_replace( '/(\sclass\s?=\s?("|\'))/', '$1' . $target_class, $tag ); |
|
| 803 | + public function inject_classes_in_tag($tag, $target_class) { |
|
| 804 | + if (strpos($tag, 'class=') !== false) { |
|
| 805 | + $tag = preg_replace('/(\sclass\s?=\s?("|\'))/', '$1'.$target_class, $tag); |
|
| 806 | 806 | } else { |
| 807 | - $tag = str_replace( '<img ', '<img class="' . trim( $target_class ) . '" ', $tag ); |
|
| 807 | + $tag = str_replace('<img ', '<img class="'.trim($target_class).'" ', $tag); |
|
| 808 | 808 | } |
| 809 | 809 | |
| 810 | 810 | return $tag; |
| 811 | 811 | } |
| 812 | 812 | |
| 813 | - public function get_default_lazyload_placeholder( $imgopt_w, $imgopt_h ) { |
|
| 814 | - return 'data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220%200%20' . $imgopt_w . '%20' . $imgopt_h . '%22%3E%3C/svg%3E'; |
|
| 813 | + public function get_default_lazyload_placeholder($imgopt_w, $imgopt_h) { |
|
| 814 | + return 'data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220%200%20'.$imgopt_w.'%20'.$imgopt_h.'%22%3E%3C/svg%3E'; |
|
| 815 | 815 | } |
| 816 | 816 | |
| 817 | 817 | public function should_webp() { |
| 818 | 818 | static $webp_return = null; |
| 819 | 819 | |
| 820 | - if ( is_null( $webp_return ) ) { |
|
| 820 | + if (is_null($webp_return)) { |
|
| 821 | 821 | // webp only works if imgopt and lazyload are also active. |
| 822 | - if ( ! empty( $this->options['autoptimize_imgopt_checkbox_field_4'] ) && ! empty( $this->options['autoptimize_imgopt_checkbox_field_3'] ) && $this->imgopt_active() ) { |
|
| 822 | + if (!empty($this->options['autoptimize_imgopt_checkbox_field_4']) && !empty($this->options['autoptimize_imgopt_checkbox_field_3']) && $this->imgopt_active()) { |
|
| 823 | 823 | $webp_return = true; |
| 824 | 824 | } else { |
| 825 | 825 | $webp_return = false; |
@@ -829,9 +829,9 @@ discard block |
||
| 829 | 829 | return $webp_return; |
| 830 | 830 | } |
| 831 | 831 | |
| 832 | - public function process_picture_tag( $in, $imgopt = false, $lazy = false ) { |
|
| 832 | + public function process_picture_tag($in, $imgopt = false, $lazy = false) { |
|
| 833 | 833 | // check if "<picture" is present and if filter allows us to process <picture>. |
| 834 | - if ( strpos( $in, '<picture' ) === false || apply_filters( 'autoptimize_filter_imgopt_dopicture', true ) === false ) { |
|
| 834 | + if (strpos($in, '<picture') === false || apply_filters('autoptimize_filter_imgopt_dopicture', true) === false) { |
|
| 835 | 835 | return $in; |
| 836 | 836 | } |
| 837 | 837 | |
@@ -839,36 +839,36 @@ discard block |
||
| 839 | 839 | $to_replace_pict = array(); |
| 840 | 840 | |
| 841 | 841 | // extract and process each picture-node. |
| 842 | - preg_match_all( '#<picture.*</picture>#Usmi', $in, $_pictures, PREG_SET_ORDER ); |
|
| 843 | - foreach ( $_pictures as $_picture ) { |
|
| 844 | - $_picture = $this->maybe_fix_missing_quotes( $_picture ); |
|
| 845 | - if ( strpos( $_picture[0], '<source ' ) !== false && preg_match_all( '#<source .*srcset=(?:"|\')(?!data)(.*)(?:"|\').*>#Usmi', $_picture[0], $_sources, PREG_SET_ORDER ) !== false ) { |
|
| 846 | - foreach ( $_sources as $_source ) { |
|
| 842 | + preg_match_all('#<picture.*</picture>#Usmi', $in, $_pictures, PREG_SET_ORDER); |
|
| 843 | + foreach ($_pictures as $_picture) { |
|
| 844 | + $_picture = $this->maybe_fix_missing_quotes($_picture); |
|
| 845 | + if (strpos($_picture[0], '<source ') !== false && preg_match_all('#<source .*srcset=(?:"|\')(?!data)(.*)(?:"|\').*>#Usmi', $_picture[0], $_sources, PREG_SET_ORDER) !== false) { |
|
| 846 | + foreach ($_sources as $_source) { |
|
| 847 | 847 | $_picture_replacement = $_source[0]; |
| 848 | 848 | |
| 849 | 849 | // should we optimize the image? |
| 850 | - if ( $imgopt && $this->can_optimize_image( $_source[1] ) ) { |
|
| 851 | - $_picture_replacement = str_replace( $_source[1], $this->build_imgopt_url( $_source[1] ), $_picture_replacement ); |
|
| 850 | + if ($imgopt && $this->can_optimize_image($_source[1])) { |
|
| 851 | + $_picture_replacement = str_replace($_source[1], $this->build_imgopt_url($_source[1]), $_picture_replacement); |
|
| 852 | 852 | } |
| 853 | 853 | // should we lazy-load? |
| 854 | - if ( $lazy && str_ireplace( $_exclusions, '', $_picture_replacement ) === $_picture_replacement ) { |
|
| 855 | - $_picture_replacement = str_replace( ' srcset=', ' data-srcset=', $_picture_replacement ); |
|
| 854 | + if ($lazy && str_ireplace($_exclusions, '', $_picture_replacement) === $_picture_replacement) { |
|
| 855 | + $_picture_replacement = str_replace(' srcset=', ' data-srcset=', $_picture_replacement); |
|
| 856 | 856 | } |
| 857 | - $to_replace_pict[ $_source[0] ] = $_picture_replacement; |
|
| 857 | + $to_replace_pict[$_source[0]] = $_picture_replacement; |
|
| 858 | 858 | } |
| 859 | 859 | } |
| 860 | 860 | } |
| 861 | 861 | |
| 862 | 862 | // and return the fully procesed $in. |
| 863 | - $out = str_replace( array_keys( $to_replace_pict ), array_values( $to_replace_pict ), $in ); |
|
| 863 | + $out = str_replace(array_keys($to_replace_pict), array_values($to_replace_pict), $in); |
|
| 864 | 864 | |
| 865 | 865 | return $out; |
| 866 | 866 | } |
| 867 | 867 | |
| 868 | - public function maybe_fix_missing_quotes( $tag_in ) { |
|
| 868 | + public function maybe_fix_missing_quotes($tag_in) { |
|
| 869 | 869 | // W3TC's Minify_HTML class removes quotes around attribute value, this re-adds them. |
| 870 | - if ( file_exists( WP_PLUGIN_DIR . '/w3-total-cache/w3-total-cache.php' ) && class_exists( 'Minify_HTML' ) && apply_filters( 'autoptimize_filter_imgopt_fixquotes', true ) ) { |
|
| 871 | - return preg_replace( '/=([^("|\')]*)(\s|>)/U', '=\'$1\'$2', $tag_in ); |
|
| 870 | + if (file_exists(WP_PLUGIN_DIR.'/w3-total-cache/w3-total-cache.php') && class_exists('Minify_HTML') && apply_filters('autoptimize_filter_imgopt_fixquotes', true)) { |
|
| 871 | + return preg_replace('/=([^("|\')]*)(\s|>)/U', '=\'$1\'$2', $tag_in); |
|
| 872 | 872 | } else { |
| 873 | 873 | return $tag_in; |
| 874 | 874 | } |
@@ -885,14 +885,14 @@ discard block |
||
| 885 | 885 | 'autoptimize_imgopt', |
| 886 | 886 | 'manage_options', |
| 887 | 887 | 'autoptimize_imgopt', |
| 888 | - array( $this, 'imgopt_options_page' ) |
|
| 888 | + array($this, 'imgopt_options_page') |
|
| 889 | 889 | ); |
| 890 | - register_setting( 'autoptimize_imgopt_settings', 'autoptimize_imgopt_settings' ); |
|
| 890 | + register_setting('autoptimize_imgopt_settings', 'autoptimize_imgopt_settings'); |
|
| 891 | 891 | } |
| 892 | 892 | |
| 893 | - public function add_imgopt_tab( $in ) |
|
| 893 | + public function add_imgopt_tab($in) |
|
| 894 | 894 | { |
| 895 | - $in = array_merge( $in, array( 'autoptimize_imgopt' => __( 'Images', 'autoptimize' ) ) ); |
|
| 895 | + $in = array_merge($in, array('autoptimize_imgopt' => __('Images', 'autoptimize'))); |
|
| 896 | 896 | |
| 897 | 897 | return $in; |
| 898 | 898 | } |
@@ -900,7 +900,7 @@ discard block |
||
| 900 | 900 | public function imgopt_options_page() |
| 901 | 901 | { |
| 902 | 902 | // Check querystring for "refreshCacheChecker" and call cachechecker if so. |
| 903 | - if ( array_key_exists( 'refreshImgProvStats', $_GET ) && 1 == $_GET['refreshImgProvStats'] ) { |
|
| 903 | + if (array_key_exists('refreshImgProvStats', $_GET) && 1 == $_GET['refreshImgProvStats']) { |
|
| 904 | 904 | $this->query_img_provider_stats(); |
| 905 | 905 | } |
| 906 | 906 | |
@@ -913,45 +913,45 @@ discard block |
||
| 913 | 913 | #autoptimize_imgopt_descr{font-size: 120%;} |
| 914 | 914 | </style> |
| 915 | 915 | <div class="wrap"> |
| 916 | - <h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1> |
|
| 916 | + <h1><?php _e('Autoptimize Settings', 'autoptimize'); ?></h1> |
|
| 917 | 917 | <?php echo autoptimizeConfig::ao_admin_tabs(); ?> |
| 918 | - <?php if ( 'down' === $options['availabilities']['extra_imgopt']['status'] ) { ?> |
|
| 918 | + <?php if ('down' === $options['availabilities']['extra_imgopt']['status']) { ?> |
|
| 919 | 919 | <div class="notice-warning notice"><p> |
| 920 | 920 | <?php |
| 921 | 921 | // translators: "Autoptimize support forum" will appear in a "a href". |
| 922 | - echo sprintf( __( 'The image optimization service is currently down, image optimization will be skipped until further notice. Check the %1$sAutoptimize support forum%2$s for more info.', 'autoptimize' ), '<a href="https://wordpress.org/support/plugin/autoptimize/" target="_blank">', '</a>' ); |
|
| 922 | + echo sprintf(__('The image optimization service is currently down, image optimization will be skipped until further notice. Check the %1$sAutoptimize support forum%2$s for more info.', 'autoptimize'), '<a href="https://wordpress.org/support/plugin/autoptimize/" target="_blank">', '</a>'); |
|
| 923 | 923 | ?> |
| 924 | 924 | </p></div> |
| 925 | 925 | <?php } ?> |
| 926 | 926 | |
| 927 | - <?php if ( 'launch' === $options['availabilities']['extra_imgopt']['status'] && ! autoptimizeImages::instance()->launch_ok() ) { ?> |
|
| 927 | + <?php if ('launch' === $options['availabilities']['extra_imgopt']['status'] && !autoptimizeImages::instance()->launch_ok()) { ?> |
|
| 928 | 928 | <div class="notice-warning notice"><p> |
| 929 | - <?php _e( 'The image optimization service is launching, but not yet available for this domain, it should become available in the next couple of days.', 'autoptimize' ); ?> |
|
| 929 | + <?php _e('The image optimization service is launching, but not yet available for this domain, it should become available in the next couple of days.', 'autoptimize'); ?> |
|
| 930 | 930 | </p></div> |
| 931 | 931 | <?php } ?> |
| 932 | 932 | |
| 933 | - <?php if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'get_active_modules' ) && in_array( 'photon', Jetpack::get_active_modules() ) ) { ?> |
|
| 933 | + <?php if (class_exists('Jetpack') && method_exists('Jetpack', 'get_active_modules') && in_array('photon', Jetpack::get_active_modules())) { ?> |
|
| 934 | 934 | <div class="notice-warning notice"><p> |
| 935 | 935 | <?php |
| 936 | 936 | // translators: "disable Jetpack's site accelerator for images" will appear in a "a href" linking to the jetpack settings page. |
| 937 | - echo sprintf( __( 'Please %1$sdisable Jetpack\'s site accelerator for images%2$s to be able to use Autoptomize\'s advanced image optimization features below.', 'autoptimize' ), '<a href="admin.php?page=jetpack#/settings">', '</a>' ); |
|
| 937 | + echo sprintf(__('Please %1$sdisable Jetpack\'s site accelerator for images%2$s to be able to use Autoptomize\'s advanced image optimization features below.', 'autoptimize'), '<a href="admin.php?page=jetpack#/settings">', '</a>'); |
|
| 938 | 938 | ?> |
| 939 | 939 | </p></div> |
| 940 | 940 | <?php } ?> |
| 941 | 941 | <form id='ao_settings_form' action='options.php' method='post'> |
| 942 | - <?php settings_fields( 'autoptimize_imgopt_settings' ); ?> |
|
| 943 | - <h2><?php _e( 'Image optimization', 'autoptimize' ); ?></h2> |
|
| 944 | - <span id='autoptimize_imgopt_descr'><?php _e( 'Make your site significantly faster by just ticking a couple of checkboxes to optimize and lazy load your images, WebP support included!', 'autoptimize' ); ?></span> |
|
| 942 | + <?php settings_fields('autoptimize_imgopt_settings'); ?> |
|
| 943 | + <h2><?php _e('Image optimization', 'autoptimize'); ?></h2> |
|
| 944 | + <span id='autoptimize_imgopt_descr'><?php _e('Make your site significantly faster by just ticking a couple of checkboxes to optimize and lazy load your images, WebP support included!', 'autoptimize'); ?></span> |
|
| 945 | 945 | <table class="form-table"> |
| 946 | 946 | <tr> |
| 947 | - <th scope="row"><?php _e( 'Optimize Images', 'autoptimize' ); ?></th> |
|
| 947 | + <th scope="row"><?php _e('Optimize Images', 'autoptimize'); ?></th> |
|
| 948 | 948 | <td> |
| 949 | - <label><input id='autoptimize_imgopt_checkbox' type='checkbox' name='autoptimize_imgopt_settings[autoptimize_imgopt_checkbox_field_1]' <?php if ( ! empty( $options['autoptimize_imgopt_checkbox_field_1'] ) && '1' === $options['autoptimize_imgopt_checkbox_field_1'] ) { echo 'checked="checked"'; } ?> value='1'><?php _e( 'Optimize images on the fly and serve them from Shortpixel\'s global CDN.', 'autoptimize' ); ?></label> |
|
| 949 | + <label><input id='autoptimize_imgopt_checkbox' type='checkbox' name='autoptimize_imgopt_settings[autoptimize_imgopt_checkbox_field_1]' <?php if (!empty($options['autoptimize_imgopt_checkbox_field_1']) && '1' === $options['autoptimize_imgopt_checkbox_field_1']) { echo 'checked="checked"'; } ?> value='1'><?php _e('Optimize images on the fly and serve them from Shortpixel\'s global CDN.', 'autoptimize'); ?></label> |
|
| 950 | 950 | <?php |
| 951 | 951 | // show shortpixel status. |
| 952 | 952 | $_notice = autoptimizeImages::instance()->get_status_notice(); |
| 953 | - if ( $_notice ) { |
|
| 954 | - switch ( $_notice['status'] ) { |
|
| 953 | + if ($_notice) { |
|
| 954 | + switch ($_notice['status']) { |
|
| 955 | 955 | case 2: |
| 956 | 956 | $_notice_color = 'green'; |
| 957 | 957 | break; |
@@ -967,28 +967,28 @@ discard block |
||
| 967 | 967 | default: |
| 968 | 968 | $_notice_color = 'green'; |
| 969 | 969 | } |
| 970 | - echo apply_filters( 'autoptimize_filter_imgopt_settings_status', '<p><strong><span style="color:' . $_notice_color . ';">' . __( 'Shortpixel status: ', 'autoptimize' ) . '</span></strong>' . $_notice['notice'] . '</p>' ); |
|
| 970 | + echo apply_filters('autoptimize_filter_imgopt_settings_status', '<p><strong><span style="color:'.$_notice_color.';">'.__('Shortpixel status: ', 'autoptimize').'</span></strong>'.$_notice['notice'].'</p>'); |
|
| 971 | 971 | } else { |
| 972 | 972 | // translators: link points to shortpixel. |
| 973 | - $upsell_msg_1 = '<p>' . sprintf( __( 'Get more Google love and improve your website\'s loading speed by having the images optimized on the fly (also in the "next-gen" WebP image format) by %1$sShortPixel%2$s and then cached and served fast from Shortpixel\'s global CDN.', 'autoptimize' ), '<a href="https://shortpixel.com/aospai' . $sp_url_suffix . '" target="_blank">', '</a>' ); |
|
| 974 | - if ( 'launch' === $options['availabilities']['extra_imgopt']['status'] ) { |
|
| 975 | - $upsell_msg_2 = __( 'For a limited time only, this service is offered free for all Autoptimize users, <b>don\'t miss the chance to test it</b> and see how much it could improve your site\'s speed.', 'autoptimize' ); |
|
| 973 | + $upsell_msg_1 = '<p>'.sprintf(__('Get more Google love and improve your website\'s loading speed by having the images optimized on the fly (also in the "next-gen" WebP image format) by %1$sShortPixel%2$s and then cached and served fast from Shortpixel\'s global CDN.', 'autoptimize'), '<a href="https://shortpixel.com/aospai'.$sp_url_suffix.'" target="_blank">', '</a>'); |
|
| 974 | + if ('launch' === $options['availabilities']['extra_imgopt']['status']) { |
|
| 975 | + $upsell_msg_2 = __('For a limited time only, this service is offered free for all Autoptimize users, <b>don\'t miss the chance to test it</b> and see how much it could improve your site\'s speed.', 'autoptimize'); |
|
| 976 | 976 | } else { |
| 977 | 977 | // translators: link points to shortpixel. |
| 978 | - $upsell_msg_2 = sprintf( __( '%1$sSign-up now%2$s to receive a 1 000 bonus + 50% more image optimization credits regardless of the traffic used. More image optimizations can be purchased starting with $4.99.', 'autoptimize' ), '<a href="https://shortpixel.com/aospai' . $sp_url_suffix . '" target="_blank">', '</a>' ); |
|
| 978 | + $upsell_msg_2 = sprintf(__('%1$sSign-up now%2$s to receive a 1 000 bonus + 50% more image optimization credits regardless of the traffic used. More image optimizations can be purchased starting with $4.99.', 'autoptimize'), '<a href="https://shortpixel.com/aospai'.$sp_url_suffix.'" target="_blank">', '</a>'); |
|
| 979 | 979 | } |
| 980 | - echo apply_filters( 'autoptimize_imgopt_imgopt_settings_copy', $upsell_msg_1 . ' ' . $upsell_msg_2 . '</p>' ); |
|
| 980 | + echo apply_filters('autoptimize_imgopt_imgopt_settings_copy', $upsell_msg_1.' '.$upsell_msg_2.'</p>'); |
|
| 981 | 981 | } |
| 982 | 982 | // translators: link points to shortpixel FAQ. |
| 983 | - $faqcopy = sprintf( __( '<strong>Questions</strong>? Have a look at the %1$sShortPixel FAQ%2$s!', 'autoptimize' ), '<strong><a href="https://shortpixel.helpscoutdocs.com/category/60-shortpixel-ai-cdn" target="_blank">', '</strong></a>' ); |
|
| 983 | + $faqcopy = sprintf(__('<strong>Questions</strong>? Have a look at the %1$sShortPixel FAQ%2$s!', 'autoptimize'), '<strong><a href="https://shortpixel.helpscoutdocs.com/category/60-shortpixel-ai-cdn" target="_blank">', '</strong></a>'); |
|
| 984 | 984 | // translators: links points to shortpixel TOS & Privacy Policy. |
| 985 | - $toscopy = sprintf( __( 'Usage of this feature is subject to Shortpixel\'s %1$sTerms of Use%2$s and %3$sPrivacy policy%4$s.', 'autoptimize' ), '<a href="https://shortpixel.com/tos' . $sp_url_suffix . '" target="_blank">', '</a>', '<a href="https://shortpixel.com/pp' . $sp_url_suffix . '" target="_blank">', '</a>' ); |
|
| 986 | - echo apply_filters( 'autoptimize_imgopt_imgopt_settings_tos', '<p>' . $faqcopy . ' ' . $toscopy . '</p>' ); |
|
| 985 | + $toscopy = sprintf(__('Usage of this feature is subject to Shortpixel\'s %1$sTerms of Use%2$s and %3$sPrivacy policy%4$s.', 'autoptimize'), '<a href="https://shortpixel.com/tos'.$sp_url_suffix.'" target="_blank">', '</a>', '<a href="https://shortpixel.com/pp'.$sp_url_suffix.'" target="_blank">', '</a>'); |
|
| 986 | + echo apply_filters('autoptimize_imgopt_imgopt_settings_tos', '<p>'.$faqcopy.' '.$toscopy.'</p>'); |
|
| 987 | 987 | ?> |
| 988 | 988 | </td> |
| 989 | 989 | </tr> |
| 990 | - <tr id='autoptimize_imgopt_quality' <?php if ( ! array_key_exists( 'autoptimize_imgopt_checkbox_field_1', $options ) || ( isset( $options['autoptimize_imgopt_checkbox_field_1'] ) && '1' !== $options['autoptimize_imgopt_checkbox_field_1'] ) ) { echo 'class="hidden"'; } ?>> |
|
| 991 | - <th scope="row"><?php _e( 'Image Optimization quality', 'autoptimize' ); ?></th> |
|
| 990 | + <tr id='autoptimize_imgopt_quality' <?php if (!array_key_exists('autoptimize_imgopt_checkbox_field_1', $options) || (isset($options['autoptimize_imgopt_checkbox_field_1']) && '1' !== $options['autoptimize_imgopt_checkbox_field_1'])) { echo 'class="hidden"'; } ?>> |
|
| 991 | + <th scope="row"><?php _e('Image Optimization quality', 'autoptimize'); ?></th> |
|
| 992 | 992 | <td> |
| 993 | 993 | <label> |
| 994 | 994 | <select name='autoptimize_imgopt_settings[autoptimize_imgopt_select_field_2]'> |
@@ -996,12 +996,12 @@ discard block |
||
| 996 | 996 | $_imgopt_array = autoptimizeImages::instance()->get_img_quality_array(); |
| 997 | 997 | $_imgopt_val = autoptimizeImages::instance()->get_img_quality_setting(); |
| 998 | 998 | |
| 999 | - foreach ( $_imgopt_array as $key => $value ) { |
|
| 1000 | - echo '<option value="' . $key . '"'; |
|
| 1001 | - if ( $_imgopt_val == $key ) { |
|
| 999 | + foreach ($_imgopt_array as $key => $value) { |
|
| 1000 | + echo '<option value="'.$key.'"'; |
|
| 1001 | + if ($_imgopt_val == $key) { |
|
| 1002 | 1002 | echo ' selected'; |
| 1003 | 1003 | } |
| 1004 | - echo '>' . ucfirst( $value ) . '</option>'; |
|
| 1004 | + echo '>'.ucfirst($value).'</option>'; |
|
| 1005 | 1005 | } |
| 1006 | 1006 | echo "\n"; |
| 1007 | 1007 | ?> |
@@ -1010,31 +1010,31 @@ discard block |
||
| 1010 | 1010 | <p> |
| 1011 | 1011 | <?php |
| 1012 | 1012 | // translators: link points to shortpixel image test page. |
| 1013 | - echo apply_filters( 'autoptimize_imgopt_imgopt_quality_copy', sprintf( __( 'You can %1$stest compression levels here%2$s.', 'autoptimize' ), '<a href="https://shortpixel.com/oic' . $sp_url_suffix . '" target="_blank">', '</a>' ) ); |
|
| 1013 | + echo apply_filters('autoptimize_imgopt_imgopt_quality_copy', sprintf(__('You can %1$stest compression levels here%2$s.', 'autoptimize'), '<a href="https://shortpixel.com/oic'.$sp_url_suffix.'" target="_blank">', '</a>')); |
|
| 1014 | 1014 | ?> |
| 1015 | 1015 | </p> |
| 1016 | 1016 | </td> |
| 1017 | 1017 | </tr> |
| 1018 | - <tr id='autoptimize_imgopt_webp' <?php if ( ! array_key_exists( 'autoptimize_imgopt_checkbox_field_1', $options ) || ( isset( $options['autoptimize_imgopt_checkbox_field_1'] ) && '1' !== $options['autoptimize_imgopt_checkbox_field_1'] ) ) { echo 'class="hidden"'; } ?>> |
|
| 1019 | - <th scope="row"><?php _e( 'Load WebP in supported browsers?', 'autoptimize' ); ?></th> |
|
| 1018 | + <tr id='autoptimize_imgopt_webp' <?php if (!array_key_exists('autoptimize_imgopt_checkbox_field_1', $options) || (isset($options['autoptimize_imgopt_checkbox_field_1']) && '1' !== $options['autoptimize_imgopt_checkbox_field_1'])) { echo 'class="hidden"'; } ?>> |
|
| 1019 | + <th scope="row"><?php _e('Load WebP in supported browsers?', 'autoptimize'); ?></th> |
|
| 1020 | 1020 | <td> |
| 1021 | - <label><input type='checkbox' id='autoptimize_imgopt_webp_checkbox' name='autoptimize_imgopt_settings[autoptimize_imgopt_checkbox_field_4]' <?php if ( ! empty( $options['autoptimize_imgopt_checkbox_field_4'] ) && '1' === $options['autoptimize_imgopt_checkbox_field_3'] ) { echo 'checked="checked"'; } ?> value='1'><?php _e( 'Automatically serve "next-gen" WebP image format to any browser that supports it (requires lazy load to be active).', 'autoptimize' ); ?></label> |
|
| 1021 | + <label><input type='checkbox' id='autoptimize_imgopt_webp_checkbox' name='autoptimize_imgopt_settings[autoptimize_imgopt_checkbox_field_4]' <?php if (!empty($options['autoptimize_imgopt_checkbox_field_4']) && '1' === $options['autoptimize_imgopt_checkbox_field_3']) { echo 'checked="checked"'; } ?> value='1'><?php _e('Automatically serve "next-gen" WebP image format to any browser that supports it (requires lazy load to be active).', 'autoptimize'); ?></label> |
|
| 1022 | 1022 | </td> |
| 1023 | 1023 | </tr> |
| 1024 | 1024 | <tr> |
| 1025 | - <th scope="row"><?php _e( 'Lazy-load images?', 'autoptimize' ); ?></th> |
|
| 1025 | + <th scope="row"><?php _e('Lazy-load images?', 'autoptimize'); ?></th> |
|
| 1026 | 1026 | <td> |
| 1027 | - <label><input type='checkbox' id='autoptimize_imgopt_lazyload_checkbox' name='autoptimize_imgopt_settings[autoptimize_imgopt_checkbox_field_3]' <?php if ( ! empty( $options['autoptimize_imgopt_checkbox_field_3'] ) && '1' === $options['autoptimize_imgopt_checkbox_field_3'] ) { echo 'checked="checked"'; } ?> value='1'><?php _e( 'Image lazy-loading will delay the loading of non-visible images to allow the browser to optimally load all resources for the "above the fold"-page first.', 'autoptimize' ); ?></label> |
|
| 1027 | + <label><input type='checkbox' id='autoptimize_imgopt_lazyload_checkbox' name='autoptimize_imgopt_settings[autoptimize_imgopt_checkbox_field_3]' <?php if (!empty($options['autoptimize_imgopt_checkbox_field_3']) && '1' === $options['autoptimize_imgopt_checkbox_field_3']) { echo 'checked="checked"'; } ?> value='1'><?php _e('Image lazy-loading will delay the loading of non-visible images to allow the browser to optimally load all resources for the "above the fold"-page first.', 'autoptimize'); ?></label> |
|
| 1028 | 1028 | </td> |
| 1029 | 1029 | </tr> |
| 1030 | - <tr id='autoptimize_imgopt_lazyload_exclusions' <?php if ( ! array_key_exists( 'autoptimize_imgopt_checkbox_field_3', $options ) || ( isset( $options['autoptimize_imgopt_checkbox_field_3'] ) && '1' !== $options['autoptimize_imgopt_checkbox_field_3'] ) ) { echo 'class="hidden"'; } ?>> |
|
| 1031 | - <th scope="row"><?php _e( 'Lazy-load exclusions', 'autoptimize' ); ?></th> |
|
| 1030 | + <tr id='autoptimize_imgopt_lazyload_exclusions' <?php if (!array_key_exists('autoptimize_imgopt_checkbox_field_3', $options) || (isset($options['autoptimize_imgopt_checkbox_field_3']) && '1' !== $options['autoptimize_imgopt_checkbox_field_3'])) { echo 'class="hidden"'; } ?>> |
|
| 1031 | + <th scope="row"><?php _e('Lazy-load exclusions', 'autoptimize'); ?></th> |
|
| 1032 | 1032 | <td> |
| 1033 | - <label><input type='text' style='width:80%' id='autoptimize_imgopt_lazyload_exclusions' name='autoptimize_imgopt_settings[autoptimize_imgopt_text_field_5]' value='<?php if ( ! empty( $options['autoptimize_imgopt_text_field_5'] ) ) { echo esc_attr( $options['autoptimize_imgopt_text_field_5'] ); } ?>'><br /><?php _e( 'Comma-separated list of to be excluded image classes or filenames.', 'autoptimize' ); ?></label> |
|
| 1033 | + <label><input type='text' style='width:80%' id='autoptimize_imgopt_lazyload_exclusions' name='autoptimize_imgopt_settings[autoptimize_imgopt_text_field_5]' value='<?php if (!empty($options['autoptimize_imgopt_text_field_5'])) { echo esc_attr($options['autoptimize_imgopt_text_field_5']); } ?>'><br /><?php _e('Comma-separated list of to be excluded image classes or filenames.', 'autoptimize'); ?></label> |
|
| 1034 | 1034 | </td> |
| 1035 | 1035 | </tr> |
| 1036 | 1036 | </table> |
| 1037 | - <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e( 'Save Changes', 'autoptimize' ); ?>" /></p> |
|
| 1037 | + <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e('Save Changes', 'autoptimize'); ?>" /></p> |
|
| 1038 | 1038 | </form> |
| 1039 | 1039 | <script> |
| 1040 | 1040 | jQuery(document).ready(function() { |
@@ -1070,37 +1070,37 @@ discard block |
||
| 1070 | 1070 | * Ïmg opt status as used on dashboard. |
| 1071 | 1071 | */ |
| 1072 | 1072 | public function get_imgopt_status_notice() { |
| 1073 | - if ( $this->imgopt_active() ) { |
|
| 1073 | + if ($this->imgopt_active()) { |
|
| 1074 | 1074 | $_imgopt_notice = ''; |
| 1075 | - $_stat = get_option( 'autoptimize_imgopt_provider_stat', '' ); |
|
| 1075 | + $_stat = get_option('autoptimize_imgopt_provider_stat', ''); |
|
| 1076 | 1076 | $_site_host = AUTOPTIMIZE_SITE_DOMAIN; |
| 1077 | - $_imgopt_upsell = 'https://shortpixel.com/aospai/af/GWRGFLW109483/' . $_site_host; |
|
| 1077 | + $_imgopt_upsell = 'https://shortpixel.com/aospai/af/GWRGFLW109483/'.$_site_host; |
|
| 1078 | 1078 | |
| 1079 | - if ( is_array( $_stat ) ) { |
|
| 1080 | - if ( 1 == $_stat['Status'] ) { |
|
| 1079 | + if (is_array($_stat)) { |
|
| 1080 | + if (1 == $_stat['Status']) { |
|
| 1081 | 1081 | // translators: "add more credits" will appear in a "a href". |
| 1082 | - $_imgopt_notice = sprintf( __( 'Your ShortPixel image optimization and CDN quota is almost used, make sure you %1$sadd more credits%2$s to avoid slowing down your website.', 'autoptimize' ), '<a href="' . $_imgopt_upsell . '" target="_blank">', '</a>' ); |
|
| 1082 | + $_imgopt_notice = sprintf(__('Your ShortPixel image optimization and CDN quota is almost used, make sure you %1$sadd more credits%2$s to avoid slowing down your website.', 'autoptimize'), '<a href="'.$_imgopt_upsell.'" target="_blank">', '</a>'); |
|
| 1083 | 1083 | } elseif ( -1 == $_stat['Status'] || -2 == $_stat['Status'] ) { |
| 1084 | 1084 | // translators: "add more credits" will appear in a "a href". |
| 1085 | - $_imgopt_notice = sprintf( __( 'Your ShortPixel image optimization and CDN quota was used, %1$sadd more credits%2$s to keep fast serving optimized images on your site', 'autoptimize' ), '<a href="' . $_imgopt_upsell . '" target="_blank">', '</a>' ); |
|
| 1086 | - $_imgopt_stats_refresh_url = add_query_arg( array( |
|
| 1085 | + $_imgopt_notice = sprintf(__('Your ShortPixel image optimization and CDN quota was used, %1$sadd more credits%2$s to keep fast serving optimized images on your site', 'autoptimize'), '<a href="'.$_imgopt_upsell.'" target="_blank">', '</a>'); |
|
| 1086 | + $_imgopt_stats_refresh_url = add_query_arg(array( |
|
| 1087 | 1087 | 'page' => 'autoptimize_imgopt', |
| 1088 | 1088 | 'refreshImgProvStats' => '1', |
| 1089 | - ), admin_url( 'options-general.php' ) ); |
|
| 1090 | - if ( $_stat && array_key_exists( 'timestamp', $_stat ) && ! empty( $_stat['timestamp'] ) ) { |
|
| 1091 | - $_imgopt_stats_last_run = __( 'based on status at ', 'autoptimize' ) . date_i18n( get_option( 'time_format' ), $_stat['timestamp'] ); |
|
| 1089 | + ), admin_url('options-general.php')); |
|
| 1090 | + if ($_stat && array_key_exists('timestamp', $_stat) && !empty($_stat['timestamp'])) { |
|
| 1091 | + $_imgopt_stats_last_run = __('based on status at ', 'autoptimize').date_i18n(get_option('time_format'), $_stat['timestamp']); |
|
| 1092 | 1092 | } else { |
| 1093 | - $_imgopt_stats_last_run = __( 'based on previously fetched data', 'autoptimize' ); |
|
| 1093 | + $_imgopt_stats_last_run = __('based on previously fetched data', 'autoptimize'); |
|
| 1094 | 1094 | } |
| 1095 | - $_imgopt_notice .= ' (' . $_imgopt_stats_last_run . ', '; |
|
| 1095 | + $_imgopt_notice .= ' ('.$_imgopt_stats_last_run.', '; |
|
| 1096 | 1096 | // translators: "here to refresh" links to the Autoptimize Extra page and forces a refresh of the img opt stats. |
| 1097 | - $_imgopt_notice .= sprintf( __( 'click %1$shere to refresh%2$s', 'autoptimize' ), '<a href="' . $_imgopt_stats_refresh_url . '">', '</a>).' ); |
|
| 1097 | + $_imgopt_notice .= sprintf(__('click %1$shere to refresh%2$s', 'autoptimize'), '<a href="'.$_imgopt_stats_refresh_url.'">', '</a>).'); |
|
| 1098 | 1098 | } else { |
| 1099 | 1099 | $_imgopt_upsell = 'https://shortpixel.com/g/af/GWRGFLW109483'; |
| 1100 | 1100 | // translators: "log in to check your account" will appear in a "a href". |
| 1101 | - $_imgopt_notice = sprintf( __( 'Your ShortPixel image optimization and CDN quota are in good shape, %1$slog in to check your account%2$s.', 'autoptimize' ), '<a href="' . $_imgopt_upsell . '" target="_blank">', '</a>' ); |
|
| 1101 | + $_imgopt_notice = sprintf(__('Your ShortPixel image optimization and CDN quota are in good shape, %1$slog in to check your account%2$s.', 'autoptimize'), '<a href="'.$_imgopt_upsell.'" target="_blank">', '</a>'); |
|
| 1102 | 1102 | } |
| 1103 | - $_imgopt_notice = apply_filters( 'autoptimize_filter_imgopt_notice', $_imgopt_notice ); |
|
| 1103 | + $_imgopt_notice = apply_filters('autoptimize_filter_imgopt_notice', $_imgopt_notice); |
|
| 1104 | 1104 | |
| 1105 | 1105 | return array( |
| 1106 | 1106 | 'status' => $_stat['Status'], |
@@ -1121,14 +1121,14 @@ discard block |
||
| 1121 | 1121 | * Get img provider stats (used to display notice). |
| 1122 | 1122 | */ |
| 1123 | 1123 | public function query_img_provider_stats() { |
| 1124 | - if ( ! empty( $this->options['autoptimize_imgopt_checkbox_field_1'] ) ) { |
|
| 1124 | + if (!empty($this->options['autoptimize_imgopt_checkbox_field_1'])) { |
|
| 1125 | 1125 | $url = ''; |
| 1126 | - $endpoint = $this->get_imgopt_host() . 'read-domain/'; |
|
| 1126 | + $endpoint = $this->get_imgopt_host().'read-domain/'; |
|
| 1127 | 1127 | $domain = AUTOPTIMIZE_SITE_DOMAIN; |
| 1128 | 1128 | |
| 1129 | 1129 | // make sure parse_url result makes sense, keeping $url empty if not. |
| 1130 | - if ( $domain && ! empty( $domain ) ) { |
|
| 1131 | - $url = $endpoint . $domain; |
|
| 1130 | + if ($domain && !empty($domain)) { |
|
| 1131 | + $url = $endpoint.$domain; |
|
| 1132 | 1132 | } |
| 1133 | 1133 | |
| 1134 | 1134 | $url = apply_filters( |
@@ -1138,12 +1138,12 @@ discard block |
||
| 1138 | 1138 | |
| 1139 | 1139 | // only do the remote call if $url is not empty to make sure no parse_url |
| 1140 | 1140 | // weirdness results in useless calls. |
| 1141 | - if ( ! empty( $url ) ) { |
|
| 1142 | - $response = wp_remote_get( $url ); |
|
| 1143 | - if ( ! is_wp_error( $response ) ) { |
|
| 1144 | - if ( '200' == wp_remote_retrieve_response_code( $response ) ) { |
|
| 1145 | - $stats = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 1146 | - update_option( 'autoptimize_imgopt_provider_stat', $stats ); |
|
| 1141 | + if (!empty($url)) { |
|
| 1142 | + $response = wp_remote_get($url); |
|
| 1143 | + if (!is_wp_error($response)) { |
|
| 1144 | + if ('200' == wp_remote_retrieve_response_code($response)) { |
|
| 1145 | + $stats = json_decode(wp_remote_retrieve_body($response), true); |
|
| 1146 | + update_option('autoptimize_imgopt_provider_stat', $stats); |
|
| 1147 | 1147 | } |
| 1148 | 1148 | } |
| 1149 | 1149 | } |
@@ -1166,15 +1166,15 @@ discard block |
||
| 1166 | 1166 | { |
| 1167 | 1167 | static $launch_status = null; |
| 1168 | 1168 | |
| 1169 | - if ( null === $launch_status ) { |
|
| 1169 | + if (null === $launch_status) { |
|
| 1170 | 1170 | $avail_imgopt = $this->options['availabilities']['extra_imgopt']; |
| 1171 | - $magic_number = intval( substr( md5( parse_url( AUTOPTIMIZE_WP_SITE_URL, PHP_URL_HOST ) ), 0, 3 ), 16 ); |
|
| 1172 | - $has_launched = get_option( 'autoptimize_imgopt_launched', '' ); |
|
| 1171 | + $magic_number = intval(substr(md5(parse_url(AUTOPTIMIZE_WP_SITE_URL, PHP_URL_HOST)), 0, 3), 16); |
|
| 1172 | + $has_launched = get_option('autoptimize_imgopt_launched', ''); |
|
| 1173 | 1173 | $launch_status = false; |
| 1174 | - if ( $has_launched || ( is_array( $avail_imgopt ) && array_key_exists( 'launch-threshold', $avail_imgopt ) && $magic_number < $avail_imgopt['launch-threshold'] ) ) { |
|
| 1174 | + if ($has_launched || (is_array($avail_imgopt) && array_key_exists('launch-threshold', $avail_imgopt) && $magic_number < $avail_imgopt['launch-threshold'])) { |
|
| 1175 | 1175 | $launch_status = true; |
| 1176 | - if ( ! $has_launched ) { |
|
| 1177 | - update_option( 'autoptimize_imgopt_launched', 'on' ); |
|
| 1176 | + if (!$has_launched) { |
|
| 1177 | + update_option('autoptimize_imgopt_launched', 'on'); |
|
| 1178 | 1178 | } |
| 1179 | 1179 | } |
| 1180 | 1180 | } |
@@ -1191,16 +1191,16 @@ discard block |
||
| 1191 | 1191 | public function get_imgopt_provider_userstatus() { |
| 1192 | 1192 | static $_provider_userstatus = null; |
| 1193 | 1193 | |
| 1194 | - if ( is_null( $_provider_userstatus ) ) { |
|
| 1195 | - $_stat = get_option( 'autoptimize_imgopt_provider_stat', '' ); |
|
| 1196 | - if ( is_array( $_stat ) ) { |
|
| 1197 | - if ( array_key_exists( 'Status', $_stat ) ) { |
|
| 1194 | + if (is_null($_provider_userstatus)) { |
|
| 1195 | + $_stat = get_option('autoptimize_imgopt_provider_stat', ''); |
|
| 1196 | + if (is_array($_stat)) { |
|
| 1197 | + if (array_key_exists('Status', $_stat)) { |
|
| 1198 | 1198 | $_provider_userstatus['Status'] = $_stat['Status']; |
| 1199 | 1199 | } else { |
| 1200 | 1200 | // if no stats then we assume all is well. |
| 1201 | 1201 | $_provider_userstatus['Status'] = 2; |
| 1202 | 1202 | } |
| 1203 | - if ( array_key_exists( 'timestamp', $_stat ) ) { |
|
| 1203 | + if (array_key_exists('timestamp', $_stat)) { |
|
| 1204 | 1204 | $_provider_userstatus['timestamp'] = $_stat['timestamp']; |
| 1205 | 1205 | } else { |
| 1206 | 1206 | // if no timestamp then we return "". |
@@ -1213,27 +1213,27 @@ discard block |
||
| 1213 | 1213 | } |
| 1214 | 1214 | |
| 1215 | 1215 | public function get_status_notice() { |
| 1216 | - if ( $this->imgopt_active() ) { |
|
| 1216 | + if ($this->imgopt_active()) { |
|
| 1217 | 1217 | $notice = ''; |
| 1218 | 1218 | $stat = $this->get_imgopt_provider_userstatus(); |
| 1219 | - $upsell = 'https://shortpixel.com/aospai/af/GWRGFLW109483/' . AUTOPTIMIZE_SITE_DOMAIN; |
|
| 1219 | + $upsell = 'https://shortpixel.com/aospai/af/GWRGFLW109483/'.AUTOPTIMIZE_SITE_DOMAIN; |
|
| 1220 | 1220 | $assoc = 'https://shortpixel.helpscoutdocs.com/article/94-how-to-associate-a-domain-to-my-account'; |
| 1221 | 1221 | |
| 1222 | - if ( is_array( $stat ) ) { |
|
| 1223 | - if ( 1 == $stat['Status'] ) { |
|
| 1222 | + if (is_array($stat)) { |
|
| 1223 | + if (1 == $stat['Status']) { |
|
| 1224 | 1224 | // translators: "add more credits" will appear in a "a href". |
| 1225 | - $notice = sprintf( __( 'Your ShortPixel image optimization and CDN quota is almost used, make sure you %1$sadd more credits%2$s to avoid slowing down your website.', 'autoptimize' ), '<a rel="noopener noreferrer" href="' . $upsell . '" target="_blank">', '</a>' ); |
|
| 1225 | + $notice = sprintf(__('Your ShortPixel image optimization and CDN quota is almost used, make sure you %1$sadd more credits%2$s to avoid slowing down your website.', 'autoptimize'), '<a rel="noopener noreferrer" href="'.$upsell.'" target="_blank">', '</a>'); |
|
| 1226 | 1226 | } elseif ( -1 == $stat['Status'] || -2 == $stat['Status'] ) { |
| 1227 | 1227 | // translators: "add more credits" will appear in a "a href". |
| 1228 | - $notice = sprintf( __( 'Your ShortPixel image optimization and CDN quota was used, %1$sadd more credits%2$s to keep fast serving optimized images on your site.', 'autoptimize' ), '<a rel="noopener noreferrer" href="' . $upsell . '" target="_blank">', '</a>' ); |
|
| 1228 | + $notice = sprintf(__('Your ShortPixel image optimization and CDN quota was used, %1$sadd more credits%2$s to keep fast serving optimized images on your site.', 'autoptimize'), '<a rel="noopener noreferrer" href="'.$upsell.'" target="_blank">', '</a>'); |
|
| 1229 | 1229 | // translators: "associate your domain" will appear in a "a href". |
| 1230 | - $notice = $notice . ' ' . sprintf( __( 'If you already have enough credits then you may need to %1$sassociate your domain%2$s to your Shortpixel account.', 'autoptimize' ), '<a rel="noopener noreferrer" href="' . $assoc . '" target="_blank">', '</a>' ); |
|
| 1230 | + $notice = $notice.' '.sprintf(__('If you already have enough credits then you may need to %1$sassociate your domain%2$s to your Shortpixel account.', 'autoptimize'), '<a rel="noopener noreferrer" href="'.$assoc.'" target="_blank">', '</a>'); |
|
| 1231 | 1231 | } else { |
| 1232 | 1232 | $upsell = 'https://shortpixel.com/g/af/GWRGFLW109483'; |
| 1233 | 1233 | // translators: "log in to check your account" will appear in a "a href". |
| 1234 | - $notice = sprintf( __( 'Your ShortPixel image optimization and CDN quota are in good shape, %1$slog in to check your account%2$s.', 'autoptimize' ), '<a rel="noopener noreferrer" href="' . $upsell . '" target="_blank">', '</a>' ); |
|
| 1234 | + $notice = sprintf(__('Your ShortPixel image optimization and CDN quota are in good shape, %1$slog in to check your account%2$s.', 'autoptimize'), '<a rel="noopener noreferrer" href="'.$upsell.'" target="_blank">', '</a>'); |
|
| 1235 | 1235 | } |
| 1236 | - $notice = apply_filters( 'autoptimize_filter_imgopt_notice', $notice ); |
|
| 1236 | + $notice = apply_filters('autoptimize_filter_imgopt_notice', $notice); |
|
| 1237 | 1237 | |
| 1238 | 1238 | return array( |
| 1239 | 1239 | 'status' => $stat['Status'], |