| 1 |
<?php |
| 2 |
/** |
| 3 |
* Temporary compatibility shims for features present in Gutenberg, pending |
| 4 |
* upstream commit to the WordPress core source repository. Functions here |
| 5 |
* exist only as long as necessary for corresponding WordPress support, and |
| 6 |
* each should be associated with a Trac ticket. |
| 7 |
* |
| 8 |
* @package gutenberg |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* Determine if the current theme needs to load separate block styles or not. |
| 13 |
* |
| 14 |
* @return bool |
| 15 |
*/ |
| 16 |
function gutenberg_should_load_separate_block_assets() { |
| 17 |
$load_separate_styles = gutenberg_is_fse_theme(); |
| 18 |
/** |
| 19 |
* Determine if separate styles will be loaded for blocks on-render or not. |
| 20 |
* |
| 21 |
* @param bool $load_separate_styles Whether separate styles will be loaded or not. |
| 22 |
* |
| 23 |
* @return bool |
| 24 |
*/ |
| 25 |
return apply_filters( 'load_separate_block_assets', $load_separate_styles ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Remove the `wp_enqueue_registered_block_scripts_and_styles` hook if needed. |
| 30 |
* |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
function gutenberg_remove_hook_wp_enqueue_registered_block_scripts_and_styles() { |
| 34 |
if ( gutenberg_should_load_separate_block_assets() ) { |
| 35 |
/** |
| 36 |
* Avoid enqueueing block assets of all registered blocks for all posts, instead |
| 37 |
* deferring to block render mechanics to enqueue scripts, thereby ensuring only |
| 38 |
* blocks of the content have their assets enqueued. |
| 39 |
* |
| 40 |
* This can be removed once minimum support for the plugin is outside the range |
| 41 |
* of the version associated with closure of the following ticket. |
| 42 |
* |
| 43 |
* @see https://core.trac.wordpress.org/ticket/50328 |
| 44 |
* |
| 45 |
* @see WP_Block::render |
| 46 |
*/ |
| 47 |
remove_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
add_action( 'init', 'gutenberg_remove_hook_wp_enqueue_registered_block_scripts_and_styles' ); |
| 52 |
|
| 53 |
/** |
| 54 |
* Callback hooked to the register_block_type_args filter. |
| 55 |
* |
| 56 |
* This hooks into block registration to inject the default context into the block object. |
| 57 |
* It can be removed once the default context is added into Core. |
| 58 |
* |
| 59 |
* @param array $args Block attributes. |
| 60 |
* @return array Block attributes. |
| 61 |
*/ |
| 62 |
function gutenberg_inject_default_block_context( $args ) { |
| 63 |
if ( is_callable( $args['render_callback'] ) ) { |
| 64 |
$block_render_callback = $args['render_callback']; |
| 65 |
$args['render_callback'] = function( $attributes, $content, $block = null ) use ( $block_render_callback ) { |
| 66 |
global $post, $wp_query; |
| 67 |
|
| 68 |
// Check for null for back compatibility with WP_Block_Type->render |
| 69 |
// which is unused since the introduction of WP_Block class. |
| 70 |
// |
| 71 |
// See: |
| 72 |
// - https://core.trac.wordpress.org/ticket/49927 |
| 73 |
// - commit 910de8f6890c87f93359c6f2edc6c27b9a3f3292 at wordpress-develop. |
| 74 |
|
| 75 |
if ( null === $block ) { |
| 76 |
return $block_render_callback( $attributes, $content ); |
| 77 |
} |
| 78 |
|
| 79 |
$registry = WP_Block_Type_Registry::get_instance(); |
| 80 |
$block_type = $registry->get_registered( $block->name ); |
| 81 |
|
| 82 |
// For WordPress versions that don't support the context API. |
| 83 |
if ( ! $block->context ) { |
| 84 |
$block->context = array(); |
| 85 |
} |
| 86 |
|
| 87 |
// Inject the post context if not done by Core. |
| 88 |
$needs_post_id = ! empty( $block_type->uses_context ) && in_array( 'postId', $block_type->uses_context, true ); |
| 89 |
if ( $post instanceof WP_Post && $needs_post_id && ! isset( $block->context['postId'] ) && 'wp_template' !== $post->post_type && 'wp_template_part' !== $post->post_type ) { |
| 90 |
$block->context['postId'] = $post->ID; |
| 91 |
} |
| 92 |
$needs_post_type = ! empty( $block_type->uses_context ) && in_array( 'postType', $block_type->uses_context, true ); |
| 93 |
if ( $post instanceof WP_Post && $needs_post_type && ! isset( $block->context['postType'] ) && 'wp_template' !== $post->post_type && 'wp_template_part' !== $post->post_type ) { |
| 94 |
/* |
| 95 |
* The `postType` context is largely unnecessary server-side, since the |
| 96 |
* ID is usually sufficient on its own. That being said, since a block's |
| 97 |
* manifest is expected to be shared between the server and the client, |
| 98 |
* it should be included to consistently fulfill the expectation. |
| 99 |
*/ |
| 100 |
$block->context['postType'] = $post->post_type; |
| 101 |
} |
| 102 |
|
| 103 |
// Inject the query context if not done by Core. |
| 104 |
$needs_query = ! empty( $block_type->uses_context ) && in_array( 'query', $block_type->uses_context, true ); |
| 105 |
if ( ! isset( $block->context['query'] ) && $needs_query ) { |
| 106 |
if ( isset( $wp_query->tax_query->queried_terms['category'] ) ) { |
| 107 |
$block->context['query'] = array( 'categoryIds' => array() ); |
| 108 |
|
| 109 |
foreach ( $wp_query->tax_query->queried_terms['category']['terms'] as $category_slug_or_id ) { |
| 110 |
$block->context['query']['categoryIds'][] = 'slug' === $wp_query->tax_query->queried_terms['category']['field'] ? get_cat_ID( $category_slug_or_id ) : $category_slug_or_id; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if ( isset( $wp_query->tax_query->queried_terms['post_tag'] ) ) { |
| 115 |
if ( isset( $block->context['query'] ) ) { |
| 116 |
$block->context['query']['tagIds'] = array(); |
| 117 |
} else { |
| 118 |
$block->context['query'] = array( 'tagIds' => array() ); |
| 119 |
} |
| 120 |
|
| 121 |
foreach ( $wp_query->tax_query->queried_terms['post_tag']['terms'] as $tag_slug_or_id ) { |
| 122 |
$tag_ID = $tag_slug_or_id; |
| 123 |
|
| 124 |
if ( 'slug' === $wp_query->tax_query->queried_terms['post_tag']['field'] ) { |
| 125 |
$tag = get_term_by( 'slug', $tag_slug_or_id, 'post_tag' ); |
| 126 |
|
| 127 |
if ( $tag ) { |
| 128 |
$tag_ID = $tag->term_id; |
| 129 |
} |
| 130 |
} |
| 131 |
$block->context['query']['tagIds'][] = $tag_ID; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return $block_render_callback( $attributes, $content, $block ); |
| 137 |
}; |
| 138 |
} |
| 139 |
return $args; |
| 140 |
} |
| 141 |
|
| 142 |
add_filter( 'register_block_type_args', 'gutenberg_inject_default_block_context' ); |
| 143 |
|
| 144 |
/** |
| 145 |
* Amends the paths to preload when initializing edit post. |
| 146 |
* |
| 147 |
* @see https://core.trac.wordpress.org/ticket/50606 |
| 148 |
* |
| 149 |
* @since 8.4.0 |
| 150 |
* |
| 151 |
* @param array $preload_paths Default path list that will be preloaded. |
| 152 |
* @return array Modified path list to preload. |
| 153 |
*/ |
| 154 |
function gutenberg_preload_edit_post( $preload_paths ) { |
| 155 |
$additional_paths = array( '/?context=edit' ); |
| 156 |
return array_merge( $preload_paths, $additional_paths ); |
| 157 |
} |
| 158 |
|
| 159 |
add_filter( 'block_editor_preload_paths', 'gutenberg_preload_edit_post' ); |
| 160 |
|
| 161 |
/** |
| 162 |
* Override post type labels for Reusable Block custom post type. |
| 163 |
* The labels are different from the ones in Core. |
| 164 |
* |
| 165 |
* Remove this when Core receives the new labels (minimum supported version WordPress 5.8) |
| 166 |
* |
| 167 |
* @return array Array of new labels for Reusable Block post type. |
| 168 |
*/ |
| 169 |
function gutenberg_override_reusable_block_post_type_labels() { |
| 170 |
return array( |
| 171 |
'name' => _x( 'Reusable blocks', 'post type general name', 'gutenberg' ), |
| 172 |
'singular_name' => _x( 'Reusable block', 'post type singular name', 'gutenberg' ), |
| 173 |
'menu_name' => _x( 'Reusable blocks', 'admin menu', 'gutenberg' ), |
| 174 |
'name_admin_bar' => _x( 'Reusable block', 'add new on admin bar', 'gutenberg' ), |
| 175 |
'add_new' => _x( 'Add New', 'Reusable block', 'gutenberg' ), |
| 176 |
'add_new_item' => __( 'Add new Reusable block', 'gutenberg' ), |
| 177 |
'new_item' => __( 'New Reusable block', 'gutenberg' ), |
| 178 |
'edit_item' => __( 'Edit Reusable block', 'gutenberg' ), |
| 179 |
'view_item' => __( 'View Reusable block', 'gutenberg' ), |
| 180 |
'all_items' => __( 'All Reusable blocks', 'gutenberg' ), |
| 181 |
'search_items' => __( 'Search Reusable blocks', 'gutenberg' ), |
| 182 |
'not_found' => __( 'No reusable blocks found.', 'gutenberg' ), |
| 183 |
'not_found_in_trash' => __( 'No reusable blocks found in Trash.', 'gutenberg' ), |
| 184 |
'filter_items_list' => __( 'Filter reusable blocks list', 'gutenberg' ), |
| 185 |
'items_list_navigation' => __( 'Reusable blocks list navigation', 'gutenberg' ), |
| 186 |
'items_list' => __( 'Reusable blocks list', 'gutenberg' ), |
| 187 |
'item_published' => __( 'Reusable block published.', 'gutenberg' ), |
| 188 |
'item_published_privately' => __( 'Reusable block published privately.', 'gutenberg' ), |
| 189 |
'item_reverted_to_draft' => __( 'Reusable block reverted to draft.', 'gutenberg' ), |
| 190 |
'item_scheduled' => __( 'Reusable block scheduled.', 'gutenberg' ), |
| 191 |
'item_updated' => __( 'Reusable block updated.', 'gutenberg' ), |
| 192 |
); |
| 193 |
} |
| 194 |
add_filter( 'post_type_labels_wp_block', 'gutenberg_override_reusable_block_post_type_labels', 10, 0 ); |
| 195 |
|