| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class to check media types |
| 7 |
*/ |
| 8 |
class Media_Type_Checker { |
| 9 |
|
| 10 |
/** |
| 11 |
* Check if an attachment is an image |
| 12 |
* |
| 13 |
* @param int|\WP_Post $attachment Attachment ID or post object. |
| 14 |
* |
| 15 |
* @return bool True if the attachment is an image, false otherwise. |
| 16 |
*/ |
| 17 |
public static function is_image( $attachment ): bool { |
| 18 |
if ( ! is_int( $attachment ) && ! $attachment instanceof \WP_Post ) { |
| 19 |
return false; |
| 20 |
} |
| 21 |
|
| 22 |
$mime_type = get_post_mime_type( $attachment ); |
| 23 |
if ( ! $mime_type ) { |
| 24 |
return false; |
| 25 |
} |
| 26 |
|
| 27 |
return strpos( $mime_type, 'image/' ) === 0; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Check the images-only option |
| 32 |
* |
| 33 |
* @return bool True if images-only is enabled, false otherwise. |
| 34 |
*/ |
| 35 |
public static function enabled_images_only_option(): bool { |
| 36 |
$options = \ISC\Plugin::get_options(); |
| 37 |
|
| 38 |
// Check if images_only is enabled |
| 39 |
return ! empty( $options['images_only'] ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Check if we should process this attachment based on settings |
| 44 |
* |
| 45 |
* @param int|\WP_Post $attachment Attachment ID or post object. |
| 46 |
* |
| 47 |
* @return bool True if we should process this attachment, false otherwise. |
| 48 |
*/ |
| 49 |
public static function should_process_attachment( $attachment ): bool { |
| 50 |
// If images_only is enabled, only process images |
| 51 |
if ( self::enabled_images_only_option() ) { |
| 52 |
return self::is_image( $attachment ); |
| 53 |
} |
| 54 |
|
| 55 |
// Otherwise process all attachments |
| 56 |
return true; |
| 57 |
} |
| 58 |
} |
| 59 |
|