PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.5
28.6 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 All 130 releases
wordpress-seo / src / schema-aggregator / infrastructure / schema-pieces / wordpress-global-state-adapter.php

wordpress-global-state-adapter.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.5, at src/schema-aggregator/infrastructure/schema-pieces/wordpress-global-state-adapter.php

107 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
3 namespace Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Schema_Pieces;
4
5 use WP_Post;
6 use Yoast\WP\SEO\Models\Indexable;
7 /**
8 * Helper class to set and reset WordPress global state.
9 */
10 class WordPress_Global_State_Adapter {
11
12 /**
13 * Previous global $post
14 *
15 * @var WP_Post|null
16 */
17 private $previous_post;
18
19 /**
20 * Previous global $wp_query->queried_object
21 *
22 * @var WP_Post|null
23 */
24 private $previous_queried_object;
25
26 /**
27 * Previous global $wp_query->queried_object_id
28 *
29 * @var int|string|null
30 */
31 private $previous_queried_object_id;
32
33 /**
34 * Previous query flags
35 *
36 * @var array<string, bool>
37 */
38 private $previous_query_flags;
39
40 /**
41 * Set WordPress global state
42 *
43 * Helper method to set $post and $wp_query globals based on the given indexable.
44 * This is critical to ensure that schema pieces relying on global state function correctly.
45 *
46 * @param Indexable $indexable The indexable to set the global state for.
47 *
48 * @return void
49 */
50 public function set_global_state( Indexable $indexable ): void {
51 global $post, $wp_query;
52 $this->previous_post = $post;
53 $this->previous_queried_object = ( $wp_query->queried_object ?? null );
54 $this->previous_queried_object_id = ( $wp_query->queried_object_id ?? null );
55
56 $this->previous_query_flags = [
57 'is_single' => ( $wp_query->is_single ?? false ),
58 'is_page' => ( $wp_query->is_page ?? false ),
59 'is_singular' => ( $wp_query->is_singular ?? false ),
60 ];
61
62 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
63 $post = \get_post( $indexable->object_id );
64 $wp_query->queried_object = \get_post( $indexable->object_id );
65 $wp_query->queried_object_id = $indexable->object_id;
66
67 $wp_query->is_single = false;
68 $wp_query->is_page = false;
69 $wp_query->is_singular = true;
70
71 if ( $indexable->object_sub_type === 'page' ) {
72 $wp_query->is_page = true;
73 }
74 else {
75 $wp_query->is_single = true;
76
77 }
78
79 \setup_postdata( $post );
80 }
81
82 /**
83 * Restore WordPress global state
84 *
85 * Helper method to restore $post and $wp_query globals after schema collection.
86 * This is critical to prevent side effects that could corrupt WordPress's global context.
87 *
88 * @return void
89 */
90 public function reset_global_state(): void {
91 global $post, $wp_query;
92
93 \wp_reset_postdata();
94
95 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To reset the post we need to do this explicitly.
96 $post = $this->previous_post;
97
98 if ( isset( $wp_query ) && \is_object( $wp_query ) ) {
99 $wp_query->queried_object = $this->previous_queried_object;
100 $wp_query->queried_object_id = $this->previous_queried_object_id;
101 $wp_query->is_single = $this->previous_query_flags['is_single'];
102 $wp_query->is_page = $this->previous_query_flags['is_page'];
103 $wp_query->is_singular = $this->previous_query_flags['is_singular'];
104 }
105 }
106 }
107