Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.50% covered (success)
97.50%
117 / 120
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Shortlinks_Abilities
99.15% covered (success)
99.15%
117 / 118
83.33% covered (warning)
83.33%
5 / 6
22
0.00% covered (danger)
0.00%
0 / 1
 enabled_by_default
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
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%
68 / 68
100.00% covered (success)
100.00%
1 / 1
1
 can_view_shortlinks
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_shortlinks
97.67% covered (success)
97.67%
42 / 43
0.00% covered (danger)
0.00%
0 / 1
17
1<?php
2/**
3 * Jetpack Shortlinks Abilities Registration
4 *
5 * Registers Jetpack WP.me Shortlinks abilities with the WordPress Abilities API.
6 *
7 * @package automattic/jetpack
8 */
9
10namespace Automattic\Jetpack\Plugin\Abilities;
11
12use Automattic\Jetpack\Connection\Manager as Connection_Manager;
13use Automattic\Jetpack\WP_Abilities\Registrar;
14
15if ( ! defined( 'ABSPATH' ) ) {
16    exit( 0 );
17}
18
19/**
20 * Registers Jetpack WP.me Shortlinks abilities with the WordPress Abilities API.
21 *
22 * Exposes a single read ability that returns wp.me shortlinks for the
23 * requested posts and (optionally) the site homepage so AI agents can fetch
24 * shareable URLs in bulk through the standard `wp-abilities/v1` REST surface.
25 */
26class Shortlinks_Abilities extends Registrar {
27
28    /**
29     * Maximum number of post IDs accepted per call.
30     */
31    const MAX_POST_IDS = 100;
32
33    /**
34     * {@inheritDoc}
35     */
36    protected static function enabled_by_default(): bool {
37        return true;
38    }
39
40    /**
41     * Returns the category slug this registrar owns.
42     *
43     * @return string
44     */
45    public static function get_category_slug(): string {
46        return 'jetpack-shortlinks';
47    }
48
49    /**
50     * Returns the category definition passed to wp_register_ability_category().
51     *
52     * @return array
53     */
54    public static function get_category_definition(): array {
55        return array(
56            // "Jetpack" is a product name and should not be translated.
57            'label'       => 'Jetpack WP.me Shortlinks',
58            'description' => __( 'Abilities for retrieving wp.me shortlinks for posts, pages, attachments, and the site homepage.', 'jetpack' ),
59        );
60    }
61
62    /**
63     * Returns the ability specs this registrar owns as a [ slug => spec ] map.
64     *
65     * @return array<string, array>
66     */
67    public static function get_abilities(): array {
68        $item_schema = array(
69            'type'       => 'object',
70            'properties' => array(
71                'post_id'    => array(
72                    'type'        => 'integer',
73                    'description' => __( '0 for the site homepage entry; otherwise the WordPress post ID.', 'jetpack' ),
74                ),
75                'post_title' => array( 'type' => 'string' ),
76                'post_type'  => array(
77                    'type'        => 'string',
78                    'description' => __( '"blog" for the homepage entry; otherwise the WordPress post type slug (post, page, attachment, or a CPT that supports shortlinks).', 'jetpack' ),
79                ),
80                'shortlink'  => array(
81                    'type'        => 'string',
82                    'description' => __( 'The wp.me URL.', 'jetpack' ),
83                ),
84                'target_url' => array(
85                    'type'        => 'string',
86                    'description' => __( 'The canonical permalink the shortlink resolves to.', 'jetpack' ),
87                ),
88            ),
89        );
90
91        return array(
92            'jetpack-shortlinks/get-shortlinks' => array(
93                'label'               => __( 'Get wp.me shortlinks', 'jetpack' ),
94                'description'         => __( 'Return wp.me shortlinks for the given posts and (optionally) the site homepage. Each result is a { post_id, post_title, post_type, shortlink, target_url } object â€” post_id is 0 for the homepage entry, post_type is "blog". IDs that do not exist, are unreadable to the current user, or belong to a post type without shortlinks support are silently omitted (returns a shorter array, not WP_Error). Caps post_ids at 100 per call. Requires the WP.me Shortlinks Jetpack module (slug: shortlinks) to be active and the site to be connected; an unconnected site returns jetpack_shortlinks_blog_id_unavailable. Use jetpack/get-modules to verify the module is on, jetpack/set-module-status to activate it.', 'jetpack' ),
95                'input_schema'        => array(
96                    'type'                 => 'object',
97                    'default'              => array(),
98                    'properties'           => array(
99                        'post_ids'     => array(
100                            'type'        => 'array',
101                            'description' => __( 'Up to 100 post IDs to fetch shortlinks for. Order is preserved in the response. Defaults to an empty list.', 'jetpack' ),
102                            'maxItems'    => self::MAX_POST_IDS,
103                            'items'       => array(
104                                'type'    => 'integer',
105                                'minimum' => 1,
106                            ),
107                        ),
108                        'include_blog' => array(
109                            'type'        => 'boolean',
110                            'description' => __( 'When true, prepend a single entry for the site homepage (post_id 0, post_type "blog") to the response.', 'jetpack' ),
111                            'default'     => false,
112                        ),
113                    ),
114                    'additionalProperties' => false,
115                ),
116                'output_schema'       => array(
117                    'type'  => 'array',
118                    'items' => $item_schema,
119                ),
120                'execute_callback'    => array( __CLASS__, 'get_shortlinks' ),
121                'permission_callback' => array( __CLASS__, 'can_view_shortlinks' ),
122                'meta'                => array(
123                    'annotations'  => array(
124                        'readonly'    => true,
125                        'destructive' => false,
126                        'idempotent'  => true,
127                    ),
128                    'public'       => true,
129                    'show_in_rest' => true,
130                    'mcp'          => array(
131                        'public' => true,
132                        'type'   => 'tool', // default is already "tool", but can be explicit.
133                    ),
134                ),
135            ),
136        );
137    }
138
139    /**
140     * Permission check: require edit_posts so only users with content-management
141     * rights may enumerate shortlinks. Per-post read capability is still
142     * enforced inside the execute callback so unreadable IDs are silently
143     * omitted rather than failing the whole call.
144     */
145    public static function can_view_shortlinks(): bool {
146        return current_user_can( 'edit_posts' );
147    }
148
149    /**
150     * Execute: return wp.me shortlinks for the requested posts and (optionally) the homepage.
151     *
152     * @param array|null $input Input matching the ability's input_schema.
153     * @return array|\WP_Error
154     */
155    public static function get_shortlinks( $input = null ) {
156        $input = is_array( $input ) ? $input : array();
157
158        if ( null === Connection_Manager::get_site_id( true ) ) {
159            return new \WP_Error(
160                'jetpack_shortlinks_blog_id_unavailable',
161                __( 'No Jetpack/WordPress.com blog ID is available for this site. Connect Jetpack before requesting wp.me shortlinks.', 'jetpack' )
162            );
163        }
164
165        $post_ids = array();
166        if ( isset( $input['post_ids'] ) && is_array( $input['post_ids'] ) ) {
167            foreach ( $input['post_ids'] as $candidate ) {
168                $candidate_id = absint( $candidate );
169                if ( $candidate_id > 0 ) {
170                    $post_ids[] = $candidate_id;
171                }
172            }
173        }
174        if ( count( $post_ids ) > self::MAX_POST_IDS ) {
175            $post_ids = array_slice( $post_ids, 0, self::MAX_POST_IDS );
176        }
177
178        $include_blog = ! empty( $input['include_blog'] );
179
180        $out = array();
181
182        if ( $include_blog ) {
183            $blog_shortlink = wp_get_shortlink( 0, 'blog' );
184            if ( is_string( $blog_shortlink ) && '' !== $blog_shortlink ) {
185                $out[] = array(
186                    'post_id'    => 0,
187                    'post_title' => (string) get_bloginfo( 'name' ),
188                    'post_type'  => 'blog',
189                    'shortlink'  => $blog_shortlink,
190                    'target_url' => home_url( '/' ),
191                );
192            }
193        }
194
195        if ( ! empty( $post_ids ) ) {
196            _prime_post_caches( $post_ids, false, false );
197        }
198
199        foreach ( $post_ids as $post_id ) {
200            $post = get_post( $post_id );
201            if ( ! $post || ! current_user_can( 'read_post', $post_id ) ) {
202                continue;
203            }
204            $shortlink = wp_get_shortlink( $post_id, 'post' );
205            if ( ! is_string( $shortlink ) || '' === $shortlink ) {
206                continue;
207            }
208            $out[] = array(
209                'post_id'    => (int) $post->ID,
210                'post_title' => (string) $post->post_title,
211                'post_type'  => (string) $post->post_type,
212                'shortlink'  => $shortlink,
213                'target_url' => (string) get_permalink( $post ),
214            );
215        }
216
217        return $out;
218    }
219}