base = $base; } /** * Helper method to retrieve Image Options * * @since 3.4.3 * * @param bool $network Network (false = defaults). * @param bool|string $post_type Post Type. * @return array Image Options */ public function get_status_image_options( $network = false, $post_type = false ) { // If a Post Type has been specified, get its featured_image label. $label = __( 'Feat. Image', 'wp-to-buffer' ); if ( $post_type !== false && $post_type !== 'bulk' ) { $post_type_object = get_post_type_object( $post_type ); $label = $post_type_object->labels->featured_image; } // Build featured image options. $options = array( 'featured_image' => array( 'label' => $label, 'additional_images' => true, 'text_to_image' => false, ), ); /** * Defines the available Featured Image select dropdown options on a status, depending * on the Plugin and Social Network the status message is for. * * @since 3.4.3 * * @param array $options Featured Image Dropdown Options. * @param bool|string $network Social Network. * @param string $post_type Post Type. */ $options = apply_filters( $this->base->plugin->filter_name . '_get_status_image_options', $options, $network, $post_type ); // Return filtered results. return $options; } /** * Returns the image for the given Attachment ID, based on the social media service * the image will be used for. * * If the image isn't a compatible mime type, this function will attempt to convert the image * from e.g. webp --> jpg. * * Checks that the image will meet the aspect ratio requirements for posting to Instagram, * returning a valid image if the large size would fail. * * @since 4.6.6 * * @param int $image_id Image ID. * @param string $source Source Image ID was derived from (plugin, featured_image, post_content, text_to_image). * @param bool|string $service Social Media Service the image is for. If not defined, just return the large version. * @param bool|string $status_post_type Status format (for example, 'story' or 'post' for Instagram). * @return array|\WP_Error Image ID, Image URLs, Source */ public function get_image_sources( $image_id, $source, $service = false, $status_post_type = false ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed // Load WordPress image libraries, if they are not currently loaded. if ( ! class_exists( 'WP_Image_Editor' ) ) { require_once ABSPATH . WPINC . '/class-wp-image-editor.php'; require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php'; require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php'; } $image_mime_type = get_post_mime_type( $image_id ); // If the image is a webp, attempt to convert it to a JPEG and store in the Media Library // as webp isn't supported by all social media services. // Check that the image source is a supported format i.e. not a webp. switch ( $image_mime_type ) { /** * Webp */ case 'image/webp': // Don't do anything if the service supports webp and the image isn't for Instagram. // If it is for Instagram, we want to convert to a JPEG as we might need to resize/crop // later in this function. if ( $this->base->supports( 'webp' ) && $service !== 'instagram' ) { break; } // Get image. $image_path_and_file = get_attached_file( $image_id ); // Return the large image sources if we couldn't get the image path and file. if ( empty( $image_path_and_file ) || ! file_exists( $image_path_and_file ) ) { return $this->get_image_source_by_size( $image_id, $source, 'large' ); } // Load webp image. $image = wp_get_image_editor( $image_path_and_file ); // Bail if an error occured. if ( is_wp_error( $image ) ) { return $image; } // Save to temporary file on disk. $converted_image = $image->save( get_temp_dir() . 'wpzinc-social-' . $image_id . '-converted-' . bin2hex( random_bytes( 5 ) ) ); // Bail if an error occured. if ( is_wp_error( $converted_image ) ) { return $converted_image; } // Upload to Media Library. $converted_image_id = $this->base->get_class( 'media_library' )->upload_local_image( $converted_image['path'] ); // Bail if an error occured. if ( is_wp_error( $converted_image_id ) ) { return $converted_image_id; } // Assign image ID. $image_id = $converted_image_id; break; default: /** * Defines the image ID to use as the image or additional image for the status message. * If an image's mime type is not supported by the social media scheduling service, this * filter can be used to convert the image to a supported type, store it in the Media Library * and return the converted image ID. * * This is already performed for webp images. * * @since 4.6.8 * * @param int $image_id Image ID. * @param string $source Source Image ID was derived from (plugin, featured_image, post_content, text_to_image). * @param string $service Social Media Service the image is for. If not defined, just return the large version. * @param string $image_mime_type Image MIME Type. */ $image_id = apply_filters( $this->base->plugin->filter_name . '_image_get_images_sources_convert', $image_id, $source, $service, $image_mime_type ); break; } return $this->get_image_source_by_size( $image_id, $source, 'large' ); } /** * Returns an array comprising of the image ID, image URL and alt text for the requested size, thumbnail size * and the source of the image. * * @since 4.6.6 * * @param int $image_id Image ID. * @param string $source Source Image ID was derived from (plugin, featured_image, post_content, text_to_image). * @param string $size WordPress Registered Image Size to return the image as. * @return array Image ID, Image URLs, Source */ private function get_image_source_by_size( $image_id, $source, $size ) { // Get image at requested size. $image = wp_get_attachment_image_src( $image_id, $size ); // Get thumbnail version, which some APIs might use for a small preview. $thumbnail = wp_get_attachment_image_src( $image_id, 'thumbnail' ); // Return URLs only. return array( 'id' => $image_id, 'image' => ( is_array( $image ) ? strtok( $image[0], '?' ) : false ), // Strip query parameters that might break some APIs. 'thumbnail' => ( is_array( $thumbnail ) ? strtok( $thumbnail[0], '?' ) : false ), // Strip query parameters that might break some APIs. 'alt_text' => get_post_meta( $image_id, '_wp_attachment_image_alt', true ), 'source' => $source, 'width' => ( is_array( $image ) ? $image[1] : '' ), 'height' => ( is_array( $image ) ? $image[2] : '' ), ); } }