Skip to content

Commit

Permalink
This commit: (#50563)
Browse files Browse the repository at this point in the history
- adds an `_edit_link` property to `wp_global_styles`, `wp_template`, and `wp_template-part` custom post type schemata via filter
- uses the `_edit_link` value to create an edit link via the `get_edit_post_link` hook
  • Loading branch information
ramonjd authored May 13, 2023
1 parent 99c3dda commit f32c9a4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
34 changes: 34 additions & 0 deletions lib/compat/wordpress-6.3/link-template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Overrides Core's wp-includes/link-template.php for WP 6.3.
*
* @package gutenberg
*/

/**
* Updates the post edit link using the `_edit_link` property in wp_global_styles`, `wp_template`,
* and `wp_template_part` custom post types.
*
* `_edit_link` for these custom post types is added by `gutenberg_update_templates_template_parts_rest_controller()`
* in lib/compat/wordpress-6.3/rest-api.php.
*
* This functionality has already been ported to Core. See https://github.com/WordPress/gutenberg/issues/48065
* The following hook is a modified version that passes only 2 arguments to `sprintf()` to be compatible with WP <= 6.2.
*
* @param string $link The edit link.
* @param int $post_id Post ID.
* @return string|null The edit post link for the given post. Null if the post type does not exist
* or does not allow an editing UI.
*/
function gutenberg_update_get_edit_post_link( $link, $post_id ) {
$post = get_post( $post_id );

if ( 'wp_template' === $post->post_type || 'wp_template_part' === $post->post_type ) {
$post_type_object = get_post_type_object( $post->post_type );
$slug = urlencode( get_stylesheet() . '//' . $post->post_name );
$link = admin_url( sprintf( $post_type_object->_edit_link, $slug ) );
}
return $link;
}

add_filter( 'get_edit_post_link', 'gutenberg_update_get_edit_post_link', 10, 2 );
20 changes: 17 additions & 3 deletions lib/compat/wordpress-6.3/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,35 @@ function gutenberg_register_rest_pattern_directory() {
add_action( 'rest_api_init', 'gutenberg_register_rest_pattern_directory' );

/**
* Update `wp_template` and `wp_template-part` post types to use
* Gutenberg's REST controller.
* Updates `wp_template` and `wp_template_part` post types to use
* Gutenberg's REST controllers
*
* Adds `_edit_link` to the `wp_global_styles`, `wp_template`,
* and `wp_template_part` post type schemata. See https://github.com/WordPress/gutenberg/issues/48065
*
* @param array $args Array of arguments for registering a post type.
* @param string $post_type Post type key.
*/
function gutenberg_update_templates_template_parts_rest_controller( $args, $post_type ) {
if ( in_array( $post_type, array( 'wp_template', 'wp_template_part' ), true ) ) {
$template_edit_link = 'site-editor.php?' . build_query(
array(
'postType' => $post_type,
'postId' => '%s',
'canvas' => 'edit',
)
);
$args['_edit_link'] = $template_edit_link;
$args['rest_controller_class'] = 'Gutenberg_REST_Templates_Controller_6_3';
}

if ( in_array( $post_type, array( 'wp_global_styles' ), true ) ) {
$args['_edit_link'] = '/site-editor.php?canvas=edit';
}
return $args;
}
add_filter( 'register_post_type_args', 'gutenberg_update_templates_template_parts_rest_controller', 10, 2 );


/**
* Registers the Global Styles Revisions REST API routes.
*/
Expand Down
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require_once __DIR__ . '/compat/wordpress-6.3/rest-api.php';
require_once __DIR__ . '/compat/wordpress-6.3/theme-previews.php';
require_once __DIR__ . '/compat/wordpress-6.3/navigation-block-preloading.php';
require_once __DIR__ . '/compat/wordpress-6.3/link-template.php';

// Experimental.
if ( ! class_exists( 'WP_Rest_Customizer_Nonces' ) ) {
Expand Down

0 comments on commit f32c9a4

Please sign in to comment.