Skip to content

Commit fedabef

Browse files
authored
Merge pull request #910 from Codeinwp/fix/image-editor
Add Image editor to prevent errors with offloaded images
2 parents cf696a0 + 81181b2 commit fedabef

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed

inc/media_offload.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* @author Optimole <[email protected]>
77
*/
88

9+
use OptimoleWP\Offload\Loader;
910
use Optimole\Sdk\Exception\InvalidArgumentException;
1011
use Optimole\Sdk\Exception\InvalidUploadApiResponseException;
1112
use Optimole\Sdk\Exception\RuntimeException;
@@ -186,6 +187,7 @@ public static function instance() {
186187
if ( self::$is_legacy_install === null ) {
187188
self::$is_legacy_install = get_option( 'optimole_wp_install', 0 ) > 1677171600;
188189
}
190+
( new Loader() )->register_hooks();
189191
}
190192
}
191193

inc/v2/Offload/ImageEditor.php

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
namespace OptimoleWP\Offload;
4+
5+
use Optml_Config;
6+
use Optml_Media_Offload;
7+
use WP_Error;
8+
9+
/**
10+
* Class ImageEditor
11+
*
12+
* Custom image editor implementation for Optimole offloaded images.
13+
* This class extends WP_Image_Editor to handle images that have been offloaded
14+
* to Optimole's servers, preventing local editing operations on remote images.
15+
*
16+
* @package OptimoleWP\Offload
17+
*/
18+
class ImageEditor extends \WP_Image_Editor {
19+
/**
20+
* Tests whether this editor can handle the given file.
21+
*
22+
* @param array $args Arguments to test the editor.
23+
* @return bool True if the file is an offloaded image, false otherwise.
24+
*/
25+
public static function test( $args = [] ) {
26+
if ( isset( $args['path'] ) && Optml_Media_Offload::is_uploaded_image( $args['path'] ) ) {
27+
return true;
28+
}
29+
30+
return false;
31+
}
32+
33+
/**
34+
* Checks if this editor supports the given MIME type.
35+
*
36+
* @param string $mime_type The MIME type to check.
37+
* @return bool True if the MIME type is supported, false otherwise.
38+
*/
39+
public static function supports_mime_type( $mime_type ) {
40+
return in_array( $mime_type, Optml_Config::$image_extensions, true );
41+
}
42+
43+
/**
44+
* Saves the image to a file.
45+
*
46+
* For offloaded images, this operation is not supported and always returns false.
47+
*
48+
* @param string $destfilename Optional. The destination filename.
49+
* @param string $mime_type Optional. The MIME type of the image.
50+
* @return array|WP_Error {
51+
* Array on success or WP_Error if the file failed to save.
52+
*
53+
* @type string $path Path to the image file.
54+
* @type string $file Name of the image file.
55+
* @type int $width Image width.
56+
* @type int $height Image height.
57+
* @type string $mime-type The mime type of the image.
58+
* @type int $filesize File size of the image.
59+
* }
60+
*/
61+
public function save( $destfilename = null, $mime_type = null ) {
62+
return false; // @phpstan-ignore-line
63+
}
64+
65+
/**
66+
* Loads the image file.
67+
*
68+
* @return bool True if the file is an offloaded image, false otherwise.
69+
*/
70+
public function load() {
71+
if ( isset( $this->file ) && Optml_Media_Offload::is_uploaded_image( $this->file ) ) {
72+
return true;
73+
}
74+
return false;
75+
}
76+
77+
/**
78+
* Resizes the image.
79+
*
80+
* For offloaded images, this operation is not performed locally and always returns true.
81+
*
82+
* @param int $max_w Maximum width.
83+
* @param int $max_h Maximum height.
84+
* @param bool $crop Optional. Whether to crop the image. Default false.
85+
* @return bool Always returns true for offloaded images.
86+
*/
87+
public function resize( $max_w, $max_h, $crop = false ) {
88+
return true;
89+
}
90+
91+
/**
92+
* Resizes the image to multiple sizes.
93+
*
94+
* For offloaded images, this operation is not performed locally and always returns an empty array.
95+
*
96+
* @param array $sizes Array of size arrays. Each size array must include width, height, crop.
97+
* @return array Empty array for offloaded images.
98+
*/
99+
public function multi_resize( $sizes ) {
100+
return [];
101+
}
102+
103+
/**
104+
* Crops the image.
105+
*
106+
* For offloaded images, this operation is not performed locally and always returns true.
107+
*
108+
* @param int $src_x The start x position to crop from.
109+
* @param int $src_y The start y position to crop from.
110+
* @param int $src_w The width to crop.
111+
* @param int $src_h The height to crop.
112+
* @param int $dst_w Optional. The destination width.
113+
* @param int $dst_h Optional. The destination height.
114+
* @param bool $src_abs Optional. If the source crop points are absolute.
115+
* @return bool Always returns true for offloaded images.
116+
*/
117+
public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
118+
return true;
119+
}
120+
121+
/**
122+
* Rotates the image.
123+
*
124+
* For offloaded images, this operation is not performed locally and always returns true.
125+
*
126+
* @param float $angle The angle of rotation.
127+
* @return bool Always returns true for offloaded images.
128+
*/
129+
public function rotate( $angle ) {
130+
return true;
131+
}
132+
133+
/**
134+
* Flips the image.
135+
*
136+
* For offloaded images, this operation is not performed locally and always returns true.
137+
*
138+
* @param bool $horz Whether to flip horizontally.
139+
* @param bool $vert Whether to flip vertically.
140+
* @return bool Always returns true for offloaded images.
141+
*/
142+
public function flip( $horz, $vert ) {
143+
return true;
144+
}
145+
146+
/**
147+
* Streams the image to the browser.
148+
*
149+
* For offloaded images, this redirects to the remote image URL.
150+
*
151+
* @param string $mime_type Optional. The MIME type of the image.
152+
*/
153+
public function stream( $mime_type = null ) {
154+
header( 'Location: ' . esc_url( $this->file ) );
155+
return true;
156+
}
157+
}

inc/v2/Offload/Loader.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace OptimoleWP\Offload;
4+
5+
/**
6+
* Class Loader
7+
*
8+
* Handles the registration of custom image editors for the Optimole WordPress plugin.
9+
* This class is responsible for integrating Optimole's custom image processing
10+
* capabilities into WordPress's image editing system.
11+
*
12+
* @package OptimoleWP\Offload
13+
*/
14+
class Loader {
15+
16+
/**
17+
* Constructor for the Loader class.
18+
*/
19+
public function __construct() {
20+
}
21+
22+
/**
23+
* Register WordPress hooks needed for the image editor functionality.
24+
*
25+
* Adds filters to integrate the custom image editor into WordPress.
26+
*/
27+
public function register_hooks() {
28+
add_filter( 'wp_image_editors', [ $this, 'register_image_editor' ] );
29+
}
30+
31+
/**
32+
* Register the custom ImageEditor class to WordPress's image editors list.
33+
*
34+
* Adds Optimole's custom ImageEditor to the beginning of WordPress's image editors array,
35+
* giving it priority over the default editors.
36+
*
37+
* @param array $editors Array of image editor class names.
38+
* @return array Modified array of image editor class names.
39+
*/
40+
public function register_image_editor( $editors ) {
41+
array_unshift( $editors, ImageEditor::class );
42+
return $editors;
43+
}
44+
}

0 commit comments

Comments
 (0)