Complex classes like PodsI18n often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PodsI18n, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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_data = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Singleton handling for a basic pods_i18n() request |
||
| 31 | * |
||
| 32 | * @since 2.7 |
||
| 33 | */ |
||
| 34 | private function __construct() { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Singleton handling for a basic pods_i18n() request |
||
| 49 | * |
||
| 50 | * @return \PodsI18n |
||
| 51 | * |
||
| 52 | * @since 2.7 |
||
| 53 | */ |
||
| 54 | public static function get_instance() { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @since 2.7 |
||
| 66 | */ |
||
| 67 | public function enqueue_scripts() { |
||
| 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 |
||
| 82 | */ |
||
| 83 | private static function localize_assets() { |
||
| 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 |
||
| 120 | */ |
||
| 121 | private static function register( $string_key, $translation ) { |
||
| 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 |
||
| 144 | */ |
||
| 145 | private static function default_strings() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get current locale information from Multilingual plugins |
||
| 207 | * |
||
| 208 | * @since 2.7 |
||
| 209 | * |
||
| 210 | * @param array $args (optional) { |
||
| 211 | * |
||
| 212 | * @type bool $refresh Rerun get_current_language() logic? |
||
| 213 | * } |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function get_current_language( $args = array() ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get current language information from Multilingual plugins |
||
| 236 | * |
||
| 237 | * @since 2.6.6 |
||
| 238 | * @since 2.7 Moved to this class from PodsAPI |
||
| 239 | * |
||
| 240 | * @param array $args (optional) { |
||
| 241 | * |
||
| 242 | * @type bool $refresh Rerun logic? |
||
| 243 | * } |
||
| 244 | * |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 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 | |||
| 455 | /** |
||
| 456 | * Add Pods templates to possible i18n enabled post-types (polylang settings). |
||
| 457 | * |
||
| 458 | * @since 2.7 |
||
| 459 | * |
||
| 460 | * @param array $post_types |
||
| 461 | * @param bool $is_settings |
||
| 462 | * |
||
| 463 | * @return array mixed |
||
| 464 | */ |
||
| 465 | public function pll_get_post_types( $post_types, $is_settings = false ) { |
||
| 473 | |||
| 474 | } |
||
| 475 |
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