| 1 |
<?php |
| 2 |
/** |
| 3 |
* Get pillar content ability. |
| 4 |
* |
| 5 |
* @package ThinkRank\Abilities\Content |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace ThinkRank\Abilities\Content; |
| 11 |
|
| 12 |
use ThinkRank\Abilities\Ability_Base; |
| 13 |
use ThinkRank\API\Pillar_Content_Endpoint; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Reads pillar-content state and internal-link suggestions for a post. |
| 21 |
* |
| 22 |
* Reports whether the post is flagged as pillar content |
| 23 |
* (`_thinkrank_pillar_content`) and returns the same internal-link suggestions |
| 24 |
* as `GET /thinkrank/v1/pillar-content/suggestions` — related posts flagged as |
| 25 |
* pillar content that share at least one taxonomy term. |
| 26 |
*/ |
| 27 |
class Get_Pillar_Content extends Ability_Base { |
| 28 |
/** |
| 29 |
* Constructor. |
| 30 |
*/ |
| 31 |
public function __construct() { |
| 32 |
$this->id = 'thinkrank/get-pillar-content'; |
| 33 |
$this->label = __( 'Get Pillar Content', 'thinkrank' ); |
| 34 |
$this->description = __( 'Read whether a post is flagged as pillar content and get its internal-link suggestions (related pillar posts sharing a taxonomy term). Use update-pillar-content to flag or unflag a post.', 'thinkrank' ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* {@inheritDoc} |
| 39 |
* |
| 40 |
* @return array<string, bool|float|string> |
| 41 |
*/ |
| 42 |
public function get_annotations() { |
| 43 |
return [ |
| 44 |
'readonly' => true, |
| 45 |
'destructive' => false, |
| 46 |
'idempotent' => true, |
| 47 |
'priority' => 1.0, |
| 48 |
'openWorldHint' => false, |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* {@inheritDoc} |
| 54 |
* |
| 55 |
* @return array<string, mixed> |
| 56 |
*/ |
| 57 |
public function get_input_schema() { |
| 58 |
return [ |
| 59 |
'type' => 'object', |
| 60 |
'additionalProperties' => false, |
| 61 |
'properties' => [ |
| 62 |
'post_id' => [ |
| 63 |
'type' => 'integer', |
| 64 |
'description' => __( 'The post to inspect.', 'thinkrank' ), |
| 65 |
], |
| 66 |
], |
| 67 |
'required' => [ 'post_id' ], |
| 68 |
]; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* {@inheritDoc} |
| 73 |
* |
| 74 |
* @return array<string, mixed> |
| 75 |
*/ |
| 76 |
public function get_output_schema() { |
| 77 |
return [ |
| 78 |
'type' => 'object', |
| 79 |
'properties' => [ |
| 80 |
'post_id' => [ 'type' => 'integer' ], |
| 81 |
'is_pillar' => [ 'type' => 'boolean' ], |
| 82 |
'suggestions' => [ |
| 83 |
'type' => 'array', |
| 84 |
'items' => [ |
| 85 |
'type' => 'object', |
| 86 |
'additionalProperties' => true, |
| 87 |
], |
| 88 |
], |
| 89 |
], |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Execute ability. |
| 95 |
* |
| 96 |
* @param array<string, mixed> $input Ability input payload. |
| 97 |
* @return array<string, mixed>|\WP_Error |
| 98 |
*/ |
| 99 |
public function execute( $input ) { |
| 100 |
$post_id = isset( $input['post_id'] ) ? absint( $input['post_id'] ) : 0; |
| 101 |
|
| 102 |
if ( ! $post_id || ! get_post( $post_id ) instanceof \WP_Post ) { |
| 103 |
return new \WP_Error( |
| 104 |
'thinkrank_invalid_post_id', |
| 105 |
__( 'A valid post ID is required.', 'thinkrank' ), |
| 106 |
[ 'status' => 400 ] |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
$is_pillar = '1' === (string) get_post_meta( $post_id, '_thinkrank_pillar_content', true ); |
| 111 |
|
| 112 |
// Reuse the endpoint's suggestion query so output matches the UI exactly. |
| 113 |
$request = new \WP_REST_Request( 'GET' ); |
| 114 |
$request->set_param( 'post_id', $post_id ); |
| 115 |
|
| 116 |
$response = ( new Pillar_Content_Endpoint() )->get_suggestions( $request ); |
| 117 |
$suggestions = ( $response instanceof \WP_REST_Response ) ? (array) $response->get_data() : []; |
| 118 |
|
| 119 |
return [ |
| 120 |
'post_id' => $post_id, |
| 121 |
'is_pillar' => $is_pillar, |
| 122 |
'suggestions' => array_values( $suggestions ), |
| 123 |
]; |
| 124 |
} |
| 125 |
} |
| 126 |
|