diff --git a/class.jetpack-gutenberg.php b/class.jetpack-gutenberg.php index d6132a3a2736d..d480a6630af77 100644 --- a/class.jetpack-gutenberg.php +++ b/class.jetpack-gutenberg.php @@ -76,6 +76,7 @@ class Jetpack_Gutenberg { // PLUGINS private static $default_plugins = array( 'publicize', + 'shortlinks', ); /** * @var array Array of plugins information. diff --git a/modules/shortlinks.php b/modules/shortlinks.php index 54e30b94f96c1..9aa30eaab407f 100644 --- a/modules/shortlinks.php +++ b/modules/shortlinks.php @@ -87,3 +87,46 @@ function wpme_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) { function wpme_get_shortlink_handler( $shortlink, $id, $context, $allow_slugs ) { return wpme_get_shortlink( $id, $context, $allow_slugs ); } + +/** + * Add Shortlinks to the REST API Post response. + * + * @since 6.9.0 + * + * @action rest_api_init + * @uses register_rest_field, wpme_rest_get_shortlink + */ +function wpme_rest_register_shortlinks() { + register_rest_field( + 'post', + 'jetpack_shortlink', + array( + 'get_callback' => 'wpme_rest_get_shortlink', + 'update_callback' => null, + 'schema' => null, + ) + ); +} + +/** + * Get the shortlink of a post. + * + * @since 6.9.0 + * + * @param array $object Details of current post. + * + * @uses wpme_get_shortlink + * + * @return string + */ +function wpme_rest_get_shortlink( $object ) { + return wpme_get_shortlink( $object['id'], array() ); +} + +// Add shortlinks to the REST API Post response. +if ( function_exists( 'register_rest_field' ) ) { + add_action( 'rest_api_init', 'wpme_rest_register_shortlinks' ); +} + +// Register Gutenberg plugin +jetpack_register_plugin( 'shortlinks' );