PluginProbe
Gutenberg / 15.2.0
Gutenberg v15.2.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 7.4.0 All 402 releases
gutenberg / lib / experimental / rest-api.php

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

125 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
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 customizer nonces REST API routes.
15 */
16 function gutenberg_register_rest_customizer_nonces() {
17 $customizer_nonces = new WP_Rest_Customizer_Nonces();
18 $customizer_nonces->register_routes();
19 }
20 add_action( 'rest_api_init', 'gutenberg_register_rest_customizer_nonces' );
21
22 /**
23 * Registers the Block editor settings REST API routes.
24 */
25 function gutenberg_register_block_editor_settings() {
26 $editor_settings = new WP_REST_Block_Editor_Settings_Controller();
27 $editor_settings->register_routes();
28 }
29 add_action( 'rest_api_init', 'gutenberg_register_block_editor_settings' );
30
31
32 /**
33 * Hook in to the template and template part post types and decorate
34 * the rest endpoint with the revision count.
35 *
36 * When merging to core, this can be removed once Gutenberg_REST_Template_Revision_Count is
37 * merged with WP_REST_Template_Controller.
38 *
39 * @param array $args Current registered post type args.
40 * @param string $post_type Name of post type.
41 *
42 * @return array
43 */
44 function wp_api_template_revision_args( $args, $post_type ) {
45 if ( 'wp_template' === $post_type || 'wp_template_part' === $post_type ) {
46 $args['rest_controller_class'] = 'Gutenberg_REST_Template_Revision_Count';
47 }
48
49 return $args;
50 }
51 add_filter( 'register_post_type_args', 'wp_api_template_revision_args', 10, 2 );
52
53 /**
54 * Shim for get_sample_permalink() to add support for auto-draft status.
55 *
56 * This function filters the return from get_sample_permalink() and essentially
57 * re-runs the same logic minus the filters, but pretends a status of auto-save
58 * is actually publish in order to return the future permalink format.
59 *
60 * This is a temporary fix until we can patch get_sample_permalink()
61 *
62 * @see https://core.trac.wordpress.org/ticket/46266
63 *
64 * @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name.
65 * @param int $id ID of the post.
66 * @param string $title Title of the post.
67 * @param string $name Slug of the post.
68 * @param object $post WP_Post object.
69 *
70 * @return array Array containing the sample permalink with placeholder for the post name, and the post name.
71 */
72 function gutenberg_auto_draft_get_sample_permalink( $permalink, $id, $title, $name, $post ) {
73 if ( 'auto-draft' !== $post->post_status ) {
74 return $permalink;
75 }
76 $ptype = get_post_type_object( $post->post_type );
77
78 $original_status = $post->post_status;
79 $original_date = $post->post_date;
80 $original_name = $post->post_name;
81
82 // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
83 $post->post_status = 'publish';
84 $post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
85
86 // If the user wants to set a new name -- override the current one.
87 // Note: if empty name is supplied -- use the title instead, see #6072.
88 if ( ! is_null( $name ) ) {
89 $post->post_name = sanitize_title( $name ? $name : $title, $post->ID );
90 }
91
92 $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent );
93
94 $post->filter = 'sample';
95
96 $permalink = get_permalink( $post, true );
97
98 // Replace custom post_type Token with generic pagename token for ease of use.
99 $permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink );
100
101 // Handle page hierarchy.
102 if ( $ptype->hierarchical ) {
103 $uri = get_page_uri( $post );
104 if ( $uri ) {
105 $uri = untrailingslashit( $uri );
106 $uri = strrev( stristr( strrev( $uri ), '/' ) );
107 $uri = untrailingslashit( $uri );
108 }
109
110 if ( ! empty( $uri ) ) {
111 $uri .= '/';
112 }
113 $permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink );
114 }
115
116 $permalink = array( $permalink, $post->post_name );
117 $post->post_status = $original_status;
118 $post->post_date = $original_date;
119 $post->post_name = $original_name;
120 unset( $post->filter );
121
122 return $permalink;
123 }
124 add_filter( 'get_sample_permalink', 'gutenberg_auto_draft_get_sample_permalink', 10, 5 );
125