| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/) |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
|
| 10 |
die( 'These aren\'t the droids you\'re looking for.' ); |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! defined( 'WPSSO_PLUGINDIR' ) ) { |
| 14 |
|
| 15 |
die( 'Do. Or do not. There is no try.' ); |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'WpssoMediaFilters' ) ) { |
| 19 |
|
| 20 |
/* |
| 21 |
* Since WPSSO Core v13.2.0. |
| 22 |
*/ |
| 23 |
class WpssoMediaFilters { |
| 24 |
|
| 25 |
private $p; // Wpsso class object. |
| 26 |
|
| 27 |
/* |
| 28 |
* Instantiated by WpssoMedia->__construct(). |
| 29 |
*/ |
| 30 |
public function __construct( &$plugin ) { |
| 31 |
|
| 32 |
$this->p =& $plugin; |
| 33 |
|
| 34 |
if ( $this->p->debug->enabled ) { |
| 35 |
|
| 36 |
$this->p->debug->mark(); |
| 37 |
} |
| 38 |
|
| 39 |
/* |
| 40 |
* Filters for the "Image Dimension Checks" option. |
| 41 |
*/ |
| 42 |
if ( $this->p->options[ 'plugin_check_img_dims' ] ) { |
| 43 |
|
| 44 |
$this->p->util->add_plugin_filters( $this, array( |
| 45 |
'attached_accept_img_dims' => 6, |
| 46 |
'content_accept_img_dims' => 6, |
| 47 |
) ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/* |
| 52 |
* $size_name must be a string. |
| 53 |
*/ |
| 54 |
public function filter_attached_accept_img_dims( $accept, $img_url, $img_width, $img_height, $size_name, $pid ) { |
| 55 |
|
| 56 |
/* |
| 57 |
* Don't re-check already rejected images. |
| 58 |
*/ |
| 59 |
if ( ! $accept ) { // Value is false. |
| 60 |
|
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
if ( 0 !== strpos( $size_name, 'wpsso-' ) ) { |
| 65 |
|
| 66 |
if ( $this->p->debug->enabled ) { |
| 67 |
|
| 68 |
$this->p->debug->log( 'exiting early: ' . $size_name . ' not a wpsso custom image size' ); |
| 69 |
} |
| 70 |
|
| 71 |
return $accept; |
| 72 |
} |
| 73 |
|
| 74 |
$size_info = $this->p->util->get_size_info( $size_name, $pid ); |
| 75 |
$is_sufficient_w = $img_width >= $size_info[ 'width' ] ? true : false; |
| 76 |
$is_sufficient_h = $img_height >= $size_info[ 'height' ] ? true : false; |
| 77 |
|
| 78 |
if ( ( ! $size_info[ 'is_cropped' ] && ( $is_sufficient_w || $is_sufficient_h ) ) || |
| 79 |
( $size_info[ 'is_cropped' ] && ( $is_sufficient_w && $is_sufficient_h ) ) ) { |
| 80 |
|
| 81 |
if ( $this->p->debug->enabled ) { |
| 82 |
|
| 83 |
$this->p->debug->log( 'image ID ' . $pid . ' dimensions (' . $img_width . 'x' . $img_height . ') are sufficient' ); |
| 84 |
} |
| 85 |
|
| 86 |
return true; // Image dimensions are sufficient. |
| 87 |
} |
| 88 |
|
| 89 |
$img_meta = wp_get_attachment_metadata( $pid ); // Returns a WP_Error object on failure. |
| 90 |
|
| 91 |
if ( is_array( $img_meta ) && isset( $img_meta[ 'width' ] ) && isset( $img_meta[ 'height' ] ) && |
| 92 |
$img_meta[ 'width' ] < $size_info[ 'width' ] && $img_meta[ 'height' ] < $size_info[ 'height' ] ) { |
| 93 |
|
| 94 |
$img_dims = $img_meta[ 'width' ] . 'x' . $img_meta[ 'height' ]; |
| 95 |
$img_dims_transl = $img_dims . ' (' . __( 'full size original', 'wpsso' ) . ')'; |
| 96 |
|
| 97 |
} else { |
| 98 |
|
| 99 |
$img_dims = $img_width . 'x' . $img_height; |
| 100 |
$img_dims_transl = $img_dims; |
| 101 |
} |
| 102 |
|
| 103 |
if ( $this->p->debug->enabled ) { |
| 104 |
|
| 105 |
$this->p->debug->log( 'image ID ' . $pid . ' rejected - ' . $img_dims . |
| 106 |
' too small for the ' . $size_name . ' (' . $size_info[ 'dims_transl' ] . ') image size' ); |
| 107 |
} |
| 108 |
|
| 109 |
/* |
| 110 |
* Add notice only if the admin notices have not already been shown. |
| 111 |
* |
| 112 |
* An is_admin() test is required to make sure the WpssoMessages class is available. |
| 113 |
*/ |
| 114 |
if ( $this->p->notice->is_admin_pre_notices() ) { |
| 115 |
|
| 116 |
// translators: Please ignore - translation uses a different text domain. |
| 117 |
$img_lib = __( 'Media Library' ); |
| 118 |
$img_edit_url = get_edit_post_link( $pid ); |
| 119 |
$img_title = get_the_title( $pid ); |
| 120 |
$img_label = sprintf( __( 'image ID %1$s (%2$s)', 'wpsso' ), $pid, $img_title ); |
| 121 |
$img_label = empty( $img_edit_url ) ? $img_label : '<a href="' . $img_edit_url . '">' . $img_label . '</a>'; |
| 122 |
|
| 123 |
$notice_msg = sprintf( __( '%1$s %2$s ignored - the resulting resized image of %3$s is too small for the required %4$s image dimensions.', |
| 124 |
'wpsso' ), $img_lib, $img_label, $img_dims_transl, '<b>' . $size_info[ 'label_transl' ] . '</b> (' . $size_info[ 'dims_transl' ] . ')' ) . |
| 125 |
' ' . $this->p->msgs->get( 'notice-image-rejected' ); |
| 126 |
|
| 127 |
$notice_key = 'wp_' . $pid . '_' . $img_dims . '_' . $size_name . '_' . $size_info[ 'dims_transl' ] . '_rejected'; |
| 128 |
|
| 129 |
$this->p->notice->warn( $notice_msg, null, $notice_key, $dismiss_time = true ); |
| 130 |
} |
| 131 |
|
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
/* |
| 136 |
* $size_name must be a string. |
| 137 |
*/ |
| 138 |
public function filter_content_accept_img_dims( $accept, $og_image, $size_name, $attr_name, $content_passed ) { |
| 139 |
|
| 140 |
/* |
| 141 |
* Don't re-check already rejected images. |
| 142 |
*/ |
| 143 |
if ( ! $accept ) { // Value is false. |
| 144 |
|
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
if ( 0 !== strpos( $size_name, 'wpsso-' ) ) { |
| 149 |
|
| 150 |
if ( $this->p->debug->enabled ) { |
| 151 |
|
| 152 |
$this->p->debug->log( 'exiting early: ' . $size_name . ' not a wpsso custom image size' ); |
| 153 |
} |
| 154 |
|
| 155 |
return $accept; |
| 156 |
} |
| 157 |
|
| 158 |
$size_info = $this->p->util->get_size_info( $size_name ); |
| 159 |
$is_sufficient_w = $og_image[ 'og:image:width' ] >= $size_info[ 'width' ] ? true : false; |
| 160 |
$is_sufficient_h = $og_image[ 'og:image:height' ] >= $size_info[ 'height' ] ? true : false; |
| 161 |
$image_url = SucomUtil::get_first_mt_media_url( $og_image ); |
| 162 |
|
| 163 |
if ( ( $attr_name == 'src' && ! $size_info[ 'is_cropped' ] && ( $is_sufficient_w || $is_sufficient_h ) ) || |
| 164 |
( $attr_name == 'src' && $size_info[ 'is_cropped' ] && ( $is_sufficient_w && $is_sufficient_h ) ) || |
| 165 |
$attr_name == 'data-share-src' ) { |
| 166 |
|
| 167 |
if ( $this->p->debug->enabled ) { |
| 168 |
|
| 169 |
$this->p->debug->log( $image_url . ' dimensions (' . $og_image[ 'og:image:width' ] . 'x' . |
| 170 |
$og_image[ 'og:image:height' ] . ') are sufficient' ); |
| 171 |
} |
| 172 |
|
| 173 |
return true; // Image dimensions are sufficient. |
| 174 |
} |
| 175 |
|
| 176 |
if ( $this->p->debug->enabled ) { |
| 177 |
|
| 178 |
$this->p->debug->log( 'content image rejected: width / height missing or too small for ' . $size_name ); |
| 179 |
} |
| 180 |
|
| 181 |
/* |
| 182 |
* Add notice only if the admin notices have not already been shown. |
| 183 |
*/ |
| 184 |
if ( $this->p->notice->is_admin_pre_notices() ) { |
| 185 |
|
| 186 |
$notice_msg = sprintf( __( 'Image %1$s in content ignored - the image width and height is too small for the required %2$s image dimensions.', |
| 187 |
'wpsso' ), $image_url, '<b>' . $size_info[ 'label_transl' ] . '</b> (' . $size_info[ 'dims_transl' ] . ')' ); |
| 188 |
|
| 189 |
$notice_msg .= $content_passed ? '' : ' ' . sprintf( __( '%1$s includes an additional \'data-wp-pid\' attribute for WordPress Media Library images - if this image was selected from the Media Library before %1$s was activated, try removing and adding the image back to your content.', |
| 190 |
'wpsso' ), $this->p->cf[ 'plugin' ][ $this->p->id ][ 'short' ] ); |
| 191 |
|
| 192 |
$notice_key = 'content_' . $image_url . '_' . $size_name . '_rejected'; |
| 193 |
|
| 194 |
$this->p->notice->warn( $notice_msg, null, $notice_key, $dismiss_time = true ); |
| 195 |
} |
| 196 |
|
| 197 |
return false; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|