guid ) ) { return true; } return false; } /** * Determine whether a WP post is a cached external event. * * @param WP_Post $post The WordPress post object. * @return bool */ public static function is_cached_external_event( $post ): bool { if ( ! str_starts_with( \get_site_url(), $post->guid ) ) { return true; } return false; } /** * Add the ActivityPub template for EventPrime. * * @param string $template The path to the template object. * @return string The new path to the JSON template. */ public static function redirect_activitypub_requests_for_cached_external_events( $template ) { if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { return $template; } if ( ! is_activitypub_request() ) { return $template; } if ( ! \is_singular() ) { return $template; } global $post; if ( 'gatherpress_event' !== $post->post_type ) { return $template; } if ( self::is_cached_external_event( $post ) ) { \wp_safe_redirect( $post->guid, 301 ); exit; } return $template; } /** * Delete old cached events that took place in the past. */ public static function clear_cache() { $cache_retention_period = get_option( 'event_bridge_for_activitypub_event_source_cache_retention', WEEK_IN_SECONDS ); $past_event_ids = GatherPress::get_past_events( $cache_retention_period ); foreach ( $past_event_ids as $post_id ) { if ( has_post_thumbnail( $post_id ) ) { $attachment_id = get_post_thumbnail_id( $post_id ); wp_delete_attachment( $attachment_id, true ); } wp_delete_post( $post_id, true ); } } }