Edit File by line
/home/barbar84/www/wp-conte.../plugins/wordpres.../src/helpers
File: image-helper.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Helpers;
[2] Fix | Delete
[3] Fix | Delete
use WPSEO_Image_Utils;
[4] Fix | Delete
use Yoast\WP\SEO\Repositories\Indexable_Repository;
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* A helper object for images.
[8] Fix | Delete
*/
[9] Fix | Delete
class Image_Helper {
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Image types that are supported by Open Graph.
[13] Fix | Delete
*
[14] Fix | Delete
* @var array
[15] Fix | Delete
*/
[16] Fix | Delete
protected static $valid_image_types = [ 'image/jpeg', 'image/gif', 'image/png' ];
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* Image extensions that are supported by Open Graph.
[20] Fix | Delete
*
[21] Fix | Delete
* @var array
[22] Fix | Delete
*/
[23] Fix | Delete
protected static $valid_image_extensions = [ 'jpeg', 'jpg', 'gif', 'png' ];
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Represents the indexables repository.
[27] Fix | Delete
*
[28] Fix | Delete
* @var Indexable_Repository
[29] Fix | Delete
*/
[30] Fix | Delete
protected $indexable_repository;
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* The options helper.
[34] Fix | Delete
*
[35] Fix | Delete
* @var Options_Helper
[36] Fix | Delete
*/
[37] Fix | Delete
private $options;
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Image_Helper constructor.
[41] Fix | Delete
*
[42] Fix | Delete
* @param Indexable_Repository $indexable_repository The indexable repository.
[43] Fix | Delete
* @param Options_Helper $options The options helper.
[44] Fix | Delete
*/
[45] Fix | Delete
public function __construct( Indexable_Repository $indexable_repository, Options_Helper $options ) {
[46] Fix | Delete
$this->indexable_repository = $indexable_repository;
[47] Fix | Delete
$this->options = $options;
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* Determines whether or not the wanted attachment is considered valid.
[52] Fix | Delete
*
[53] Fix | Delete
* @param int $attachment_id The attachment ID to get the attachment by.
[54] Fix | Delete
*
[55] Fix | Delete
* @return bool Whether or not the attachment is valid.
[56] Fix | Delete
*/
[57] Fix | Delete
public function is_valid_attachment( $attachment_id ) {
[58] Fix | Delete
if ( ! \wp_attachment_is_image( $attachment_id ) ) {
[59] Fix | Delete
return false;
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
$post_mime_type = \get_post_mime_type( $attachment_id );
[63] Fix | Delete
if ( $post_mime_type === false ) {
[64] Fix | Delete
return false;
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
return $this->is_valid_image_type( $post_mime_type );
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
/**
[71] Fix | Delete
* Checks if the given extension is a valid extension
[72] Fix | Delete
*
[73] Fix | Delete
* @param string $image_extension The image extension.
[74] Fix | Delete
*
[75] Fix | Delete
* @return bool True when valid.
[76] Fix | Delete
*/
[77] Fix | Delete
public function is_extension_valid( $image_extension ) {
[78] Fix | Delete
return \in_array( $image_extension, static::$valid_image_extensions, true );
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
/**
[82] Fix | Delete
* Determines whether the passed mime type is a valid image type.
[83] Fix | Delete
*
[84] Fix | Delete
* @param string $mime_type The detected mime type.
[85] Fix | Delete
*
[86] Fix | Delete
* @return bool Whether or not the attachment is a valid image type.
[87] Fix | Delete
*/
[88] Fix | Delete
public function is_valid_image_type( $mime_type ) {
[89] Fix | Delete
return \in_array( $mime_type, static::$valid_image_types, true );
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
/**
[93] Fix | Delete
* Retrieves the image source for an attachment.
[94] Fix | Delete
*
[95] Fix | Delete
* @param int $attachment_id The attachment.
[96] Fix | Delete
* @param string $image_size The image size to retrieve.
[97] Fix | Delete
*
[98] Fix | Delete
* @return string The image url or an empty string when not found.
[99] Fix | Delete
*/
[100] Fix | Delete
public function get_attachment_image_source( $attachment_id, $image_size = 'full' ) {
[101] Fix | Delete
$attachment = \wp_get_attachment_image_src( $attachment_id, $image_size );
[102] Fix | Delete
[103] Fix | Delete
if ( ! $attachment ) {
[104] Fix | Delete
return '';
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
return $attachment[0];
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
/**
[111] Fix | Delete
* Retrieves the ID of the featured image.
[112] Fix | Delete
*
[113] Fix | Delete
* @param int $post_id The post id to get featured image id for.
[114] Fix | Delete
*
[115] Fix | Delete
* @return int|bool ID when found, false when not.
[116] Fix | Delete
*/
[117] Fix | Delete
public function get_featured_image_id( $post_id ) {
[118] Fix | Delete
if ( ! \has_post_thumbnail( $post_id ) ) {
[119] Fix | Delete
return false;
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
return \get_post_thumbnail_id( $post_id );
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
/**
[126] Fix | Delete
* Gets the image url from the content.
[127] Fix | Delete
*
[128] Fix | Delete
* @param int $post_id The post id to extract the images from.
[129] Fix | Delete
*
[130] Fix | Delete
* @return string The image url or an empty string when not found.
[131] Fix | Delete
*/
[132] Fix | Delete
public function get_post_content_image( $post_id ) {
[133] Fix | Delete
$image_url = $this->get_first_usable_content_image_for_post( $post_id );
[134] Fix | Delete
[135] Fix | Delete
if ( $image_url === null ) {
[136] Fix | Delete
return '';
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
return $image_url;
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
/**
[143] Fix | Delete
* Gets the first image url of a gallery.
[144] Fix | Delete
*
[145] Fix | Delete
* @param int $post_id Post ID to use.
[146] Fix | Delete
*
[147] Fix | Delete
* @return string The image url or an empty string when not found.
[148] Fix | Delete
*/
[149] Fix | Delete
public function get_gallery_image( $post_id ) {
[150] Fix | Delete
$post = \get_post( $post_id );
[151] Fix | Delete
if ( \strpos( $post->post_content, '[gallery' ) === false ) {
[152] Fix | Delete
return '';
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
$images = \get_post_gallery_images( $post );
[156] Fix | Delete
if ( empty( $images ) ) {
[157] Fix | Delete
return '';
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
return \reset( $images );
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
/**
[164] Fix | Delete
* Gets the image url from the term content.
[165] Fix | Delete
*
[166] Fix | Delete
* @param int $term_id The term id to extract the images from.
[167] Fix | Delete
*
[168] Fix | Delete
* @return string The image url or an empty string when not found.
[169] Fix | Delete
*/
[170] Fix | Delete
public function get_term_content_image( $term_id ) {
[171] Fix | Delete
$image_url = $this->get_first_content_image_for_term( $term_id );
[172] Fix | Delete
[173] Fix | Delete
if ( $image_url === null ) {
[174] Fix | Delete
return '';
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
return $image_url;
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
/**
[181] Fix | Delete
* Retrieves the caption for an attachment.
[182] Fix | Delete
*
[183] Fix | Delete
* @param int $attachment_id Attachment ID.
[184] Fix | Delete
*
[185] Fix | Delete
* @return string The caption when found, empty string when no caption is found.
[186] Fix | Delete
*/
[187] Fix | Delete
public function get_caption( $attachment_id ) {
[188] Fix | Delete
$caption = \wp_get_attachment_caption( $attachment_id );
[189] Fix | Delete
if ( ! empty( $caption ) ) {
[190] Fix | Delete
return $caption;
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
$caption = \get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
[194] Fix | Delete
if ( ! empty( $caption ) ) {
[195] Fix | Delete
return $caption;
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
return '';
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
/**
[202] Fix | Delete
* Retrieves the attachment metadata.
[203] Fix | Delete
*
[204] Fix | Delete
* @param int $attachment_id Attachment ID.
[205] Fix | Delete
*
[206] Fix | Delete
* @return array The metadata, empty array when no metadata is found.
[207] Fix | Delete
*/
[208] Fix | Delete
public function get_metadata( $attachment_id ) {
[209] Fix | Delete
$metadata = \wp_get_attachment_metadata( $attachment_id );
[210] Fix | Delete
if ( ! $metadata || ! \is_array( $metadata ) ) {
[211] Fix | Delete
return [];
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
return $metadata;
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
/**
[218] Fix | Delete
* Retrieves the attachment image url.
[219] Fix | Delete
*
[220] Fix | Delete
* @param int $attachment_id Attachment ID.
[221] Fix | Delete
* @param string $size The size to get.
[222] Fix | Delete
*
[223] Fix | Delete
* @return string The url when found, empty string otherwise.
[224] Fix | Delete
*/
[225] Fix | Delete
public function get_attachment_image_url( $attachment_id, $size ) {
[226] Fix | Delete
$url = \wp_get_attachment_image_url( $attachment_id, $size );
[227] Fix | Delete
if ( ! $url ) {
[228] Fix | Delete
return '';
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
return $url;
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
/**
[235] Fix | Delete
* Find the right version of an image based on size.
[236] Fix | Delete
*
[237] Fix | Delete
* @codeCoverageIgnore - We have to write test when this method contains own code.
[238] Fix | Delete
*
[239] Fix | Delete
* @param int $attachment_id Attachment ID.
[240] Fix | Delete
* @param string $size Size name.
[241] Fix | Delete
*
[242] Fix | Delete
* @return array|false Returns an array with image data on success, false on failure.
[243] Fix | Delete
*/
[244] Fix | Delete
public function get_image( $attachment_id, $size ) {
[245] Fix | Delete
return WPSEO_Image_Utils::get_image( $attachment_id, $size );
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
/**
[249] Fix | Delete
* Retrieves the best attachment variation for the given attachment.
[250] Fix | Delete
*
[251] Fix | Delete
* @codeCoverageIgnore - We have to write test when this method contains own code.
[252] Fix | Delete
*
[253] Fix | Delete
* @param int $attachment_id The attachment id.
[254] Fix | Delete
*
[255] Fix | Delete
* @return bool|string The attachment url or false when no variations found.
[256] Fix | Delete
*/
[257] Fix | Delete
public function get_best_attachment_variation( $attachment_id ) {
[258] Fix | Delete
$variations = WPSEO_Image_Utils::get_variations( $attachment_id );
[259] Fix | Delete
$variations = WPSEO_Image_Utils::filter_usable_file_size( $variations );
[260] Fix | Delete
[261] Fix | Delete
// If we are left without variations, there is no valid variation for this attachment.
[262] Fix | Delete
if ( empty( $variations ) ) {
[263] Fix | Delete
return false;
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
// The variations are ordered so the first variations is by definition the best one.
[267] Fix | Delete
return \reset( $variations );
[268] Fix | Delete
}
[269] Fix | Delete
[270] Fix | Delete
/**
[271] Fix | Delete
* Find an attachment ID for a given URL.
[272] Fix | Delete
*
[273] Fix | Delete
* @param string $url The URL to find the attachment for.
[274] Fix | Delete
*
[275] Fix | Delete
* @return int The found attachment ID, or 0 if none was found.
[276] Fix | Delete
*/
[277] Fix | Delete
public function get_attachment_by_url( $url ) {
[278] Fix | Delete
// Strip out the size part of an image URL.
[279] Fix | Delete
$url = \preg_replace( '/(.*)-\d+x\d+\.(jpeg|jpg|png|gif)$/', '$1.$2', $url );
[280] Fix | Delete
[281] Fix | Delete
// Don't try to do this for external URLs.
[282] Fix | Delete
if ( \strpos( $url, \get_site_url() ) !== 0 ) {
[283] Fix | Delete
return 0;
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
$indexable = $this->indexable_repository->find_by_permalink( $url );
[287] Fix | Delete
[288] Fix | Delete
if ( $indexable && $indexable->object_type === 'post' && $indexable->object_sub_type === 'attachment' ) {
[289] Fix | Delete
return $indexable->object_id;
[290] Fix | Delete
}
[291] Fix | Delete
[292] Fix | Delete
$post_id = WPSEO_Image_Utils::get_attachment_by_url( $url );
[293] Fix | Delete
[294] Fix | Delete
if ( $post_id !== 0 ) {
[295] Fix | Delete
// Find the indexable, this triggers creating it so it can be found next time.
[296] Fix | Delete
$this->indexable_repository->find_by_id_and_type( $post_id, 'post' );
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
return $post_id;
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
/**
[303] Fix | Delete
* Retrieves an attachment ID for an image uploaded in the settings.
[304] Fix | Delete
*
[305] Fix | Delete
* Due to self::get_attachment_by_url returning 0 instead of false.
[306] Fix | Delete
* 0 is also a possibility when no ID is available.
[307] Fix | Delete
*
[308] Fix | Delete
* @codeCoverageIgnore - We have to write test when this method contains own code.
[309] Fix | Delete
*
[310] Fix | Delete
* @param string $setting The setting the image is stored in.
[311] Fix | Delete
*
[312] Fix | Delete
* @return int|bool The attachment id, or false or 0 if no ID is available.
[313] Fix | Delete
*/
[314] Fix | Delete
public function get_attachment_id_from_settings( $setting ) {
[315] Fix | Delete
return WPSEO_Image_Utils::get_attachment_id_from_settings( $setting );
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
/**
[319] Fix | Delete
* Based on and image ID return array with the best variation of that image. If it's not saved to the DB, save it to an option.
[320] Fix | Delete
*
[321] Fix | Delete
* @param string $setting The setting name. Should be company or person.
[322] Fix | Delete
*
[323] Fix | Delete
* @return array|bool Array with image details when the image is found, boolean when it's not found.
[324] Fix | Delete
*/
[325] Fix | Delete
public function get_attachment_meta_from_settings( $setting ) {
[326] Fix | Delete
$image_meta = $this->options->get( $setting . '_meta', false );
[327] Fix | Delete
if ( ! $image_meta ) {
[328] Fix | Delete
$image_id = $this->options->get( $setting . '_id', false );
[329] Fix | Delete
if ( $image_id ) {
[330] Fix | Delete
// There is not an option to put a URL in an image field in the settings anymore, only to upload it through the media manager.
[331] Fix | Delete
// This means an attachment always exists, so doing this is only needed once.
[332] Fix | Delete
$image_meta = $this->get_best_attachment_variation( $image_id );
[333] Fix | Delete
if ( $image_meta ) {
[334] Fix | Delete
$this->options->set( $setting . '_meta', $image_meta );
[335] Fix | Delete
}
[336] Fix | Delete
}
[337] Fix | Delete
}
[338] Fix | Delete
[339] Fix | Delete
return $image_meta;
[340] Fix | Delete
}
[341] Fix | Delete
[342] Fix | Delete
/**
[343] Fix | Delete
* Retrieves the first usable content image for a post.
[344] Fix | Delete
*
[345] Fix | Delete
* @codeCoverageIgnore - We have to write test when this method contains own code.
[346] Fix | Delete
*
[347] Fix | Delete
* @param int $post_id The post id to extract the images from.
[348] Fix | Delete
*
[349] Fix | Delete
* @return string|null
[350] Fix | Delete
*/
[351] Fix | Delete
protected function get_first_usable_content_image_for_post( $post_id ) {
[352] Fix | Delete
return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id );
[353] Fix | Delete
}
[354] Fix | Delete
[355] Fix | Delete
/**
[356] Fix | Delete
* Gets the term's first usable content image. Null if none is available.
[357] Fix | Delete
*
[358] Fix | Delete
* @codeCoverageIgnore - We have to write test when this method contains own code.
[359] Fix | Delete
*
[360] Fix | Delete
* @param int $term_id The term id.
[361] Fix | Delete
*
[362] Fix | Delete
* @return string|null The image URL.
[363] Fix | Delete
*/
[364] Fix | Delete
protected function get_first_content_image_for_term( $term_id ) {
[365] Fix | Delete
return WPSEO_Image_Utils::get_first_content_image_for_term( $term_id );
[366] Fix | Delete
}
[367] Fix | Delete
}
[368] Fix | Delete
[369] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function