Edit File by line
/home/barbar84/www/wp-conte.../plugins/wordpres.../src/generato...
File: schema-generator.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Generators;
[2] Fix | Delete
[3] Fix | Delete
use WP_Block_Parser_Block;
[4] Fix | Delete
use Yoast\WP\SEO\Context\Meta_Tags_Context;
[5] Fix | Delete
use Yoast\WP\SEO\Generators\Schema\Abstract_Schema_Piece;
[6] Fix | Delete
use Yoast\WP\SEO\Helpers\Schema\Replace_Vars_Helper;
[7] Fix | Delete
use Yoast\WP\SEO\Surfaces\Helpers_Surface;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Class Schema_Generator.
[11] Fix | Delete
*/
[12] Fix | Delete
class Schema_Generator implements Generator_Interface {
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* The helpers surface.
[16] Fix | Delete
*
[17] Fix | Delete
* @var Helpers_Surface
[18] Fix | Delete
*/
[19] Fix | Delete
protected $helpers;
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* The Schema replace vars helper.
[23] Fix | Delete
*
[24] Fix | Delete
* @var Replace_Vars_Helper
[25] Fix | Delete
*/
[26] Fix | Delete
protected $schema_replace_vars_helper;
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Generator constructor.
[30] Fix | Delete
*
[31] Fix | Delete
* @param Helpers_Surface $helpers The helpers surface.
[32] Fix | Delete
* @param Replace_Vars_Helper $schema_replace_vars_helper The replace vars helper.
[33] Fix | Delete
*/
[34] Fix | Delete
public function __construct(
[35] Fix | Delete
Helpers_Surface $helpers,
[36] Fix | Delete
Replace_Vars_Helper $schema_replace_vars_helper
[37] Fix | Delete
) {
[38] Fix | Delete
$this->helpers = $helpers;
[39] Fix | Delete
$this->schema_replace_vars_helper = $schema_replace_vars_helper;
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Returns a Schema graph array.
[44] Fix | Delete
*
[45] Fix | Delete
* @param Meta_Tags_Context $context The meta tags context.
[46] Fix | Delete
*
[47] Fix | Delete
* @return array The graph.
[48] Fix | Delete
*/
[49] Fix | Delete
public function generate( Meta_Tags_Context $context ) {
[50] Fix | Delete
$pieces = $this->get_graph_pieces( $context );
[51] Fix | Delete
[52] Fix | Delete
$this->schema_replace_vars_helper->register_replace_vars( $context );
[53] Fix | Delete
[54] Fix | Delete
foreach ( \array_keys( $context->blocks ) as $block_type ) {
[55] Fix | Delete
/**
[56] Fix | Delete
* Filter: 'wpseo_pre_schema_block_type_<block-type>' - Allows hooking things to change graph output based on the blocks on the page.
[57] Fix | Delete
*
[58] Fix | Delete
* @param string $block_type The block type.
[59] Fix | Delete
* @param WP_Block_Parser_Block[] $blocks All the blocks of this block type.
[60] Fix | Delete
* @param Meta_Tags_Context $context A value object with context variables.
[61] Fix | Delete
*/
[62] Fix | Delete
\do_action( 'wpseo_pre_schema_block_type_' . $block_type, $context->blocks[ $block_type ], $context );
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
// Do a loop before everything else to inject the context and helpers.
[66] Fix | Delete
foreach ( $pieces as $piece ) {
[67] Fix | Delete
if ( \is_a( $piece, Abstract_Schema_Piece::class ) ) {
[68] Fix | Delete
$piece->context = $context;
[69] Fix | Delete
$piece->helpers = $this->helpers;
[70] Fix | Delete
}
[71] Fix | Delete
}
[72] Fix | Delete
[73] Fix | Delete
$pieces_to_generate = $this->filter_graph_pieces_to_generate( $pieces );
[74] Fix | Delete
$graph = $this->generate_graph( $pieces_to_generate, $context );
[75] Fix | Delete
$graph = $this->add_schema_blocks_graph_pieces( $graph, $context );
[76] Fix | Delete
[77] Fix | Delete
return [
[78] Fix | Delete
'@context' => 'https://schema.org',
[79] Fix | Delete
'@graph' => $graph,
[80] Fix | Delete
];
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
/**
[84] Fix | Delete
* Filters out any graph pieces that should not be generated.
[85] Fix | Delete
* (Using the `wpseo_schema_needs_<graph_piece_identifier>` series of filters).
[86] Fix | Delete
*
[87] Fix | Delete
* @param array $graph_pieces The current list of graph pieces that we want to generate.
[88] Fix | Delete
*
[89] Fix | Delete
* @return array The graph pieces to generate.
[90] Fix | Delete
*/
[91] Fix | Delete
protected function filter_graph_pieces_to_generate( $graph_pieces ) {
[92] Fix | Delete
$pieces_to_generate = [];
[93] Fix | Delete
foreach ( $graph_pieces as $piece ) {
[94] Fix | Delete
$identifier = \strtolower( \str_replace( 'Yoast\WP\SEO\Generators\Schema\\', '', \get_class( $piece ) ) );
[95] Fix | Delete
if ( \property_exists( $piece, 'identifier' ) ) {
[96] Fix | Delete
$identifier = $piece->identifier;
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
/**
[100] Fix | Delete
* Filter: 'wpseo_schema_needs_<identifier>' - Allows changing which graph pieces we output.
[101] Fix | Delete
*
[102] Fix | Delete
* @api bool $is_needed Whether or not to show a graph piece.
[103] Fix | Delete
*/
[104] Fix | Delete
$is_needed = \apply_filters( 'wpseo_schema_needs_' . $identifier, $piece->is_needed() );
[105] Fix | Delete
if ( ! $is_needed ) {
[106] Fix | Delete
continue;
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
$pieces_to_generate[ $identifier ] = $piece;
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
return $pieces_to_generate;
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
/**
[116] Fix | Delete
* Generates the schema graph.
[117] Fix | Delete
*
[118] Fix | Delete
* @param array $graph_piece_generators The schema graph pieces to generate.
[119] Fix | Delete
* @param Meta_Tags_Context $context The meta tags context to use.
[120] Fix | Delete
*
[121] Fix | Delete
* @return array The generated schema graph.
[122] Fix | Delete
*/
[123] Fix | Delete
protected function generate_graph( $graph_piece_generators, $context ) {
[124] Fix | Delete
$graph = [];
[125] Fix | Delete
foreach ( $graph_piece_generators as $identifier => $graph_piece_generator ) {
[126] Fix | Delete
$graph_pieces = $graph_piece_generator->generate();
[127] Fix | Delete
// If only a single graph piece was returned.
[128] Fix | Delete
if ( $graph_pieces !== false && \array_key_exists( '@type', $graph_pieces ) ) {
[129] Fix | Delete
$graph_pieces = [ $graph_pieces ];
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
if ( ! \is_array( $graph_pieces ) ) {
[133] Fix | Delete
continue;
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
foreach ( $graph_pieces as $graph_piece ) {
[137] Fix | Delete
/**
[138] Fix | Delete
* Filter: 'wpseo_schema_<identifier>' - Allows changing graph piece output.
[139] Fix | Delete
* This filter can be called with either an identifier or a block type (see `add_schema_blocks_graph_pieces()`).
[140] Fix | Delete
*
[141] Fix | Delete
* @api array $graph_piece The graph piece to filter.
[142] Fix | Delete
*
[143] Fix | Delete
* @param Meta_Tags_Context $context A value object with context variables.
[144] Fix | Delete
* @param Abstract_Schema_Piece $graph_piece_generator A value object with context variables.
[145] Fix | Delete
* @param Abstract_Schema_Piece[] $graph_piece_generators A value object with context variables.
[146] Fix | Delete
*/
[147] Fix | Delete
$graph_piece = \apply_filters( 'wpseo_schema_' . $identifier, $graph_piece, $context, $graph_piece_generator, $graph_piece_generators );
[148] Fix | Delete
$graph_piece = $this->type_filter( $graph_piece, $identifier, $context, $graph_piece_generator, $graph_piece_generators );
[149] Fix | Delete
$graph_piece = $this->validate_type( $graph_piece );
[150] Fix | Delete
[151] Fix | Delete
if ( \is_array( $graph_piece ) ) {
[152] Fix | Delete
$graph[] = $graph_piece;
[153] Fix | Delete
}
[154] Fix | Delete
}
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
return $graph;
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
/**
[161] Fix | Delete
* Adds schema graph pieces from Gutenberg blocks on the current page to
[162] Fix | Delete
* the given schema graph.
[163] Fix | Delete
*
[164] Fix | Delete
* Think of blocks like the Yoast FAQ block or the How To block.
[165] Fix | Delete
*
[166] Fix | Delete
* @param array $graph The current schema graph.
[167] Fix | Delete
* @param Meta_Tags_Context $context The meta tags context.
[168] Fix | Delete
*
[169] Fix | Delete
* @return array The graph with the schema blocks graph pieces added.
[170] Fix | Delete
*/
[171] Fix | Delete
protected function add_schema_blocks_graph_pieces( $graph, $context ) {
[172] Fix | Delete
foreach ( $context->blocks as $block_type => $blocks ) {
[173] Fix | Delete
foreach ( $blocks as $block ) {
[174] Fix | Delete
$block_type = \strtolower( $block['blockName'] );
[175] Fix | Delete
/**
[176] Fix | Delete
* Filter: 'wpseo_schema_block_<block-type>'.
[177] Fix | Delete
* This filter is documented in the `generate_graph()` function in this class.
[178] Fix | Delete
*/
[179] Fix | Delete
$graph = \apply_filters( 'wpseo_schema_block_' . $block_type, $graph, $block, $context );
[180] Fix | Delete
[181] Fix | Delete
if ( isset( $block['attrs']['yoast-schema'] ) ) {
[182] Fix | Delete
$graph[] = $this->schema_replace_vars_helper->replace( $block['attrs']['yoast-schema'], $context->presentation );
[183] Fix | Delete
}
[184] Fix | Delete
}
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
return $graph;
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
/**
[191] Fix | Delete
* Adapts the WebPage graph piece for password-protected posts.
[192] Fix | Delete
*
[193] Fix | Delete
* It should only have certain whitelisted properties.
[194] Fix | Delete
* The type should always be WebPage.
[195] Fix | Delete
*
[196] Fix | Delete
* @param array $graph_piece The WebPage graph piece that should be adapted for password-protected posts.
[197] Fix | Delete
*
[198] Fix | Delete
* @return array The WebPage graph piece that has been adapted for password-protected posts.
[199] Fix | Delete
*/
[200] Fix | Delete
public function protected_webpage_schema( $graph_piece ) {
[201] Fix | Delete
$properties_to_show = \array_flip(
[202] Fix | Delete
[
[203] Fix | Delete
'@type',
[204] Fix | Delete
'@id',
[205] Fix | Delete
'url',
[206] Fix | Delete
'name',
[207] Fix | Delete
'isPartOf',
[208] Fix | Delete
'inLanguage',
[209] Fix | Delete
'datePublished',
[210] Fix | Delete
'dateModified',
[211] Fix | Delete
'breadcrumb',
[212] Fix | Delete
]
[213] Fix | Delete
);
[214] Fix | Delete
[215] Fix | Delete
$graph_piece = \array_intersect_key( $graph_piece, $properties_to_show );
[216] Fix | Delete
$graph_piece['@type'] = 'WebPage';
[217] Fix | Delete
[218] Fix | Delete
return $graph_piece;
[219] Fix | Delete
}
[220] Fix | Delete
[221] Fix | Delete
/**
[222] Fix | Delete
* Gets all the graph pieces we need.
[223] Fix | Delete
*
[224] Fix | Delete
* @param Meta_Tags_Context $context The meta tags context.
[225] Fix | Delete
*
[226] Fix | Delete
* @return Abstract_Schema_Piece[] A filtered array of graph pieces.
[227] Fix | Delete
*/
[228] Fix | Delete
protected function get_graph_pieces( $context ) {
[229] Fix | Delete
if ( $context->indexable->object_type === 'post' && \post_password_required( $context->post ) ) {
[230] Fix | Delete
$schema_pieces = [
[231] Fix | Delete
new Schema\Organization(),
[232] Fix | Delete
new Schema\Website(),
[233] Fix | Delete
new Schema\WebPage(),
[234] Fix | Delete
];
[235] Fix | Delete
[236] Fix | Delete
\add_filter( 'wpseo_schema_webpage', [ $this, 'protected_webpage_schema' ], 1 );
[237] Fix | Delete
}
[238] Fix | Delete
else {
[239] Fix | Delete
$schema_pieces = [
[240] Fix | Delete
new Schema\Organization(),
[241] Fix | Delete
new Schema\Person(),
[242] Fix | Delete
new Schema\Website(),
[243] Fix | Delete
new Schema\Main_Image(),
[244] Fix | Delete
new Schema\WebPage(),
[245] Fix | Delete
new Schema\Breadcrumb(),
[246] Fix | Delete
new Schema\Article(),
[247] Fix | Delete
new Schema\Author(),
[248] Fix | Delete
new Schema\FAQ(),
[249] Fix | Delete
new Schema\HowTo(),
[250] Fix | Delete
];
[251] Fix | Delete
}
[252] Fix | Delete
[253] Fix | Delete
/**
[254] Fix | Delete
* Filter: 'wpseo_schema_graph_pieces' - Allows adding pieces to the graph.
[255] Fix | Delete
*
[256] Fix | Delete
* @param Meta_Tags_Context $context An object with context variables.
[257] Fix | Delete
*
[258] Fix | Delete
* @api array $pieces The schema pieces.
[259] Fix | Delete
*/
[260] Fix | Delete
return \apply_filters( 'wpseo_schema_graph_pieces', $schema_pieces, $context );
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
/**
[264] Fix | Delete
* Allows filtering the graph piece by its schema type.
[265] Fix | Delete
*
[266] Fix | Delete
* Note: We removed the Abstract_Schema_Piece type-hint from the $graph_piece_generator argument, because
[267] Fix | Delete
* it caused conflicts with old code, Yoast SEO Video specifically.
[268] Fix | Delete
*
[269] Fix | Delete
* @param array $graph_piece The graph piece we're filtering.
[270] Fix | Delete
* @param string $identifier The identifier of the graph piece that is being filtered.
[271] Fix | Delete
* @param Meta_Tags_Context $context The meta tags context.
[272] Fix | Delete
* @param Abstract_Schema_Piece $graph_piece_generator A value object with context variables.
[273] Fix | Delete
* @param Abstract_Schema_Piece[] $graph_piece_generators A value object with context variables.
[274] Fix | Delete
*
[275] Fix | Delete
* @return array The filtered graph piece.
[276] Fix | Delete
*/
[277] Fix | Delete
private function type_filter( $graph_piece, $identifier, Meta_Tags_Context $context, $graph_piece_generator, array $graph_piece_generators ) {
[278] Fix | Delete
$types = $this->get_type_from_piece( $graph_piece );
[279] Fix | Delete
foreach ( $types as $type ) {
[280] Fix | Delete
$type = \strtolower( $type );
[281] Fix | Delete
[282] Fix | Delete
// Prevent running the same filter twice. This makes sure we run f/i. for 'author' and for 'person'.
[283] Fix | Delete
if ( $type && $type !== $identifier ) {
[284] Fix | Delete
/**
[285] Fix | Delete
* Filter: 'wpseo_schema_<type>' - Allows changing graph piece output by @type.
[286] Fix | Delete
*
[287] Fix | Delete
* @api array $graph_piece The graph piece to filter.
[288] Fix | Delete
*
[289] Fix | Delete
* @param Meta_Tags_Context $context A value object with context variables.
[290] Fix | Delete
* @param Abstract_Schema_Piece $graph_piece_generator A value object with context variables.
[291] Fix | Delete
* @param Abstract_Schema_Piece[] $graph_piece_generators A value object with context variables.
[292] Fix | Delete
*/
[293] Fix | Delete
$graph_piece = \apply_filters( 'wpseo_schema_' . $type, $graph_piece, $context, $graph_piece_generator, $graph_piece_generators );
[294] Fix | Delete
}
[295] Fix | Delete
}
[296] Fix | Delete
[297] Fix | Delete
return $graph_piece;
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
/**
[301] Fix | Delete
* Retrieves the type from a graph piece.
[302] Fix | Delete
*
[303] Fix | Delete
* @param array $piece The graph piece.
[304] Fix | Delete
*
[305] Fix | Delete
* @return array An array of the piece's types.
[306] Fix | Delete
*/
[307] Fix | Delete
private function get_type_from_piece( $piece ) {
[308] Fix | Delete
if ( isset( $piece['@type'] ) ) {
[309] Fix | Delete
if ( \is_array( $piece['@type'] ) ) {
[310] Fix | Delete
return $piece['@type'];
[311] Fix | Delete
}
[312] Fix | Delete
[313] Fix | Delete
return [ $piece['@type'] ];
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
return [];
[317] Fix | Delete
}
[318] Fix | Delete
[319] Fix | Delete
/**
[320] Fix | Delete
* Validates a graph piece's type.
[321] Fix | Delete
*
[322] Fix | Delete
* When the type is an array:
[323] Fix | Delete
* - Ensure the values are unique.
[324] Fix | Delete
* - Only 1 value? Use that value without the array wrapping.
[325] Fix | Delete
*
[326] Fix | Delete
* @param array $piece The graph piece.
[327] Fix | Delete
*
[328] Fix | Delete
* @return array The graph piece.
[329] Fix | Delete
*/
[330] Fix | Delete
private function validate_type( $piece ) {
[331] Fix | Delete
if ( ! isset( $piece['@type'] ) ) {
[332] Fix | Delete
// No type to validate.
[333] Fix | Delete
return $piece;
[334] Fix | Delete
}
[335] Fix | Delete
[336] Fix | Delete
// If it is not an array, we can return immediately.
[337] Fix | Delete
if ( ! \is_array( $piece['@type'] ) ) {
[338] Fix | Delete
return $piece;
[339] Fix | Delete
}
[340] Fix | Delete
[341] Fix | Delete
/*
[342] Fix | Delete
* Ensure the types are unique.
[343] Fix | Delete
* Use array_values to reset the indices (e.g. no 0, 2 because 1 was a duplicate).
[344] Fix | Delete
*/
[345] Fix | Delete
$piece['@type'] = \array_values( \array_unique( $piece['@type'] ) );
[346] Fix | Delete
[347] Fix | Delete
// Use the first value if there is only 1 type.
[348] Fix | Delete
if ( \count( $piece['@type'] ) === 1 ) {
[349] Fix | Delete
$piece['@type'] = \reset( $piece['@type'] );
[350] Fix | Delete
}
[351] Fix | Delete
[352] Fix | Delete
return $piece;
[353] Fix | Delete
}
[354] Fix | Delete
}
[355] Fix | Delete
[356] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function