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