PluginProbe
FileBird – WordPress Media Library Folders & File Manager / 5.3.2
FileBird – WordPress Media Library Folders & File Manager v5.3.2
6.5.8 6.5.7 6.5.5 6.5.4 trunk 4.3.1 5.1.6 5.3 5.3.2 5.4.3 5.4.4 5.4.5 5.5 5.5.3 5.5.5 5.5.8 5.5.8.1 5.6 5.6.1 5.6.2 5.6.3 5.6.4 6.2.3 6.2.5 6.3 All 36 releases
filebird / includes / Support / WPML.php

WPML.php in FileBird – WordPress Media Library Folders & File Manager 5.3.2, at includes/Support/WPML.php

226 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace FileBird\Support;
3
4 use FileBird\Model\Folder as FolderModel;
5 use FileBird\Controller\Controller;
6
7 defined( 'ABSPATH' ) || exit;
8
9 class WPML extends Controller {
10 protected $post_translations;
11 private $sitepress;
12 private $lang;
13 private $wpdb;
14 private $table_icl_translations;
15
16 public function __construct() {
17 global $sitepress, $wpdb;
18 if ( $sitepress === null || get_class( $sitepress ) !== 'SitePress' ) {
19 return;
20 }
21 $this->sitepress = $sitepress;
22 $this->lang = $sitepress->get_current_language();
23 $this->wpdb = $wpdb;
24 $this->table_icl_translations = $wpdb->prefix . 'icl_translations';
25 $this->post_translations = $sitepress->post_translations();
26 $this->cpt_sync_options = $this->sitepress->get_setting( 'custom_posts_sync_option', array() );
27
28 add_action( 'fbv_after_set_folder', array( $this, 'fbvAfterSetFolder' ), 10, 2 );
29 add_filter( 'wpml_pre_parse_query', array( $this, 'preParseQuery' ) );
30 add_filter( 'wpml_post_parse_query', array( $this, 'postParseQuery' ) );
31
32 if ( ! isset( $this->cpt_sync_options['attachment'] ) || $this->cpt_sync_options['attachment'] != '0' ) {
33 add_filter( 'fbv_in_not_in_query', array( $this, 'fbv_in_not_in_query' ), 10, 2 );
34 add_filter( 'fbv_get_count_query', array( $this, 'fbv_get_count_query' ), 10, 3 );
35 add_filter( 'fbv_speedup_get_count_query', '__return_true' );
36 add_filter( 'fbv_all_folders_and_count', array( $this, 'all_folders_and_count_query' ), 10, 2 );
37 }
38 }
39
40 public function fbv_get_count_query( $q, $folder_id, $lang ) {
41 global $wpdb;
42 if ( $folder_id == -1 ) {
43 $q = "SELECT COUNT(*) FROM {$wpdb->posts}
44 JOIN {$this->table_icl_translations} wpml_translations ON {$wpdb->posts}.ID = wpml_translations.element_id
45 AND wpml_translations.element_type = CONCAT('post_', {$wpdb->posts}.post_type)
46 WHERE 1=1
47 AND {$wpdb->posts}.ID NOT IN
48 (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_elementor_is_screenshot')
49 AND wpml_translations.element_type = 'post_attachment'
50 AND (( {$wpdb->posts}.post_status = 'inherit' OR {$wpdb->posts}.post_status = 'private'))"; //->This query maybe not correct in grid mode
51
52 if ( $lang == 'all' ) {
53 $q .= $this->all_langs_where();
54 } else {
55 $where = $this->specific_lang_where( $lang );
56 $q .= $where;
57 }
58 } else {
59 $q = "SELECT count(wpmlt.element_id) FROM {$this->table_icl_translations} AS wpmlt
60 INNER JOIN {$wpdb->posts} as posts ON posts.ID = wpmlt.element_id
61 INNER join {$wpdb->prefix}fbv_attachment_folder as fbvaf on wpmlt.element_id = fbvaf.attachment_id
62 WHERE
63 posts.ID NOT IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_elementor_is_screenshot')
64 AND (post_status = 'inherit' OR post_status = 'private') AND wpmlt.element_type = 'post_attachment' AND wpmlt.language_code = '{$this->lang}' AND fbvaf.folder_id = " . (int) $folder_id;
65 }
66 return $q;
67 }
68 public function fbv_in_not_in_query( $q, $fbv ) {
69 global $wpdb;
70 if ( $fbv == 0 ) {
71 $q = "SELECT wpml_translations.element_id FROM {$this->table_icl_translations} AS wpml_translations
72 INNER JOIN {$wpdb->posts} as posts ON posts.ID = wpml_translations.element_id
73 INNER join {$wpdb->prefix}fbv_attachment_folder as fbvaf on wpml_translations.element_id = fbvaf.attachment_id
74 WHERE (post_status = 'inherit' OR post_status = 'private') AND wpml_translations.element_type = 'post_attachment'";
75 if ( $this->lang == 'all' ) {
76 $q .= $this->all_langs_where();
77 } else {
78 $q .= "AND wpml_translations.language_code IN ('$this->lang')";
79 }
80 } elseif ( is_array( $fbv ) ) {
81 $q = "SELECT wpml_translations.element_id FROM {$this->table_icl_translations} AS wpml_translations
82 INNER JOIN {$wpdb->posts} as posts ON posts.ID = wpml_translations.element_id
83 INNER join {$wpdb->prefix}fbv_attachment_folder as fbvaf on wpml_translations.element_id = fbvaf.attachment_id
84 WHERE (post_status = 'inherit' OR post_status = 'private') AND wpml_translations.element_type = 'post_attachment'";
85 if ( $this->lang == 'all' ) {
86 $q .= $this->all_langs_where();
87 } else {
88 $q .= "AND wpml_translations.language_code IN ('$this->lang')";
89 }
90 $q .= 'AND fbvaf.folder_id IN (' . implode( ', ', $fbv ) . ')';
91 }
92 return $q;
93 }
94 public function all_folders_and_count_query( $query, $lang ) {
95 global $wpdb;
96 $query = "SELECT fbva.folder_id as folder_id, count(fbva.attachment_id) as count FROM {$wpdb->prefix}fbv_attachment_folder AS fbva
97 INNER JOIN {$wpdb->prefix}fbv as fbv ON fbv.id = fbva.folder_id
98 INNER JOIN {$this->table_icl_translations} AS wpml_translations ON fbva.attachment_id = wpml_translations.element_id
99 INNER JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = fbva.attachment_id
100 WHERE ({$wpdb->posts}.post_status = 'inherit' OR {$wpdb->posts}.post_status = 'private') AND wpml_translations.element_type = 'post_attachment'";
101 if ( $lang == 'all' ) {
102 $query .= $this->all_langs_where();
103 } else {
104 $where = $this->specific_lang_where( $lang );
105 $query .= $where;
106 }
107 $query .= 'AND fbv.created_by = ' . apply_filters( 'fbv_in_not_in_created_by', '0' ) . ' GROUP BY fbva.folder_id';
108
109 return $query;
110 }
111 public function fbvAfterSetFolder( $id, $folder ) {
112 global $wpdb;
113 $cpt_sync_options = $this->sitepress->get_setting( 'custom_posts_sync_option', array() );
114 if ( isset( $cpt_sync_options['attachment'] ) && $cpt_sync_options['attachment'] == '0' ) {
115 return;
116 }
117 $post = get_post( $id );
118 $post_type = $post->post_type;
119 $post_trid = $this->sitepress->get_element_trid( $id, 'post_' . $post_type );
120 $post_translations = $this->sitepress->get_element_translations( $post_trid, 'post_' . $post_type );
121
122 foreach ( $post_translations as $post_language => $translated_post ) {
123 $translated_post_id = $translated_post->element_id;
124 if ( ! $translated_post_id ) {
125 continue;
126 }
127 FolderModel::setFoldersForPosts( $translated_post_id, (int) $folder, false );
128 }
129 }
130 public function filterInNotIn( $query ) {
131 $query = $this->adjust_q_var_pids( $query, 'post__not_in' );
132 $query = $this->adjust_q_var_pids( $query, 'post__in' );
133 return $query;
134 }
135 public function preParseQuery( $q ) {
136 if ( ! empty( $q->query_vars['post_type'] ) && $q->query_vars['post_type'] == 'attachment' ) {
137 $cpt_sync_options = $this->sitepress->get_setting( 'custom_posts_sync_option', array() );
138 if ( isset( $cpt_sync_options['attachment'] ) && $cpt_sync_options['attachment'] == '0' ) {
139 $q->query_vars['fbv_backup_post__in'] = $q->query_vars['post__in'];
140 $q->query_vars['fbv_backup_post__not_in'] = $q->query_vars['post__not_in'];
141 $q->query_vars['post__in'] = array();
142 $q->query_vars['post__not_in'] = array();
143 }
144 }
145 return $q;
146 }
147 public function postParseQuery( $q ) {
148 if ( ! empty( $q->query_vars['post_type'] ) && $q->query_vars['post_type'] == 'attachment' ) {
149 $cpt_sync_options = $this->sitepress->get_setting( 'custom_posts_sync_option', array() );
150 if ( isset( $cpt_sync_options['attachment'] ) && $cpt_sync_options['attachment'] == '0' ) {
151 $q->query_vars['post__in'] = $q->query_vars['fbv_backup_post__in'];
152 $q->query_vars['post__not_in'] = $q->query_vars['fbv_backup_post__not_in'];
153 unset( $q->query_vars['fbv_backup_post__in'] );
154 unset( $q->query_vars['fbv_backup_post__not_in'] );
155 }
156 }
157 return $q;
158 }
159 private function adjust_q_var_pids( $q, $index ) {
160 if ( ! empty( $q[ $index ] ) ) {
161
162 $untranslated = $q[ $index ];
163 $this->post_translations->prefetch_ids( $untranslated );
164 $current_lang = $this->sitepress->get_current_language();
165 $pid = array();
166 foreach ( $q[ $index ] as $p ) {
167 $pid[] = $this->post_translations->element_id_in( $p, $current_lang, true );
168 }
169 $q[ $index ] = $pid;
170 }
171
172 return $q;
173 }
174 public function countArgs( $args ) {
175 $args['suppress_filters'] = false;
176 return $args;
177 }
178
179 public function all_langs_where() {
180 return ' AND wpml_translations.language_code IN (' . \wpml_prepare_in( array_keys( $this->sitepress->get_active_languages() ) ) . ') ';
181 }
182
183 public function specific_lang_where( $lang ) {
184 // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
185 $default_language = $this->sitepress->get_default_language();
186 $current_language = $lang;
187 return $this->wpdb->prepare(
188 ' AND ( ( ( wpml_translations.language_code = %s OR '
189 . $this->display_as_translated_snippet( $current_language, $default_language )
190 . ' ) AND '
191 . $this->in_translated_types_snippet()
192 . ' ) OR ' . $this->in_translated_types_snippet( true ) . ' )',
193 $current_language
194 );
195 }
196
197 public function display_as_translated_snippet( $current_language, $fallback_language ) {
198 $content_types = null;
199 $skip_content_check = true;
200
201 if ( ! apply_filters( 'wpml_should_force_display_as_translated_snippet', false ) ) {
202 $post_types = $this->sitepress->get_display_as_translated_documents();
203 if ( ! $post_types || ! apply_filters( 'wpml_should_use_display_as_translated_snippet', ! is_admin(), $post_types ) ) {
204 return '0';
205 }
206 $content_types = array_keys( $post_types );
207 $skip_content_check = false;
208 }
209
210 $display_as_translated_query = new \WPML_Display_As_Translated_Posts_Query( $this->wpdb );
211
212 return $display_as_translated_query->get_language_snippet( $current_language, $fallback_language, $content_types, $skip_content_check );
213 }
214
215 public function in_translated_types_snippet( $not = false, $posts_alias = false ) {
216 $not = $not ? ' NOT ' : '';
217 $posts_alias = $posts_alias ? $posts_alias : $this->wpdb->posts;
218
219 $post_types = $this->sitepress->get_translatable_documents( false );
220 if ( $post_types ) {
221 return "{$posts_alias}.post_type {$not} IN (" . wpml_prepare_in( array_keys( $post_types ) ) . ' ) ';
222 } else {
223 return '';
224 }
225 }
226 }