PluginProbe
Gutenberg / 16.5.0
Gutenberg v16.5.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 16.5.0, at lib/experimental/rest-api.php

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