Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Extracts the date from the custom permalink of a recurring event.
- *
- * This function parses URIs to isolate event dates in YYYY-MM-DD format.
- * If a valid date is found, it adjusts the URI and sets corresponding endpoint values.
- *
- * @param array $uri_parts The components of the parsed URI.
- * @param string $request_url The original requested URL.
- * @param array $endpoints The available permalink manager endpoints.
- *
- * @return array Modified URI parts with extracted date, if available.
- */
- function pm_parse_recurring_event_uri($uri_parts, $request_url, $endpoints) {
- preg_match('/(.*)\/([\d]{4}-[\d]{2}-[\d]{2})$/', $uri_parts['uri'], $event_uri_parts);
- if(!empty($event_uri_parts[2])) {
- $uri_parts['uri'] = $event_uri_parts[1];
- $uri_parts['endpoint'] = 'sc_event_occurrence_date';
- $uri_parts['endpoint_value'] = $event_uri_parts[2];
- }
- return $uri_parts;
- }
- add_filter('permalink_manager_detect_uri', 'pm_parse_recurring_event_uri', 5, 3);
- /**
- * Adjusts query parameters to handle recurring event permalinks when using Permalink Manager.
- *
- * @param array $query Modified query parameters.
- * @param array $old_query Original query parameters.
- * @param array $uri_parts Parsed URI segments.
- * @param array $pm_query Permalink Manager specific query details.
- * @param string $content_type Content type handled by Permalink Manager.
- *
- * @return array Modified query parameters.
- */
- function pm_query_recurring_events( $query, $old_query, $uri_parts, $pm_query, $content_type ) {
- if ( ! empty( $query['sc_recurring_event'] ) && ! empty( $pm_query['id'] ) ) {
- $query['sc_event_parent'] = $query['sc_recurring_event'];
- if ( empty( $pm_query['endpoint'] ) && function_exists( 'sugar_calendar_get_event_by' ) ) {
- $first_occurrence = sugar_calendar_get_event_by( 'object_id', $pm_query['id'] );
- if ( $first_occurrence && method_exists( $first_occurrence, 'start_date_dto' ) ) {
- $query['sc_event_occurrence_date'] = $first_occurrence->start_date_dto( 'Y-m-d' );
- }
- }
- unset( $query['name'] );
- unset( $query['post_type'] );
- unset( $query['sc_recurring_event'] );
- } else if ( ! empty( $query['sc_event_occurrence_date'] ) ) {
- $query['do_not_redirect'] = 1;
- }
- return $query;
- }
- add_filter( 'permalink_manager_filter_query', 'pm_query_recurring_events', 5, 5 );
- /**
- * Overwrites URLs for recurring events, adding the occurrence date to the permalink.
- *
- * @param string $link Original URL for the event.
- * @param object $event Event object containing details.
- *
- * @return string Modified URL with appended date for recurring events.
- */
- function pm_overwrite_recurring_events_urls( $link, $event ) {
- if ( ! empty( $event->recurrence ) && ! empty( $event->object_id ) && method_exists( $event, 'start_date_dto' ) ) {
- $new_link = sprintf( '%s/%s', trim( get_permalink( $event->object_id ), '/' ), $event->start_date_dto( 'Y-m-d' ) );
- return user_trailingslashit( $new_link );
- }
- return $link;
- }
- add_filter( 'sugar_calendar_get_non_standard_event_frontend_url', 'pm_overwrite_recurring_events_urls', 10000, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement