Skip to content

Commit

Permalink
Merge pull request #510 from devgeniem/TMS-1043
Browse files Browse the repository at this point in the history
TMS-1043: Eventz optimization
  • Loading branch information
eebbi authored Jun 4, 2024
2 parents 8f0a3da + d99f239 commit f8d2bff
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

- TMS-1043:
- Fix date checking for event
- Minimize the amount of event data loops

## [1.54.11] - 2024-05-14

- TMS-1029: Change contacts-blocks width
Expand Down
52 changes: 46 additions & 6 deletions lib/Eventz.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,45 @@ public static function normalize_event( $event ) : array {
];
}

/**
* Normalize event name data
*
* @param object $event Event object.
*
* @return array
*/
public static function normalize_event_title( $event ) : array {
return [
'name' => $event->name ?? null,
];
}

/**
* Normalize event description data
*
* @param object $event Event object.
*
* @return array
*/
public static function normalize_event_description( $event ) : array {
return [
'short_description' => static::get_short_description( $event ) ?? null,
];
}

/**
* Normalize event url data
*
* @param object $event Event object.
*
* @return array
*/
public static function normalize_event_url( $event ) : array {
return [
'url' => static::get_event_url( $event->_id ),
];
}

/**
* Get event data for json+ld
*
Expand Down Expand Up @@ -181,16 +220,14 @@ public static function get_event_date( $event ) {
return null;
}

$start_time = static::get_as_datetime( $event->event->start );
$end_time = static::get_as_datetime( $event->event->end );
$date_format = get_option( 'date_format' );
$date_format = \get_option( 'date_format' );

// If date-parameter exists in url
if ( ! empty( $_GET['date'] ) ) {
list( $start_date, $end_date ) = array_merge( explode( ' - ', $_GET['date'] ), array( true ) );
list( $start_date, $end_date ) = array_merge( explode( ' - ', urldecode( $_GET['date'] ) ), array( false ) );

$start_datetime = static::get_as_datetime( $start_date );
$end_datetime = ! is_null($end_date) ? static::get_as_datetime( $end_date ) : '';
$end_datetime = ! empty( $end_date ) ? static::get_as_datetime( $end_date ) : '';

if ( $start_datetime && $end_datetime && $start_datetime->diff( $end_datetime )->days >= 1 ) {
return sprintf(
Expand All @@ -203,6 +240,9 @@ public static function get_event_date( $event ) {
return $start_datetime->format( $date_format );
}

$start_time = static::get_as_datetime( $event->event->start );
$end_time = static::get_as_datetime( $event->event->end );

if ( $start_time && $end_time && $start_time->diff( $end_time )->days >= 1 ) {
return sprintf(
'%s - %s',
Expand All @@ -228,7 +268,7 @@ public static function get_event_time( $event ) {

// If time-parameter exists in url
if ( ! empty( $_GET['time'] ) ) {
list( $start_time, $end_time) = array_merge( explode( ' - ', urldecode( $_GET['time'] ) ), array( false ) );
list( $start_time, $end_time ) = array_merge( explode( ' - ', urldecode( $_GET['time'] ) ), array( false ) );

if ( $start_time && $end_time ) {
return sprintf(
Expand Down
22 changes: 11 additions & 11 deletions models/page-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,41 +38,41 @@ class PageEvent extends BaseModel {
* @return void
*/
public function hooks() : void {
add_filter(
\add_filter(
'the_seo_framework_title_from_generation',
Closure::fromCallable( [ $this, 'alter_title' ] )
);

// og:title.
add_filter(
\add_filter(
'the_seo_framework_title_from_custom_field',
Closure::fromCallable( [ $this, 'alter_title' ] )
);

// og:image.
add_filter(
\add_filter(
'the_seo_framework_image_generation_params',
Closure::fromCallable( [ $this, 'alter_image' ] )
);

// og:description.
add_filter(
\add_filter(
'the_seo_framework_custom_field_description',
Closure::fromCallable( [ $this, 'alter_desc' ] )
);

// og:url.
add_filter(
\add_filter(
'the_seo_framework_ogurl_output',
Closure::fromCallable( [ $this, 'alter_url' ] )
);

add_action(
\add_action(
'wp_head',
Closure::fromCallable( [ $this, 'add_json_ld_data' ] )
);

add_filter(
\add_filter(
'tms/base/breadcrumbs/after_prepare',
Closure::fromCallable( [ $this, 'alter_breadcrumbs' ] )
);
Expand Down Expand Up @@ -109,14 +109,14 @@ protected function add_json_ld_data() : void {
* @return string
*/
protected function alter_title( string $title ) : string {
if ( ! is_page_template( static::TEMPLATE ) ) {
if ( ! \is_page_template( static::TEMPLATE ) ) {
return $title;
}

$event = $this->get_event();

if ( $event ) {
$event = Eventz::normalize_event( $event );
$event = Eventz::normalize_event_title( $event );
$title = $event['name'];
}

Expand Down Expand Up @@ -174,7 +174,7 @@ protected function alter_desc( $description ) {
$event = $this->get_event();

if ( $event ) {
$event = Eventz::normalize_event( $event );
$event = Eventz::normalize_event_description( $event );
$description = $event['short_description'];
}

Expand All @@ -192,7 +192,7 @@ protected function alter_url( $url ) {
$event = $this->get_event();

if ( $event ) {
$event = Eventz::normalize_event( $event );
$event = Eventz::normalize_event_url( $event );
$url = $event['url'];
}

Expand Down

0 comments on commit f8d2bff

Please sign in to comment.