Skip to content

Commit

Permalink
Merge pull request #155 from devgeniem/TMS-208
Browse files Browse the repository at this point in the history
  • Loading branch information
Ville committed Sep 7, 2021
2 parents 4d8fc11 + b5e8380 commit 1b4079d
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 17 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.MD
Expand Up @@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- TMS-208: Event search today-option #155

### Fixed
- TMS-208: Event search keywords #155
- TMS-208: Event page breadcrumbs #155
- TMS-208: Dynamic event breadcrumbs #155
- TMS-208: Subpage page order #155

## [1.10.0]

Expand Down
27 changes: 23 additions & 4 deletions lib/ACF/DynamicEventGroup.php
Expand Up @@ -219,9 +219,31 @@ protected function fill_keyword_choices( array $field ) : array {
return $field;
}

$response = $this->get_choices(
'keyword_set',
[

'data_source' => 'system,tampere',
'include' => 'keywords',
]
);

$keywords = [];
$sets = array_filter( $response, fn( $set ) => $set->usage === 'keyword' );

foreach ( $sets as $set ) {
if ( empty( $set->keywords ) ) {
continue;
}

foreach ( $set->keywords as $keyword ) {
$keywords[] = $keyword;
}
}

return $this->fill_choices_from_response(
$field,
$this->get_choices( 'keyword', [ 'page_size' => 250 ] ),
$keywords
);
}

Expand Down Expand Up @@ -272,9 +294,6 @@ protected function get_choices( string $slug, array $params = [], int $cache_dur
catch ( LinkedEventsException $e ) {
( new Logger() )->error( $e->getMessage(), $e->getTrace() );
}
catch ( \JsonException $e ) {
( new Logger() )->error( $e->getMessage(), $e->getTrace() );
}
}

