| Conditions | 53 |
| Paths | 1801 |
| Total Lines | 223 |
| Code Lines | 76 |
| Lines | 14 |
| Ratio | 6.28 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 244 | public function get_current_language_data( $args = array() ) { |
||
| 245 | |||
| 246 | $args = wp_parse_args( $args, array( |
||
| 247 | 'refresh' => false, |
||
| 248 | ) ); |
||
| 249 | |||
| 250 | if ( ! $args['refresh'] && ! empty( self::$current_language_data ) ) { |
||
| 251 | return self::$current_language_data; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @var $sitepress SitePress object |
||
| 256 | * @var $polylang Polylang object |
||
| 257 | */ |
||
| 258 | /* |
||
| 259 | * @todo wpml-comp Remove global object usage |
||
| 260 | */ |
||
| 261 | global $sitepress, $polylang; |
||
|
|
|||
| 262 | |||
| 263 | $lang_data = false; |
||
| 264 | $translator = false; |
||
| 265 | $current_language = false; |
||
| 266 | |||
| 267 | // Multilingual support |
||
| 268 | if ( did_action( 'wpml_loaded' ) && apply_filters( 'wpml_setting', true, 'auto_adjust_ids' ) ) { |
||
| 269 | // WPML support |
||
| 270 | $translator = 'WPML'; |
||
| 271 | |||
| 272 | // Get the global current language (if set) |
||
| 273 | $wpml_language = apply_filters( 'wpml_current_language', null ); |
||
| 274 | $current_language = ( $wpml_language != 'all' ) ? $wpml_language : ''; |
||
| 275 | |||
| 276 | } elseif ( ( function_exists( 'PLL' ) || is_object( $polylang ) ) && function_exists( 'pll_current_language' ) ) { |
||
| 277 | // Polylang support |
||
| 278 | $translator = 'PLL'; |
||
| 279 | |||
| 280 | // Get the global current language (if set) |
||
| 281 | $current_language = pll_current_language( 'slug' ); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Admin functions that overwrite the current language |
||
| 286 | * |
||
| 287 | * @since 2.6.6 |
||
| 288 | */ |
||
| 289 | if ( is_admin() && ! empty( $translator ) ) { |
||
| 290 | if ( $translator == 'PLL' ) { |
||
| 291 | /** |
||
| 292 | * Polylang support |
||
| 293 | * Get the current user's preferred language. |
||
| 294 | * This is a user meta setting that will overwrite the language returned from pll_current_language() |
||
| 295 | * @see polylang/admin/admin-base.php -> init_user() |
||
| 296 | */ |
||
| 297 | $current_language = get_user_meta( get_current_user_id(), 'pll_filter_content', true ); |
||
| 298 | } |
||
| 299 | |||
| 300 | // Get current language based on the object language if available |
||
| 301 | if ( function_exists( 'get_current_screen' ) ) { |
||
| 302 | $current_screen = get_current_screen(); |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Overwrite the current language if needed for post types |
||
| 306 | */ |
||
| 307 | if ( isset( $current_screen->base ) && ( $current_screen->base == 'post' || $current_screen->base == 'edit' ) ) { |
||
| 308 | if ( ! empty( $_GET['post'] ) ) { |
||
| 309 | /** |
||
| 310 | * WPML support |
||
| 311 | * In WPML the current language is always set to default on an edit screen |
||
| 312 | * We need to overwrite this when the current object is not-translatable to enable relationships with different languages |
||
| 313 | */ |
||
| 314 | if ( $translator == 'WPML' |
||
| 315 | && ! apply_filters( 'wpml_is_translated_post_type', false, ( get_post_type( $_GET['post'] ) ) ) |
||
| 316 | ) { |
||
| 317 | // Overwrite the current language to nothing if this is a NOT-translatable post_type |
||
| 318 | $current_language = ''; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Polylang support (1.5.4+) |
||
| 323 | * In polylang the preferred language could be anything. |
||
| 324 | * We only want the related objects if they are not translatable OR the same language as the current object |
||
| 325 | */ |
||
| 326 | if ( $translator == 'PLL' |
||
| 327 | && function_exists( 'pll_get_post_language' ) |
||
| 328 | && pll_is_translated_post_type( get_post_type( $_GET['post'] ) ) |
||
| 329 | ) { |
||
| 330 | // Overwrite the current language if this is a translatable post_type |
||
| 331 | $current_language = pll_get_post_language( (int) $_GET['post'] ); |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Polylang support (1.0.1+) |
||
| 337 | * In polylang the preferred language could be anything. |
||
| 338 | * When we're adding a new object and language is set we only want the related objects if they are not translatable OR the same language |
||
| 339 | */ |
||
| 340 | View Code Duplication | if ( $translator == 'PLL' |
|
| 341 | && ! empty( $_GET['new_lang'] ) |
||
| 342 | && ! empty( $_GET['post_type'] ) |
||
| 343 | && pll_is_translated_post_type( sanitize_text_field( $_GET['post_type'] ) ) |
||
| 344 | ) { |
||
| 345 | $current_language = $_GET['new_lang']; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Overwrite the current language if needed for taxonomies |
||
| 350 | */ |
||
| 351 | } elseif ( isset( $current_screen->base ) && ( $current_screen->base == 'term' || $current_screen->base == 'edit-tags' ) ) { |
||
| 352 | // @todo MAYBE: Similar function like get_post_type for taxonomies so we don't need to check for $_GET['taxonomy'] |
||
| 353 | if ( ! empty( $_GET['taxonomy'] ) ) { |
||
| 354 | /* |
||
| 355 | * @todo wpml-comp API call for taxonomy needed! |
||
| 356 | * Suggested API call: |
||
| 357 | * add_filter( 'wpml_is_translated_taxonomy', $_GET['taxonomy'], 10, 2 ); |
||
| 358 | */ |
||
| 359 | /** |
||
| 360 | * WPML support |
||
| 361 | * In WPML the current language is always set to default on an edit screen |
||
| 362 | * We need to overwrite this when the current object is not-translatable to enable relationships with different languages |
||
| 363 | */ |
||
| 364 | if ( $translator == 'WPML' |
||
| 365 | && method_exists( $sitepress, 'is_translated_taxonomy') |
||
| 366 | && ! $sitepress->is_translated_taxonomy( $_GET['taxonomy'] ) |
||
| 367 | ) { |
||
| 368 | // Overwrite the current language to nothing if this is a NOT-translatable taxonomy |
||
| 369 | $current_language = ''; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Polylang support (1.5.4+) |
||
| 374 | * In polylang the preferred language could be anything. |
||
| 375 | * We only want the related objects if they are not translatable OR the same language as the current object |
||
| 376 | */ |
||
| 377 | if ( $translator == 'PLL' |
||
| 378 | && ! empty( $_GET['tag_ID'] ) |
||
| 379 | && function_exists( 'pll_get_term_language' ) |
||
| 380 | && pll_is_translated_taxonomy( sanitize_text_field( $_GET['taxonomy'] ) ) |
||
| 381 | ) { |
||
| 382 | // Overwrite the current language if this is a translatable taxonomy |
||
| 383 | $current_language = pll_get_term_language( (int) $_GET['tag_ID'] ); |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Polylang support (1.0.1+) |
||
| 389 | * In polylang the preferred language could be anything. |
||
| 390 | * When we're adding a new object and language is set we only want the related objects if they are not translatable OR the same language |
||
| 391 | */ |
||
| 392 | View Code Duplication | if ( $translator == 'PLL' |
|
| 393 | && ! empty( $_GET['new_lang'] ) |
||
| 394 | && ! empty( $_GET['taxonomy'] ) |
||
| 395 | && pll_is_translated_taxonomy( sanitize_text_field( $_GET['taxonomy'] ) ) |
||
| 396 | ) { |
||
| 397 | $current_language = $_GET['new_lang']; |
||
| 398 | } |
||
| 399 | } |
||
| 400 | } |
||
| 401 | } |
||
| 402 | |||
| 403 | $current_language = pods_sanitize( sanitize_text_field( $current_language ) ); |
||
| 404 | |||
| 405 | if ( ! empty( $current_language ) ) { |
||
| 406 | // We need to return language data |
||
| 407 | $lang_data = array( |
||
| 408 | 'language' => $current_language, |
||
| 409 | 't_id' => 0, |
||
| 410 | 'tt_id' => 0, |
||
| 411 | 'term' => null, |
||
| 412 | ); |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Polylang support |
||
| 416 | * Get the language taxonomy object for the current language |
||
| 417 | */ |
||
| 418 | if ( $translator == 'PLL' ) { |
||
| 419 | $current_language_t = false; |
||
| 420 | |||
| 421 | // Get the language term object |
||
| 422 | if ( function_exists( 'PLL' ) && isset( PLL()->model ) && method_exists( PLL()->model, 'get_language' ) ) { |
||
| 423 | // Polylang 1.8 and newer |
||
| 424 | $current_language_t = PLL()->model->get_language( $current_language ); |
||
| 425 | } elseif ( is_object( $polylang ) && isset( $polylang->model ) && method_exists( $polylang->model, 'get_language' ) ) { |
||
| 426 | // Polylang 1.2 - 1.7.x |
||
| 427 | $current_language_t = $polylang->model->get_language( $current_language ); |
||
| 428 | } elseif ( is_object( $polylang ) && method_exists( $polylang, 'get_language' ) ) { |
||
| 429 | // Polylang 1.1.x and older |
||
| 430 | $current_language_t = $polylang->get_language( $current_language ); |
||
| 431 | } |
||
| 432 | |||
| 433 | // If the language object exists, add it! |
||
| 434 | if ( $current_language_t && ! empty( $current_language_t->term_id ) ) { |
||
| 435 | $lang_data['t_id'] = (int) $current_language_t->term_id; |
||
| 436 | $lang_data['tt_id'] = (int) $current_language_t->term_taxonomy_id; |
||
| 437 | $lang_data['tl_t_id'] = (int) $current_language_t->tl_term_id; |
||
| 438 | $lang_data['tl_tt_id'] = (int) $current_language_t->tl_term_taxonomy_id; |
||
| 439 | $lang_data['term'] = $current_language_t; |
||
| 440 | } |
||
| 441 | } |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Override language data used by Pods. |
||
| 446 | * |
||
| 447 | * @since 2.6.6 |
||
| 448 | * |
||
| 449 | * @param array|false $lang_data { |
||
| 450 | * Language data |
||
| 451 | * |
||
| 452 | * @type string $language Language slug |
||
| 453 | * @type int $t_id Language term_id |
||
| 454 | * @type int $tt_id Language term_taxonomy_id |
||
| 455 | * @type WP_Term $term Language term object |
||
| 456 | * } |
||
| 457 | * @param string|boolean $translator Language plugin used |
||
| 458 | */ |
||
| 459 | $lang_data = apply_filters( 'pods_get_current_language', $lang_data, $translator ); |
||
| 460 | |||
| 461 | self::$current_language = $lang_data['language']; |
||
| 462 | self::$current_language_data = $lang_data; |
||
| 463 | |||
| 464 | return $lang_data; |
||
| 465 | |||
| 466 | } |
||
| 467 | |||
| 469 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state