PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.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 19.0 All 129 releases
wordpress-seo / src / actions / indexables / indexable-head-action.php

indexable-head-action.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.6, at src/actions/indexables/indexable-head-action.php

175 lines 3.8 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\Actions\Indexables;
4
5 use Yoast\WP\SEO\Surfaces\Meta_Surface;
6 use Yoast\WP\SEO\Surfaces\Values\Meta;
7
8 /**
9 * Get head action for indexables.
10 */
11 class Indexable_Head_Action {
12
13 /**
14 * Caches the output.
15 *
16 * @var mixed
17 */
18 protected $cache;
19
20 /**
21 * The meta surface.
22 *
23 * @var Meta_Surface
24 */
25 private $meta_surface;
26
27 /**
28 * Indexable_Head_Action constructor.
29 *
30 * @param Meta_Surface $meta_surface The meta surface.
31 */
32 public function __construct( Meta_Surface $meta_surface ) {
33 $this->meta_surface = $meta_surface;
34 }
35
36 /**
37 * Retrieves the head for a url.
38 *
39 * @param string $url The url to get the head for.
40 *
41 * @return object Object with head and status properties.
42 */
43 public function for_url( $url ) {
44 if ( $url === \trailingslashit( \get_home_url() ) ) {
45 return $this->with_404_fallback( $this->with_cache( 'home_page' ) );
46 }
47 return $this->with_404_fallback( $this->with_cache( 'url', $url ) );
48 }
49
50 /**
51 * Retrieves the head for a post.
52 *
53 * @param int $id The id.
54 *
55 * @return object Object with head and status properties.
56 */
57 public function for_post( $id ) {
58 return $this->with_404_fallback( $this->with_cache( 'post', $id ) );
59 }
60
61 /**
62 * Retrieves the head for a term.
63 *
64 * @param int $id The id.
65 *
66 * @return object Object with head and status properties.
67 */
68 public function for_term( $id ) {
69 return $this->with_404_fallback( $this->with_cache( 'term', $id ) );
70 }
71
72 /**
73 * Retrieves the head for an author.
74 *
75 * @param int $id The id.
76 *
77 * @return object Object with head and status properties.
78 */
79 public function for_author( $id ) {
80 return $this->with_404_fallback( $this->with_cache( 'author', $id ) );
81 }
82
83 /**
84 * Retrieves the head for a post type archive.
85 *
86 * @param int $type The id.
87 *
88 * @return object Object with head and status properties.
89 */
90 public function for_post_type_archive( $type ) {
91 return $this->with_404_fallback( $this->with_cache( 'post_type_archive', $type ) );
92 }
93
94 /**
95 * Retrieves the head for the posts page.
96 *
97 * @return object Object with head and status properties.
98 */
99 public function for_posts_page() {
100 return $this->with_404_fallback( $this->with_cache( 'posts_page' ) );
101 }
102
103 /**
104 * Retrieves the head for the 404 page. Always sets the status to 404.
105 *
106 * @return object Object with head and status properties.
107 */
108 public function for_404() {
109 $meta = $this->with_cache( '404' );
110
111 if ( ! $meta ) {
112 return (object) [
113 'html' => '',
114 'json' => [],
115 'status' => 404,
116 ];
117 }
118
119 $head = $meta->get_head();
120
121 return (object) [
122 'html' => $head->html,
123 'json' => $head->json,
124 'status' => 404,
125 ];
126 }
127
128 /**
129 * Retrieves the head for a successful page load.
130 *
131 * @param object $head The calculated Yoast head.
132 *
133 * @return object The presentations and status code 200.
134 */
135 protected function for_200( $head ) {
136 return (object) [
137 'html' => $head->html,
138 'json' => $head->json,
139 'status' => 200,
140 ];
141 }
142
143 /**
144 * Returns the head with 404 fallback
145 *
146 * @param Meta|false $meta The meta object.
147 *
148 * @return object The head response.
149 */
150 protected function with_404_fallback( $meta ) {
151 if ( $meta === false ) {
152 return $this->for_404();
153 }
154 else {
155 return $this->for_200( $meta->get_head() );
156 }
157 }
158
159 /**
160 * Retrieves a value from the meta surface cached.
161 *
162 * @param string $type The type of value to retrieve.
163 * @param string $argument Optional. The argument for the value.
164 *
165 * @return Meta The meta object.
166 */
167 protected function with_cache( $type, $argument = '' ) {
168 if ( ! isset( $this->cache[ $type ][ $argument ] ) ) {
169 $this->cache[ $type ][ $argument ] = \call_user_func( [ $this->meta_surface, "for_$type" ], $argument );
170 }
171
172 return $this->cache[ $type ][ $argument ];
173 }
174 }
175