| 1 |
<?php |
| 2 |
namespace OptimoleWP\Preload; |
| 3 |
|
| 4 |
/** |
| 5 |
* Class Links |
| 6 |
* |
| 7 |
* @package OptimoleWP\Preload |
| 8 |
*/ |
| 9 |
class Links { |
| 10 |
/** |
| 11 |
* The maximum number of links to preload. |
| 12 |
* |
| 13 |
* @var int $MAX_LINKS The maximum number of links to preload. |
| 14 |
*/ |
| 15 |
const MAX_LINKS = 10; |
| 16 |
/** |
| 17 |
* Links map that contains the url as the key and the url data as the value. |
| 18 |
* |
| 19 |
* @var array $links The array of links to preload. |
| 20 |
*/ |
| 21 |
private static $links = []; |
| 22 |
/** |
| 23 |
* Ids map of the images that are preloaded. |
| 24 |
* |
| 25 |
* @var array $ids The array of ids to preload. |
| 26 |
*/ |
| 27 |
private static $ids = []; |
| 28 |
/** |
| 29 |
* Add a link to the preload array. |
| 30 |
* |
| 31 |
* @param array $url_data The array of url data to add. |
| 32 |
*/ |
| 33 |
public static function add_link( array $url_data ) { |
| 34 |
if ( ! isset( $url_data['url'] ) ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
if ( OPTML_DEBUG ) { |
| 38 |
do_action( 'optml_log', 'add preload link : ' . print_r( $url_data, true ) ); |
| 39 |
} |
| 40 |
self::$links[ crc32( $url_data['url'] ) ] = $url_data; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Add an id to the preload array. |
| 45 |
* |
| 46 |
* @param int $id The id to add. |
| 47 |
* @param string $priority The priority of the id. |
| 48 |
*/ |
| 49 |
public static function add_id( int $id, string $priority = 'auto' ) { |
| 50 |
if ( OPTML_DEBUG ) { |
| 51 |
do_action( 'optml_log', 'add preload id : ' . $id . ' ' . $priority ); |
| 52 |
} |
| 53 |
self::$ids[ $id ] = $priority; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Check if an id is preloaded. |
| 58 |
* |
| 59 |
* @param int $id The id to check. |
| 60 |
* @return string|false The priority of the id or false if it is not preloaded. |
| 61 |
*/ |
| 62 |
public static function is_preloaded( int $id ) { |
| 63 |
return self::$ids[ $id ] ?? false; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Preload a tag. |
| 68 |
* |
| 69 |
* @param string $tag The tag to preload. |
| 70 |
* @param string $priority The priority of the tag. |
| 71 |
*/ |
| 72 |
public static function preload_tag( string $tag, string $priority = '' ) { |
| 73 |
// Extract src, srcset, and sizes from the tag using regexes for each one |
| 74 |
$src = ''; |
| 75 |
$srcset = ''; |
| 76 |
$sizes = ''; |
| 77 |
|
| 78 |
$src_pattern = '/<img[^>]+src=["|\']([^"|\']+)["|\']/i'; |
| 79 |
$srcset_pattern = '/<img[^>]+srcset=["|\']([^"|\']+)["|\']/i'; |
| 80 |
$sizes_pattern = '/<img[^>]+sizes=["|\']([^"|\']+)["|\']/i'; |
| 81 |
|
| 82 |
if ( preg_match( $src_pattern, $tag, $matches ) ) { |
| 83 |
$src = $matches[1]; |
| 84 |
} |
| 85 |
if ( preg_match( $srcset_pattern, $tag, $matches ) ) { |
| 86 |
$srcset = $matches[1]; |
| 87 |
} |
| 88 |
if ( preg_match( $sizes_pattern, $tag, $matches ) ) { |
| 89 |
$sizes = $matches[1]; |
| 90 |
} |
| 91 |
if ( OPTML_DEBUG ) { |
| 92 |
do_action( |
| 93 |
'optml_log', |
| 94 |
'preload_tag: ' . print_r( |
| 95 |
[ |
| 96 |
'url' => $src, |
| 97 |
'srcset' => $srcset, |
| 98 |
'sizes' => $sizes, |
| 99 |
'priority' => $priority, |
| 100 |
], |
| 101 |
true |
| 102 |
) . ' ' . $priority |
| 103 |
); |
| 104 |
} |
| 105 |
// Add the preload link to the links array |
| 106 |
self::add_link( |
| 107 |
[ |
| 108 |
'url' => $src, |
| 109 |
'srcset' => $srcset, |
| 110 |
'sizes' => $sizes, |
| 111 |
'priority' => $priority, |
| 112 |
] |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get the links. |
| 118 |
* |
| 119 |
* @return array The links. |
| 120 |
*/ |
| 121 |
public static function get_links(): array { |
| 122 |
return self::$links; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get the links count. |
| 127 |
* |
| 128 |
* @return int The links count. |
| 129 |
*/ |
| 130 |
public static function get_links_count(): int { |
| 131 |
return count( self::$links ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Get the links html. |
| 136 |
* |
| 137 |
* @return string The links html. |
| 138 |
*/ |
| 139 |
public static function get_links_html(): string { |
| 140 |
// generate image preload links for all links |
| 141 |
$links = []; |
| 142 |
foreach ( self::$links as $link ) { |
| 143 |
$url = esc_url( $link['url'] ); |
| 144 |
if ( empty( $url ) ) { |
| 145 |
continue; |
| 146 |
} |
| 147 |
$preload = '<link rel="preload" media="screen" href="' . $url . '" '; |
| 148 |
if ( isset( $link['priority'] ) && $link['priority'] !== 'auto' ) { |
| 149 |
$preload .= 'fetchpriority="' . esc_attr( $link['priority'] ) . '" '; |
| 150 |
} |
| 151 |
// Add imagesrcset if available |
| 152 |
if ( isset( $link['srcset'] ) && ! empty( $link['srcset'] ) ) { |
| 153 |
$preload .= 'imagesrcset="' . esc_attr( $link['srcset'] ) . '" '; |
| 154 |
} |
| 155 |
// Add imagesizes if available |
| 156 |
if ( isset( $link['sizes'] ) && ! empty( $link['sizes'] ) ) { |
| 157 |
$preload .= 'imagesizes="' . esc_attr( $link['sizes'] ) . '" '; |
| 158 |
} |
| 159 |
// Complete the preload tag |
| 160 |
$preload .= 'as="image">'; |
| 161 |
if ( count( $links ) >= self::MAX_LINKS ) { |
| 162 |
break; |
| 163 |
} |
| 164 |
$links[] = $preload; |
| 165 |
} |
| 166 |
return implode( "\n", $links ) . "\n"; |
| 167 |
} |
| 168 |
} |
| 169 |
|