PluginProbe
Polylang / 1.6.2
Polylang v1.6.2
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / plugins-compat.php

plugins-compat.php in Polylang 1.6.2, at include/plugins-compat.php

280 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * manages compatibility with 3rd party plugins (and themes)
5 * this class is available as soon as the plugin is loaded
6 *
7 * @since 1.0
8 */
9 class PLL_Plugins_Compat {
10
11 /*
12 * constructor
13 *
14 * @since 1.0
15 */
16 public function __construct() {
17 // WordPress Importer
18 add_action('init', array(&$this, 'maybe_wordpress_importer'));
19
20 // YARPP
21 // just makes YARPP aware of the language taxonomy (after Polylang registered it)
22 add_action('init', create_function('',"\$GLOBALS['wp_taxonomies']['language']->yarpp_support = 1;"), 20);
23
24 // WordPress SEO by Yoast
25 add_action('pll_language_defined', array(&$this, 'wpseo_translate_options'));
26 add_filter('get_terms_args', array(&$this, 'wpseo_remove_terms_filter'));
27 add_filter('pll_home_url_white_list', create_function('$arr', "return array_merge(\$arr, array(array('file' => 'wordpress-seo')));"));
28 add_action('wpseo_opengraph', array(&$this, 'wpseo_ogp'), 2);
29
30 // Custom field template
31 add_action('add_meta_boxes', array(&$this, 'cft_copy'), 10, 2);
32
33 // Aqua Resizer
34 add_filter('pll_home_url_black_list', create_function('$arr', "return array_merge(\$arr, array(array('function' => 'aq_resize')));"));
35
36 // Twenty Fourteen
37 add_filter('transient_featured_content_ids', array(&$this, 'twenty_fourteen_featured_content_ids'));
38 add_filter('option_featured-content', array(&$this, 'twenty_fourteen_option_featured_content'));
39
40 // Jetpack 3
41 add_action('jetpack_widget_get_top_posts', array(&$this, 'jetpack_widget_get_top_posts'), 10, 3);
42 add_filter('grunion_contact_form_field_html', array(&$this, 'grunion_contact_form_field_html_filter'), 10, 3);
43 add_filter('jetpack_open_graph_tags', array(&$this, 'jetpack_ogp'));
44 }
45
46 /*
47 * WordPress Importer
48 * if WordPress Importer is active, replace the wordpress_importer_init function
49 *
50 * @since 1.2
51 */
52 function maybe_wordpress_importer() {
53 if (class_exists('WP_Import')) {
54 remove_action('admin_init', 'wordpress_importer_init');
55 add_action('admin_init', array(&$this, 'wordpress_importer_init'));
56 }
57 }
58
59 /*
60 * WordPress Importer
61 * loads our child class PLL_WP_Import instead of WP_Import
62 *
63 * @since 1.2
64 */
65 function wordpress_importer_init() {
66 $class = new ReflectionClass('WP_Import');
67 load_plugin_textdomain( 'wordpress-importer', false, basename(dirname( $class->getFileName() )) . '/languages' );
68
69 $GLOBALS['wp_import'] = new PLL_WP_Import();
70 register_importer( 'wordpress', 'WordPress', __('Import <strong>posts, pages, comments, custom fields, categories, and tags</strong> from a WordPress export file.', 'wordpress-importer'), array( $GLOBALS['wp_import'], 'dispatch' ) );
71 }
72
73 /*
74 * WordPress SEO by Yoast
75 * reloads options once the language has been defined to enable translations
76 * useful only when the language is set from content
77 *
78 * @since 1.2
79 */
80 public function wpseo_translate_options() {
81 if (defined('WPSEO_VERSION') && !PLL_ADMIN && did_action('wp_loaded')) {
82 global $wpseo_front;
83 $options = version_compare(WPSEO_VERSION, '1.5', '<') ? get_wpseo_options_arr() : WPSEO_Options::get_option_names();
84 foreach ( $options as $opt )
85 $wpseo_front->options = array_merge( $wpseo_front->options, (array) get_option( $opt ) );
86 }
87 }
88
89 /*
90 * WordPress SEO by Yoast
91 * removes the language filter for the taxonomy sitemaps
92 *
93 * @since 1.0.3
94 *
95 * @param array $args get_terms arguments
96 * @return array modified list of arguments
97 */
98 public function wpseo_remove_terms_filter($args) {
99 if (defined('WPSEO_VERSION') && isset($GLOBALS['wp_query']->query['sitemap']))
100 $args['lang'] = 0;
101 return $args;
102 }
103
104 /*
105 * WordPress SEO by Yoast
106 * adds opengraph support for translations
107 *
108 * @since 1.6
109 */
110 public function wpseo_ogp() {
111 global $polylang, $wpseo_og;
112
113 // WPSEO already deals with the locale
114 if (isset($polylang)) {
115 foreach ($polylang->model->get_languages_list() as $language) {
116 if ($language->slug != $polylang->curlang->slug && $polylang->links->get_translation_url($language) && $fb_locale = self::get_fb_locale($language)) {
117 $wpseo_og->og_tag('og:locale:alternate', $fb_locale);
118 }
119 }
120 }
121 }
122
123 /*
124 * Custom field template does check $_REQUEST['post'] to populate the custom fields values
125 *
126 * @since 1.0.2
127 *
128 * @param string $post_type unused
129 * @param object $post current post object
130 */
131 public function cft_copy($post_type, $post) {
132 global $custom_field_template;
133 if (isset($custom_field_template, $_REQUEST['from_post'], $_REQUEST['new_lang']) && !empty($post))
134 $_REQUEST['post'] = $post->ID;
135 }
136
137 /*
138 * rewrites the function Featured_Content::get_featured_post_ids()
139 *
140 * @since 1.4
141 *
142 * @param array $ids featured posts ids
143 * @return array modified featured posts ids (include all languages)
144 */
145 public function twenty_fourteen_featured_content_ids($featured_ids) {
146 global $polylang;
147
148 if (empty($polylang) || false !== $featured_ids)
149 return $featured_ids;
150
151 $settings = Featured_Content::get_setting();
152
153 if (!$term = get_term_by( 'name', $settings['tag-name'], 'post_tag' ))
154 return $featured_ids;
155
156 // get featured tag translations
157 $tags = $polylang->model->get_translations('term' ,$term->term_id);
158 $ids = array();
159
160 // Query for featured posts in all languages
161 // one query per language to get the correct number of posts per language
162 foreach ($tags as $tag) {
163 $_ids = get_posts(array(
164 'lang' => 0, // avoid language filters
165 'fields' => 'ids',
166 'numberposts' => Featured_Content::$max_posts,
167 'tax_query' => array(array(
168 'taxonomy' => 'post_tag',
169 'terms' => (int) $tag,
170 )),
171 ));
172
173 $ids = array_merge($ids, $_ids);
174 }
175
176 $ids = array_map( 'absint', $ids );
177 set_transient( 'featured_content_ids', $ids );
178
179 return $ids;
180 }
181
182 /*
183 * translates the featured tag id in featured content settings
184 * mainly to allow hiding it when requested in featured content options
185 * acts only on frontend
186 *
187 * @since 1.4
188 *
189 * @param array $settings featured content settings
190 * @return array modified $settings
191 */
192 public function twenty_fourteen_option_featured_content($settings) {
193 if (!PLL_ADMIN && $settings['tag-id'] && $tr = pll_get_term($settings['tag-id']))
194 $settings['tag-id'] = $tr;
195
196 return $settings;
197 }
198
199 /*
200 * adapted from the same function in jetpack-3.0.2/3rd-party/wpml.php
201 *
202 * @since 1.5.4
203 */
204 public function jetpack_widget_get_top_posts( $posts, $post_ids, $count ) {
205 foreach ( $posts as $k => $post ) {
206 if (pll_current_language() !== pll_get_post_language($post['post_id']))
207 unset( $posts[ $k ] );
208 }
209
210 return $posts;
211 }
212
213 /*
214 * adapted from the same function in jetpack-3.0.2/3rd-party/wpml.php
215 * keeps using 'icl_translate' as the function registers the string
216 *
217 * @since 1.5.4
218 */
219 public function grunion_contact_form_field_html_filter( $r, $field_label, $id ){
220 if ( function_exists( 'icl_translate' ) ) {
221 if ( pll_current_language() !== pll_default_language() ) {
222 $label_translation = icl_translate( 'jetpack ', $field_label . '_label', $field_label );
223 $r = str_replace( $field_label, $label_translation, $r );
224 }
225 }
226
227 return $r;
228 }
229
230 /*
231 * adds opengraph support for locale and translations
232 *
233 * @since 1.6
234 *
235 * @param array $tags opengraph tags to output
236 * @return array
237 */
238 public function jetpack_ogp($tags) {
239 global $polylang;
240
241 if (isset($polylang)) {
242 foreach ($polylang->model->get_languages_list() as $language) {
243 if ($language->slug != $polylang->curlang->slug && $polylang->links->get_translation_url($language) && $fb_locale = self::get_fb_locale($language))
244 $tags['og:locale:alternate'][] = $fb_locale;
245 if ($language->slug == $polylang->curlang->slug && $fb_locale = self::get_fb_locale($language))
246 $tags['og:locale'] = $fb_locale;
247 }
248 }
249 return $tags;
250 }
251
252 /*
253 * correspondance between WordPress locales and Facebook locales according to GP_Locales class in Jetpack 3.1.1
254 *
255 * @since 1.6
256 *
257 * @param object $language
258 * @return bool|string
259 */
260 static public function get_fb_locale($language) {
261 static $facebook_locales = array(
262 'af' => 'af_ZA', 'ar' => 'ar_AR', 'az' => 'az_AZ', 'bg_BG' => 'bg_BG', 'bn_BD' => 'bn_IN', 'bs_BA' => 'bs_BA',
263 'ca' => 'ca_ES', 'cs_CZ' => 'cs_CZ', 'cy' => 'cy_GB', 'da_DK' => 'da_DK', 'de_DE' => 'de_DE', 'el' => 'el_GR',
264 'en_US' => 'en_US', 'en_GB' => 'en_GB', 'eo' => 'eo_EO', 'es_CL' => 'es_LA', 'es_PE' => 'es_LA', 'es_PR' => 'es_LA',
265 'es_VE' => 'es_LA', 'es_CO' => 'es_LA', 'es_ES' => 'es_ES', 'et' => 'et_EE', 'eu' => 'eu_ES', 'fa_IR' => 'fa_IR',
266 'fi' => 'fi_FI', 'fo' => 'fo_FO', 'fr_FR' => 'fr_FR', 'fy' => 'fy_NL', 'ga' => 'ga_IE', 'gl_ES' => 'gl_ES',
267 'he_IL' => 'he_IL', 'hi_IN' => 'hi_IN', 'hr' => 'hr_HR', 'hu_HU' => 'hu_HU', 'hy' => 'hy_AM', 'id_ID' => 'id_ID',
268 'is_IS' => 'is_IS', 'it_IT' => 'it_IT', 'ja' => 'ja_JP', 'ka_GE' => 'ka_GE', 'ko_KR' => 'ko_KR', 'lt_LT' => 'lt_LT',
269 'lv' => 'lv_LV', 'mk_MK' => 'mk_MK', 'ml_IN' => 'ml_IN', 'ms_MY' => 'ms_MY', 'ne_NP' => 'ne_NP', 'nb_NO' => 'nb_NO',
270 'nl_NL' => 'nl_NL', 'nn_NO' => 'nn_NO', 'pa_IN' => 'pa_IN', 'pl_PL' => 'pl_PL', 'pt_BR' => 'pt_BR', 'pt_PT' => 'pt_PT',
271 'ps' => 'ps_AF', 'ro_RO' => 'ro_RO', 'ru_RU' => 'ru_RU', 'sk_SK' => 'sk_SK', 'sl_SI' => 'sl_SI', 'sq' => 'sq_AL',
272 'sr_RS' => 'sr_RS', 'sv_SE' => 'sv_SE', 'sw' => 'sw_KE', 'ta_IN' => 'ta_IN', 'te' => 'te_IN', 'th' => 'th_TH',
273 'ph' => 'tl_PH', 'tr_TR' => 'tr_TR', 'uk' => 'uk_UA', 'vi' => 'vi_VN', 'zh_CN' => 'zh_CN', 'zh_HK' => 'zh_HK',
274 'zh_TW' => 'zh_TW'
275 );
276
277 return isset($facebook_locales[$language->locale]) ? $facebook_locales[$language->locale] : false;
278 }
279 }
280