query argument set on the query. || ! empty( $query->get( $ignore_flag, false ) ) ) { // The query should be ignored, move on. return []; } if ( $this->modifiers->contains( $query ) ) { // Already attached Modifiers to this query? return $this->modifiers[ $query ]; } $modifiers = []; foreach ( $this->get_implementations() as $class_name ) { $modifier = $this->container->make( $class_name ); if ( $modifier instanceof WP_Query_Modifier && $modifier->applies_to( $query ) ) { $query->set( 'tribe_remove_date_filters', true ); $query->tribe_remove_date_filters = true; $query->set( 'tribe_include_date_meta', false ); $query->tribe_include_date_meta = false; $modifier->set_query( $query ); $query->ical_modifier = $modifier; $this->register_modifier_for( $modifier, $query ); $modifiers[] = $modifier; } else { unset( $modifier ); } } return $modifiers; } /** * Registers the attachment of a Modifier to a `WP_Query` instance. * * @since 6.0.0 * * @param WP_Query_Modifier $modifier A reference to the Modifier instance attached to the `WP_Query` instance. * @param WP_Query $query A reference to the `WP_Query` object the modifier is attached to. */ private function register_modifier_for( WP_Query_Modifier $modifier, WP_Query $query ) { if ( ! isset( $this->modifiers[ $query ] ) ) { $this->modifiers[ $query ] = []; } $this->modifiers[ $query ] = array_merge( $this->modifiers[ $query ], [ $modifier ] ); $modifier_class = get_class( $modifier ); add_action( "tec_events_custom_tables_v1_{$modifier_class}_done", $this->drop_modifiers( $query ) ); } /** * Returns a closure that will remove a set of Modifiers from the tracked Modifiers. * * @since 6.0.0 * * @param WP_Query $query The query that will be used as key to remove the modifiers. * * @return Closure A Closure that will remove the modifiers, using the Query as key, * from the list of modifiers. */ private function drop_modifiers( WP_Query $query ) { return function () use ( $query ) { if ( $this->keep_modifiers_reference ) { return; } if ( ! isset( $this->modifiers[ $query ] ) ) { return; } unset( $this->modifiers[ $query ] ); }; } /** * Return the number of Queries to which at least one modifier is attached. * * @since 6.0.0 * * @return int The number of modifier instances. */ #[\ReturnTypeWillChange] public function count() { return $this->modifiers->count(); } /** * Remove instances from the modifiers so it can be garbage collected. * * @since 6.0.0 */ public function detach() { while ( $this->modifiers->valid() ) { $item = $this->modifiers->current(); $this->modifiers->next(); $this->modifiers->detach( $item ); } } /** * Returns a reference to the Modifier instance attached to a specific `WP_Query` instance, if any. * * @since 6.0.0 * * @param WP_Query $wp_query A reference to the `WP_Query` object to fetch the Modifier instance for. * @param int $index The index of the modifier to return * * @return WP_Query_Modifier|null Either a reference to the Modifier instance attached to the `WP_Query` * instance, or `null` if no Modifier is attached to it. */ public function get_modifier_for_query( WP_Query $wp_query, $index = 0 ) { if ( $this->modifiers->contains( $wp_query ) ) { return $this->modifiers[ $wp_query ][ $index ]; } return null; } /** * Disables the Monitor. * * If the Monitor was already disabled, the method will not have any effect. * * @since 6.0.0 */ public function disable() { $this->enabled = false; } /** * Enables the Monitor. * * If the Monitor was already enabled, the method will not have any effect. * * @since 6.0.0 */ public function enable() { $this->enabled = true; } /** * Sets whether the Monitor should keep a reference to "done" Modifiers or not. * * By default, the Monitor will try to remove a reference to a Modifier that has * completed its work, and the Query it modified, to allow for the cascading garbage * collection to happen. * Think of it like a "weak map" implementation built on what we have. * Setting the flat to `true` will force the Monitor to keep references to all the modifiers * and cascading objects referenced by them: use only for testing, please. * * @since 6.0.0 * * @param bool $keep_modifiers_reference Whether the Monitor should keep a reference * to "done" Modifiers or not. */ public function keep_modifiers_reference( $keep_modifiers_reference ) { $this->keep_modifiers_reference = (bool) $keep_modifiers_reference; } }