PluginProbe ʕ •ᴥ•ʔ
Nested Pages / 3.0.8
Nested Pages v3.0.8
3.3.1 3.2.15 3.2.14 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.3.0 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.2 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.3.1 1.6.3.2 1.6.4 1.6.5 1.6.5.1 1.6.5.2 1.6.6 1.6.7 1.6.8 1.7.0 1.7.1 2.0.1 2.0.2 2.0.3 2.0.4 3.0.1 3.0.10 3.0.11 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.20 3.1.21 3.1.22 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7
wp-nested-pages / app / Entities / PluginIntegration / WPML.php
wp-nested-pages / app / Entities / PluginIntegration Last commit date
AdvancedCustomFields.php 8 years ago DarkMode.php 7 years ago EditorialAccessManager.php 8 years ago IntegrationFactory.php 7 years ago WPML.php 8 years ago YoastSeo.php 9 years ago
WPML.php
345 lines
1 <?php
2 namespace NestedPages\Entities\PluginIntegration;
3
4 use NestedPages\Entities\Post\PostRepository;
5
6 /**
7 * WPML Integration
8 * @link https://wpml.org/
9 */
10 class WPML
11 {
12 /**
13 * Installed
14 * @var boolean
15 */
16 public $installed = false;
17
18 /**
19 * Global Sitepress object
20 */
21 private $sitepress;
22
23 /**
24 * WPML Settings
25 */
26 private $settings;
27
28 /**
29 * Post Repository
30 */
31 private $post_repo;
32
33 public function __construct()
34 {
35 if ( defined('ICL_SITEPRESS_VERSION') ){
36 $this->installed = true;
37 global $sitepress;
38 $this->sitepress = $sitepress;
39 $this->settings = get_option('icl_sitepress_settings');
40 $this->post_repo = new PostRepository;
41 return;
42 }
43 }
44
45 /**
46 * Get the current language
47 * @return string
48 */
49 public function getCurrentLanguage($return_type = 'code')
50 {
51 if ( $return_type == 'code' ) return ICL_LANGUAGE_CODE;
52 if ( $return_type == 'name' ) return ICL_LANGUAGE_NAME;
53 if ( $return_type == 'name_en' ) return ICL_LANGUAGE_NAME_EN;
54 }
55
56 /**
57 * Get all available languages
58 * @return array
59 */
60 public function getLanguages()
61 {
62 return icl_get_languages();
63 }
64
65 /**
66 * Get the default language
67 * @return array
68 */
69 public function getDefaultLanguage()
70 {
71 return $this->sitepress->get_default_language();
72 }
73
74 /**
75 * Get a single language
76 * @return array
77 */
78 public function getSingleLanguage($language = 'en')
79 {
80 $languages = $this->getLanguages();
81 return ( array_key_exists($language, $languages) ) ? $languages[$language] : [];
82 }
83
84 /**
85 * Is the default language currently being shown
86 * @return boolean
87 */
88 public function isDefaultLanguage()
89 {
90 return ( $this->getCurrentLanguage() == $this->getDefaultLanguage() ) ? true : false;
91 }
92
93 /**
94 * Get all translations for a post
95 * @return array or int
96 */
97 public function getAllTranslations($post_id, $return = 'array')
98 {
99 if ( !$post_id && $return == 'array' ) return [];
100 if ( !$post_id && $return == 'count' ) return 0;
101 $true_id = $this->sitepress->get_element_trid($post_id);
102 if ( $return == 'array' ) return $this->sitepress->get_element_translations($true_id);
103 if ( $return == 'count' ) return count($this->sitepress->get_element_translations($true_id));
104 }
105
106 /**
107 * Get the primary language post ID for a provided child id
108 * @param int
109 * @return int - post id
110 */
111 public function getPrimaryLanguagePost($post_id)
112 {
113 $all_translations = $this->getAllTranslations($post_id);
114 foreach ( $all_translations as $translation ){
115 if ( $translation->language_code == $this->getDefaultLanguage() ) return $translation->element_id;
116 }
117 }
118
119 /**
120 * Sync Post Order among translations
121 * @param array of posts with children
122 */
123 public function syncPostOrder($posts)
124 {
125 if ( $this->settings['sync_page_ordering'] !== 1 ) return;
126 global $wpdb;
127 if ( !is_array($posts) ) return;
128 foreach ( $posts as $order => $post ) :
129 $translations = $this->getAllTranslations($post['id']);
130 foreach ( $translations as $lang_code => $post_info ) :
131 $post_id = $post_info->element_id;
132 $query = "UPDATE $wpdb->posts SET menu_order = '$order' WHERE ID = '$post_id'";
133 $wpdb->query( $query );
134 endforeach;
135 if ( isset($post['children']) ) $this->syncPostOrder($post['children']);
136 endforeach;
137 }
138
139 /**
140 * Output the sync menus button
141 * @return html
142 */
143 public function syncMenusButton()
144 {
145 $url = esc_url(admin_url('admin.php?page=sitepress-multilingual-cms/menu/menu-sync/menus-sync.php'));
146 return '<a href="' . $url . '" class="np-btn">' . __('Sync WPML Menus', 'wp-nested-pages') . '</a>';
147 }
148
149 /**
150 * Get the Translated IDs of an array of post IDs
151 * @param array $post_ids
152 * @return array of post ids
153 */
154 public function getAllTranslatedIds($post_ids)
155 {
156 if ( !is_array($post_ids) ) return [];
157 foreach ( $post_ids as $id ){
158 $translations = $this->getAllTranslations($id);
159 if ( empty($translations) ) continue;
160 foreach ( $translations as $lang => $post ){
161 if ( $post->element_id == $id ) continue;
162 array_push($post_ids, $post->element_id);
163 }
164 }
165 return $post_ids;
166 }
167
168 /**
169 * Get the number of translated posts for a post type
170 */
171 public function translatedPostCount($language_code = '', $post_type = '')
172 {
173 global $wpdb;
174
175 $default_language_code = $this->getDefaultLanguage();
176 $post_type = 'post_' . $post_type;
177
178 $query = $wpdb->prepare("SELECT COUNT( {$wpdb->prefix}posts.ID ) FROM {$wpdb->prefix}posts LEFT JOIN {$wpdb->prefix}icl_translations ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}icl_translations.element_id WHERE {$wpdb->prefix}icl_translations.language_code = '%s' AND {$wpdb->prefix}icl_translations.source_language_code = '%s' AND {$wpdb->prefix}icl_translations.element_type = '%s'", $language_code, $default_language_code, $post_type);
179
180 return $wpdb->get_var( $query );
181 }
182
183 /**
184 * Get the number of default language posts for a post type
185 */
186 public function defaultPostCount($post_type = '')
187 {
188 global $wpdb;
189 $default_language_code = $this->getDefaultLanguage();
190 $query = $wpdb->prepare("SELECT COUNT(p.ID) FROM {$wpdb->prefix}posts AS p LEFT JOIN {$wpdb->prefix}icl_translations AS t ON t.element_id = p.ID WHERE p.post_type = '%s' AND p.post_status = 'publish' AND t.language_code = '%s'", $post_type, $default_language_code);
191 return $wpdb->get_var( $query );
192 }
193
194 /**
195 * Output a list of language links in the tools section of the listing head
196 * @param string $post_type
197 * @return string html
198 * @example English (2) | German (1) | Spanish (0)
199 */
200 public function languageToolLinks($post_type)
201 {
202 $html = '<ul class="subsubsub" style="clear:both;">';
203 $c = 1;
204 $languages = $this->getLanguages();
205 foreach ( $languages as $lang_code => $lang ){
206 $translated_language = $this->getTranslatedLanguageName($this->getDefaultLanguage(), $lang_code);
207 if ( !$translated_language ) continue;
208 $post_count = ( $lang_code == $this->getDefaultLanguage() )
209 ? $this->defaultPostCount($post_type)
210 : $this->translatedPostCount($lang_code, $post_type);
211 $html .= '<li>';
212 if ( $c > 1 ) $html .= '|&nbsp;';
213 if ( $lang['active'] ) $html .= '<strong>';
214 if ( $post_count > 0 ) $html .= '<a href="' . $this->getLanguageFilteredLink($lang_code, $post_type) . '">';
215 $html .= $translated_language . ' ';
216 if ( $lang_code !== $this->getDefaultLanguage() ) $html .= '(' . $post_count . ')';
217 if ( $lang_code == $this->getDefaultLanguage() ) $html .= '(' . $post_count . ')';
218 if ( $lang['active'] ) $html .= '</strong>';
219 if ( $post_count > 0 ) $html .= '</a>';
220 $html .= '&nbsp;</li>';
221 $c++;
222 }
223 $html .= '<li>|&nbsp;<a href="' . $this->getLanguageFilteredLink('all', $post_type) . '">' . __('All Languages', 'wp-nested-pages') . '</a></li>';
224 $html .= '</ul>';
225 return $html;
226 }
227
228 /**
229 * Get the Links to a filtered language
230 */
231 public function getLanguageFilteredLink($language, $post_type)
232 {
233 $url = 'admin.php?page=nestedpages';
234 if ( $post_type !== 'page' ) $url .= '-' . $post_type;
235 $url .= '&lang=' . $language;
236 return esc_url(admin_url($url));
237 }
238
239 /**
240 * Get a translated language name for a given language
241 * @param string $translated_language - the language to return as
242 * @param string $target_language - the language to translate
243 * @return string - translated name of language (ex: if default is english, and target is spanish, will return "Spanish" rather than "Espanol")
244 */
245 public function getTranslatedLanguageName($translated_language, $target_language)
246 {
247 global $wpdb;
248 $query = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}icl_languages_translations WHERE language_code = '%s' AND display_language_code = '%s'", $target_language, $translated_language);
249 $results = $wpdb->get_results( $query );
250 return ( $results[0]->name ) ? $results[0]->name : null;
251 }
252
253 /**
254 * Sync fields across all langauges
255 */
256 public function syncPosts($post_id)
257 {
258 $all_translations = $this->getAllTranslations($post_id);
259 $source_template = get_post_meta($post_id, '_wp_page_template', true);
260 $source_post = get_post($post_id);
261 $sticky_flag = ( $this->settings['sync_sticky_flag'] ) ? get_option('sticky_posts') : [];
262 foreach ( $all_translations as $translation ) :
263 if ( $translation->element_id == $post_id ) continue;
264 if ( $this->settings['sync_page_template'] && $source_template ){
265 update_post_meta(
266 $translation->element_id,
267 '_wp_page_template',
268 $source_template
269 );
270 }
271 $update_args = [];
272 if ( $this->settings['sync_comment_status'] ) $update_args['comment_status'] = $source_post->comment_status;
273 if ( $this->settings['sync_post_date'] ) $update_args['post_date'] = $source_post->post_date;
274 if ( $this->settings['sync_post_date'] ) $update_args['post_date_gmt'] = $source_post->post_date_gmt;
275 if ( $this->settings['sync_ping_status'] ) $update_args['post_status'] = $source_post->post_status;
276 if ( $this->settings['sync_password'] ) $update_args['post_password'] = $source_post->post_password;
277 if ( $this->settings['sync_private_flag'] ) $update_args['post_status'] = $source_post->post_status;
278 if ( $this->settings['sync_post_taxonomies'] ) $this->syncTaxonomies($post_id);
279 endforeach;
280 }
281
282 /**
283 * Sync Taxonomies across all languages
284 */
285 public function syncTaxonomies($source_post_id)
286 {
287 global $wpdb;
288 $all_translations = $this->getAllTranslations($source_post_id);
289 $terms = $this->post_repo->getAllTerms($source_post_id);
290 $translated_terms = [];
291
292 // Get all translations for the terms
293 foreach ( $terms as $term ){
294 if ( $term->tax_name == 'language' ) continue;
295 $query = "SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id = $term->term_id AND element_type LIKE 'tax_%'";
296 $trid = $wpdb->get_var($query);
297 if ( !$trid ) continue;
298 $translated_terms[] = $this->getTermTranslations($trid);
299 if ( !$translated_terms ) continue;
300 }
301
302 foreach ( $all_translations as $translation ) :
303 if ( $translation->element_id == $source_post_id ) continue;
304 if ( !$terms ) continue;
305
306 // Build the array of terms to sync
307 foreach ( $translated_terms as $term_translations ) :
308 foreach ( $term_translations as $trans ) :
309 $taxonomy = get_taxonomy($trans->taxonomy);
310 $tax_hierarchical = $taxonomy->hierarchical;
311 if ( $trans->language_code == $translation->language_code ) {
312 $translation->terms[$trans->taxonomy][] = ($tax_hierarchical) ? $trans->term_id : $trans->name;
313 }
314 endforeach;
315 endforeach;
316
317 if ( !is_array($translation->terms) ) continue;
318 foreach ( $translation->terms as $taxonomy => $terms ) :
319 wp_set_post_terms($translation->element_id, $terms, $taxonomy, false);
320 endforeach;
321 endforeach;
322 }
323
324 /**
325 * Get all the translations for a term
326 */
327 public function getTermTranslations($trid)
328 {
329 global $wpdb;
330 return $wpdb->get_results($wpdb->prepare("SELECT iclt.language_code, t.term_id, t.name, t.slug, tt.taxonomy FROM {$wpdb->prefix}icl_translations AS iclt LEFT JOIN {$wpdb->prefix}terms AS t ON t.term_id = iclt.element_id LEFT JOIN {$wpdb->prefix}term_taxonomy AS tt ON tt.term_id = t.term_id WHERE trid = %s", $trid));
331 }
332
333 /**
334 * Get the count of published posts for a post type in a specific language
335 * @param $post_type string – post type name
336 * @param $language string - language code
337 */
338 public function getPostTypeCountByLanguage($post_type, $language = null)
339 {
340 if ( !$language ) $language = $this->getCurrentLanguage();
341 global $wpdb;
342 $query = $wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->prefix}posts AS p LEFT JOIN {$wpdb->prefix}icl_translations AS trans ON p.ID = trans.element_id WHERE post_type = %s AND p.post_status = 'publish' AND trans.language_code = %s", $post_type, $language);
343 return $wpdb->get_var($query);
344 }
345 }