| @@ -8,11 +8,175 @@ | ||
| 8 | 8 | * @package gutenberg |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | + * Backporting wp_should_load_separate_core_block_assets from WP-Core. | |
| 13 | + * | |
| 14 | + * @todo Remove this function when the minimum supported version is WordPress 5.8. | |
| 15 | + */ | |
| 16 | +if ( ! function_exists( 'wp_should_load_separate_core_block_assets' ) ) { | |
| 17 | + /** | |
| 18 | + * Checks whether separate assets should be loaded for core blocks on-render. | |
| 19 | + * | |
| 20 | + * @since 5.8.0 | |
| 21 | + * | |
| 22 | + * @return bool Whether separate assets will be loaded. | |
| 23 | + */ | |
| 24 | + function wp_should_load_separate_core_block_assets() { | |
| 25 | + if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { | |
| 26 | + return false; | |
| 27 | + } | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * Filters the flag that decides whether separate scripts and styles | |
| 31 | + * will be loaded for core blocks on-render. | |
| 32 | + * | |
| 33 | + * @since 5.8.0 | |
| 34 | + * | |
| 35 | + * @param bool $load_separate_assets Whether separate assets will be loaded. | |
| 36 | + * Default false. | |
| 37 | + */ | |
| 38 | + return apply_filters( 'should_load_separate_core_block_assets', false ); | |
| 39 | + } | |
| 40 | +} | |
| 41 | + | |
| 42 | +/** | |
| 43 | + * Opt-in to separate styles loading for block themes in WordPress 5.8. | |
| 44 | + * | |
| 45 | + * @todo Remove this function when the minimum supported version is WordPress 5.8. | |
| 46 | + */ | |
| 47 | +add_filter( | |
| 48 | + 'separate_core_block_assets', | |
| 49 | + function( $load_separate_styles ) { | |
| 50 | + if ( function_exists( 'gutenberg_is_fse_theme' ) && gutenberg_is_fse_theme() ) { | |
| 51 | + return true; | |
| 52 | + } | |
| 53 | + return $load_separate_styles; | |
| 54 | + } | |
| 55 | +); | |
| 56 | + | |
| 57 | +/** | |
| 58 | + * Remove the `wp_enqueue_registered_block_scripts_and_styles` hook if needed. | |
| 59 | + * | |
| 60 | + * @return void | |
| 61 | + */ | |
| 62 | +function gutenberg_remove_hook_wp_enqueue_registered_block_scripts_and_styles() { | |
| 63 | + if ( wp_should_load_separate_core_block_assets() ) { | |
| 64 | + /** | |
| 65 | + * Avoid enqueueing block assets of all registered blocks for all posts, instead | |
| 66 | + * deferring to block render mechanics to enqueue scripts, thereby ensuring only | |
| 67 | + * blocks of the content have their assets enqueued. | |
| 68 | + * | |
| 69 | + * This can be removed once minimum support for the plugin is outside the range | |
| 70 | + * of the version associated with closure of the following ticket. | |
| 71 | + * | |
| 72 | + * @see https://core.trac.wordpress.org/ticket/50328 | |
| 73 | + * | |
| 74 | + * @see WP_Block::render | |
| 75 | + */ | |
| 76 | + remove_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' ); | |
| 77 | + } | |
| 78 | +} | |
| 79 | + | |
| 80 | +add_action( 'init', 'gutenberg_remove_hook_wp_enqueue_registered_block_scripts_and_styles' ); | |
| 81 | + | |
| 82 | +/** | |
| 83 | + * Callback hooked to the register_block_type_args filter. | |
| 84 | + * | |
| 85 | + * This hooks into block registration to inject the default context into the block object. | |
| 86 | + * It can be removed once the default context is added into Core. | |
| 87 | + * | |
| 88 | + * @param array $args Block attributes. | |
| 89 | + * @return array Block attributes. | |
| 90 | + */ | |
| 91 | +function gutenberg_inject_default_block_context( $args ) { | |
| 92 | + if ( is_callable( $args['render_callback'] ) ) { | |
| 93 | + $block_render_callback = $args['render_callback']; | |
| 94 | + $args['render_callback'] = function( $attributes, $content, $block = null ) use ( $block_render_callback ) { | |
| 95 | + global $post; | |
| 96 | + | |
| 97 | + // Check for null for back compatibility with WP_Block_Type->render | |
| 98 | + // which is unused since the introduction of WP_Block class. | |
| 99 | + // | |
| 100 | + // See: | |
| 101 | + // - https://core.trac.wordpress.org/ticket/49927 | |
| 102 | + // - commit 910de8f6890c87f93359c6f2edc6c27b9a3f3292 at wordpress-develop. | |
| 103 | + | |
| 104 | + if ( null === $block ) { | |
| 105 | + return $block_render_callback( $attributes, $content ); | |
| 106 | + } | |
| 107 | + | |
| 108 | + $registry = WP_Block_Type_Registry::get_instance(); | |
| 109 | + $block_type = $registry->get_registered( $block->name ); | |
| 110 | + | |
| 111 | + // For WordPress versions that don't support the context API. | |
| 112 | + if ( ! $block->context ) { | |
| 113 | + $block->context = array(); | |
| 114 | + } | |
| 115 | + | |
| 116 | + // Inject the post context if not done by Core. | |
| 117 | + $needs_post_id = ! empty( $block_type->uses_context ) && in_array( 'postId', $block_type->uses_context, true ); | |
| 118 | + if ( $post instanceof WP_Post && $needs_post_id && ! isset( $block->context['postId'] ) && 'wp_template' !== $post->post_type && 'wp_template_part' !== $post->post_type ) { | |
| 119 | + $block->context['postId'] = $post->ID; | |
| 120 | + } | |
| 121 | + $needs_post_type = ! empty( $block_type->uses_context ) && in_array( 'postType', $block_type->uses_context, true ); | |
| 122 | + if ( $post instanceof WP_Post && $needs_post_type && ! isset( $block->context['postType'] ) && 'wp_template' !== $post->post_type && 'wp_template_part' !== $post->post_type ) { | |
| 123 | + /* | |
| 124 | + * The `postType` context is largely unnecessary server-side, since the | |
| 125 | + * ID is usually sufficient on its own. That being said, since a block's | |
| 126 | + * manifest is expected to be shared between the server and the client, | |
| 127 | + * it should be included to consistently fulfill the expectation. | |
| 128 | + */ | |
| 129 | + $block->context['postType'] = $post->post_type; | |
| 130 | + } | |
| 131 | + | |
| 132 | + return $block_render_callback( $attributes, $content, $block ); | |
| 133 | + }; | |
| 134 | + } | |
| 135 | + return $args; | |
| 136 | +} | |
| 137 | + | |
| 138 | +add_filter( 'register_block_type_args', 'gutenberg_inject_default_block_context' ); | |
| 139 | + | |
| 140 | +/** | |
| 141 | + * Override post type labels for Reusable Block custom post type. | |
| 142 | + * The labels are different from the ones in Core. | |
| 143 | + * | |
| 144 | + * Remove this when Core receives the new labels (minimum supported version WordPress 5.8) | |
| 145 | + * | |
| 146 | + * @return array Array of new labels for Reusable Block post type. | |
| 147 | + */ | |
| 148 | +function gutenberg_override_reusable_block_post_type_labels() { | |
| 149 | + return array( | |
| 150 | + 'name' => _x( 'Reusable blocks', 'post type general name', 'gutenberg' ), | |
| 151 | + 'singular_name' => _x( 'Reusable block', 'post type singular name', 'gutenberg' ), | |
| 152 | + 'menu_name' => _x( 'Reusable blocks', 'admin menu', 'gutenberg' ), | |
| 153 | + 'name_admin_bar' => _x( 'Reusable block', 'add new on admin bar', 'gutenberg' ), | |
| 154 | + 'add_new' => _x( 'Add New', 'Reusable block', 'gutenberg' ), | |
| 155 | + 'add_new_item' => __( 'Add new Reusable block', 'gutenberg' ), | |
| 156 | + 'new_item' => __( 'New Reusable block', 'gutenberg' ), | |
| 157 | + 'edit_item' => __( 'Edit Reusable block', 'gutenberg' ), | |
| 158 | + 'view_item' => __( 'View Reusable block', 'gutenberg' ), | |
| 159 | + 'all_items' => __( 'All Reusable blocks', 'gutenberg' ), | |
| 160 | + 'search_items' => __( 'Search Reusable blocks', 'gutenberg' ), | |
| 161 | + 'not_found' => __( 'No reusable blocks found.', 'gutenberg' ), | |
| 162 | + 'not_found_in_trash' => __( 'No reusable blocks found in Trash.', 'gutenberg' ), | |
| 163 | + 'filter_items_list' => __( 'Filter reusable blocks list', 'gutenberg' ), | |
| 164 | + 'items_list_navigation' => __( 'Reusable blocks list navigation', 'gutenberg' ), | |
| 165 | + 'items_list' => __( 'Reusable blocks list', 'gutenberg' ), | |
| 166 | + 'item_published' => __( 'Reusable block published.', 'gutenberg' ), | |
| 167 | + 'item_published_privately' => __( 'Reusable block published privately.', 'gutenberg' ), | |
| 168 | + 'item_reverted_to_draft' => __( 'Reusable block reverted to draft.', 'gutenberg' ), | |
| 169 | + 'item_scheduled' => __( 'Reusable block scheduled.', 'gutenberg' ), | |
| 170 | + 'item_updated' => __( 'Reusable block updated.', 'gutenberg' ), | |
| 171 | + ); | |
| 172 | +} | |
| 173 | +add_filter( 'post_type_labels_wp_block', 'gutenberg_override_reusable_block_post_type_labels', 10, 0 ); | |
| 174 | + | |
| 175 | +/** | |
| 12 | 176 | * Update allowed inline style attributes list. |
| 13 | 177 | * |
| 14 | - * Note: This should be removed when the minimum required WP version is >= 5.9. | |
| 178 | + * Note: This should be removed when the minimum required WP version is >= 5.8. | |
| 15 | 179 | * |
| 16 | 180 | * @param string[] $attrs Array of allowed CSS attributes. |
| 17 | 181 | * @return string[] CSS attributes. |
| 18 | 182 | */ |
| @@ -32,9 +196,9 @@ | ||
| 32 | 196 | * The new gallery block format is not compatible with the use_BalanceTags option |
| 33 | 197 | * in WP versions <= 5.8 https://core.trac.wordpress.org/ticket/54130. |
| 34 | 198 | * This method adds a variable to the wp namespace to indicate if the new gallery block |
| 35 | 199 | * format can be enabled or not. It needs to be added this early and to the wp namespace |
| 36 | - * as it needs to be available when the initial block parsing runs on editor load, and most of | |
| 200 | + * as it needs to be available when the intial block parsing runs on editor load, and most of | |
| 37 | 201 | * the editor store and standard flags are not loaded yet at that point |
| 38 | 202 | * |
| 39 | 203 | * @since 12.1.0 |
| 40 | 204 | * @todo This should be removed when the minimum required WP version is >= 5.9. |