| 1 |
<?php |
| 2 |
/** |
| 3 |
* Update 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\Admin\Metabox_Manager; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Flags or unflags a post as pillar content. |
| 21 |
* |
| 22 |
* Writes the `_thinkrank_pillar_content` post meta via the same metabox writer |
| 23 |
* the editor uses. Pillar posts surface as internal-link suggestions for |
| 24 |
* related posts sharing a taxonomy term. |
| 25 |
*/ |
| 26 |
class Update_Pillar_Content extends Ability_Base { |
| 27 |
/** |
| 28 |
* Constructor. |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
$this->id = 'thinkrank/update-pillar-content'; |
| 32 |
$this->label = __( 'Update Pillar Content', 'thinkrank' ); |
| 33 |
$this->description = __( 'Flag or unflag a post as pillar content. Pillar posts appear as internal-link suggestions for related posts that share a taxonomy term.', 'thinkrank' ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* {@inheritDoc} |
| 38 |
* |
| 39 |
* @return array<string, mixed> |
| 40 |
*/ |
| 41 |
public function get_input_schema() { |
| 42 |
return [ |
| 43 |
'type' => 'object', |
| 44 |
'additionalProperties' => false, |
| 45 |
'properties' => [ |
| 46 |
'post_id' => [ |
| 47 |
'type' => 'integer', |
| 48 |
'description' => __( 'The target post ID.', 'thinkrank' ), |
| 49 |
], |
| 50 |
'is_pillar' => [ |
| 51 |
'type' => 'boolean', |
| 52 |
'description' => __( 'Whether the post should be flagged as pillar content.', 'thinkrank' ), |
| 53 |
], |
| 54 |
], |
| 55 |
'required' => [ 'post_id', 'is_pillar' ], |
| 56 |
]; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* {@inheritDoc} |
| 61 |
* |
| 62 |
* @return array<string, mixed> |
| 63 |
*/ |
| 64 |
public function get_output_schema() { |
| 65 |
return [ |
| 66 |
'type' => 'object', |
| 67 |
'properties' => [ |
| 68 |
'success' => [ 'type' => 'boolean' ], |
| 69 |
'message' => [ 'type' => 'string' ], |
| 70 |
'post_id' => [ 'type' => 'integer' ], |
| 71 |
'is_pillar' => [ 'type' => 'boolean' ], |
| 72 |
], |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Execute ability. |
| 78 |
* |
| 79 |
* @param array<string, mixed> $input Ability input payload. |
| 80 |
* @return array<string, mixed>|\WP_Error |
| 81 |
*/ |
| 82 |
public function execute( $input ) { |
| 83 |
$post_id = isset( $input['post_id'] ) ? absint( $input['post_id'] ) : 0; |
| 84 |
|
| 85 |
if ( ! $post_id || ! get_post( $post_id ) instanceof \WP_Post ) { |
| 86 |
return new \WP_Error( |
| 87 |
'thinkrank_post_not_found', |
| 88 |
__( 'A valid post ID is required.', 'thinkrank' ), |
| 89 |
[ 'status' => 404 ] |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 94 |
return new \WP_Error( |
| 95 |
'thinkrank_cannot_edit_post', |
| 96 |
__( 'You are not allowed to edit this post.', 'thinkrank' ), |
| 97 |
[ 'status' => 403 ] |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
$is_pillar = ! empty( $input['is_pillar'] ); |
| 102 |
|
| 103 |
( new Metabox_Manager() )->save_seo_fields( |
| 104 |
$post_id, |
| 105 |
[ 'thinkrank_pillar_content' => $is_pillar ? '1' : '0' ] |
| 106 |
); |
| 107 |
|
| 108 |
return [ |
| 109 |
'success' => true, |
| 110 |
'message' => $is_pillar |
| 111 |
? __( 'Post flagged as pillar content.', 'thinkrank' ) |
| 112 |
: __( 'Post removed from pillar content.', 'thinkrank' ), |
| 113 |
'post_id' => $post_id, |
| 114 |
'is_pillar' => $is_pillar, |
| 115 |
]; |
| 116 |
} |
| 117 |
} |
| 118 |
|