Skip to content
Merged
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
1 change: 1 addition & 0 deletions backport-changelog/7.1/11272.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ https://github.com/WordPress/wordpress-develop/pull/11272

* https://github.com/WordPress/gutenberg/pull/76573
* https://github.com/WordPress/gutenberg/pull/76734
* https://github.com/WordPress/gutenberg/pull/76622
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public function get_items( $request ) {
} elseif ( 'postType' === $kind && 'wp_template_part' === $name ) {
$default_layouts = $this->get_default_layouts_for_wp_template_part();
$default_view = $this->get_default_view_for_wp_template_part( $default_layouts );
} elseif ( 'postType' === $kind && 'wp_template' === $name ) {
$default_view = $this->get_default_view_for_wp_template();
$default_layouts = $this->get_default_layouts_for_wp_template();
$view_list = $this->get_view_list_for_wp_template();
}

$response = array(
Expand Down Expand Up @@ -675,4 +679,185 @@ private function get_default_view_for_wp_template_part( $default_layouts ) {
'layout' => $default_layouts['grid']['layout'],
);
}

/**
* Returns the original source of a template.
*
* @param WP_Block_Template $template_object Template instance.
* @return string The original source ('theme', 'plugin', 'site', or 'user').
*/
private static function get_wp_templates_original_source_field( $template_object ) {
if ( 'wp_template' === $template_object->type || 'wp_template_part' === $template_object->type ) {
/*
* Added by theme.
* Template originally provided by a theme, but customized by a user.
* Templates originally didn't have the 'origin' field so identify
* older customized templates by checking for no origin and a 'theme'
* or 'custom' source.
*/
if ( $template_object->has_theme_file &&
( 'theme' === $template_object->origin || (
empty( $template_object->origin ) && in_array(
$template_object->source,
array(
'theme',
'custom',
),
true
) )
)
) {
return 'theme';
}

// Added by plugin.
if ( 'plugin' === $template_object->origin ) {
return 'plugin';
}

/*
* Added by site.
* Template was created from scratch, but has no author. Author support
* was only added to templates in WordPress 5.9. Fallback to showing the
* site logo and title.
*/
if ( empty( $template_object->has_theme_file ) && 'custom' === $template_object->source && empty( $template_object->author ) ) {
return 'site';
}
}

// Added by user.
return 'user';
}

/**
* Returns a human readable text for the author of a template.
*
* @param WP_Block_Template $template_object Template instance.
* @return string Human readable text for the author.
*/
private static function get_wp_templates_author_text_field( $template_object ) {
$original_source = self::get_wp_templates_original_source_field( $template_object );
switch ( $original_source ) {
case 'theme':
$theme_name = wp_get_theme( $template_object->theme )->get( 'Name' );
return empty( $theme_name ) ? $template_object->theme : $theme_name;
case 'plugin':
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if ( isset( $template_object->plugin ) ) {
$plugins = wp_get_active_and_valid_plugins();

foreach ( $plugins as $plugin_file ) {
$plugin_basename = plugin_basename( $plugin_file );
list( $plugin_slug, ) = explode( '/', $plugin_basename );

if ( $plugin_slug === $template_object->plugin ) {
$plugin_data = get_plugin_data( $plugin_file );

if ( ! empty( $plugin_data['Name'] ) ) {
return $plugin_data['Name'];
}

break;
}
}
}

/*
* Fall back to the theme name if the plugin is not defined. That's needed to keep backwards
* compatibility with templates that were registered before the plugin attribute was added.
*/
$plugins = get_plugins();
$plugin_basename = plugin_basename( sanitize_text_field( $template_object->theme . '.php' ) );
if ( isset( $plugins[ $plugin_basename ] ) && isset( $plugins[ $plugin_basename ]['Name'] ) ) {
return $plugins[ $plugin_basename ]['Name'];
}
return $template_object->plugin ?? $template_object->theme;
case 'site':
return get_bloginfo( 'name' );
case 'user':
$author = get_user_by( 'id', $template_object->author );
if ( ! $author ) {
return __( 'Unknown author', 'gutenberg' );
}
return $author->get( 'display_name' );
}

// Fail-safe to return a string should the original source ever fall through.
return '';
}

