| Total Complexity | 63 |
| Total Lines | 504 |
| Duplicated Lines | 0 % |
| Changes | 18 | ||
| Bugs | 2 | Features | 2 |
Complex classes like WPInv_Plugin 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.
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 WPInv_Plugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class WPInv_Plugin { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * GetPaid version. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | public $version; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Data container. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $data = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Form elements instance. |
||
| 33 | * |
||
| 34 | * @var WPInv_Payment_Form_Elements |
||
| 35 | */ |
||
| 36 | public $form_elements; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Tax instance. |
||
| 40 | * |
||
| 41 | * @var WPInv_EUVat |
||
| 42 | */ |
||
| 43 | public $tax; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param array An array of payment gateways. |
||
| 47 | */ |
||
| 48 | public $gateways; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Class constructor. |
||
| 52 | */ |
||
| 53 | public function __construct() { |
||
| 54 | $this->define_constants(); |
||
| 55 | $this->includes(); |
||
| 56 | $this->init_hooks(); |
||
| 57 | $this->set_properties(); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Sets a custom data property. |
||
| 62 | * |
||
| 63 | * @param string $prop The prop to set. |
||
| 64 | * @param mixed $value The value to retrieve. |
||
| 65 | */ |
||
| 66 | public function set( $prop, $value ) { |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Gets a custom data property. |
||
| 72 | * |
||
| 73 | * @param string $prop The prop to set. |
||
| 74 | * @return mixed The value. |
||
| 75 | */ |
||
| 76 | public function get( $prop ) { |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Define class properties. |
||
| 87 | */ |
||
| 88 | public function set_properties() { |
||
| 89 | |||
| 90 | // Sessions. |
||
| 91 | $this->set( 'session', new WPInv_Session_Handler() ); |
||
| 92 | $GLOBALS['wpi_session'] = $this->get( 'session' ); // Backwards compatibility. |
||
| 93 | $this->form_elements = new WPInv_Payment_Form_Elements(); |
||
| 94 | $this->tax = new WPInv_EUVat(); |
||
| 95 | $this->tax->init(); |
||
| 96 | $GLOBALS['wpinv_euvat'] = $this->tax; // Backwards compatibility. |
||
| 97 | |||
| 98 | // Init other objects. |
||
| 99 | $this->set( 'reports', new WPInv_Reports() ); // TODO: Refactor. |
||
| 100 | $this->set( 'session', new WPInv_Session_Handler() ); |
||
| 101 | $this->set( 'notes', new WPInv_Notes() ); |
||
| 102 | $this->set( 'api', new WPInv_API() ); |
||
| 103 | $this->set( 'post_types', new GetPaid_Post_Types() ); |
||
| 104 | $this->set( 'template', new GetPaid_Template() ); |
||
| 105 | $this->set( 'admin', new GetPaid_Admin() ); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Define plugin constants. |
||
| 110 | */ |
||
| 111 | public function define_constants() { |
||
| 112 | define( 'WPINV_PLUGIN_DIR', plugin_dir_path( WPINV_PLUGIN_FILE ) ); |
||
| 113 | define( 'WPINV_PLUGIN_URL', plugin_dir_url( WPINV_PLUGIN_FILE ) ); |
||
| 114 | $this->version = WPINV_VERSION; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Hook into actions and filters. |
||
| 119 | * |
||
| 120 | * @since 1.0.19 |
||
| 121 | */ |
||
| 122 | protected function init_hooks() { |
||
| 123 | /* Internationalize the text strings used. */ |
||
| 124 | add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) ); |
||
| 125 | |||
| 126 | // Init the plugin after WordPress inits. |
||
| 127 | add_action( 'init', array( $this, 'init' ), 1 ); |
||
| 128 | add_action( 'getpaid_init', array( $this, 'maybe_process_ipn' ), 5 ); |
||
| 129 | add_action( 'init', array( &$this, 'wpinv_actions' ) ); |
||
| 130 | |||
| 131 | if ( class_exists( 'BuddyPress' ) ) { |
||
| 132 | add_action( 'bp_include', array( &$this, 'bp_invoicing_init' ) ); |
||
| 133 | } |
||
| 134 | |||
| 135 | add_action( 'wp_enqueue_scripts', array( &$this, 'enqueue_scripts' ) ); |
||
| 136 | add_action( 'wp_footer', array( &$this, 'wp_footer' ) ); |
||
| 137 | add_action( 'widgets_init', array( &$this, 'register_widgets' ) ); |
||
| 138 | add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', array( $this, 'wpseo_exclude_from_sitemap_by_post_ids' ) ); |
||
| 139 | add_filter( 'pre_get_posts', array( &$this, 'pre_get_posts' ) ); |
||
| 140 | |||
| 141 | // Fires after registering actions. |
||
| 142 | do_action( 'wpinv_actions', $this ); |
||
| 143 | do_action( 'getpaid_actions', $this ); |
||
| 144 | |||
| 145 | } |
||
| 146 | |||
| 147 | public function plugins_loaded() { |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Load the translation of the plugin. |
||
| 161 | * |
||
| 162 | * @since 1.0 |
||
| 163 | */ |
||
| 164 | public function load_textdomain( $locale = NULL ) { |
||
| 165 | if ( empty( $locale ) ) { |
||
| 166 | $locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale(); |
||
| 167 | } |
||
| 168 | |||
| 169 | $locale = apply_filters( 'plugin_locale', $locale, 'invoicing' ); |
||
|
|
|||
| 170 | |||
| 171 | unload_textdomain( 'invoicing' ); |
||
| 172 | load_textdomain( 'invoicing', WP_LANG_DIR . '/invoicing/invoicing-' . $locale . '.mo' ); |
||
| 173 | load_plugin_textdomain( 'invoicing', false, WPINV_PLUGIN_DIR . 'languages' ); |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Define language constants. |
||
| 177 | */ |
||
| 178 | require_once( WPINV_PLUGIN_DIR . 'language.php' ); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Include required core files used in admin and on the frontend. |
||
| 183 | */ |
||
| 184 | public function includes() { |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Class autoloader |
||
| 299 | * |
||
| 300 | * @param string $class_name The name of the class to load. |
||
| 301 | * @access public |
||
| 302 | * @since 1.0.19 |
||
| 303 | * @return void |
||
| 304 | */ |
||
| 305 | public function autoload( $class_name ) { |
||
| 306 | |||
| 307 | // Normalize the class name... |
||
| 308 | $class_name = strtolower( $class_name ); |
||
| 309 | |||
| 310 | // ... and make sure it is our class. |
||
| 311 | if ( false === strpos( $class_name, 'getpaid_' ) && false === strpos( $class_name, 'wpinv_' ) ) { |
||
| 312 | return; |
||
| 313 | } |
||
| 314 | |||
| 315 | // Next, prepare the file name from the class. |
||
| 316 | $file_name = 'class-' . str_replace( '_', '-', $class_name ) . '.php'; |
||
| 317 | |||
| 318 | // Base path of the classes. |
||
| 319 | $plugin_path = untrailingslashit( WPINV_PLUGIN_DIR ); |
||
| 320 | |||
| 321 | // And an array of possible locations in order of importance. |
||
| 322 | $locations = array( |
||
| 323 | "$plugin_path/includes", |
||
| 324 | "$plugin_path/includes/data-stores", |
||
| 325 | "$plugin_path/includes/gateways", |
||
| 326 | "$plugin_path/includes/api", |
||
| 327 | "$plugin_path/includes/admin", |
||
| 328 | "$plugin_path/includes/admin/meta-boxes", |
||
| 329 | ); |
||
| 330 | |||
| 331 | foreach ( apply_filters( 'getpaid_autoload_locations', $locations ) as $location ) { |
||
| 332 | |||
| 333 | if ( file_exists( trailingslashit( $location ) . $file_name ) ) { |
||
| 334 | include trailingslashit( $location ) . $file_name; |
||
| 335 | break; |
||
| 336 | } |
||
| 337 | |||
| 338 | } |
||
| 339 | |||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Inits hooks etc. |
||
| 344 | */ |
||
| 345 | public function init() { |
||
| 368 | |||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Checks if this is an IPN request and processes it. |
||
| 373 | */ |
||
| 374 | public function maybe_process_ipn() { |
||
| 375 | |||
| 376 | // Ensure that this is an IPN request. |
||
| 377 | if ( empty( $_GET['wpi-listener'] ) || 'IPN' !== $_GET['wpi-listener'] || empty( $_GET['wpi-gateway'] ) ) { |
||
| 378 | return; |
||
| 379 | } |
||
| 380 | |||
| 381 | $gateway = wpinv_clean( $_GET['wpi-gateway'] ); |
||
| 382 | |||
| 383 | do_action( 'wpinv_verify_payment_ipn', $gateway ); |
||
| 384 | do_action( "wpinv_verify_{$gateway}_ipn" ); |
||
| 385 | exit; |
||
| 386 | |||
| 387 | } |
||
| 388 | |||
| 389 | public function enqueue_scripts() { |
||
| 390 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
||
| 391 | |||
| 392 | $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/invoice-front.css' ); |
||
| 393 | wp_register_style( 'wpinv_front_style', WPINV_PLUGIN_URL . 'assets/css/invoice-front.css', array(), $version ); |
||
| 394 | wp_enqueue_style( 'wpinv_front_style' ); |
||
| 395 | |||
| 396 | // Register scripts |
||
| 397 | wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '2.70', true ); |
||
| 398 | wp_register_script( 'wpinv-front-script', WPINV_PLUGIN_URL . 'assets/js/invoice-front.js', array( 'jquery' ), filemtime( WPINV_PLUGIN_DIR . 'assets/js/invoice-front.js' ) ); |
||
| 399 | |||
| 400 | $localize = array(); |
||
| 401 | $localize['ajax_url'] = admin_url( 'admin-ajax.php' ); |
||
| 402 | $localize['nonce'] = wp_create_nonce( 'wpinv-nonce' ); |
||
| 403 | $localize['currency_symbol'] = wpinv_currency_symbol(); |
||
| 404 | $localize['currency_pos'] = wpinv_currency_position(); |
||
| 405 | $localize['thousand_sep'] = wpinv_thousands_separator(); |
||
| 406 | $localize['decimal_sep'] = wpinv_decimal_separator(); |
||
| 407 | $localize['decimals'] = wpinv_decimals(); |
||
| 408 | $localize['txtComplete'] = __( 'Continue', 'invoicing' ); |
||
| 409 | $localize['UseTaxes'] = wpinv_use_taxes(); |
||
| 410 | $localize['checkoutNonce'] = wp_create_nonce( 'wpinv_checkout_nonce' ); |
||
| 411 | $localize['formNonce'] = wp_create_nonce( 'getpaid_form_nonce' ); |
||
| 412 | $localize['connectionError'] = __( 'Could not establish a connection to the server.', 'invoicing' ); |
||
| 413 | |||
| 414 | $localize = apply_filters( 'wpinv_front_js_localize', $localize ); |
||
| 415 | |||
| 416 | wp_enqueue_script( 'jquery-blockui' ); |
||
| 417 | $autofill_api = wpinv_get_option('address_autofill_api'); |
||
| 418 | $autofill_active = wpinv_get_option('address_autofill_active'); |
||
| 419 | if ( isset( $autofill_active ) && 1 == $autofill_active && !empty( $autofill_api ) && wpinv_is_checkout() ) { |
||
| 420 | if ( wp_script_is( 'google-maps-api', 'enqueued' ) ) { |
||
| 421 | wp_dequeue_script( 'google-maps-api' ); |
||
| 422 | } |
||
| 423 | wp_enqueue_script( 'google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . $autofill_api . '&libraries=places', array( 'jquery' ), '', false ); |
||
| 424 | wp_enqueue_script( 'google-maps-init', WPINV_PLUGIN_URL . 'assets/js/gaaf.js', array( 'jquery', 'google-maps-api' ), '', true ); |
||
| 425 | } |
||
| 426 | |||
| 427 | wp_enqueue_style( "select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all' ); |
||
| 428 | wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION ); |
||
| 429 | |||
| 430 | wp_enqueue_script( 'wpinv-front-script' ); |
||
| 431 | wp_localize_script( 'wpinv-front-script', 'WPInv', $localize ); |
||
| 432 | |||
| 433 | $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/payment-forms.js' ); |
||
| 434 | wp_enqueue_script( 'wpinv-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/payment-forms.js', array( 'wpinv-front-script', 'wp-hooks' ), $version, true ); |
||
| 435 | } |
||
| 436 | |||
| 437 | public function wpinv_actions() { |
||
| 440 | } |
||
| 441 | } |
||
| 442 | |||
| 443 | public function pre_get_posts( $wp_query ) { |
||
| 444 | if ( ! is_admin() && !empty( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] == 'wpi_invoice' && is_user_logged_in() && is_single() && $wp_query->is_main_query() ) { |
||
| 445 | $wp_query->query_vars['post_status'] = array_keys( wpinv_get_invoice_statuses() ); |
||
| 446 | } |
||
| 447 | |||
| 448 | return $wp_query; |
||
| 449 | } |
||
| 450 | |||
| 451 | public function bp_invoicing_init() { |
||
| 452 | require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-bp-core.php' ); |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Register widgets |
||
| 457 | * |
||
| 458 | */ |
||
| 459 | public function register_widgets() { |
||
| 475 | } |
||
| 476 | |||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Remove our pages from yoast sitemaps. |
||
| 481 | * |
||
| 482 | * @since 1.0.19 |
||
| 483 | * @param int[] $excluded_posts_ids |
||
| 484 | */ |
||
| 485 | public function wpseo_exclude_from_sitemap_by_post_ids( $excluded_posts_ids ){ |
||
| 514 | |||
| 515 | } |
||
| 516 | |||
| 517 | public function wp_footer() { |
||
| 518 | echo ' |
||
| 519 | <div class="bsui"> |
||
| 520 | <div id="getpaid-payment-modal" class="modal" tabindex="-1" role="dialog"> |
||
| 521 | <div class="modal-dialog modal-dialog-centered modal-lg" role="checkout" style="max-width: 650px;"> |
||
| 522 | <div class="modal-content"> |
||
| 532 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.