-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranslatorFilterSql.php
136 lines (100 loc) · 4.23 KB
/
TranslatorFilterSql.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Src\Module\Translator;
use Src\Module\Translator\Enum\EntityType;
use Src\Module\Translator\Service\TranslatorEntityService;
class TranslatorFilterSql {
public function __construct(
private readonly TranslatorState $state,
private readonly TranslatorEntityService $translatorEntityService
) {
}
public function init(): void {
add_filter( 'posts_join', function ( string $join, $query ) {
return $this->filterPostRequest( $join, $query, function ( $join, $postType ) {
return $this->postsJoinFilter( $join, $postType );
} );
}, 10, 2 );
add_filter( 'posts_where', function ( string $where, $query ) {
return $this->postsWhereFilter( $where, $query);
}, 10, 2 );
add_filter( 'terms_clauses', [$this, 'editTermsClauses'], 10, 2 );
}
public function editTermsClauses( $pieces, $taxonomies ) {
if($this->state->getCurrentLanguage()->code === Config::ALL_LANGUAGES_CODE){
return $pieces;
}
foreach ( $taxonomies as $taxonomy ) {
if ( $this->translatorEntityService->isSupportEntityType( EntityType::Tax, $taxonomy ) ) {
$pieces['join'] = $this->termsJoinFilter( $pieces['join'] ?? '', $taxonomy );
$pieces['where'] = $this->termsWhereFilter( $pieces['where'] ?? '', $taxonomy );
}
}
return $pieces;
}
private function postsWhereFilter( string $stringRequest, \WP_Query $query ): string {
if($this->state->getCurrentLanguage()->code === Config::ALL_LANGUAGES_CODE){
return $stringRequest;
}
if ( $query->is_page ) {
$postTypes = [ 'page' ];
} else {
if ( empty( $query->query['post_type'] ) ) {
return $stringRequest;
}
$postTypes = is_array( $query->query['post_type'] ) ? $query->query['post_type'] : [ $query->query['post_type'] ];
}
$whereSupport = [];
$Unsupported = [];
foreach ( $postTypes as $postType ) {
if ( $this->translatorEntityService->isSupportEntityType( EntityType::Post, $postType ) ) {
$postType = str_replace('-', '', $postType);
$whereSupport[] = "{$postType}_te.entity_type='" . EntityType::Post->value . "' AND {$postType}_te.code_lang='{$this->state->getCurrentLanguage()->code}'";
}else{
$Unsupported[] = $postType;
}
}
if(!$whereSupport){
return $stringRequest;
}
$whereString = implode(" AND ", $whereSupport);
if($Unsupported){
$whereString = "(($whereString) OR wp_posts.post_type IN ('".implode("','", $Unsupported)."')) ";
}
$stringRequest .= " AND " . $whereString;
return $stringRequest;
}
private function filterPostRequest( string $stringRequest, \WP_Query $query, callable $callable ): string {
if($this->state->getCurrentLanguage()->code === Config::ALL_LANGUAGES_CODE){
return $stringRequest;
}
if ( $query->is_page ) {
$postTypes = [ 'page' ];
} else {
if ( empty( $query->query['post_type'] ) ) {
return $stringRequest;
}
$postTypes = is_array( $query->query['post_type'] ) ? $query->query['post_type'] : [ $query->query['post_type'] ];
}
foreach ( $postTypes as $postType ) {
if ( $this->translatorEntityService->isSupportEntityType( EntityType::Post, $postType ) ) {
$stringRequest = $callable( $stringRequest, $postType );
}
}
return $stringRequest;
}
private function postsJoinFilter( string $join, string $postType ): string {
global $wpdb;
$postType = str_replace('-', '', $postType);
$join .= " LEFT JOIN {$wpdb->prefix}translator_entities AS {$postType}_te ON {$wpdb->posts}.ID={$postType}_te.entity_id ";
return $join;
}
private function termsWhereFilter( string $where, string $taxonomy ): string {
$where .= " AND {$taxonomy}_te.entity_type='" . EntityType::Tax->value . "' AND {$taxonomy}_te.code_lang='{$this->state->getCurrentLanguage()->code}' ";
return $where;
}
private function termsJoinFilter( string $join, string $taxonomy ): string {
global $wpdb;
$join .= " LEFT JOIN {$wpdb->prefix}translator_entities AS {$taxonomy}_te ON t.term_id={$taxonomy}_te.entity_id ";
return $join;
}
}