| @@ -8,42 +8,237 @@ | ||
| 8 | 8 | * @package gutenberg |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | - * Filters allowed CSS attributes to include `flex-basis`, included in saved | |
| 13 | - * markup of the Column block. | |
| 12 | + * Backporting wp_should_load_separate_core_block_assets from WP-Core. | |
| 14 | 13 | * |
| 15 | - * This can be removed when plugin support requires WordPress 5.3.0+. | |
| 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. | |
| 16 | 44 | * |
| 17 | - * @see https://core.trac.wordpress.org/ticket/47281 | |
| 18 | - * @see https://core.trac.wordpress.org/changeset/45363 | |
| 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. | |
| 19 | 59 | * |
| 20 | - * @since 5.7.0 | |
| 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. | |
| 21 | 84 | * |
| 22 | - * @param string[] $attr Array of allowed CSS attributes. | |
| 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. | |
| 23 | 87 | * |
| 24 | - * @return string[] Filtered array of allowed CSS attributes. | |
| 88 | + * @param array $args Block attributes. | |
| 89 | + * @return array Block attributes. | |
| 25 | 90 | */ |
| 26 | -function gutenberg_safe_style_css_column_flex_basis( $attr ) { | |
| 27 | - $attr[] = 'flex-basis'; | |
| 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; | |
| 28 | 96 | |
| 29 | - return $attr; | |
| 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; | |
| 30 | 136 | } |
| 31 | -add_filter( 'safe_style_css', 'gutenberg_safe_style_css_column_flex_basis' ); | |
| 32 | 137 | |
| 138 | +add_filter( 'register_block_type_args', 'gutenberg_inject_default_block_context' ); | |
| 139 | + | |
| 33 | 140 | /** |
| 34 | - * Sets the current post for usage in template blocks. | |
| 141 | + * Override post type labels for Reusable Block custom post type. | |
| 142 | + * The labels are different from the ones in Core. | |
| 35 | 143 | * |
| 36 | - * @return WP_Post|null The post if any, or null otherwise. | |
| 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. | |
| 37 | 147 | */ |
| 38 | -function gutenberg_get_post_from_context() { | |
| 39 | - // TODO: Without this temporary fix, an infinite loop can occur where | |
| 40 | - // posts with post content blocks render themselves recursively. | |
| 41 | - if ( is_admin() || defined( 'REST_REQUEST' ) ) { | |
| 42 | - return null; | |
| 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 | +/** | |
| 176 | + * Update allowed inline style attributes list. | |
| 177 | + * | |
| 178 | + * Note: This should be removed when the minimum required WP version is >= 5.8. | |
| 179 | + * | |
| 180 | + * @param string[] $attrs Array of allowed CSS attributes. | |
| 181 | + * @return string[] CSS attributes. | |
| 182 | + */ | |
| 183 | +function gutenberg_safe_style_attrs( $attrs ) { | |
| 184 | + $attrs[] = 'object-position'; | |
| 185 | + $attrs[] = 'border-top-left-radius'; | |
| 186 | + $attrs[] = 'border-top-right-radius'; | |
| 187 | + $attrs[] = 'border-bottom-right-radius'; | |
| 188 | + $attrs[] = 'border-bottom-left-radius'; | |
| 189 | + $attrs[] = 'filter'; | |
| 190 | + | |
| 191 | + return $attrs; | |
| 192 | +} | |
| 193 | +add_filter( 'safe_style_css', 'gutenberg_safe_style_attrs' ); | |
| 194 | + | |
| 195 | +/** | |
| 196 | + * The new gallery block format is not compatible with the use_BalanceTags option | |
| 197 | + * in WP versions <= 5.8 https://core.trac.wordpress.org/ticket/54130. | |
| 198 | + * This method adds a variable to the wp namespace to indicate if the new gallery block | |
| 199 | + * format can be enabled or not. It needs to be added this early and to the wp namespace | |
| 200 | + * as it needs to be available when the intial block parsing runs on editor load, and most of | |
| 201 | + * the editor store and standard flags are not loaded yet at that point | |
| 202 | + * | |
| 203 | + * @since 12.1.0 | |
| 204 | + * @todo This should be removed when the minimum required WP version is >= 5.9. | |
| 205 | + * | |
| 206 | + * @return void. | |
| 207 | + */ | |
| 208 | +function gutenberg_check_gallery_block_v2_compatibility() { | |
| 209 | + $use_balance_tags = (int) get_option( 'use_balanceTags' ); | |
| 210 | + $v2_gallery_enabled = boolval( 1 !== $use_balance_tags || is_wp_version_compatible( '5.9' ) ) ? 'true' : 'false'; | |
| 211 | + | |
| 212 | + wp_add_inline_script( | |
| 213 | + 'wp-dom-ready', | |
| 214 | + 'wp.galleryBlockV2Enabled = ' . $v2_gallery_enabled . ';', | |
| 215 | + 'after' | |
| 216 | + ); | |
| 217 | +} | |
| 218 | +add_action( 'init', 'gutenberg_check_gallery_block_v2_compatibility' ); | |
| 219 | + | |
| 220 | +/** | |
| 221 | + * Prevent use_balanceTags being enabled on WordPress 5.8 or earlier as it breaks | |
| 222 | + * the layout of the new Gallery block. | |
| 223 | + * | |
| 224 | + * @since 12.1.0 | |
| 225 | + * @todo This should be removed when the minimum required WP version is >= 5.9. | |
| 226 | + * | |
| 227 | + * @param int $new_value The new value for use_balanceTags. | |
| 228 | + */ | |
| 229 | +function gutenberg_use_balancetags_check( $new_value ) { | |
| 230 | + global $wp_version; | |
| 231 | + | |
| 232 | + if ( 1 === (int) $new_value && version_compare( $wp_version, '5.9', '<' ) ) { | |
| 233 | + /* translators: %s: Minimum required version */ | |
| 234 | + $message = sprintf( __( 'Gutenberg requires WordPress %s or later in order to enable the “Correct invalidly nested XHTML automatically” option. Please upgrade WordPress before enabling.', 'gutenberg' ), '5.9' ); | |
| 235 | + add_settings_error( 'gutenberg_use_balancetags_check', 'gutenberg_use_balancetags_check', $message, 'error' ); | |
| 236 | + if ( class_exists( 'WP_CLI' ) ) { | |
| 237 | + WP_CLI::error( $message ); | |
| 238 | + } | |
| 239 | + return 0; | |
| 43 | 240 | } |
| 44 | - if ( ! in_the_loop() ) { | |
| 45 | - rewind_posts(); | |
| 46 | - the_post(); | |
| 47 | - } | |
| 48 | - return get_post(); | |
| 241 | + | |
| 242 | + return $new_value; | |
| 49 | 243 | } |
| 244 | +add_filter( 'pre_update_option_use_balanceTags', 'gutenberg_use_balancetags_check' ); | |