PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.4.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.4.0
2.7.0 2.6.0 2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.10.0 All 48 releases
thinkrank / includes / abilities / content / class-get-term-seo.php

class-get-term-seo.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 2.4.0, at includes/abilities/content/class-get-term-seo.php

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