Skip to content
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

Block Themes: Add Infrastructure #1796

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
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 Nov 2, 2021
abd5e6d
Update some PHPDoc
ockham Nov 2, 2021
717bb06
Register dynamic Template Part block
ockham Nov 2, 2021
8adb467
Add template part utils
ockham Nov 2, 2021
d963be9
Add template part block stub
ockham Nov 2, 2021
02fb994
Clean up block-template-part-utils.php
ockham Nov 2, 2021
05a3b95
Bring the template part CPT and unify the template parts and template…
youknowriad Nov 3, 2021
fdb7f1d
use the renamed block_template_part function
ntsekouras Nov 3, 2021
aebe3fe
PHPCS fixes
youknowriad Nov 4, 2021
3ebd5f0
Remove trailing comma
youknowriad Nov 4, 2021
e704d28
Fix unit tests
youknowriad Nov 4, 2021
daf9f0a
More unit tests fixes
youknowriad Nov 4, 2021
a7e6604
Fix generated api test
youknowriad Nov 4, 2021
8b051d8
Add missing php function
youknowriad Nov 4, 2021
d3e4a04
Fix @since
youknowriad Nov 4, 2021
7708e19
Fix function calls
youknowriad Nov 4, 2021
b4c3780
Add @since
noisysocks Nov 8, 2021
deb4883
Add @since
noisysocks Nov 8, 2021
89be5a2
Add static keyword
noisysocks Nov 8, 2021
3d2d209
Move translators comment
noisysocks Nov 8, 2021
7d52340
Add default to doc comment
noisysocks Nov 8, 2021
0918354
Update doc comment
noisysocks Nov 8, 2021
9c268fc
Update doc comment
noisysocks Nov 8, 2021
511ff55
Add default to doc comment
noisysocks Nov 8, 2021
9926b25
Update doc comment
noisysocks Nov 8, 2021
c577604
Update doc comment
noisysocks Nov 8, 2021
3f1110a
Remove unnecessary @return void
noisysocks Nov 8, 2021
f0dc3d7
Remove unnecessary @return void
noisysocks Nov 8, 2021
4995cf1
Remove unnecessary @return void
noisysocks Nov 8, 2021
1913435
Fix @since version
noisysocks Nov 8, 2021
72dd3be
Update doc comment
noisysocks Nov 8, 2021
b6d21a5
Add is_array() check
noisysocks Nov 8, 2021
6271831
Remove unnecessary second line of doc comment
noisysocks Nov 8, 2021
5b7672c
Add extra @since
noisysocks Nov 8, 2021
cfd6368
Add extra @since
noisysocks Nov 8, 2021
ddd1377
Add extra @since
noisysocks Nov 8, 2021
3bff874
Update Template Part dynamic block to match GB
ockham Nov 8, 2021
c69cf78
Fix unit test
ockham Nov 8, 2021
f96f963
add `get_query_pagination_arrow`
ntsekouras Nov 8, 2021
30b18f8
Add template_type guards
ockham Nov 8, 2021
83a2b11
Add is_array() guards
ockham Nov 8, 2021
58d1523
Fix rebase/duplication conflict
ockham Nov 8, 2021
923a429
De-dupe get_template_parts
ockham Nov 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
733 changes: 715 additions & 18 deletions src/wp-includes/block-template-utils.php

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,8 @@ function build_query_vars_from_query_block( $block, $page ) {
*
* It's used in QueryPaginationNext and QueryPaginationPrevious blocks.
*
* @since 5.9.0
*
* @param WP_Block $block Block instance.
* @param boolean $is_next Flag for hanlding `next/previous` blocks.
*
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/blocks/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
require ABSPATH . WPINC . '/blocks/site-title.php';
require ABSPATH . WPINC . '/blocks/social-link.php';
require ABSPATH . WPINC . '/blocks/tag-cloud.php';
require ABSPATH . WPINC . '/blocks/template-part.php';

/**
* Registers core block types using metadata files.
Expand Down
155 changes: 155 additions & 0 deletions src/wp-includes/blocks/template-part.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
ockham marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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( ']]>', ']]&gt;', $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' );
35 changes: 35 additions & 0 deletions src/wp-includes/blocks/template-part/block.json
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"
}
9 changes: 9 additions & 0 deletions src/wp-includes/class-wp-block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,13 @@ class WP_Block_Template {
* @var boolean
*/
public $has_theme_file;

/**
* Whether a template is a custom template.
*
* @since 5.9.0
*
* @var boolean
*/
public $is_custom = true;
}
4 changes: 2 additions & 2 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ public function get_stylesheet( $types = array( 'variables', 'styles', 'presets'
*/
public function get_custom_templates() {
$custom_templates = array();
if ( ! isset( $this->theme_json['customTemplates'] ) ) {
if ( ! isset( $this->theme_json['customTemplates'] ) || ! is_array( $this->theme_json['customTemplates'] ) ) {
return $custom_templates;
}

Expand All @@ -625,7 +625,7 @@ public function get_custom_templates() {
*/
public function get_template_parts() {
$template_parts = array();
if ( ! isset( $this->theme_json['templateParts'] ) ) {
if ( ! isset( $this->theme_json['templateParts'] ) || ! is_array( $this->theme_json['templateParts'] ) ) {
return $template_parts;
}

Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@
// Block Templates CPT and Rendering
add_filter( 'render_block_context', '_block_template_render_without_post_block_context' );
add_filter( 'pre_wp_unique_post_slug', 'wp_filter_wp_template_unique_post_slug', 10, 5 );
add_action( 'save_post_wp_template_part', 'wp_set_unique_slug_on_create_template_part' );
add_action( 'wp_footer', 'the_block_template_skip_link' );
add_action( 'setup_theme', 'wp_enable_block_templates' );

Expand Down
58 changes: 58 additions & 0 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,64 @@ function create_initial_post_types() {
)
);

register_post_type(
'wp_template_part',
array(
'labels' => array(
'name' => __( 'Template Parts' ),
'singular_name' => __( 'Template Part' ),
'add_new' => _x( 'Add New', 'Template Part' ),
'add_new_item' => __( 'Add New Template Part' ),
'new_item' => __( 'New Template Part' ),
'edit_item' => __( 'Edit Template Part' ),
'view_item' => __( 'View Template Part' ),
'all_items' => __( 'All Template Parts' ),
'search_items' => __( 'Search Template Parts' ),
'parent_item_colon' => __( 'Parent Template Part:' ),
'not_found' => __( 'No template parts found.' ),
'not_found_in_trash' => __( 'No template parts found in Trash.' ),
'archives' => __( 'Template part archives' ),
'insert_into_item' => __( 'Insert into template part' ),
'uploaded_to_this_item' => __( 'Uploaded to this template part' ),
'filter_items_list' => __( 'Filter template parts list' ),
'items_list_navigation' => __( 'Template parts list navigation' ),
'items_list' => __( 'Template parts list' ),
),
'description' => __( 'Template parts to include in your templates.' ),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'has_archive' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_rest' => true,
'rewrite' => false,
'rest_base' => 'template-parts',
'rest_controller_class' => 'WP_REST_Templates_Controller',
'map_meta_cap' => true,
'capabilities' => array(
'create_posts' => 'edit_theme_options',
'delete_posts' => 'edit_theme_options',
'delete_others_posts' => 'edit_theme_options',
'delete_private_posts' => 'edit_theme_options',
'delete_published_posts' => 'edit_theme_options',
'edit_posts' => 'edit_theme_options',
'edit_others_posts' => 'edit_theme_options',
'edit_private_posts' => 'edit_theme_options',
'edit_published_posts' => 'edit_theme_options',
'publish_posts' => 'edit_theme_options',
'read' => 'edit_theme_options',
'read_private_posts' => 'edit_theme_options',
),
'supports' => array(
'title',
'slug',
'excerpt',
'editor',
'revisions',
),
)
);

register_post_type(
'wp_global_styles',
array(
Expand Down
Loading