PluginProbe
FileBird – WordPress Media Library Folders & File Manager / 5.5
FileBird – WordPress Media Library Folders & File Manager v5.5
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.5, at includes/Support/WPML.php

258 lines 10.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 add_action( 'wp_ajax_fbv_sync_wpml', array( $this, 'syncWPML' ) );
32
33 if ( ! isset( $this->cpt_sync_options['attachment'] ) || $this->cpt_sync_options['attachment'] != '0' ) {
34 add_filter( 'fbv_in_not_in_query', array( $this, 'fbv_in_not_in_query' ), 10, 2 );
35 add_filter( 'fbv_get_count_query', array( $this, 'fbv_get_count_query' ), 10, 3 );
36 add_filter( 'fbv_speedup_get_count_query', '__return_true' );
37 add_filter( 'fbv_all_folders_and_count', array( $this, 'all_folders_and_count_query' ), 10, 2 );
38 }
39 }
40
41 public function syncWPML() {
42 global $wpdb;
43
44 check_ajax_referer( 'fbv_nonce', 'nonce', true );
45
46 $translationNotInFolder = $wpdb->get_results(
47 "SELECT GROUP_CONCAT( IF(fbv.folder_id is NULL, icl.element_id, NULL) ) as attachment_ids, GROUP_CONCAT(DISTINCT(fbv.folder_id)) as folder_id
48 FROM `{$wpdb->prefix}icl_translations` icl
49 LEFT JOIN `{$wpdb->prefix}fbv_attachment_folder` fbv
50 ON fbv.attachment_id = icl.element_id
51 WHERE ( icl.element_type = 'post_attachment' )
52 GROUP BY icl.trid
53 HAVING ( COUNT(icl.element_id) > COUNT(fbv.folder_id) AND COUNT(fbv.folder_id) > 0 )"
54 );
55
56 foreach ( $translationNotInFolder as $translation ) {
57 $elementIds = explode( ',', $translation->attachment_ids );
58 $folderId = intval( $translation->folder_id );
59
60 foreach ( $elementIds as $elementId ) {
61 FolderModel::setFoldersForPosts( $elementId, $folderId );
62 }
63 }
64
65 return wp_send_json(
66 array(
67 'message' => __( 'Done!', 'filebird' ),
68 )
69 );
70 }
71
72 public function fbv_get_count_query( $q, $folder_id, $lang ) {
73 global $wpdb;
74 if ( $folder_id == -1 ) {
75 $q = "SELECT COUNT(*) FROM {$wpdb->posts}
76 JOIN {$this->table_icl_translations} wpml_translations ON {$wpdb->posts}.ID = wpml_translations.element_id
77 AND wpml_translations.element_type = CONCAT('post_', {$wpdb->posts}.post_type)
78 WHERE 1=1
79 AND {$wpdb->posts}.ID NOT IN
80 (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_elementor_is_screenshot')
81 AND wpml_translations.element_type = 'post_attachment'
82 AND (( {$wpdb->posts}.post_status = 'inherit' OR {$wpdb->posts}.post_status = 'private'))"; //->This query maybe not correct in grid mode
83
84 if ( $lang == 'all' ) {
85 $q .= $this->all_langs_where();
86 } else {
87 $where = $this->specific_lang_where( $lang );
88 $q .= $where;
89 }
90 } else {
91 $q = "SELECT count(wpmlt.element_id) FROM {$this->table_icl_translations} AS wpmlt
92 INNER JOIN {$wpdb->posts} as posts ON posts.ID = wpmlt.element_id
93 INNER join {$wpdb->prefix}fbv_attachment_folder as fbvaf on wpmlt.element_id = fbvaf.attachment_id
94 WHERE
95 posts.ID NOT IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_elementor_is_screenshot')
96 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;
97 }
98 return $q;
99 }
100 public function fbv_in_not_in_query( $q, $fbv ) {
101 global $wpdb;
102 if ( $fbv == 0 ) {
103 $q = "SELECT wpml_translations.element_id FROM {$this->table_icl_translations} AS wpml_translations
104 INNER JOIN {$wpdb->posts} as posts ON posts.ID = wpml_translations.element_id
105 INNER join {$wpdb->prefix}fbv_attachment_folder as fbvaf on wpml_translations.element_id = fbvaf.attachment_id
106 WHERE (post_status = 'inherit' OR post_status = 'private') AND wpml_translations.element_type = 'post_attachment'";
107 if ( $this->lang == 'all' ) {
108 $q .= $this->all_langs_where();
109 } else {
110 $q .= "AND wpml_translations.language_code IN ('$this->lang')";
111 }
112 } elseif ( is_array( $fbv ) ) {
113 $q = "SELECT wpml_translations.element_id FROM {$this->table_icl_translations} AS wpml_translations
114 INNER JOIN {$wpdb->posts} as posts ON posts.ID = wpml_translations.element_id
115 INNER join {$wpdb->prefix}fbv_attachment_folder as fbvaf on wpml_translations.element_id = fbvaf.attachment_id
116 WHERE (post_status = 'inherit' OR post_status = 'private') AND wpml_translations.element_type = 'post_attachment'";
117 if ( $this->lang == 'all' ) {
118 $q .= $this->all_langs_where();
119 } else {
120 $q .= "AND wpml_translations.language_code IN ('$this->lang')";
121 }
122 $q .= 'AND fbvaf.folder_id IN (' . implode( ', ', $fbv ) . ')';
123 }
124 return $q;
125 }
126 public function all_folders_and_count_query( $query, $lang ) {
127 global $wpdb;
128 $query = "SELECT fbva.folder_id as folder_id, count(fbva.attachment_id) as count FROM {$wpdb->prefix}fbv_attachment_folder AS fbva
129 INNER JOIN {$wpdb->prefix}fbv as fbv ON fbv.id = fbva.folder_id
130 INNER JOIN {$this->table_icl_translations} AS wpml_translations ON fbva.attachment_id = wpml_translations.element_id
131 INNER JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = fbva.attachment_id
132 WHERE ({$wpdb->posts}.post_status = 'inherit' OR {$wpdb->posts}.post_status = 'private') AND wpml_translations.element_type = 'post_attachment'";
133 if ( $lang == 'all' ) {
134 $query .= $this->all_langs_where();
135 } else {
136 $where = $this->specific_lang_where( $lang );
137 $query .= $where;
138 }
139 $query .= 'AND fbv.created_by = ' . apply_filters( 'fbv_in_not_in_created_by', '0' ) . ' GROUP BY fbva.folder_id';
140
141 return $query;
142 }
143 public function fbvAfterSetFolder( $id, $folder ) {
144 global $wpdb;
145 $cpt_sync_options = $this->sitepress->get_setting( 'custom_posts_sync_option', array() );
146 if ( isset( $cpt_sync_options['attachment'] ) && $cpt_sync_options['attachment'] == '0' ) {
147 return;
148 }
149 $post = get_post( $id );
150 $post_type = $post->post_type;
151 $post_trid = $this->sitepress->get_element_trid( $id, 'post_' . $post_type );
152 $post_translations = $this->sitepress->get_element_translations( $post_trid, 'post_' . $post_type );
153
154 foreach ( $post_translations as $post_language => $translated_post ) {
155 $translated_post_id = $translated_post->element_id;
156 if ( ! $translated_post_id ) {
157 continue;
158 }
159 FolderModel::setFoldersForPosts( $translated_post_id, (int) $folder, false );
160 }
161 }
162 public function filterInNotIn( $query ) {
163 $query = $this->adjust_q_var_pids( $query, 'post__not_in' );
164 $query = $this->adjust_q_var_pids( $query, 'post__in' );
165 return $query;
166 }
167 public function preParseQuery( $q ) {
168 if ( ! empty( $q->query_vars['post_type'] ) && $q->query_vars['post_type'] == 'attachment' ) {
169 $cpt_sync_options = $this->sitepress->get_setting( 'custom_posts_sync_option', array() );
170 if ( isset( $cpt_sync_options['attachment'] ) && $cpt_sync_options['attachment'] == '0' ) {
171 $q->query_vars['fbv_backup_post__in'] = $q->query_vars['post__in'];
172 $q->query_vars['fbv_backup_post__not_in'] = $q->query_vars['post__not_in'];
173 $q->query_vars['post__in'] = array();
174 $q->query_vars['post__not_in'] = array();
175 }
176 }
177 return $q;
178 }
179 public function postParseQuery( $q ) {
180 if ( ! empty( $q->query_vars['post_type'] ) && $q->query_vars['post_type'] == 'attachment' ) {
181 $cpt_sync_options = $this->sitepress->get_setting( 'custom_posts_sync_option', array() );
182 if ( isset( $cpt_sync_options['attachment'] ) && $cpt_sync_options['attachment'] == '0' ) {
183 $q->query_vars['post__in'] = $q->query_vars['fbv_backup_post__in'];
184 $q->query_vars['post__not_in'] = $q->query_vars['fbv_backup_post__not_in'];
185 unset( $q->query_vars['fbv_backup_post__in'] );
186 unset( $q->query_vars['fbv_backup_post__not_in'] );
187 }
188 }
189 return $q;
190 }
191 private function adjust_q_var_pids( $q, $index ) {
192 if ( ! empty( $q[ $index ] ) ) {
193
194 $untranslated = $q[ $index ];
195 $this->post_translations->prefetch_ids( $untranslated );
196 $current_lang = $this->sitepress->get_current_language();
197 $pid = array();
198 foreach ( $q[ $index ] as $p ) {
199 $pid[] = $this->post_translations->element_id_in( $p, $current_lang, true );
200 }
201 $q[ $index ] = $pid;
202 }
203
204 return $q;
205 }
206 public function countArgs( $args ) {
207 $args['suppress_filters'] = false;
208 return $args;
209 }
210
211 public function all_langs_where() {
212 return ' AND wpml_translations.language_code IN (' . \wpml_prepare_in( array_keys( $this->sitepress->get_active_languages() ) ) . ') ';
213 }
214
215 public function specific_lang_where( $lang ) {
216 // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
217 $default_language = $this->sitepress->get_default_language();
218 $current_language = $lang;
219 return $this->wpdb->prepare(
220 ' AND ( ( ( wpml_translations.language_code = %s OR '
221 . $this->display_as_translated_snippet( $current_language, $default_language )
222 . ' ) AND '
223 . $this->in_translated_types_snippet()
224 . ' ) OR ' . $this->in_translated_types_snippet( true ) . ' )',
225 $current_language
226 );
227 }
228
229 public function display_as_translated_snippet( $current_language, $fallback_language ) {
230 $content_types = null;
231 $skip_content_check = true;
232
233 if ( ! apply_filters( 'wpml_should_force_display_as_translated_snippet', false ) ) {
234 $post_types = $this->sitepress->get_display_as_translated_documents();
235 if ( ! $post_types || ! apply_filters( 'wpml_should_use_display_as_translated_snippet', ! is_admin(), $post_types ) ) {
236 return '0';
237 }
238 $content_types = array_keys( $post_types );
239 $skip_content_check = false;
240 }
241
242 $display_as_translated_query = new \WPML_Display_As_Translated_Posts_Query( $this->wpdb );
243
244 return $display_as_translated_query->get_language_snippet( $current_language, $fallback_language, $content_types, $skip_content_check );
245 }
246
247 public function in_translated_types_snippet( $not = false, $posts_alias = false ) {
248 $not = $not ? ' NOT ' : '';
249 $posts_alias = $posts_alias ? $posts_alias : $this->wpdb->posts;
250
251 $post_types = $this->sitepress->get_translatable_documents( false );
252 if ( $post_types ) {
253 return "{$posts_alias}.post_type {$not} IN (" . wpml_prepare_in( array_keys( $post_types ) ) . ' ) ';
254 } else {
255 return '';
256 }
257 }
258 }