← All changes
|
src/schema-aggregator/infrastructure/schema-pieces/wordpress-global-state-adapter.php
+33
-3
27.5
→
trunk
View file →
| @@ -2,8 +2,10 @@ | ||
| 2 | 2 | // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 | 3 | namespace Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Schema_Pieces; |
| 4 | 4 | |
| 5 | 5 | use WP_Post; |
| 6 | +use Yoast\WP\SEO\Context\Meta_Tags_Context; | |
| 7 | +use Yoast\WP\SEO\Memoizers\Meta_Tags_Context_Memoizer; | |
| 6 | 8 | use Yoast\WP\SEO\Models\Indexable; |
| 7 | 9 | /** |
| 8 | 10 | * Helper class to set and reset WordPress global state. |
| 9 | 11 | */ |
| @@ -9,8 +11,15 @@ | ||
| 9 | 11 | */ |
| 10 | 12 | class WordPress_Global_State_Adapter { |
| 11 | 13 | |
| 12 | 14 | /** |
| 15 | + * The meta tags context memoizer. | |
| 16 | + * | |
| 17 | + * @var Meta_Tags_Context_Memoizer | |
| 18 | + */ | |
| 19 | + private $memoizer; | |
| 20 | + | |
| 21 | + /** | |
| 13 | 22 | * Previous global $post |
| 14 | 23 | * |
| 15 | 24 | * @var WP_Post|null |
| 16 | 25 | */ |
| @@ -37,18 +46,30 @@ | ||
| 37 | 46 | */ |
| 38 | 47 | private $previous_query_flags; |
| 39 | 48 | |
| 40 | 49 | /** |
| 50 | + * Constructor. | |
| 51 | + * | |
| 52 | + * @param Meta_Tags_Context_Memoizer $memoizer The meta tags context memoizer. | |
| 53 | + */ | |
| 54 | + public function __construct( Meta_Tags_Context_Memoizer $memoizer ) { | |
| 55 | + $this->memoizer = $memoizer; | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 41 | 59 | * Set WordPress global state |
| 42 | 60 | * |
| 43 | - * Helper method to set $post and $wp_query globals based on the given indexable. | |
| 61 | + * Helper method to set $post and $wp_query globals based on the given indexable, and | |
| 62 | + * prime the memoizer's current_page slot with the indexable's context so external schema | |
| 63 | + * generators (e.g. WPSEO_WooCommerce_Schema) read the correct per-indexable values. | |
| 44 | 64 | * This is critical to ensure that schema pieces relying on global state function correctly. |
| 45 | 65 | * |
| 46 | - * @param Indexable $indexable The indexable to set the global state for. | |
| 66 | + * @param Indexable $indexable The indexable to set the global state for. | |
| 67 | + * @param Meta_Tags_Context $context The indexable's context, installed as the current_page slot. | |
| 47 | 68 | * |
| 48 | 69 | * @return void |
| 49 | 70 | */ |
| 50 | - public function set_global_state( Indexable $indexable ): void { | |
| 71 | + public function set_global_state( Indexable $indexable, Meta_Tags_Context $context ): void { | |
| 51 | 72 | global $post, $wp_query; |
| 52 | 73 | $this->previous_post = $post; |
| 53 | 74 | $this->previous_queried_object = ( $wp_query->queried_object ?? null ); |
| 54 | 75 | $this->previous_queried_object_id = ( $wp_query->queried_object_id ?? null ); |
| @@ -76,8 +97,13 @@ | ||
| 76 | 97 | |
| 77 | 98 | } |
| 78 | 99 | |
| 79 | 100 | \setup_postdata( $post ); |
| 101 | + | |
| 102 | + // Make for_current_page() resolve to the indexable being processed, so external schema | |
| 103 | + // generators (e.g. WPSEO_WooCommerce_Schema) read the correct per-indexable canonical / | |
| 104 | + // main_schema_id. reset_global_state() clears this slot at the end of the iteration. | |
| 105 | + $this->memoizer->set_for_current_page( $context ); | |
| 80 | 106 | } |
| 81 | 107 | |
| 82 | 108 | /** |
| 83 | 109 | * Restore WordPress global state |
| @@ -101,6 +127,10 @@ | ||
| 101 | 127 | $wp_query->is_single = $this->previous_query_flags['is_single']; |
| 102 | 128 | $wp_query->is_page = $this->previous_query_flags['is_page']; |
| 103 | 129 | $wp_query->is_singular = $this->previous_query_flags['is_singular']; |
| 104 | 130 | } |
| 131 | + | |
| 132 | + // Drop the per-iteration current_page context primed by Schema_Piece_Repository::get(), | |
| 133 | + // so the next iteration re-resolves cleanly. | |
| 134 | + $this->memoizer->clear_for_current_page(); | |
| 105 | 135 | } |
| 106 | 136 | } |