| 1 |
<?php |
| 2 |
/** |
| 3 |
* Get term SEO 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\SEO\Object_Redirect; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Retrieves ThinkRank SEO data for a taxonomy term. |
| 21 |
*/ |
| 22 |
class Get_Term_Seo extends Ability_Base { |
| 23 |
/** |
| 24 |
* Constructor. |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
$this->id = 'thinkrank/get-term-seo'; |
| 28 |
$this->label = __( 'Get ThinkRank Term SEO', 'thinkrank' ); |
| 29 |
$this->description = __( 'Retrieve ThinkRank SEO metadata for a taxonomy term. Use update-term-seo to change them, and get-term-seo-checks to see what would improve.', 'thinkrank' ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* {@inheritDoc} |
| 34 |
* |
| 35 |
* @return array<string, bool|float|string> |
| 36 |
*/ |
| 37 |
public function get_annotations() { |
| 38 |
return [ |
| 39 |
'readonly' => true, |
| 40 |
'destructive' => false, |
| 41 |
'idempotent' => true, |
| 42 |
'priority' => 1.0, |
| 43 |
'openWorldHint' => false, |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* {@inheritDoc} |
| 49 |
* |
| 50 |
* @return array<string, mixed> |
| 51 |
*/ |
| 52 |
public function get_input_schema() { |
| 53 |
return [ |
| 54 |
'type' => 'object', |
| 55 |
'additionalProperties' => false, |
| 56 |
'properties' => [ |
| 57 |
'term_id' => [ |
| 58 |
'type' => 'integer', |
| 59 |
'description' => __( 'The target term ID.', 'thinkrank' ), |
| 60 |
], |
| 61 |
], |
| 62 |
'required' => [ 'term_id' ], |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* {@inheritDoc} |
| 68 |
* |
| 69 |
* @return array<string, mixed> |
| 70 |
*/ |
| 71 |
public function get_output_schema() { |
| 72 |
return [ |
| 73 |
'type' => 'object', |
| 74 |
'properties' => [ |
| 75 |
'term_id' => [ 'type' => 'integer' ], |
| 76 |
'taxonomy' => [ 'type' => 'string' ], |
| 77 |
'data' => [ 'type' => 'object' ], |
| 78 |
], |
| 79 |
]; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Execute ability. |
| 84 |
* |
| 85 |
* @param array<string, mixed> $input Ability input payload. |
| 86 |
* @return array<string, mixed>|\WP_Error |
| 87 |
*/ |
| 88 |
public function execute( $input ) { |
| 89 |
$term_id = isset( $input['term_id'] ) ? absint( $input['term_id'] ) : 0; |
| 90 |
|
| 91 |
if ( ! $term_id ) { |
| 92 |
return new \WP_Error( |
| 93 |
'thinkrank_invalid_term_target', |
| 94 |
__( 'A valid term ID is required.', 'thinkrank' ), |
| 95 |
[ 'status' => 400 ] |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
$term = get_term( $term_id ); |
| 100 |
if ( ! $term instanceof \WP_Term ) { |
| 101 |
return new \WP_Error( |
| 102 |
'thinkrank_term_not_found', |
| 103 |
__( 'The provided term ID does not exist.', 'thinkrank' ), |
| 104 |
[ 'status' => 404 ] |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
$redirect = Object_Redirect::get( 'term', $term_id ); |
| 109 |
|
| 110 |
$data = [ |
| 111 |
'title' => (string) get_term_meta( $term_id, '_thinkrank_seo_title', true ), |
| 112 |
'description' => (string) get_term_meta( $term_id, '_thinkrank_meta_description', true ), |
| 113 |
'canonical_url' => (string) get_term_meta( $term_id, '_thinkrank_canonical_url', true ), |
| 114 |
// Not term meta: the rule in Pro's redirections table is the value. |
| 115 |
'redirect_url' => $redirect['url'], |
| 116 |
'redirect_type' => $redirect['type'], |
| 117 |
'focus_keyword' => (string) get_term_meta( $term_id, '_thinkrank_focus_keyword', true ), |
| 118 |
'robots_meta_enabled' => get_term_meta( $term_id, '_thinkrank_robots_meta_enabled', true ), |
| 119 |
'robots_meta' => $this->decode_json_field( get_term_meta( $term_id, '_thinkrank_robots_meta', true ) ), |
| 120 |
'advanced_robots_meta' => $this->decode_json_field( get_term_meta( $term_id, '_thinkrank_advanced_robots_meta', true ) ), |
| 121 |
'og_title' => (string) get_term_meta( $term_id, '_thinkrank_og_title', true ), |
| 122 |
'og_description' => (string) get_term_meta( $term_id, '_thinkrank_og_description', true ), |
| 123 |
'og_image' => (string) get_term_meta( $term_id, '_thinkrank_og_image', true ), |
| 124 |
'twitter_title' => (string) get_term_meta( $term_id, '_thinkrank_twitter_title', true ), |
| 125 |
'twitter_description' => (string) get_term_meta( $term_id, '_thinkrank_twitter_description', true ), |
| 126 |
'twitter_image' => (string) get_term_meta( $term_id, '_thinkrank_twitter_image', true ), |
| 127 |
]; |
| 128 |
|
| 129 |
return [ |
| 130 |
'term_id' => $term_id, |
| 131 |
'taxonomy' => (string) $term->taxonomy, |
| 132 |
'data' => $data, |
| 133 |
]; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Decode a JSON-encoded meta field into an array. |
| 138 |
* |
| 139 |
* @param mixed $value Raw stored value. |
| 140 |
* @return array<string, mixed> |
| 141 |
*/ |
| 142 |
private function decode_json_field( $value ) { |
| 143 |
if ( is_array( $value ) ) { |
| 144 |
return $value; |
| 145 |
} |
| 146 |
|
| 147 |
if ( ! is_string( $value ) || '' === $value ) { |
| 148 |
return []; |
| 149 |
} |
| 150 |
|
| 151 |
$decoded = json_decode( $value, true ); |
| 152 |
|
| 153 |
return is_array( $decoded ) ? $decoded : []; |
| 154 |
} |
| 155 |
} |
| 156 |
|