Phase 5: Content and SEO - Yoast SEO, Schema.org markup, Open Graph, favicon support, XML sitemap
This commit is contained in:
+97
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Integrations\Blocks;
|
||||
|
||||
use Yoast\WP\SEO\Integrations\Integration_Interface;
|
||||
|
||||
/**
|
||||
* Dynamic_Block class.
|
||||
*/
|
||||
abstract class Dynamic_Block_V3 implements Integration_Interface {
|
||||
|
||||
/**
|
||||
* The name of the block.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $block_name;
|
||||
|
||||
/**
|
||||
* The editor script for the block.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $script;
|
||||
|
||||
/**
|
||||
* The base path for the block.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $base_path;
|
||||
|
||||
/**
|
||||
* Returns the conditionals based on which this loadable should be active.
|
||||
*
|
||||
* @return array<string>
|
||||
*/
|
||||
public static function get_conditionals() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the integration.
|
||||
*
|
||||
* Integrations hooking on `init` need to have a priority of 11 or higher to
|
||||
* ensure that they run, as priority 10 is used by the loader to load the integrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_hooks() {
|
||||
\add_action( 'init', [ $this, 'register_block' ], 11 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the block.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_block() {
|
||||
\register_block_type(
|
||||
$this->base_path . $this->block_name . '/block.json',
|
||||
[
|
||||
'editor_script' => $this->script,
|
||||
'render_callback' => [ $this, 'present' ],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Presents the block output. This is abstract because in the loop we need to be able to build the data for the
|
||||
* presenter in the last moment.
|
||||
*
|
||||
* @param array<string, bool|string|int|array> $attributes The block attributes.
|
||||
*
|
||||
* @return string The block output.
|
||||
*/
|
||||
abstract public function present( $attributes );
|
||||
|
||||
/**
|
||||
* Checks whether the links in the block should have target="blank".
|
||||
*
|
||||
* This is needed because when the editor is loaded in an Iframe the link needs to open in a different browser window.
|
||||
* We don't want this behaviour in the front-end and the way to check this is to check if the block is rendered in a REST request with the `context` set as 'edit'. Thus being in the editor.
|
||||
*
|
||||
* @return bool returns if the block should be opened in another window.
|
||||
*/
|
||||
protected function should_link_target_blank(): bool {
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
|
||||
if ( isset( $_GET['context'] ) && \is_string( $_GET['context'] ) ) {
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are not processing form information, We are only strictly comparing.
|
||||
if ( \wp_unslash( $_GET['context'] ) === 'edit' ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Integrations\Blocks;
|
||||
|
||||
use Yoast\WP\SEO\Integrations\Integration_Interface;
|
||||
|
||||
/**
|
||||
* Dynamic_Block class.
|
||||
*/
|
||||
abstract class Dynamic_Block implements Integration_Interface {
|
||||
|
||||
/**
|
||||
* The name of the block.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $block_name;
|
||||
|
||||
/**
|
||||
* The editor script for the block.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $script;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public static function get_conditionals() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function register_hooks() {
|
||||
\add_action( 'init', [ $this, 'register_block' ], 11 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the block.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_block() {
|
||||
\register_block_type(
|
||||
'yoast-seo/' . $this->block_name,
|
||||
[
|
||||
'editor_script' => $this->script,
|
||||
'render_callback' => [ $this, 'present' ],
|
||||
'attributes' => [
|
||||
'className' => [
|
||||
'default' => '',
|
||||
'type' => 'string',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Presents the block output. This is abstract because in the loop we need to be able to build the data for the
|
||||
* presenter in the last moment.
|
||||
*
|
||||
* @param array $attributes The block attributes.
|
||||
*
|
||||
* @return string The block output.
|
||||
*/
|
||||
abstract public function present( $attributes );
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Integrations\Blocks;
|
||||
|
||||
use Yoast\WP\SEO\Integrations\Integration_Interface;
|
||||
|
||||
/**
|
||||
* Internal_Linking_Category block class.
|
||||
*/
|
||||
class Internal_Linking_Category implements Integration_Interface {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public static function get_conditionals() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function register_hooks() {
|
||||
\add_filter( 'block_categories_all', [ $this, 'add_block_categories' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds Yoast block categories.
|
||||
*
|
||||
* @param array $categories The categories.
|
||||
* @return array The filtered categories.
|
||||
*/
|
||||
public function add_block_categories( $categories ) {
|
||||
$categories[] = [
|
||||
'slug' => 'yoast-structured-data-blocks',
|
||||
'title' => \sprintf(
|
||||
/* translators: %1$s expands to Yoast. */
|
||||
\__( '%1$s Structured Data Blocks', 'wordpress-seo' ),
|
||||
'Yoast'
|
||||
),
|
||||
];
|
||||
$categories[] = [
|
||||
'slug' => 'yoast-internal-linking-blocks',
|
||||
'title' => \sprintf(
|
||||
/* translators: %1$s expands to Yoast. */
|
||||
\__( '%1$s Internal Linking Blocks', 'wordpress-seo' ),
|
||||
'Yoast'
|
||||
),
|
||||
];
|
||||
|
||||
return $categories;
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Integrations\Blocks;
|
||||
|
||||
use WPSEO_Admin_Asset_Manager;
|
||||
use Yoast\WP\SEO\Conditionals\Admin\Post_Conditional;
|
||||
use Yoast\WP\SEO\Integrations\Integration_Interface;
|
||||
|
||||
/**
|
||||
* Block_Editor_Integration class to enqueue the block editor assets also for the iframe.
|
||||
*/
|
||||
class Block_Editor_Integration implements Integration_Interface {
|
||||
|
||||
/**
|
||||
* Represents the admin asset manager.
|
||||
*
|
||||
* @var WPSEO_Admin_Asset_Manager
|
||||
*/
|
||||
protected $asset_manager;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return array<Post_Conditional>
|
||||
*/
|
||||
public static function get_conditionals() {
|
||||
return [ Post_Conditional::class ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param WPSEO_Admin_Asset_Manager $asset_manager The asset manager.
|
||||
*/
|
||||
public function __construct( WPSEO_Admin_Asset_Manager $asset_manager ) {
|
||||
$this->asset_manager = $asset_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the integration.
|
||||
*
|
||||
* This is the place to register hooks and filters.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_hooks() {
|
||||
\add_action( 'enqueue_block_assets', [ $this, 'enqueue' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues the assets for the block editor.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue() {
|
||||
$this->asset_manager->enqueue_style( 'block-editor' );
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Integrations\Blocks;
|
||||
|
||||
use WPSEO_Replace_Vars;
|
||||
use Yoast\WP\SEO\Memoizers\Meta_Tags_Context_Memoizer;
|
||||
use Yoast\WP\SEO\Presenters\Breadcrumbs_Presenter;
|
||||
use Yoast\WP\SEO\Repositories\Indexable_Repository;
|
||||
use Yoast\WP\SEO\Surfaces\Helpers_Surface;
|
||||
|
||||
/**
|
||||
* Siblings block class
|
||||
*/
|
||||
class Breadcrumbs_Block extends Dynamic_Block_V3 {
|
||||
|
||||
/**
|
||||
* The name of the block.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $block_name = 'breadcrumbs';
|
||||
|
||||
/**
|
||||
* The editor script for the block.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $script = 'yoast-seo-dynamic-blocks';
|
||||
|
||||
/**
|
||||
* The Meta_Tags_Context_Memoizer.
|
||||
*
|
||||
* @var Meta_Tags_Context_Memoizer
|
||||
*/
|
||||
private $context_memoizer;
|
||||
|
||||
/**
|
||||
* The Replace vars helper.
|
||||
*
|
||||
* @var WPSEO_Replace_Vars
|
||||
*/
|
||||
private $replace_vars;
|
||||
|
||||
/**
|
||||
* The helpers surface.
|
||||
*
|
||||
* @var Helpers_Surface
|
||||
*/
|
||||
private $helpers;
|
||||
|
||||
/**
|
||||
* The indexable repository.
|
||||
*
|
||||
* @var Indexable_Repository
|
||||
*/
|
||||
private $indexable_repository;
|
||||
|
||||
/**
|
||||
* Siblings_Block constructor.
|
||||
*
|
||||
* @param Meta_Tags_Context_Memoizer $context_memoizer The context.
|
||||
* @param WPSEO_Replace_Vars $replace_vars The replace variable helper.
|
||||
* @param Helpers_Surface $helpers The Helpers surface.
|
||||
* @param Indexable_Repository $indexable_repository The indexable repository.
|
||||
*/
|
||||
public function __construct(
|
||||
Meta_Tags_Context_Memoizer $context_memoizer,
|
||||
WPSEO_Replace_Vars $replace_vars,
|
||||
Helpers_Surface $helpers,
|
||||
Indexable_Repository $indexable_repository
|
||||
) {
|
||||
$this->context_memoizer = $context_memoizer;
|
||||
$this->replace_vars = $replace_vars;
|
||||
$this->helpers = $helpers;
|
||||
$this->indexable_repository = $indexable_repository;
|
||||
|
||||
$this->base_path = \WPSEO_PATH . 'blocks/dynamic-blocks/';
|
||||
}
|
||||
|
||||
/**
|
||||
* Presents the breadcrumbs output for the current page or the available post_id.
|
||||
*
|
||||
* @param array<string, bool|string|int|array> $attributes The block attributes.
|
||||
*
|
||||
* @return string The block output.
|
||||
*/
|
||||
public function present( $attributes ) {
|
||||
$presenter = new Breadcrumbs_Presenter();
|
||||
// $this->context_memoizer->for_current_page only works on the frontend. To render the right breadcrumb in the
|
||||
// editor, we need the repository.
|
||||
if ( \wp_is_serving_rest_request() || \is_admin() ) {
|
||||
$post_id = \get_the_ID();
|
||||
if ( $post_id ) {
|
||||
$indexable = $this->indexable_repository->find_by_id_and_type( $post_id, 'post' );
|
||||
|
||||
if ( ! $indexable ) {
|
||||
$post = \get_post( $post_id );
|
||||
$indexable = $this->indexable_repository->query()->create(
|
||||
[
|
||||
'object_id' => $post_id,
|
||||
'object_type' => 'post',
|
||||
'object_sub_type' => $post->post_type,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
$context = $this->context_memoizer->get( $indexable, 'Post_Type' );
|
||||
}
|
||||
}
|
||||
if ( ! isset( $context ) ) {
|
||||
$context = $this->context_memoizer->for_current_page();
|
||||
}
|
||||
|
||||
/** This filter is documented in src/integrations/front-end-integration.php */
|
||||
$presentation = \apply_filters( 'wpseo_frontend_presentation', $context->presentation, $context );
|
||||
$presenter->presentation = $presentation;
|
||||
$presenter->replace_vars = $this->replace_vars;
|
||||
$presenter->helpers = $this->helpers;
|
||||
$class_name = 'yoast-breadcrumbs';
|
||||
|
||||
if ( ! empty( $attributes['className'] ) ) {
|
||||
$class_name .= ' ' . \esc_attr( $attributes['className'] );
|
||||
}
|
||||
|
||||
return '<div class="' . $class_name . '">' . $presenter->present() . '</div>';
|
||||
}
|
||||
}
|
||||
+422
@@ -0,0 +1,422 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Integrations\Blocks;
|
||||
|
||||
use WPSEO_Admin_Asset_Manager;
|
||||
use Yoast\WP\SEO\Conditionals\No_Conditionals;
|
||||
use Yoast\WP\SEO\Helpers\Image_Helper;
|
||||
use Yoast\WP\SEO\Integrations\Integration_Interface;
|
||||
|
||||
/**
|
||||
* Class to load assets required for structured data blocks.
|
||||
*/
|
||||
class Structured_Data_Blocks implements Integration_Interface {
|
||||
|
||||
use No_Conditionals;
|
||||
|
||||
/**
|
||||
* An instance of the WPSEO_Admin_Asset_Manager class.
|
||||
*
|
||||
* @var WPSEO_Admin_Asset_Manager
|
||||
*/
|
||||
protected $asset_manager;
|
||||
|
||||
/**
|
||||
* An instance of the image helper class.
|
||||
*
|
||||
* @var Image_Helper
|
||||
*/
|
||||
protected $image_helper;
|
||||
|
||||
/**
|
||||
* The image caches per post.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $caches = [];
|
||||
|
||||
/**
|
||||
* The used cache keys per post.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $used_caches = [];
|
||||
|
||||
/**
|
||||
* Whether or not we've registered our shutdown function.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $registered_shutdown_function = false;
|
||||
|
||||
/**
|
||||
* Structured_Data_Blocks constructor.
|
||||
*
|
||||
* @param WPSEO_Admin_Asset_Manager $asset_manager The asset manager.
|
||||
* @param Image_Helper $image_helper The image helper.
|
||||
*/
|
||||
public function __construct( WPSEO_Admin_Asset_Manager $asset_manager, Image_Helper $image_helper ) {
|
||||
$this->asset_manager = $asset_manager;
|
||||
$this->image_helper = $image_helper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers hooks for Structured Data Blocks with WordPress.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_hooks() {
|
||||
$this->register_blocks();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the blocks.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_blocks() {
|
||||
/**
|
||||
* Filter: 'wpseo_enable_structured_data_blocks' - Allows disabling Yoast's schema blocks entirely.
|
||||
*
|
||||
* @param bool $enable If false, our structured data blocks won't show.
|
||||
*/
|
||||
if ( ! \apply_filters( 'wpseo_enable_structured_data_blocks', true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
\register_block_type(
|
||||
\WPSEO_PATH . 'blocks/structured-data-blocks/faq/block.json',
|
||||
[
|
||||
'render_callback' => [ $this, 'optimize_faq_images' ],
|
||||
]
|
||||
);
|
||||
\register_block_type(
|
||||
\WPSEO_PATH . 'blocks/structured-data-blocks/how-to/block.json',
|
||||
[
|
||||
'render_callback' => [ $this, 'optimize_how_to_images' ],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimizes images in the FAQ blocks.
|
||||
*
|
||||
* @param array $attributes The attributes.
|
||||
* @param string $content The content.
|
||||
*
|
||||
* @return string The content with images optimized.
|
||||
*/
|
||||
public function optimize_faq_images( $attributes, $content ) {
|
||||
if ( ! isset( $attributes['questions'] ) ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
return $this->optimize_images( $attributes['questions'], 'answer', $content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the durations into a translated string containing the count, and either singular or plural unit.
|
||||
* For example (in en-US): If 'days' is 1, it returns "1 day". If 'days' is 2, it returns "2 days".
|
||||
* If a number value is 0, we don't output the string.
|
||||
*
|
||||
* @param number $days Number of days.
|
||||
* @param number $hours Number of hours.
|
||||
* @param number $minutes Number of minutes.
|
||||
* @return array Array of pluralized durations.
|
||||
*/
|
||||
private function transform_duration_to_string( $days, $hours, $minutes ) {
|
||||
$strings = [];
|
||||
if ( $days ) {
|
||||
$strings[] = \sprintf(
|
||||
/* translators: %d expands to the number of day/days. */
|
||||
\_n( '%d day', '%d days', $days, 'wordpress-seo' ),
|
||||
$days
|
||||
);
|
||||
}
|
||||
if ( $hours ) {
|
||||
$strings[] = \sprintf(
|
||||
/* translators: %d expands to the number of hour/hours. */
|
||||
\_n( '%d hour', '%d hours', $hours, 'wordpress-seo' ),
|
||||
$hours
|
||||
);
|
||||
}
|
||||
if ( $minutes ) {
|
||||
$strings[] = \sprintf(
|
||||
/* translators: %d expands to the number of minute/minutes. */
|
||||
\_n( '%d minute', '%d minutes', $minutes, 'wordpress-seo' ),
|
||||
$minutes
|
||||
);
|
||||
}
|
||||
return $strings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the durations into a translated string.
|
||||
*
|
||||
* @param array $attributes The attributes.
|
||||
* @return string The formatted duration.
|
||||
*/
|
||||
private function build_duration_string( $attributes ) {
|
||||
$days = ( $attributes['days'] ?? 0 );
|
||||
$hours = ( $attributes['hours'] ?? 0 );
|
||||
$minutes = ( $attributes['minutes'] ?? 0 );
|
||||
$elements = $this->transform_duration_to_string( $days, $hours, $minutes );
|
||||
$elements_length = \count( $elements );
|
||||
|
||||
switch ( $elements_length ) {
|
||||
case 1:
|
||||
return $elements[0];
|
||||
case 2:
|
||||
return \sprintf(
|
||||
/* translators: %s expands to a unit of time (e.g. 1 day). */
|
||||
\__( '%1$s and %2$s', 'wordpress-seo' ),
|
||||
...$elements
|
||||
);
|
||||
case 3:
|
||||
return \sprintf(
|
||||
/* translators: %s expands to a unit of time (e.g. 1 day). */
|
||||
\__( '%1$s, %2$s and %3$s', 'wordpress-seo' ),
|
||||
...$elements
|
||||
);
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Presents the duration text of the How-To block in the site language.
|
||||
*
|
||||
* @param array $attributes The attributes.
|
||||
* @param string $content The content.
|
||||
*
|
||||
* @return string The content with the duration text in the site language.
|
||||
*/
|
||||
public function present_duration_text( $attributes, $content ) {
|
||||
$duration = $this->build_duration_string( $attributes );
|
||||
// 'Time needed:' is the default duration text that will be shown if a user doesn't add one.
|
||||
$duration_text = \__( 'Time needed:', 'wordpress-seo' );
|
||||
|
||||
if ( isset( $attributes['durationText'] ) && $attributes['durationText'] !== '' ) {
|
||||
$duration_text = $attributes['durationText'];
|
||||
}
|
||||
|
||||
return \preg_replace(
|
||||
'/(<p class="schema-how-to-total-time">)(<span class="schema-how-to-duration-time-text">.*<\/span>)(.[^\/p>]*)(<\/p>)/',
|
||||
'<p class="schema-how-to-total-time"><span class="schema-how-to-duration-time-text">' . $duration_text . ' </span>' . $duration . '</p>',
|
||||
$content,
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimizes images in the How-To blocks.
|
||||
*
|
||||
* @param array $attributes The attributes.
|
||||
* @param string $content The content.
|
||||
*
|
||||
* @return string The content with images optimized.
|
||||
*/
|
||||
public function optimize_how_to_images( $attributes, $content ) {
|
||||
if ( ! isset( $attributes['steps'] ) ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
$content = $this->present_duration_text( $attributes, $content );
|
||||
|
||||
return $this->optimize_images( $attributes['steps'], 'text', $content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimizes images in structured data blocks.
|
||||
*
|
||||
* @param array $elements The list of elements from the block attributes.
|
||||
* @param string $key The key in the data to iterate over.
|
||||
* @param string $content The content.
|
||||
*
|
||||
* @return string The content with images optimized.
|
||||
*/
|
||||
private function optimize_images( $elements, $key, $content ) {
|
||||
global $post;
|
||||
if ( ! $post ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
$this->add_images_from_attributes_to_used_cache( $post->ID, $elements, $key );
|
||||
|
||||
// Then replace all images with optimized versions in the content.
|
||||
$content = \preg_replace_callback(
|
||||
'/<img[^>]+>/',
|
||||
function ( $matches ) {
|
||||
\preg_match( '/src="([^"]+)"/', $matches[0], $src_matches );
|
||||
if ( ! $src_matches || ! isset( $src_matches[1] ) ) {
|
||||
return $matches[0];
|
||||
}
|
||||
$attachment_id = $this->attachment_src_to_id( $src_matches[1] );
|
||||
if ( $attachment_id === 0 ) {
|
||||
return $matches[0];
|
||||
}
|
||||
$image_size = 'full';
|
||||
$image_style = [ 'style' => 'max-width: 100%; height: auto;' ];
|
||||
\preg_match( '/style="[^"]*width:\s*(\d+)px[^"]*"/', $matches[0], $style_matches );
|
||||
if ( $style_matches && isset( $style_matches[1] ) ) {
|
||||
$width = (int) $style_matches[1];
|
||||
$meta_data = \wp_get_attachment_metadata( $attachment_id );
|
||||
if ( isset( $meta_data['height'] ) && isset( $meta_data['width'] ) && $meta_data['height'] > 0 && $meta_data['width'] > 0 ) {
|
||||
$aspect_ratio = ( $meta_data['height'] / $meta_data['width'] );
|
||||
$height = ( $width * $aspect_ratio );
|
||||
$image_size = [ $width, $height ];
|
||||
}
|
||||
$image_style = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter: 'wpseo_structured_data_blocks_image_size' - Allows adjusting the image size in structured data blocks.
|
||||
*
|
||||
* @since 18.2
|
||||
*
|
||||
* @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).
|
||||
* @param int $attachment_id The id of the attachment.
|
||||
* @param string $attachment_src The attachment src.
|
||||
*/
|
||||
$image_size = \apply_filters(
|
||||
'wpseo_structured_data_blocks_image_size',
|
||||
$image_size,
|
||||
$attachment_id,
|
||||
$src_matches[1]
|
||||
);
|
||||
$image_html = \wp_get_attachment_image(
|
||||
$attachment_id,
|
||||
$image_size,
|
||||
false,
|
||||
$image_style
|
||||
);
|
||||
|
||||
if ( empty( $image_html ) ) {
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
return $image_html;
|
||||
},
|
||||
$content
|
||||
);
|
||||
|
||||
if ( ! $this->registered_shutdown_function ) {
|
||||
\register_shutdown_function( [ $this, 'maybe_save_used_caches' ] );
|
||||
$this->registered_shutdown_function = true;
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the caches of structured data block images have been changed, saves them.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function maybe_save_used_caches() {
|
||||
foreach ( $this->used_caches as $post_id => $used_cache ) {
|
||||
if ( isset( $this->caches[ $post_id ] ) && $used_cache === $this->caches[ $post_id ] ) {
|
||||
continue;
|
||||
}
|
||||
\update_post_meta( $post_id, 'yoast-structured-data-blocks-images-cache', $used_cache );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an attachment src to an attachment ID.
|
||||
*
|
||||
* @param string $src The attachment src.
|
||||
*
|
||||
* @return int The attachment ID. 0 if none was found.
|
||||
*/
|
||||
private function attachment_src_to_id( $src ) {
|
||||
global $post;
|
||||
|
||||
if ( isset( $this->used_caches[ $post->ID ][ $src ] ) ) {
|
||||
return $this->used_caches[ $post->ID ][ $src ];
|
||||
}
|
||||
|
||||
$cache = $this->get_cache_for_post( $post->ID );
|
||||
if ( isset( $cache[ $src ] ) ) {
|
||||
$this->used_caches[ $post->ID ][ $src ] = $cache[ $src ];
|
||||
return $cache[ $src ];
|
||||
}
|
||||
|
||||
$this->used_caches[ $post->ID ][ $src ] = $this->image_helper->get_attachment_by_url( $src );
|
||||
return $this->used_caches[ $post->ID ][ $src ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the cache from postmeta for a given post.
|
||||
*
|
||||
* @param int $post_id The post ID.
|
||||
*
|
||||
* @return array The images cache.
|
||||
*/
|
||||
private function get_cache_for_post( $post_id ) {
|
||||
if ( isset( $this->caches[ $post_id ] ) ) {
|
||||
return $this->caches[ $post_id ];
|
||||
}
|
||||
|
||||
$cache = \get_post_meta( $post_id, 'yoast-structured-data-blocks-images-cache', true );
|
||||
if ( ! $cache ) {
|
||||
$cache = [];
|
||||
}
|
||||
|
||||
$this->caches[ $post_id ] = $cache;
|
||||
return $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds any images that have their ID in the block attributes to the cache.
|
||||
*
|
||||
* @param int $post_id The post ID.
|
||||
* @param array $elements The elements.
|
||||
* @param string $key The key in the elements we should loop over.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function add_images_from_attributes_to_used_cache( $post_id, $elements, $key ) {
|
||||
// First grab all image IDs from the attributes.
|
||||
$images = [];
|
||||
foreach ( $elements as $element ) {
|
||||
if ( ! isset( $element[ $key ] ) ) {
|
||||
continue;
|
||||
}
|
||||
if ( isset( $element[ $key ] ) && \is_array( $element[ $key ] ) ) {
|
||||
foreach ( $element[ $key ] as $part ) {
|
||||
if ( ! \is_array( $part ) || ! isset( $part['type'] ) || $part['type'] !== 'img' ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ! isset( $part['key'] ) || ! isset( $part['props']['src'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$images[ $part['props']['src'] ] = (int) $part['key'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $this->used_caches[ $post_id ] ) ) {
|
||||
$this->used_caches[ $post_id ] = \array_merge( $this->used_caches[ $post_id ], $images );
|
||||
}
|
||||
else {
|
||||
$this->used_caches[ $post_id ] = $images;
|
||||
}
|
||||
}
|
||||
|
||||
/* DEPRECATED METHODS */
|
||||
|
||||
/**
|
||||
* Enqueue Gutenberg block assets for backend editor.
|
||||
*
|
||||
* @deprecated 22.7
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue_block_editor_assets() {
|
||||
\_deprecated_function( __METHOD__, 'Yoast SEO 22.7' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user