@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) return; |
|
| 2 | +if ( ! defined('ABSPATH')) return; |
|
| 3 | 3 | |
| 4 | 4 | /* |
| 5 | 5 | * LSX Lazy Load Images Class |
@@ -25,143 +25,143 @@ discard block |
||
| 25 | 25 | protected static $noscripts = array(); |
| 26 | 26 | |
| 27 | 27 | static function init() { |
| 28 | - if ( is_admin() ) |
|
| 28 | + if (is_admin()) |
|
| 29 | 29 | return; |
| 30 | 30 | |
| 31 | - if ( get_theme_mod( 'lsx_lazyload_status', '1' ) === false ) { |
|
| 31 | + if (get_theme_mod('lsx_lazyload_status', '1') === false) { |
|
| 32 | 32 | self::$enabled = false; |
| 33 | 33 | return; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - if ( ! apply_filters( 'lsx_lazyload_is_enabled', true ) ) { |
|
| 36 | + if ( ! apply_filters('lsx_lazyload_is_enabled', true)) { |
|
| 37 | 37 | self::$enabled = false; |
| 38 | 38 | return; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - add_action( 'wp_enqueue_scripts', array( __CLASS__, 'add_scripts' ) ); |
|
| 42 | - add_action( 'wp_head', array( __CLASS__, 'setup_filters' ), 9999 ); |
|
| 43 | - add_filter( 'wp_kses_allowed_html', array( __CLASS__, 'kses_allowed_html' ), 10, 2 ); |
|
| 44 | - add_filter( 'kses_allowed_protocols', array( __CLASS__, 'kses_allowed_protocols' ) ); |
|
| 41 | + add_action('wp_enqueue_scripts', array(__CLASS__, 'add_scripts')); |
|
| 42 | + add_action('wp_head', array(__CLASS__, 'setup_filters'), 9999); |
|
| 43 | + add_filter('wp_kses_allowed_html', array(__CLASS__, 'kses_allowed_html'), 10, 2); |
|
| 44 | + add_filter('kses_allowed_protocols', array(__CLASS__, 'kses_allowed_protocols')); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | static function setup_filters() { |
| 48 | 48 | // WordPress |
| 49 | - add_filter( 'the_content', array( __CLASS__, 'filter_images' ), 200 ); |
|
| 50 | - add_filter( 'widget_text', array( __CLASS__, 'filter_images' ), 200 ); |
|
| 51 | - add_filter( 'post_thumbnail_html', array( __CLASS__, 'filter_images' ), 200 ); |
|
| 52 | - add_filter( 'get_avatar', array( __CLASS__, 'filter_images' ), 200 ); |
|
| 49 | + add_filter('the_content', array(__CLASS__, 'filter_images'), 200); |
|
| 50 | + add_filter('widget_text', array(__CLASS__, 'filter_images'), 200); |
|
| 51 | + add_filter('post_thumbnail_html', array(__CLASS__, 'filter_images'), 200); |
|
| 52 | + add_filter('get_avatar', array(__CLASS__, 'filter_images'), 200); |
|
| 53 | 53 | |
| 54 | 54 | // LSX |
| 55 | - add_filter( 'lsx_lazyload_filter_images', array( __CLASS__, 'filter_images' ), 200 ); |
|
| 55 | + add_filter('lsx_lazyload_filter_images', array(__CLASS__, 'filter_images'), 200); |
|
| 56 | 56 | |
| 57 | 57 | // Envira Gallery |
| 58 | - add_filter( 'envira_gallery_output_image', array( __CLASS__, 'filter_images' ), 200 ); |
|
| 58 | + add_filter('envira_gallery_output_image', array(__CLASS__, 'filter_images'), 200); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | static function add_scripts() { |
| 62 | - wp_enqueue_script( 'lazysizes', get_template_directory_uri() .'/js/vendor/lazysizes.min.js', array( 'jquery' ), null, true ); |
|
| 62 | + wp_enqueue_script('lazysizes', get_template_directory_uri() . '/js/vendor/lazysizes.min.js', array('jquery'), null, true); |
|
| 63 | 63 | // Plugin that enables use lazysizes in brackground images |
| 64 | 64 | //wp_enqueue_script( 'lazysizes', get_template_directory_uri() .'/js/vendor/ls.unveilhooks.min.js', array( 'jquery', 'lazysizes' ), null, true ); |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - static function filter_images( $content ) { |
|
| 68 | - if ( ! self::is_enabled() ) { |
|
| 67 | + static function filter_images($content) { |
|
| 68 | + if ( ! self::is_enabled()) { |
|
| 69 | 69 | return $content; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - $http_user_agent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ); |
|
| 73 | - $http_user_agent = ! empty( $http_user_agent ) ? $http_user_agent : ''; |
|
| 72 | + $http_user_agent = sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])); |
|
| 73 | + $http_user_agent = ! empty($http_user_agent) ? $http_user_agent : ''; |
|
| 74 | 74 | |
| 75 | - if ( is_feed() |
|
| 75 | + if (is_feed() |
|
| 76 | 76 | || is_preview() |
| 77 | - || intval( get_query_var( 'print' ) ) == 1 |
|
| 78 | - || intval( get_query_var( 'printpage' ) ) == 1 |
|
| 79 | - || strpos( $http_user_agent, 'Opera Mini' ) !== false |
|
| 77 | + || intval(get_query_var('print')) == 1 |
|
| 78 | + || intval(get_query_var('printpage')) == 1 |
|
| 79 | + || strpos($http_user_agent, 'Opera Mini') !== false |
|
| 80 | 80 | ) { |
| 81 | 81 | return $content; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | $skip_images_regex = '/class=".*(lazyload|disable-lazyload).*"/'; |
| 85 | - $placeholder_image = apply_filters( 'lsx_lazyload_placeholder_image', 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' ); |
|
| 85 | + $placeholder_image = apply_filters('lsx_lazyload_placeholder_image', 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='); |
|
| 86 | 86 | |
| 87 | 87 | $matches = array(); |
| 88 | 88 | $search = array(); |
| 89 | 89 | $replace = array(); |
| 90 | 90 | |
| 91 | - $content = preg_replace_callback( '~<noscript.+?</noscript>~s', 'self::noscripts_remove', $content ); |
|
| 92 | - preg_match_all( '/<img[^>]*>/', $content, $matches ); |
|
| 91 | + $content = preg_replace_callback('~<noscript.+?</noscript>~s', 'self::noscripts_remove', $content); |
|
| 92 | + preg_match_all('/<img[^>]*>/', $content, $matches); |
|
| 93 | 93 | |
| 94 | - foreach ( $matches[0] as $img_html ) { |
|
| 95 | - if ( ! ( preg_match( $skip_images_regex, $img_html ) ) ) { |
|
| 94 | + foreach ($matches[0] as $img_html) { |
|
| 95 | + if ( ! (preg_match($skip_images_regex, $img_html))) { |
|
| 96 | 96 | $add_class = false; |
| 97 | 97 | |
| 98 | - if ( ! preg_match( '/src=[\'"]([^\'"]+)[\'"]/', $img_html ) && preg_match( '/srcset=[\'"]([^\'"]+)[\'"]/', $img_html ) ) { |
|
| 99 | - $replace_html = preg_replace( '/<img(.*?)srcset=/i', '<img$1srcset="' . $placeholder_image . '" data-srcset=', $img_html ); |
|
| 98 | + if ( ! preg_match('/src=[\'"]([^\'"]+)[\'"]/', $img_html) && preg_match('/srcset=[\'"]([^\'"]+)[\'"]/', $img_html)) { |
|
| 99 | + $replace_html = preg_replace('/<img(.*?)srcset=/i', '<img$1srcset="' . $placeholder_image . '" data-srcset=', $img_html); |
|
| 100 | 100 | |
| 101 | - if ( preg_match( '/sizes=[\'"]([^\'"]+)[\'"]/', $img_html ) ) { |
|
| 102 | - $replace_html = preg_replace( '/sizes=/i', 'data-sizes=', $replace_html ); |
|
| 101 | + if (preg_match('/sizes=[\'"]([^\'"]+)[\'"]/', $img_html)) { |
|
| 102 | + $replace_html = preg_replace('/sizes=/i', 'data-sizes=', $replace_html); |
|
| 103 | 103 | } else { |
| 104 | - $replace_html = preg_replace( '/data-srcset=/i', 'data-sizes="auto" data-srcset=', $replace_html ); |
|
| 104 | + $replace_html = preg_replace('/data-srcset=/i', 'data-sizes="auto" data-srcset=', $replace_html); |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | $add_class = true; |
| 108 | - } elseif ( preg_match( '/src=[\'"]([^\'"]+)[\'"]/', $img_html ) ) { |
|
| 109 | - $replace_html = preg_replace( '/<img(.*?)src=/i', '<img$1src="' . $placeholder_image . '" data-src=', $img_html ); |
|
| 108 | + } elseif (preg_match('/src=[\'"]([^\'"]+)[\'"]/', $img_html)) { |
|
| 109 | + $replace_html = preg_replace('/<img(.*?)src=/i', '<img$1src="' . $placeholder_image . '" data-src=', $img_html); |
|
| 110 | 110 | |
| 111 | - if ( preg_match( '/srcset=[\'"]([^\'"]+)[\'"]/', $img_html ) ) { |
|
| 112 | - if ( preg_match( '/sizes=[\'"]([^\'"]+)[\'"]/', $img_html ) ) { |
|
| 113 | - $replace_html = preg_replace( '/srcset=/i', 'data-srcset=', $replace_html ); |
|
| 114 | - $replace_html = preg_replace( '/sizes=/i', 'data-sizes=', $replace_html ); |
|
| 111 | + if (preg_match('/srcset=[\'"]([^\'"]+)[\'"]/', $img_html)) { |
|
| 112 | + if (preg_match('/sizes=[\'"]([^\'"]+)[\'"]/', $img_html)) { |
|
| 113 | + $replace_html = preg_replace('/srcset=/i', 'data-srcset=', $replace_html); |
|
| 114 | + $replace_html = preg_replace('/sizes=/i', 'data-sizes=', $replace_html); |
|
| 115 | 115 | } else { |
| 116 | - $replace_html = preg_replace( '/srcset=/i', 'data-sizes="auto" data-srcset=', $replace_html ); |
|
| 116 | + $replace_html = preg_replace('/srcset=/i', 'data-sizes="auto" data-srcset=', $replace_html); |
|
| 117 | 117 | } |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | $add_class = true; |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - if ( $add_class ) { |
|
| 124 | - $replace_html = self::add_class( $replace_html, 'lazyload' ); |
|
| 123 | + if ($add_class) { |
|
| 124 | + $replace_html = self::add_class($replace_html, 'lazyload'); |
|
| 125 | 125 | $replace_html .= '<noscript>' . $img_html . '</noscript>'; |
| 126 | 126 | |
| 127 | - array_push( $search, $img_html ); |
|
| 128 | - array_push( $replace, $replace_html ); |
|
| 127 | + array_push($search, $img_html); |
|
| 128 | + array_push($replace, $replace_html); |
|
| 129 | 129 | } |
| 130 | 130 | } |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - $content = str_replace( $search, $replace, $content ); |
|
| 134 | - $content = preg_replace_callback( '~' . chr(20) . '([0-9]+)' . chr(20) . '~', 'self::noscripts_restore', $content ); |
|
| 133 | + $content = str_replace($search, $replace, $content); |
|
| 134 | + $content = preg_replace_callback('~' . chr(20) . '([0-9]+)' . chr(20) . '~', 'self::noscripts_restore', $content); |
|
| 135 | 135 | return $content; |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - static function noscripts_remove( $match ) { |
|
| 138 | + static function noscripts_remove($match) { |
|
| 139 | 139 | self::$noscript_id++; |
| 140 | 140 | self::$noscripts[self::$noscript_id] = $match[0]; |
| 141 | 141 | return chr(20) . self::$noscript_id . chr(20); |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | - static function noscripts_restore( $match ) { |
|
| 144 | + static function noscripts_restore($match) { |
|
| 145 | 145 | return self::$noscripts[(int) $match[1]]; |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | - static function add_class( $html_string = '', $new_class ) { |
|
| 148 | + static function add_class($html_string = '', $new_class) { |
|
| 149 | 149 | $pattern = '/class=[\'"]([^\'"]*)[\'"]/'; |
| 150 | 150 | |
| 151 | - if ( preg_match( $pattern, $html_string, $matches ) ) { |
|
| 152 | - $defined_classes = explode( ' ', $matches[1] ); |
|
| 151 | + if (preg_match($pattern, $html_string, $matches)) { |
|
| 152 | + $defined_classes = explode(' ', $matches[1]); |
|
| 153 | 153 | |
| 154 | - if ( ! in_array( $new_class, $defined_classes ) ) { |
|
| 154 | + if ( ! in_array($new_class, $defined_classes)) { |
|
| 155 | 155 | $defined_classes[] = $new_class; |
| 156 | 156 | |
| 157 | 157 | $html_string = str_replace( |
| 158 | 158 | $matches[0], |
| 159 | - sprintf( 'class="%s"', implode( ' ', $defined_classes ) ), |
|
| 159 | + sprintf('class="%s"', implode(' ', $defined_classes)), |
|
| 160 | 160 | $html_string |
| 161 | 161 | ); |
| 162 | 162 | } |
| 163 | 163 | } else { |
| 164 | - $html_string = preg_replace( '/(\<.+\s)/', sprintf( '$1class="%s" ', $new_class ), $html_string ); |
|
| 164 | + $html_string = preg_replace('/(\<.+\s)/', sprintf('$1class="%s" ', $new_class), $html_string); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | return $html_string; |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | return self::$enabled; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - static function kses_allowed_html( $allowedtags, $context ) { |
|
| 174 | + static function kses_allowed_html($allowedtags, $context) { |
|
| 175 | 175 | $allowedtags['noscript'] = array(); |
| 176 | 176 | |
| 177 | 177 | $allowedtags['img']['data-src'] = true; |
@@ -181,10 +181,10 @@ discard block |
||
| 181 | 181 | return $allowedtags; |
| 182 | 182 | } |
| 183 | 183 | |
| 184 | - static function kses_allowed_protocols( $allowedprotocols ) { |
|
| 184 | + static function kses_allowed_protocols($allowedprotocols) { |
|
| 185 | 185 | $allowedprotocols[] = 'data'; |
| 186 | 186 | return $allowedprotocols; |
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | -add_action( 'init', array( 'LSX_LazyLoadImages', 'init' ) ); |
|
| 190 | +add_action('init', array('LSX_LazyLoadImages', 'init')); |
|