PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 4.2.11
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v4.2.11
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 / vendor / codeinwp / optimole-sdk / src / Optimole.php

Optimole.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 4.2.11, at vendor/codeinwp/optimole-sdk/src/Optimole.php

159 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 /*
6 * This file is part of Optimole PHP SDK.
7 *
8 * (c) Optimole Team <friends@optimole.com>
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13
14 namespace Optimole\Sdk;
15
16 use GuzzleHttp\Client;
17 use Optimole\Sdk\Exception\BadMethodCallException;
18 use Optimole\Sdk\Exception\RuntimeException;
19 use Optimole\Sdk\Http\ClientInterface;
20 use Optimole\Sdk\Http\GuzzleClient;
21 use Optimole\Sdk\Http\WordPressClient;
22 use Optimole\Sdk\Offload\Manager;
23 use Optimole\Sdk\Resource\Asset;
24 use Optimole\Sdk\Resource\Image;
25
26 /**
27 * @method static Asset asset(string $assetUrl, string $cacheBuster = '')
28 * @method static Image image(string $imageUrl, string $cacheBuster = '')
29 * @method static Manager offload()
30 */
31 final class Optimole
32 {
33 /**
34 * The Optimole SDK version.
35 */
36 public const VERSION = '1.2.4';
37
38 /**
39 * The Optimole dashboard API URL.
40 */
41 private const DASHBOARD_API_URL = 'https://dashboard.optimole.com/api';
42
43 /**
44 * The Optimole upload API URL.
45 */
46 private const UPLOAD_API_URL = 'https://generateurls-prod.i.optimole.com/upload';
47
48 /**
49 * The Optimole SDK factory.
50 */
51 private static ?self $instance;
52
53 /**
54 * The Optimole API key.
55 */
56 private string $key;
57
58 /**
59 * The Optimole SDK global options.
60 */
61 private array $options;
62
63 /**
64 * Constructor.
65 */
66 private function __construct(string $key, array $options = [])
67 {
68 $this->key = $key;
69 $this->options = $options;
70 }
71
72 /**
73 * SDK factory method.
74 */
75 public static function __callStatic($name, $arguments)
76 {
77 $method = sprintf('create%s', ucfirst($name));
78
79 if (!self::initialized()) {
80 throw new RuntimeException('Please initialize the Optimole SDK first.');
81 } elseif (!method_exists(self::class, $method)) {
82 throw new BadMethodCallException(sprintf('No factory method for "%s" exists.', $name));
83 }
84
85 return self::$instance->$method(...$arguments);
86 }
87
88 /**
89 * Initialize the Optimole SDK.
90 */
91 public static function init(string $key, array $options = []): void
92 {
93 $key = trim(strtolower($key));
94 $options = array_merge([
95 'base_domain' => 'i.optimole.com',
96 'cache_buster' => '',
97 'dashboard_api_key' => '',
98 'dashboard_api_url' => self::DASHBOARD_API_URL,
99 'upload_api_url' => self::UPLOAD_API_URL,
100 ], $options);
101
102 self::$instance = new self($key, $options);
103 }
104
105 /**
106 * Check if the SDK has been initialized.
107 */
108 public static function initialized(): bool
109 {
110 return isset(self::$instance);
111 }
112
113 /**
114 * Create an asset resource.
115 */
116 private function createAsset(string $assetUrl, string $cacheBuster = ''): Asset
117 {
118 return new Asset($this->getDomain(), $assetUrl, $cacheBuster ?: $this->options['cache_buster']);
119 }
120
121 /**
122 * Create an image resource.
123 */
124 private function createImage(string $imageUrl, string $cacheBuster = ''): Image
125 {
126 return new Image($this->getDomain(), $imageUrl, $cacheBuster ?: $this->options['cache_buster']);
127 }
128
129 /**
130 * Create an instance of offload manager.
131 */
132 private function createOffload(array $options = []): Manager
133 {
134 return new Manager($this->getHttpClient(), array_merge($this->options, $options));
135 }
136
137 /**
138 * Get the Optimole domain to use.
139 */
140 private function getDomain(): string
141 {
142 return $this->options['domain'] ?? sprintf('%s.%s', $this->key, $this->options['base_domain']);
143 }
144
145 /**
146 * Get the HTTP client available in the environment.
147 */
148 private function getHttpClient(): ClientInterface
149 {
150 if (class_exists(Client::class)) {
151 return new GuzzleClient(new Client());
152 } elseif (function_exists('_wp_http_get_object')) {
153 return new WordPressClient(_wp_http_get_object());
154 }
155
156 throw new RuntimeException('Unable to find a suitable HTTP client for this environment');
157 }
158 }
159