PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.7
Elementor Website Builder – more than just a page builder v4.0.7
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / utils / force-locale.php

force-locale.php in Elementor Website Builder – more than just a page builder 4.0.7, at core/utils/force-locale.php

139 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Utils;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 /**
9 * Force translation to use a specific locale.
10 *
11 * A hacky class to force any translation functions in the call-stack between the
12 * `force()` & `reset()` methods to use a specific locale.
13 */
14 class Force_Locale {
15
16 /**
17 * @var string Locale to force (e.g. `he_IL`).
18 */
19 private $new_locale;
20
21 /**
22 * @var string Original locale before forcing.
23 */
24 private $original_locale;
25
26 /**
27 * @var \WP_Textdomain_Registry
28 */
29 private $original_textdomain_registry;
30
31 /**
32 * @var \Closure Filter reference `pre_determine_locale`.
33 */
34 private $filter;
35
36 public function __construct( $new_locale, $original_locale = null ) {
37 $this->new_locale = $new_locale;
38
39 $this->original_locale = $original_locale ? $original_locale : determine_locale();
40
41 $this->filter = function() use ( $new_locale ) {
42 return $new_locale;
43 };
44 }
45
46 /**
47 * Force the translations to use a specific locale.
48 *
49 * @return void
50 */
51 public function force() {
52 switch_to_locale( $this->new_locale );
53
54 /**
55 * Reset the \WP_Textdomain_Registry instance to clear its cache.
56 *
57 * @see https://github.com/WordPress/wordpress-develop/blob/799d7dc86f5b07b17f7a418948fc851bd2fc334b/src/wp-includes/class-wp-textdomain-registry.php#L179-L187
58 * @see https://github.com/WordPress/wordpress-develop/blob/799d7dc86f5b07b17f7a418948fc851bd2fc334b/tests/phpunit/tests/l10n/wpLocaleSwitcher.php#L19-L31
59 */
60 $this->reset_textdomain_registry();
61
62 /**
63 * Reset l10n in order to clear the translations cache.
64 *
65 * @see https://github.com/WordPress/wordpress-develop/blob/2437ef5130f10153bc4fffa412d4f37e65e3d66b/src/wp-includes/l10n.php#L1324
66 * @see https://github.com/WordPress/wordpress-develop/blob/2437ef5130f10153bc4fffa412d4f37e65e3d66b/src/wp-includes/l10n.php#L1222
67 * @see https://github.com/WordPress/wordpress-develop/blob/2437ef5130f10153bc4fffa412d4f37e65e3d66b/src/wp-includes/l10n.php#L821
68 */
69 $this->reset_l10n();
70
71 /**
72 * Force the translations of `$new_locale` to be loaded.
73 *
74 * @see https://github.com/WordPress/wordpress-develop/blob/2437ef5130f10153bc4fffa412d4f37e65e3d66b/src/wp-includes/l10n.php#L1294
75 */
76 add_filter( 'pre_determine_locale', $this->filter );
77 }
78
79 /**
80 * Restore the original locale and cleanup filters, etc.
81 *
82 * @return void
83 */
84 public function restore() {
85 $this->restore_textdomain_registry();
86
87 $this->reset_l10n();
88
89 switch_to_locale( $this->original_locale );
90
91 remove_filter( 'pre_determine_locale', $this->filter );
92 }
93
94 private function reset_textdomain_registry() {
95 if ( ! class_exists( '\WP_Textdomain_Registry' ) ) {
96 return;
97 }
98
99 /** @var \WP_Textdomain_Registry $wp_textdomain_registry */
100 global $wp_textdomain_registry;
101
102 $this->original_textdomain_registry = $wp_textdomain_registry;
103
104 $wp_textdomain_registry = new \WP_Textdomain_Registry();
105 }
106
107 private function restore_textdomain_registry() {
108 if ( ! $this->original_textdomain_registry ) {
109 return;
110 }
111
112 /** @var \WP_Textdomain_Registry $wp_textdomain_registry */
113 global $wp_textdomain_registry;
114
115 $wp_textdomain_registry = $this->original_textdomain_registry;
116 }
117
118 /**
119 * Reset the l10n global variables.
120 *
121 * @return void
122 */
123 private function reset_l10n() {
124 global $l10n, $l10n_unloaded;
125
126 if ( is_array( $l10n ) ) {
127 foreach ( $l10n as $domain => $l10n_data ) {
128 unset( $l10n[ $domain ] );
129 }
130 }
131
132 if ( is_array( $l10n_unloaded ) ) {
133 foreach ( $l10n_unloaded as $domain => $l10n_unloaded_data ) {
134 unset( $l10n_unloaded[ $domain ] );
135 }
136 }
137 }
138 }
139