Skip to content

Commit

Permalink
TMS-977: Add combined events search page-template (#19)
Browse files Browse the repository at this point in the history
* TMS-977: Add combined events search page-template

* TMS-977: Update changelog

* TMS-977: No results if no search terms

* Update pr-template

* TMS-977: phpcs fix

* 1.2.0
  • Loading branch information
eebbi authored Jan 31, 2024
1 parent 6bb9f60 commit b59992a
Show file tree
Hide file tree
Showing 8 changed files with 425 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Severa-ID: 2108
Severa-kuvaus:
Task: https://hiondigital.atlassian.net/browse/TMS-

<!--- Provide a general summary of your changes in the Title above -->

## Description
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning][semver].

## [Unreleased]

## [1.2.0] - 2024-02-01

- TMS-977: Add combined events search page-template.

## [1.1.0] - 2023-11-21

- TMS-974: Add recurring events to lists as single item.
Expand Down
2 changes: 1 addition & 1 deletion plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: TMS Manual Events
* Plugin URI: https://github.com/devgeniem/tms-plugin-manual-events
* Description: TMS Manual Events
* Version: 1.1.0
* Version: 1.2.0
* Requires PHP: 7.4
* Author: Geniem Oy
* Author URI: https://geniem.com
Expand Down
124 changes: 124 additions & 0 deletions src/Fields/PageCombinedEventsSearchGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
/**
* Copyright (c) 2023. Geniem Oy
*/

namespace TMS\Plugin\ManualEvents\Fields;

use Geniem\ACF\Exception;
use Geniem\ACF\Group;
use Geniem\ACF\RuleGroup;
use Geniem\ACF\Field;
use TMS\Theme\Base\ACF\Field\TextEditor;
use TMS\Theme\Base\ACF\Fields\EventsFields;
use TMS\Theme\Base\Logger;

/**
* Class PageCombinedEventsSearchGroup.
*
* @package TMS\Plugin\ManualEvents\Fields
*/
class PageCombinedEventsSearchGroup {

/**
* ManualEventGroup constructor.
*/
public function __construct() {
add_action(
'init',
\Closure::fromCallable( [ $this, 'register_fields' ] )
);
}

/**
* Register fields
*/
protected function register_fields() : void {
try {
$field_group = ( new Group( 'Sivun asetukset' ) )
->set_key( 'fg_page_combined_events_search' );

$rule_group = ( new RuleGroup() )
->add_rule( 'page_template', '==', \PageCombinedEventsSearch::TEMPLATE );

$field_group
->add_rule_group( $rule_group )
->set_position( 'normal' )
->set_hidden_elements(
[
'discussion',
'comments',
'format',
'send-trackbacks',
]
);

$field_group->add_fields(
apply_filters(
'tms/acf/group/' . $field_group->get_key() . '/fields',
[
$this->get_page_fields( $field_group->get_key() ),
]
)
);

$field_group = apply_filters(
'tms/acf/group/' . $field_group->get_key(),
$field_group
);

$field_group->register();
}
catch ( Exception $e ) {
( new Logger() )->error( $e->getMessage(), $e->getTraceAsString() );
}
}

/**
* Get page fields
*
* @param string $key Field group key.
*
* @return Field\Tab
* @throws Exception In case of invalid option.
*/
protected function get_page_fields( string $key ) : Field\Tab {
$strings = [
'tab' => 'Tapahtumat',
'description' => [
'title' => 'Kuvausteksti',
'instructions' => '',
],
];

$tab = ( new Field\Tab( $strings['tab'] ) )
->set_placement( 'left' );

$description_field = ( new TextEditor( $strings['description']['title'] ) )
->set_key( "{$key}_description" )
->set_name( 'description' )
->redipress_include_search()
->set_instructions( $strings['description']['instructions'] );

$search_fields = ( new EventsFields( 'Tapahtumahaku', $key ) )
->remove_field( 'title' )
->remove_field( 'start' )
->remove_field( 'end' )
->remove_field( 'starts_today' )
->remove_field( 'area' )
->remove_field( 'tag' )
->remove_field( 'target' )
->remove_field( 'text' )
->remove_field( 'page_size' )
->remove_field( 'all_events_link' );

$fields = [
$description_field,
];
$fields = array_merge( $fields, $search_fields->get_fields() );

$tab->add_fields( $fields );

return $tab;
}
}
18 changes: 9 additions & 9 deletions src/Models/page-combined-events-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ protected function get_events() : array {
}

$params = [
'category_id' => get_field( 'category' ),
'category_id' => \get_field( 'category' ),
'page_size' => 200, // Use an arbitrary limit as a sanity check.
'show_images' => get_field( 'show_images' ),
'show_images' => \get_field( 'show_images' ),
'start' => date( 'Y-m-d' ),
'areas' => '',
'tags' => '',
Expand All @@ -94,8 +94,8 @@ protected function get_events() : array {
$params = $formatter->format_query_params( $params );

$cache_group = 'page-combined-events-list';
$cache_key = md5( wp_json_encode( $params ) );
$response = wp_cache_get( $cache_key, $cache_group );
$cache_key = md5( \wp_json_encode( $params ) );
$response = \wp_cache_get( $cache_key, $cache_group );

if ( empty( $response ) ) {
$response = $this->do_get_events( $params );
Expand All @@ -107,7 +107,7 @@ protected function get_events() : array {
} );

if ( ! empty( $response ) ) {
wp_cache_set(
\wp_cache_set(
$cache_key,
$response,
$cache_group,
Expand Down Expand Up @@ -150,11 +150,11 @@ protected function get_manual_events() : array {

$events = array_map( function ( $e ) {
$id = $e->ID;
$event = (object) get_fields( $id );
$event = (object) \get_fields( $id );
$event->id = $id;
$event->title = get_the_title( $id );
$event->url = get_permalink( $id );
$event->image = has_post_thumbnail( $id ) ? get_the_post_thumbnail_url( $id, 'medium_large' ) : null;
$event->title = \get_the_title( $id );
$event->url = \get_permalink( $id );
$event->image = \has_post_thumbnail( $id ) ? \get_the_post_thumbnail_url( $id, 'medium_large' ) : null;

return PostType\ManualEvent::normalize_event( $event );
}, $query->posts );
Expand Down
Loading

0 comments on commit b59992a

Please sign in to comment.