| @@ -72,8 +72,13 @@ | ||
| 72 | 72 | add_action( 'add_attachment', array( $this, 'set_default_language' ) ); |
| 73 | 73 | add_action( 'delete_attachment', array( $this, 'delete_post' ) ); |
| 74 | 74 | add_filter( 'wp_delete_file', array( $this, 'wp_delete_file' ) ); |
| 75 | 75 | } |
| 76 | + | |
| 77 | + // Filter query. | |
| 78 | + add_filter( 'query_vars', array( $this, 'add_query_vars' ) ); | |
| 79 | + add_action( 'parse_query', array( $this, 'parse_query' ) ); | |
| 80 | + add_filter( 'posts_clauses', array( $this, 'posts_clauses' ), 10, 2 ); | |
| 76 | 81 | } |
| 77 | 82 | |
| 78 | 83 | /** |
| 79 | 84 | * Allows to set a language by default for posts if it has no language yet. |
| @@ -481,6 +486,88 @@ | ||
| 481 | 486 | |
| 482 | 487 | $this->model->term->save_translations( $term->term_id, $trs ); |
| 483 | 488 | |
| 484 | 489 | return $tr_term_id; |
| 490 | + } | |
| 491 | + | |
| 492 | + /** | |
| 493 | + * Add public query vars. Currently only 'untranslated in'. | |
| 494 | + * | |
| 495 | + * @since 3.9 | |
| 496 | + * | |
| 497 | + * @param string[] $qvars The array of allowed query variable names. | |
| 498 | + * @return string[] | |
| 499 | + */ | |
| 500 | + public function add_query_vars( $qvars ) { | |
| 501 | + $qvars[] = 'untranslated_in'; | |
| 502 | + return $qvars; | |
| 503 | + } | |
| 504 | + | |
| 505 | + /** | |
| 506 | + * Ensures filters are not suppressed when querying untranslated posts. | |
| 507 | + * | |
| 508 | + * @since 3.9 | |
| 509 | + * | |
| 510 | + * @param WP_Query $query The WP_Query instance (passed by reference). | |
| 511 | + * @return void | |
| 512 | + */ | |
| 513 | + public function parse_query( $query ) { | |
| 514 | + if ( ! empty( $query->query['untranslated_in'] ) ) { | |
| 515 | + unset( $query->query['suppress_filters'] ); | |
| 516 | + unset( $query->query_vars['suppress_filters'] ); | |
| 517 | + } | |
| 518 | + } | |
| 519 | + | |
| 520 | + /** | |
| 521 | + * Filters the WP_Query SQL clauses to remove posts: | |
| 522 | + * - translated in the language queried by `unstranslated_in`. | |
| 523 | + * - in the language queried by `unstranslated_in`. | |
| 524 | + * | |
| 525 | + * This second condition is necessary as a post not translated in any language | |
| 526 | + * has no translation group. | |
| 527 | + * | |
| 528 | + * @since 3.9 | |
| 529 | + * | |
| 530 | + * @param string[] $clauses The clauses for the query. | |
| 531 | + * @param WP_Query $query The WP_Query instance (passed by reference). | |
| 532 | + * @return string[] | |
| 533 | + */ | |
| 534 | + public function posts_clauses( $clauses, $query ) { | |
| 535 | + global $wpdb; | |
| 536 | + | |
| 537 | + if ( empty( $query->query['untranslated_in'] ) ) { | |
| 538 | + return $clauses; | |
| 539 | + } | |
| 540 | + | |
| 541 | + $untranslated_in = PLL()->model->languages->get( $query->query['untranslated_in'] ); | |
| 542 | + | |
| 543 | + if ( empty( $untranslated_in ) ) { | |
| 544 | + return $clauses; | |
| 545 | + } | |
| 546 | + | |
| 547 | + $tt_id = $untranslated_in->get_tax_prop( 'language', 'term_taxonomy_id' ); | |
| 548 | + | |
| 549 | + $clauses['where'] .= $wpdb->prepare( | |
| 550 | + " AND {$wpdb->posts}.ID NOT IN ( | |
| 551 | + SELECT pllutr.object_id | |
| 552 | + FROM {$wpdb->term_relationships} AS pllutr | |
| 553 | + WHERE pllutr.term_taxonomy_id = %d | |
| 554 | + )", | |
| 555 | + $tt_id | |
| 556 | + ); | |
| 557 | + | |
| 558 | + $clauses['where'] .= $wpdb->prepare( | |
| 559 | + " AND {$wpdb->posts}.ID NOT IN ( | |
| 560 | + SELECT pllutr1.object_id | |
| 561 | + FROM {$wpdb->term_relationships} AS pllutr1 | |
| 562 | + JOIN {$wpdb->term_taxonomy} AS pllutt ON pllutt.term_taxonomy_id = pllutr1.term_taxonomy_id | |
| 563 | + JOIN {$wpdb->term_relationships} AS pllutr2 ON pllutr2.term_taxonomy_id = pllutt.term_taxonomy_id | |
| 564 | + JOIN {$wpdb->term_relationships} AS pllutr3 ON pllutr3.object_id = pllutr2.object_id | |
| 565 | + WHERE pllutt.taxonomy = 'post_translations' | |
| 566 | + AND pllutr3.term_taxonomy_id = %d | |
| 567 | + )", | |
| 568 | + $tt_id | |
| 569 | + ); | |
| 570 | + | |
| 571 | + return $clauses; | |
| 485 | 572 | } |
| 486 | 573 | } |