PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.2.5
Pods – Custom Content Types and Fields v3.2.5
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / classes / PodsI18n.php
pods / classes Last commit date
cli 3 years ago fields 1 year ago widgets 3 years ago Pods.php 2 years ago PodsAPI.php 2 years ago PodsAdmin.php 1 year ago PodsArray.php 2 years ago PodsComponent.php 8 years ago PodsComponents.php 3 years ago PodsData.php 2 years ago PodsField.php 2 years ago PodsForm.php 2 years ago PodsI18n.php 4 years ago PodsInit.php 1 year ago PodsMeta.php 2 years ago PodsMigrate.php 3 years ago PodsRESTFields.php 1 year ago PodsRESTHandlers.php 3 years ago PodsTermSplitting.php 3 years ago PodsUI.php 2 years ago PodsView.php 2 years ago
PodsI18n.php
412 lines
1 <?php
2
3 /**
4 * @package Pods
5 * @since 2.7.0
6 */
7 final class PodsI18n {
8
9 /**
10 * @var PodsI18n Singleton instance
11 */
12 private static $instance = null;
13
14 /**
15 * @var array Key/value pairs with label/translation
16 */
17 private static $strings = array();
18
19 /**
20 * @var mixed Current language locale
21 */
22 private static $current_language = null;
23
24 /**
25 * @var mixed Current language data
26 */
27 private static $current_language_context = null;
28
29 /**
30 * Singleton handling for a basic pods_i18n() request
31 *
32 * @since 2.7.0
33 */
34 private function __construct() {
35
36 self::$instance = $this;
37
38 // Hook all enqueue scripts actions
39 add_action( 'pods_before_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
40 }
41
42 /**
43 * Singleton handling for a basic pods_i18n() request
44 *
45 * @return \PodsI18n
46 *
47 * @since 2.7.0
48 */
49 public static function get_instance() {
50
51 // Initialize if the class hasn't been setup yet for some reason
52 if ( ! is_object( self::$instance ) ) {
53 self::$instance = new self();
54 }
55
56 return self::$instance;
57 }
58
59 /**
60 * @since 2.7.0
61 */
62 public function enqueue_scripts() {
63
64 // Register our i18n script for JS
65 wp_register_script( 'sprintf', PODS_URL . 'ui/js/sprintf/sprintf.min.js', array(), '1.1.0', true );
66 wp_register_script( 'pods-i18n', PODS_URL . 'ui/js/pods-i18n.js', array( 'sprintf' ), PODS_VERSION, true );
67
68 self::localize_assets();
69 }
70
71 /**
72 * Localize assets:
73 * * Build localizations strings from the defaults and those provided via filter
74 * * Provide a global JavaScript object with the assembled localization strings via `wp_localize_script`
75 *
76 * @since 2.7.0
77 */
78 private static function localize_assets() {
79
80 /**
81 * Add strings to the localization
82 * Setting the key of your string to the original (non translated) value is mandatory
83 * Note: Existing keys in this class will overwrite the ones of this filter!
84 *
85 * @since 2.7.0
86 * @see default_strings()
87 *
88 * @param array
89 *
90 * @return array format: 'Untranslated string' => 'Translated string with use of WP translate functions'
91 */
92 $strings_extra = apply_filters( 'pods_localized_strings', array() );
93
94 self::$strings = array_merge( $strings_extra, self::default_strings() );
95
96 foreach ( self::$strings as $key => $str ) {
97 self::register( $key, $str );
98 }
99
100 // Some other stuff we need to pass through.
101 $i18n_base = array(
102 'debug' => ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG == true ) ? true : false,
103 );
104 // Add localization to our i18n script
105 wp_localize_script( 'pods-i18n', 'podsLocalizedStrings', array_merge( self::$strings, $i18n_base ) );
106 }
107
108 /**
109 * Register function that creates the references and combines these with the translated strings
110 *
111 * @param string $string_key
112 * @param string $translation
113 *
114 * @since 2.7.0
115 */
116 private static function register( $string_key, $translation ) {
117
118 /**
119 * Converts string into reference object variable
120 * Uses the same logic as JS to create the same references
121 */
122 $ref = '__' . $string_key;
123
124 // Add it to the strings localized
125 self::$strings[ $ref ] = $translation;
126
127 // Remove the old key
128 unset( self::$strings[ $string_key ] );
129 }
130
131 /**
132 * Register our labels to use in JS
133 * We need to register them as normal string to convert to JS references
134 * And we need to register the translations to attach to these references, these may not be variables!
135 *
136 * @return array Key/value pairs with label/translation
137 *
138 * @since 2.7.0
139 */
140 private static function default_strings() {
141
142 return array(
143
144 // Translators: %s stands for a name/identifier.
145 '%s is required.' => __( '%s is required.', 'pods' ),
146
147 'This field is required.' => __( 'This field is required.', 'pods' ),
148
149 'Add' => __( 'Add', 'pods' ),
150
151 'Add New' => __( 'Add New', 'pods' ),
152
153 'Add New Record' => __( 'Add New Record', 'pods' ),
154
155 'Added!' => __( 'Added!', 'pods' ),
156
157 'Added! Choose another or <a href="#">close this box</a>' => __( 'Added! Choose another or <a href="#">close this box</a>', 'pods' ),
158
159 'Copy' => __( 'Copy', 'pods' ),
160
161 'Reorder' => __( 'Reorder', 'pods' ),
162
163 'Remove' => __( 'Remove', 'pods' ),
164
165 'Deselect' => __( 'Deselect', 'pods' ),
166
167 'Download' => __( 'Download', 'pods' ),
168
169 'View' => __( 'View', 'pods' ),
170
171 'Edit' => __( 'Edit', 'pods' ),
172
173 'Search' => __( 'Search', 'pods' ),
174
175 // Translators: %s stands for a name/identifier.
176 'Search %s' => __( 'Search %s', 'pods' ),
177
178 'Navigating away from this page will discard any changes you have made.' => __( 'Navigating away from this page will discard any changes you have made.', 'pods' ),
179
180 'Some fields have changes that were not saved yet, please save them or cancel the changes before saving the Pod.' => __( 'Some fields have changes that were not saved yet, please save them or cancel the changes before saving the Pod.', 'pods' ),
181
182 'Unable to process request, please try again.' => __( 'Unable to process request, please try again.', 'pods' ),
183
184 'Error uploading file: ' => __( 'Error uploading file: ', 'pods' ),
185
186 'Allowed Files' => __( 'Allowed Files', 'pods' ),
187
188 'The Title' => __( 'The Title', 'pods' ),
189
190 'Select from existing' => __( 'Select from existing', 'pods' ),
191
192 'You can only select' => __( 'You can only select', 'pods' ),
193
194 // Translators: %s stands for a number.
195 '%s item' => __( '%s item', 'pods' ),
196
197 // Translators: %s stands for a number.
198 '%s items' => __( '%s items', 'pods' ),
199
200 // Translators: %s stands for a number.
201 'You can only select %s item' => __( 'You can only select %s item', 'pods' ),
202
203 // Translators: %s stands for a number.
204 'You can only select %s items' => __( 'You can only select %s items', 'pods' ),
205
206 'Icon' => __( 'Icon', 'pods' ),
207
208 );
209
210 }
211
212 /**
213 * Get current locale information from Multilingual plugins
214 *
215 * @since 2.7.0
216 *
217 * @param array $args (optional) {
218 *
219 * @type bool $refresh Rerun get_current_language() logic?
220 * }
221 *
222 * @return string
223 */
224 public function get_current_language( $args = array() ) {
225
226 $defaults = array(
227 'refresh' => empty( self::$current_language ),
228 );
229
230 $args = wp_parse_args( $args, $defaults );
231
232 if ( doing_filter( 'pods_get_current_language' ) ) {
233 // Prevent loop.
234 $args['refresh'] = false;
235 }
236
237 if ( ! $args['refresh'] ) {
238 return self::$current_language;
239 }
240
241 /**
242 * Override language data used by Pods.
243 *
244 * @since 2.8.0
245 *
246 * @param string $language Language slug
247 * @param array $context Language context
248 * @param array $args Arguments
249 */
250 self::$current_language = apply_filters( 'pods_get_current_language', self::$current_language, self::get_current_language_context( $args ), $args );
251
252 return self::$current_language;
253 }
254
255 /**
256 * Get current language context information.
257 *
258 * @since 2.6.6
259 * @since 2.7 Moved to this class from PodsAPI
260 * @since 2.8.0 Refactored from get_current_language_data()
261 *
262 * @param array $args (optional) {
263 * @type bool $refresh Rerun logic?
264 * }
265 *
266 * @return array $context {
267 * Language data.
268 * @type bool $is_admin Is admin.
269 * @type bool $is_ajax Is AJAX call.
270 * @type bool $is_pods_ajax Is Pods AJAX call.
271 * @type string $current_page Current admin page.
272 * @type string $current_object_type Current object type (post / term / comment / user).
273 * @type int $current_item_id Current item id.
274 * @type string $current_item_type Current item type.
275 * }
276 */
277 public function get_current_language_context( $args = array() ) {
278
279 $defaults = array(
280 'refresh' => empty( self::$current_language_context ),
281 );
282
283 $args = wp_parse_args( $args, $defaults );
284
285 if ( doing_filter( 'pods_get_current_language_context' ) ) {
286 // Prevent loop.
287 $args['refresh'] = false;
288 }
289
290 if ( ! $args['refresh'] ) {
291 return self::$current_language_context;
292 }
293
294 $pods_ajax = pods_v( 'pods_ajax', 'request', false );
295
296 $context = [
297 'is_admin' => is_admin(),
298 'is_ajax' => defined( 'DOING_AJAX' ) && DOING_AJAX,
299 'is_pods_ajax' => $pods_ajax,
300 'current_page' => '',
301 'current_object_type' => '',
302 'current_item_id' => '',
303 'current_item_type' => '',
304 ];
305
306 /**
307 * Admin functions that overwrite the current language context.
308 *
309 * @since 2.6.6
310 * @since 2.8.0 Refactored for current context instead of language data.
311 */
312 if ( is_admin() ) {
313
314 // Get current language based on the object language if available.
315 $page = basename( pods_v( 'SCRIPT_NAME', $_SERVER, '' ) );
316 if ( $pods_ajax && 'admin-ajax.php' === $page ) {
317 $page = basename( pods_v( 'HTTP_REFERER', $_SERVER, '' ) );
318 }
319 $page = explode( '?', $page );
320 $page = reset( $page );
321
322 $context['current_page'] = $page;
323
324 switch ( $page ) {
325
326 case 'post.php':
327 case 'edit.php':
328 $context['current_object_type'] = 'post';
329
330 $current_post_id = (int) pods_v( 'post', 'request', 0 );
331 if ( $pods_ajax ) {
332 $current_post_id = (int) pods_v( 'id', 'request', $current_post_id );
333 }
334
335 $current_post_type = pods_v( 'post_type', 'request', '' );
336 if ( ! $current_post_type && $current_post_id ) {
337 $current_post_type = get_post_type( $current_post_id );
338 }
339
340 $context['current_item_id'] = $current_post_id;
341 $context['current_item_type'] = $current_post_type;
342 break;
343
344 case 'term.php':
345 case 'edit-tags.php':
346 $context['current_object_type'] = 'term';
347
348 $current_term_id = (int) pods_v( 'tag_ID', 'request', 0 );
349 if ( $pods_ajax ) {
350 $current_term_id = (int) pods_v( 'id', 'request', $current_term_id );
351 }
352
353 $current_taxonomy = pods_v( 'taxonomy', 'request', '' );
354 if ( ! $current_taxonomy && $current_term_id ) {
355 $current_taxonomy = pods_v( 'taxonomy', get_term( $current_term_id ), null );
356 }
357
358 $context['current_item_id'] = $current_term_id;
359 $context['current_item_type'] = $current_taxonomy;
360 break;
361
362 case 'comment.php':
363 $context['current_object_type'] = 'comment';
364 $context['current_item_type'] = 'comment';
365
366 $current_comment_id = (int) pods_v( 'c', 'request', 0 );
367 if ( $pods_ajax ) {
368 $current_comment_id = (int) pods_v( 'id', 'request', $current_comment_id );
369 }
370
371 $context['current_item_id'] = $current_comment_id;
372 break;
373
374 case 'user-edit.php':
375 $context['current_object_type'] = 'user';
376 $context['current_item_type'] = 'user';
377
378 $current_user_id = (int) pods_v( 'user_id', 'request', 0 );
379 if ( $pods_ajax ) {
380 $current_user_id = (int) pods_v( 'id', 'request', $current_user_id );
381 }
382
383 $context['current_item_id'] = $current_user_id;
384 break;
385 }
386
387 }//end if (admin)
388
389 /**
390 * Override language context used by Pods.
391 *
392 * @since 2.8.0
393 *
394 * @param array $context {
395 * Language data.
396 * @type bool $is_admin Is admin.
397 * @type bool $is_ajax Is AJAX call.
398 * @type bool $is_pods_ajax Is Pods AJAX call.
399 * @type string $current_page Current admin page.
400 * @type string $current_object_type Current object type (post / term / comment / user).
401 * @type int $current_item_id Current item id.
402 * @type string $current_item_type Current item type.
403 * }
404 */
405 self::$current_language_context = apply_filters( 'pods_get_current_language_context', $context );
406
407 return self::$current_language_context;
408
409 }
410
411 }
412