Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
wpml_jetpack_init
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
wpml_jetpack_widget_get_top_posts
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
grunion_contact_form_field_html_filter
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Only load these if WPML plugin is installed and active.
4 *
5 * @package automattic/jetpack
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12/**
13 * Load routines only if WPML is loaded.
14 *
15 * @since 4.4.0
16 */
17function wpml_jetpack_init() {
18    add_action( 'jetpack_widget_get_top_posts', 'wpml_jetpack_widget_get_top_posts', 10, 3 );
19    add_filter( 'grunion_contact_form_field_html', 'grunion_contact_form_field_html_filter', 10, 3 );
20}
21add_action( 'wpml_loaded', 'wpml_jetpack_init' );
22
23/**
24 * Filter the Top Posts and Pages by language.
25 *
26 * @param array $posts    Array of the most popular posts.
27 *
28 * @return array
29 */
30function wpml_jetpack_widget_get_top_posts( $posts ) {
31    global $sitepress;
32
33    foreach ( $posts as $k => $post ) {
34        $lang_information = wpml_get_language_information( $post['post_id'] );
35        if ( ! is_wp_error( $lang_information ) ) {
36            $post_language = substr( $lang_information['locale'], 0, 2 );
37            if ( $post_language !== $sitepress->get_current_language() ) {
38                unset( $posts[ $k ] );
39            }
40        }
41    }
42
43    return $posts;
44}
45
46/**
47 * Filter the HTML of the Contact Form and output the one requested by language.
48 *
49 * @param string $r           Contact Form HTML output.
50 * @param string $field_label Field label.
51 *
52 * @return string
53 */
54function grunion_contact_form_field_html_filter( $r, $field_label ) {
55    global $sitepress;
56
57    if ( function_exists( 'icl_translate' ) ) {
58        if ( $sitepress->get_current_language() !== $sitepress->get_default_language() ) {
59            $label_translation = icl_translate( 'jetpack ', $field_label . '_label', $field_label );
60            $r                 = str_replace( $field_label, $label_translation, $r );
61        }
62    }
63
64    return $r;
65}