-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from moderntribe/s3/MOOSE-48/query-results-cou…
…nt-block [MOOSE-48] Query Results Count Block
- Loading branch information
Showing
7 changed files
with
141 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
wp-content/themes/core/blocks/tribe/query-results-count/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"name": "tribe/query-results-count", | ||
"version": "0.1.0", | ||
"title": "Query Results Count", | ||
"category": "text", | ||
"icon": "chart-line", | ||
"description": "Displays (in context) the total number of posts in a archive", | ||
"supports": { | ||
"html": false, | ||
"align": true, | ||
"spacing": { | ||
"margin": true, | ||
"padding": true | ||
} | ||
}, | ||
"textdomain": "tribe", | ||
"editorScript": "file:./index.js", | ||
"editorStyle": "file:./index.css", | ||
"style": "file:./style-index.css", | ||
"render": "file:./render.php" | ||
} |
32 changes: 32 additions & 0 deletions
32
wp-content/themes/core/blocks/tribe/query-results-count/edit.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* React hook that is used to mark the block wrapper element. | ||
* It provides all the necessary props like the class name. | ||
* | ||
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops | ||
*/ | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Server-side rendering of the block in the editor view | ||
* | ||
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-server-side-render/ | ||
*/ | ||
import ServerSideRender from '@wordpress/server-side-render'; | ||
|
||
/** | ||
* The edit function describes the structure of your block in the context of the | ||
* editor. This represents what the editor will render when the block is used. | ||
* | ||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit | ||
* | ||
* @return {WPElement} Element to render. | ||
*/ | ||
export default function Edit() { | ||
const blockProps = useBlockProps(); | ||
|
||
return ( | ||
<div { ...blockProps }> | ||
<ServerSideRender block="tribe/query-results-count" /> | ||
</div> | ||
); | ||
} |
33 changes: 33 additions & 0 deletions
33
wp-content/themes/core/blocks/tribe/query-results-count/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Registers a new block provided a unique name and an object defining its behavior. | ||
* | ||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/ | ||
*/ | ||
import { registerBlockType } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files. | ||
* All files containing `style` keyword are bundled together. The code used | ||
* gets applied both to the front of your site and to the editor. | ||
* | ||
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css | ||
*/ | ||
import './style.pcss'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Edit from './edit'; | ||
import metadata from './block.json'; | ||
|
||
/** | ||
* Every block starts by registering a new block type definition. | ||
* | ||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/ | ||
*/ | ||
registerBlockType( metadata.name, { | ||
/** | ||
* @see ./edit.js | ||
*/ | ||
edit: Edit, | ||
} ); |
24 changes: 24 additions & 0 deletions
24
wp-content/themes/core/blocks/tribe/query-results-count/render.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php declare(strict_types=1); | ||
|
||
global $wp_query; | ||
$is_search = is_search(); | ||
$count = (int) $wp_query->post_count; | ||
$output = sprintf( _n( '%d result', '%d results', $count, 'tribe' ), number_format_i18n( $count ) ); | ||
|
||
if ( $is_search ) { | ||
$output = sprintf( | ||
_x( | ||
'%s %s for <span class="search-term">“%s”</span>', | ||
'First value is the number of results, second is word "result" (pluralized if necessary), third is the search term', | ||
'tribe' | ||
), | ||
number_format_i18n( $count ), | ||
_n( 'result', 'results', $count, 'tribe' ), | ||
get_search_query() | ||
); | ||
} | ||
?> | ||
|
||
<div <?php echo get_block_wrapper_attributes(); ?>> | ||
<p><?php echo wp_kses_post( $output ); ?></p> | ||
</div> |
15 changes: 15 additions & 0 deletions
15
wp-content/themes/core/blocks/tribe/query-results-count/style.pcss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* The following styles get applied both on the front of your site | ||
* and in the editor. | ||
* | ||
* Replace them with your own styles or remove the file completely. | ||
*/ | ||
|
||
.wp-block-tribe-query-results-count { | ||
|
||
@mixin t-body; | ||
|
||
.search-term { | ||
font-weight: 700; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters