Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Site editor: update custom post types with _edit_link #50563

Merged
merged 1 commit into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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