PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 3.11.2
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v3.11.2
4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 3.12.1 All 134 releases
optimole-wp / inc / hero_preloader.php

hero_preloader.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 3.11.2, at inc/hero_preloader.php

175 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Optimole Hero Preloader.
4 *
5 * @package Optimole/Inc
6 * @copyright Copyright (c) 2023, Hardeep Asrani
7 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
8 */
9
10 /**
11 * Class Optml_Hero_Preloader
12 */
13 class Optml_Hero_Preloader {
14
15 /**
16 * Hold the settings object.
17 *
18 * @var Optml_Settings Settings object.
19 */
20 public $settings;
21
22 /**
23 * Cached object instance.
24 *
25 * @var Optml_Hero_Preloader
26 */
27 protected static $instance = null;
28
29 /**
30 * Has flagged preloading image.
31 *
32 * @var bool
33 */
34 protected static $has_flagged_preloading_image = false;
35
36 /**
37 * Has flagged preloading logo to prevent footer logos from being targetted.
38 *
39 * @var bool
40 */
41 protected static $has_flagged_preloading_logo = false;
42
43 /**
44 * Class instance method.
45 *
46 * @static
47 * @return Optml_Hero_Preloader
48 * @since 3.9.0
49 * @access public
50 */
51 public static function instance() {
52 if ( null === self::$instance || ( self::$instance->settings !== null && ( ! self::$instance->settings->is_connected() ) ) ) {
53 self::$instance = new self();
54 self::$instance->settings = new Optml_Settings();
55 if ( self::$instance->settings->is_connected() && ! function_exists( 'wp_get_loading_optimization_attributes' ) ) {
56 self::$instance->init();
57 }
58 }
59
60 return self::$instance;
61 }
62
63 /**
64 * The initialize method.
65 *
66 * @since 3.9.0
67 * @access public
68 */
69 public function init() {
70 add_filter( 'get_header_image_tag_attributes', [ $this, 'add_preload' ] );
71 add_filter( 'post_thumbnail_html', [ $this, 'add_preload_to_thumbnail' ] );
72 add_filter( 'wp_get_attachment_image_attributes', [ $this, 'add_preload_to_image_attributes' ], 10, 2 );
73 add_filter( 'get_custom_logo_image_attributes', [ $this, 'add_preload_to_logo' ] );
74 add_filter( 'wp_content_img_tag', [ $this, 'add_preload_to_thumbnail' ] );
75 }
76
77 /**
78 * Add preload attribute to image.
79 *
80 * @since 3.9.0
81 * @access public
82 *
83 * @param array $attr Image attributes.
84 *
85 * @return array
86 */
87 public function add_preload( $attr ) {
88 if ( self::$has_flagged_preloading_image ) {
89 return $attr;
90 }
91
92 self::$has_flagged_preloading_image = true;
93
94 $attr['fetchpriority'] = 'high';
95 return $attr;
96 }
97
98 /**
99 * Add preload attribute to thumbnail.
100 *
101 * @since 3.9.0
102 * @access public
103 *
104 * @param string $html The post thumbnail HTML.
105 *
106 * @return string
107 */
108 public function add_preload_to_thumbnail( $html ) {
109 if ( self::$has_flagged_preloading_image ) {
110 return $html;
111 }
112
113 if ( ! empty( $html ) && strpos( $html, 'loading="lazy"' ) === false && strpos( $html, 'fetchpriority=' ) === false ) {
114 self::$has_flagged_preloading_image = true;
115 $html = str_replace( '<img', '<img fetchpriority="high"', $html );
116 }
117 return $html;
118 }
119
120 /**
121 * Filter attachment image attributes.
122 *
123 * @since 3.9.0
124 * @access public
125 *
126 * @param array $attr Image attributes.
127 * @param object $attachment Image attachment.
128 *
129 * @return array
130 */
131 public function add_preload_to_image_attributes( $attr, $attachment ) {
132 if ( self::$has_flagged_preloading_image ) {
133 return $attr;
134 }
135
136 global $wp_query;
137
138 $post = null;
139 $queried_post = get_queried_object();
140
141 if ( is_singular() && $queried_post instanceof WP_Post ) {
142 $post = $queried_post;
143 } elseif ( $wp_query->is_main_query() && $wp_query->post_count > 0 && isset( $wp_query->posts[0] ) ) {
144 $post = $wp_query->posts[0];
145 }
146
147 if ( $post instanceof WP_Post && $attachment instanceof WP_Post && (int) get_post_thumbnail_id( $post ) === $attachment->ID ) {
148 $attr = $this->add_preload( $attr );
149 }
150
151 return $attr;
152 }
153
154 /**
155 * Add preload attribute to logo.
156 *
157 * @since 3.9.0
158 * @access public
159 *
160 * @param array $attr Image attributes.
161 *
162 * @return array
163 */
164 public function add_preload_to_logo( $attr ) {
165 if ( self::$has_flagged_preloading_logo ) {
166 return $attr;
167 }
168
169 self::$has_flagged_preloading_logo = true;
170
171 $attr['fetchpriority'] = 'high';
172 return $attr;
173 }
174 }
175