wpml.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Only load these if WPML plugin is installed and active.
  4. */
  5. /**
  6. * Load routines only if WPML is loaded.
  7. *
  8. * @since 4.4.0
  9. */
  10. function wpml_jetpack_init() {
  11. add_action( 'jetpack_widget_get_top_posts', 'wpml_jetpack_widget_get_top_posts', 10, 3 );
  12. add_filter( 'grunion_contact_form_field_html', 'grunion_contact_form_field_html_filter', 10, 3 );
  13. }
  14. add_action( 'wpml_loaded', 'wpml_jetpack_init' );
  15. /**
  16. * Filter the Top Posts and Pages by language.
  17. *
  18. * @param array $posts Array of the most popular posts.
  19. * @param array $post_ids Array of Post IDs.
  20. * @param string $count Number of Top Posts we want to display.
  21. *
  22. * @return array
  23. */
  24. function wpml_jetpack_widget_get_top_posts( $posts, $post_ids, $count ) {
  25. global $sitepress;
  26. foreach ( $posts as $k => $post ) {
  27. $lang_information = wpml_get_language_information( $post['post_id'] );
  28. if ( ! is_wp_error( $lang_information ) ) {
  29. $post_language = substr( $lang_information['locale'], 0, 2 );
  30. if ( $post_language !== $sitepress->get_current_language() ) {
  31. unset( $posts[ $k ] );
  32. }
  33. }
  34. }
  35. return $posts;
  36. }
  37. /**
  38. * Filter the HTML of the Contact Form and output the one requested by language.
  39. *
  40. * @param string $r Contact Form HTML output.
  41. * @param string $field_label Field label.
  42. * @param int|null $id Post ID.
  43. *
  44. * @return string
  45. */
  46. function grunion_contact_form_field_html_filter( $r, $field_label, $id ){
  47. global $sitepress;
  48. if ( function_exists( 'icl_translate' ) ) {
  49. if ( $sitepress->get_current_language() !== $sitepress->get_default_language() ) {
  50. $label_translation = icl_translate( 'jetpack ', $field_label . '_label', $field_label );
  51. $r = str_replace( $field_label, $label_translation, $r );
  52. }
  53. }
  54. return $r;
  55. }