PluginProbe
Extendify / 3.1.4
Extendify v3.1.4
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / app / Agent / AbilitiesDiscovery.php

AbilitiesDiscovery.php in Extendify 3.1.4, at app/Agent/AbilitiesDiscovery.php

196 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Discovers WordPress Abilities registered on the site for the Agent.
5 */
6
7 namespace Extendify\Agent;
8
9 use Extendify\PartnerData;
10
11 defined('ABSPATH') || die('No direct access.');
12
13 /**
14 * Reads the Abilities API (WP 6.9+) and shapes the survivors for the Agent,
15 * grouped by category so the backend can route to a category before a tool.
16 */
17 class AbilitiesDiscovery
18 {
19 /**
20 * Discover the abilities registered on this install.
21 *
22 * @return array
23 */
24 public static function discover()
25 {
26 if (!function_exists('wp_get_abilities')) {
27 return [];
28 }
29
30 return self::shape(
31 wp_get_abilities(),
32 wp_get_ability_categories(),
33 PartnerData::setting('agentAbilitiesAllowlist')
34 );
35 }
36
37 /**
38 * Keep only REST-exposed abilities, then group their descriptors under the
39 * categories that hold at least one.
40 *
41 * No permission filtering here: an ability's permission_callback often turns
42 * on the specific input (e.g. edit_post for a given post id) which discovery
43 * doesn't have, so the /run endpoint enforces permission at call time.
44 *
45 * @param array $abilities WP_Ability objects from wp_get_abilities().
46 * @param array $categories WP_Ability_Category objects from wp_get_ability_categories().
47 * @param array|null $allowlist Namespace prefixes to keep; null keeps every namespace.
48 * @param array|null $glossary English label => our translation; null uses the shipped map.
49 * @return array
50 */
51 public static function shape(array $abilities, array $categories = [], $allowlist = null, $glossary = null)
52 {
53 $glossary = $glossary ?? self::labelGlossary();
54 $allowed = array_filter($abilities, function ($ability) use ($allowlist) {
55 if (!$ability->get_meta_item('show_in_rest')) {
56 return false;
57 }
58
59 if ($allowlist === null) {
60 return true;
61 }
62
63 return in_array(explode('/', $ability->get_name())[0], $allowlist, true);
64 });
65
66 $byCategory = [];
67 foreach ($allowed as $ability) {
68 $meta = $ability->get_meta();
69 $name = $ability->get_name();
70 $label = $ability->get_label();
71 $byCategory[$ability->get_category()][] = [
72 'name' => $name,
73 'label' => $glossary[$label] ?? $label,
74 'description' => $ability->get_description(),
75 'inputSchema' => $ability->get_input_schema(),
76 'annotations' => $meta['annotations'] ?? null,
77 'runHref' => \rest_url('wp-abilities/v1/abilities/' . $name . '/run'),
78 ];
79 }
80
81 $categoryMeta = [];
82 foreach ($categories as $category) {
83 $categoryMeta[$category->get_slug()] = [
84 'label' => $category->get_label(),
85 'description' => $category->get_description(),
86 ];
87 }
88
89 $result = [];
90 foreach ($byCategory as $slug => $abilitiesInCategory) {
91 $result[] = [
92 'slug' => $slug,
93 'label' => $categoryMeta[$slug]['label'] ?? $slug,
94 'description' => $categoryMeta[$slug]['description'] ?? '',
95 'abilities' => $abilitiesInCategory,
96 ];
97 }
98
99 return $result;
100 }
101
102 /**
103 * Our own translations for third-party ability labels, keyed by the exact
104 * English string the plugin registers. A polyfill: once the plugin ships its
105 * own translation, get_label() returns the localized string, which no longer
106 * matches the English key, so upstream wins and this map goes dormant.
107 *
108 * @return array
109 */
110 private static function labelGlossary()
111 {
112 return [
113 // translators: WooCommerce AI Agent ability — searches the store's products.
114 'Query products' => __('Query products', 'extendify-local'),
115 // translators: WooCommerce AI Agent ability — searches the store's orders.
116 'Query orders' => __('Query orders', 'extendify-local'),
117 // translators: WooCommerce AI Agent ability — creates a product.
118 'Create product' => __('Create product', 'extendify-local'),
119 // translators: WooCommerce AI Agent ability — updates a product.
120 'Update product' => __('Update product', 'extendify-local'),
121 // translators: WooCommerce AI Agent ability — deletes a product.
122 'Delete product' => __('Delete product', 'extendify-local'),
123 // translators: WooCommerce AI Agent ability — adds a note to an order.
124 'Add order note' => __('Add order note', 'extendify-local'),
125 // translators: WooCommerce AI Agent ability — changes an order's status.
126 'Update order status' => __('Update order status', 'extendify-local'),
127 // translators: TranslatePress AI Agent ability — adds a translation language.
128 'Add a translation language' => __('Add a translation language', 'extendify-local'),
129 // translators: TranslatePress AI Agent ability — sets the license key.
130 'Set TranslatePress license key' => __('Set TranslatePress license key', 'extendify-local'),
131 // translators: TranslatePress AI Agent ability — turns on automatic translation.
132 'Enable Automatic Translation' => __('Enable Automatic Translation', 'extendify-local'),
133 // translators: TranslatePress AI Agent ability — lists the site's configured languages.
134 'List configured languages' => __('List configured languages', 'extendify-local'),
135 // translators: TranslatePress AI Agent ability — lists selectable language codes.
136 'List available language codes' => __('List available language codes', 'extendify-local'),
137 // translators: TranslatePress AI Agent ability — removes a translation language.
138 'Remove a translation language' => __('Remove a translation language', 'extendify-local'),
139 // translators: TranslatePress AI Agent ability — updates a translation language.
140 'Update a translation language' => __('Update a translation language', 'extendify-local'),
141 // translators: TranslatePress AI Agent ability — sets the site's default language.
142 'Set the default language' => __('Set the default language', 'extendify-local'),
143 // translators: WPForms AI Agent ability — lists the site's forms.
144 'List Forms' => __('List Forms', 'extendify-local'),
145 // translators: WPForms AI Agent ability — fetches a form's details.
146 'Get Form' => __('Get Form', 'extendify-local'),
147 // translators: WPForms AI Agent ability — describes which form fields and settings can be edited.
148 'Describe Editing Schema' => __('Describe Editing Schema', 'extendify-local'),
149 // translators: WPForms AI Agent ability — creates a form.
150 'Create Form' => __('Create Form', 'extendify-local'),
151 // translators: WPForms AI Agent ability — updates a form's settings.
152 'Update Form Settings' => __('Update Form Settings', 'extendify-local'),
153 // translators: WPForms AI Agent ability — adds a field to a form.
154 'Add Field' => __('Add Field', 'extendify-local'),
155 // translators: WPForms AI Agent ability — updates a form field.
156 'Update Field' => __('Update Field', 'extendify-local'),
157 // translators: WPForms AI Agent ability — gets a form's entry statistics.
158 'Get Form Stats' => __('Get Form Stats', 'extendify-local'),
159 // translators: SimplyBook AI Agent ability — gets booking statistics.
160 'Get Statistics' => __('Get Statistics', 'extendify-local'),
161 // translators: SimplyBook AI Agent ability — counts bookings from the last 30 days.
162 'Count Recent Bookings' => __('Count Recent Bookings', 'extendify-local'),
163 // translators: SimplyBook AI Agent ability — updates the booking widget's design settings.
164 'Update Design Settings' => __('Update Design Settings', 'extendify-local'),
165 // translators: SimplyBook AI Agent ability — lists the booking widget's design settings.
166 'List Design Settings' => __('List Design Settings', 'extendify-local'),
167 // translators: SimplyBook AI Agent ability — lists the plugin's setup tasks.
168 'List Tasks' => __('List Tasks', 'extendify-local'),
169 // translators: SimplyBook AI Agent ability — updates a setup task's status.
170 'Update Task Status' => __('Update Task Status', 'extendify-local'),
171 // translators: SimplyBook AI Agent ability — lists service providers (staff).
172 'List Providers' => __('List Providers', 'extendify-local'),
173 // translators: SimplyBook AI Agent ability — updates a service provider (staff member).
174 'Update Service Provider' => __('Update Service Provider', 'extendify-local'),
175 // translators: SimplyBook AI Agent ability — updates a bookable service.
176 'Update Service' => __('Update Service', 'extendify-local'),
177 // translators: SimplyBook AI Agent ability — lists the bookable services.
178 'List Services' => __('List Services', 'extendify-local'),
179 // translators: Imagify AI Agent ability — optimizes an image in the media library.
180 'Optimize media' => __('Optimize media', 'extendify-local'),
181 // translators: Imagify AI Agent ability — gets an image's optimization status.
182 'Get Media Status' => __('Get Media Status', 'extendify-local'),
183 // translators: Imagify AI Agent ability — gets the site's image optimization statistics.
184 'Get Imagify optimization stats' => __('Get Imagify optimization stats', 'extendify-local'),
185 // translators: Imagify AI Agent ability — reports how many images have next-gen formats (WebP/AVIF).
186 'Get next-gen coverage' => __('Get next-gen coverage', 'extendify-local'),
187 // translators: Imagify AI Agent ability — gets the plugin's settings.
188 'Get Imagify settings' => __('Get Imagify settings', 'extendify-local'),
189 // translators: Imagify AI Agent ability — updates the plugin's settings.
190 'Update Imagify settings' => __('Update Imagify settings', 'extendify-local'),
191 // translators: Imagify AI Agent ability — gets the Imagify account status and quota.
192 'Get Imagify account status' => __('Get Imagify account status', 'extendify-local'),
193 ];
194 }
195 }
196