PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 3.1.1
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v3.1.1
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 / resource.php

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

135 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The class maps settings and options from imageproxy.
5 *
6 * @package \Optml\Inc
7 * @author Optimole <friends@optimole.com>
8 */
9 abstract class Optml_Resource {
10
11 use Optml_Validator;
12 use Optml_Normalizer;
13
14 /**
15 * Signature size.
16 */
17 const SIGNATURE_SIZE = 8;
18
19 /**
20 * Source image url.
21 *
22 * @var string Source image.
23 */
24 protected $source_url = '';
25
26 /**
27 * Cache Buster passed from Optimole Service.
28 *
29 * @var string Active Cache Buster
30 */
31 protected $cache_buster = '';
32
33
34
35 /**
36 * Optml_Resource constructor.
37 *
38 * @param string $url Source image url.
39 * @param string $cache_buster The cache buster.
40 *
41 * @throws \InvalidArgumentException In case that the url is not provided.
42 */
43 public function __construct( $url = '', $cache_buster = '' ) {
44 if ( empty( $url ) ) {
45 throw new \InvalidArgumentException( 'Optimole resource builder requires the source url to optimize.' ); // @codeCoverageIgnore
46 }
47 $this->set_defaults();
48 $this->source_url = apply_filters( 'optml_processed_url', $url );
49
50 $this->cache_buster = $cache_buster;
51 }
52
53 /**
54 * Set defaults for resource transformations.
55 */
56 abstract protected function set_defaults();
57
58 /**
59 * Return transformed url.
60 *
61 * @param array $params Either will be signed or not.
62 *
63 * @return string Transformed image url.
64 */
65 abstract public function get_url( $params = [] );
66
67 /**
68 * Method to generate tokens and cache them for further requests.
69 *
70 * @param string $source The source string to tokenize.
71 * @param int $size The size of bites for the generated token.
72 *
73 * @return string
74 */
75 private function get_token_from_cache( $source, $size = 8 ) {
76 $key = crc32( $source );
77 $cache_token = wp_cache_get( $key, 'optml_cache_tokens' );
78 if ( $cache_token === false ) {
79 $cache_token = $this->get_signature( $source, $size );
80 wp_cache_add( $key, $cache_token, 'optml_cache_tokens', DAY_IN_SECONDS );
81 }
82 return $cache_token;
83 }
84
85 /**
86 * Get the token for the domain.
87 *
88 * @return string
89 */
90 public function get_domain_token() {
91 $parts = parse_url( $this->source_url );
92 $domain = isset( $parts['host'] ) ? str_replace( 'www.', '', $parts['host'] ) : '';
93 return $this->get_token_from_cache( $domain, 5 );
94 }
95
96 /**
97 * Get token for the url.
98 *
99 * @return string
100 */
101 private function get_url_token() {
102 $url = strtok( $this->source_url, '?' );
103 return $this->get_token_from_cache( $url, 6 );
104 }
105
106 /**
107 * Return a the cache buster, defaults to the url token used previously.
108 *
109 * @return string
110 */
111 public function get_cache_buster() {
112 if ( $this->cache_buster !== '' ) {
113 return '.' . $this->cache_buster;
114 }
115 return '-' . $this->get_url_token();
116 }
117
118 /**
119 * Return the url signature.
120 *
121 * @param string $string The path from url.
122 *
123 * @return bool|string
124 */
125 public function get_signature( $string = '', $size = self::SIGNATURE_SIZE ) {
126
127 $binary = hash_hmac( 'sha256', Optml_Config::$secret . $string, Optml_Config::$key, true );
128 $binary = pack( 'A' . $size, $binary );
129 $signature = rtrim( strtr( base64_encode( $binary ), '+/', '-_' ), '=' );
130
131 return $signature;
132 }
133
134 }
135