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/integrat.../blocks
File: structured-data-blocks.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Integrations\Blocks;
[2] Fix | Delete
[3] Fix | Delete
use WPSEO_Admin_Asset_Manager;
[4] Fix | Delete
use Yoast\WP\SEO\Conditionals\No_Conditionals;
[5] Fix | Delete
use Yoast\WP\SEO\Helpers\Image_Helper;
[6] Fix | Delete
use Yoast\WP\SEO\Integrations\Integration_Interface;
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Class to load assets required for structured data blocks.
[10] Fix | Delete
*/
[11] Fix | Delete
class Structured_Data_Blocks implements Integration_Interface {
[12] Fix | Delete
[13] Fix | Delete
use No_Conditionals;
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* An instance of the WPSEO_Admin_Asset_Manager class.
[17] Fix | Delete
*
[18] Fix | Delete
* @var WPSEO_Admin_Asset_Manager
[19] Fix | Delete
*/
[20] Fix | Delete
protected $asset_manager;
[21] Fix | Delete
[22] Fix | Delete
/**
[23] Fix | Delete
* An instance of the image helper class.
[24] Fix | Delete
*
[25] Fix | Delete
* @var Image_Helper
[26] Fix | Delete
*/
[27] Fix | Delete
protected $image_helper;
[28] Fix | Delete
[29] Fix | Delete
/**
[30] Fix | Delete
* The image caches per post.
[31] Fix | Delete
*
[32] Fix | Delete
* @var array
[33] Fix | Delete
*/
[34] Fix | Delete
protected $caches = [];
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* The used cache keys per post.
[38] Fix | Delete
*
[39] Fix | Delete
* @var array
[40] Fix | Delete
*/
[41] Fix | Delete
protected $used_caches = [];
[42] Fix | Delete
[43] Fix | Delete
/**
[44] Fix | Delete
* Whether or not we've registered our shutdown function.
[45] Fix | Delete
*
[46] Fix | Delete
* @var bool
[47] Fix | Delete
*/
[48] Fix | Delete
protected $registered_shutdown_function = false;
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* Structured_Data_Blocks constructor.
[52] Fix | Delete
*
[53] Fix | Delete
* @param WPSEO_Admin_Asset_Manager $asset_manager The asset manager.
[54] Fix | Delete
* @param Image_Helper $image_helper The image helper.
[55] Fix | Delete
*/
[56] Fix | Delete
public function __construct(
[57] Fix | Delete
WPSEO_Admin_Asset_Manager $asset_manager,
[58] Fix | Delete
Image_Helper $image_helper
[59] Fix | Delete
) {
[60] Fix | Delete
$this->asset_manager = $asset_manager;
[61] Fix | Delete
$this->image_helper = $image_helper;
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
/**
[65] Fix | Delete
* Registers hooks for Structured Data Blocks with WordPress.
[66] Fix | Delete
*
[67] Fix | Delete
* @return void
[68] Fix | Delete
*/
[69] Fix | Delete
public function register_hooks() {
[70] Fix | Delete
$this->register_blocks();
[71] Fix | Delete
}
[72] Fix | Delete
[73] Fix | Delete
/**
[74] Fix | Delete
* Registers the blocks.
[75] Fix | Delete
*
[76] Fix | Delete
* @return void
[77] Fix | Delete
*/
[78] Fix | Delete
public function register_blocks() {
[79] Fix | Delete
/**
[80] Fix | Delete
* Filter: 'wpseo_enable_structured_data_blocks' - Allows disabling Yoast's schema blocks entirely.
[81] Fix | Delete
*
[82] Fix | Delete
* @param bool $enable If false, our structured data blocks won't show.
[83] Fix | Delete
*/
[84] Fix | Delete
if ( ! \apply_filters( 'wpseo_enable_structured_data_blocks', true ) ) {
[85] Fix | Delete
return;
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
\register_block_type(
[89] Fix | Delete
\WPSEO_PATH . 'blocks/structured-data-blocks/faq/block.json',
[90] Fix | Delete
[
[91] Fix | Delete
'render_callback' => [ $this, 'optimize_faq_images' ],
[92] Fix | Delete
]
[93] Fix | Delete
);
[94] Fix | Delete
\register_block_type(
[95] Fix | Delete
\WPSEO_PATH . 'blocks/structured-data-blocks/how-to/block.json',
[96] Fix | Delete
[
[97] Fix | Delete
'render_callback' => [ $this, 'optimize_how_to_images' ],
[98] Fix | Delete
]
[99] Fix | Delete
);
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
/**
[103] Fix | Delete
* Optimizes images in the FAQ blocks.
[104] Fix | Delete
*
[105] Fix | Delete
* @param array $attributes The attributes.
[106] Fix | Delete
* @param string $content The content.
[107] Fix | Delete
*
[108] Fix | Delete
* @return string The content with images optimized.
[109] Fix | Delete
*/
[110] Fix | Delete
public function optimize_faq_images( $attributes, $content ) {
[111] Fix | Delete
if ( ! isset( $attributes['questions'] ) ) {
[112] Fix | Delete
return $content;
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
return $this->optimize_images( $attributes['questions'], 'answer', $content );
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
/**
[119] Fix | Delete
* Transforms the durations into a translated string containing the count, and either singular or plural unit.
[120] Fix | Delete
* For example (in en-US): If 'days' is 1, it returns "1 day". If 'days' is 2, it returns "2 days".
[121] Fix | Delete
* If a number value is 0, we don't output the string.
[122] Fix | Delete
*
[123] Fix | Delete
* @param number $days Number of days.
[124] Fix | Delete
* @param number $hours Number of hours.
[125] Fix | Delete
* @param number $minutes Number of minutes.
[126] Fix | Delete
* @return array Array of pluralized durations.
[127] Fix | Delete
*/
[128] Fix | Delete
private function transform_duration_to_string( $days, $hours, $minutes ) {
[129] Fix | Delete
$strings = [];
[130] Fix | Delete
if ( $days ) {
[131] Fix | Delete
$strings[] = \sprintf(
[132] Fix | Delete
/* translators: %d expands to the number of day/days. */
[133] Fix | Delete
\_n( '%d day', '%d days', $days, 'wordpress-seo' ),
[134] Fix | Delete
$days
[135] Fix | Delete
);
[136] Fix | Delete
}
[137] Fix | Delete
if ( $hours ) {
[138] Fix | Delete
$strings[] = \sprintf(
[139] Fix | Delete
/* translators: %d expands to the number of hour/hours. */
[140] Fix | Delete
\_n( '%d hour', '%d hours', $hours, 'wordpress-seo' ),
[141] Fix | Delete
$hours
[142] Fix | Delete
);
[143] Fix | Delete
}
[144] Fix | Delete
if ( $minutes ) {
[145] Fix | Delete
$strings[] = \sprintf(
[146] Fix | Delete
/* translators: %d expands to the number of minute/minutes. */
[147] Fix | Delete
\_n( '%d minute', '%d minutes', $minutes, 'wordpress-seo' ),
[148] Fix | Delete
$minutes
[149] Fix | Delete
);
[150] Fix | Delete
}
[151] Fix | Delete
return $strings;
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
/**
[155] Fix | Delete
* Formats the durations into a translated string.
[156] Fix | Delete
*
[157] Fix | Delete
* @param array $attributes The attributes.
[158] Fix | Delete
* @return string The formatted duration.
[159] Fix | Delete
*/
[160] Fix | Delete
private function build_duration_string( $attributes ) {
[161] Fix | Delete
$days = ( $attributes['days'] ?? 0 );
[162] Fix | Delete
$hours = ( $attributes['hours'] ?? 0 );
[163] Fix | Delete
$minutes = ( $attributes['minutes'] ?? 0 );
[164] Fix | Delete
$elements = $this->transform_duration_to_string( $days, $hours, $minutes );
[165] Fix | Delete
$elements_length = \count( $elements );
[166] Fix | Delete
[167] Fix | Delete
switch ( $elements_length ) {
[168] Fix | Delete
case 1:
[169] Fix | Delete
return $elements[0];
[170] Fix | Delete
case 2:
[171] Fix | Delete
return \sprintf(
[172] Fix | Delete
/* translators: %s expands to a unit of time (e.g. 1 day). */
[173] Fix | Delete
\__( '%1$s and %2$s', 'wordpress-seo' ),
[174] Fix | Delete
...$elements
[175] Fix | Delete
);
[176] Fix | Delete
case 3:
[177] Fix | Delete
return \sprintf(
[178] Fix | Delete
/* translators: %s expands to a unit of time (e.g. 1 day). */
[179] Fix | Delete
\__( '%1$s, %2$s and %3$s', 'wordpress-seo' ),
[180] Fix | Delete
...$elements
[181] Fix | Delete
);
[182] Fix | Delete
default:
[183] Fix | Delete
return '';
[184] Fix | Delete
}
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
/**
[188] Fix | Delete
* Presents the duration text of the How-To block in the site language.
[189] Fix | Delete
*
[190] Fix | Delete
* @param array $attributes The attributes.
[191] Fix | Delete
* @param string $content The content.
[192] Fix | Delete
*
[193] Fix | Delete
* @return string The content with the duration text in the site language.
[194] Fix | Delete
*/
[195] Fix | Delete
public function present_duration_text( $attributes, $content ) {
[196] Fix | Delete
$duration = $this->build_duration_string( $attributes );
[197] Fix | Delete
// 'Time needed:' is the default duration text that will be shown if a user doesn't add one.
[198] Fix | Delete
$duration_text = \__( 'Time needed:', 'wordpress-seo' );
[199] Fix | Delete
[200] Fix | Delete
if ( isset( $attributes['durationText'] ) && $attributes['durationText'] !== '' ) {
[201] Fix | Delete
$duration_text = $attributes['durationText'];
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
return \preg_replace(
[205] Fix | Delete
'/(<p class="schema-how-to-total-time">)(<span class="schema-how-to-duration-time-text">.*<\/span>)(.[^\/p>]*)(<\/p>)/',
[206] Fix | Delete
'<p class="schema-how-to-total-time"><span class="schema-how-to-duration-time-text">' . $duration_text . '&nbsp;</span>' . $duration . '</p>',
[207] Fix | Delete
$content,
[208] Fix | Delete
1
[209] Fix | Delete
);
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
/**
[213] Fix | Delete
* Optimizes images in the How-To blocks.
[214] Fix | Delete
*
[215] Fix | Delete
* @param array $attributes The attributes.
[216] Fix | Delete
* @param string $content The content.
[217] Fix | Delete
*
[218] Fix | Delete
* @return string The content with images optimized.
[219] Fix | Delete
*/
[220] Fix | Delete
public function optimize_how_to_images( $attributes, $content ) {
[221] Fix | Delete
if ( ! isset( $attributes['steps'] ) ) {
[222] Fix | Delete
return $content;
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
$content = $this->present_duration_text( $attributes, $content );
[226] Fix | Delete
[227] Fix | Delete
return $this->optimize_images( $attributes['steps'], 'text', $content );
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
/**
[231] Fix | Delete
* Optimizes images in structured data blocks.
[232] Fix | Delete
*
[233] Fix | Delete
* @param array $elements The list of elements from the block attributes.
[234] Fix | Delete
* @param string $key The key in the data to iterate over.
[235] Fix | Delete
* @param string $content The content.
[236] Fix | Delete
*
[237] Fix | Delete
* @return string The content with images optimized.
[238] Fix | Delete
*/
[239] Fix | Delete
private function optimize_images( $elements, $key, $content ) {
[240] Fix | Delete
global $post;
[241] Fix | Delete
if ( ! $post ) {
[242] Fix | Delete
return $content;
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
$this->add_images_from_attributes_to_used_cache( $post->ID, $elements, $key );
[246] Fix | Delete
[247] Fix | Delete
// Then replace all images with optimized versions in the content.
[248] Fix | Delete
$content = \preg_replace_callback(
[249] Fix | Delete
'/<img[^>]+>/',
[250] Fix | Delete
function ( $matches ) {
[251] Fix | Delete
\preg_match( '/src="([^"]+)"/', $matches[0], $src_matches );
[252] Fix | Delete
if ( ! $src_matches || ! isset( $src_matches[1] ) ) {
[253] Fix | Delete
return $matches[0];
[254] Fix | Delete
}
[255] Fix | Delete
$attachment_id = $this->attachment_src_to_id( $src_matches[1] );
[256] Fix | Delete
if ( $attachment_id === 0 ) {
[257] Fix | Delete
return $matches[0];
[258] Fix | Delete
}
[259] Fix | Delete
$image_size = 'full';
[260] Fix | Delete
$image_style = [ 'style' => 'max-width: 100%; height: auto;' ];
[261] Fix | Delete
\preg_match( '/style="[^"]*width:\s*(\d+)px[^"]*"/', $matches[0], $style_matches );
[262] Fix | Delete
if ( $style_matches && isset( $style_matches[1] ) ) {
[263] Fix | Delete
$width = (int) $style_matches[1];
[264] Fix | Delete
$meta_data = \wp_get_attachment_metadata( $attachment_id );
[265] Fix | Delete
if ( isset( $meta_data['height'] ) && isset( $meta_data['width'] ) && $meta_data['height'] > 0 && $meta_data['width'] > 0 ) {
[266] Fix | Delete
$aspect_ratio = ( $meta_data['height'] / $meta_data['width'] );
[267] Fix | Delete
$height = ( $width * $aspect_ratio );
[268] Fix | Delete
$image_size = [ $width, $height ];
[269] Fix | Delete
}
[270] Fix | Delete
$image_style = '';
[271] Fix | Delete
}
[272] Fix | Delete
[273] Fix | Delete
/**
[274] Fix | Delete
* Filter: 'wpseo_structured_data_blocks_image_size' - Allows adjusting the image size in structured data blocks.
[275] Fix | Delete
*
[276] Fix | Delete
* @since 18.2
[277] Fix | Delete
*
[278] Fix | Delete
* @param string|int[] $image_size The image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order).
[279] Fix | Delete
* @param int $attachment_id The id of the attachment.
[280] Fix | Delete
* @param string $attachment_src The attachment src.
[281] Fix | Delete
*/
[282] Fix | Delete
$image_size = \apply_filters(
[283] Fix | Delete
'wpseo_structured_data_blocks_image_size',
[284] Fix | Delete
$image_size,
[285] Fix | Delete
$attachment_id,
[286] Fix | Delete
$src_matches[1]
[287] Fix | Delete
);
[288] Fix | Delete
$image_html = \wp_get_attachment_image(
[289] Fix | Delete
$attachment_id,
[290] Fix | Delete
$image_size,
[291] Fix | Delete
false,
[292] Fix | Delete
$image_style
[293] Fix | Delete
);
[294] Fix | Delete
[295] Fix | Delete
if ( empty( $image_html ) ) {
[296] Fix | Delete
return $matches[0];
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
return $image_html;
[300] Fix | Delete
},
[301] Fix | Delete
$content
[302] Fix | Delete
);
[303] Fix | Delete
[304] Fix | Delete
if ( ! $this->registered_shutdown_function ) {
[305] Fix | Delete
\register_shutdown_function( [ $this, 'maybe_save_used_caches' ] );
[306] Fix | Delete
$this->registered_shutdown_function = true;
[307] Fix | Delete
}
[308] Fix | Delete
[309] Fix | Delete
return $content;
[310] Fix | Delete
}
[311] Fix | Delete
[312] Fix | Delete
/**
[313] Fix | Delete
* If the caches of structured data block images have been changed, saves them.
[314] Fix | Delete
*
[315] Fix | Delete
* @return void
[316] Fix | Delete
*/
[317] Fix | Delete
public function maybe_save_used_caches() {
[318] Fix | Delete
foreach ( $this->used_caches as $post_id => $used_cache ) {
[319] Fix | Delete
if ( isset( $this->caches[ $post_id ] ) && $used_cache === $this->caches[ $post_id ] ) {
[320] Fix | Delete
continue;
[321] Fix | Delete
}
[322] Fix | Delete
\update_post_meta( $post_id, 'yoast-structured-data-blocks-images-cache', $used_cache );
[323] Fix | Delete
}
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
/**
[327] Fix | Delete
* Converts an attachment src to an attachment ID.
[328] Fix | Delete
*
[329] Fix | Delete
* @param string $src The attachment src.
[330] Fix | Delete
*
[331] Fix | Delete
* @return int The attachment ID. 0 if none was found.
[332] Fix | Delete
*/
[333] Fix | Delete
private function attachment_src_to_id( $src ) {
[334] Fix | Delete
global $post;
[335] Fix | Delete
[336] Fix | Delete
if ( isset( $this->used_caches[ $post->ID ][ $src ] ) ) {
[337] Fix | Delete
return $this->used_caches[ $post->ID ][ $src ];
[338] Fix | Delete
}
[339] Fix | Delete
[340] Fix | Delete
$cache = $this->get_cache_for_post( $post->ID );
[341] Fix | Delete
if ( isset( $cache[ $src ] ) ) {
[342] Fix | Delete
$this->used_caches[ $post->ID ][ $src ] = $cache[ $src ];
[343] Fix | Delete
return $cache[ $src ];
[344] Fix | Delete
}
[345] Fix | Delete
[346] Fix | Delete
$this->used_caches[ $post->ID ][ $src ] = $this->image_helper->get_attachment_by_url( $src );
[347] Fix | Delete
return $this->used_caches[ $post->ID ][ $src ];
[348] Fix | Delete
}
[349] Fix | Delete
[350] Fix | Delete
/**
[351] Fix | Delete
* Returns the cache from postmeta for a given post.
[352] Fix | Delete
*
[353] Fix | Delete
* @param int $post_id The post ID.
[354] Fix | Delete
*
[355] Fix | Delete
* @return array The images cache.
[356] Fix | Delete
*/
[357] Fix | Delete
private function get_cache_for_post( $post_id ) {
[358] Fix | Delete
if ( isset( $this->caches[ $post_id ] ) ) {
[359] Fix | Delete
return $this->caches[ $post_id ];
[360] Fix | Delete
}
[361] Fix | Delete
[362] Fix | Delete
$cache = \get_post_meta( $post_id, 'yoast-structured-data-blocks-images-cache', true );
[363] Fix | Delete
if ( ! $cache ) {
[364] Fix | Delete
$cache = [];
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
$this->caches[ $post_id ] = $cache;
[368] Fix | Delete
return $cache;
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
/**
[372] Fix | Delete
* Adds any images that have their ID in the block attributes to the cache.
[373] Fix | Delete
*
[374] Fix | Delete
* @param int $post_id The post ID.
[375] Fix | Delete
* @param array $elements The elements.
[376] Fix | Delete
* @param string $key The key in the elements we should loop over.
[377] Fix | Delete
*
[378] Fix | Delete
* @return void
[379] Fix | Delete
*/
[380] Fix | Delete
private function add_images_from_attributes_to_used_cache( $post_id, $elements, $key ) {
[381] Fix | Delete
// First grab all image IDs from the attributes.
[382] Fix | Delete
$images = [];
[383] Fix | Delete
foreach ( $elements as $element ) {
[384] Fix | Delete
if ( ! isset( $element[ $key ] ) ) {
[385] Fix | Delete
continue;
[386] Fix | Delete
}
[387] Fix | Delete
if ( isset( $element[ $key ] ) && \is_array( $element[ $key ] ) ) {
[388] Fix | Delete
foreach ( $element[ $key ] as $part ) {
[389] Fix | Delete
if ( ! \is_array( $part ) || ! isset( $part['type'] ) || $part['type'] !== 'img' ) {
[390] Fix | Delete
continue;
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
if ( ! isset( $part['key'] ) || ! isset( $part['props']['src'] ) ) {
[394] Fix | Delete
continue;
[395] Fix | Delete
}
[396] Fix | Delete
[397] Fix | Delete
$images[ $part['props']['src'] ] = (int) $part['key'];
[398] Fix | Delete
}
[399] Fix | Delete
}
[400] Fix | Delete
}
[401] Fix | Delete
[402] Fix | Delete
if ( isset( $this->used_caches[ $post_id ] ) ) {
[403] Fix | Delete
$this->used_caches[ $post_id ] = \array_merge( $this->used_caches[ $post_id ], $images );
[404] Fix | Delete
}
[405] Fix | Delete
else {
[406] Fix | Delete
$this->used_caches[ $post_id ] = $images;
[407] Fix | Delete
}
[408] Fix | Delete
}
[409] Fix | Delete
[410] Fix | Delete
/* DEPRECATED METHODS */
[411] Fix | Delete
[412] Fix | Delete
/**
[413] Fix | Delete
* Enqueue Gutenberg block assets for backend editor.
[414] Fix | Delete
*
[415] Fix | Delete
* @deprecated 22.7
[416] Fix | Delete
* @codeCoverageIgnore
[417] Fix | Delete
*
[418] Fix | Delete
* @return void
[419] Fix | Delete
*/
[420] Fix | Delete
public function enqueue_block_editor_assets() {
[421] Fix | Delete
\_deprecated_function( __METHOD__, 'Yoast SEO 22.7' );
[422] Fix | Delete
}
[423] Fix | Delete
}
[424] Fix | Delete
[425] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function