Edit File by line
/home/barbar84/www/wp-conte.../plugins/wordpres.../src/values
File: images.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Values;
[2] Fix | Delete
[3] Fix | Delete
use Yoast\WP\SEO\Helpers\Image_Helper;
[4] Fix | Delete
use Yoast\WP\SEO\Helpers\Url_Helper;
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Class Images
[8] Fix | Delete
*
[9] Fix | Delete
* Value object for the Images.
[10] Fix | Delete
*/
[11] Fix | Delete
class Images {
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* The image size.
[15] Fix | Delete
*
[16] Fix | Delete
* @var string
[17] Fix | Delete
*/
[18] Fix | Delete
public $image_size = 'full';
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Holds the images that have been put out as image.
[22] Fix | Delete
*
[23] Fix | Delete
* @var array
[24] Fix | Delete
*/
[25] Fix | Delete
protected $images = [];
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* The image helper.
[29] Fix | Delete
*
[30] Fix | Delete
* @var Image_Helper
[31] Fix | Delete
*/
[32] Fix | Delete
protected $image;
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* The URL helper.
[36] Fix | Delete
*
[37] Fix | Delete
* @var Url_Helper
[38] Fix | Delete
*/
[39] Fix | Delete
protected $url;
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* Images constructor.
[43] Fix | Delete
*
[44] Fix | Delete
* @codeCoverageIgnore
[45] Fix | Delete
*
[46] Fix | Delete
* @param Image_Helper $image The image helper.
[47] Fix | Delete
* @param Url_Helper $url The url helper.
[48] Fix | Delete
*/
[49] Fix | Delete
public function __construct( Image_Helper $image, Url_Helper $url ) {
[50] Fix | Delete
$this->image = $image;
[51] Fix | Delete
$this->url = $url;
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* Adds an image to the list by image ID.
[56] Fix | Delete
*
[57] Fix | Delete
* @param int $image_id The image ID to add.
[58] Fix | Delete
*
[59] Fix | Delete
* @return void
[60] Fix | Delete
*/
[61] Fix | Delete
public function add_image_by_id( $image_id ) {
[62] Fix | Delete
$image = $this->image->get_attachment_image_source( $image_id, $this->image_size );
[63] Fix | Delete
if ( $image ) {
[64] Fix | Delete
$this->add_image( $image );
[65] Fix | Delete
}
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* Adds an image to the list by image ID.
[70] Fix | Delete
*
[71] Fix | Delete
* @param string $image_meta JSON encoded image meta.
[72] Fix | Delete
*
[73] Fix | Delete
* @return void
[74] Fix | Delete
*/
[75] Fix | Delete
public function add_image_by_meta( $image_meta ) {
[76] Fix | Delete
$this->add_image( (array) \json_decode( $image_meta ) );
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
/**
[80] Fix | Delete
* Return the images array.
[81] Fix | Delete
*
[82] Fix | Delete
* @return array The images.
[83] Fix | Delete
*/
[84] Fix | Delete
public function get_images() {
[85] Fix | Delete
return $this->images;
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* Check whether we have images or not.
[90] Fix | Delete
*
[91] Fix | Delete
* @return bool True if we have images, false if we don't.
[92] Fix | Delete
*/
[93] Fix | Delete
public function has_images() {
[94] Fix | Delete
return ! empty( $this->images );
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
/**
[98] Fix | Delete
* Adds an image based on a given URL.
[99] Fix | Delete
*
[100] Fix | Delete
* @param string $url The given URL.
[101] Fix | Delete
*
[102] Fix | Delete
* @return number|null Returns the found image ID if it exists. Otherwise -1.
[103] Fix | Delete
* If the URL is empty we return null.
[104] Fix | Delete
*/
[105] Fix | Delete
public function add_image_by_url( $url ) {
[106] Fix | Delete
if ( empty( $url ) ) {
[107] Fix | Delete
return null;
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
$image_id = $this->image->get_attachment_by_url( $url );
[111] Fix | Delete
[112] Fix | Delete
if ( $image_id ) {
[113] Fix | Delete
$this->add_image_by_id( $image_id );
[114] Fix | Delete
[115] Fix | Delete
return $image_id;
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
$this->add_image( $url );
[119] Fix | Delete
[120] Fix | Delete
return -1;
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
/**
[124] Fix | Delete
* Adds an image to the list of images.
[125] Fix | Delete
*
[126] Fix | Delete
* @param string|array $image Image array.
[127] Fix | Delete
*
[128] Fix | Delete
* @return void
[129] Fix | Delete
*/
[130] Fix | Delete
public function add_image( $image ) {
[131] Fix | Delete
if ( \is_string( $image ) ) {
[132] Fix | Delete
$image = [ 'url' => $image ];
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
if ( ! \is_array( $image ) || empty( $image['url'] ) || ! \is_string( $image['url'] ) ) {
[136] Fix | Delete
return;
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
if ( $this->url->is_relative( $image['url'] ) && $image['url'][0] === '/' ) {
[140] Fix | Delete
$image['url'] = $this->url->build_absolute_url( $image['url'] );
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
if ( \array_key_exists( $image['url'], $this->images ) ) {
[144] Fix | Delete
return;
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
$this->images[ $image['url'] ] = $image;
[148] Fix | Delete
}
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function