private function get_default_view_for_wp_template() {
return array(
'type' => 'grid',
'perPage' => 20,
'sort' => array(
'field' => 'title',
'direction' => 'asc',
),
'titleField' => 'title',
'descriptionField' => 'description',
'mediaField' => 'preview',
'fields' => array( 'author', 'active', 'slug', 'theme' ),
'filters' => array(),
'showMedia' => true,
);
}

private function get_default_layouts_for_wp_template() {
return array(
'table' => array( 'showMedia' => false ),
'grid' => array( 'showMedia' => true ),
'list' => array( 'showMedia' => false ),
);
}

private function get_view_list_for_wp_template() {
$view_list = array(
array(
'title' => __( 'All templates', 'gutenberg' ),
'slug' => 'all',
),
);

$templates = get_block_templates( array(), 'wp_template' );

// Collect unique authors, tracking whether they come from a registered
// source (theme, plugin, site) so we can sort those before user ones.
$seen_authors = array();
$registered_authors = array();
$user_authors = array();
foreach ( $templates as $template ) {
$original_source = self::get_wp_templates_original_source_field( $template );
$author_text = self::get_wp_templates_author_text_field( $template );
if ( ! empty( $author_text ) && ! isset( $seen_authors[ $author_text ] ) ) {
$seen_authors[ $author_text ] = true;
$entry = array(
'title' => $author_text,
'slug' => $author_text,
'view' => array(
'filters' => array(
array(
'field' => 'author',
'operator' => 'is',
'value' => $author_text,
'isLocked' => true,
),
),
),
);
if ( 'user' === $original_source ) {
$user_authors[] = $entry;
} else {
$registered_authors[] = $entry;
}
}
}

$view_list = array_merge( $view_list, $registered_authors, $user_authors );

return $view_list;
}
}
25 changes: 14 additions & 11 deletions packages/edit-site/src/components/page-templates/index-legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { privateApis as routerPrivateApis } from '@wordpress/router';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { addQueryArgs } from '@wordpress/url';
import { useEvent } from '@wordpress/compose';
import { useView } from '@wordpress/views';
import { useView, useViewConfig } from '@wordpress/views';

/**
* Internal dependencies
Expand All @@ -20,32 +20,35 @@ import { TEMPLATE_POST_TYPE } from '../../utils/constants';
import { unlock } from '../../lock-unlock';
import { useEditPostAction } from '../dataviews-actions';
import { authorField, descriptionField, previewField } from './fields';
import {
defaultLayouts,
DEFAULT_VIEW,
getActiveViewOverridesForTab,
} from './view-utils';

const { usePostActions, templateTitleField } = unlock( editorPrivateApis );
const { useHistory, useLocation } = unlock( routerPrivateApis );
const { useEntityRecordsWithPermissions } = unlock( corePrivateApis );

export default function PageTemplates() {
const { path, query } = useLocation();
const { activeView = 'active', postId } = query;
const { activeView = 'all', postId } = query;
const [ selection, setSelection ] = useState( [ postId ] );

const defaultView = DEFAULT_VIEW;
const {
default_view: defaultView,
default_layouts: defaultLayouts,
view_list: viewList,
} = useViewConfig( {
kind: 'postType',
name: TEMPLATE_POST_TYPE,
} );
const activeViewOverrides = useMemo(
() => getActiveViewOverridesForTab( activeView ),
[ activeView ]
() => viewList?.find( ( v ) => v.slug === activeView )?.view ?? {},
[ viewList, activeView ]
);
const { view, updateView, isModified, resetToDefault } = useView( {
kind: 'postType',
name: TEMPLATE_POST_TYPE,
slug: 'default',
defaultView,
activeViewOverrides,
defaultLayouts,
queryParams: {
page: query.pageNumber,
search: query.search,
Expand Down Expand Up @@ -150,7 +153,7 @@ export default function PageTemplates() {
history.navigate( `/wp_template/${ id }?canvas=edit` );
} }
selection={ selection }
defaultLayouts={ defaultLayouts }
defaultLayouts={ defaultLayouts ?? {} }
onReset={
isModified
? () => {
Expand Down
Loading
Loading