-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Block Themes: Add Infrastructure #1796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
ockham
wants to merge
43
commits into
WordPress:trunk
from
ockham:add/block-template-theme-infrastructure
Closed
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
c344a98
Block Themes: Add Infrastructure
ockham abd5e6d
Update some PHPDoc
ockham 717bb06
Register dynamic Template Part block
ockham 8adb467
Add template part utils
ockham d963be9
Add template part block stub
ockham 02fb994
Clean up block-template-part-utils.php
ockham 05a3b95
Bring the template part CPT and unify the template parts and template…
youknowriad fdb7f1d
use the renamed block_template_part function
ntsekouras aebe3fe
PHPCS fixes
youknowriad 3ebd5f0
Remove trailing comma
youknowriad e704d28
Fix unit tests
youknowriad daf9f0a
More unit tests fixes
youknowriad a7e6604
Fix generated api test
youknowriad 8b051d8
Add missing php function
youknowriad d3e4a04
Fix @since
youknowriad 7708e19
Fix function calls
youknowriad b4c3780
Add @since
noisysocks deb4883
Add @since
noisysocks 89be5a2
Add static keyword
noisysocks 3d2d209
Move translators comment
noisysocks 7d52340
Add default to doc comment
noisysocks 0918354
Update doc comment
noisysocks 9c268fc
Update doc comment
noisysocks 511ff55
Add default to doc comment
noisysocks 9926b25
Update doc comment
noisysocks c577604
Update doc comment
noisysocks 3f1110a
Remove unnecessary @return void
noisysocks f0dc3d7
Remove unnecessary @return void
noisysocks 4995cf1
Remove unnecessary @return void
noisysocks 1913435
Fix @since version
noisysocks 72dd3be
Update doc comment
noisysocks b6d21a5
Add is_array() check
noisysocks 6271831
Remove unnecessary second line of doc comment
noisysocks 5b7672c
Add extra @since
noisysocks cfd6368
Add extra @since
noisysocks ddd1377
Add extra @since
noisysocks 3bff874
Update Template Part dynamic block to match GB
ockham c69cf78
Fix unit test
ockham f96f963
add `get_query_pagination_arrow`
ntsekouras 30b18f8
Add template_type guards
ockham 83a2b11
Add is_array() guards
ockham 58d1523
Fix rebase/duplication conflict
ockham 923a429
De-dupe get_template_parts
ockham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| <?php | ||
| /** | ||
| * Server-side rendering of the `core/template-part` block. | ||
| * | ||
| * @package WordPress | ||
| */ | ||
|
|
||
| /** | ||
| * Renders the `core/template-part` block on the server. | ||
| * | ||
| * @param array $attributes The block attributes. | ||
| * | ||
| * @return string The render. | ||
| */ | ||
ockham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| function render_block_core_template_part( $attributes ) { | ||
| static $seen_ids = array(); | ||
|
|
||
| $template_part_id = null; | ||
| $content = null; | ||
| $area = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; | ||
|
|
||
| if ( | ||
| isset( $attributes['slug'] ) && | ||
| isset( $attributes['theme'] ) && | ||
| wp_get_theme()->get_stylesheet() === $attributes['theme'] | ||
| ) { | ||
| $template_part_id = $attributes['theme'] . '//' . $attributes['slug']; | ||
| $template_part_query = new WP_Query( | ||
| array( | ||
| 'post_type' => 'wp_template_part', | ||
| 'post_status' => 'publish', | ||
| 'post_name__in' => array( $attributes['slug'] ), | ||
| 'tax_query' => array( | ||
| array( | ||
| 'taxonomy' => 'wp_theme', | ||
| 'field' => 'slug', | ||
| 'terms' => $attributes['theme'], | ||
| ), | ||
| ), | ||
| 'posts_per_page' => 1, | ||
| 'no_found_rows' => true, | ||
| ) | ||
| ); | ||
| $template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null; | ||
| if ( $template_part_post ) { | ||
| // A published post might already exist if this template part was customized elsewhere | ||
| // or if it's part of a customized template. | ||
| $content = $template_part_post->post_content; | ||
| $area_terms = get_the_terms( $template_part_post, 'wp_template_part_area' ); | ||
| if ( ! is_wp_error( $area_terms ) && false !== $area_terms ) { | ||
| $area = $area_terms[0]->name; | ||
| } | ||
| } else { | ||
| // Else, if the template part was provided by the active theme, | ||
| // render the corresponding file content. | ||
| $template_part_file_path = get_theme_file_path( '/block-template-parts/' . $attributes['slug'] . '.html' ); | ||
| if ( 0 === validate_file( $attributes['slug'] ) && file_exists( $template_part_file_path ) ) { | ||
| $content = _inject_theme_attribute_in_block_template_content( file_get_contents( $template_part_file_path ) ); | ||
ockham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| if ( is_null( $content ) && is_user_logged_in() ) { | ||
| if ( ! isset( $attributes['slug'] ) ) { | ||
| // If there is no slug this is a placeholder and we dont want to return any message. | ||
| return; | ||
| } | ||
| return sprintf( | ||
| /* translators: %s: Template part slug. */ | ||
| __( 'Template part has been deleted or is unavailable: %s' ), | ||
| $attributes['slug'] | ||
| ); | ||
| } | ||
|
|
||
| if ( isset( $seen_ids[ $template_part_id ] ) ) { | ||
| // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent | ||
| // is set in `wp_debug_mode()`. | ||
| $is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG && | ||
| defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY; | ||
|
|
||
| return $is_debug ? | ||
| // translators: Visible only in the front end, this warning takes the place of a faulty block. | ||
| __( '[block rendering halted]' ) : | ||
| ''; | ||
| } | ||
|
|
||
| // Run through the actions that are typically taken on the_content. | ||
| $seen_ids[ $template_part_id ] = true; | ||
| $content = do_blocks( $content ); | ||
| unset( $seen_ids[ $template_part_id ] ); | ||
| $content = wptexturize( $content ); | ||
| $content = convert_smilies( $content ); | ||
| $content = shortcode_unautop( $content ); | ||
| $content = wp_filter_content_tags( $content ); | ||
| $content = do_shortcode( $content ); | ||
|
|
||
| // Handle embeds for block template parts. | ||
| global $wp_embed; | ||
| $content = $wp_embed->autoembed( $content ); | ||
|
|
||
| if ( empty( $attributes['tagName'] ) ) { | ||
| $defined_areas = get_allowed_block_template_part_areas(); | ||
| $area_tag = 'div'; | ||
| foreach ( $defined_areas as $defined_area ) { | ||
| if ( $defined_area['area'] === $area && isset( $defined_area['area_tag'] ) ) { | ||
| $area_tag = $defined_area['area_tag']; | ||
| } | ||
| } | ||
| $html_tag = $area_tag; | ||
| } else { | ||
| $html_tag = esc_attr( $attributes['tagName'] ); | ||
| } | ||
| $wrapper_attributes = get_block_wrapper_attributes(); | ||
|
|
||
| return "<$html_tag $wrapper_attributes>" . str_replace( ']]>', ']]>', $content ) . "</$html_tag>"; | ||
| } | ||
|
|
||
| /** | ||
| * Returns an array of variation objects for the template part block. | ||
| * | ||
| * @return array Array containing the block variation objects. | ||
| */ | ||
ockham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| function build_template_part_block_variations() { | ||
| $variations = array(); | ||
| $defined_areas = get_allowed_block_template_part_areas(); | ||
| foreach ( $defined_areas as $area ) { | ||
| if ( 'uncategorized' !== $area['area'] ) { | ||
| $variations[] = array( | ||
| 'name' => $area['area'], | ||
| 'title' => $area['label'], | ||
| 'description' => $area['description'], | ||
| 'attributes' => array( | ||
| 'area' => $area['area'], | ||
| ), | ||
| 'scope' => array( 'inserter' ), | ||
| 'icon' => $area['icon'], | ||
| ); | ||
| } | ||
| } | ||
| return $variations; | ||
| } | ||
|
|
||
| /** | ||
| * Registers the `core/template-part` block on the server. | ||
| */ | ||
ockham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| function register_block_core_template_part() { | ||
| register_block_type_from_metadata( | ||
| __DIR__ . '/template-part', | ||
| array( | ||
| 'render_callback' => 'render_block_core_template_part', | ||
| 'variations' => build_template_part_block_variations(), | ||
| ) | ||
| ); | ||
| } | ||
| add_action( 'init', 'register_block_core_template_part' ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| { | ||
| "apiVersion": 2, | ||
| "name": "core/template-part", | ||
| "title": "Template Part", | ||
| "category": "theme", | ||
| "description": "Edit the different global regions of your site, like the header, footer, sidebar, or create your own.", | ||
| "textdomain": "default", | ||
| "attributes": { | ||
| "slug": { | ||
| "type": "string" | ||
| }, | ||
| "theme": { | ||
| "type": "string" | ||
| }, | ||
| "tagName": { | ||
| "type": "string" | ||
| }, | ||
| "area": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "supports": { | ||
| "align": true, | ||
| "html": false, | ||
| "color": { | ||
| "gradients": true, | ||
| "link": true | ||
| }, | ||
| "spacing": { | ||
| "padding": true | ||
| }, | ||
| "__experimentalLayout": true | ||
| }, | ||
| "editorStyle": "wp-block-template-part-editor" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.