Changeset 50983
- Timestamp:
- 05/24/2021 09:20:31 PM (4 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/edit-form-blocks.php
r50956 r50983 24 24 global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes; 25 25 26 $editor_name = 'post-editor';27 26 $block_editor_context = new WP_Block_Editor_Context( array( 'post' => $post ) ); 28 27 … … 281 280 } 282 281 283 $editor_settings = get_block_editor_settings( $editor_ name, $editor_settings);282 $editor_settings = get_block_editor_settings( $editor_settings, $block_editor_context ); 284 283 285 284 $init_script = <<<JS -
trunk/src/wp-includes/block-editor.php
r50973 r50983 59 59 * 60 60 * @since 5.0.0 61 * 62 * @param string|WP_Post $editor_name_or_post The name of the editor (e.g. 'post-editor') 63 * or the post object. 61 * @since 5.8.0 It is possible to pass the block editor context as param. 62 * 63 * @param WP_Post|WP_Block_Editor_Context $post_or_block_editor_context The current post object or 64 * the block editor context. 64 65 * 65 66 * @return array[] Array of categories for block types. 66 67 */ 67 function get_block_categories( $editor_name_or_post ) { 68 // Assume the post editor when the WP_Post object passed. 69 $editor_name = is_object( $editor_name_or_post ) ? 'post-editor' : $editor_name_or_post; 70 $block_categories = get_default_block_categories(); 68 function get_block_categories( $post_or_block_editor_context ) { 69 $block_categories = get_default_block_categories(); 70 $block_editor_context = $post_or_block_editor_context instanceof WP_Post ? 71 new WP_Block_Editor_Context( 72 array( 73 'post' => $post_or_block_editor_context, 74 ) 75 ) : $post_or_block_editor_context; 71 76 72 77 /** … … 75 80 * @since 5.8.0 76 81 * 77 * @param array[] $block_categoriesArray of categories for block types.78 * @param string $editor_name The name of the editor, e.g. 'post-editor'.82 * @param array[] $block_categories Array of categories for block types. 83 * @param WP_Block_Editor_Context $block_editor_context The current block editor context. 79 84 */ 80 $block_categories = apply_filters( 'block_categories_all', $block_categories, $ editor_name);81 if ( 'post-editor' === $editor_name) {82 $post = is_object( $editor_name_or_post ) ? $editor_name_or_post : get_post();85 $block_categories = apply_filters( 'block_categories_all', $block_categories, $block_editor_context ); 86 if ( ! empty( $block_editor_context->post ) ) { 87 $post = $block_editor_context->post; 83 88 84 89 /** … … 102 107 * @since 5.8.0 103 108 * 104 * @param string $editor_name The name of the editor (e.g. 'post-editor').109 * @param WP_Block_Editor_Context $block_editor_context The current block editor context. 105 110 * 106 111 * @return bool|array Array of block type slugs, or boolean to enable/disable all. 107 112 */ 108 function get_allowed_block_types( $ editor_name) {113 function get_allowed_block_types( $block_editor_context ) { 109 114 $allowed_block_types = true; 110 115 … … 116 121 * @since 5.8.0 117 122 * 118 * @param bool|array $allowed_block_typesArray of block type slugs, or119 * boolean to enable/disable all.120 * @param string $editor_name The name of the editor, e.g. 'post-editor'.123 * @param bool|array $allowed_block_types Array of block type slugs, or 124 * boolean to enable/disable all. 125 * @param WP_Block_Editor_Context $block_editor_context The current block editor context. 121 126 */ 122 $allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $ editor_name);123 if ( 'post-editor' === $editor_name) {124 $post = get_post();127 $allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $block_editor_context ); 128 if ( ! empty( $block_editor_context->post ) ) { 129 $post = $block_editor_context->post; 125 130 126 131 /** … … 225 230 226 231 /** 227 * Returns the contextualized block editor settings settings for a selected editor type.232 * Returns the contextualized block editor settings settings for a selected editor context. 228 233 * 229 234 * @since 5.8.0 230 235 * 231 * @param string $editor_name The name of the editor (e.g. 'post-editor').232 * @param array $custom_settings Optional custom settings to use with the editor type.236 * @param array $custom_settings Custom settings to use with the given editor type. 237 * @param WP_Block_Editor_Context $block_editor_context The current block editor context. 233 238 * 234 239 * @return array The contextualized block editor settings. 235 240 */ 236 function get_block_editor_settings( $editor_name, $custom_settings = array()) {241 function get_block_editor_settings( array $custom_settings, $block_editor_context ) { 237 242 $editor_settings = array_merge( 238 243 get_default_block_editor_settings(), 239 244 array( 240 'allowedBlockTypes' => get_allowed_block_types( $ editor_name),241 'blockCategories' => get_block_categories( $ editor_name),245 'allowedBlockTypes' => get_allowed_block_types( $block_editor_context ), 246 'blockCategories' => get_block_categories( $block_editor_context ), 242 247 ), 243 248 $custom_settings … … 302 307 * @since 5.8.0 303 308 * 304 * @param array $editor_settingsDefault editor settings.305 * @param string $editor_name The name of the editor, e.g. 'post-editor'.309 * @param array $editor_settings Default editor settings. 310 * @param WP_Block_Editor_Context $block_editor_context The current block editor context. 306 311 */ 307 $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $ editor_name);308 if ( 'post-editor' === $editor_name) {309 $post = get_post();312 $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context ); 313 if ( ! empty( $block_editor_context->post ) ) { 314 $post = $block_editor_context->post; 310 315 311 316 /** -
trunk/tests/phpunit/tests/blocks/block-editor.php
r50956 r50983 77 77 */ 78 78 function test_block_editor_context_post() { 79 $context = new WP_Block_Editor_Context( array( 'post' => $this->post) );80 81 $this->assertSame( $this->post, $context->post );79 $context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); 80 81 $this->assertSame( get_post(), $context->post ); 82 82 } 83 83 … … 112 112 add_filter( 'block_categories', array( $this, 'filter_set_block_categories_post' ), 10, 2 ); 113 113 114 $block_categories = get_block_categories( 'post-editor' ); 114 $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); 115 $block_categories = get_block_categories( $post_editor_context ); 115 116 116 117 remove_filter( 'block_categories', array( $this, 'filter_set_block_categories_post' ) ); … … 132 133 */ 133 134 function test_get_allowed_block_types_default() { 134 $allowed_block_types = get_allowed_block_types( 'post-editor' ); 135 $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); 136 $allowed_block_types = get_allowed_block_types( $post_editor_context ); 135 137 136 138 $this->assertTrue( $allowed_block_types ); … … 144 146 add_filter( 'allowed_block_types', array( $this, 'filter_set_allowed_block_types_post' ), 10, 2 ); 145 147 146 $allowed_block_types = get_allowed_block_types( 'post-editor' ); 148 $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); 149 $allowed_block_types = get_allowed_block_types( $post_editor_context ); 147 150 148 151 remove_filter( 'allowed_block_types', array( $this, 'filter_set_allowed_block_types_post' ) ); … … 280 283 add_filter( 'block_editor_settings_all', 'filter_block_editor_settings_my_editor', 10, 1 ); 281 284 282 $settings = get_block_editor_settings( 'my-editor' ); 285 $my_editor_context = new WP_Block_Editor_Context(); 286 $settings = get_block_editor_settings( array(), $my_editor_context ); 283 287 284 288 remove_filter( 'allowed_block_types_all', 'filter_allowed_block_types_my_editor' ); … … 307 311 add_filter( 'block_editor_settings', array( $this, 'filter_set_block_editor_settings_post' ), 10, 2 ); 308 312 309 $settings = get_block_editor_settings( 'post-editor' ); 313 $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); 314 $settings = get_block_editor_settings( array(), $post_editor_context ); 310 315 311 316 remove_filter( 'block_editor_settings', array( $this, 'filter_set_block_editor_settings_post' ) ); … … 323 328 */ 324 329 function test_block_editor_rest_api_preload_no_paths() { 325 $ context = new WP_Block_Editor_Context();326 block_editor_rest_api_preload( array(), $ context );330 $editor_context = new WP_Block_Editor_Context(); 331 block_editor_rest_api_preload( array(), $editor_context ); 327 332 328 333 $after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] ); … … 343 348 add_filter( 'block_editor_preload_paths', 'filter_remove_preload_paths', 10, 2 ); 344 349 345 $ context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );350 $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); 346 351 block_editor_rest_api_preload( 347 352 array( 348 353 array( '/wp/v2/blocks', 'OPTIONS' ), 349 354 ), 350 $ context355 $post_editor_context 351 356 ); 352 357 … … 370 375 add_filter( 'block_editor_rest_api_preload_paths', 'filter_add_preload_paths', 10, 2 ); 371 376 372 $ context = new WP_Block_Editor_Context();377 $editor_context = new WP_Block_Editor_Context(); 373 378 block_editor_rest_api_preload( 374 379 array( 375 380 array( '/wp/v2/blocks', 'OPTIONS' ), 376 381 ), 377 $ context382 $editor_context 378 383 ); 379 384
Note: See TracChangeset
for help on using the changeset viewer.