return $response ?? [];
Expand Down
33 changes: 23 additions & 10 deletions lib/ACF/Fields/EventsFields.php
Expand Up @@ -41,35 +41,39 @@ public function __construct( $label = '', $key = null, $name = null ) {
*/
protected function sub_fields() : array {
$strings = [
'title' => [
'title' => [
'label' => 'Otsikko',
'instructions' => '',
],
'start' => [
'start' => [
'label' => 'Alkupäivämäärä',
'instructions' => '',
],
'end' => [
'end' => [
'label' => 'Loppupäivämäärä',
'instructions' => '',
],
'keyword' => [
'starts_today' => [
'label' => 'Alkaa tänään',
'instructions' => 'Alkupäivämääränä käytetään kuluvaa päivää',
],
'keyword' => [
'label' => 'Avainsana',
'instructions' => '',
],
'location' => [
'location' => [
'label' => 'Tapahtumapaikka',
'instructions' => '',
],
'publisher' => [
'publisher' => [
'label' => 'Julkaisija',
'instructions' => '',
],
'text' => [
'text' => [
'label' => 'Vapaasanahaku',
'instructions' => '',
],
'sort' => [
'sort' => [
'label' => 'Järjestys',
'instructions' => '',
'choices' => [
Expand All @@ -83,11 +87,11 @@ protected function sub_fields() : array {
'last_modified_time' => 'Viimeksi muokattu (nouseva)',
],
],
'page_size' => [
'page_size' => [
'label' => 'Näytettävien tapahtumien määrä',
'instructions' => '',
],
'show_images' => [
'show_images' => [
'label' => 'Näytä kuvat',
'instructions' => '',
],
Expand Down Expand Up @@ -116,6 +120,14 @@ protected function sub_fields() : array {
->set_wrapper_width( 50 )
->set_instructions( $strings['end']['instructions'] );

$starts_today_field = ( new Field\TrueFalse( $strings['starts_today']['label'] ) )
->set_key( "${key}_starts_today" )
->set_name( 'starts_today' )
->use_ui()
->set_default_value( false )
->set_wrapper_width( 50 )
->set_instructions( $strings['starts_today']['instructions'] );

$keyword_field = ( new Field\Select( $strings['keyword']['label'] ) )
->set_key( "${key}_keyword" )
->set_name( 'keyword' )
Expand Down Expand Up @@ -178,6 +190,7 @@ protected function sub_fields() : array {
$title_field,
$start_field,
$end_field,
$starts_today_field,
$keyword_field,
$location_field,
$publisher_field,
Expand Down
2 changes: 1 addition & 1 deletion lib/ACF/Fields/Settings/FooterSettingsTab.php
Expand Up @@ -36,7 +36,7 @@ class FooterSettingsTab extends Tab {
'instructions' => '',
],
'contact_title' => [
'title' => 'Yhteystietojen osoite',
'title' => 'Yhteystietojen otsikko',
'instructions' => '',
],
'address' => [
Expand Down
4 changes: 4 additions & 0 deletions lib/Formatters/EventsFormatter.php
Expand Up @@ -115,6 +115,10 @@ private function format_query_params( array $layout ) : array {
}
}

if ( ! empty( $layout['starts_today'] ) && true === $layout['starts_today'] ) {
$query_params['start'] = 'today';
}

return $query_params;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/Formatters/SubpagesFormatter.php
Expand Up @@ -68,6 +68,8 @@ private function get_subpages( array $data ) : array {
'update_post_term_cache' => false,
'no_found_rows' => true,
'fields' => 'ids',
'order' => 'ASC',
'orderby' => 'menu_order',
];

$wp_query = new WP_Query( $args );
Expand Down
51 changes: 49 additions & 2 deletions models/page-event.php
Expand Up @@ -7,6 +7,7 @@
use Geniem\LinkedEvents\LinkedEventsClient;
use TMS\Theme\Base\LinkedEvents;
use TMS\Theme\Base\Logger;
use TMS\Theme\Base\Settings;
use TMS\Theme\Base\Traits\Components;

/**
Expand Down Expand Up @@ -43,6 +44,11 @@ public function hooks() : void {
'wp_head',
Closure::fromCallable( [ $this, 'add_json_ld_data' ] )
);

add_filter(
'tms/base/breadcrumbs/after_prepare',
Closure::fromCallable( [ $this, 'alter_breadcrumbs' ] )
);
}

/**
Expand Down Expand Up @@ -109,13 +115,17 @@ protected function get_home_url() {
public function hero_image() {
$event = $this->get_event();

if ( empty( $event ) || empty( $event->images ) ) {
if ( empty( $event ) ) {
return false;
}

$default_image = empty( Settings::get_setting( 'events_default_image' ) )
? null
: wp_get_attachment_image_url( Settings::get_setting( 'events_default_image' ), 'large' );

return ! empty( $event->images[0]->url )
? $event->images[0]->url
: false;
: $default_image;
}

/**
Expand Down Expand Up @@ -234,4 +244,41 @@ public function event() {
'orig' => $event,
];
}

/**
* Alter breadcrumbs
*
* @param array $breadcrumbs Array of breadcrumbs.
*
* @return array
*/
public function alter_breadcrumbs( array $breadcrumbs ) : array {
$referer = wp_get_referer();
$home_url = DPT_PLL_ACTIVE && function_exists( 'pll_current_language' )
? pll_home_url()
: home_url();

if ( false === strpos( $referer, $home_url ) ) {
return $breadcrumbs;
}

$parent = get_page_by_path(
str_replace( $home_url, '', $referer )
);

if ( empty( $parent ) ) {
return $breadcrumbs;
}

$last = array_pop( $breadcrumbs );

$breadcrumbs[] = [
'title' => $parent->post_title,
'permalink' => get_the_permalink( $parent->ID ),
];

$breadcrumbs[] = $last;

return $breadcrumbs;
}
}
4 changes: 4 additions & 0 deletions models/page-events-calendar.php
Expand Up @@ -148,6 +148,10 @@ private function do_get_events() : array {
'show_images' => get_field( 'show_images' ),
];

if ( ! empty( get_field( 'starts_today' ) ) && true === get_field( 'starts_today' ) ) {
$params['start'] = 'today';
}

$cache_group = 'page-events-calendar';
$cache_key = md5( wp_json_encode( $params ) );
$events = wp_cache_get( $cache_key, $cache_group );
Expand Down
42 changes: 42 additions & 0 deletions models/single-dynamic-event-cpt.php
Expand Up @@ -18,6 +18,11 @@ class SingleDynamicEventCpt extends PageEvent {
*/
public function hooks() : void {
add_filter( 'tms/theme/breadcrumbs/show_breadcrumbs_in_header', fn() => false );

add_filter(
'tms/base/breadcrumbs/after_prepare',
Closure::fromCallable( [ $this, 'alter_breadcrumbs' ] )
);
}

/**
Expand All @@ -39,4 +44,41 @@ public function hero_image() {
protected function get_event_id() : string {
return get_field( 'event' ) ?? '';
}

/**
* Alter breadcrumbs
*
* @param array $breadcrumbs Array of breadcrumbs.
*
* @return array
*/
public function alter_breadcrumbs( array $breadcrumbs ) : array {
$referer = wp_get_referer();
$home_url = DPT_PLL_ACTIVE && function_exists( 'pll_current_language' )
? pll_home_url()
: home_url();

if ( false === strpos( $referer, $home_url ) ) {
return $breadcrumbs;
}

$parent = get_page_by_path(
str_replace( $home_url, '', $referer )
);

if ( empty( $parent ) ) {
return $breadcrumbs;
}

$last = array_pop( $breadcrumbs );

$breadcrumbs[] = [
'title' => $parent->post_title,
'permalink' => get_the_permalink( $parent->ID ),
];

$breadcrumbs[] = $last;

return $breadcrumbs;
}
}

0 comments on commit 1b4079d

Please sign in to comment.