Passed
Pull Request — master (#444)
by Brian
05:33
created

GetPaid_Metaboxes::add_meta_boxes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 2
nc 2
nop 2
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
45
		// For invoices...
46
		self::add_invoice_meta_boxes( $post_type, $post );
47
48
		// For payment forms.
49
		self::add_payment_form_meta_boxes( $post_type );
50
51
		// For invoice items.
52
		self::add_item_meta_boxes( $post_type );
53
54
		// For invoice discounts.
55
		if ( $post_type == 'wpi_discount' ) {
56
			add_meta_box( 'wpinv_discount_details', __( 'Discount Details', 'invoicing' ), 'GetPaid_Meta_Box_Discount_Details::output', 'wpi_discount', 'normal', 'high' );
57
		}
58
59
	}
60
61
	/**
62
	 * Register core metaboxes.
63
	 */
64
	protected static function add_payment_form_meta_boxes( $post_type ) {
65
66
		// For payment forms.
67
		if ( $post_type == 'wpi_payment_form' ) {
68
69
			// Design payment form.
70
			add_meta_box( 'wpinv-payment-form-design', __( 'Payment Form', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form::output', 'wpi_payment_form', 'normal' );
71
72
			// Payment form information.
73
			add_meta_box( 'wpinv-payment-form-info', __( 'Details', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form_Info::output', 'wpi_payment_form', 'side' );
74
75
		}
76
77
	}
78
79
	/**
80
	 * Register core metaboxes.
81
	 */
82
	protected static function add_item_meta_boxes( $post_type ) {
83
84
		if ( $post_type == 'wpi_item' ) {
85
86
			// Item details.
87
			add_meta_box( 'wpinv_item_details', __( 'Item Details', 'invoicing' ), 'GetPaid_Meta_Box_Item_Details::output', 'wpi_item', 'normal', 'high' );
88
89
			// If taxes are enabled, register the tax metabox.
90
			if ( getpaid_tax()->allow_vat_rules() || getpaid_tax()->allow_vat_classes() ) {
91
				add_meta_box( 'wpinv_item_vat', __( 'VAT / Tax', 'invoicing' ), 'GetPaid_Meta_Box_Item_VAT::output', 'wpi_item', 'normal', 'high' );
92
			}
93
94
			// Item info.
95
			add_meta_box( 'wpinv_field_item_info', __( 'Item info', 'invoicing' ), 'GetPaid_Meta_Box_Item_Info::output', 'wpi_item', 'side', 'core' );
96
97
		}
98
99
	}
100
101
	/**
102
	 * Register invoice metaboxes.
103
	 */
104
	protected static function add_invoice_meta_boxes( $post_type, $post ) {
105
106
		// For invoices...
107
		if ( getpaid_is_invoice_post_type( $post_type ) ) {
108
			$invoice = new WPInv_Invoice( $post );
109
110
			// Resend invoice.
111
			if ( ! $invoice->is_draft() && ! $invoice->is_paid() ) {
112
113
				add_meta_box(
114
					'wpinv-mb-resend-invoice',
115
					sprintf(
116
						__( 'Resend %s', 'invoicing' ),
117
						ucfirst( $invoice->get_invoice_quote_type() )
118
					),
119
					'GetPaid_Meta_Box_Resend_Invoice::output',
120
					$post_type,
121
					'side',
122
					'low'
123
				);
124
125
			}
126
127
			// Subscriptions.
128
			$subscription = getpaid_get_invoice_subscription( $invoice );
129
			if ( ! empty( $subscription ) ) {
130
				add_meta_box( 'wpinv-mb-subscriptions', __( 'Subscription Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output', $post_type, 'advanced' );
131
				add_meta_box( 'wpinv-mb-subscription-invoices', __( 'Related Payments', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Subscription::output_invoices', $post_type, 'advanced' );
132
			}
133
134
			// Invoice details.
135
			add_meta_box(
136
				'wpinv-details',
137
				sprintf(
138
					__( '%s Details', 'invoicing' ),
139
					ucfirst( $invoice->get_invoice_quote_type() )
140
				),
141
				'GetPaid_Meta_Box_Invoice_Details::output',
142
				$post_type,
143
				'side'
144
			);
145
146
			// Payment details.
147
			if ( ! $invoice->is_draft() ) {
148
				add_meta_box( 'wpinv-payment-meta', __( 'Payment Meta', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Payment_Meta::output', $post_type, 'side', 'default' );
149
			}
150
151
			// Billing details.
152
			add_meta_box( 'wpinv-address', __( 'Billing Details', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Address::output', $post_type, 'normal', 'high' );
153
			
154
			// Invoice items.
155
			add_meta_box(
156
				'wpinv-items',
157
				sprintf(
158
					__( '%s Items', 'invoicing' ),
159
					ucfirst( $invoice->get_invoice_quote_type() )
160
				),
161
				'GetPaid_Meta_Box_Invoice_Items::output',
162
				$post_type,
163
				'normal',
164
				'high'
165
			);
166
			
167
			// Invoice notes.
168
			add_meta_box(
169
				'wpinv-notes',
170
				sprintf(
171
					__( '%s Notes', 'invoicing' ),
172
					ucfirst( $invoice->get_invoice_quote_type() )
173
				),
174
				'WPInv_Meta_Box_Notes::output',
175
				$post_type,
176
				'side',
177
				'low'
178
			);
179
180
			// Shipping Address.
181
			if ( get_post_meta( $invoice->get_id(), 'shipping_address', true ) ) {
182
				add_meta_box( 'wpinv-invoice-shipping-details', __( 'Shipping Address', 'invoicing' ), 'GetPaid_Meta_Box_Invoice_Shipping_Address::output', $post_type, 'side', 'high' );
183
			}
184
185
			// Payment form information.
186
			if ( get_post_meta( $invoice->get_id(), 'payment_form_data', true ) ) {
187
				add_meta_box( 'wpinv-invoice-payment-form-details', __( 'Payment Form Details', 'invoicing' ), 'WPInv_Meta_Box_Payment_Form::output_details', $post_type, 'side', 'high' );
188
			}
189
190
		}
191
192
	}
193
194
	/**
195
	 * Remove some metaboxes.
196
	 */
197
	public static function remove_meta_boxes() {
198
		remove_meta_box( 'wpseo_meta', 'wpi_invoice', 'normal' );
199
	}
200
201
	/**
202
	 * Rename other metaboxes.
203
	 */
204
	public static function rename_meta_boxes() {
205
		
206
	}
207
208
	/**
209
	 * Check if we're saving, then trigger an action based on the post type.
210
	 *
211
	 * @param  int    $post_id Post ID.
212
	 * @param  object $post Post object.
213
	 */
214
	public static function save_meta_boxes( $post_id, $post ) {
215
		$post_id = absint( $post_id );
216
		$data    = wp_unslash( $_POST );
217
218
		// Do not save for ajax requests.
219
		if ( ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) {
220
			return;
221
		}
222
223
		// $post_id and $post are required
224
		if ( empty( $post_id ) || empty( $post ) || self::$saved_meta_boxes ) {
225
			return;
226
		}
227
228
		// Dont' save meta boxes for revisions or autosaves.
229
		if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
230
			return;
231
		}
232
233
		// Check the nonce.
234
		if ( empty( $data['getpaid_meta_nonce'] ) || ! wp_verify_nonce( $data['getpaid_meta_nonce'], 'getpaid_meta_nonce' ) ) {
235
			return;
236
		}
237
238
		// Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
239
		if ( empty( $data['post_ID'] ) || absint( $data['post_ID'] ) !== $post_id ) {
240
			return;
241
		}
242
243
		// Check user has permission to edit.
244
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
245
			return;
246
		}
247
248
		if ( getpaid_is_invoice_post_type( $post->post_type ) ) {
249
250
			// We need this save event to run once to avoid potential endless loops.
251
			self::$saved_meta_boxes = true;
252
253
			return GetPaid_Meta_Box_Invoice_Address::save( $post_id );
0 ignored issues
show
Bug introduced by
Are you sure the usage of GetPaid_Meta_Box_Invoice_Address::save($post_id) targeting GetPaid_Meta_Box_Invoice_Address::save() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
254
255
		}
256
257
		// Ensure this is our post type.
258
		$post_types_map = array(
259
			'wpi_item'         => 'GetPaid_Meta_Box_Item_Details',
260
			'wpi_payment_form' => 'GetPaid_Meta_Box_Payment_Form',
261
			'wpi_discount'     => 'GetPaid_Meta_Box_Discount_Details',
262
		);
263
264
		// Is this our post type?
265
		if ( ! isset( $post_types_map[ $post->post_type ] ) ) {
266
			return;
267
		}
268
269
		// We need this save event to run once to avoid potential endless loops.
270
		self::$saved_meta_boxes = true;
271
		
272
		// Save the post.
273
		$class = $post_types_map[ $post->post_type ];
274
		$class::save( $post_id, $_POST, $post );
275
276
	}
277
278
}
279