| 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 |
return $this->with_404_fallback( $this->with_cache( 'url', $url ) ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Retrieves the head for a post. |
| 49 |
* |
| 50 |
* @param int $id The id. |
| 51 |
* |
| 52 |
* @return object Object with head and status properties. |
| 53 |
*/ |
| 54 |
public function for_post( $id ) { |
| 55 |
return $this->with_404_fallback( $this->with_cache( 'post', $id ) ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Retrieves the head for a term. |
| 60 |
* |
| 61 |
* @param int $id The id. |
| 62 |
* |
| 63 |
* @return object Object with head and status properties. |
| 64 |
*/ |
| 65 |
public function for_term( $id ) { |
| 66 |
return $this->with_404_fallback( $this->with_cache( 'term', $id ) ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Retrieves the head for an author. |
| 71 |
* |
| 72 |
* @param int $id The id. |
| 73 |
* |
| 74 |
* @return object Object with head and status properties. |
| 75 |
*/ |
| 76 |
public function for_author( $id ) { |
| 77 |
return $this->with_404_fallback( $this->with_cache( 'author', $id ) ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Retrieves the head for a post type archive. |
| 82 |
* |
| 83 |
* @param int $type The id. |
| 84 |
* |
| 85 |
* @return object Object with head and status properties. |
| 86 |
*/ |
| 87 |
public function for_post_type_archive( $type ) { |
| 88 |
return $this->with_404_fallback( $this->with_cache( 'post_type_archive', $type ) ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Retrieves the head for the posts page. |
| 93 |
* |
| 94 |
* @return object Object with head and status properties. |
| 95 |
*/ |
| 96 |
public function for_posts_page() { |
| 97 |
return $this->with_404_fallback( $this->with_cache( 'posts_page' ) ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Retrieves the head for the 404 page. Always sets the status to 404. |
| 102 |
* |
| 103 |
* @return object Object with head and status properties. |
| 104 |
*/ |
| 105 |
public function for_404() { |
| 106 |
$meta = $this->with_cache( '404' ); |
| 107 |
|
| 108 |
if ( ! $meta ) { |
| 109 |
return (object) [ |
| 110 |
'html' => '', |
| 111 |
'json' => [], |
| 112 |
'status' => 404, |
| 113 |
]; |
| 114 |
} |
| 115 |
|
| 116 |
$head = $meta->get_head(); |
| 117 |
|
| 118 |
return (object) [ |
| 119 |
'html' => $head->html, |
| 120 |
'json' => $head->json, |
| 121 |
'status' => 404, |
| 122 |
]; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Retrieves the head for a successful page load. |
| 127 |
* |
| 128 |
* @param object $head The calculated Yoast head. |
| 129 |
* |
| 130 |
* @return object The presentations and status code 200. |
| 131 |
*/ |
| 132 |
protected function for_200( $head ) { |
| 133 |
return (object) [ |
| 134 |
'html' => $head->html, |
| 135 |
'json' => $head->json, |
| 136 |
'status' => 200, |
| 137 |
]; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Returns the head with 404 fallback |
| 142 |
* |
| 143 |
* @param Meta|false $meta The meta object. |
| 144 |
* |
| 145 |
* @return object The head response. |
| 146 |
*/ |
| 147 |
protected function with_404_fallback( $meta ) { |
| 148 |
if ( $meta === false ) { |
| 149 |
return $this->for_404(); |
| 150 |
} |
| 151 |
else { |
| 152 |
return $this->for_200( $meta->get_head() ); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Retrieves a value from the meta surface cached. |
| 158 |
* |
| 159 |
* @param string $type The type of value to retrieve. |
| 160 |
* @param string $argument Optional. The argument for the value. |
| 161 |
* |
| 162 |
* @return Meta The meta object. |
| 163 |
*/ |
| 164 |
protected function with_cache( $type, $argument = '' ) { |
| 165 |
if ( ! isset( $this->cache[ $type ][ $argument ] ) ) { |
| 166 |
$this->cache[ $type ][ $argument ] = \call_user_func( [ $this->meta_surface, "for_$type" ], $argument ); |
| 167 |
} |
| 168 |
|
| 169 |
return $this->cache[ $type ][ $argument ]; |
| 170 |
} |
| 171 |
} |
| 172 |
|