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

Add available page templates REST field and selector to sidebar #2086

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
comment:
require_changes: true
1 change: 1 addition & 0 deletions gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
require_once dirname( __FILE__ ) . '/lib/i18n.php';
require_once dirname( __FILE__ ) . '/lib/parser.php';
require_once dirname( __FILE__ ) . '/lib/register.php';
require_once dirname( __FILE__ ) . '/lib/rest-api.php';

// Register server-side code for individual blocks.
require_once dirname( __FILE__ ) . '/lib/blocks/latest-posts.php';
Expand Down
67 changes: 67 additions & 0 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* REST API Extensions.
*
* @package gutenberg
*/

if ( ! defined( 'ABSPATH' ) ) {
die( 'Silence is golden.' );
}

/**
* Registers REST field for `available_page_templates` on page resources.
*
* @since 0.7.0
*
* @return void
*/
function gutenberg_register_available_page_templates_field() {
register_rest_field( 'page', 'available_page_templates', array(
'get_callback' => 'gutenberg_get_available_page_templates',
'schema' => array(
'description' => __( 'Available templates for a given page.', 'gutenberg' ),
'type' => 'array',
'items' => array(
'type' => 'object',
'properties' => array(
'name' => array(
'type' => 'string',
'description' => __( 'Template name', 'gutenberg' ),
),
'template' => array(
'type' => 'string',
'description' => __( 'Template slug', 'gutenberg' ),
),
),
),
'readonly' => true,
'context' => array( 'edit' ),
),
) );
}
add_action( 'rest_api_init', 'gutenberg_register_available_page_templates_field' );

/**
* Returns available page templates for a requested page..
*
* @since 0.7.0
* @see gutenberg_register_available_page_templates_field()
*
* @param array $page Page data.
* @return array Available page templates.
*/
function gutenberg_get_available_page_templates( $page ) {
$available_templates = array();

// No page templates are available if the post is the page on front. See page_attributes_meta_box().
if ( intval( get_option( 'page_for_posts' ) ) === $page['id'] ) {
return array();
}

$page_templates = wp_get_theme()->get_page_templates( $page['id'] );
foreach ( $page_templates as $template => $name ) {
$available_templates[] = compact( 'name', 'template' );
}
return $available_templates;
}