Edit File by line

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