Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PodsComponents 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 PodsComponents, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class PodsComponents { |
||
| 8 | |||
| 9 | /** |
||
| 10 | * @var PodsComponents |
||
| 11 | */ |
||
| 12 | static $instance = null; |
||
|
|
|||
| 13 | |||
| 14 | /** |
||
| 15 | * Root of Components directory |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | * |
||
| 19 | * @private |
||
| 20 | * @since 2.0 |
||
| 21 | */ |
||
| 22 | private $components_dir = null; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Available components |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | * |
||
| 29 | * @since 2.0 |
||
| 30 | */ |
||
| 31 | public $components = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Components settings |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | * |
||
| 38 | * @since 2.0 |
||
| 39 | */ |
||
| 40 | public $settings = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Singleton handling for a basic pods_components() request |
||
| 44 | * |
||
| 45 | * @return \PodsComponents |
||
| 46 | * |
||
| 47 | * @since 2.3.5 |
||
| 48 | */ |
||
| 49 | public static function init () { |
||
| 50 | if ( !is_object( self::$instance ) ) |
||
| 51 | self::$instance = new PodsComponents(); |
||
| 52 | |||
| 53 | return self::$instance; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Setup actions and get options |
||
| 58 | * |
||
| 59 | * @return \PodsComponents |
||
| 60 | * |
||
| 61 | * @since 2.0 |
||
| 62 | */ |
||
| 63 | public function __construct () { |
||
| 64 | $this->components_dir = realpath( apply_filters( 'pods_components_dir', PODS_DIR . 'components' ) ) . '/'; |
||
| 65 | |||
| 66 | $settings = get_option( 'pods_component_settings', '' ); |
||
| 67 | |||
| 68 | if ( !empty( $settings ) ) |
||
| 69 | $this->settings = (array) json_decode( $settings, true ); |
||
| 70 | |||
| 71 | if ( !isset( $this->settings[ 'components' ] ) ) |
||
| 72 | $this->settings[ 'components' ] = array(); |
||
| 73 | |||
| 74 | // Get components (give it access to theme) |
||
| 75 | add_action( 'setup_theme', array( $this, 'get_components' ), 11 ); |
||
| 76 | |||
| 77 | // Load in components |
||
| 78 | add_action( 'setup_theme', array( $this, 'load' ), 12 ); |
||
| 79 | |||
| 80 | // AJAX handling |
||
| 81 | if ( is_admin() ) { |
||
| 82 | add_action( 'wp_ajax_pods_admin_components', array( $this, 'admin_ajax' ) ); |
||
| 83 | add_action( 'wp_ajax_nopriv_pods_admin_components', array( $this, 'admin_ajax' ) ); |
||
| 84 | |||
| 85 | // Add the Pods Components capabilities |
||
| 86 | add_filter( 'members_get_capabilities', array( $this, 'admin_capabilities' ) ); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Add menu item |
||
| 92 | * |
||
| 93 | * @param string $parent The parent slug. |
||
| 94 | * |
||
| 95 | * @since 2.0 |
||
| 96 | * |
||
| 97 | * @uses add_submenu_page |
||
| 98 | */ |
||
| 99 | public function menu ( $parent ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Load activated components and init component |
||
| 236 | * |
||
| 237 | * @since 2.0 |
||
| 238 | */ |
||
| 239 | public function load () { |
||
| 240 | do_action( 'pods_components_load' ); |
||
| 241 | |||
| 242 | foreach ( (array) $this->components as $component => $component_data ) { |
||
| 243 | $component_data[ 'MustUse' ] = apply_filters( 'pods_component_require_' . $component_data[ 'ID' ], $component_data[ 'MustUse' ], $component_data ); |
||
| 244 | |||
| 245 | View Code Duplication | if ( false === $component_data[ 'MustUse' ] && ( !isset( $this->settings[ 'components' ][ $component ] ) || 0 == $this->settings[ 'components' ][ $component ] ) ) |
|
| 246 | continue; |
||
| 247 | |||
| 248 | if ( !empty( $component_data[ 'PluginDependency' ] ) ) { |
||
| 249 | $dependency = explode( '|', $component_data[ 'PluginDependency' ] ); |
||
| 250 | |||
| 251 | if ( !pods_is_plugin_active( $dependency[ 1 ] ) ) |
||
| 252 | continue; |
||
| 253 | } |
||
| 254 | |||
| 255 | if ( !empty( $component_data[ 'ThemeDependency' ] ) ) { |
||
| 256 | $dependency = explode( '|', $component_data[ 'ThemeDependency' ] ); |
||
| 257 | |||
| 258 | if ( strtolower( $dependency[ 1 ] ) != strtolower( get_template() ) && strtolower( $dependency[ 1 ] ) != strtolower( get_stylesheet() ) ) |
||
| 259 | continue; |
||
| 260 | } |
||
| 261 | |||
| 262 | View Code Duplication | if ( false === $component_data[ 'External' ] ) |
|
| 263 | $component_data[ 'File' ] = realpath( $this->components_dir . $component_data[ 'File' ] ); |
||
| 264 | |||
| 265 | if ( empty( $component_data[ 'File' ] ) ) { |
||
| 266 | pods_transient_clear( 'pods_components' ); |
||
| 267 | |||
| 268 | continue; |
||
| 269 | } |
||
| 270 | |||
| 271 | View Code Duplication | if ( !file_exists( $component_data[ 'File' ] ) ) { |
|
| 272 | pods_message( 'Pods Component not found: ' . $component_data[ 'File' ] ); |
||
| 273 | |||
| 274 | pods_transient_clear( 'pods_components' ); |
||
| 275 | |||
| 276 | continue; |
||
| 277 | } |
||
| 278 | |||
| 279 | include_once $component_data[ 'File' ]; |
||
| 280 | |||
| 281 | if ( ( !empty( $component_data[ 'Class' ] ) && class_exists( $component_data[ 'Class' ] ) ) || isset( $component_data[ 'object' ] ) ) { |
||
| 282 | if ( !isset( $this->components[ $component ][ 'object' ] ) ) |
||
| 283 | $this->components[ $component ][ 'object' ] = new $component_data[ 'Class' ]; |
||
| 284 | |||
| 285 | if ( method_exists( $this->components[ $component ][ 'object' ], 'options' ) ) { |
||
| 286 | if ( isset( $this->settings[ 'components' ][ $component ] ) ) |
||
| 287 | $this->components[ $component ][ 'options' ] = $this->components[ $component ][ 'object' ]->options( $this->settings[ 'components' ][ $component ] ); |
||
| 288 | else |
||
| 289 | $this->components[ $component ][ 'options' ] = $this->components[ $component ][ 'object' ]->options( array() ); |
||
| 290 | |||
| 291 | $this->options( $component, $this->components[ $component ][ 'options' ] ); |
||
| 292 | } |
||
| 293 | else |
||
| 294 | $this->options( $component, array() ); |
||
| 295 | |||
| 296 | View Code Duplication | if ( method_exists( $this->components[ $component ][ 'object' ], 'handler' ) ) |
|
| 297 | $this->components[ $component ][ 'object' ]->handler( $this->settings[ 'components' ][ $component ] ); |
||
| 298 | } |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get list of components available |
||
| 304 | * |
||
| 305 | * @since 2.0 |
||
| 306 | */ |
||
| 307 | public function get_components () { |
||
| 308 | $components = pods_transient_get( 'pods_components' ); |
||
| 309 | |||
| 310 | View Code Duplication | if ( 1 == pods_var( 'pods_debug_components', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) |
|
| 311 | $components = array(); |
||
| 312 | |||
| 313 | if ( PodsInit::$version != PODS_VERSION || !is_array( $components ) || empty( $components ) || ( is_admin() && isset( $_GET[ 'page' ] ) && 'pods-components' == $_GET[ 'page' ] && 1 !== pods_transient_get( 'pods_components_refresh' ) ) ) { |
||
| 314 | do_action( 'pods_components_get' ); |
||
| 315 | |||
| 316 | $component_dir = @opendir( untrailingslashit( $this->components_dir ) ); |
||
| 317 | $component_files = array(); |
||
| 318 | |||
| 319 | if ( false !== $component_dir ) { |
||
| 320 | while ( false !== ( $file = readdir( $component_dir ) ) ) { |
||
| 321 | if ( '.' == substr( $file, 0, 1 ) ) |
||
| 322 | continue; |
||
| 323 | elseif ( is_dir( $this->components_dir . $file ) ) { |
||
| 324 | $component_subdir = @opendir( $this->components_dir . $file ); |
||
| 325 | |||
| 326 | if ( $component_subdir ) { |
||
| 327 | while ( false !== ( $subfile = readdir( $component_subdir ) ) ) { |
||
| 328 | if ( '.' == substr( $subfile, 0, 1 ) ) |
||
| 329 | continue; |
||
| 330 | elseif ( '.php' == substr( $subfile, -4 ) ) |
||
| 331 | $component_files[] = str_replace( '\\', '/', $file . '/' . $subfile ); |
||
| 332 | } |
||
| 333 | |||
| 334 | closedir( $component_subdir ); |
||
| 335 | } |
||
| 336 | } |
||
| 337 | elseif ( '.php' == substr( $file, -4 ) ) |
||
| 338 | $component_files[] = $file; |
||
| 339 | } |
||
| 340 | |||
| 341 | closedir( $component_dir ); |
||
| 342 | } |
||
| 343 | |||
| 344 | $default_headers = array( |
||
| 345 | 'ID' => 'ID', |
||
| 346 | 'Name' => 'Name', |
||
| 347 | 'ShortName' => 'Short Name', |
||
| 348 | 'PluginName' => 'Plugin Name', |
||
| 349 | 'ComponentName' => 'Component Name', |
||
| 350 | 'URI' => 'URI', |
||
| 351 | 'MenuName' => 'Menu Name', |
||
| 352 | 'MenuPage' => 'Menu Page', |
||
| 353 | 'MenuAddPage' => 'Menu Add Page', |
||
| 354 | 'MustUse' => 'Must Use', |
||
| 355 | 'Description' => 'Description', |
||
| 356 | 'Version' => 'Version', |
||
| 357 | 'Category' => 'Category', |
||
| 358 | 'Author' => 'Author', |
||
| 359 | 'AuthorURI' => 'Author URI', |
||
| 360 | 'Class' => 'Class', |
||
| 361 | 'Hide' => 'Hide', |
||
| 362 | 'PluginDependency' => 'Plugin Dependency', |
||
| 363 | 'ThemeDependency' => 'Theme Dependency', |
||
| 364 | 'DeveloperMode' => 'Developer Mode', |
||
| 365 | 'TablelessMode' => 'Tableless Mode', |
||
| 366 | 'Capability' => 'Capability', |
||
| 367 | 'Plugin' => 'Plugin' |
||
| 368 | ); |
||
| 369 | |||
| 370 | $component_files = apply_filters( 'pods_components_register', $component_files ); |
||
| 371 | |||
| 372 | $components = array(); |
||
| 373 | |||
| 374 | foreach ( $component_files as $component_file ) { |
||
| 375 | $external = false; |
||
| 376 | |||
| 377 | if ( is_array( $component_file ) && isset( $component_file[ 'File' ] ) ) { |
||
| 378 | $component = $component_file = $component_file[ 'File' ]; |
||
| 379 | |||
| 380 | $external = true; |
||
| 381 | } |
||
| 382 | else |
||
| 383 | $component = $this->components_dir . $component_file; |
||
| 384 | |||
| 385 | if ( !is_readable( $component ) ) |
||
| 386 | continue; |
||
| 387 | |||
| 388 | $component_data = get_file_data( $component, $default_headers, 'pods_component' ); |
||
| 389 | |||
| 390 | if ( ( empty( $component_data[ 'Name' ] ) && empty( $component_data[ 'ComponentName' ] ) && empty( $component_data[ 'PluginName' ] ) ) || 'yes' == $component_data[ 'Hide' ] ) |
||
| 391 | continue; |
||
| 392 | |||
| 393 | if ( isset( $component_data[ 'Plugin' ] ) && pods_is_plugin_active( $component_data[ 'Plugin' ] ) ) |
||
| 394 | continue; |
||
| 395 | |||
| 396 | if ( empty( $component_data[ 'Name' ] ) ) { |
||
| 397 | if ( !empty( $component_data[ 'ComponentName' ] ) ) |
||
| 398 | $component_data[ 'Name' ] = $component_data[ 'ComponentName' ]; |
||
| 399 | elseif ( !empty( $component_data[ 'PluginName' ] ) ) |
||
| 400 | $component_data[ 'Name' ] = $component_data[ 'PluginName' ]; |
||
| 401 | } |
||
| 402 | |||
| 403 | if ( empty( $component_data[ 'ShortName' ] ) ) |
||
| 404 | $component_data[ 'ShortName' ] = $component_data[ 'Name' ]; |
||
| 405 | |||
| 406 | if ( empty( $component_data[ 'MenuName' ] ) ) |
||
| 407 | $component_data[ 'MenuName' ] = $component_data[ 'Name' ]; |
||
| 408 | |||
| 409 | if ( empty( $component_data[ 'Class' ] ) ) |
||
| 410 | $component_data[ 'Class' ] = 'Pods_' . pods_js_name( basename( $component, '.php' ), false ); |
||
| 411 | |||
| 412 | if ( empty( $component_data[ 'ID' ] ) ) |
||
| 413 | $component_data[ 'ID' ] = $component_data[ 'Name' ]; |
||
| 414 | |||
| 415 | $component_data[ 'ID' ] = sanitize_title( $component_data[ 'ID' ] ); |
||
| 416 | |||
| 417 | View Code Duplication | if ( 'on' == strtolower( $component_data[ 'DeveloperMode' ] ) || 1 == $component_data[ 'DeveloperMode' ] ) |
|
| 418 | $component_data[ 'DeveloperMode' ] = true; |
||
| 419 | else |
||
| 420 | $component_data[ 'DeveloperMode' ] = false; |
||
| 421 | |||
| 422 | View Code Duplication | if ( 'on' == strtolower( $component_data[ 'TablelessMode' ] ) || 1 == $component_data[ 'TablelessMode' ] ) |
|
| 423 | $component_data[ 'TablelessMode' ] = true; |
||
| 424 | else |
||
| 425 | $component_data[ 'TablelessMode' ] = false; |
||
| 426 | |||
| 427 | $component_data[ 'External' ] = (boolean) $external; |
||
| 428 | |||
| 429 | if ( 'on' == strtolower($component_data[ 'MustUse' ] ) || '1' == $component_data[ 'MustUse' ] ) |
||
| 430 | $component_data[ 'MustUse' ] = true; |
||
| 431 | elseif ( 'off' == strtolower($component_data[ 'MustUse' ] ) || '0' == $component_data[ 'MustUse' ] ) |
||
| 432 | $component_data[ 'MustUse' ] = false; |
||
| 433 | else |
||
| 434 | $component_data[ 'MustUse' ] = $component_data[ 'External' ]; |
||
| 435 | |||
| 436 | $component_data[ 'File' ] = $component_file; |
||
| 437 | |||
| 438 | $components[ $component_data[ 'ID' ] ] = $component_data; |
||
| 439 | } |
||
| 440 | |||
| 441 | ksort( $components ); |
||
| 442 | |||
| 443 | pods_transient_set( 'pods_components_refresh', 1, ( 60 * 60 * 12 ) ); |
||
| 444 | |||
| 445 | pods_transient_set( 'pods_components', $components ); |
||
| 446 | } |
||
| 447 | |||
| 448 | View Code Duplication | if ( 1 == pods_var( 'pods_debug_components', 'get', 0 ) && pods_is_admin( array( 'pods' ) ) ) |
|
| 449 | pods_debug( $components ); |
||
| 450 | |||
| 451 | $this->components = $components; |
||
| 452 | |||
| 453 | return $this->components; |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Set component options |
||
| 458 | * |
||
| 459 | * @param $component |
||
| 460 | * @param $options |
||
| 461 | * |
||
| 462 | * @since 2.0 |
||
| 463 | */ |
||
| 464 | public function options ( $component, $options ) { |
||
| 465 | if ( !isset( $this->settings[ 'components' ][ $component ] ) || !is_array( $this->settings[ 'components' ][ $component ] ) ) |
||
| 466 | $this->settings[ 'components' ][ $component ] = array(); |
||
| 467 | |||
| 468 | foreach ( $options as $option => $data ) { |
||
| 469 | if ( !isset( $this->settings[ 'components' ][ $component ][ $option ] ) && isset( $data[ 'default' ] ) ) |
||
| 470 | $this->settings[ 'components' ][ $component ][ $option ] = $data[ 'default' ]; |
||
| 471 | } |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Call component specific admin functions |
||
| 476 | * |
||
| 477 | * @since 2.0 |
||
| 478 | */ |
||
| 479 | public function admin_handler () { |
||
| 480 | $component = str_replace( 'pods-component-', '', $_GET[ 'page' ] ); |
||
| 481 | |||
| 482 | if ( isset( $this->components[ $component ] ) && isset( $this->components[ $component ][ 'object' ] ) && is_object( $this->components[ $component ][ 'object' ] ) ) { |
||
| 483 | // Component init |
||
| 484 | View Code Duplication | if ( method_exists( $this->components[ $component ][ 'object' ], 'init' ) ) |
|
| 485 | $this->components[ $component ][ 'object' ]->init( $this->settings[ 'components' ][ $component ], $component ); |
||
| 486 | |||
| 487 | // Component Admin handler |
||
| 488 | if ( method_exists( $this->components[ $component ][ 'object' ], 'admin' ) ) |
||
| 489 | $this->components[ $component ][ 'object' ]->admin( $this->settings[ 'components' ][ $component ], $component ); |
||
| 490 | // Built-in Admin Handler |
||
| 491 | View Code Duplication | elseif ( method_exists( $this->components[ $component ][ 'object' ], 'options' ) ) |
|
| 492 | $this->admin( $this->components[ $component ][ 'object' ]->options( $this->settings[ 'components' ][ $component ] ), $this->settings[ 'components' ][ $component ], $component ); |
||
| 493 | } |
||
| 494 | } |
||
| 495 | |||
| 496 | public function admin ( $options, $settings, $component ) { |
||
| 497 | if ( !isset( $this->components[ $component ] ) ) |
||
| 498 | wp_die( 'Invalid Component' ); |
||
| 499 | |||
| 500 | $component_label = $this->components[ $component ][ 'Name' ]; |
||
| 501 | |||
| 502 | include PODS_DIR . 'ui/admin/components-admin.php'; |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Check if a component is active or not |
||
| 507 | * |
||
| 508 | * @param string $component The component name to check if active |
||
| 509 | * |
||
| 510 | * @return bool |
||
| 511 | * |
||
| 512 | * @since 2.7 |
||
| 513 | */ |
||
| 514 | public function is_component_active( $component ) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Activate a component |
||
| 528 | * |
||
| 529 | * @param string $component The component name to activate |
||
| 530 | * |
||
| 531 | * @since 2.7 |
||
| 532 | */ |
||
| 533 | View Code Duplication | public function activate_component( $component ) { |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Deactivate a component |
||
| 549 | * |
||
| 550 | * @param string $component The component name to deactivate |
||
| 551 | * |
||
| 552 | * @since 2.7 |
||
| 553 | */ |
||
| 554 | View Code Duplication | public function deactivate_component( $component ) { |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Toggle a component on or off |
||
| 570 | * |
||
| 571 | * @param string $component The component name to toggle |
||
| 572 | * |
||
| 573 | * @return bool |
||
| 574 | * |
||
| 575 | * @since 2.0 |
||
| 576 | */ |
||
| 577 | public function toggle( $component ) { |
||
| 578 | |||
| 579 | $toggle = null; |
||
| 580 | |||
| 581 | $toggle_mode = (int) pods_v( 'toggle', 'get' ); |
||
| 582 | |||
| 583 | if ( 1 == $toggle_mode ) { |
||
| 584 | $this->activate_component( $component ); |
||
| 585 | $toggle = true; |
||
| 586 | } |
||
| 587 | else { |
||
| 588 | $this->deactivate_component( $component ); |
||
| 589 | $toggle = false; |
||
| 590 | } |
||
| 591 | |||
| 592 | return $toggle; |
||
| 593 | |||
| 594 | } |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Add pods specific capabilities. |
||
| 598 | * |
||
| 599 | * @param $capabilities List of extra capabilities to add |
||
| 600 | * |
||
| 601 | * @return array |
||
| 602 | */ |
||
| 603 | public function admin_capabilities ( $capabilities ) { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Handle admin ajax |
||
| 631 | * |
||
| 632 | * @since 2.0 |
||
| 633 | */ |
||
| 634 | public function admin_ajax () { |
||
| 693 | |||
| 694 | public function admin_ajax_settings ( $component, $params ) { |
||
| 728 | } |
||
| 729 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.