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 / memoizers / presentation-memoizer.php

presentation-memoizer.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.9, at src/memoizers/presentation-memoizer.php

90 lines 2.3 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\Memoizers;
4
5 use Yoast\WP\SEO\Context\Meta_Tags_Context;
6 use Yoast\WP\SEO\Models\Indexable;
7 use Yoast\WP\SEO\Presentations\Indexable_Presentation;
8 use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11 * The presentation memoizer.
12 */
13 class Presentation_Memoizer {
14
15 /**
16 * The service container.
17 *
18 * @var ContainerInterface
19 */
20 protected $container;
21
22 /**
23 * Cache with indexable presentations.
24 *
25 * @var Indexable_Presentation[]
26 */
27 protected $cache = [];
28
29 /**
30 * Presentation_Memoizer constructor.
31 *
32 * @param ContainerInterface $service_container The service container.
33 */
34 public function __construct( ContainerInterface $service_container ) {
35 $this->container = $service_container;
36 }
37
38 /**
39 * Gets the presentation of an indexable for a specific page type.
40 * This function is memoized by the indexable so every call with the same indexable will yield the same result.
41 *
42 * @param Indexable $indexable The indexable to get a presentation of.
43 * @param Meta_Tags_Context $context The current meta tags context.
44 * @param string $page_type The page type.
45 *
46 * @return Indexable_Presentation The indexable presentation.
47 */
48 public function get( Indexable $indexable, Meta_Tags_Context $context, $page_type ) {
49 if ( ! isset( $this->cache[ $indexable->id ] ) ) {
50 $presentation = $this->container->get( "Yoast\WP\SEO\Presentations\Indexable_{$page_type}_Presentation", ContainerInterface::NULL_ON_INVALID_REFERENCE );
51
52 if ( ! $presentation ) {
53 $presentation = $this->container->get( Indexable_Presentation::class );
54 }
55
56 $context->presentation = $presentation->of(
57 [
58 'model' => $indexable,
59 'context' => $context,
60 ],
61 );
62
63 $this->cache[ $indexable->id ] = $context->presentation;
64 }
65
66 return $this->cache[ $indexable->id ];
67 }
68
69 /**
70 * Clears the memoization of either a specific indexable or all indexables.
71 *
72 * @param Indexable|int|null $indexable Optional. The indexable or indexable id to clear the memoization of.
73 *
74 * @return void
75 */
76 public function clear( $indexable = null ) {
77 if ( $indexable instanceof Indexable ) {
78 unset( $this->cache[ $indexable->id ] );
79 return;
80 }
81 if ( \is_int( $indexable ) ) {
82 unset( $this->cache[ $indexable ] );
83 return;
84 }
85 if ( $indexable === null ) {
86 $this->cache = [];
87 }
88 }
89 }
90