$vars Parsed query vars. * @return array */ public function filter_request( array $vars ): array { if ( ! isset( $vars['random'] ) ) { return $vars; } $settings = self::settings(); if ( ! $settings['enabled'] || self::is_allowed( $vars, $settings['allow'], $settings['terms'] ) ) { return $vars; } unset( $vars['random'] ); // ?random added to a normal address: show that page as usual. if ( isset( $_GET['random'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only: only tells a query string apart from a /random/ path. return $vars; } // A /random/… address: it only exists for random content. return array( 'error' => '404' ); } /** * Whether a random request is for a kept archive. The site-wide * /random/, searches, date archives and author archives never are. * * @param array $vars Parsed query vars. * @param string[] $allow Kept choice keys (`tax:x`, `type:x`). * @param string[] $terms Kept term slugs; empty for all. * @return bool */ public static function is_allowed( array $vars, array $allow, array $terms ): bool { if ( ! $allow || ! empty( $vars['author'] ) || ! empty( $vars['author_name'] ) || isset( $vars['s'] ) ) { return false; } $found = false; foreach ( self::taxonomy_vars() as $taxonomy => $query_var ) { if ( empty( $vars[ $query_var ] ) ) { continue; } if ( ! in_array( 'tax:' . $taxonomy, $allow, true ) ) { return false; } // category_name holds the full path (parent/child): the last part is the term. $parts = explode( '/', trim( (string) $vars[ $query_var ], '/' ) ); $slug = (string) end( $parts ); if ( $terms && ! in_array( $slug, $terms, true ) ) { return false; } $found = true; } if ( $found ) { return true; } if ( ! empty( $vars['post_type'] ) && is_string( $vars['post_type'] ) ) { return in_array( 'type:' . $vars['post_type'], $allow, true ); } return false; } /** * After the first save of the Performance tab. * * @param string $option Option name. * @param mixed $value New value. * @return void */ public function option_added( string $option, mixed $value ): void { $this->option_updated( array(), $value ); } /** * Rebuild the permalink rules when this section changes, so core's * /random/ rules are added or removed. WordPress regenerates the * saved rules on the next request once they are deleted. * * @param mixed $old_value Previous value. * @param mixed $value New value. * @return void */ public function option_updated( mixed $old_value, mixed $value ): void { $old = self::normalize( is_array( $old_value ) ? (array) ( $old_value[ Module::SECTION ] ?? array() ) : array() ); $new = self::normalize( is_array( $value ) ? (array) ( $value[ Module::SECTION ] ?? array() ) : array() ); if ( $old !== $new ) { delete_option( 'rewrite_rules' ); } } /** * Plugin deactivation: let WordPress rebuild its rules without this * plugin's filter if it had removed the /random/ rules. * * @return void */ public static function deactivate(): void { if ( self::settings()['enabled'] ) { delete_option( 'rewrite_rules' ); } } /** * Saved settings, read straight from the option: `filter_enabled()` * runs before the settings registry exists. * * @return array{enabled: bool, allow: string[], terms: string[]} */ public static function settings(): array { $stored = get_option( self::option_name(), array() ); return self::normalize( is_array( $stored ) ? (array) ( $stored[ Module::SECTION ] ?? array() ) : array() ); } /** * Saved section values in a fixed shape. * * @param array $section Raw section values. * @return array{enabled: bool, allow: string[], terms: string[]} */ private static function normalize( array $section ): array { $allow = array_values( array_filter( array_map( 'strval', (array) ( $section['allow'] ?? array() ) ) ) ); $terms = preg_split( '/[\s,]+/', strtolower( (string) ( $section['terms'] ?? '' ) ) ); $terms = array_values( array_unique( array_filter( array_map( 'sanitize_title', (array) $terms ) ) ) ); return array( 'enabled' => ! empty( $section['enabled'] ), 'allow' => $allow, 'terms' => $terms, ); } /** * Taxonomy name => query var, for publicly queryable taxonomies. * * @return array */ private static function taxonomy_vars(): array { $vars = array(); foreach ( get_taxonomies( array( 'publicly_queryable' => true ), 'objects' ) as $taxonomy ) { if ( $taxonomy->query_var ) { $vars[ $taxonomy->name ] = (string) $taxonomy->query_var; } } // Core's own query vars for these two, whatever their query_var says. $vars['category'] = 'category_name'; $vars['post_tag'] = 'tag'; return $vars; } /** * The option the Performance tab saves to. * * @return string */ private static function option_name(): string { return 'aiou_settings_' . Module::TAB; } }