Advertisement
mbis

Sugar Calendar Pro + Permalink Manager

Jun 4th, 2025 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.09 KB | None | 0 0
  1. /**
  2.  * Extracts the date from the custom permalink of a recurring event.
  3.  *
  4.  * This function parses URIs to isolate event dates in YYYY-MM-DD format.
  5.  * If a valid date is found, it adjusts the URI and sets corresponding endpoint values.
  6.  *
  7.  * @param array  $uri_parts    The components of the parsed URI.
  8.  * @param string $request_url  The original requested URL.
  9.  * @param array  $endpoints    The available permalink manager endpoints.
  10.  *
  11.  * @return array Modified URI parts with extracted date, if available.
  12.  */
  13. function pm_parse_recurring_event_uri($uri_parts, $request_url, $endpoints) {
  14.     preg_match('/(.*)\/([\d]{4}-[\d]{2}-[\d]{2})$/', $uri_parts['uri'], $event_uri_parts);
  15.    
  16.     if(!empty($event_uri_parts[2])) {
  17.         $uri_parts['uri'] = $event_uri_parts[1];
  18.         $uri_parts['endpoint'] = 'sc_event_occurrence_date';
  19.         $uri_parts['endpoint_value'] = $event_uri_parts[2];
  20.     }
  21.    
  22.     return $uri_parts;
  23. }
  24. add_filter('permalink_manager_detect_uri', 'pm_parse_recurring_event_uri', 5, 3);
  25.  
  26. /**
  27.  * Adjusts query parameters to handle recurring event permalinks when using Permalink Manager.
  28.  *
  29.  * @param array $query Modified query parameters.
  30.  * @param array $old_query Original query parameters.
  31.  * @param array $uri_parts Parsed URI segments.
  32.  * @param array $pm_query Permalink Manager specific query details.
  33.  * @param string $content_type Content type handled by Permalink Manager.
  34.  *
  35.  * @return array Modified query parameters.
  36.  */
  37. function pm_query_recurring_events( $query, $old_query, $uri_parts, $pm_query, $content_type ) {
  38.     if ( ! empty( $query['sc_recurring_event'] ) && ! empty( $pm_query['id'] ) ) {
  39.         $query['sc_event_parent'] = $query['sc_recurring_event'];
  40.  
  41.         if ( empty( $pm_query['endpoint'] ) && function_exists( 'sugar_calendar_get_event_by' ) ) {
  42.             $first_occurrence = sugar_calendar_get_event_by( 'object_id', $pm_query['id'] );
  43.  
  44.             if ( $first_occurrence && method_exists( $first_occurrence, 'start_date_dto' ) ) {
  45.                 $query['sc_event_occurrence_date'] = $first_occurrence->start_date_dto( 'Y-m-d' );
  46.             }
  47.         }
  48.  
  49.         unset( $query['name'] );
  50.         unset( $query['post_type'] );
  51.         unset( $query['sc_recurring_event'] );
  52.     } else if ( ! empty( $query['sc_event_occurrence_date'] ) ) {
  53.         $query['do_not_redirect'] = 1;
  54.     }
  55.  
  56.     return $query;
  57. }
  58. add_filter( 'permalink_manager_filter_query', 'pm_query_recurring_events', 5, 5 );
  59.  
  60. /**
  61.  * Overwrites URLs for recurring events, adding the occurrence date to the permalink.
  62.  *
  63.  * @param string $link Original URL for the event.
  64.  * @param object $event Event object containing details.
  65.  *
  66.  * @return string Modified URL with appended date for recurring events.
  67.  */
  68. function pm_overwrite_recurring_events_urls( $link, $event ) {
  69.     if ( ! empty( $event->recurrence ) && ! empty( $event->object_id ) && method_exists( $event, 'start_date_dto' ) ) {
  70.         $new_link = sprintf( '%s/%s', trim( get_permalink( $event->object_id ), '/' ), $event->start_date_dto( 'Y-m-d' ) );
  71.  
  72.         return user_trailingslashit( $new_link );
  73.     }
  74.  
  75.     return $link;
  76. }
  77. 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