| 1 |
<?php |
| 2 |
/** |
| 3 |
* Block template part functions. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Registers block editor 'wp_template_part' post type. |
| 10 |
*/ |
| 11 |
function gutenberg_register_template_part_post_type() { |
| 12 |
if ( ! gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing' ) ) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
$labels = array( |
| 17 |
'name' => __( 'Template Parts', 'gutenberg' ), |
| 18 |
'singular_name' => __( 'Template Part', 'gutenberg' ), |
| 19 |
'menu_name' => _x( 'Template Parts', 'Admin Menu text', 'gutenberg' ), |
| 20 |
'add_new' => _x( 'Add New', 'Template Part', 'gutenberg' ), |
| 21 |
'add_new_item' => __( 'Add New Template Part', 'gutenberg' ), |
| 22 |
'new_item' => __( 'New Template Part', 'gutenberg' ), |
| 23 |
'edit_item' => __( 'Edit Template Part', 'gutenberg' ), |
| 24 |
'view_item' => __( 'View Template Part', 'gutenberg' ), |
| 25 |
'all_items' => __( 'All Template Parts', 'gutenberg' ), |
| 26 |
'search_items' => __( 'Search Template Parts', 'gutenberg' ), |
| 27 |
'parent_item_colon' => __( 'Parent Template Part:', 'gutenberg' ), |
| 28 |
'not_found' => __( 'No template parts found.', 'gutenberg' ), |
| 29 |
'not_found_in_trash' => __( 'No template parts found in Trash.', 'gutenberg' ), |
| 30 |
'archives' => __( 'Template part archives', 'gutenberg' ), |
| 31 |
'insert_into_item' => __( 'Insert into template part', 'gutenberg' ), |
| 32 |
'uploaded_to_this_item' => __( 'Uploaded to this template part', 'gutenberg' ), |
| 33 |
'filter_items_list' => __( 'Filter template parts list', 'gutenberg' ), |
| 34 |
'items_list_navigation' => __( 'Template parts list navigation', 'gutenberg' ), |
| 35 |
'items_list' => __( 'Template parts list', 'gutenberg' ), |
| 36 |
); |
| 37 |
|
| 38 |
$args = array( |
| 39 |
'labels' => $labels, |
| 40 |
'description' => __( 'Template parts to include in your templates.', 'gutenberg' ), |
| 41 |
'public' => false, |
| 42 |
'has_archive' => false, |
| 43 |
'show_ui' => true, |
| 44 |
'show_in_menu' => 'themes.php', |
| 45 |
'show_in_admin_bar' => false, |
| 46 |
'show_in_rest' => true, |
| 47 |
'rest_base' => 'template-parts', |
| 48 |
'map_meta_cap' => true, |
| 49 |
'supports' => array( |
| 50 |
'title', |
| 51 |
'slug', |
| 52 |
'editor', |
| 53 |
'revisions', |
| 54 |
'custom-fields', |
| 55 |
), |
| 56 |
); |
| 57 |
|
| 58 |
$meta_args = array( |
| 59 |
'object_subtype' => 'wp_template_part', |
| 60 |
'type' => 'string', |
| 61 |
'description' => 'The theme that provided the template part, if any.', |
| 62 |
'single' => true, |
| 63 |
'show_in_rest' => true, |
| 64 |
); |
| 65 |
|
| 66 |
register_post_type( 'wp_template_part', $args ); |
| 67 |
register_meta( 'post', 'theme', $meta_args ); |
| 68 |
} |
| 69 |
add_action( 'init', 'gutenberg_register_template_part_post_type' ); |
| 70 |
|
| 71 |
/** |
| 72 |
* Filters `wp_template_part` posts slug resolution to bypass deduplication logic as |
| 73 |
* template part slugs should be unique. |
| 74 |
* |
| 75 |
* @param string $slug The resolved slug (post_name). |
| 76 |
* @param int $post_ID Post ID. |
| 77 |
* @param string $post_status No uniqueness checks are made if the post is still draft or pending. |
| 78 |
* @param string $post_type Post type. |
| 79 |
* @param int $post_parent Post parent ID. |
| 80 |
* @param int $original_slug The desired slug (post_name). |
| 81 |
* @return string The original, desired slug. |
| 82 |
*/ |
| 83 |
function gutenberg_filter_wp_template_part_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) { |
| 84 |
if ( 'wp_template_part' === $post_type ) { |
| 85 |
return $original_slug; |
| 86 |
} |
| 87 |
return $slug; |
| 88 |
} |
| 89 |
add_filter( 'wp_unique_post_slug', 'gutenberg_filter_wp_template_part_wp_unique_post_slug', 10, 6 ); |
| 90 |
|
| 91 |
/** |
| 92 |
* Fixes the label of the 'wp_template_part' admin menu entry. |
| 93 |
*/ |
| 94 |
function gutenberg_fix_template_part_admin_menu_entry() { |
| 95 |
global $submenu; |
| 96 |
if ( ! isset( $submenu['themes.php'] ) ) { |
| 97 |
return; |
| 98 |
} |
| 99 |
$post_type = get_post_type_object( 'wp_template_part' ); |
| 100 |
if ( ! $post_type ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
foreach ( $submenu['themes.php'] as $key => $submenu_entry ) { |
| 104 |
if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) { |
| 105 |
$submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride |
| 106 |
break; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
add_action( 'admin_menu', 'gutenberg_fix_template_part_admin_menu_entry' ); |
| 111 |
|
| 112 |
/** |
| 113 |
* Filters the 'wp_template_part' post type columns in the admin list table. |
| 114 |
* |
| 115 |
* @param array $columns Columns to display. |
| 116 |
* @return array Filtered $columns. |
| 117 |
*/ |
| 118 |
function gutenberg_filter_template_part_list_table_columns( array $columns ) { |
| 119 |
$columns['slug'] = __( 'Slug', 'gutenberg' ); |
| 120 |
if ( isset( $columns['date'] ) ) { |
| 121 |
unset( $columns['date'] ); |
| 122 |
} |
| 123 |
return $columns; |
| 124 |
} |
| 125 |
add_filter( 'manage_wp_template_part_posts_columns', 'gutenberg_filter_template_part_list_table_columns' ); |
| 126 |
|
| 127 |
/** |
| 128 |
* Renders column content for the 'wp_template_part' post type list table. |
| 129 |
* |
| 130 |
* @param string $column_name Column name to render. |
| 131 |
* @param int $post_id Post ID. |
| 132 |
*/ |
| 133 |
function gutenberg_render_template_part_list_table_column( $column_name, $post_id ) { |
| 134 |
if ( 'slug' !== $column_name ) { |
| 135 |
return; |
| 136 |
} |
| 137 |
$post = get_post( $post_id ); |
| 138 |
echo esc_html( $post->post_name ); |
| 139 |
} |
| 140 |
add_action( 'manage_wp_template_part_posts_custom_column', 'gutenberg_render_template_part_list_table_column', 10, 2 ); |
| 141 |
|
| 142 |
|
| 143 |
/** |
| 144 |
* Filter for adding a `theme` parameter to `wp_template_part` queries. |
| 145 |
* |
| 146 |
* @param array $query_params The query parameters. |
| 147 |
* @return array Filtered $query_params. |
| 148 |
*/ |
| 149 |
function filter_rest_wp_template_part_collection_params( $query_params ) { |
| 150 |
$query_params += array( |
| 151 |
'theme' => array( |
| 152 |
'description' => __( 'The theme slug for the theme that created the template part.', 'gutenberg' ), |
| 153 |
'type' => 'string', |
| 154 |
), |
| 155 |
); |
| 156 |
return $query_params; |
| 157 |
} |
| 158 |
apply_filters( 'rest_wp_template_part_collection_params', 'filter_rest_wp_template_part_collection_params', 99, 1 ); |
| 159 |
|
| 160 |
/** |
| 161 |
* Filter for supporting the `theme` parameter in `wp_template_part` queries. |
| 162 |
* |
| 163 |
* @param array $args The query arguments. |
| 164 |
* @param WP_REST_Request $request The request object. |
| 165 |
* @return array Filtered $args. |
| 166 |
*/ |
| 167 |
function filter_rest_wp_template_part_query( $args, $request ) { |
| 168 |
if ( $request['theme'] ) { |
| 169 |
$meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array(); |
| 170 |
$meta_query[] = array( |
| 171 |
'key' => 'theme', |
| 172 |
'value' => $request['theme'], |
| 173 |
); |
| 174 |
|
| 175 |
$args['meta_query'] = $meta_query; |
| 176 |
} |
| 177 |
return $args; |
| 178 |
} |
| 179 |
add_filter( 'rest_wp_template_part_query', 'filter_rest_wp_template_part_query', 99, 2 ); |
| 180 |
|