Passed
Push — master ( 6621fa...568391 )
by Brian
06:12
created

GetPaid_Metaboxes::add_meta_boxes()   C

Complexity

Conditions 13
Paths 204

Size

Total Lines 72
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 72
rs 5.7333
cc 13
nc 204
nop 2

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Metaboxes Admin.
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * Metaboxes Admin Class
11
 *
12
 */
13
class GetPaid_Metaboxes {
14
15
	/**
16
	 * Only save metaboxes once.
17
	 *
18
	 * @var boolean
19
	 */
20
	private static $saved_meta_boxes = false;
21
22
    /**
23
	 * Hook in methods.
24
	 */
25
	public static function init() {
26
27
		// Register metaboxes.
28
		add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::add_meta_boxes', 5, 2 );
29
30
		// Remove metaboxes.
31
		add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::remove_meta_boxes', 30 );
32
33
		// Rename metaboxes.
34
		add_action( 'add_meta_boxes', 'GetPaid_Metaboxes::rename_meta_boxes', 45 );
35
36
		// Save metaboxes.
37
		add_action( 'save_post', 'GetPaid_Metaboxes::save_meta_boxes', 1, 2 );
38
	}
39
40
	/**
41
	 * Register core metaboxes.
42
	 */
43
	public static function add_meta_boxes( $post_type, $post ) {
44
		global $wpinv_euvat;
45
46
		// For invoices...
47
		if ( $post_type == 'wpi_invoice' ) {
48
			$invoice = new WPInv_Invoice( $post );
49
50
			// Resend invoice.
51
			if ( ! $invoice->is_draft() && ! $invoice->is_paid() ) {
52
				add_meta_box( 'wpinv-mb-resend-invoice', __( 'Resend Invoice', 'invoicing' ), 'GetPaid_Meta_Box_Resend_Invoice::output', 'wpi_invoice', 'side', 'low' );
53
			}
54
55
			// Subscriptions.
56
			$subscription = getpaid_get_invoice_subscription( $invoice );
57
			if ( ! empty( $subscription ) ) {
58
				add_meta_box( 'wpinv-mb-subscriptions', __( 'Subscription Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output', 'wpi_invoice', 'advanced' );
59
				add_meta_box( 'wpinv-mb-subscription-invoices', __( 'Related Payments', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output_invoices', 'wpi_invoice', 'advanced' );
60
			}
61
62
			// Invoice details.
63
			add_meta_box( 'wpinv-details', __( 'Invoice Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Details::output', 'wpi_invoice', 'side', 'default' );
64
			
65
			// Payment details.
66
			if ( ! $invoice->is_draft() ) {
67
				add_meta_box( 'wpinv-payment-meta', __( 'Payment Meta', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Payment_Meta::output', 'wpi_invoice', 'side', 'default' );
68
			}
69
70
			// Billing details.
71
			add_meta_box( 'wpinv-address', __( 'Billing Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Address::output', 'wpi_invoice', 'normal', 'high' );
72
			
73
			// Invoice items.
74
			add_meta_box( 'wpinv-items', __( 'Invoice Items', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Items::output', 'wpi_invoice', 'normal', 'high' );
75
			
76
			// Invoice notes.
77
			add_meta_box( 'wpinv-notes', __( 'Invoice Notes', 'invoicing' ), 'WPInv_Meta_Box_Notes::output', 'wpi_invoice', 'side', 'low' );
78
79
			// Payment form information.
80
			if ( ! empty( $post->ID ) && get_post_meta( $post->ID, 'payment_form_data', true ) ) {
81
				add_meta_box( 'wpinv-invoice-payment-form-details', __( 'Payment Form Details', 'invoicing' ), 'WPInv_Meta_Box_Payment_Form::output_details', 'wpi_invoice', 'side', 'high' );
82
			}
83
		}
84
85
		// For payment forms.
86
		if ( $post_type == 'wpi_payment_form' ) {
87
88
			// Design payment form.
89
			add_meta_box( 'wpinv-payment-form-design', __( 'Payment Form', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form::output', 'wpi_payment_form', 'normal' );
90
91
			// Payment form information.
92
			add_meta_box( 'wpinv-payment-form-info', __( 'Details', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form_Info::output', 'wpi_payment_form', 'side' );
93
94
		}
95
96
		// For invoice items.
97
		if ( $post_type == 'wpi_item' ) {
98
99
			// Item details.
100
			add_meta_box( 'wpinv_item_details', __( 'Item Details', 'invoicing' ), 'GetPaid_Meta_Box_Item_Details::output', 'wpi_item', 'normal', 'high' );
101
102
			// If taxes are enabled, register the tax metabox.
103
			if ( $wpinv_euvat->allow_vat_rules() || $wpinv_euvat->allow_vat_classes() ) {
104
				add_meta_box( 'wpinv_item_vat', __( 'VAT / Tax', 'invoicing' ), 'GetPaid_Meta_Box_Item_VAT::output', 'wpi_item', 'normal', 'high' );
105
			}
106
107
			// Item info.
108
			add_meta_box( 'wpinv_field_item_info', __( 'Item info', 'invoicing' ), 'GetPaid_Meta_Box_Item_Info::output', 'wpi_item', 'side', 'core' );
109
110
		}
111
112
		// For invoice discounts.
113
		if ( $post_type == 'wpi_discount' ) {
114
			add_meta_box( 'wpinv_discount_details', __( 'Discount Details', 'invoicing' ), 'GetPaid_Meta_Box_Discount_Details::output', 'wpi_discount', 'normal', 'high' );
115
		}
116
		
117
118
	}
119
120
	/**
121
	 * Remove some metaboxes.
122
	 */
123
	public static function remove_meta_boxes() {
124
		remove_meta_box( 'wpseo_meta', 'wpi_invoice', 'normal' );
125
	}
126
127
	/**
128
	 * Rename other metaboxes.
129
	 */
130
	public static function rename_meta_boxes() {
131
		
132
	}
133
134
	/**
135
	 * Check if we're saving, then trigger an action based on the post type.
136
	 *
137
	 * @param  int    $post_id Post ID.
138
	 * @param  object $post Post object.
139
	 */
140
	public static function save_meta_boxes( $post_id, $post ) {
141
		$post_id = absint( $post_id );
142
		$data    = wp_unslash( $_POST );
143
144
		// Do not save for ajax requests.
145
		if ( ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) {
146
			return;
147
		}
148
149
		// $post_id and $post are required
150
		if ( empty( $post_id ) || empty( $post ) || self::$saved_meta_boxes ) {
151
			return;
152
		}
153
154
		// Dont' save meta boxes for revisions or autosaves.
155
		if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
156
			return;
157
		}
158
159
		// Check the nonce.
160
		if ( empty( $data['getpaid_meta_nonce'] ) || ! wp_verify_nonce( $data['getpaid_meta_nonce'], 'getpaid_meta_nonce' ) ) {
161
			return;
162
		}
163
164
		// Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
165
		if ( empty( $data['post_ID'] ) || absint( $data['post_ID'] ) !== $post_id ) {
166
			return;
167
		}
168
169
		// Check user has permission to edit.
170
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
171
			return;
172
		}
173
174
		// Ensure this is our post type.
175
		$post_types_map = array(
176
			'wpi_invoice'      => 'GetPaid_Meta_Box_Invoice_Address',
177
			'wpi_quote'        => 'GetPaid_Meta_Box_Invoice_Address',
178
			'wpi_item'         => 'GetPaid_Meta_Box_Item_Details',
179
			'wpi_payment_form' => 'GetPaid_Meta_Box_Payment_Form',
180
			'wpi_discount'     => 'GetPaid_Meta_Box_Discount_Details',
181
		);
182
183
		// Is this our post type?
184
		if ( empty( $post->post_type ) || ! isset( $post_types_map[ $post->post_type ] ) ) {
185
			return;
186
		}
187
188
		// We need this save event to run once to avoid potential endless loops.
189
		self::$saved_meta_boxes = true;
190
		
191
		// Save the post.
192
		$class = $post_types_map[ $post->post_type ];
193
		$class::save( $post_id, $_POST, $post );
194
195
	}
196
197
}
198