Total Complexity | 57 |
Total Lines | 488 |
Duplicated Lines | 0 % |
Changes | 13 | ||
Bugs | 0 | Features | 0 |
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->tax = new WPInv_EUVat(); |
||
94 | $this->tax->init(); |
||
95 | $GLOBALS['wpinv_euvat'] = $this->tax; // Backwards compatibility. |
||
96 | |||
97 | // Init other objects. |
||
98 | $this->set( 'reports', new WPInv_Reports() ); // TODO: Refactor. |
||
99 | $this->set( 'session', new WPInv_Session_Handler() ); |
||
100 | $this->set( 'notes', new WPInv_Notes() ); |
||
101 | $this->set( 'api', new WPInv_API() ); |
||
102 | $this->set( 'post_types', new GetPaid_Post_Types() ); |
||
103 | $this->set( 'template', new GetPaid_Template() ); |
||
104 | $this->set( 'admin', new GetPaid_Admin() ); |
||
105 | $this->set( 'subscriptions', new WPInv_Subscriptions() ); |
||
106 | $this->set( 'invoice_emails', new GetPaid_Invoice_Notification_Emails() ); |
||
107 | $this->set( 'subscription_emails', new GetPaid_Subscription_Notification_Emails() ); |
||
108 | $this->set( 'daily_maintenace', new GetPaid_Daily_Maintenance() ); |
||
109 | $this->set( 'payment_forms', new GetPaid_Payment_Forms() ); |
||
110 | |||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Define plugin constants. |
||
115 | */ |
||
116 | public function define_constants() { |
||
117 | define( 'WPINV_PLUGIN_DIR', plugin_dir_path( WPINV_PLUGIN_FILE ) ); |
||
118 | define( 'WPINV_PLUGIN_URL', plugin_dir_url( WPINV_PLUGIN_FILE ) ); |
||
119 | $this->version = WPINV_VERSION; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Hook into actions and filters. |
||
124 | * |
||
125 | * @since 1.0.19 |
||
126 | */ |
||
127 | protected function init_hooks() { |
||
150 | |||
151 | } |
||
152 | |||
153 | public function plugins_loaded() { |
||
162 | } |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Load the translation of the plugin. |
||
167 | * |
||
168 | * @since 1.0 |
||
169 | */ |
||
170 | public function load_textdomain( $locale = NULL ) { |
||
171 | if ( empty( $locale ) ) { |
||
172 | $locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale(); |
||
173 | } |
||
174 | |||
175 | $locale = apply_filters( 'plugin_locale', $locale, 'invoicing' ); |
||
176 | |||
177 | unload_textdomain( 'invoicing' ); |
||
178 | load_textdomain( 'invoicing', WP_LANG_DIR . '/invoicing/invoicing-' . $locale . '.mo' ); |
||
179 | load_plugin_textdomain( 'invoicing', false, WPINV_PLUGIN_DIR . 'languages' ); |
||
180 | |||
181 | /** |
||
182 | * Define language constants. |
||
183 | */ |
||
184 | require_once( WPINV_PLUGIN_DIR . 'language.php' ); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Include required core files used in admin and on the frontend. |
||
189 | */ |
||
190 | public function includes() { |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Class autoloader |
||
281 | * |
||
282 | * @param string $class_name The name of the class to load. |
||
283 | * @access public |
||
284 | * @since 1.0.19 |
||
285 | * @return void |
||
286 | */ |
||
287 | public function autoload( $class_name ) { |
||
288 | |||
289 | // Normalize the class name... |
||
290 | $class_name = strtolower( $class_name ); |
||
291 | |||
292 | // ... and make sure it is our class. |
||
293 | if ( false === strpos( $class_name, 'getpaid_' ) && false === strpos( $class_name, 'wpinv_' ) ) { |
||
294 | return; |
||
295 | } |
||
296 | |||
297 | // Next, prepare the file name from the class. |
||
298 | $file_name = 'class-' . str_replace( '_', '-', $class_name ) . '.php'; |
||
299 | |||
300 | // Base path of the classes. |
||
301 | $plugin_path = untrailingslashit( WPINV_PLUGIN_DIR ); |
||
302 | |||
303 | // And an array of possible locations in order of importance. |
||
304 | $locations = array( |
||
305 | "$plugin_path/includes", |
||
306 | "$plugin_path/includes/data-stores", |
||
307 | "$plugin_path/includes/gateways", |
||
308 | "$plugin_path/includes/payments", |
||
309 | "$plugin_path/includes/api", |
||
310 | "$plugin_path/includes/admin", |
||
311 | "$plugin_path/includes/admin/meta-boxes", |
||
312 | ); |
||
313 | |||
314 | foreach ( apply_filters( 'getpaid_autoload_locations', $locations ) as $location ) { |
||
315 | |||
316 | if ( file_exists( trailingslashit( $location ) . $file_name ) ) { |
||
317 | include trailingslashit( $location ) . $file_name; |
||
318 | break; |
||
319 | } |
||
320 | |||
321 | } |
||
322 | |||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Inits hooks etc. |
||
327 | */ |
||
328 | public function init() { |
||
351 | |||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Checks if this is an IPN request and processes it. |
||
356 | */ |
||
357 | public function maybe_process_ipn() { |
||
369 | |||
370 | } |
||
371 | |||
372 | public function enqueue_scripts() { |
||
373 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
||
374 | |||
375 | $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/invoice-front.css' ); |
||
376 | wp_register_style( 'wpinv_front_style', WPINV_PLUGIN_URL . 'assets/css/invoice-front.css', array(), $version ); |
||
377 | wp_enqueue_style( 'wpinv_front_style' ); |
||
378 | |||
379 | // Register scripts |
||
380 | wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '2.70', true ); |
||
381 | 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' ) ); |
||
382 | |||
383 | $localize = array(); |
||
384 | $localize['ajax_url'] = admin_url( 'admin-ajax.php' ); |
||
385 | $localize['nonce'] = wp_create_nonce( 'wpinv-nonce' ); |
||
386 | $localize['txtComplete'] = __( 'Continue', 'invoicing' ); |
||
387 | $localize['UseTaxes'] = wpinv_use_taxes(); |
||
388 | $localize['checkoutNonce'] = wp_create_nonce( 'wpinv_checkout_nonce' ); |
||
389 | $localize['formNonce'] = wp_create_nonce( 'getpaid_form_nonce' ); |
||
390 | $localize['connectionError'] = __( 'Could not establish a connection to the server.', 'invoicing' ); |
||
391 | |||
392 | $localize = apply_filters( 'wpinv_front_js_localize', $localize ); |
||
393 | |||
394 | wp_enqueue_script( 'jquery-blockui' ); |
||
395 | |||
396 | wp_enqueue_style( "select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.min.css', array(), WPINV_VERSION, 'all' ); |
||
397 | wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION ); |
||
398 | |||
399 | wp_enqueue_script( 'wpinv-front-script' ); |
||
400 | wp_localize_script( 'wpinv-front-script', 'WPInv', $localize ); |
||
401 | |||
402 | $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/payment-forms.js' ); |
||
403 | wp_enqueue_script( 'wpinv-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/payment-forms.js', array( 'wpinv-front-script', 'wp-hooks' ), $version, true ); |
||
404 | } |
||
405 | |||
406 | public function wpinv_actions() { |
||
407 | if ( isset( $_REQUEST['wpi_action'] ) ) { |
||
408 | do_action( 'wpinv_' . wpinv_sanitize_key( $_REQUEST['wpi_action'] ), $_REQUEST ); |
||
409 | } |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Fires an action after verifying that a user can fire them. |
||
414 | * |
||
415 | * Note: If the action is on an invoice, subscription etc, esure that the |
||
416 | * current user owns the invoice/subscription. |
||
417 | */ |
||
418 | public function maybe_do_authenticated_action() { |
||
423 | } |
||
424 | |||
425 | } |
||
426 | |||
427 | public function pre_get_posts( $wp_query ) { |
||
428 | 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() ) { |
||
429 | $wp_query->query_vars['post_status'] = array_keys( wpinv_get_invoice_statuses() ); |
||
430 | } |
||
431 | |||
432 | return $wp_query; |
||
433 | } |
||
434 | |||
435 | public function bp_invoicing_init() { |
||
436 | require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-bp-core.php' ); |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Register widgets |
||
441 | * |
||
442 | */ |
||
443 | public function register_widgets() { |
||
459 | } |
||
460 | |||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Remove our pages from yoast sitemaps. |
||
465 | * |
||
466 | * @since 1.0.19 |
||
467 | * @param int[] $excluded_posts_ids |
||
468 | */ |
||
469 | public function wpseo_exclude_from_sitemap_by_post_ids( $excluded_posts_ids ){ |
||
498 | |||
499 | } |
||
500 | |||
501 | public function wp_footer() { |
||
503 | <div class="bsui"> |
||
504 | <div id="getpaid-payment-modal" class="modal" tabindex="-1" role="dialog"> |
||
516 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths