diff --git a/class-expressivedate.php b/class-expressivedate.php index 4fa0f9b3..8e19c766 100644 --- a/class-expressivedate.php +++ b/class-expressivedate.php @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/eme-filters.php b/eme-filters.php index fcb82eab..ff007e6d 100644 --- a/eme-filters.php +++ b/eme-filters.php @@ -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++ ) { @@ -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(); @@ -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"; @@ -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 ) ) { @@ -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 ) ) { diff --git a/readme.txt b/readme.txt index 11ef8c5d..7e7d6bb9 100644 --- a/readme.txt +++ b/readme.txt @@ -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