| 1 |
<?php |
| 2 |
/** |
| 3 |
* Image class. |
| 4 |
* |
| 5 |
* @package WPZinc\Social |
| 6 |
* @author WP Zinc |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPZinc\Social; |
| 10 |
|
| 11 |
/** |
| 12 |
* Determines optimal image sizes and aspect ratios for each |
| 13 |
* social networks, detects if such sizes are registered |
| 14 |
* in WordPress and (where possible) resizes and crops |
| 15 |
* images. |
| 16 |
* |
| 17 |
* @package WPZinc\Social |
| 18 |
* @author WP Zinc |
| 19 |
* @version 4.6.6 |
| 20 |
*/ |
| 21 |
class Image { |
| 22 |
|
| 23 |
/** |
| 24 |
* Holds the base class object. |
| 25 |
* |
| 26 |
* @since 4.6.6 |
| 27 |
* |
| 28 |
* @var object |
| 29 |
*/ |
| 30 |
public $base; |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor |
| 34 |
* |
| 35 |
* @since 4.6.6 |
| 36 |
* |
| 37 |
* @param object $base Base Plugin Class. |
| 38 |
*/ |
| 39 |
public function __construct( $base ) { |
| 40 |
|
| 41 |
// Store base class. |
| 42 |
$this->base = $base; |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Helper method to retrieve Image Options |
| 48 |
* |
| 49 |
* @since 3.4.3 |
| 50 |
* |
| 51 |
* @param bool $network Network (false = defaults). |
| 52 |
* @param bool|string $post_type Post Type. |
| 53 |
* @return array Image Options |
| 54 |
*/ |
| 55 |
public function get_status_image_options( $network = false, $post_type = false ) { |
| 56 |
|
| 57 |
// If a Post Type has been specified, get its featured_image label. |
| 58 |
$label = __( 'Feat. Image', 'wp-to-buffer' ); |
| 59 |
if ( $post_type !== false && $post_type !== 'bulk' ) { |
| 60 |
$post_type_object = get_post_type_object( $post_type ); |
| 61 |
$label = $post_type_object->labels->featured_image; |
| 62 |
} |
| 63 |
|
| 64 |
// Build featured image options. |
| 65 |
$options = array( |
| 66 |
'featured_image' => array( |
| 67 |
'label' => $label, |
| 68 |
'additional_images' => true, |
| 69 |
'text_to_image' => false, |
| 70 |
), |
| 71 |
); |
| 72 |
|
| 73 |
/** |
| 74 |
* Defines the available Featured Image select dropdown options on a status, depending |
| 75 |
* on the Plugin and Social Network the status message is for. |
| 76 |
* |
| 77 |
* @since 3.4.3 |
| 78 |
* |
| 79 |
* @param array $options Featured Image Dropdown Options. |
| 80 |
* @param bool|string $network Social Network. |
| 81 |
* @param string $post_type Post Type. |
| 82 |
*/ |
| 83 |
$options = apply_filters( $this->base->plugin->filter_name . '_get_status_image_options', $options, $network, $post_type ); |
| 84 |
|
| 85 |
// Return filtered results. |
| 86 |
return $options; |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Returns the image for the given Attachment ID, based on the social media service |
| 92 |
* the image will be used for. |
| 93 |
* |
| 94 |
* If the image isn't a compatible mime type, this function will attempt to convert the image |
| 95 |
* from e.g. webp --> jpg. |
| 96 |
* |
| 97 |
* Checks that the image will meet the aspect ratio requirements for posting to Instagram, |
| 98 |
* returning a valid image if the large size would fail. |
| 99 |
* |
| 100 |
* @since 4.6.6 |
| 101 |
* |
| 102 |
* @param int $image_id Image ID. |
| 103 |
* @param string $source Source Image ID was derived from (plugin, featured_image, post_content, text_to_image). |
| 104 |
* @param bool|string $service Social Media Service the image is for. If not defined, just return the large version. |
| 105 |
* @param bool|string $status_post_type Status format (for example, 'story' or 'post' for Instagram). |
| 106 |
* @return array|\WP_Error Image ID, Image URLs, Source |
| 107 |
*/ |
| 108 |
public function get_image_sources( $image_id, $source, $service = false, $status_post_type = false ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 109 |
|
| 110 |
// Load WordPress image libraries, if they are not currently loaded. |
| 111 |
if ( ! class_exists( 'WP_Image_Editor' ) ) { |
| 112 |
require_once ABSPATH . WPINC . '/class-wp-image-editor.php'; |
| 113 |
require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php'; |
| 114 |
require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php'; |
| 115 |
} |
| 116 |
|
| 117 |
$image_mime_type = get_post_mime_type( $image_id ); |
| 118 |
|
| 119 |
// If the image is a webp, attempt to convert it to a JPEG and store in the Media Library |
| 120 |
// as webp isn't supported by all social media services. |
| 121 |
// Check that the image source is a supported format i.e. not a webp. |
| 122 |
switch ( $image_mime_type ) { |
| 123 |
/** |
| 124 |
* Webp |
| 125 |
*/ |
| 126 |
case 'image/webp': |
| 127 |
// Don't do anything if the service supports webp and the image isn't for Instagram. |
| 128 |
// If it is for Instagram, we want to convert to a JPEG as we might need to resize/crop |
| 129 |
// later in this function. |
| 130 |
if ( $this->base->supports( 'webp' ) && $service !== 'instagram' ) { |
| 131 |
break; |
| 132 |
} |
| 133 |
|
| 134 |
// Get image. |
| 135 |
$image_path_and_file = get_attached_file( $image_id ); |
| 136 |
|
| 137 |
// Return the large image sources if we couldn't get the image path and file. |
| 138 |
if ( empty( $image_path_and_file ) || ! file_exists( $image_path_and_file ) ) { |
| 139 |
return $this->get_image_source_by_size( $image_id, $source, 'large' ); |
| 140 |
} |
| 141 |
|
| 142 |
// Load webp image. |
| 143 |
$image = wp_get_image_editor( $image_path_and_file ); |
| 144 |
|
| 145 |
// Bail if an error occured. |
| 146 |
if ( is_wp_error( $image ) ) { |
| 147 |
return $image; |
| 148 |
} |
| 149 |
|
| 150 |
// Save to temporary file on disk. |
| 151 |
$converted_image = $image->save( get_temp_dir() . 'wpzinc-social-' . $image_id . '-converted-' . bin2hex( random_bytes( 5 ) ) ); |
| 152 |
|
| 153 |
// Bail if an error occured. |
| 154 |
if ( is_wp_error( $converted_image ) ) { |
| 155 |
return $converted_image; |
| 156 |
} |
| 157 |
|
| 158 |
// Upload to Media Library. |
| 159 |
$converted_image_id = $this->base->get_class( 'media_library' )->upload_local_image( $converted_image['path'] ); |
| 160 |
|
| 161 |
// Bail if an error occured. |
| 162 |
if ( is_wp_error( $converted_image_id ) ) { |
| 163 |
return $converted_image_id; |
| 164 |
} |
| 165 |
|
| 166 |
// Assign image ID. |
| 167 |
$image_id = $converted_image_id; |
| 168 |
break; |
| 169 |
|
| 170 |
default: |
| 171 |
/** |
| 172 |
* Defines the image ID to use as the image or additional image for the status message. |
| 173 |
* If an image's mime type is not supported by the social media scheduling service, this |
| 174 |
* filter can be used to convert the image to a supported type, store it in the Media Library |
| 175 |
* and return the converted image ID. |
| 176 |
* |
| 177 |
* This is already performed for webp images. |
| 178 |
* |
| 179 |
* @since 4.6.8 |
| 180 |
* |
| 181 |
* @param int $image_id Image ID. |
| 182 |
* @param string $source Source Image ID was derived from (plugin, featured_image, post_content, text_to_image). |
| 183 |
* @param string $service Social Media Service the image is for. If not defined, just return the large version. |
| 184 |
* @param string $image_mime_type Image MIME Type. |
| 185 |
*/ |
| 186 |
$image_id = apply_filters( $this->base->plugin->filter_name . '_image_get_images_sources_convert', $image_id, $source, $service, $image_mime_type ); |
| 187 |
break; |
| 188 |
} |
| 189 |
|
| 190 |
return $this->get_image_source_by_size( $image_id, $source, 'large' ); |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Returns an array comprising of the image ID, image URL and alt text for the requested size, thumbnail size |
| 196 |
* and the source of the image. |
| 197 |
* |
| 198 |
* @since 4.6.6 |
| 199 |
* |
| 200 |
* @param int $image_id Image ID. |
| 201 |
* @param string $source Source Image ID was derived from (plugin, featured_image, post_content, text_to_image). |
| 202 |
* @param string $size WordPress Registered Image Size to return the image as. |
| 203 |
* @return array Image ID, Image URLs, Source |
| 204 |
*/ |
| 205 |
private function get_image_source_by_size( $image_id, $source, $size ) { |
| 206 |
|
| 207 |
// Get image at requested size. |
| 208 |
$image = wp_get_attachment_image_src( $image_id, $size ); |
| 209 |
|
| 210 |
// Get thumbnail version, which some APIs might use for a small preview. |
| 211 |
$thumbnail = wp_get_attachment_image_src( $image_id, 'thumbnail' ); |
| 212 |
|
| 213 |
// Return URLs only. |
| 214 |
return array( |
| 215 |
'id' => $image_id, |
| 216 |
'image' => ( is_array( $image ) ? strtok( $image[0], '?' ) : false ), // Strip query parameters that might break some APIs. |
| 217 |
'thumbnail' => ( is_array( $thumbnail ) ? strtok( $thumbnail[0], '?' ) : false ), // Strip query parameters that might break some APIs. |
| 218 |
'alt_text' => get_post_meta( $image_id, '_wp_attachment_image_alt', true ), |
| 219 |
'source' => $source, |
| 220 |
'width' => ( is_array( $image ) ? $image[1] : '' ), |
| 221 |
'height' => ( is_array( $image ) ? $image[2] : '' ), |
| 222 |
); |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
|