router = $router; } public function register(): void { if ( ! $this->router->is_frontend() ) { return; } add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ), 5 ); add_filter( 'posts_where', array( $this, 'posts_where' ), 10, 2 ); add_filter( 'terms_clauses', array( $this, 'terms_clauses' ), 10, 3 ); add_filter( 'get_next_post_where', array( $this, 'adjacent_where' ) ); add_filter( 'get_previous_post_where', array( $this, 'adjacent_where' ) ); } /** * Whether language filtering is disabled for this request (e.g. sitemaps). */ private function bypass(): bool { return (bool) apply_filters( 'translateplus_bypass_query_filter', false ); } public function pre_get_posts( \WP_Query $q ): void { if ( $this->bypass() || $q->get( 'tp_lang' ) ) { return; } // Singular lookups by slug resolve any language; Router redirects mismatches. if ( $q->is_main_query() && ( $q->get( 'name' ) || $q->get( 'pagename' ) || $q->get( 'p' ) || $q->get( 'page_id' ) || $q->get( 'attachment' ) ) ) { return; } if ( $q->get( 'p' ) || $q->get( 'post__in' ) && ! $q->is_main_query() ) { return; // Explicit IDs are honoured. } if ( ! $this->types_are_translatable( $q ) ) { return; } $q->set( 'tp_lang', Languages::current() ); } private function types_are_translatable( \WP_Query $q ): bool { $types = $q->get( 'post_type' ); if ( empty( $types ) ) { $types = $q->is_page() ? array( 'page' ) : array( 'post' ); if ( $q->is_tax() || $q->is_search() ) { return true; } } if ( 'any' === $types ) { return true; } foreach ( (array) $types as $type ) { if ( PostTypes::is_translatable( (string) $type ) ) { return true; } } return false; } public function posts_where( string $where, \WP_Query $q ): string { $lang = (string) $q->get( 'tp_lang' ); if ( '' === $lang || self::ALL === $lang ) { return $where; } global $wpdb; return $where . self::clause( $wpdb->posts, 'ID', 'post', $lang ); } public function adjacent_where( string $where ): string { if ( $this->bypass() ) { return $where; } return $where . self::clause( 'p', 'ID', 'post', Languages::current() ); } /** * @param array $clauses SQL clauses. * @param string[] $taxonomies Taxonomies. * @param array $args get_terms args. */ public function terms_clauses( $clauses, $taxonomies, $args ) { if ( $this->bypass() || ! empty( $args['include'] ) || ! empty( $args['object_ids'] ) || ( isset( $args['tp_lang'] ) && self::ALL === $args['tp_lang'] ) ) { return $clauses; } foreach ( (array) $taxonomies as $tax ) { if ( ! PostTypes::is_translatable_taxonomy( (string) $tax ) ) { return $clauses; } } $clauses['where'] .= self::clause( 't', 'term_id', 'term', Languages::current() ); return $clauses; } /** * SQL fragment restricting an ID column (table or alias + column) to a language. */ public static function clause( string $table, string $column, string $type, string $lang ): string { global $wpdb; if ( $lang === Languages::source() ) { return $wpdb->prepare( ' AND %i.%i NOT IN (SELECT object_id FROM %i WHERE object_type = %s AND lang <> %s)', $table, $column, $wpdb->tp_translation_groups, $type, $lang ); } return $wpdb->prepare( ' AND %i.%i IN (SELECT object_id FROM %i WHERE object_type = %s AND lang = %s)', $table, $column, $wpdb->tp_translation_groups, $type, $lang ); } }