PluginProbe
Gutenberg / 17.6.0
Gutenberg v17.6.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 17.6.0, at lib/experimental/rest-api.php

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