Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.62% covered (warning)
80.62%
208 / 258
55.56% covered (warning)
55.56%
5 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Modules_Abilities
80.62% covered (warning)
80.62%
208 / 258
55.56% covered (warning)
55.56%
5 / 9
95.75
0.00% covered (danger)
0.00%
0 / 1
 get_category_slug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_category_definition
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 get_abilities
100.00% covered (success)
100.00%
113 / 113
100.00% covered (success)
100.00%
1 / 1
1
 can_view_modules
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 can_manage_modules
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 summarize_module
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
13.03
 get_modules
76.92% covered (warning)
76.92%
30 / 39
0.00% covered (danger)
0.00%
0 / 1
29.50
 set_module_status
63.49% covered (warning)
63.49%
40 / 63
0.00% covered (danger)
0.00%
0 / 1
23.54
 find_conflicting_active_plugin
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2/**
3 * Jetpack Modules Abilities Registration
4 *
5 * Registers Jetpack module management abilities with the WordPress Abilities API.
6 *
7 * @package automattic/jetpack
8 */
9
10// @phan-file-suppress PhanUndeclaredFunction, PhanUndeclaredClassMethod @phan-suppress-current-line UnusedSuppression -- Abilities API added in WP 6.9; suppressions needed for older-WP compatibility runs.
11
12namespace Automattic\Jetpack\Plugin\Abilities;
13
14use Automattic\Jetpack\WP_Abilities\Registrar;
15use Jetpack;
16
17/**
18 * Registers Jetpack module management abilities with the WordPress Abilities API.
19 *
20 * Exposes a filtered read (`get-modules`) and a declarative state-setter
21 * (`set-module-status`) so AI agents can discover and toggle Jetpack modules
22 * through the standard `wp-abilities/v1` REST surface.
23 */
24class Modules_Abilities extends Registrar {
25
26    /**
27     * {@inheritDoc}
28     */
29    public static function get_category_slug(): string {
30        return 'jetpack';
31    }
32
33    /**
34     * {@inheritDoc}
35     */
36    public static function get_category_definition(): array {
37        return array(
38            // "Jetpack" is a product name and should not be translated.
39            'label'       => 'Jetpack',
40            'description' => __( 'Abilities exposed by the Jetpack plugin.', 'jetpack' ),
41        );
42    }
43
44    /**
45     * {@inheritDoc}
46     */
47    public static function get_abilities(): array {
48        $module_schema = array(
49            'type'       => 'object',
50            'properties' => array(
51                'slug'                     => array( 'type' => 'string' ),
52                'name'                     => array( 'type' => 'string' ),
53                'description'              => array( 'type' => 'string' ),
54                'active'                   => array( 'type' => 'boolean' ),
55                'sort'                     => array( 'type' => 'integer' ),
56                'feature'                  => array(
57                    'type'  => 'array',
58                    'items' => array( 'type' => 'string' ),
59                ),
60                'plan_classes'             => array(
61                    'type'  => 'array',
62                    'items' => array( 'type' => 'string' ),
63                ),
64                'requires_connection'      => array( 'type' => 'boolean' ),
65                'requires_user_connection' => array( 'type' => 'boolean' ),
66                'auto_activate'            => array( 'type' => 'string' ),
67            ),
68        );
69
70        return array(
71            'jetpack/get-modules'       => array(
72                'label'               => __( 'Get Jetpack modules', 'jetpack' ),
73                'description'         => __( 'Return zero or more Jetpack modules as an array. Each element has { slug, name, description, active, sort, feature, plan_classes, requires_connection, requires_user_connection, auto_activate }. Combine slug / active / feature / search filters to narrow the list. When slug is provided and unknown, the result is an empty array (not an error). Use this before calling jetpack/set-module-status to enumerate legal slugs.', 'jetpack' ),
74                'input_schema'        => array(
75                    'type'                 => 'object',
76                    'default'              => array(),
77                    'properties'           => array(
78                        'slug'    => array(
79                            'type'        => 'string',
80                            'description' => __( 'Return a single module by slug. Unknown slugs yield an empty array.', 'jetpack' ),
81                            'minLength'   => 1,
82                        ),
83                        'active'  => array(
84                            'type'        => 'boolean',
85                            'description' => __( 'When set, only return modules whose current active state matches this value.', 'jetpack' ),
86                        ),
87                        'feature' => array(
88                            'type'        => 'string',
89                            'description' => __( 'Case-insensitive match against a module\'s feature tag (e.g. "Recommended", "Security", "Performance").', 'jetpack' ),
90                            'minLength'   => 1,
91                        ),
92                        'search'  => array(
93                            'type'        => 'string',
94                            'description' => __( 'Case-insensitive substring match against the module name, slug, and description.', 'jetpack' ),
95                            'minLength'   => 1,
96                        ),
97                    ),
98                    'additionalProperties' => false,
99                ),
100                'output_schema'       => array(
101                    'type'  => 'array',
102                    'items' => $module_schema,
103                ),
104                'execute_callback'    => array( __CLASS__, 'get_modules' ),
105                'permission_callback' => array( __CLASS__, 'can_view_modules' ),
106                'meta'                => array(
107                    'annotations'  => array(
108                        'readonly'    => true,
109                        'destructive' => false,
110                        'idempotent'  => true,
111                    ),
112                    'show_in_rest' => true,
113                    'mcp'          => array(
114                        'public' => true,
115                        'type'   => 'tool', // default is already "tool", but can be explicit.
116                    ),
117                ),
118            ),
119
120            'jetpack/set-module-status' => array(
121                'label'               => __( 'Set Jetpack module status', 'jetpack' ),
122                'description'         => __( 'Set a Jetpack module\'s active state. Idempotent — setting a module to its current state returns changed=false. Returns { slug, active, changed }. Call jetpack/get-modules first to enumerate valid slugs. Modules requiring a Jetpack connection or a paid plan may fail with jetpack_modules_activate_failed; the message indicates the next step.', 'jetpack' ),
123                'input_schema'        => array(
124                    'type'                 => 'object',
125                    'required'             => array( 'slug', 'active' ),
126                    'properties'           => array(
127                        'slug'   => array(
128                            'type'        => 'string',
129                            'description' => __( 'The Jetpack module slug (e.g. "stats", "sso", "sharedaddy").', 'jetpack' ),
130                            'minLength'   => 1,
131                        ),
132                        'active' => array(
133                            'type'        => 'boolean',
134                            'description' => __( 'Desired active state. true activates the module; false deactivates it.', 'jetpack' ),
135                        ),
136                    ),
137                    'additionalProperties' => false,
138                ),
139                'output_schema'       => array(
140                    'type'       => 'object',
141                    'properties' => array(
142                        'slug'    => array( 'type' => 'string' ),
143                        'active'  => array( 'type' => 'boolean' ),
144                        'changed' => array( 'type' => 'boolean' ),
145                    ),
146                ),
147                'execute_callback'    => array( __CLASS__, 'set_module_status' ),
148                'permission_callback' => array( __CLASS__, 'can_manage_modules' ),
149                'meta'                => array(
150                    'annotations'  => array(
151                        'readonly'    => false,
152                        'destructive' => false,
153                        'idempotent'  => true,
154                    ),
155                    'show_in_rest' => true,
156                    'mcp'          => array(
157                        'public' => true,
158                        'type'   => 'tool', // default is already "tool", but can be explicit.
159                    ),
160                ),
161            ),
162        );
163    }
164
165    /**
166     * Permission check: can the current user read module listings?
167     *
168     * Mirrors the capability used by the Jetpack admin page so subscribers and
169     * contributors are denied.
170     */
171    public static function can_view_modules(): bool {
172        return current_user_can( 'jetpack_admin_page' );
173    }
174
175    /**
176     * Permission check: can the current user toggle modules?
177     */
178    public static function can_manage_modules(): bool {
179        return current_user_can( 'jetpack_manage_modules' )
180            && current_user_can( 'jetpack_activate_modules' );
181    }
182
183    /**
184     * Build the high-signal summary shape used by `get-modules`.
185     *
186     * Adapts the raw `Jetpack::get_module()` array down to the fields agents
187     * actually consume — dropping internal bookkeeping like `module_tags`,
188     * changelog URLs, and file paths. Applies the site's current locale to
189     * `name` and `description` so agent-facing output mirrors what a user
190     * would see in the admin UI.
191     *
192     * @param string $slug      Module slug.
193     * @param bool   $is_active Whether the module is currently active. Passed in to avoid
194     *                          O(N) calls to the `jetpack_active_modules` filter in a list loop.
195     * @return array|null Compact module entry, or null when the module has no info.
196     */
197    private static function summarize_module( $slug, $is_active ) {
198        $mod = Jetpack::get_module( $slug );
199        if ( ! is_array( $mod ) ) {
200            return null;
201        }
202
203        $i18n = function_exists( 'jetpack_get_module_i18n' ) ? jetpack_get_module_i18n( $slug ) : array();
204        $name = isset( $i18n['name'] ) ? (string) $i18n['name'] : ( isset( $mod['name'] ) ? (string) $mod['name'] : $slug );
205        $desc = isset( $i18n['description'] ) ? (string) $i18n['description'] : ( isset( $mod['description'] ) ? (string) $mod['description'] : '' );
206
207        return array(
208            'slug'                     => $slug,
209            'name'                     => $name,
210            'description'              => $desc,
211            'active'                   => $is_active,
212            'sort'                     => isset( $mod['sort'] ) ? (int) $mod['sort'] : 10,
213            'feature'                  => isset( $mod['feature'] ) && is_array( $mod['feature'] ) ? array_values( $mod['feature'] ) : array(),
214            'plan_classes'             => isset( $mod['plan_classes'] ) && is_array( $mod['plan_classes'] ) ? array_values( $mod['plan_classes'] ) : array(),
215            'requires_connection'      => ! empty( $mod['requires_connection'] ),
216            'requires_user_connection' => ! empty( $mod['requires_user_connection'] ),
217            'auto_activate'            => isset( $mod['auto_activate'] ) ? (string) $mod['auto_activate'] : 'No',
218        );
219    }
220
221    /**
222     * Consolidated read callback. Returns an array of module summaries.
223     *
224     * When `slug` is provided, returns a 0- or 1-element array; unknown slugs
225     * yield an empty array rather than a `WP_Error` so the shape is uniform.
226     *
227     * @param array|null $input Input matching the ability's input_schema.
228     * @return array
229     */
230    public static function get_modules( $input = null ) {
231        $input = is_array( $input ) ? $input : array();
232
233        // Fetch the active-slug map once per call — `Jetpack::is_module_active()` fires the
234        // user-extensible `jetpack_active_modules` filter on each call, so looking up
235        // active state per-module in a loop amplifies any hook cost by N.
236        $active_map = array_flip( Jetpack::get_active_modules() );
237
238        // Narrow the candidate set up front when `slug` is supplied; remaining
239        // filters (active / feature / search) still apply so combinations like
240        // { slug: 'stats', active: false } correctly return an empty array when
241        // stats is active.
242        if ( isset( $input['slug'] ) && is_string( $input['slug'] ) && '' !== $input['slug'] ) {
243            if ( ! Jetpack::is_module( $input['slug'] ) ) {
244                return array();
245            }
246            $candidate_slugs = array( $input['slug'] );
247        } else {
248            $candidate_slugs = Jetpack::get_available_modules();
249        }
250
251        $active_filter  = array_key_exists( 'active', $input ) && is_bool( $input['active'] ) ? $input['active'] : null;
252        $feature_filter = isset( $input['feature'] ) && is_string( $input['feature'] ) && '' !== $input['feature']
253            ? strtolower( $input['feature'] )
254            : null;
255        $search_filter  = isset( $input['search'] ) && is_string( $input['search'] ) && '' !== $input['search']
256            ? strtolower( $input['search'] )
257            : null;
258
259        $out = array();
260        foreach ( $candidate_slugs as $slug ) {
261            $summary = self::summarize_module( $slug, isset( $active_map[ $slug ] ) );
262            if ( null === $summary ) {
263                continue;
264            }
265
266            if ( null !== $active_filter && $summary['active'] !== $active_filter ) {
267                continue;
268            }
269
270            if ( null !== $feature_filter ) {
271                $features_lower = array_map( 'strtolower', $summary['feature'] );
272                if ( ! in_array( $feature_filter, $features_lower, true ) ) {
273                    continue;
274                }
275            }
276
277            if ( null !== $search_filter ) {
278                $haystack = strtolower( $summary['slug'] . ' ' . $summary['name'] . ' ' . $summary['description'] );
279                if ( false === strpos( $haystack, $search_filter ) ) {
280                    continue;
281                }
282            }
283
284            $out[] = $summary;
285        }
286
287        usort(
288            $out,
289            static function ( $a, $b ) {
290                if ( $a['sort'] === $b['sort'] ) {
291                    return strcmp( $a['slug'], $b['slug'] );
292                }
293                return $a['sort'] <=> $b['sort'];
294            }
295        );
296
297        return $out;
298    }
299
300    /**
301     * Declarative state-setter callback. Idempotent: returns changed=false
302     * when the desired state already matches current state.
303     *
304     * @param array|null $input Input matching the ability's input_schema.
305     * @return array|\WP_Error
306     */
307    public static function set_module_status( $input = null ) {
308        $input = is_array( $input ) ? $input : array();
309
310        if ( ! isset( $input['slug'] ) || ! is_string( $input['slug'] ) || '' === $input['slug'] ) {
311            return new \WP_Error(
312                'jetpack_modules_missing_slug',
313                __( 'A module slug is required. Call jetpack/get-modules to enumerate valid slugs.', 'jetpack' )
314            );
315        }
316
317        if ( ! array_key_exists( 'active', $input ) ) {
318            return new \WP_Error(
319                'jetpack_modules_missing_active',
320                __( 'A desired active state (boolean) is required.', 'jetpack' )
321            );
322        }
323        if ( ! is_bool( $input['active'] ) ) {
324            return new \WP_Error(
325                'jetpack_modules_invalid_active',
326                __( 'The active parameter must be a boolean. Strings like "true" / "false" are not accepted.', 'jetpack' )
327            );
328        }
329
330        $slug    = $input['slug'];
331        $desired = $input['active'];
332
333        if ( ! Jetpack::is_module( $slug ) ) {
334            return new \WP_Error(
335                'jetpack_modules_invalid_slug',
336                __( 'Unknown Jetpack module slug. Call jetpack/get-modules to enumerate valid slugs.', 'jetpack' )
337            );
338        }
339
340        $current = Jetpack::is_module_active( $slug );
341
342        if ( $desired === $current ) {
343            return array(
344                'slug'    => $slug,
345                'active'  => $current,
346                'changed' => false,
347            );
348        }
349
350        if ( $desired ) {
351            // Preflight: Jetpack::activate_module() ignores its $exit/$redirect flags when a
352            // conflicting standalone plugin (e.g. WordPress.com Stats vs. the stats module) is
353            // active — it calls wp_safe_redirect()+exit() and would terminate this REST request
354            // mid-response. Refuse here with a structured error instead.
355            $conflict = self::find_conflicting_active_plugin( $slug );
356            if ( null !== $conflict ) {
357                return new \WP_Error(
358                    'jetpack_modules_conflicting_plugin_active',
359                    sprintf(
360                        /* translators: %s: name of the conflicting plugin that must be deactivated first. */
361                        __( 'Cannot activate the module while a conflicting plugin (%s) is active. Deactivate it on the WordPress Plugins screen, then retry.', 'jetpack' ),
362                        $conflict
363                    )
364                );
365            }
366
367            // Always pass exit=false, redirect=false so the ability runs headless over REST.
368            $ok = Jetpack::activate_module( $slug, false, false );
369            if ( ! $ok ) {
370                return new \WP_Error(
371                    'jetpack_modules_activate_failed',
372                    __( 'Unable to activate the module. It may require a Jetpack connection or a higher plan. Inspect requires_connection and plan_classes on the module and retry after those preconditions are met.', 'jetpack' )
373                );
374            }
375        } else {
376            $ok = Jetpack::deactivate_module( $slug );
377            if ( ! $ok ) {
378                return new \WP_Error(
379                    'jetpack_modules_deactivate_failed',
380                    __( 'Unable to deactivate the module.', 'jetpack' )
381                );
382            }
383        }
384
385        // Re-check state: activate_module() / deactivate_module() can return truthy even when a
386        // pre_update_option_jetpack_active_modules filter blocks the option write, so the response
387        // would otherwise lie about reaching the requested state.
388        $actual = Jetpack::is_module_active( $slug );
389        if ( $desired !== $actual ) {
390            return new \WP_Error(
391                'jetpack_modules_state_mismatch',
392                __( 'The module did not reach the requested state. A filter on jetpack_active_modules may have rejected the change.', 'jetpack' )
393            );
394        }
395
396        return array(
397            'slug'    => $slug,
398            'active'  => $actual,
399            'changed' => true,
400        );
401    }
402
403    /**
404     * Return the human-readable name of the first standalone plugin currently active that
405     * would force {@see Jetpack::activate_module()} to redirect/exit, or null when none.
406     *
407     * Mirrors the lookup in {@see Jetpack_Client_Server::deactivate_plugin()}: prefer the
408     * known plugin file path, fall back to a name match across active plugins.
409     *
410     * @param string $slug Module slug.
411     * @return string|null
412     */
413    private static function find_conflicting_active_plugin( $slug ) {
414        $jetpack = Jetpack::init();
415        if ( empty( $jetpack->plugins_to_deactivate[ $slug ] ) ) {
416            return null;
417        }
418
419        if ( ! function_exists( 'is_plugin_active' ) ) {
420            require_once ABSPATH . 'wp-admin/includes/plugin.php';
421        }
422
423        $active_plugins = null;
424        foreach ( $jetpack->plugins_to_deactivate[ $slug ] as $candidate ) {
425            list( $plugin_file, $plugin_name ) = $candidate;
426
427            if ( is_plugin_active( $plugin_file ) ) {
428                return $plugin_name;
429            }
430
431            if ( null === $active_plugins ) {
432                $active_plugins = Jetpack::get_active_plugins();
433            }
434            foreach ( $active_plugins as $active ) {
435                $data = get_plugin_data( WP_PLUGIN_DIR . '/' . $active );
436                if ( isset( $data['Name'] ) && $data['Name'] === $plugin_name ) {
437                    return $plugin_name;
438                }
439            }
440        }
441
442        return null;
443    }
444}