| 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 |
$schema = $ability->get_input_schema(); |
| 72 |
// Filters out unnecessary context |
| 73 |
if (function_exists('wp_prepare_json_schema_for_client')) { |
| 74 |
$schema = wp_prepare_json_schema_for_client($schema); |
| 75 |
} |
| 76 |
$byCategory[$ability->get_category()][] = [ |
| 77 |
'name' => $name, |
| 78 |
'label' => $glossary[$label] ?? $label, |
| 79 |
'description' => $ability->get_description(), |
| 80 |
'inputSchema' => $schema, |
| 81 |
'annotations' => $meta['annotations'] ?? null, |
| 82 |
'runHref' => \rest_url('wp-abilities/v1/abilities/' . $name . '/run'), |
| 83 |
]; |
| 84 |
} |
| 85 |
|
| 86 |
$categoryMeta = []; |
| 87 |
foreach ($categories as $category) { |
| 88 |
$categoryMeta[$category->get_slug()] = [ |
| 89 |
'label' => $category->get_label(), |
| 90 |
'description' => $category->get_description(), |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
$result = []; |
| 95 |
foreach ($byCategory as $slug => $abilitiesInCategory) { |
| 96 |
$result[] = [ |
| 97 |
'slug' => $slug, |
| 98 |
'label' => $categoryMeta[$slug]['label'] ?? $slug, |
| 99 |
'description' => $categoryMeta[$slug]['description'] ?? '', |
| 100 |
'abilities' => $abilitiesInCategory, |
| 101 |
]; |
| 102 |
} |
| 103 |
|
| 104 |
return $result; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Our own translations for third-party ability labels, keyed by the exact |
| 109 |
* English string the plugin registers. A polyfill: once the plugin ships its |
| 110 |
* own translation, get_label() returns the localized string, which no longer |
| 111 |
* matches the English key, so upstream wins and this map goes dormant. |
| 112 |
* |
| 113 |
* @return array |
| 114 |
*/ |
| 115 |
private static function labelGlossary() |
| 116 |
{ |
| 117 |
return [ |
| 118 |
// translators: WooCommerce AI Agent ability — searches the store's products. |
| 119 |
'Query products' => __('Query products', 'extendify-local'), |
| 120 |
// translators: WooCommerce AI Agent ability — searches the store's orders. |
| 121 |
'Query orders' => __('Query orders', 'extendify-local'), |
| 122 |
// translators: WooCommerce AI Agent ability — creates a product. |
| 123 |
'Create product' => __('Create product', 'extendify-local'), |
| 124 |
// translators: WooCommerce AI Agent ability — updates a product. |
| 125 |
'Update product' => __('Update product', 'extendify-local'), |
| 126 |
// translators: WooCommerce AI Agent ability — deletes a product. |
| 127 |
'Delete product' => __('Delete product', 'extendify-local'), |
| 128 |
// translators: WooCommerce AI Agent ability — adds a note to an order. |
| 129 |
'Add order note' => __('Add order note', 'extendify-local'), |
| 130 |
// translators: WooCommerce AI Agent ability — changes an order's status. |
| 131 |
'Update order status' => __('Update order status', 'extendify-local'), |
| 132 |
// translators: TranslatePress AI Agent ability — adds a translation language. |
| 133 |
'Add a translation language' => __('Add a translation language', 'extendify-local'), |
| 134 |
// translators: TranslatePress AI Agent ability — sets the license key. |
| 135 |
'Set TranslatePress license key' => __('Set TranslatePress license key', 'extendify-local'), |
| 136 |
// translators: TranslatePress AI Agent ability — turns on automatic translation. |
| 137 |
'Enable Automatic Translation' => __('Enable Automatic Translation', 'extendify-local'), |
| 138 |
// translators: TranslatePress AI Agent ability — lists the site's configured languages. |
| 139 |
'List configured languages' => __('List configured languages', 'extendify-local'), |
| 140 |
// translators: TranslatePress AI Agent ability — lists selectable language codes. |
| 141 |
'List available language codes' => __('List available language codes', 'extendify-local'), |
| 142 |
// translators: TranslatePress AI Agent ability — removes a translation language. |
| 143 |
'Remove a translation language' => __('Remove a translation language', 'extendify-local'), |
| 144 |
// translators: TranslatePress AI Agent ability — updates a translation language. |
| 145 |
'Update a translation language' => __('Update a translation language', 'extendify-local'), |
| 146 |
// translators: TranslatePress AI Agent ability — sets the site's default language. |
| 147 |
'Set the default language' => __('Set the default language', 'extendify-local'), |
| 148 |
// translators: WPForms AI Agent ability — lists the site's forms. |
| 149 |
'List Forms' => __('List Forms', 'extendify-local'), |
| 150 |
// translators: WPForms AI Agent ability — fetches a form's details. |
| 151 |
'Get Form' => __('Get Form', 'extendify-local'), |
| 152 |
// translators: WPForms AI Agent ability — describes which form fields and settings can be edited. |
| 153 |
'Describe Editing Schema' => __('Describe Editing Schema', 'extendify-local'), |
| 154 |
// translators: WPForms AI Agent ability — creates a form. |
| 155 |
'Create Form' => __('Create Form', 'extendify-local'), |
| 156 |
// translators: WPForms AI Agent ability — updates a form's settings. |
| 157 |
'Update Form Settings' => __('Update Form Settings', 'extendify-local'), |
| 158 |
// translators: WPForms AI Agent ability — adds a field to a form. |
| 159 |
'Add Field' => __('Add Field', 'extendify-local'), |
| 160 |
// translators: WPForms AI Agent ability — updates a form field. |
| 161 |
'Update Field' => __('Update Field', 'extendify-local'), |
| 162 |
// translators: WPForms AI Agent ability — gets a form's entry statistics. |
| 163 |
'Get Form Stats' => __('Get Form Stats', 'extendify-local'), |
| 164 |
// translators: SimplyBook AI Agent ability — gets booking statistics. |
| 165 |
'Get Statistics' => __('Get Statistics', 'extendify-local'), |
| 166 |
// translators: SimplyBook AI Agent ability — counts bookings from the last 30 days. |
| 167 |
'Count Recent Bookings' => __('Count Recent Bookings', 'extendify-local'), |
| 168 |
// translators: SimplyBook AI Agent ability — updates the booking widget's design settings. |
| 169 |
'Update Design Settings' => __('Update Design Settings', 'extendify-local'), |
| 170 |
// translators: SimplyBook AI Agent ability — lists the booking widget's design settings. |
| 171 |
'List Design Settings' => __('List Design Settings', 'extendify-local'), |
| 172 |
// translators: SimplyBook AI Agent ability — lists the plugin's setup tasks. |
| 173 |
'List Tasks' => __('List Tasks', 'extendify-local'), |
| 174 |
// translators: SimplyBook AI Agent ability — updates a setup task's status. |
| 175 |
'Update Task Status' => __('Update Task Status', 'extendify-local'), |
| 176 |
// translators: SimplyBook AI Agent ability — lists service providers (staff). |
| 177 |
'List Providers' => __('List Providers', 'extendify-local'), |
| 178 |
// translators: SimplyBook AI Agent ability — updates a service provider (staff member). |
| 179 |
'Update Service Provider' => __('Update Service Provider', 'extendify-local'), |
| 180 |
// translators: SimplyBook AI Agent ability — updates a bookable service. |
| 181 |
'Update Service' => __('Update Service', 'extendify-local'), |
| 182 |
// translators: SimplyBook AI Agent ability — lists the bookable services. |
| 183 |
'List Services' => __('List Services', 'extendify-local'), |
| 184 |
// translators: Imagify AI Agent ability — optimizes an image in the media library. |
| 185 |
'Optimize media' => __('Optimize media', 'extendify-local'), |
| 186 |
// translators: Imagify AI Agent ability — gets an image's optimization status. |
| 187 |
'Get Media Status' => __('Get Media Status', 'extendify-local'), |
| 188 |
// translators: Imagify AI Agent ability — gets the site's image optimization statistics. |
| 189 |
'Get Imagify optimization stats' => __('Get Imagify optimization stats', 'extendify-local'), |
| 190 |
// translators: Imagify AI Agent ability — reports how many images have next-gen formats (WebP/AVIF). |
| 191 |
'Get next-gen coverage' => __('Get next-gen coverage', 'extendify-local'), |
| 192 |
// translators: Imagify AI Agent ability — gets the plugin's settings. |
| 193 |
'Get Imagify settings' => __('Get Imagify settings', 'extendify-local'), |
| 194 |
// translators: Imagify AI Agent ability — updates the plugin's settings. |
| 195 |
'Update Imagify settings' => __('Update Imagify settings', 'extendify-local'), |
| 196 |
// translators: Imagify AI Agent ability — gets the Imagify account status and quota. |
| 197 |
'Get Imagify account status' => __('Get Imagify account status', 'extendify-local'), |
| 198 |
]; |
| 199 |
} |
| 200 |
} |
| 201 |
|