PluginProbe
Polylang / 2.8.1
Polylang v2.8.1
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 / integrations / jetpack / jetpack.php

jetpack.php in Polylang 2.8.1, at integrations/jetpack/jetpack.php

141 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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' ), 10, 3 );
20 add_filter( 'grunion_contact_form_field_html', array( $this, 'grunion_contact_form_field_html_filter' ), 10, 3 );
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 * @param array $post_ids Array of Post IDs.
55 * @param string $count Number of Top Posts we want to display.
56 * @return array
57 */
58 public function jetpack_widget_get_top_posts( $posts, $post_ids, $count ) {
59 foreach ( $posts as $k => $post ) {
60 if ( pll_current_language() !== pll_get_post_language( $post['post_id'] ) ) {
61 unset( $posts[ $k ] );
62 }
63 }
64
65 return $posts;
66 }
67
68 /**
69 * Filter the HTML of the Contact Form and output the one requested by language.
70 * Adapted from the same function in jetpack-3.0.2/3rd-party/wpml.php
71 * Keeps using 'icl_translate' as the function registers the string
72 *
73 * @since 1.5.4
74 *
75 * @param string $r Contact Form HTML output.
76 * @param string $field_label Field label.
77 * @param int|null $id Post ID.
78 * @return string
79 */
80 public function grunion_contact_form_field_html_filter( $r, $field_label, $id ) {
81 if ( function_exists( 'icl_translate' ) ) {
82 if ( pll_current_language() !== pll_default_language() ) {
83 $label_translation = icl_translate( 'jetpack ', $field_label . '_label', $field_label );
84 $r = str_replace( $field_label, $label_translation, $r );
85 }
86 }
87
88 return $r;
89 }
90
91 /**
92 * Adds opengraph support for locale and translations
93 *
94 * @since 1.6
95 *
96 * @param array $tags opengraph tags to output
97 * @return array
98 */
99 public function jetpack_ogp( $tags ) {
100 if ( did_action( 'pll_init' ) ) {
101 foreach ( PLL()->model->get_languages_list() as $language ) {
102 if ( PLL()->curlang->slug !== $language->slug && PLL()->links->get_translation_url( $language ) && isset( $language->facebook ) ) {
103 $tags['og:locale:alternate'][] = $language->facebook;
104 }
105 if ( PLL()->curlang->slug === $language->slug && isset( $language->facebook ) ) {
106 $tags['og:locale'] = $language->facebook;
107 }
108 }
109 }
110 return $tags;
111 }
112
113 /**
114 * Allows to make sure that related posts are in the correct language
115 *
116 * @since 1.8
117 *
118 * @param array $filters Array of ElasticSearch filters based on the post_id and args.
119 * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
120 * @return array
121 */
122 public function jetpack_relatedposts_filter_filters( $filters, $post_id ) {
123 $slug = sanitize_title( pll_get_post_language( $post_id, 'slug' ) );
124 $filters[] = array( 'term' => array( 'taxonomy.language.slug' => $slug ) );
125 return $filters;
126 }
127
128 /**
129 * Fixes the settings history host for infinite scroll when using subdomains or multiple domains
130 *
131 * @since 2.1
132 *
133 * @param array $settings
134 * @return array
135 */
136 public function jetpack_infinite_scroll_js_settings( $settings ) {
137 $settings['history']['host'] = wp_parse_url( pll_home_url(), PHP_URL_HOST ); // Jetpack uses get_option( 'home' )
138 return $settings;
139 }
140 }
141