| @@ -8,73 +8,262 @@ | ||
| 8 | 8 | * @package gutenberg |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | - * Update allowed inline style attributes list. | |
| 12 | + * Adds a wp.date.setSettings with timezone abbr parameter | |
| 13 | 13 | * |
| 14 | - * Note: This should be removed when the minimum required WP version is >= 5.9. | |
| 14 | + * This can be removed when plugin support requires WordPress 5.6.0+. | |
| 15 | 15 | * |
| 16 | - * @param string[] $attrs Array of allowed CSS attributes. | |
| 17 | - * @return string[] CSS attributes. | |
| 16 | + * The script registration occurs in core wp-includes/script-loader.php | |
| 17 | + * wp_default_packages_inline_scripts() | |
| 18 | + * | |
| 19 | + * @since 8.6.0 | |
| 20 | + * | |
| 21 | + * @param WP_Scripts $scripts WP_Scripts object. | |
| 18 | 22 | */ |
| 19 | -function gutenberg_safe_style_attrs( $attrs ) { | |
| 20 | - $attrs[] = 'object-position'; | |
| 21 | - $attrs[] = 'border-top-left-radius'; | |
| 22 | - $attrs[] = 'border-top-right-radius'; | |
| 23 | - $attrs[] = 'border-bottom-right-radius'; | |
| 24 | - $attrs[] = 'border-bottom-left-radius'; | |
| 25 | - $attrs[] = 'filter'; | |
| 23 | +function gutenberg_add_date_settings_timezone( $scripts ) { | |
| 24 | + if ( ! did_action( 'init' ) ) { | |
| 25 | + return; | |
| 26 | + } | |
| 26 | 27 | |
| 27 | - return $attrs; | |
| 28 | + global $wp_locale; | |
| 29 | + | |
| 30 | + // Calculate the timezone abbr (EDT, PST) if possible. | |
| 31 | + $timezone_string = get_option( 'timezone_string', 'UTC' ); | |
| 32 | + $timezone_abbr = ''; | |
| 33 | + | |
| 34 | + if ( ! empty( $timezone_string ) ) { | |
| 35 | + $timezone_date = new DateTime( null, new DateTimeZone( $timezone_string ) ); | |
| 36 | + $timezone_abbr = $timezone_date->format( 'T' ); | |
| 37 | + } | |
| 38 | + | |
| 39 | + $scripts->add_inline_script( | |
| 40 | + 'wp-date', | |
| 41 | + sprintf( | |
| 42 | + 'wp.date.setSettings( %s );', | |
| 43 | + wp_json_encode( | |
| 44 | + array( | |
| 45 | + 'l10n' => array( | |
| 46 | + 'locale' => get_user_locale(), | |
| 47 | + 'months' => array_values( $wp_locale->month ), | |
| 48 | + 'monthsShort' => array_values( $wp_locale->month_abbrev ), | |
| 49 | + 'weekdays' => array_values( $wp_locale->weekday ), | |
| 50 | + 'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ), | |
| 51 | + 'meridiem' => (object) $wp_locale->meridiem, | |
| 52 | + 'relative' => array( | |
| 53 | + /* translators: %s: Duration. */ | |
| 54 | + 'future' => __( '%s from now', 'default' ), | |
| 55 | + /* translators: %s: Duration. */ | |
| 56 | + 'past' => __( '%s ago', 'default' ), | |
| 57 | + ), | |
| 58 | + ), | |
| 59 | + 'formats' => array( | |
| 60 | + /* translators: Time format, see https://www.php.net/date */ | |
| 61 | + 'time' => get_option( 'time_format', __( 'g:i a', 'default' ) ), | |
| 62 | + /* translators: Date format, see https://www.php.net/date */ | |
| 63 | + 'date' => get_option( 'date_format', __( 'F j, Y', 'default' ) ), | |
| 64 | + /* translators: Date/Time format, see https://www.php.net/date */ | |
| 65 | + 'datetime' => __( 'F j, Y g:i a', 'default' ), | |
| 66 | + /* translators: Abbreviated date/time format, see https://www.php.net/date */ | |
| 67 | + 'datetimeAbbreviated' => __( 'M j, Y g:i a', 'default' ), | |
| 68 | + ), | |
| 69 | + 'timezone' => array( | |
| 70 | + 'offset' => get_option( 'gmt_offset', 0 ), | |
| 71 | + 'string' => $timezone_string, | |
| 72 | + 'abbr' => $timezone_abbr, | |
| 73 | + ), | |
| 74 | + ) | |
| 75 | + ) | |
| 76 | + ), | |
| 77 | + 'after' | |
| 78 | + ); | |
| 28 | 79 | } |
| 29 | -add_filter( 'safe_style_css', 'gutenberg_safe_style_attrs' ); | |
| 80 | +add_action( 'wp_default_scripts', 'gutenberg_add_date_settings_timezone', 20 ); | |
| 30 | 81 | |
| 31 | 82 | /** |
| 32 | - * The new gallery block format is not compatible with the use_BalanceTags option | |
| 33 | - * in WP versions <= 5.8 https://core.trac.wordpress.org/ticket/54130. | |
| 34 | - * This method adds a variable to the wp namespace to indicate if the new gallery block | |
| 35 | - * 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 | |
| 37 | - * the editor store and standard flags are not loaded yet at that point | |
| 83 | + * Determine if the current theme needs to load separate block styles or not. | |
| 38 | 84 | * |
| 39 | - * @since 12.1.0 | |
| 40 | - * @todo This should be removed when the minimum required WP version is >= 5.9. | |
| 85 | + * @return bool | |
| 86 | + */ | |
| 87 | +function gutenberg_should_load_separate_block_styles() { | |
| 88 | + $load_separate_styles = gutenberg_is_fse_theme(); | |
| 89 | + /** | |
| 90 | + * Determine if separate styles will be loaded for blocks on-render or not. | |
| 91 | + * | |
| 92 | + * @param bool $load_separate_styles Whether separate styles will be loaded or not. | |
| 93 | + * | |
| 94 | + * @return bool | |
| 95 | + */ | |
| 96 | + return apply_filters( 'load_separate_block_styles', $load_separate_styles ); | |
| 97 | +} | |
| 98 | + | |
| 99 | +/** | |
| 100 | + * Remove the `wp_enqueue_registered_block_scripts_and_styles` hook if needed. | |
| 41 | 101 | * |
| 42 | - * @return void. | |
| 102 | + * @return void | |
| 43 | 103 | */ |
| 44 | -function gutenberg_check_gallery_block_v2_compatibility() { | |
| 45 | - $use_balance_tags = (int) get_option( 'use_balanceTags' ); | |
| 46 | - $v2_gallery_enabled = boolval( 1 !== $use_balance_tags || is_wp_version_compatible( '5.9' ) ) ? 'true' : 'false'; | |
| 104 | +function gutenberg_remove_hook_wp_enqueue_registered_block_scripts_and_styles() { | |
| 105 | + if ( gutenberg_should_load_separate_block_styles() ) { | |
| 106 | + /** | |
| 107 | + * Avoid enqueueing block assets of all registered blocks for all posts, instead | |
| 108 | + * deferring to block render mechanics to enqueue scripts, thereby ensuring only | |
| 109 | + * blocks of the content have their assets enqueued. | |
| 110 | + * | |
| 111 | + * This can be removed once minimum support for the plugin is outside the range | |
| 112 | + * of the version associated with closure of the following ticket. | |
| 113 | + * | |
| 114 | + * @see https://core.trac.wordpress.org/ticket/50328 | |
| 115 | + * | |
| 116 | + * @see WP_Block::render | |
| 117 | + */ | |
| 118 | + remove_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' ); | |
| 119 | + } | |
| 120 | +} | |
| 47 | 121 | |
| 48 | - wp_add_inline_script( | |
| 49 | - 'wp-dom-ready', | |
| 50 | - 'wp.galleryBlockV2Enabled = ' . $v2_gallery_enabled . ';', | |
| 51 | - 'after' | |
| 52 | - ); | |
| 122 | +add_action( 'init', 'gutenberg_remove_hook_wp_enqueue_registered_block_scripts_and_styles' ); | |
| 123 | + | |
| 124 | +/** | |
| 125 | + * Callback hooked to the register_block_type_args filter. | |
| 126 | + * | |
| 127 | + * This hooks into block registration to inject the default context into the block object. | |
| 128 | + * It can be removed once the default context is added into Core. | |
| 129 | + * | |
| 130 | + * @param array $args Block attributes. | |
| 131 | + * @return array Block attributes. | |
| 132 | + */ | |
| 133 | +function gutenberg_inject_default_block_context( $args ) { | |
| 134 | + if ( is_callable( $args['render_callback'] ) ) { | |
| 135 | + $block_render_callback = $args['render_callback']; | |
| 136 | + $args['render_callback'] = function( $attributes, $content, $block = null ) use ( $block_render_callback ) { | |
| 137 | + global $post, $wp_query; | |
| 138 | + | |
| 139 | + // Check for null for back compatibility with WP_Block_Type->render | |
| 140 | + // which is unused since the introduction of WP_Block class. | |
| 141 | + // | |
| 142 | + // See: | |
| 143 | + // - https://core.trac.wordpress.org/ticket/49927 | |
| 144 | + // - commit 910de8f6890c87f93359c6f2edc6c27b9a3f3292 at wordpress-develop. | |
| 145 | + | |
| 146 | + if ( null === $block ) { | |
| 147 | + return $block_render_callback( $attributes, $content ); | |
| 148 | + } | |
| 149 | + | |
| 150 | + $registry = WP_Block_Type_Registry::get_instance(); | |
| 151 | + $block_type = $registry->get_registered( $block->name ); | |
| 152 | + | |
| 153 | + // For WordPress versions that don't support the context API. | |
| 154 | + if ( ! $block->context ) { | |
| 155 | + $block->context = array(); | |
| 156 | + } | |
| 157 | + | |
| 158 | + // Inject the post context if not done by Core. | |
| 159 | + $needs_post_id = ! empty( $block_type->uses_context ) && in_array( 'postId', $block_type->uses_context, true ); | |
| 160 | + if ( $post instanceof WP_Post && $needs_post_id && ! isset( $block->context['postId'] ) && 'wp_template' !== $post->post_type && 'wp_template_part' !== $post->post_type ) { | |
| 161 | + $block->context['postId'] = $post->ID; | |
| 162 | + } | |
| 163 | + $needs_post_type = ! empty( $block_type->uses_context ) && in_array( 'postType', $block_type->uses_context, true ); | |
| 164 | + if ( $post instanceof WP_Post && $needs_post_type && ! isset( $block->context['postType'] ) && 'wp_template' !== $post->post_type && 'wp_template_part' !== $post->post_type ) { | |
| 165 | + /* | |
| 166 | + * The `postType` context is largely unnecessary server-side, since the | |
| 167 | + * ID is usually sufficient on its own. That being said, since a block's | |
| 168 | + * manifest is expected to be shared between the server and the client, | |
| 169 | + * it should be included to consistently fulfill the expectation. | |
| 170 | + */ | |
| 171 | + $block->context['postType'] = $post->post_type; | |
| 172 | + } | |
| 173 | + | |
| 174 | + // Inject the query context if not done by Core. | |
| 175 | + $needs_query = ! empty( $block_type->uses_context ) && in_array( 'query', $block_type->uses_context, true ); | |
| 176 | + if ( ! isset( $block->context['query'] ) && $needs_query ) { | |
| 177 | + if ( isset( $wp_query->tax_query->queried_terms['category'] ) ) { | |
| 178 | + $block->context['query'] = array( 'categoryIds' => array() ); | |
| 179 | + | |
| 180 | + foreach ( $wp_query->tax_query->queried_terms['category']['terms'] as $category_slug_or_id ) { | |
| 181 | + $block->context['query']['categoryIds'][] = 'slug' === $wp_query->tax_query->queried_terms['category']['field'] ? get_cat_ID( $category_slug_or_id ) : $category_slug_or_id; | |
| 182 | + } | |
| 183 | + } | |
| 184 | + | |
| 185 | + if ( isset( $wp_query->tax_query->queried_terms['post_tag'] ) ) { | |
| 186 | + if ( isset( $block->context['query'] ) ) { | |
| 187 | + $block->context['query']['tagIds'] = array(); | |
| 188 | + } else { | |
| 189 | + $block->context['query'] = array( 'tagIds' => array() ); | |
| 190 | + } | |
| 191 | + | |
| 192 | + foreach ( $wp_query->tax_query->queried_terms['post_tag']['terms'] as $tag_slug_or_id ) { | |
| 193 | + $tag_ID = $tag_slug_or_id; | |
| 194 | + | |
| 195 | + if ( 'slug' === $wp_query->tax_query->queried_terms['post_tag']['field'] ) { | |
| 196 | + $tag = get_term_by( 'slug', $tag_slug_or_id, 'post_tag' ); | |
| 197 | + | |
| 198 | + if ( $tag ) { | |
| 199 | + $tag_ID = $tag->term_id; | |
| 200 | + } | |
| 201 | + } | |
| 202 | + $block->context['query']['tagIds'][] = $tag_ID; | |
| 203 | + } | |
| 204 | + } | |
| 205 | + } | |
| 206 | + | |
| 207 | + return $block_render_callback( $attributes, $content, $block ); | |
| 208 | + }; | |
| 209 | + } | |
| 210 | + return $args; | |
| 53 | 211 | } |
| 54 | -add_action( 'init', 'gutenberg_check_gallery_block_v2_compatibility' ); | |
| 55 | 212 | |
| 213 | +add_filter( 'register_block_type_args', 'gutenberg_inject_default_block_context' ); | |
| 214 | + | |
| 56 | 215 | /** |
| 57 | - * Prevent use_balanceTags being enabled on WordPress 5.8 or earlier as it breaks | |
| 58 | - * the layout of the new Gallery block. | |
| 216 | + * Amends the paths to preload when initializing edit post. | |
| 59 | 217 | * |
| 60 | - * @since 12.1.0 | |
| 61 | - * @todo This should be removed when the minimum required WP version is >= 5.9. | |
| 218 | + * @see https://core.trac.wordpress.org/ticket/50606 | |
| 62 | 219 | * |
| 63 | - * @param int $new_value The new value for use_balanceTags. | |
| 220 | + * @since 8.4.0 | |
| 221 | + * | |
| 222 | + * @param array $preload_paths Default path list that will be preloaded. | |
| 223 | + * @return array Modified path list to preload. | |
| 64 | 224 | */ |
| 65 | -function gutenberg_use_balancetags_check( $new_value ) { | |
| 66 | - global $wp_version; | |
| 225 | +function gutenberg_preload_edit_post( $preload_paths ) { | |
| 226 | + $additional_paths = array( '/?context=edit' ); | |
| 227 | + return array_merge( $preload_paths, $additional_paths ); | |
| 228 | +} | |
| 67 | 229 | |
| 68 | - if ( 1 === (int) $new_value && version_compare( $wp_version, '5.9', '<' ) ) { | |
| 69 | - /* translators: %s: Minimum required version */ | |
| 70 | - $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' ); | |
| 71 | - add_settings_error( 'gutenberg_use_balancetags_check', 'gutenberg_use_balancetags_check', $message, 'error' ); | |
| 72 | - if ( class_exists( 'WP_CLI' ) ) { | |
| 73 | - WP_CLI::error( $message ); | |
| 74 | - } | |
| 75 | - return 0; | |
| 76 | - } | |
| 230 | +add_filter( 'block_editor_preload_paths', 'gutenberg_preload_edit_post' ); | |
| 77 | 231 | |
| 78 | - return $new_value; | |
| 232 | +/** | |
| 233 | + * Override post type labels for Reusable Block custom post type. | |
| 234 | + * | |
| 235 | + * This shim can be removed when the Gutenberg plugin requires a WordPress | |
| 236 | + * version that has the ticket below. | |
| 237 | + * | |
| 238 | + * @see https://core.trac.wordpress.org/ticket/50755 | |
| 239 | + * | |
| 240 | + * @since 8.6.0 | |
| 241 | + * | |
| 242 | + * @return array Array of new labels for Reusable Block post type. | |
| 243 | + */ | |
| 244 | +function gutenberg_override_reusable_block_post_type_labels() { | |
| 245 | + return array( | |
| 246 | + 'name' => _x( 'Reusable Blocks', 'post type general name', 'gutenberg' ), | |
| 247 | + 'singular_name' => _x( 'Reusable Block', 'post type singular name', 'gutenberg' ), | |
| 248 | + 'menu_name' => _x( 'Reusable Blocks', 'admin menu', 'gutenberg' ), | |
| 249 | + 'name_admin_bar' => _x( 'Reusable Block', 'add new on admin bar', 'gutenberg' ), | |
| 250 | + 'add_new' => _x( 'Add New', 'Reusable Block', 'gutenberg' ), | |
| 251 | + 'add_new_item' => __( 'Add New Reusable Block', 'gutenberg' ), | |
| 252 | + 'new_item' => __( 'New Reusable Block', 'gutenberg' ), | |
| 253 | + 'edit_item' => __( 'Edit Reusable Block', 'gutenberg' ), | |
| 254 | + 'view_item' => __( 'View Reusable Block', 'gutenberg' ), | |
| 255 | + 'all_items' => __( 'All Reusable Blocks', 'gutenberg' ), | |
| 256 | + 'search_items' => __( 'Search Reusable Blocks', 'gutenberg' ), | |
| 257 | + 'not_found' => __( 'No reusable blocks found.', 'gutenberg' ), | |
| 258 | + 'not_found_in_trash' => __( 'No reusable blocks found in Trash.', 'gutenberg' ), | |
| 259 | + 'filter_items_list' => __( 'Filter reusable blocks list', 'gutenberg' ), | |
| 260 | + 'items_list_navigation' => __( 'Reusable Blocks list navigation', 'gutenberg' ), | |
| 261 | + 'items_list' => __( 'Reusable Blocks list', 'gutenberg' ), | |
| 262 | + 'item_published' => __( 'Reusable Block published.', 'gutenberg' ), | |
| 263 | + 'item_published_privately' => __( 'Reusable Block published privately.', 'gutenberg' ), | |
| 264 | + 'item_reverted_to_draft' => __( 'Reusable Block reverted to draft.', 'gutenberg' ), | |
| 265 | + 'item_scheduled' => __( 'Reusable Block scheduled.', 'gutenberg' ), | |
| 266 | + 'item_updated' => __( 'Reusable Block updated.', 'gutenberg' ), | |
| 267 | + ); | |
| 79 | 268 | } |
| 80 | -add_filter( 'pre_update_option_use_balanceTags', 'gutenberg_use_balancetags_check' ); | |
| 269 | +add_filter( 'post_type_labels_wp_block', 'gutenberg_override_reusable_block_post_type_labels', 10, 0 ); | |