Skip to content

Commit

Permalink
Merge pull request #65 from moderntribe/s3/MOOSE-48/query-results-cou…
Browse files Browse the repository at this point in the history
…nt-block

[MOOSE-48] Query Results Count Block
  • Loading branch information
GeoffDusome authored Jul 28, 2023
2 parents 867bb7c + 7735b9c commit 6ee433c
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 9 deletions.
1 change: 1 addition & 0 deletions wp-content/plugins/core/src/Blocks/Blocks_Definer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function define(): array {
self::TYPES => DI\add( [
'tribe/post-type-name',
'tribe/post-permalink',
'tribe/query-results-count',
] ),

self::EXTENDED => DI\add( [
Expand Down
23 changes: 23 additions & 0 deletions wp-content/themes/core/blocks/tribe/query-results-count/block.json
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 wp-content/themes/core/blocks/tribe/query-results-count/edit.js
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 wp-content/themes/core/blocks/tribe/query-results-count/index.js
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 wp-content/themes/core/blocks/tribe/query-results-count/render.php
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">&ldquo;%s&rdquo;</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 wp-content/themes/core/blocks/tribe/query-results-count/style.pcss
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;
}
}
22 changes: 13 additions & 9 deletions wp-content/themes/core/templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ <h1 class="wp-block-heading has-text-align-center" style="margin-top:0">Search</
<!-- wp:search {"label":"Search","showLabel":false,"buttonText":"Search","buttonPosition":"button-inside","buttonUseIcon":true} /--></div>
<!-- /wp:group -->

<!-- wp:tribe/query-results-count {"align":"wide","style":{"spacing":{"margin":{"bottom":"var:preset|spacing|30"}}}} /-->

<!-- wp:query {"queryId":0,"query":{"perPage":1,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":true,"taxQuery":null,"parents":[]},"displayLayout":{"type":"list"},"align":"wide"} -->
<div class="wp-block-query alignwide"><!-- wp:post-template -->
<!-- wp:columns {"verticalAlignment":"top","className":"p-search-card"} -->
<div class="wp-block-columns are-vertically-aligned-top p-search-card"><!-- wp:column {"verticalAlignment":"top","width":"%"} -->
<div class="wp-block-column is-vertically-aligned-top"><!-- wp:post-title {"isLink":true,"style":{"spacing":{"margin":{"top":"0","bottom":"var:preset|spacing|10"}}}} /-->
<!-- wp:group {"style":{"spacing":{"padding":{"top":"var:preset|spacing|40","right":"0","bottom":"var:preset|spacing|40","left":"0"}}},"className":"p-card-search-result l-clearfix","layout":{"type":"default"}} -->
<div class="wp-block-group p-card-search-result l-clearfix" style="padding-top:var(--wp--preset--spacing--40);padding-right:0;padding-bottom:var(--wp--preset--spacing--40);padding-left:0"><!-- wp:post-featured-image {"width":"22%","align":"right","className":"p-card-search-result__image s-aspect-ratio-cover s-aspect-ratio-4-3"} /-->

<!-- wp:tribe/post-type-name {"style":{"spacing":{"margin":{"bottom":"var:preset|spacing|10"}}}} /-->

<!-- wp:post-title {"style":{"spacing":{"margin":{"top":"0","bottom":"var:preset|spacing|10"}}},"className":"p-card-search-result__title","fontSize":"60"} /-->

<!-- wp:post-excerpt /--></div>
<!-- /wp:column -->
<!-- wp:post-excerpt /-->

<!-- wp:column {"verticalAlignment":"top","width":"248px"} -->
<div class="wp-block-column is-vertically-aligned-top" style="flex-basis:248px"><!-- wp:post-featured-image /--></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->
<!-- wp:tribe/post-permalink {"style":{"spacing":{"margin":{"top":"var:preset|spacing|20","right":"0","bottom":"0","left":"0"}}}} /-->

<!-- wp:post-title {"isLink":true,"className":"a-hidden-link-cover"} /--></div>
<!-- /wp:group -->
<!-- /wp:post-template -->

<!-- wp:query-no-results -->
Expand Down

0 comments on commit 6ee433c

Please sign in to comment.