| 1 |
<?php |
| 2 |
/** |
| 3 |
* Get content type matrix ability. |
| 4 |
* |
| 5 |
* @package ThinkRank\Abilities\Matrix |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace ThinkRank\Abilities\Matrix; |
| 11 |
|
| 12 |
use ThinkRank\Abilities\Ability_Base; |
| 13 |
use ThinkRank\SEO\Content_Type_Settings; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Reads the per-content-type SEO feature matrix. |
| 21 |
* |
| 22 |
* Mirrors `GET /thinkrank/v1/global-seo/matrix`: every entity the site has |
| 23 |
* (post types, taxonomies, the blog index, author and date archives, search and |
| 24 |
* 404) against the features that can be switched for it. |
| 25 |
* |
| 26 |
* This ability is also how a caller discovers valid entity keys, which are not |
| 27 |
* guessable: a post type is stored under its bare slug, while the other |
| 28 |
* entities carry a prefix that cannot collide with one — `taxonomy:category`, |
| 29 |
* `archive:author`, `special:404`. Read this before calling |
| 30 |
* update-content-type-matrix. |
| 31 |
* |
| 32 |
* Every feature is three-state and defaults to `inherit`, which resolves to the |
| 33 |
* site-wide value returned under `globals`. That is why an untouched site emits |
| 34 |
* exactly what it emitted before the matrix existed. |
| 35 |
*/ |
| 36 |
class Get_Content_Type_Matrix extends Ability_Base { |
| 37 |
|
| 38 |
/** |
| 39 |
* Constructor. |
| 40 |
*/ |
| 41 |
public function __construct() { |
| 42 |
$this->id = 'thinkrank/get-content-type-matrix'; |
| 43 |
$this->label = __( 'Get ThinkRank Content Type Matrix', 'thinkrank' ); |
| 44 |
$this->description = __( 'Read the per-content-type SEO feature matrix: for each post type, taxonomy, archive and special page, whether metas, schema, Open Graph, Twitter cards and analytics are inherited, forced on, or forced off, plus its robots directives and sitemap inclusion. Also returns the site-wide value each "inherit" resolves to. Use this to discover valid entity keys, then update-content-type-matrix to change one.', 'thinkrank' ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* {@inheritDoc} |
| 49 |
* |
| 50 |
* @return array<string, bool|float|string> |
| 51 |
*/ |
| 52 |
public function get_annotations() { |
| 53 |
return [ |
| 54 |
'readonly' => true, |
| 55 |
'destructive' => false, |
| 56 |
'idempotent' => true, |
| 57 |
'priority' => 1.0, |
| 58 |
'openWorldHint' => false, |
| 59 |
]; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* {@inheritDoc} |
| 64 |
* |
| 65 |
* @return array<string, mixed> |
| 66 |
*/ |
| 67 |
public function get_input_schema() { |
| 68 |
return [ |
| 69 |
'type' => 'object', |
| 70 |
'additionalProperties' => false, |
| 71 |
'properties' => self::empty_properties(), |
| 72 |
]; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* {@inheritDoc} |
| 77 |
* |
| 78 |
* @return array<string, mixed> |
| 79 |
*/ |
| 80 |
public function get_output_schema() { |
| 81 |
return [ |
| 82 |
'type' => 'object', |
| 83 |
'properties' => [ |
| 84 |
'entities' => [ |
| 85 |
'type' => 'array', |
| 86 |
'items' => [ |
| 87 |
'type' => 'object', |
| 88 |
'additionalProperties' => true, |
| 89 |
'properties' => [ |
| 90 |
'key' => [ |
| 91 |
'type' => 'string', |
| 92 |
'description' => __( 'Entity key. A post type is its bare slug; everything else is prefixed: taxonomy:, archive:, special:.', 'thinkrank' ), |
| 93 |
], |
| 94 |
'label' => [ 'type' => 'string' ], |
| 95 |
'group' => [ |
| 96 |
'type' => 'string', |
| 97 |
'enum' => [ 'post_type', 'taxonomy', 'archive', 'special' ], |
| 98 |
], |
| 99 |
'features' => [ |
| 100 |
'type' => 'array', |
| 101 |
'items' => [ 'type' => 'string' ], |
| 102 |
'description' => __( 'Which features this entity exposes. Search and 404 carry no schema, for example.', 'thinkrank' ), |
| 103 |
], |
| 104 |
'values' => [ |
| 105 |
'type' => 'object', |
| 106 |
'additionalProperties' => [ |
| 107 |
'type' => 'string', |
| 108 |
'enum' => Content_Type_Settings::STATES, |
| 109 |
], |
| 110 |
], |
| 111 |
'robots_meta' => [ |
| 112 |
'type' => 'object', |
| 113 |
'additionalProperties' => true, |
| 114 |
], |
| 115 |
'sitemap_include' => [ 'type' => [ 'boolean', 'null' ] ], |
| 116 |
'supports_sitemap' => [ 'type' => 'boolean' ], |
| 117 |
], |
| 118 |
], |
| 119 |
], |
| 120 |
'globals' => [ |
| 121 |
'type' => 'object', |
| 122 |
'additionalProperties' => [ 'type' => 'boolean' ], |
| 123 |
'description' => __( 'The site-wide value each feature\'s "inherit" currently resolves to.', 'thinkrank' ), |
| 124 |
], |
| 125 |
], |
| 126 |
]; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Execute ability. |
| 131 |
* |
| 132 |
* @param array<string, mixed> $input Ability input payload. |
| 133 |
* @return array<string, mixed>|\WP_Error |
| 134 |
*/ |
| 135 |
public function execute( $input ) { |
| 136 |
$response = rest_do_request( new \WP_REST_Request( 'GET', '/thinkrank/v1/global-seo/matrix' ) ); |
| 137 |
|
| 138 |
if ( $response->is_error() ) { |
| 139 |
return $response->as_error(); |
| 140 |
} |
| 141 |
|
| 142 |
$data = $response->get_data(); |
| 143 |
|
| 144 |
return [ |
| 145 |
'entities' => $data['data']['entities'] ?? [], |
| 146 |
'globals' => $data['data']['globals'] ?? [], |
| 147 |
]; |
| 148 |
} |
| 149 |
} |
| 150 |
|