Skip to content

Commit

Permalink
filterform placeholders #_FILTERWEEKS, #_FILTERMONTHS, #_FILTERYEARS …
Browse files Browse the repository at this point in the history
…extensions
  • Loading branch information
liedekef committed Aug 11, 2024
1 parent 912e36c commit 36d2cf2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 10 deletions.
21 changes: 21 additions & 0 deletions class-expressivedate.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ public function minusDays( $amount ): ExpressiveDate {
*/
public function modifyDays( $amount, $invert = false ) {
$amount = floatval($amount);
if (empty($amount)) {
return $this;
}
if ( $amount < 0 ) {
$amount = abs( $amount );
$invert = true;
Expand Down Expand Up @@ -396,6 +399,9 @@ public function minusMonths( $amount ) {
*/
public function modifyMonths( $amount, $invert = false ) {
$amount = floatval($amount);
if (empty($amount)) {
return $this;
}
if ( $amount < 0 ) {
$amount = abs( $amount );
$invert = true;
Expand Down Expand Up @@ -466,6 +472,9 @@ public function minusYears( $amount ) {
*/
public function modifyYears( $amount, $invert = false ) {
$amount = floatval($amount);
if (empty($amount)) {
return $this;
}
if ( $amount < 0 ) {
$amount = abs( $amount );
$invert = true;
Expand Down Expand Up @@ -536,6 +545,9 @@ public function minusHours( $amount ) {
*/
public function modifyHours( $amount, $invert = false ) {
$amount = floatval($amount);
if (empty($amount)) {
return $this;
}
if ( $amount < 0 ) {
$amount = abs( $amount );
$invert = true;
Expand Down Expand Up @@ -606,6 +618,9 @@ public function minusMinutes( $amount ) {
*/
public function modifyMinutes( $amount, $invert = false ) {
$amount = floatval($amount);
if (empty($amount)) {
return $this;
}
if ( $amount < 0 ) {
$amount = abs( $amount );
$invert = true;
Expand Down Expand Up @@ -677,6 +692,9 @@ public function minusSeconds( $amount ) {
public function modifySeconds( $amount, $invert = false ) {
// seconds are the smallest unit here and need to be an integer, otherwise DateInterval will complain
$amount = intval($amount);
if (empty($amount)) {
return $this;
}
if ( $amount < 0 ) {
$amount = abs( $amount );
$invert = true;
Expand Down Expand Up @@ -742,6 +760,9 @@ public function minusWeeks( $amount ) {
*/
public function modifyWeeks( $amount, $invert = false ) {
$amount = floatval($amount);
if (empty($amount)) {
return $this;
}
if ( $amount < 0 ) {
$amount = abs( $amount );
$invert = true;
Expand Down
67 changes: 57 additions & 10 deletions eme-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ function eme_filter_form_shortcode( $atts ) {
return $form;
}

function eme_create_week_scope( $count, $eventful = 0 ) {
function eme_create_week_scope( $past_count, $future_count, $eventful = 0 ) {
$start_of_week = get_option( 'start_of_week' );
$eme_date_obj = new ExpressiveDate( 'now', EME_TIMEZONE );
if ($past_count) {
$eme_date_obj->minusWeeks($past_count);
}
$count = $past_count + $future_count;
$eme_date_obj->setWeekStartDay( $start_of_week );
$scope = [];
for ( $i = 0; $i < $count; $i++ ) {
Expand All @@ -87,10 +91,14 @@ function eme_create_week_scope( $count, $eventful = 0 ) {
return $scope;
}

function eme_create_month_scope( $count, $eventful = 0 ) {
function eme_create_month_scope( $past_count, $future_count, $eventful = 0 ) {
$scope = [];
$scope[0] = __( 'Select Month', 'events-made-easy' );
$eme_date_obj = new ExpressiveDate( 'now', EME_TIMEZONE );
if ($past_count) {
$eme_date_obj->minusMonths($past_count);
}
$count = $past_count + $future_count;
for ( $i = 0; $i < $count; $i++ ) {
$limit_start = $eme_date_obj->startOfMonth()->format( 'Y-m-d' );
$days_in_month = $eme_date_obj->getDaysInMonth();
Expand All @@ -112,12 +120,15 @@ function eme_create_month_scope( $count, $eventful = 0 ) {
return $scope;
}

function eme_create_year_scope( $count, $eventful = 0 ) {

function eme_create_year_scope( $past_count, $future_count, $eventful = 0 ) {
$scope = [];
$scope[0] = __( 'Select Year', 'events-made-easy' );

$eme_date_obj = new ExpressiveDate( 'now', EME_TIMEZONE );
if ($past_count) {
$eme_date_obj->minusYears($past_count);
}
$count = $past_count + $future_count;
for ( $i = 0; $i < $count; $i++ ) {
$year = $eme_date_obj->getYear();
$limit_start = "$year-01-01";
Expand Down Expand Up @@ -338,22 +349,46 @@ function eme_replace_filter_form_placeholders( $format, $multiple, $multisize, $
}
}
}
} elseif ( preg_match( '/#_(EVENTFUL_)?FILTER_WEEKS/', $result, $matches ) ) {
} elseif ( preg_match( '/#_(EVENTFUL_)?FILTER_WEEKS(\{.+?\})?(\{.+?\})?/', $result, $matches ) ) {
if ( isset( $matches[1] ) && $matches[1] == 'EVENTFUL_' ) {
$eventful = 1;
}
if ( isset( $matches[2] ) ) {
// remove { and } (first and last char of second match)
$past_count = intval(substr( $matches[2], 1, -1 ));
} else {
$past_count = 0;
}
if ( isset( $matches[3] ) ) {
// remove { and } (first and last char of second match)
$future_count = intval(substr( $matches[3], 1, -1 ));
} else {
$future_count = $scope_count;
}
if ( $scope_fieldcount == 0 ) {
$label = __( 'Select Week', 'events-made-easy' );
$aria_label = 'aria-label="' . eme_esc_html( $label ) . '"';
$replacement = eme_ui_select( $selected_scope, $scope_post_name, eme_create_week_scope( $scope_count, $eventful ), $label, 0, '', $aria_label );
$replacement = eme_ui_select( $selected_scope, $scope_post_name, eme_create_week_scope( $past_count, $future_count, $eventful ), $label, 0, '', $aria_label );
++$scope_fieldcount;
}
} elseif ( preg_match( '/#_(EVENTFUL_)?FILTER_MONTHS/', $result, $matches ) ) {
} elseif ( preg_match( '/#_(EVENTFUL_)?FILTER_MONTHS(\{.+?\})?(\{.+?\})?/', $result, $matches ) ) {
if ( isset( $matches[1] ) && $matches[1] == 'EVENTFUL_' ) {
$eventful = 1;
}
if ( isset( $matches[2] ) ) {
// remove { and } (first and last char of second match)
$past_count = intval(substr( $matches[2], 1, -1 ));
} else {
$past_count = 0;
}
if ( isset( $matches[3] ) ) {
// remove { and } (first and last char of second match)
$future_count = intval(substr( $matches[3], 1, -1 ));
} else {
$future_count = $scope_count;
}
if ( $scope_fieldcount == 0 ) {
$replacement = eme_ui_select( $selected_scope, $scope_post_name, eme_create_month_scope( $scope_count, $eventful ) );
$replacement = eme_ui_select( $selected_scope, $scope_post_name, eme_create_month_scope( $past_count, $future_count, $eventful ) );
++$scope_fieldcount;
}
} elseif ( preg_match( '/#_FILTER_MONTHRANGE/', $result ) ) {
Expand All @@ -364,12 +399,24 @@ function eme_replace_filter_form_placeholders( $format, $multiple, $multisize, $
eme_enqueue_datetimepicker();
++$scope_fieldcount;
}
} elseif ( preg_match( '/#_(EVENTFUL_)?FILTER_YEARS/', $result, $matches ) ) {
} elseif ( preg_match( '/#_(EVENTFUL_)?FILTER_YEARS(\{.+?\})?(\{.+?\})?/', $result, $matches ) ) {
if ( isset( $matches[1] ) && $matches[1] == 'EVENTFUL_' ) {
$eventful = 1;
}
if ( isset( $matches[2] ) ) {
// remove { and } (first and last char of second match)
$past_count = intval(substr( $matches[2], 1, -1 ));
} else {
$past_count = 0;
}
if ( isset( $matches[3] ) ) {
// remove { and } (first and last char of second match)
$future_count = intval(substr( $matches[3], 1, -1 ));
} else {
$future_count = $scope_count;
}
if ( $scope_fieldcount == 0 ) {
$replacement = eme_ui_select( $selected_scope, $scope_post_name, eme_create_year_scope( $scope_count, $eventful ) );
$replacement = eme_ui_select( $selected_scope, $scope_post_name, eme_create_year_scope( $past_count, $future_count, $eventful ) );
++$scope_fieldcount;
}
} elseif ( preg_match( '/#_FILTER_CONTACT(\{.+?\})?(\{.+?\})?/', $result, $matches ) ) {
Expand Down
6 changes: 6 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ Events list and calendars can be added to your blogs through widgets, shortcodes
See the FAQ section at the [Official site](https://www.e-dynamics.be/wordpress/).

== Changelog ==
= 2.5.7 (2024//) =
* The filterform placeholders #_FILTERWEEKS, #_FILTERMONTHS, #_FILTERYEARS now take 2 extra optional placeholders that indicate the number of week/months/years in the past and the future you want the scope to be. Example:
#_FILTERYEARS{5}{3} to create a year scope from 5 years in the past till 2 years in the future
#_FILTERYEARS{5} to create a year scope from 5 years in the past till the default scope count (taken from the eme_filterform shortcode) in the future
#_FILTERYEARS to create a year scope from now till the default scope count (taken from the eme_filterform shortcode) in the future

= 2.5.6 (2024/08/10) =
* Fix guest frontend event submit
* Fix redirection to event: if the submitted event is not public and a guest submitted it, we show the success message and don't redirect
Expand Down

0 comments on commit 36d2cf2

Please sign in to comment.