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

Query Title: add Search and 404 page titles #33515

Closed
wants to merge 54 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
1744a97
Add Archive Title block
ntsekouras Jan 5, 2021
a6ceb90
remove extra lines
ntsekouras Jan 5, 2021
03b2704
convert to int
ntsekouras Jan 5, 2021
47aae1b
Add new ArchiveTitle icon
ntsekouras Jan 6, 2021
00d949f
query title with block variations
ntsekouras Jan 18, 2021
3b694a9
Merge branch 'trunk' into try/archive-title
mikachan Jul 15, 2021
82ec6c7
Fix merge errors
mikachan Jul 15, 2021
b2fb7d7
Add support for search in query title
mikachan Jul 16, 2021
258dc44
Add support for 404 in query title
mikachan Jul 16, 2021
4ebfe53
Merge branch 'WordPress:trunk' into add/search-404-titles
mikachan Jul 16, 2021
be5836c
Remove translators comment for 404 title
mikachan Jul 16, 2021
9dd89e5
Merge branch 'WordPress:trunk' into add/search-404-titles
mikachan Jul 16, 2021
900c95b
Remove subhead from block folders array
mikachan Jul 16, 2021
7e3f308
Add another wording option for search title
mikachan Jul 16, 2021
3244717
Reformat type if statement
mikachan Jul 16, 2021
22d458d
Add context to 404 template title
mikachan Jul 19, 2021
08da5a2
Merge branch 'WordPress:trunk' into add/search-404-titles
mikachan Jul 19, 2021
1f40aca
Add icons to search and 404 title blocks
mikachan Jul 19, 2021
ecdd94b
Remove Query Title variations
mikachan Jul 19, 2021
600204c
Remove type attribute from Query Title
mikachan Jul 19, 2021
ff7179c
Add templateSlug context to Query Title
mikachan Jul 19, 2021
0e17396
Change Query Title content relative to templateSlug context
mikachan Jul 19, 2021
fb20f58
Refactor title content variable
mikachan Jul 19, 2021
a571c6f
Use _x function for archive title
mikachan Jul 19, 2021
ad39fe9
Add supported templates list; display warning if template is not yet …
mikachan Jul 20, 2021
2420657
Add comment to useEffect
mikachan Jul 20, 2021
3fa11cc
Add alternative option for search results title
mikachan Jul 20, 2021
9ca7923
Merge branch 'WordPress:trunk' into add/search-404-titles
mikachan Jul 20, 2021
e02f72d
Add archive title icon for Query Title
mikachan Jul 20, 2021
a527d24
Remove alternative search title
mikachan Jul 20, 2021
5845908
Use RichText for search title
mikachan Jul 20, 2021
710759e
Add more detail to Query Title description
mikachan Jul 20, 2021
018dbb0
Don't output anything when the title isn't set
scruffian Jul 20, 2021
38b7675
typo fix
scruffian Jul 20, 2021
cf5f3e5
Update comment
scruffian Jul 20, 2021
64738dc
Fix query title unit test
mikachan Jul 20, 2021
9ca257a
Remove Query title placeholder text
mikachan Jul 20, 2021
ca7498c
Move empty title check further down
mikachan Jul 20, 2021
b2ebbc8
Format equals sign alignment
mikachan Jul 20, 2021
5e0eef7
Change if to elseif in Query Title index.php
mikachan Jul 20, 2021
9ed105b
Remove content from archive title block
mikachan Jul 20, 2021
152103b
Change default content to 'Query title'
mikachan Jul 20, 2021
358cb81
Add __unstableMarkNextChangeAsNotPersistent to useEffect
mikachan Jul 20, 2021
b5b52a8
Add searchTitleContent & nothingFoundTitleContent attributes
mikachan Jul 21, 2021
9df71fa
Add InspectorControls for title variations
mikachan Jul 21, 2021
b1c26ae
Simplify title variable names
mikachan Jul 22, 2021
5d37503
Remove RichText import
mikachan Jul 22, 2021
91b79cd
Change placeholder title to 'Query title placeholder'
mikachan Jul 22, 2021
ca655a2
Remove default placeholder
mikachan Jul 22, 2021
b88c5de
Remove content attribute
mikachan Jul 22, 2021
cc02de1
Remove Inspector Controls
mikachan Jul 22, 2021
6ccbdad
Remove content attribute from block test
mikachan Jul 22, 2021
9f15ae5
Add default state for index template in editor
mikachan Jul 22, 2021
eff88a8
Remove title variable based on content attribute
mikachan Jul 22, 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
8 changes: 6 additions & 2 deletions packages/block-library/src/query-title/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
"name": "core/query-title",
"title": "Query Title",
"category": "design",
"description": "Display the query title.",
"description": "Display the query title based on the queried object. Supports search, archive and 404 templates. Dynamic content is available on the search template for: %search%, %total%.",
"textdomain": "default",
"usesContext": [ "templateSlug" ],
"attributes": {
"type": {
"searchTitle": {
"type": "string"
},
"nothingFoundTitle": {
"type": "string"
},
"textAlign": {
Expand Down
123 changes: 107 additions & 16 deletions packages/block-library/src/query-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,140 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
// import { useSelect, useDispatch } from '@wordpress/data';
import { useDispatch } from '@wordpress/data';
import {
AlignmentControl,
BlockControls,
useBlockProps,
Warning,
RichText,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { __, _x } from '@wordpress/i18n';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import HeadingLevelDropdown from '../heading/heading-level-dropdown';

const SUPPORTED_TYPES = [ 'archive' ];
const SUPPORTED_TEMPLATES = [ 'archive', 'search', '404', 'index' ];

export default function QueryTitleEdit( {
attributes: { type, level, textAlign },
attributes: { searchTitle, nothingFoundTitle, level, textAlign },
setAttributes,
context: { templateSlug },
} ) {
const TagName = `h${ level }`;
const blockProps = useBlockProps( {
className: classnames( {
className: classnames( 'wp-block-query-title__placeholder', {
[ `has-text-align-${ textAlign }` ]: textAlign,
'wp-block-query-title__placeholder': type === 'archive',
} ),
} );
// The plan is to augment this block with more
// block variations like `Search Title`.
if ( ! SUPPORTED_TYPES.includes( type ) ) {
const { __unstableMarkNextChangeAsNotPersistent } = useDispatch(
blockEditorStore
);
let titleElement;

// translators: Title for archive template.
const defaultTitle = _x(
'Query title placeholder',
'query template title'
);

// translators: Title for archive template.
const archiveTitle = _x(
'Archive title placeholder',
'archive template title'
);

// translators: Title for 404 template.
const defaultNothingFoundTitle = _x(
'Nothing found',
'404 template title'
);

// translators: Title for search template with dynamic content placeholders.
const defaultSearchTitle = _x(
'Search results for "%search%"',
'search template title'
);

// Infer title content from template slug context
switch ( templateSlug ) {
case 'archive':
titleElement = (
<TagName { ...blockProps }>{ archiveTitle }</TagName>
);
break;
case 'search':
titleElement = (
<div { ...blockProps }>
<RichText
tagName={ TagName }
value={ searchTitle }
placeholder={ searchTitle }
allowedFormats={ [ 'core/bold', 'core/italic' ] }
onChange={ ( newSearchTitle ) =>
setAttributes( { searchTitle: newSearchTitle } )
}
disableLineBreaks={ true }
/>
</div>
);
break;
case '404':
titleElement = (
<div { ...blockProps }>
<RichText
tagName={ TagName }
value={ nothingFoundTitle }
placeholder={ nothingFoundTitle }
allowedFormats={ [ 'core/bold', 'core/italic' ] }
onChange={ ( newNothingFoundTitle ) =>
setAttributes( {
nothingFoundTitle: newNothingFoundTitle,
} )
}
disableLineBreaks={ true }
/>
</div>
);
break;
case 'index':
titleElement = (
<TagName { ...blockProps }>{ defaultTitle }</TagName>
);
break;
default:
break;
}

// Update default content based on current template
useEffect( () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems hacky to me 😄 . We use React's implementation details to update the content after the first render and by updating the content attribute, we also create an undo level in history. That's one of the reasons I had block variations in mind back then. The php handling remains the same, the above content attribute handling is done by the variations and we can provide a bit more specific info for the block (BlockCard etc..).

An alternative, if this approach is preferred, is to use __unstableMarkNextChangeAsNotPersistent here and a new variable for the first content (to avoid changing the existing content var).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha I wasn't sure where else to turn so I jumped to useEffect! Thanks so much for the detailed explanation.

As soon as I moved this logic from the variations file to edit.js, I could see the advantage of using variations. However, as @scruffian has just mentioned above, I think the advantage to not using variations is that the user (or a theme) only needs to insert one block, and the block works the content out itself based on context (i.e. the template).

With that in mind, I'm going to attempt your suggested alternative. Thanks for the suggestion and input, it's really helped me understand this more.

__unstableMarkNextChangeAsNotPersistent();

if ( ! searchTitle ) {
setAttributes( {
searchTitle: defaultSearchTitle,
} );
}

if ( ! nothingFoundTitle ) {
setAttributes( {
nothingFoundTitle: defaultNothingFoundTitle,
} );
}
}, [] );

if ( ! SUPPORTED_TEMPLATES.includes( templateSlug ) ) {
return (
<div { ...blockProps }>
<Warning>{ __( 'Provided type is not supported.' ) }</Warning>
<Warning>{ __( 'Template is not supported.' ) }</Warning>
</div>
);
}

let titleElement;
if ( type === 'archive' ) {
titleElement = (
<TagName { ...blockProps }>{ __( 'Archive title' ) }</TagName>
);
}
return (
<>
<BlockControls group="block">
Expand Down
8 changes: 6 additions & 2 deletions packages/block-library/src/query-title/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
/**
* WordPress dependencies
*/
import { archiveTitle as icon } from '@wordpress/icons';

/**
* Internal dependencies
*/
import metadata from './block.json';
import edit from './edit';
import variations from './variations';

const { name } = metadata;
export { metadata, name };

export const settings = {
icon,
edit,
variations,
};
20 changes: 14 additions & 6 deletions packages/block-library/src/query-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,34 @@

/**
* Renders the `core/query-title` block on the server.
* For now it only supports Archive title,
* For now it supports Archive title, Search title and 404 title,
* using queried object information
*
* @param array $attributes Block attributes.
*
* @return string Returns the query title based on the queried object.
*/
function render_block_core_query_title( $attributes ) {
$type = isset( $attributes['type'] ) ? $attributes['type'] : null;
$is_search = is_search();
$is_archive = is_archive();
if ( ! $type || ( 'archive' === $type && ! $is_archive ) ) {
return '';
}
$title = '';
$is_404 = is_404();
if ( $is_archive ) {
$title = get_the_archive_title();
} elseif ( $is_search ) {
global $wp_query;
$title = isset( $attributes['searchTitle'] ) ? $attributes['searchTitle'] : '';
$formats = array( '%total%', '%search%' );
$replacements = array( $wp_query->found_posts, get_search_query() );
$title = str_replace( $formats, $replacements, $title );
} elseif ( $is_404 ) {
$title = isset( $attributes['nothingFoundTitle'] ) ? $attributes['nothingFoundTitle'] : '';
}
$tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1';
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
if ( empty( $title ) ) {
return;
}
return sprintf(
'<%1$s %2$s>%3$s</%1$s>',
$tag_name,
Expand Down
33 changes: 0 additions & 33 deletions packages/block-library/src/query-title/variations.js

This file was deleted.