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