Completed
Branch BUG/undefined-function-get_use... (134d66)
by
unknown
14:47 queued 20s
created
core/services/assets/I18nRegistry.php 1 patch
Indentation   +234 added lines, -234 removed lines patch added patch discarded remove patch
@@ -14,238 +14,238 @@
 block discarded – undo
14 14
  */
15 15
 class I18nRegistry
16 16
 {
17
-    /**
18
-     * @var DomainInterface
19
-     */
20
-    private $domain;
21
-
22
-    /**
23
-     * Will hold all registered i18n scripts.  Prevents script handles from being registered more than once.
24
-     *
25
-     * @var array
26
-     */
27
-    private $registered_i18n = array();
28
-
29
-
30
-    /**
31
-     * Used to hold queued translations for the chunks loading in a view.
32
-     *
33
-     * @var array
34
-     */
35
-    private $queued_handle_translations = array();
36
-
37
-    /**
38
-     * Used to track script handles queued for adding translation strings as inline data in the dom.
39
-     *
40
-     * @var array
41
-     */
42
-    private $queued_scripts = array();
43
-
44
-
45
-    /**
46
-     * Obtained from the generated json file from the all javascript using wp.i18n with a map of script handle names to
47
-     * translation strings.
48
-     *
49
-     * @var array
50
-     */
51
-    private $i18n_map;
52
-
53
-
54
-    /**
55
-     * I18nRegistry constructor.
56
-     *
57
-     * @param array() $i18n_map  An array of script handle names and the strings translated for those handles.  If not
58
-     *                            provided, the class will look for map in root of plugin with filename of
59
-     *                            'translation-map.json'.
60
-     * @param DomainInterface $domain
61
-     */
62
-    public function __construct(array $i18n_map = array(), DomainInterface $domain)
63
-    {
64
-        $this->domain = $domain;
65
-        $this->setI18nMap($i18n_map);
66
-        add_filter('print_scripts_array', array($this, 'queueI18n'));
67
-    }
68
-
69
-
70
-    /**
71
-     * Used to register a script that has i18n strings for its $handle
72
-     *
73
-     * @param string $handle The script handle reference.
74
-     * @param string $domain The i18n domain for the strings.
75
-     */
76
-    public function registerScriptI18n($handle, $domain = 'event_espresso')
77
-    {
78
-        if(! isset($this->registered_i18n[$handle])) {
79
-            $this->registered_i18n[ $handle ] = 1;
80
-            $this->queued_scripts[ $handle ] = $domain;
81
-        }
82
-    }
83
-
84
-
85
-
86
-    /**
87
-     * Callback on print_scripts_array to listen for scripts enqueued and handle setting up the localized data.
88
-     *
89
-     * @param array $handles Array of registered script handles.
90
-     * @return array
91
-     */
92
-    public function queueI18n(array $handles)
93
-    {
94
-        if (empty($this->queued_scripts)) {
95
-            return $handles;
96
-        }
97
-        foreach ($handles as $handle) {
98
-            $this->queueI18nTranslationsForHandle($handle);
99
-        }
100
-        if ($this->queued_handle_translations) {
101
-            foreach ($this->queued_handle_translations as $handle => $translations_for_domain) {
102
-                $this->registerInlineScript(
103
-                    $handle,
104
-                    $translations_for_domain['translations'],
105
-                    $translations_for_domain['domain']
106
-                );
107
-            }
108
-        }
109
-        return $handles;
110
-    }
111
-
112
-
113
-    /**
114
-     * Registers inline script with translations for given handle and domain.
115
-     *
116
-     * @param string $handle       Handle used to register javascript file containing translations.
117
-     * @param array  $translations Array of string translations.
118
-     * @param string $domain       Domain for translations.  If left empty then strings are registered with the default
119
-     *                             domain for the javascript.
120
-     */
121
-    protected function registerInlineScript($handle, array $translations, $domain)
122
-    {
123
-        $script = $domain ?
124
-            'eejs.i18n.setLocaleData( ' . wp_json_encode($translations) . ', "' . $domain . '" );' :
125
-            'eejs.i18n.setLocaleData( ' . wp_json_encode($translations) . ' );';
126
-        wp_add_inline_script($handle, $script, 'before');
127
-    }
128
-
129
-
130
-    /**
131
-     * Queues up the translation strings for the given handle.
132
-     *
133
-     * @param string $handle The script handle being queued up.
134
-     */
135
-    private function queueI18nTranslationsForHandle($handle)
136
-    {
137
-        if (isset($this->queued_scripts[$handle])) {
138
-            $domain = $this->queued_scripts[$handle];
139
-            $translations = $this->getJedLocaleDataForDomainAndChunk($handle, $domain);
140
-            if (count($translations) > 0) {
141
-                $this->queued_handle_translations[$handle] = array(
142
-                    'domain'       => $domain,
143
-                    'translations' => $translations,
144
-                );
145
-            }
146
-            unset($this->queued_scripts[$handle]);
147
-        }
148
-    }
149
-
150
-
151
-    /**
152
-     * Sets the internal i18n_map property.
153
-     * If $chunk_map is empty or not an array, will attempt to load a chunk map from a default named map.
154
-     *
155
-     * @param array $i18n_map  If provided, an array of translation strings indexed by script handle names they
156
-     *                         correspond to.
157
-     */
158
-    private function setI18nMap(array $i18n_map)
159
-    {
160
-        if (empty($i18n_map)) {
161
-            $i18n_map = file_exists($this->domain->pluginPath() . 'translation-map.json')
162
-                ? json_decode(
163
-                        file_get_contents($this->domain->pluginPath() . 'translation-map.json'),
164
-                        true
165
-                    )
166
-                : array();
167
-        }
168
-        $this->i18n_map = $i18n_map;
169
-    }
170
-
171
-
172
-    /**
173
-     * Get the jed locale data for a given $handle and domain
174
-     *
175
-     * @param string $handle The name for the script handle we want strings returned for.
176
-     * @param string $domain The i18n domain.
177
-     * @return array
178
-     */
179
-    protected function getJedLocaleDataForDomainAndChunk($handle, $domain)
180
-    {
181
-        $translations = $this->getJedLocaleData($domain);
182
-        // get index for adding back after extracting strings for this $chunk.
183
-        $index = $translations[''];
184
-        $translations = $this->getLocaleDataMatchingMap(
185
-            $this->getOriginalStringsForHandleFromMap($handle),
186
-            $translations
187
-        );
188
-        $translations[''] = $index;
189
-        return $translations;
190
-    }
191
-
192
-
193
-    /**
194
-     * Get locale data for given strings from given translations
195
-     *
196
-     * @param array $string_set   This is the subset of strings (msgIds) we want to extract from the translations array.
197
-     * @param array $translations Translation data to extra strings from.
198
-     * @return array
199
-     */
200
-    protected function getLocaleDataMatchingMap(array $string_set, array $translations)
201
-    {
202
-        if (empty($string_set)) {
203
-            return array();
204
-        }
205
-        // some strings with quotes in them will break on the array_flip, so making sure quotes in the string are
206
-        // slashed also filter falsey values.
207
-        $string_set = array_unique(array_filter(wp_slash($string_set)));
208
-        return array_intersect_key($translations, array_flip($string_set));
209
-    }
210
-
211
-
212
-    /**
213
-     * Get original strings to translate for the given chunk from the map
214
-     *
215
-     * @param string $handle The script handle name to get strings from the map for.
216
-     * @return array
217
-     */
218
-    protected function getOriginalStringsForHandleFromMap($handle)
219
-    {
220
-        return isset($this->i18n_map[$handle]) ? $this->i18n_map[$handle] : array();
221
-    }
222
-
223
-
224
-    /**
225
-     * Returns Jed-formatted localization data.
226
-     *
227
-     * @param  string $domain Translation domain.
228
-     * @return array
229
-     */
230
-    private function getJedLocaleData($domain)
231
-    {
232
-        $translations = get_translations_for_domain($domain);
233
-
234
-        $locale = array(
235
-            '' => array(
236
-                'domain' => $domain,
237
-                'lang'   => is_admin() ? EEH_DTT_Helper::get_user_locale() : get_locale()
238
-            ),
239
-        );
240
-
241
-        if (! empty($translations->headers['Plural-Forms'])) {
242
-            $locale['']['plural_forms'] = $translations->headers['Plural-Forms'];
243
-        }
244
-
245
-        foreach ($translations->entries as $msgid => $entry) {
246
-            $locale[$msgid] = $entry->translations;
247
-        }
248
-
249
-        return $locale;
250
-    }
17
+	/**
18
+	 * @var DomainInterface
19
+	 */
20
+	private $domain;
21
+
22
+	/**
23
+	 * Will hold all registered i18n scripts.  Prevents script handles from being registered more than once.
24
+	 *
25
+	 * @var array
26
+	 */
27
+	private $registered_i18n = array();
28
+
29
+
30
+	/**
31
+	 * Used to hold queued translations for the chunks loading in a view.
32
+	 *
33
+	 * @var array
34
+	 */
35
+	private $queued_handle_translations = array();
36
+
37
+	/**
38
+	 * Used to track script handles queued for adding translation strings as inline data in the dom.
39
+	 *
40
+	 * @var array
41
+	 */
42
+	private $queued_scripts = array();
43
+
44
+
45
+	/**
46
+	 * Obtained from the generated json file from the all javascript using wp.i18n with a map of script handle names to
47
+	 * translation strings.
48
+	 *
49
+	 * @var array
50
+	 */
51
+	private $i18n_map;
52
+
53
+
54
+	/**
55
+	 * I18nRegistry constructor.
56
+	 *
57
+	 * @param array() $i18n_map  An array of script handle names and the strings translated for those handles.  If not
58
+	 *                            provided, the class will look for map in root of plugin with filename of
59
+	 *                            'translation-map.json'.
60
+	 * @param DomainInterface $domain
61
+	 */
62
+	public function __construct(array $i18n_map = array(), DomainInterface $domain)
63
+	{
64
+		$this->domain = $domain;
65
+		$this->setI18nMap($i18n_map);
66
+		add_filter('print_scripts_array', array($this, 'queueI18n'));
67
+	}
68
+
69
+
70
+	/**
71
+	 * Used to register a script that has i18n strings for its $handle
72
+	 *
73
+	 * @param string $handle The script handle reference.
74
+	 * @param string $domain The i18n domain for the strings.
75
+	 */
76
+	public function registerScriptI18n($handle, $domain = 'event_espresso')
77
+	{
78
+		if(! isset($this->registered_i18n[$handle])) {
79
+			$this->registered_i18n[ $handle ] = 1;
80
+			$this->queued_scripts[ $handle ] = $domain;
81
+		}
82
+	}
83
+
84
+
85
+
86
+	/**
87
+	 * Callback on print_scripts_array to listen for scripts enqueued and handle setting up the localized data.
88
+	 *
89
+	 * @param array $handles Array of registered script handles.
90
+	 * @return array
91
+	 */
92
+	public function queueI18n(array $handles)
93
+	{
94
+		if (empty($this->queued_scripts)) {
95
+			return $handles;
96
+		}
97
+		foreach ($handles as $handle) {
98
+			$this->queueI18nTranslationsForHandle($handle);
99
+		}
100
+		if ($this->queued_handle_translations) {
101
+			foreach ($this->queued_handle_translations as $handle => $translations_for_domain) {
102
+				$this->registerInlineScript(
103
+					$handle,
104
+					$translations_for_domain['translations'],
105
+					$translations_for_domain['domain']
106
+				);
107
+			}
108
+		}
109
+		return $handles;
110
+	}
111
+
112
+
113
+	/**
114
+	 * Registers inline script with translations for given handle and domain.
115
+	 *
116
+	 * @param string $handle       Handle used to register javascript file containing translations.
117
+	 * @param array  $translations Array of string translations.
118
+	 * @param string $domain       Domain for translations.  If left empty then strings are registered with the default
119
+	 *                             domain for the javascript.
120
+	 */
121
+	protected function registerInlineScript($handle, array $translations, $domain)
122
+	{
123
+		$script = $domain ?
124
+			'eejs.i18n.setLocaleData( ' . wp_json_encode($translations) . ', "' . $domain . '" );' :
125
+			'eejs.i18n.setLocaleData( ' . wp_json_encode($translations) . ' );';
126
+		wp_add_inline_script($handle, $script, 'before');
127
+	}
128
+
129
+
130
+	/**
131
+	 * Queues up the translation strings for the given handle.
132
+	 *
133
+	 * @param string $handle The script handle being queued up.
134
+	 */
135
+	private function queueI18nTranslationsForHandle($handle)
136
+	{
137
+		if (isset($this->queued_scripts[$handle])) {
138
+			$domain = $this->queued_scripts[$handle];
139
+			$translations = $this->getJedLocaleDataForDomainAndChunk($handle, $domain);
140
+			if (count($translations) > 0) {
141
+				$this->queued_handle_translations[$handle] = array(
142
+					'domain'       => $domain,
143
+					'translations' => $translations,
144
+				);
145
+			}
146
+			unset($this->queued_scripts[$handle]);
147
+		}
148
+	}
149
+
150
+
151
+	/**
152
+	 * Sets the internal i18n_map property.
153
+	 * If $chunk_map is empty or not an array, will attempt to load a chunk map from a default named map.
154
+	 *
155
+	 * @param array $i18n_map  If provided, an array of translation strings indexed by script handle names they
156
+	 *                         correspond to.
157
+	 */
158
+	private function setI18nMap(array $i18n_map)
159
+	{
160
+		if (empty($i18n_map)) {
161
+			$i18n_map = file_exists($this->domain->pluginPath() . 'translation-map.json')
162
+				? json_decode(
163
+						file_get_contents($this->domain->pluginPath() . 'translation-map.json'),
164
+						true
165
+					)
166
+				: array();
167
+		}
168
+		$this->i18n_map = $i18n_map;
169
+	}
170
+
171
+
172
+	/**
173
+	 * Get the jed locale data for a given $handle and domain
174
+	 *
175
+	 * @param string $handle The name for the script handle we want strings returned for.
176
+	 * @param string $domain The i18n domain.
177
+	 * @return array
178
+	 */
179
+	protected function getJedLocaleDataForDomainAndChunk($handle, $domain)
180
+	{
181
+		$translations = $this->getJedLocaleData($domain);
182
+		// get index for adding back after extracting strings for this $chunk.
183
+		$index = $translations[''];
184
+		$translations = $this->getLocaleDataMatchingMap(
185
+			$this->getOriginalStringsForHandleFromMap($handle),
186
+			$translations
187
+		);
188
+		$translations[''] = $index;
189
+		return $translations;
190
+	}
191
+
192
+
193
+	/**
194
+	 * Get locale data for given strings from given translations
195
+	 *
196
+	 * @param array $string_set   This is the subset of strings (msgIds) we want to extract from the translations array.
197
+	 * @param array $translations Translation data to extra strings from.
198
+	 * @return array
199
+	 */
200
+	protected function getLocaleDataMatchingMap(array $string_set, array $translations)
201
+	{
202
+		if (empty($string_set)) {
203
+			return array();
204
+		}
205
+		// some strings with quotes in them will break on the array_flip, so making sure quotes in the string are
206
+		// slashed also filter falsey values.
207
+		$string_set = array_unique(array_filter(wp_slash($string_set)));
208
+		return array_intersect_key($translations, array_flip($string_set));
209
+	}
210
+
211
+
212
+	/**
213
+	 * Get original strings to translate for the given chunk from the map
214
+	 *
215
+	 * @param string $handle The script handle name to get strings from the map for.
216
+	 * @return array
217
+	 */
218
+	protected function getOriginalStringsForHandleFromMap($handle)
219
+	{
220
+		return isset($this->i18n_map[$handle]) ? $this->i18n_map[$handle] : array();
221
+	}
222
+
223
+
224
+	/**
225
+	 * Returns Jed-formatted localization data.
226
+	 *
227
+	 * @param  string $domain Translation domain.
228
+	 * @return array
229
+	 */
230
+	private function getJedLocaleData($domain)
231
+	{
232
+		$translations = get_translations_for_domain($domain);
233
+
234
+		$locale = array(
235
+			'' => array(
236
+				'domain' => $domain,
237
+				'lang'   => is_admin() ? EEH_DTT_Helper::get_user_locale() : get_locale()
238
+			),
239
+		);
240
+
241
+		if (! empty($translations->headers['Plural-Forms'])) {
242
+			$locale['']['plural_forms'] = $translations->headers['Plural-Forms'];
243
+		}
244
+
245
+		foreach ($translations->entries as $msgid => $entry) {
246
+			$locale[$msgid] = $entry->translations;
247
+		}
248
+
249
+		return $locale;
250
+	}
251 251
 }
252 252
\ No newline at end of file
Please login to merge, or discard this patch.