| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHP and WordPress configuration compatibility functions for the Gutenberg |
| 4 |
* editor plugin changes related to REST API. |
| 5 |
* |
| 6 |
* @package gutenberg |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
die( 'Silence is golden.' ); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Registers the Block editor settings REST API routes. |
| 15 |
*/ |
| 16 |
function gutenberg_register_block_editor_settings() { |
| 17 |
$editor_settings = new WP_REST_Block_Editor_Settings_Controller(); |
| 18 |
$editor_settings->register_routes(); |
| 19 |
} |
| 20 |
add_action( 'rest_api_init', 'gutenberg_register_block_editor_settings' ); |
| 21 |
|
| 22 |
|
| 23 |
/** |
| 24 |
* Shim for get_sample_permalink() to add support for auto-draft status. |
| 25 |
* |
| 26 |
* This function filters the return from get_sample_permalink() and essentially |
| 27 |
* re-runs the same logic minus the filters, but pretends a status of auto-save |
| 28 |
* is actually publish in order to return the future permalink format. |
| 29 |
* |
| 30 |
* This is a temporary fix until we can patch get_sample_permalink() |
| 31 |
* |
| 32 |
* @see https://core.trac.wordpress.org/ticket/46266 |
| 33 |
* |
| 34 |
* @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name. |
| 35 |
* @param int $id ID of the post. |
| 36 |
* @param string $title Title of the post. |
| 37 |
* @param string $name Slug of the post. |
| 38 |
* @param object $post WP_Post object. |
| 39 |
* |
| 40 |
* @return array Array containing the sample permalink with placeholder for the post name, and the post name. |
| 41 |
*/ |
| 42 |
function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $name, $post ) { |
| 43 |
if ( 'auto-draft' !== $post->post_status ) { |
| 44 |
return $permalink; |
| 45 |
} |
| 46 |
$ptype = get_post_type_object( $post->post_type ); |
| 47 |
|
| 48 |
$original_status = $post->post_status; |
| 49 |
$original_date = $post->post_date; |
| 50 |
$original_name = $post->post_name; |
| 51 |
|
| 52 |
// Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published. |
| 53 |
$post->post_status = 'publish'; |
| 54 |
$post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID ); |
| 55 |
|
| 56 |
// If the user wants to set a new name -- override the current one. |
| 57 |
// Note: if empty name is supplied -- use the title instead, see #6072. |
| 58 |
if ( ! is_null( $name ) ) { |
| 59 |
$post->post_name = sanitize_title( $name ? $name : $title, $post->ID ); |
| 60 |
} |
| 61 |
|
| 62 |
$post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent ); |
| 63 |
|
| 64 |
$post->filter = 'sample'; |
| 65 |
|
| 66 |
$permalink = get_permalink( $post, true ); |
| 67 |
|
| 68 |
// Replace custom post_type Token with generic pagename token for ease of use. |
| 69 |
$permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink ); |
| 70 |
|
| 71 |
// Handle page hierarchy. |
| 72 |
if ( $ptype->hierarchical ) { |
| 73 |
$uri = get_page_uri( $post ); |
| 74 |
if ( $uri ) { |
| 75 |
$uri = untrailingslashit( $uri ); |
| 76 |
$uri = strrev( stristr( strrev( $uri ), '/' ) ); |
| 77 |
$uri = untrailingslashit( $uri ); |
| 78 |
} |
| 79 |
|
| 80 |
if ( ! empty( $uri ) ) { |
| 81 |
$uri .= '/'; |
| 82 |
} |
| 83 |
$permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink ); |
| 84 |
} |
| 85 |
|
| 86 |
$permalink = array( $permalink, $post->post_name ); |
| 87 |
$post->post_status = $original_status; |
| 88 |
$post->post_date = $original_date; |
| 89 |
$post->post_name = $original_name; |
| 90 |
unset( $post->filter ); |
| 91 |
|
| 92 |
return $permalink; |
| 93 |
} |
| 94 |
add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 ); |
| 95 |
|