Skip to content

Commit

Permalink
Replace searchpage.php with FSE template
Browse files Browse the repository at this point in the history
Many props to @Gentyspun
  • Loading branch information
jaymcp committed Sep 19, 2024
2 parents a38a989 + 0560675 commit c20a6f6
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 112 deletions.
7 changes: 6 additions & 1 deletion private/src/styles/blocks/core-blocks/post/_main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
background: var(--wp--preset--color--white);
}

.search-results .wp-block-post,
.search-results .wp-block-post > .wp-block-group {
min-height: unset;
}

.wp-block-post .post-content {
display: flex;
flex-direction: column;
Expand Down Expand Up @@ -38,7 +43,7 @@
}
}

.wp-block-post.has-post-thumbnail .wp-block-post-terms.post-category {
.wp-block-post.has-post-thumbnail > *:not(.post--result) .wp-block-post-terms.post-category {
position: absolute;
bottom: 100%;
left: 12px;
Expand Down
16 changes: 5 additions & 11 deletions private/src/styles/blocks/postlist/_post-search.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ul.wp-block-post-template {
padding: 12px;
}

.post--result + .post--result {
.search-results .wp-block-post + .wp-block-post {
margin-top: var(--wp--preset--spacing--single);
}

Expand All @@ -45,6 +45,10 @@ ul.wp-block-post-template {
font-size: var(--wp--preset--font-size--small);
}

.post--result .post-terms {
margin-bottom: var(--wp--preset--spacing--single);
}

.post--result .post-category,
.post--result .post-topic,
.post--result .post-location {
Expand All @@ -54,16 +58,6 @@ ul.wp-block-post-template {
padding-bottom: 4px;
}

.post--result .post-category {
margin-bottom: 20px;
margin-right: 20px;

.rtl & {
margin-right: 0;
margin-left: 20px;
}
}

.post--result .post-topic {
font-family: var(--wp--preset--font-family--secondary);
text-transform: uppercase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
*/
class Search_Page {

/**
* The query object
*
* @var \WP_Query|null
*/
protected ?WP_Query $query = null;

/**
* Count of found search results
*
Expand All @@ -42,48 +49,29 @@ public function __construct( bool $execute = true ) {
}

/**
* Query the database for the search results
* Retrieve the query object for the search page
*
* @return array<int,\WP_Post>
* @return WP_Query|null
*/
public function get_results(): array {
global $wpdb;

if ( isset( $this->found_posts ) ) {
return $this->found_posts;
public function get_wp_query(): ?WP_Query {
if ( is_null( $this->query ) ) {
$this->query = new WP_Query( $this->get_query_vars() );
}

$query_sql = $this->get_sql();
$cache_key_base = md5( $query_sql );
$cached_posts = wp_cache_get( $cache_key_base . '_posts', __METHOD__ );
$cached_count = wp_cache_get( $cache_key_base . '_count', __METHOD__ );

if ( is_array( $cached_posts ) && absint( $cached_count ) ) {
$this->found_posts = $cached_posts;
$this->found_rows = absint( $cached_count );

return $this->found_posts;
}

// reset just in case
$this->found_rows = 0;

// phpcs:ignore WordPress.DB
$results = (array) $wpdb->get_results( $query_sql );
return $this->query;
}

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$this->found_rows = absint( $wpdb->get_var( 'SELECT found_rows()' ) );
$this->found_posts = array_map( fn ( $row ): WP_Post => new WP_Post( $row ), $results );
/**
* Query the database for the search results
*
* @return array<int,\WP_Post>
*/
public function get_results(): array {
$query = $this->get_wp_query();

// no results
if ( is_null( $this->found_posts ) ) {
wp_cache_set( $cache_key_base . '_count', $this->found_rows, __METHOD__, 60 );
wp_cache_set( $cache_key_base . '_posts', $this->found_posts, __METHOD__, 60 );
return [];
}
$this->found_posts = $query->posts;
$this->found_rows = $query->found_posts;

wp_cache_set( $cache_key_base . '_count', $this->found_rows, __METHOD__, HOUR_IN_SECONDS );
wp_cache_set( $cache_key_base . '_posts', $this->found_posts, __METHOD__, HOUR_IN_SECONDS );
return $this->found_posts;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

require_once realpath( __DIR__ ) . '/archive-filters/register.php';
require_once realpath( __DIR__ ) . '/archive-filters/render.php';
require_once realpath( __DIR__ ) . '/archive-header/register.php';
require_once realpath( __DIR__ ) . '/archive-header/render.php';
require_once realpath( __DIR__ ) . '/pop-in/register.php';
require_once realpath( __DIR__ ) . '/pop-in/render.php';
require_once realpath( __DIR__ ) . '/query-count/register.php';
require_once realpath( __DIR__ ) . '/query-count/render.php';
require_once realpath( __DIR__ ) . '/site-header/register.php';
require_once realpath( __DIR__ ) . '/site-header/render.php';
require_once realpath( __DIR__ ) . '/search-form/register.php';
require_once realpath( __DIR__ ) . '/search-form/render.php';
require_once realpath( __DIR__ ) . '/archive-header/render.php';
require_once realpath( __DIR__ ) . '/archive-header/register.php';
require_once realpath( __DIR__ ) . '/search-header/register.php';
require_once realpath( __DIR__ ) . '/search-header/render.php';
require_once realpath( __DIR__ ) . '/site-header/register.php';
require_once realpath( __DIR__ ) . '/site-header/render.php';

if ( ! function_exists( 'amnesty_register_full_site_editing_blocks' ) ) {
/**
Expand All @@ -25,11 +27,12 @@
*/
function amnesty_register_full_site_editing_blocks(): void {
register_archive_filters_block();
register_archive_header_block();
register_pop_in_block();
register_query_count_block();
register_site_header_block();
register_search_form_block();
register_archive_header_block();
register_search_header_block();
register_site_header_block();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://raw.githubusercontent.com/WordPress/gutenberg/trunk/schemas/json/block.json",
"apiVersion": 2,
"name": "amnesty-core/search-header",
"title": "Search Header",
"description": "For presenting the search page header",
"textdomain": "amnesty",
"category": "amnesty-core",
"icon": "editor-table",
"keywords": ["Search Header"],
"usesContext": [],
"supports": {
"multiple": false,
"align": false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare( strict_types = 1 );

if ( ! function_exists( 'register_search_header_block' ) ) {
/**
* Register the search header block
*
* @package Amnesty\Blocks
*
* @return void
*/
function register_search_header_block(): void {
register_block_type_from_metadata(
__DIR__,
[
'render_callback' => 'render_search_header_block',
],
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare( strict_types = 1 );

if ( ! function_exists( 'render_search_header_block' ) ) {
/**
* Render the post search header block
*
* @return string
*/
function render_search_header_block(): string {
spaceless();

get_template_part( 'partials/archive/header' );

return endspaceless( false );
}
}
44 changes: 27 additions & 17 deletions wp-content/themes/humanity-theme/patterns/search-results.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,36 @@
add_filter( 'get_the_terms', 'amnesty_limit_post_terms_results_for_search' );

?>
<!-- wp:query {"inherit":true, "className":"section--tinted"} -->
<div class="wp-block-query section--tinted">
<!-- wp:post-template {"layout":{"type":"constrained","justifyContent":"left"}} -->
<!-- wp:query {"inherit":true} -->
<div class="wp-block-query">
<!-- wp:group {"tagName":"div","className":"section section--tinted search-results"} -->
<div class="wp-block-group section section--tinted search-results">
<!-- wp:amnesty-core/archive-header /-->
<!-- wp:post-template {"layout":{"type":"constrained","justifyContent":"left"}} -->

<!-- wp:group {"tagName":"article","className":"post post--result"} -->
<article class="wp-block-group post post--result">
<!-- wp:post-terms {"term":"category","className":"post-category"} /-->
<!-- wp:post-terms {"term":"<?php echo esc_attr( $location_slug ); ?>","className":"post-location"} /-->
<!-- wp:post-terms {"term":"topic","className":"post-topic"} /-->
<!-- wp:post-title {"isLink":true,"className":"post-title"} /-->
<!-- wp:post-excerpt {"className":"post-excerpt"} /-->
<!-- wp:post-date {"className":"post-byline"} /-->
</article>
<!-- wp:group {"tagName":"article","className":"post post--result"} -->
<article class="wp-block-group post post--result">
<!-- wp:group {"tagName":"div","className":"post-terms","layout":{"type":"flex","flexWrap":"nowrap"}} -->
<div class="wp-block-group post-terms">
<!-- wp:post-terms {"term":"category","className":"post-category"} /-->
<!-- wp:post-terms {"term":"<?php echo esc_attr( $location_slug ); ?>","className":"post-location"} /-->
<!-- wp:post-terms {"term":"topic","className":"post-topic"} /-->
</div>
<!-- /wp:group -->
<!-- wp:post-title {"isLink":true,"className":"post-title"} /-->
<!-- wp:post-excerpt {"className":"post-excerpt"} /-->
<!-- wp:post-date {"className":"post-byline"} /-->
</article>
<!-- /wp:group -->

<!-- /wp:post-template -->
</div>
<!-- /wp:group -->

<!-- /wp:post-template -->
<!-- wp:query-pagination {"align":"center","className":"section section--small post-paginationContainer","layout":{"type":"flex","justifyContent":"space-between","flexWrap":"nowrap"}} -->
<!-- wp:query-pagination-previous {"className":"post-paginationLink post-paginationPrevious"} /-->
<!-- wp:query-pagination-numbers {"className":"page-numbers"} /-->
<!-- wp:query-pagination-next {"className":"post-paginationLink post-paginationNext"} /-->
<!-- wp:query-pagination {"align":"center","className":"section section--small","paginationArrow":"none","layout":{"type":"flex","justifyContent":"space-between","flexWrap":"nowrap"}} -->
<!-- wp:query-pagination-previous {"label":"Previous"} /-->
<!-- wp:query-pagination-numbers {"midSize":1,"className":"page-numbers"} /-->
<!-- wp:query-pagination-next {"label":"Next"} /-->
<!-- /wp:query-pagination -->
</div>
<!-- /wp:query -->
80 changes: 80 additions & 0 deletions wp-content/themes/humanity-theme/patterns/search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* Title: Search pattern
* Description: Search pattern for the theme
* Slug: amnesty/search
* Inserter: no
*/

$location_slug = get_option( 'amnesty_location_slug' ) ?: 'location';
$search_object = amnesty_get_searchpage_query_object( false );
$order_vars = $search_object->get_order_vars();

$args = [
'inherit' => false,
'query' => [
'perPage' => null,
'pages' => 0,
'offset' => 0,
'postType' => apply_filters( 'amnesty_list_query_post_types', [ 'page', 'post' ] ),
'order' => $order_vars['order'],
'orderby' => $order_vars['orderby'],
'author' => '',
'search' => '', // if there's a term, we'll be on a different template
'exclude' => [], // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
'sticky' => false,
'taxQuery' => $search_object->build_tax_args(),
'parents' => [],
],
];

add_filter( 'query_loop_block_query_vars', fn () => $args['query'] );

if ( amnesty_get_query_var( 'qyear' ) ) {
add_filter( 'query_loop_block_query_vars', fn ( array $vars ): array => $vars + [ 'year' => amnesty_get_query_var( 'qyear' ) ] );
}

if ( amnesty_get_query_var( 'qmonth' ) ) {
add_filter( 'query_loop_block_query_vars', fn ( array $vars ): array => $vars + [ 'monthnum' => amnesty_get_query_var( 'qmonth' ) ] );
}


// add filter to limit the post terms results for search
add_filter( 'get_the_terms', 'amnesty_limit_post_terms_results_for_search' );

?>

<!-- wp:query <?php echo wp_kses_data( wp_json_encode( $args ) ); ?> -->
<div class="wp-block-query">
<!-- wp:group {"tagName":"div","className":"section section--tinted search-results"} -->
<div class="wp-block-group section section--tinted search-results">
<!-- wp:amnesty-core/search-header /-->
<!-- wp:post-template {"layout":{"type":"constrained","justifyContent":"left"}} -->

<!-- wp:group {"tagName":"article","className":"post post--result"} -->
<article class="wp-block-group post post--result">
<!-- wp:group {"tagName":"div","className":"post-terms","layout":{"type":"flex","flexWrap":"nowrap"}} -->
<div class="wp-block-group post-terms">
<!-- wp:post-terms {"term":"category","className":"post-category"} /-->
<!-- wp:post-terms {"term":"<?php echo esc_attr( $location_slug ); ?>","className":"post-location"} /-->
<!-- wp:post-terms {"term":"topic","className":"post-topic"} /-->
</div>
<!-- /wp:group -->
<!-- wp:post-title {"isLink":true,"className":"post-title"} /-->
<!-- wp:post-excerpt {"className":"post-excerpt"} /-->
<!-- wp:post-date {"className":"post-byline"} /-->
</article>
<!-- /wp:group -->

<!-- /wp:post-template -->
</div>
<!-- /wp:group -->

<!-- wp:query-pagination {"align":"center","className":"section section--small","paginationArrow":"none","layout":{"type":"flex","justifyContent":"space-between","flexWrap":"nowrap"}} -->
<!-- wp:query-pagination-previous {"label":"Previous"} /-->
<!-- wp:query-pagination-numbers {"midSize":1,"className":"page-numbers"} /-->
<!-- wp:query-pagination-next {"label":"Next"} /-->
<!-- /wp:query-pagination -->
</div>
<!-- /wp:query -->
Loading

0 comments on commit c20a6f6

Please sign in to comment.