1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
<?php
namespace Yoast\WP\SEO\Helpers\Schema;
use Closure; use WPSEO_Replace_Vars; use Yoast\WP\SEO\Conditionals\No_Conditionals; use Yoast\WP\SEO\Config\Schema_IDs; use Yoast\WP\SEO\Context\Meta_Tags_Context; use Yoast\WP\SEO\Presentations\Indexable_Presentation;
/** * Registers the Schema replace variables and exposes a method to replace variables on a Schema graph. */ class Replace_Vars_Helper {
use No_Conditionals;
/** * The replace vars. * * @var WPSEO_Replace_Vars */ protected $replace_vars;
/** * The Schema ID helper. * * @var ID_Helper */ protected $id_helper;
/** * Replace_Vars_Helper constructor. * * @param WPSEO_Replace_Vars $replace_vars The replace vars. * @param ID_Helper $id_helper The Schema ID helper. */ public function __construct( WPSEO_Replace_Vars $replace_vars, ID_Helper $id_helper ) { $this->replace_vars = $replace_vars; $this->id_helper = $id_helper; }
/** * Replaces the variables. * * @param array $schema_data The Schema data. * @param Indexable_Presentation $presentation The indexable presentation. * * @return array The array with replaced vars. */ public function replace( array $schema_data, Indexable_Presentation $presentation ) { foreach ( $schema_data as $key => $value ) { if ( \is_array( $value ) ) { $schema_data[ $key ] = $this->replace( $value, $presentation );
continue; }
$schema_data[ $key ] = $this->replace_vars->replace( $value, $presentation->source ); }
return $schema_data; }
/** * Registers the Schema-related replace vars. * * @param Meta_Tags_Context $context The meta tags context. * * @return void */ public function register_replace_vars( $context ) { $replace_vars = [ 'main_schema_id' => $context->main_schema_id, 'author_id' => $this->id_helper->get_user_schema_id( $context->indexable->author_id, $context ), 'person_id' => $context->site_url . Schema_IDs::PERSON_HASH, 'primary_image_id' => $context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH, 'webpage_id' => $context->canonical . Schema_IDs::WEBPAGE_HASH, 'website_id' => $context->site_url . Schema_IDs::WEBSITE_HASH, 'organization_id' => $context->site_url . Schema_IDs::ORGANIZATION_HASH, ];
if ( $context->post ) { // Post does not always exist, e.g. on term pages. $replace_vars['post_date'] = $context->post->post_date; }
foreach ( $replace_vars as $var => $value ) { $this->register_replacement( $var, $value ); } }
/** * Registers a replace var and its value. * * @param string $variable The replace variable. * @param string $value The value that the variable should be replaced with. */ protected function register_replacement( $variable, $value ) { $this->replace_vars->safe_register_replacement( $variable, $this->get_identity_function( $value ) ); }
/** * Returns an anonymous function that in turn just returns the given value. * * @param mixed $value The value that the function should return. * * @return Closure A function that returns the given value. */ protected function get_identity_function( $value ) { return static function() use ( $value ) { return $value; }; } }
|