PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.7.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.7.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.7.0, at includes/abilities/content/class-get-term-seo.php

156 lines 4.4 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 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