Completed
Push — fix/3rd-party-wpml-too-early ( 6b0035 )
by
unknown
94:26 queued 86:07
created

wpml.php ➔ wpml_jetpack_init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Only load these if WPML plugin is installed and active.
4
 */
5
6
/**
7
 * Load routines only if WPML is loaded.
8
 *
9
 * @since 4.4.0
10
 */
11
function wpml_jetpack_init() {
12
	add_action( 'jetpack_widget_get_top_posts', 'wpml_jetpack_widget_get_top_posts', 10, 3 );
13
	add_filter( 'grunion_contact_form_field_html', 'grunion_contact_form_field_html_filter', 10, 3 );
14
}
15
add_action( 'wpml_loaded', 'wpml_jetpack_init' );
16
17
/**
18
 * Filter the Top Posts and Pages by language.
19
 *
20
 * @param array  $posts    Array of the most popular posts.
21
 * @param array  $post_ids Array of Post IDs.
22
 * @param string $count    Number of Top Posts we want to display.
23
 *
24
 * @return array
25
 */
26
function wpml_jetpack_widget_get_top_posts( $posts, $post_ids, $count ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_ids is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $count is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
27
	global $sitepress;
28
29
	foreach ( $posts as $k => $post ) {
30
		$lang_information = wpml_get_language_information( $post['post_id'] );
31
		$post_language    = substr( $lang_information['locale'], 0, 2 );
32
		if ( $post_language !== $sitepress->get_current_language() ) {
33
			unset( $posts[ $k ] );
34
		}
35
	}
36
37
	return $posts;
38
}
39
40
/**
41
 * Filter the HTML of the Contact Form and output the one requested by language.
42
 *
43
 * @param string   $r           Contact Form HTML output.
44
 * @param string   $field_label Field label.
45
 * @param int|null $id          Post ID.
46
 *
47
 * @return string
48
 */
49
function grunion_contact_form_field_html_filter( $r, $field_label, $id ){
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
	global $sitepress;
51
52
	if ( function_exists( 'icl_translate' ) ) {
53
		if ( $sitepress->get_current_language() !== $sitepress->get_default_language() ) {
54
			$label_translation = icl_translate( 'jetpack ', $field_label . '_label', $field_label );
55
			$r                 = str_replace( $field_label, $label_translation, $r );
56
		}
57
	}
58
59
	return $r;
60
}