PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.9
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / helpers / schema / replace-vars-helper.php

replace-vars-helper.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.9, at src/helpers/schema/replace-vars-helper.php

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