PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 1.27.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v1.27.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 / class-ability-base.php

class-ability-base.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 1.27.0, at includes/abilities/class-ability-base.php

197 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Ability base class.
4 *
5 * @package ThinkRank\Abilities
6 */
7
8 declare(strict_types=1);
9
10 namespace ThinkRank\Abilities;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly.
14 }
15
16 /**
17 * Base ability implementation for ThinkRank.
18 *
19 * Each ThinkRank MCP ability extends this class, providing an input/output JSON
20 * schema and an execute() body. Abilities are registered with the WordPress
21 * Abilities API and exposed to AI clients through the MCP server.
22 */
23 abstract class Ability_Base {
24 /**
25 * Minimum capability allowed for ThinkRank abilities.
26 */
27 private const MIN_CAPABILITY = 'manage_options';
28
29 /**
30 * Unique ability identifier.
31 *
32 * @var string
33 */
34 protected $id = '';
35
36 /**
37 * Human-readable label.
38 *
39 * @var string
40 */
41 protected $label = '';
42
43 /**
44 * Ability description.
45 *
46 * @var string
47 */
48 protected $description = '';
49
50 /**
51 * Ability category.
52 *
53 * @var string
54 */
55 protected $category = 'thinkrank';
56
57 /**
58 * Required WordPress capability.
59 *
60 * @var string
61 */
62 protected $capability = 'manage_options';
63
64 /**
65 * Get the JSON Schema for ability input.
66 *
67 * @return array<string, mixed>
68 */
69 abstract public function get_input_schema();
70
71 /**
72 * Get the JSON Schema for ability output.
73 *
74 * @return array<string, mixed>
75 */
76 abstract public function get_output_schema();
77
78 /**
79 * Execute the ability.
80 *
81 * @param array<string, mixed> $input Validated input.
82 * @return array<string, mixed>|\WP_Error
83 */
84 abstract public function execute( $input );
85
86 /**
87 * Check whether abilities are enabled.
88 *
89 * @return bool
90 */
91 public static function abilities_enabled() {
92 return (bool) apply_filters( 'thinkrank_abilities_api_enabled', true );
93 }
94
95 /**
96 * Check whether this ability can be registered and executed.
97 *
98 * @return bool
99 */
100 public function is_enabled() {
101 return (bool) apply_filters( 'thinkrank_ability_enabled', self::abilities_enabled(), $this->id, $this );
102 }
103
104 /**
105 * Permission callback for the abilities API.
106 *
107 * @return bool
108 */
109 public function permission_callback() {
110 if ( ! $this->is_enabled() ) {
111 return false;
112 }
113
114 return current_user_can( $this->capability );
115 }
116
117 /**
118 * Enforce ThinkRank's current admin capability policy.
119 *
120 * @return bool
121 */
122 public function meets_capability_policy() {
123 return self::MIN_CAPABILITY === $this->capability;
124 }
125
126 /**
127 * MCP-compatible annotations for this ability.
128 *
129 * @return array<string, bool|float|string>
130 */
131 public function get_annotations() {
132 return [
133 'readonly' => false,
134 'destructive' => false,
135 'idempotent' => false,
136 'priority' => 2.0,
137 'openWorldHint' => false,
138 ];
139 }
140
141 /**
142 * Wrapper around execute() with action hooks.
143 *
144 * @param array<string, mixed> $input Validated input.
145 * @return array<string, mixed>|\WP_Error
146 */
147 public function execute_wrapper( $input ) {
148 do_action( 'thinkrank_before_ability_execute', $this->id, $input );
149
150 $output = $this->execute( $input );
151
152 do_action( 'thinkrank_after_ability_execute', $this->id, $input, $output );
153
154 return $output;
155 }
156
157 /**
158 * Register the ability with the WordPress Abilities API.
159 *
160 * @return void
161 */
162 public function register() {
163 if ( ! function_exists( 'wp_register_ability' ) ) {
164 return;
165 }
166
167 wp_register_ability(
168 $this->id,
169 [
170 'label' => $this->label,
171 'description' => $this->description,
172 'category' => $this->category,
173 'input_schema' => $this->get_input_schema(),
174 'output_schema' => $this->get_output_schema(),
175 'permission_callback' => [ $this, 'permission_callback' ],
176 'execute_callback' => [ $this, 'execute_wrapper' ],
177 'meta' => [
178 'show_in_rest' => true,
179 'annotations' => $this->get_annotations(),
180 'mcp' => [
181 'public' => false,
182 ],
183 ],
184 ]
185 );
186 }
187
188 /**
189 * Get the ability ID.
190 *
191 * @return string
192 */
193 public function get_id() {
194 return $this->id;
195 }
196 }
197