PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.5
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 / surfaces / values / meta.php

meta.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.5, at src/surfaces/values/meta.php

290 lines 10.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\Surfaces\Values;
4
5 use Exception;
6 use WPSEO_Replace_Vars;
7 use Yoast\WP\SEO\Context\Meta_Tags_Context;
8 use Yoast\WP\SEO\Integrations\Front_End_Integration;
9 use Yoast\WP\SEO\Models\Indexable;
10 use Yoast\WP\SEO\Presenters\Abstract_Indexable_Presenter;
11 use Yoast\WP\SEO\Presenters\Rel_Next_Presenter;
12 use Yoast\WP\SEO\Presenters\Rel_Prev_Presenter;
13 use Yoast\WP\SEO\Surfaces\Helpers_Surface;
14 use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface;
15
16 /**
17 * Meta value object.
18 *
19 * @property array $breadcrumbs The breadcrumbs array for the current page.
20 * @property string $canonical The canonical URL for the current page.
21 * @property string $company_name The company name from the Knowledge graph settings.
22 * @property int $company_logo_id The attachment ID for the company logo.
23 * @property string $description The meta description for the current page, if set.
24 * @property int $estimated_reading_time_minutes The estimated reading time in minutes for posts.
25 * @property Indexable $indexable The indexable object.
26 * @property string $main_schema_id Schema ID that points to the main Schema thing on the page, usually the webpage or article Schema piece.
27 * @property string $meta_description The meta description for the current page, if set.
28 * @property string $open_graph_article_author The article:author value.
29 * @property string $open_graph_article_modified_time The article:modified_time value.
30 * @property string $open_graph_article_published_time The article:published_time value.
31 * @property string $open_graph_article_publisher The article:publisher value.
32 * @property string $open_graph_description The og:description.
33 * @property bool $open_graph_enabled Whether OpenGraph is enabled on this site.
34 * @property string $open_graph_fb_app_id The Facebook App ID.
35 * @property array $open_graph_images The array of images we have for this page.
36 * @property string $open_graph_locale The og:locale for the current page.
37 * @property string $open_graph_publisher The OpenGraph publisher reference.
38 * @property string $open_graph_site_name The og:site_name.
39 * @property string $open_graph_title The og:title.
40 * @property string $open_graph_type The og:type.
41 * @property string $open_graph_url The og:url.
42 * @property string $page_type The Schema page type.
43 * @property array $robots An array of the robots values set for the current page.
44 * @property string $rel_next The next page in the series, if any.
45 * @property string $rel_prev The previous page in the series, if any.
46 * @property array $schema The entire Schema array for the current page.
47 * @property string $schema_page_type The Schema page type.
48 * @property string $site_name The site name from the Yoast SEO settings.
49 * @property string $site_represents Whether the site represents a 'person' or a 'company'.
50 * @property array|false $site_represents_reference The schema reference ID for what this site represents.
51 * @property string $site_url The site's main URL.
52 * @property int $site_user_id If the site represents a 'person', this is the ID of the accompanying user profile.
53 * @property string $title The SEO title for the current page.
54 * @property string $twitter_card The Twitter card type for the current page.
55 * @property string $twitter_creator The Twitter card author for the current page.
56 * @property string $twitter_description The Twitter card description for the current page.
57 * @property string $twitter_image The Twitter card image for the current page.
58 * @property string $twitter_site The Twitter card site reference for the current page.
59 * @property string $twitter_title The Twitter card title for the current page.
60 * @property string $wordpress_site_name The site name from the WordPress settings.
61 */
62 class Meta {
63
64 /**
65 * The container.
66 *
67 * @var ContainerInterface
68 */
69 protected $container;
70
71 /**
72 * The meta tags context.
73 *
74 * @var Meta_Tags_Context
75 */
76 protected $context;
77
78 /**
79 * The front end integration.
80 *
81 * @var Front_End_Integration
82 */
83 protected $front_end;
84
85 /**
86 * The helpers surface.
87 *
88 * @var Helpers_Surface
89 */
90 protected $helpers;
91
92 /**
93 * The replace vars helper
94 *
95 * @var WPSEO_Replace_Vars
96 */
97 protected $replace_vars;
98
99 /**
100 * Create a meta value object.
101 *
102 * @param Meta_Tags_Context $context The indexable presentation.
103 * @param ContainerInterface $container The DI container.
104 */
105 public function __construct(
106 Meta_Tags_Context $context,
107 ContainerInterface $container
108 ) {
109 $this->container = $container;
110 $this->context = $context;
111
112 $this->helpers = $this->container->get( Helpers_Surface::class );
113 $this->replace_vars = $this->container->get( WPSEO_Replace_Vars::class );
114 $this->front_end = $this->container->get( Front_End_Integration::class );
115 }
116
117 /**
118 * Returns the output as would be presented in the head.
119 *
120 * @return object The HTML and JSON presentation of the head metadata.
121 */
122 public function get_head() {
123 $presenters = $this->get_presenters();
124
125 /** This filter is documented in src/integrations/front-end-integration.php */
126 $presentation = \apply_filters( 'wpseo_frontend_presentation', $this->context->presentation, $this->context );
127
128 $html_output = '';
129 $json_head_fields = [];
130
131 foreach ( $presenters as $presenter ) {
132 $presenter->presentation = $presentation;
133 $presenter->replace_vars = $this->replace_vars;
134 $presenter->helpers = $this->helpers;
135
136 $html_output .= $this->create_html_presentation( $presenter );
137 $json_field = $this->create_json_field( $presenter );
138
139 // Only use the output of presenters that could successfully present their data.
140 if ( $json_field !== null && ! empty( $json_field->key ) ) {
141 $json_head_fields[ $json_field->key ] = $json_field->value;
142 }
143 }
144 $html_output = \trim( $html_output );
145
146 return (object) [
147 'html' => $html_output,
148 'json' => $json_head_fields,
149 ];
150 }
151
152 /**
153 * Magic getter for presenting values through the appropriate presenter, if it exists.
154 *
155 * @param string $name The property to get.
156 *
157 * @return mixed The value, as presented by teh appropriate presenter.
158 *
159 * @throws Exception If an invalid property is accessed.
160 */
161 public function __get( $name ) {
162 /** This filter is documented in src/integrations/front-end-integration.php */
163 $presentation = \apply_filters( 'wpseo_frontend_presentation', $this->context->presentation, $this->context );
164
165 if ( ! isset( $presentation->{$name} ) ) {
166 if ( isset( $this->context->{$name} ) ) {
167 $this->{$name} = $this->context->{$name};
168 return $this->{$name};
169 }
170 return null;
171 }
172
173 $presenter_namespace = 'Yoast\WP\SEO\Presenters\\';
174 $parts = \explode( '_', $name );
175 if ( $parts[0] === 'twitter' ) {
176 $presenter_namespace .= 'Twitter\\';
177 $parts = \array_slice( $parts, 1 );
178 }
179 elseif ( $parts[0] === 'open' && $parts[1] === 'graph' ) {
180 $presenter_namespace .= 'Open_Graph\\';
181 $parts = \array_slice( $parts, 2 );
182 }
183
184 $presenter_class = $presenter_namespace . \implode( '_', \array_map( 'ucfirst', $parts ) ) . '_Presenter';
185
186 if ( \class_exists( $presenter_class ) ) {
187 /**
188 * The indexable presenter.
189 *
190 * @var Abstract_Indexable_Presenter
191 */
192 $presenter = new $presenter_class();
193 $presenter->presentation = $presentation;
194 $presenter->helpers = $this->helpers;
195 $presenter->replace_vars = $this->replace_vars;
196 $value = $presenter->get();
197 }
198 else {
199 $value = $presentation->{$name};
200 }
201
202 $this->{$name} = $value;
203 return $this->{$name};
204 }
205
206 /**
207 * Magic isset for ensuring properties on the presentation are recognised.
208 *
209 * @param string $name The property to get.
210 *
211 * @return bool Whether or not the requested property exists.
212 */
213 public function __isset( $name ) {
214 return isset( $this->context->presentation->{$name} );
215 }
216
217 /**
218 * Strips all nested dependencies from the debug info.
219 *
220 * @return array
221 */
222 public function __debugInfo() {
223 return [ 'context' => $this->context ];
224 }
225
226 /**
227 * Returns all presenters.
228 *
229 * @return Abstract_Indexable_Presenter[]
230 */
231 protected function get_presenters() {
232 $presenters = $this->front_end->get_presenters( $this->context->page_type, $this->context );
233
234 if ( $this->context->page_type === 'Date_Archive' ) {
235 /**
236 * Define a filter that removes objects of type Rel_Next_Presenter or Rel_Prev_Presenter from a list.
237 *
238 * @param object $presenter The presenter to verify.
239 *
240 * @return bool True if the presenter is not a Rel_Next or Rel_Prev presenter.
241 */
242 $callback = static function ( $presenter ) {
243 return ! \is_a( $presenter, Rel_Next_Presenter::class )
244 && ! \is_a( $presenter, Rel_Prev_Presenter::class );
245 };
246 $presenters = \array_filter( $presenters, $callback );
247 }
248
249 return $presenters;
250 }
251
252 /**
253 * Uses the presenter to create a line of HTML.
254 *
255 * @param Abstract_Indexable_Presenter $presenter The presenter.
256 *
257 * @return string
258 */
259 protected function create_html_presentation( $presenter ) {
260 $presenter_output = $presenter->present();
261 if ( ! empty( $presenter_output ) ) {
262 return $presenter_output . \PHP_EOL;
263 }
264 return '';
265 }
266
267 /**
268 * Converts a presenter's key and value to JSON.
269 *
270 * @param Abstract_Indexable_Presenter $presenter The presenter whose key and value are to be converted to JSON.
271 *
272 * @return object|null
273 */
274 protected function create_json_field( $presenter ) {
275 if ( $presenter->get_key() === 'NO KEY PROVIDED' ) {
276 return null;
277 }
278
279 $value = $presenter->get();
280 if ( empty( $value ) ) {
281 return null;
282 }
283
284 return (object) [
285 'key' => $presenter->escape_key(),
286 'value' => $value,
287 ];
288 }
289 }
290