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/wp-conte.../plugins/wordpres.../src/helpers/schema
File: image-helper.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Helpers\Schema;
[2] Fix | Delete
[3] Fix | Delete
use Yoast\WP\SEO\Helpers\Image_Helper as Main_Image_Helper;
[4] Fix | Delete
[5] Fix | Delete
/**
[6] Fix | Delete
* Class Image_Helper.
[7] Fix | Delete
*/
[8] Fix | Delete
class Image_Helper {
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* The HTML helper.
[12] Fix | Delete
*
[13] Fix | Delete
* @var HTML_Helper
[14] Fix | Delete
*/
[15] Fix | Delete
private $html;
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* The language helper.
[19] Fix | Delete
*
[20] Fix | Delete
* @var Language_Helper
[21] Fix | Delete
*/
[22] Fix | Delete
private $language;
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* Represents the main image helper.
[26] Fix | Delete
*
[27] Fix | Delete
* @var Main_Image_Helper
[28] Fix | Delete
*/
[29] Fix | Delete
private $image;
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Image_Helper constructor.
[33] Fix | Delete
*
[34] Fix | Delete
* @codeCoverageIgnore It handles dependencies.
[35] Fix | Delete
*
[36] Fix | Delete
* @param HTML_Helper $html The HTML helper.
[37] Fix | Delete
* @param Language_Helper $language The language helper.
[38] Fix | Delete
* @param Main_Image_Helper $image The 'main' image helper.
[39] Fix | Delete
*/
[40] Fix | Delete
public function __construct( HTML_Helper $html, Language_Helper $language, Main_Image_Helper $image ) {
[41] Fix | Delete
$this->html = $html;
[42] Fix | Delete
$this->language = $language;
[43] Fix | Delete
$this->image = $image;
[44] Fix | Delete
}
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* Find an image based on its URL and generate a Schema object for it.
[48] Fix | Delete
*
[49] Fix | Delete
* @param string $schema_id The `@id` to use for the returned image.
[50] Fix | Delete
* @param string $url The image URL to base our object on.
[51] Fix | Delete
* @param string $caption An optional caption.
[52] Fix | Delete
* @param bool $add_hash Whether a hash will be added as a suffix in the @id.
[53] Fix | Delete
* @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id.
[54] Fix | Delete
*
[55] Fix | Delete
* @return array Schema ImageObject array.
[56] Fix | Delete
*/
[57] Fix | Delete
public function generate_from_url( $schema_id, $url, $caption = '', $add_hash = false, $use_link_table = true ) {
[58] Fix | Delete
$attachment_id = $this->image->get_attachment_by_url( $url, $use_link_table );
[59] Fix | Delete
if ( $attachment_id > 0 ) {
[60] Fix | Delete
return $this->generate_from_attachment_id( $schema_id, $attachment_id, $caption, $add_hash );
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
return $this->simple_image_object( $schema_id, $url, $caption, $add_hash );
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
/**
[67] Fix | Delete
* Retrieve data about an image from the database and use it to generate a Schema object.
[68] Fix | Delete
*
[69] Fix | Delete
* @param string $schema_id The `@id` to use for the returned image.
[70] Fix | Delete
* @param int $attachment_id The attachment to retrieve data from.
[71] Fix | Delete
* @param string $caption The caption string, if there is one.
[72] Fix | Delete
* @param bool $add_hash Whether a hash will be added as a suffix in the @id.
[73] Fix | Delete
*
[74] Fix | Delete
* @return array Schema ImageObject array.
[75] Fix | Delete
*/
[76] Fix | Delete
public function generate_from_attachment_id( $schema_id, $attachment_id, $caption = '', $add_hash = false ) {
[77] Fix | Delete
$data = $this->generate_object();
[78] Fix | Delete
$url = $this->image->get_attachment_image_url( $attachment_id, 'full' );
[79] Fix | Delete
[80] Fix | Delete
$id_suffix = ( $add_hash ) ? \md5( $url ) : '';
[81] Fix | Delete
[82] Fix | Delete
$data['@id'] = $schema_id . $id_suffix;
[83] Fix | Delete
$data['url'] = $url;
[84] Fix | Delete
$data['contentUrl'] = $url;
[85] Fix | Delete
$data = $this->add_image_size( $data, $attachment_id );
[86] Fix | Delete
$data = $this->add_caption( $data, $attachment_id, $caption );
[87] Fix | Delete
[88] Fix | Delete
return $data;
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
/**
[92] Fix | Delete
* Retrieve data about an image from the database and use it to generate a Schema object.
[93] Fix | Delete
*
[94] Fix | Delete
* @param string $schema_id The `@id` to use for the returned image.
[95] Fix | Delete
* @param array $attachment_meta The attachment metadata.
[96] Fix | Delete
* @param string $caption The caption string, if there is one.
[97] Fix | Delete
* @param bool $add_hash Whether a hash will be added as a suffix in the @id.
[98] Fix | Delete
*
[99] Fix | Delete
* @return array Schema ImageObject array.
[100] Fix | Delete
*/
[101] Fix | Delete
public function generate_from_attachment_meta( $schema_id, $attachment_meta, $caption = '', $add_hash = false ) {
[102] Fix | Delete
$data = $this->generate_object();
[103] Fix | Delete
[104] Fix | Delete
$id_suffix = ( $add_hash ) ? \md5( $attachment_meta['url'] ) : '';
[105] Fix | Delete
[106] Fix | Delete
$data['@id'] = $schema_id . $id_suffix;
[107] Fix | Delete
$data['url'] = $attachment_meta['url'];
[108] Fix | Delete
$data['contentUrl'] = $data['url'];
[109] Fix | Delete
$data['width'] = $attachment_meta['width'];
[110] Fix | Delete
$data['height'] = $attachment_meta['height'];
[111] Fix | Delete
if ( ! empty( $caption ) ) {
[112] Fix | Delete
$data['caption'] = $this->html->smart_strip_tags( $caption );
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
return $data;
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
/**
[119] Fix | Delete
* If we can't find $url in our database, we output a simple ImageObject.
[120] Fix | Delete
*
[121] Fix | Delete
* @param string $schema_id The `@id` to use for the returned image.
[122] Fix | Delete
* @param string $url The image URL.
[123] Fix | Delete
* @param string $caption A caption, if set.
[124] Fix | Delete
* @param bool $add_hash Whether a hash will be added as a suffix in the @id.
[125] Fix | Delete
*
[126] Fix | Delete
* @return array Schema ImageObject array.
[127] Fix | Delete
*/
[128] Fix | Delete
public function simple_image_object( $schema_id, $url, $caption = '', $add_hash = false ) {
[129] Fix | Delete
$data = $this->generate_object();
[130] Fix | Delete
[131] Fix | Delete
$id_suffix = ( $add_hash ) ? \md5( $url ) : '';
[132] Fix | Delete
[133] Fix | Delete
$data['@id'] = $schema_id . $id_suffix;
[134] Fix | Delete
$data['url'] = $url;
[135] Fix | Delete
$data['contentUrl'] = $url;
[136] Fix | Delete
[137] Fix | Delete
if ( ! empty( $caption ) ) {
[138] Fix | Delete
$data['caption'] = $this->html->smart_strip_tags( $caption );
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
return $data;
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
/**
[145] Fix | Delete
* Retrieves an image's caption if set, or uses the alt tag if that's set.
[146] Fix | Delete
*
[147] Fix | Delete
* @param array $data An ImageObject Schema array.
[148] Fix | Delete
* @param int $attachment_id Attachment ID.
[149] Fix | Delete
* @param string $caption The caption string, if there is one.
[150] Fix | Delete
*
[151] Fix | Delete
* @return array An imageObject with width and height set if available.
[152] Fix | Delete
*/
[153] Fix | Delete
private function add_caption( $data, $attachment_id, $caption = '' ) {
[154] Fix | Delete
if ( $caption !== '' ) {
[155] Fix | Delete
$data['caption'] = $caption;
[156] Fix | Delete
[157] Fix | Delete
return $data;
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
$caption = $this->image->get_caption( $attachment_id );
[161] Fix | Delete
if ( ! empty( $caption ) ) {
[162] Fix | Delete
$data['caption'] = $this->html->smart_strip_tags( $caption );
[163] Fix | Delete
[164] Fix | Delete
return $data;
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
return $data;
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
/**
[171] Fix | Delete
* Generates our bare bone ImageObject.
[172] Fix | Delete
*
[173] Fix | Delete
* @return array an empty ImageObject
[174] Fix | Delete
*/
[175] Fix | Delete
private function generate_object() {
[176] Fix | Delete
$data = [
[177] Fix | Delete
'@type' => 'ImageObject',
[178] Fix | Delete
];
[179] Fix | Delete
[180] Fix | Delete
$data = $this->language->add_piece_language( $data );
[181] Fix | Delete
[182] Fix | Delete
return $data;
[183] Fix | Delete
}
[184] Fix | Delete
[185] Fix | Delete
/**
[186] Fix | Delete
* Adds image's width and height.
[187] Fix | Delete
*
[188] Fix | Delete
* @param array $data An ImageObject Schema array.
[189] Fix | Delete
* @param int $attachment_id Attachment ID.
[190] Fix | Delete
*
[191] Fix | Delete
* @return array An imageObject with width and height set if available.
[192] Fix | Delete
*/
[193] Fix | Delete
private function add_image_size( $data, $attachment_id ) {
[194] Fix | Delete
$image_meta = $this->image->get_metadata( $attachment_id );
[195] Fix | Delete
if ( empty( $image_meta['width'] ) || empty( $image_meta['height'] ) ) {
[196] Fix | Delete
return $data;
[197] Fix | Delete
}
[198] Fix | Delete
$data['width'] = $image_meta['width'];
[199] Fix | Delete
$data['height'] = $image_meta['height'];
[200] Fix | Delete
[201] Fix | Delete
return $data;
[202] Fix | Delete
}
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function