| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Manages the compatibility with Jetpack |
| 8 |
* |
| 9 |
* @since 2.3 |
| 10 |
*/ |
| 11 |
class PLL_Jetpack { |
| 12 |
/** |
| 13 |
* Constructor |
| 14 |
* |
| 15 |
* @since 2.3 |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
add_action( 'init', array( $this, 'jetpack_init' ) ); |
| 19 |
add_action( 'jetpack_widget_get_top_posts', array( $this, 'jetpack_widget_get_top_posts' ) ); |
| 20 |
add_filter( 'grunion_contact_form_field_html', array( $this, 'grunion_contact_form_field_html_filter' ), 10, 2 ); |
| 21 |
add_filter( 'jetpack_open_graph_tags', array( $this, 'jetpack_ogp' ) ); |
| 22 |
add_filter( 'jetpack_relatedposts_filter_filters', array( $this, 'jetpack_relatedposts_filter_filters' ), 10, 2 ); |
| 23 |
|
| 24 |
// Jetpack infinite scroll |
| 25 |
if ( isset( $_GET['infinity'], $_POST['action'] ) && 'infinite_scroll' == $_POST['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 26 |
add_filter( 'pll_is_ajax_on_front', '__return_true' ); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Add filters |
| 32 |
* |
| 33 |
* @since 2.1 |
| 34 |
*/ |
| 35 |
public function jetpack_init() { |
| 36 |
if ( ! defined( 'JETPACK__VERSION' ) ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
// Infinite scroll ajax url must be on the right domain |
| 41 |
if ( did_action( 'pll_init' ) && PLL()->options['force_lang'] > 1 ) { |
| 42 |
add_filter( 'infinite_scroll_ajax_url', array( PLL()->links_model, 'site_url' ) ); |
| 43 |
add_filter( 'infinite_scroll_js_settings', array( $this, 'jetpack_infinite_scroll_js_settings' ) ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Filter the Top Posts and Pages by language. |
| 49 |
* Adapted from the same function in jetpack-3.0.2/3rd-party/wpml.php |
| 50 |
* |
| 51 |
* @since 1.5.4 |
| 52 |
* |
| 53 |
* @param array $posts Array of the most popular posts. |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function jetpack_widget_get_top_posts( $posts ) { |
| 57 |
foreach ( $posts as $k => $post ) { |
| 58 |
if ( pll_current_language() !== pll_get_post_language( $post['post_id'] ) ) { |
| 59 |
unset( $posts[ $k ] ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return $posts; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Filter the HTML of the Contact Form and output the one requested by language. |
| 68 |
* Adapted from the same function in jetpack-3.0.2/3rd-party/wpml.php |
| 69 |
* Keeps using 'icl_translate' as the function registers the string |
| 70 |
* |
| 71 |
* @since 1.5.4 |
| 72 |
* |
| 73 |
* @param string $r Contact Form HTML output. |
| 74 |
* @param string $field_label Field label. |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public function grunion_contact_form_field_html_filter( $r, $field_label ) { |
| 78 |
if ( function_exists( 'icl_translate' ) ) { |
| 79 |
if ( pll_current_language() !== pll_default_language() ) { |
| 80 |
$label_translation = icl_translate( 'jetpack ', $field_label . '_label', $field_label ); |
| 81 |
$r = str_replace( $field_label, $label_translation, $r ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return $r; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Adds opengraph support for locale and translations |
| 90 |
* |
| 91 |
* @since 1.6 |
| 92 |
* |
| 93 |
* @param array $tags opengraph tags to output |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
public function jetpack_ogp( $tags ) { |
| 97 |
if ( did_action( 'pll_init' ) ) { |
| 98 |
foreach ( PLL()->model->get_languages_list() as $language ) { |
| 99 |
if ( PLL()->curlang->slug !== $language->slug && PLL()->links->get_translation_url( $language ) && isset( $language->facebook ) ) { |
| 100 |
$tags['og:locale:alternate'][] = $language->facebook; |
| 101 |
} |
| 102 |
if ( PLL()->curlang->slug === $language->slug && isset( $language->facebook ) ) { |
| 103 |
$tags['og:locale'] = $language->facebook; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
return $tags; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Allows to make sure that related posts are in the correct language |
| 112 |
* |
| 113 |
* @since 1.8 |
| 114 |
* |
| 115 |
* @param array $filters Array of ElasticSearch filters based on the post_id and args. |
| 116 |
* @param string $post_id Post ID of the post for which we are retrieving Related Posts. |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
public function jetpack_relatedposts_filter_filters( $filters, $post_id ) { |
| 120 |
$slug = sanitize_title( pll_get_post_language( $post_id, 'slug' ) ); |
| 121 |
$filters[] = array( 'term' => array( 'taxonomy.language.slug' => $slug ) ); |
| 122 |
return $filters; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Fixes the settings history host for infinite scroll when using subdomains or multiple domains |
| 127 |
* |
| 128 |
* @since 2.1 |
| 129 |
* |
| 130 |
* @param array $settings |
| 131 |
* @return array |
| 132 |
*/ |
| 133 |
public function jetpack_infinite_scroll_js_settings( $settings ) { |
| 134 |
$settings['history']['host'] = wp_parse_url( pll_home_url(), PHP_URL_HOST ); // Jetpack uses get_option( 'home' ) |
| 135 |
return $settings; |
| 136 |
} |
| 137 |
} |
| 138 |
|