Skip to content

Commit

Permalink
Ensure required WP_Image_Editor subclass is present
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed May 3, 2024
1 parent 4d63333 commit f4fec1f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions plugins/dominant-color-images/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ function dominant_color_set_image_editors( $editors ) {
}

$replaces = array(
'WP_Image_Editor_GD' => 'Dominant_Color_Image_Editor_GD',
'WP_Image_Editor_Imagick' => 'Dominant_Color_Image_Editor_Imagick',
WP_Image_Editor_GD::class => Dominant_Color_Image_Editor_GD::class,
WP_Image_Editor_Imagick::class => Dominant_Color_Image_Editor_Imagick::class,
);

foreach ( $replaces as $old => $new ) {
Expand Down Expand Up @@ -58,6 +58,13 @@ function dominant_color_get_dominant_color_data( $attachment_id ) {
$file = get_attached_file( $attachment_id );
}
add_filter( 'wp_image_editors', 'dominant_color_set_image_editors' );

/**
* Editor.
*
* @see dominant_color_set_image_editors()
* @var WP_Image_Editor|Dominant_Color_Image_Editor_GD|Dominant_Color_Image_Editor_Imagick|WP_Error $editor
*/
$editor = wp_get_image_editor(
$file,
array(
Expand All @@ -73,6 +80,10 @@ function dominant_color_get_dominant_color_data( $attachment_id ) {
return $editor;
}

if ( ! ( $editor instanceof Dominant_Color_Image_Editor_GD || $editor instanceof Dominant_Color_Image_Editor_Imagick ) ) {
return new WP_Error( 'image_no_editor', __( 'No editor could be selected.', 'default' ) );
}

$has_transparency = $editor->has_transparency();
if ( is_wp_error( $has_transparency ) ) {
return $has_transparency;
Expand Down

0 comments on commit f4fec1f

Please sign in to comment.