PluginProbe
WP Ultimate Post Grid / 4.1.1
WP Ultimate Post Grid v4.1.1
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / includes / public / class-wpupg-multilingual.php

class-wpupg-multilingual.php in WP Ultimate Post Grid 4.1.1, at includes/public/class-wpupg-multilingual.php

127 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle multilingual functionality.
4 *
5 * @link https://bootstrapped.ventures
6 * @since 3.9.0
7 *
8 * @package WP_Ultimate_Post_Grid
9 * @subpackage WP_Ultimate_Post_Grid/includes/public
10 */
11
12 /**
13 * Handle multilingual functionality.
14 *
15 * @since 3.9.0
16 * @package WP_Ultimate_Post_Grid
17 * @subpackage WP_Ultimate_Post_Grid/includes/public
18 * @author Brecht Vandersmissen <brecht@bootstrapped.ventures>
19 */
20 class WPUPG_Multilingual {
21
22 /**
23 * Language to set for WPML
24 *
25 * @since 3.9.0
26 * @access private
27 * @var mixed $wpml_language Language to set for WPML.
28 */
29 private static $wpml_language = false;
30
31 /**
32 * Register actions and filters.
33 *
34 * @since 3.9.0
35 */
36 public static function init() {
37 add_action( 'pre_get_posts', array( __CLASS__, 'set_language_wpml' ) );
38 }
39
40 /**
41 * Check if and what multilingual plugin is getting used.
42 *
43 * @since 3.9.0
44 */
45 public static function get() {
46 $plugin = false;
47 $languages = array();
48 $current_language = false;
49 $default_language = false;
50
51 // WPML.
52 $wpml_languages = apply_filters( 'wpml_active_languages', false );
53
54 if ( $wpml_languages ) {
55 $plugin = 'wpml';
56
57 foreach ( $wpml_languages as $code => $options ) {
58 $languages[ $code ] = array(
59 'value' => $code,
60 'label' => $options['native_name'],
61 );
62 }
63
64 $current_language = ICL_LANGUAGE_CODE;
65 $default_language = apply_filters( 'wpml_default_language', false );
66 }
67
68 // Return either false (no multilingual plugin) or an array with the plugin and activated languages.
69 return ! $plugin ? false : array(
70 'plugin' => $plugin,
71 'languages' => $languages,
72 'current' => $current_language,
73 'default' => $default_language,
74 );
75 }
76
77 /**
78 * Set the language to use.
79 *
80 * @since 3.9.0
81 * @param mixed $language Language to set.
82 */
83 public static function set_language( $language ) {
84 if ( $language ) {
85 $multilingual = self::get();
86
87 if ( $multilingual ) {
88 // WPML.
89 if ( 'wpml' === $multilingual['plugin'] ) {
90 self::$wpml_language = $language;
91 }
92 }
93 }
94 }
95
96 /**
97 * Set the WPML language.
98 *
99 * @since 3.9.0
100 * @param mixed $query Post query.
101 */
102 public static function set_language_wpml( $query ) {
103 if ( self::$wpml_language ) {
104 do_action( 'wpml_switch_language', self::$wpml_language );
105 }
106 }
107
108 /**
109 * Unset the language.
110 *
111 * @since 3.9.0
112 */
113 public static function unset_language() {
114 $multilingual = self::get();
115
116 if ( $multilingual ) {
117 // WPML.
118 if ( 'wpml' === $multilingual['plugin'] ) {
119 do_action( 'wpml_switch_language', null );
120 self::$wpml_language = false;
121 }
122 }
123 }
124 }
125
126 WPUPG_Multilingual::init();
127