| 1 |
<?php |
| 2 |
/** |
| 3 |
* Initialization and wp-admin integration for the Gutenberg editor plugin. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
die( 'Silence is golden.' ); |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Collect information about meta_boxes registered for the current post. |
| 14 |
* |
| 15 |
* Redirects to classic editor if a meta box is incompatible. |
| 16 |
* |
| 17 |
* @since 1.5.0 |
| 18 |
* @deprecated 5.0.0 register_and_do_post_meta_boxes |
| 19 |
*/ |
| 20 |
function gutenberg_collect_meta_box_data() { |
| 21 |
_deprecated_function( __FUNCTION__, '5.0.0', 'register_and_do_post_meta_boxes' ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Return whether the post can be edited in Gutenberg and by the current user. |
| 26 |
* |
| 27 |
* @since 0.5.0 |
| 28 |
* |
| 29 |
* @param int|WP_Post $post Post ID or WP_Post object. |
| 30 |
* @return bool Whether the post can be edited with Gutenberg. |
| 31 |
*/ |
| 32 |
function gutenberg_can_edit_post( $post ) { |
| 33 |
$post = get_post( $post ); |
| 34 |
$can_edit = true; |
| 35 |
|
| 36 |
if ( ! $post ) { |
| 37 |
$can_edit = false; |
| 38 |
} |
| 39 |
|
| 40 |
if ( $can_edit && 'trash' === $post->post_status ) { |
| 41 |
$can_edit = false; |
| 42 |
} |
| 43 |
|
| 44 |
if ( $can_edit && ! gutenberg_can_edit_post_type( $post->post_type ) ) { |
| 45 |
$can_edit = false; |
| 46 |
} |
| 47 |
|
| 48 |
if ( $can_edit && ! current_user_can( 'edit_post', $post->ID ) ) { |
| 49 |
$can_edit = false; |
| 50 |
} |
| 51 |
|
| 52 |
// Disable the editor if on the blog page and there is no content. |
| 53 |
if ( $can_edit && absint( get_option( 'page_for_posts' ) ) === $post->ID && empty( $post->post_content ) ) { |
| 54 |
$can_edit = false; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Filter to allow plugins to enable/disable Gutenberg for particular post. |
| 59 |
* |
| 60 |
* @since 3.5 |
| 61 |
* |
| 62 |
* @param bool $can_edit Whether the post can be edited or not. |
| 63 |
* @param WP_Post $post The post being checked. |
| 64 |
*/ |
| 65 |
return apply_filters( 'gutenberg_can_edit_post', $can_edit, $post ); |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Return whether the post type can be edited in Gutenberg. |
| 71 |
* |
| 72 |
* Gutenberg depends on the REST API, and if the post type is not shown in the |
| 73 |
* REST API, then the post cannot be edited in Gutenberg. |
| 74 |
* |
| 75 |
* @since 1.5.2 |
| 76 |
* |
| 77 |
* @param string $post_type The post type. |
| 78 |
* @return bool Whether the post type can be edited with Gutenberg. |
| 79 |
*/ |
| 80 |
function gutenberg_can_edit_post_type( $post_type ) { |
| 81 |
$can_edit = true; |
| 82 |
if ( ! post_type_exists( $post_type ) ) { |
| 83 |
$can_edit = false; |
| 84 |
} |
| 85 |
|
| 86 |
if ( ! post_type_supports( $post_type, 'editor' ) ) { |
| 87 |
$can_edit = false; |
| 88 |
} |
| 89 |
|
| 90 |
$post_type_object = get_post_type_object( $post_type ); |
| 91 |
if ( $post_type_object && ! $post_type_object->show_in_rest ) { |
| 92 |
$can_edit = false; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Filter to allow plugins to enable/disable Gutenberg for particular post types. |
| 97 |
* |
| 98 |
* @since 1.5.2 |
| 99 |
* |
| 100 |
* @param bool $can_edit Whether the post type can be edited or not. |
| 101 |
* @param string $post_type The post type being checked. |
| 102 |
*/ |
| 103 |
return apply_filters( 'gutenberg_can_edit_post_type', $can_edit, $post_type ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Determine whether a post has blocks. This test optimizes for performance |
| 108 |
* rather than strict accuracy, detecting the pattern of a block but not |
| 109 |
* validating its structure. For strict accuracy, you should use the block |
| 110 |
* parser on post content. |
| 111 |
* |
| 112 |
* @see gutenberg_parse_blocks() |
| 113 |
* |
| 114 |
* @since 0.5.0 |
| 115 |
* @deprecated 3.6.0 Use has_blocks() |
| 116 |
* |
| 117 |
* @param object $post Post. |
| 118 |
* @return bool Whether the post has blocks. |
| 119 |
*/ |
| 120 |
function gutenberg_post_has_blocks( $post ) { |
| 121 |
_deprecated_function( __FUNCTION__, '3.6.0', 'has_blocks()' ); |
| 122 |
return has_blocks( $post ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Determine whether a content string contains blocks. This test optimizes for |
| 127 |
* performance rather than strict accuracy, detecting the pattern of a block |
| 128 |
* but not validating its structure. For strict accuracy, you should use the |
| 129 |
* block parser on post content. |
| 130 |
* |
| 131 |
* @see gutenberg_parse_blocks() |
| 132 |
* |
| 133 |
* @since 1.6.0 |
| 134 |
* @deprecated 3.6.0 Use has_blocks() |
| 135 |
* |
| 136 |
* @param string $content Content to test. |
| 137 |
* @return bool Whether the content contains blocks. |
| 138 |
*/ |
| 139 |
function gutenberg_content_has_blocks( $content ) { |
| 140 |
_deprecated_function( __FUNCTION__, '3.6.0', 'has_blocks()' ); |
| 141 |
return has_blocks( $content ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Returns the current version of the block format that the content string is using. |
| 146 |
* |
| 147 |
* If the string doesn't contain blocks, it returns 0. |
| 148 |
* |
| 149 |
* @since 2.8.0 |
| 150 |
* @see gutenberg_content_has_blocks() |
| 151 |
* @deprecated 5.0.0 block_version |
| 152 |
* |
| 153 |
* @param string $content Content to test. |
| 154 |
* @return int The block format version. |
| 155 |
*/ |
| 156 |
function gutenberg_content_block_version( $content ) { |
| 157 |
_deprecated_function( __FUNCTION__, '5.0.0', 'block_version' ); |
| 158 |
|
| 159 |
return block_version( $content ); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Adds a "Gutenberg" post state for post tables, if the post contains blocks. |
| 164 |
* |
| 165 |
* @deprecated 5.0.0 |
| 166 |
* |
| 167 |
* @param array $post_states An array of post display states. |
| 168 |
* @return array A filtered array of post display states. |
| 169 |
*/ |
| 170 |
function gutenberg_add_gutenberg_post_state( $post_states ) { |
| 171 |
_deprecated_function( __FUNCTION__, '5.0.0' ); |
| 172 |
|
| 173 |
return $post_states; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Registers custom post types required by the Gutenberg editor. |
| 178 |
* |
| 179 |
* @since 0.10.0 |
| 180 |
* @deprecated 5.0.0 |
| 181 |
*/ |
| 182 |
function gutenberg_register_post_types() { |
| 183 |
_deprecated_function( __FUNCTION__, '5.0.0' ); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Apply the correct labels for Reusable Blocks in the bulk action updated messages. |
| 188 |
* |
| 189 |
* @since 4.3.0 |
| 190 |
* @deprecated 5.0.0 |
| 191 |
* |
| 192 |
* @param array $messages Arrays of messages, each keyed by the corresponding post type. |
| 193 |
* |
| 194 |
* @return array |
| 195 |
*/ |
| 196 |
function gutenberg_bulk_post_updated_messages( $messages ) { |
| 197 |
_deprecated_function( __FUNCTION__, '5.0.0' ); |
| 198 |
|
| 199 |
return $messages; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Injects a hidden input in the edit form to propagate the information that classic editor is selected. |
| 204 |
* |
| 205 |
* @since 1.5.2 |
| 206 |
*/ |
| 207 |
function gutenberg_remember_classic_editor_when_saving_posts() { |
| 208 |
?> |
| 209 |
<input type="hidden" name="classic-editor" /> |
| 210 |
<?php |
| 211 |
} |
| 212 |
add_action( 'edit_form_top', 'gutenberg_remember_classic_editor_when_saving_posts' ); |
| 213 |
|
| 214 |
/** |
| 215 |
* Appends a query argument to the redirect url to make sure it gets redirected to the classic editor. |
| 216 |
* |
| 217 |
* @since 1.5.2 |
| 218 |
* |
| 219 |
* @param string $url Redirect url. |
| 220 |
* @return string Redirect url. |
| 221 |
*/ |
| 222 |
function gutenberg_redirect_to_classic_editor_when_saving_posts( $url ) { |
| 223 |
if ( isset( $_REQUEST['classic-editor'] ) ) { |
| 224 |
$url = add_query_arg( 'classic-editor', '', $url ); |
| 225 |
} |
| 226 |
return $url; |
| 227 |
} |
| 228 |
add_filter( 'redirect_post_location', 'gutenberg_redirect_to_classic_editor_when_saving_posts', 10, 1 ); |
| 229 |
|
| 230 |
/** |
| 231 |
* Appends a query argument to the edit url to make sure it is redirected to |
| 232 |
* the editor from which the user navigated. |
| 233 |
* |
| 234 |
* @since 1.5.2 |
| 235 |
* |
| 236 |
* @param string $url Edit url. |
| 237 |
* @return string Edit url. |
| 238 |
*/ |
| 239 |
function gutenberg_revisions_link_to_editor( $url ) { |
| 240 |
global $pagenow; |
| 241 |
if ( 'revision.php' !== $pagenow || isset( $_REQUEST['gutenberg'] ) ) { |
| 242 |
return $url; |
| 243 |
} |
| 244 |
|
| 245 |
return add_query_arg( 'classic-editor', '', $url ); |
| 246 |
} |
| 247 |
add_filter( 'get_edit_post_link', 'gutenberg_revisions_link_to_editor' ); |
| 248 |
|
| 249 |
/** |
| 250 |
* Modifies revisions data to preserve Gutenberg argument used in determining |
| 251 |
* where to redirect user returning to editor. |
| 252 |
* |
| 253 |
* @since 1.9.0 |
| 254 |
* |
| 255 |
* @param array $revisions_data The bootstrapped data for the revisions screen. |
| 256 |
* @return array Modified bootstrapped data for the revisions screen. |
| 257 |
*/ |
| 258 |
function gutenberg_revisions_restore( $revisions_data ) { |
| 259 |
if ( isset( $_REQUEST['gutenberg'] ) ) { |
| 260 |
$revisions_data['restoreUrl'] = add_query_arg( |
| 261 |
'gutenberg', |
| 262 |
$_REQUEST['gutenberg'], |
| 263 |
$revisions_data['restoreUrl'] |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
return $revisions_data; |
| 268 |
} |
| 269 |
add_filter( 'wp_prepare_revision_for_js', 'gutenberg_revisions_restore' ); |
| 270 |
|