Skip to content

Commit

Permalink
Shortlinks: Register as REST API post field and Gutenberg plugin (#10981
Browse files Browse the repository at this point in the history
)

In the WP.com old editor, we used to have the shortlinks handy in the sidebar. We don't have them in Gutenberg, and they can sometimes be a very handy feature. This PR registers the post shortlink as a REST API field, and as a Gutenberg plugin, which are necessary to be able to build the UI for shortlinks in Gutenberg.

#### Changes proposed in this Pull Request:

* Add shortlinks to the REST API post response.
* Register shortlinks as a Gutenberg plugin, so they're available in our block availability endpoint.

#### Testing instructions:

* Start a JN site with this branch.
* Connect the site, and activate all recommended features.
* Write a post and save it.
* Type `wp.data.select( 'core/editor' ).getCurrentPost().jetpack_shortlink` in your browser console.
* Verify it returns a correct shortlink that leads to the post you just created.
* Type `window.Jetpack_Editor_Initial_State.available_blocks.shortlinks` in your browser console.
* Verify it returns `{available: true}`.
* Want to test the actual Gutenberg extension that uses this? Go to Automattic/wp-calypso#29475 and follow the test instructions there

#### Proposed changelog entry for your changes:

* Shortlinks: Register as REST API post field and Gutenberg plugin

#### Notes

I'm marking this as low priority, as it isn't super important to have. In the same time, I think it's handy to have it, and it's straightforward enough so we could have it ready for the next Jetpack version.
  • Loading branch information
tyxla authored and jeherve committed Dec 17, 2018
1 parent 3c25b0d commit 285e775
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Jetpack_Gutenberg {
// PLUGINS
private static $default_plugins = array(
'publicize',
'shortlinks',
);
/**
* @var array Array of plugins information.
Expand Down
43 changes: 43 additions & 0 deletions modules/shortlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

0 comments on commit 285e775

Please sign in to comment.