PluginProbe
Gutenberg / 23.2.0
Gutenberg v23.2.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / experimental / rest-api.php
lib/experimental
connectors 3 months ago content-types 3 months ago dashboard-widgets 3 months ago experiments 3 months ago font-face 1 year ago guidelines 3 months ago interactivity-api 1 year ago media-editor 3 months ago pages 6 months ago block-editor-settings-mobile.php 888 B 2 years ago blocks.php 4.4 KB 7 months ago class-gutenberg-hierarchical-sort.php 4.7 KB 6 months ago class-wp-rest-block-editor-settings-controller.php 24.2 KB 7 months ago editor-settings.php 3.3 KB 3 months ago extensible-site-editor.php 975 B 7 months ago kses-allowed-html.php 965 B 1 year ago kses.php 6.2 KB 5 months ago navigation-theme-opt-in.php 13.3 KB 6 months ago rest-api-overrides.php 1.6 KB 9 months ago rest-api.php 3.2 KB 7 months ago script-modules.php 7.9 KB 6 months ago workflow-palette.php 355 B 9 months ago

rest-api.php in Gutenberg 23.2.0, at lib/experimental/rest-api.php

95 lines 3.2 KB Raw Download Zip
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 * @link 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