Passed
Push — master ( 1599c5...2383b4 )
by Brian
11:02
created
includes/admin/meta-boxes/class-getpaid-meta-box-discount-details.php 1 patch
Indentation   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
  */
9 9
 
10 10
 if ( ! defined( 'ABSPATH' ) ) {
11
-	exit; // Exit if accessed directly
11
+    exit; // Exit if accessed directly
12 12
 }
13 13
 
14 14
 /**
@@ -17,10 +17,10 @@  discard block
 block discarded – undo
17 17
 class GetPaid_Meta_Box_Discount_Details {
18 18
 
19 19
     /**
20
-	 * Output the metabox.
21
-	 *
22
-	 * @param WP_Post $post
23
-	 */
20
+     * Output the metabox.
21
+     *
22
+     * @param WP_Post $post
23
+     */
24 24
     public static function output( $post ) {
25 25
 
26 26
         // Prepare the discount.
@@ -368,34 +368,34 @@  discard block
 block discarded – undo
368 368
     }
369 369
 
370 370
     /**
371
-	 * Save meta box data.
372
-	 *
373
-	 * @param int $post_id
374
-	 */
375
-	public static function save( $post_id ) {
371
+     * Save meta box data.
372
+     *
373
+     * @param int $post_id
374
+     */
375
+    public static function save( $post_id ) {
376 376
 
377 377
         // Prepare the discount.
378 378
         $discount = new WPInv_Discount( $post_id );
379 379
 
380 380
         // Load new data.
381 381
         $discount->set_props(
382
-			array(
383
-				'code'                 => isset( $_POST['wpinv_discount_code'] ) ? $_POST['wpinv_discount_code'] : null,
384
-				'amount'               => isset( $_POST['wpinv_discount_amount'] ) ? $_POST['wpinv_discount_amount'] : null,
385
-				'start'                => isset( $_POST['wpinv_discount_start'] ) ? wpinv_clean( $_POST['wpinv_discount_start'] ) : null,
386
-				'expiration'           => isset( $_POST['wpinv_discount_expiration'] ) ? wpinv_clean( $_POST['wpinv_discount_expiration'] ) : null,
387
-				'is_single_use'        => isset( $_POST['wpinv_discount_single_use'] ),
382
+            array(
383
+                'code'                 => isset( $_POST['wpinv_discount_code'] ) ? $_POST['wpinv_discount_code'] : null,
384
+                'amount'               => isset( $_POST['wpinv_discount_amount'] ) ? $_POST['wpinv_discount_amount'] : null,
385
+                'start'                => isset( $_POST['wpinv_discount_start'] ) ? wpinv_clean( $_POST['wpinv_discount_start'] ) : null,
386
+                'expiration'           => isset( $_POST['wpinv_discount_expiration'] ) ? wpinv_clean( $_POST['wpinv_discount_expiration'] ) : null,
387
+                'is_single_use'        => isset( $_POST['wpinv_discount_single_use'] ),
388 388
                 'type'                 => isset( $_POST['wpinv_discount_type'] ) ? $_POST['wpinv_discount_type'] : null,
389
-				'is_recurring'         => isset( $_POST['wpinv_discount_recurring'] ),
390
-				'items'                => isset( $_POST['wpinv_discount_items'] ) ? $_POST['wpinv_discount_items'] : array(),
391
-				'excluded_items'       => isset( $_POST['wpinv_discount_excluded_items'] ) ? $_POST['wpinv_discount_excluded_items'] : array(),
392
-				'max_uses'             => isset( $_POST['wpinv_discount_max_uses'] ) ? $_POST['wpinv_discount_max_uses'] : null,
393
-				'min_total'            => isset( $_POST['wpinv_discount_min_total'] ) ? $_POST['wpinv_discount_min_total'] : null,
394
-				'max_total'            => isset( $_POST['wpinv_discount_max_total'] ) ? $_POST['wpinv_discount_max_total'] : null,
395
-			)
389
+                'is_recurring'         => isset( $_POST['wpinv_discount_recurring'] ),
390
+                'items'                => isset( $_POST['wpinv_discount_items'] ) ? $_POST['wpinv_discount_items'] : array(),
391
+                'excluded_items'       => isset( $_POST['wpinv_discount_excluded_items'] ) ? $_POST['wpinv_discount_excluded_items'] : array(),
392
+                'max_uses'             => isset( $_POST['wpinv_discount_max_uses'] ) ? $_POST['wpinv_discount_max_uses'] : null,
393
+                'min_total'            => isset( $_POST['wpinv_discount_min_total'] ) ? $_POST['wpinv_discount_min_total'] : null,
394
+                'max_total'            => isset( $_POST['wpinv_discount_max_total'] ) ? $_POST['wpinv_discount_max_total'] : null,
395
+            )
396 396
         );
397 397
 
398
-		$discount->save();
399
-		do_action( 'getpaid_discount_metabox_save', $post_id, $discount );
400
-	}
398
+        $discount->save();
399
+        do_action( 'getpaid_discount_metabox_save', $post_id, $discount );
400
+    }
401 401
 }
Please login to merge, or discard this patch.
includes/payments/class-getpaid-payment-form-submission-fees.php 1 patch
Indentation   +101 added lines, -101 removed lines patch added patch discarded remove patch
@@ -12,108 +12,108 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Payment_Form_Submission_Fees {
14 14
 
15
-	/**
16
-	 * The fee validation error.
17
-	 * @var string
18
-	 */
19
-	public $fee_error;
20
-
21
-	/**
22
-	 * Submission fees.
23
-	 * @var array
24
-	 */
25
-	public $fees = array();
15
+    /**
16
+     * The fee validation error.
17
+     * @var string
18
+     */
19
+    public $fee_error;
20
+
21
+    /**
22
+     * Submission fees.
23
+     * @var array
24
+     */
25
+    public $fees = array();
26
+
27
+    /**
28
+     * Class constructor
29
+     *
30
+     * @param GetPaid_Payment_Form_Submission $submission
31
+     */
32
+    public function __construct( $submission ) {
33
+
34
+        // Process any existing invoice fees.
35
+        if ( $submission->has_invoice() ) {
36
+            $this->fees = $submission->get_invoice()->get_fees();
37
+        }
38
+
39
+        // Process price fields.
40
+        $data         = $submission->get_data();
41
+        $payment_form = $submission->get_payment_form();
42
+
43
+        foreach ( $payment_form->get_elements() as $element ) {
44
+
45
+            if ( 'price_input' == $element['type'] ) {
46
+                $this->process_price_input( $element, $data );
47
+            }
48
+
49
+            if ( 'price_select' == $element['type'] ) {
50
+                $this->process_price_select( $element, $data );
51
+            }
52
+
53
+        }
54
+
55
+    }
56
+
57
+    /**
58
+     * Process a price input field.
59
+     *
60
+     * @param array $element
61
+     * @param array $data
62
+     */
63
+    public function process_price_input( $element, $data ) {
64
+
65
+        // Abort if not passed.
66
+        if ( empty( $data[ $element['id'] ] ) ) {
67
+            return;
68
+        }
69
+
70
+        $amount  = (float) wpinv_sanitize_amount( $data[ $element['id'] ] );
71
+        $minimum = empty( $element['minimum'] ) ? 0 : (float) wpinv_sanitize_amount( $element['minimum'] );
72
+
73
+        if ( $amount < $minimum ) {
74
+            throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), $minimum ) );
75
+        }
76
+
77
+        $this->fees[ $element['label'] ] = array(
78
+            'name'          => $element['label'],
79
+            'initial_fee'   => $amount,
80
+            'recurring_fee' => 0,
81
+        );
82
+
83
+    }
26 84
 
27 85
     /**
28
-	 * Class constructor
29
-	 *
30
-	 * @param GetPaid_Payment_Form_Submission $submission
31
-	 */
32
-	public function __construct( $submission ) {
33
-
34
-		// Process any existing invoice fees.
35
-		if ( $submission->has_invoice() ) {
36
-			$this->fees = $submission->get_invoice()->get_fees();
37
-		}
38
-
39
-		// Process price fields.
40
-		$data         = $submission->get_data();
41
-		$payment_form = $submission->get_payment_form();
42
-
43
-		foreach ( $payment_form->get_elements() as $element ) {
44
-
45
-			if ( 'price_input' == $element['type'] ) {
46
-				$this->process_price_input( $element, $data );
47
-			}
48
-
49
-			if ( 'price_select' == $element['type'] ) {
50
-				$this->process_price_select( $element, $data );
51
-			}
52
-
53
-		}
54
-
55
-	}
56
-
57
-	/**
58
-	 * Process a price input field.
59
-	 *
60
-	 * @param array $element
61
-	 * @param array $data
62
-	 */
63
-	public function process_price_input( $element, $data ) {
64
-
65
-		// Abort if not passed.
66
-		if ( empty( $data[ $element['id'] ] ) ) {
67
-			return;
68
-		}
69
-
70
-		$amount  = (float) wpinv_sanitize_amount( $data[ $element['id'] ] );
71
-		$minimum = empty( $element['minimum'] ) ? 0 : (float) wpinv_sanitize_amount( $element['minimum'] );
72
-
73
-		if ( $amount < $minimum ) {
74
-			throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), $minimum ) );
75
-		}
76
-
77
-		$this->fees[ $element['label'] ] = array(
78
-			'name'          => $element['label'],
79
-			'initial_fee'   => $amount,
80
-			'recurring_fee' => 0,
81
-		);
82
-
83
-	}
84
-
85
-	/**
86
-	 * Process a price select field.
87
-	 *
88
-	 * @param array $element
89
-	 * @param array $data
90
-	 */
91
-	public function process_price_select( $element, $data ) {
92
-
93
-		// Abort if not passed.
94
-		if ( empty( $data[ $element['id'] ] ) ) {
95
-			return;
96
-		}
97
-
98
-		$options  = getpaid_convert_price_string_to_options( $element['options'] );
99
-		$selected = wpinv_parse_list( $data[ $element['id'] ] );
100
-		$total    = 0;
101
-
102
-		foreach ( $selected as $price ) {
103
-
104
-			if ( ! isset( $options[ $price ] ) ) {
105
-				throw new Exception( __( 'You have selected an invalid amount', 'invoicing' ) );
106
-			}
107
-
108
-			$total += (float) wpinv_sanitize_amount( $price );
109
-		}
110
-
111
-		$this->fees[ $element['label'] ] = array(
112
-			'name'          => $element['label'],
113
-			'initial_fee'   => $total,
114
-			'recurring_fee' => 0,
115
-		);
116
-
117
-	}
86
+     * Process a price select field.
87
+     *
88
+     * @param array $element
89
+     * @param array $data
90
+     */
91
+    public function process_price_select( $element, $data ) {
92
+
93
+        // Abort if not passed.
94
+        if ( empty( $data[ $element['id'] ] ) ) {
95
+            return;
96
+        }
97
+
98
+        $options  = getpaid_convert_price_string_to_options( $element['options'] );
99
+        $selected = wpinv_parse_list( $data[ $element['id'] ] );
100
+        $total    = 0;
101
+
102
+        foreach ( $selected as $price ) {
103
+
104
+            if ( ! isset( $options[ $price ] ) ) {
105
+                throw new Exception( __( 'You have selected an invalid amount', 'invoicing' ) );
106
+            }
107
+
108
+            $total += (float) wpinv_sanitize_amount( $price );
109
+        }
110
+
111
+        $this->fees[ $element['label'] ] = array(
112
+            'name'          => $element['label'],
113
+            'initial_fee'   => $total,
114
+            'recurring_fee' => 0,
115
+        );
116
+
117
+    }
118 118
 
119 119
 }
Please login to merge, or discard this patch.
includes/payments/class-getpaid-payment-form-submission-taxes.php 1 patch
Indentation   +212 added lines, -212 removed lines patch added patch discarded remove patch
@@ -12,231 +12,231 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Payment_Form_Submission_Taxes {
14 14
 
15
-	/**
16
-	 * Submission taxes.
17
-	 * @var array
18
-	 */
19
-	public $taxes = array();
20
-
21
-	/**
22
-	 * Initial tax.
23
-	 * @var float
24
-	 */
25
-	protected $initial_tax = 0;
26
-
27
-	/**
28
-	 * Recurring tax.
29
-	 * @var float
30
-	 */
31
-	protected $recurring_tax = 0;
15
+    /**
16
+     * Submission taxes.
17
+     * @var array
18
+     */
19
+    public $taxes = array();
20
+
21
+    /**
22
+     * Initial tax.
23
+     * @var float
24
+     */
25
+    protected $initial_tax = 0;
26
+
27
+    /**
28
+     * Recurring tax.
29
+     * @var float
30
+     */
31
+    protected $recurring_tax = 0;
32
+
33
+    /**
34
+     * Class constructor
35
+     *
36
+     * @param GetPaid_Payment_Form_Submission $submission
37
+     */
38
+    public function __construct( $submission ) {
39
+
40
+        // Validate VAT number.
41
+        $this->validate_vat( $submission );
42
+
43
+        foreach ( $submission->get_items() as $item ) {
44
+            $this->process_item_tax( $item, $submission );
45
+        }
46
+
47
+        // Process any existing invoice taxes.
48
+        if ( $submission->has_invoice() ) {
49
+            $this->taxes = $submission->get_invoice()->get_taxes();
50
+        }
51
+
52
+        // Add VAT.
53
+        $this->taxes['vat'] = array(
54
+            'name'          => 'vat',
55
+            'initial_tax'   => $this->initial_tax,
56
+            'recurring_tax' => $this->recurring_tax,
57
+        );
58
+
59
+    }
60
+
61
+    /**
62
+     * Maybe process tax.
63
+     *
64
+     * @since 1.0.19
65
+     * @param GetPaid_Form_Item $item
66
+     * @param GetPaid_Payment_Form_Submission $submission
67
+     */
68
+    public function process_item_tax( $item, $submission ) {
69
+
70
+        $rate     = wpinv_get_tax_rate( $submission->country, $submission->state, $item->get_id() );
71
+        $price    = $item->get_sub_total();
72
+        $item_tax = $price * $rate * 0.01;
73
+
74
+        if ( wpinv_prices_include_tax() ) {
75
+            $item_tax = $price - ( $price - $price * $rate * 0.01 );
76
+        }
77
+
78
+        $this->initial_tax += $item_tax;
79
+
80
+        if ( $item->is_recurring() ) {
81
+            $this->recurring_tax += $item_tax;
82
+        }
83
+
84
+    }
85
+
86
+    /**
87
+     * Checks if the submission has a digital item.
88
+     *
89
+     * @param GetPaid_Payment_Form_Submission $submission
90
+     * @since 1.0.19
91
+     * @return bool
92
+     */
93
+    public function has_digital_item( $submission ) {
94
+
95
+        foreach ( $submission->get_items() as $item ) {
96
+
97
+            if ( 'digital' == $item->get_vat_rule() ) {
98
+                return true;
99
+            }
100
+
101
+        }
102
+
103
+        return false;
104
+    }
105
+
106
+    /**
107
+     * Checks if this is an eu store.
108
+     *
109
+     * @since 1.0.19
110
+     * @return bool
111
+     */
112
+    public function is_eu_store() {
113
+        return $this->is_eu_country( wpinv_get_default_country() );
114
+    }
115
+
116
+    /**
117
+     * Checks if this is an eu country.
118
+     *
119
+     * @param string $country
120
+     * @since 1.0.19
121
+     * @return bool
122
+     */
123
+    public function is_eu_country( $country ) {
124
+        return getpaid_is_eu_state( $country ) || getpaid_is_gst_country( $country );
125
+    }
126
+
127
+    /**
128
+     * Checks if this is an eu purchase.
129
+     *
130
+     * @param string $customer_country
131
+     * @since 1.0.19
132
+     * @return bool
133
+     */
134
+    public function is_eu_transaction( $customer_country ) {
135
+        return $this->is_eu_country( $customer_country ) && $this->is_eu_store();
136
+    }
137
+
138
+    /**
139
+     * Retrieves the vat number.
140
+     *
141
+     * @param GetPaid_Payment_Form_Submission $submission
142
+     * @since 1.0.19
143
+     * @return string
144
+     */
145
+    public function get_vat_number( $submission ) {
146
+
147
+        // Retrieve from the posted number.
148
+        $vat_number = $submission->get_field( 'wpinv_vat_number' );
149
+        if ( ! empty( $vat_number ) ) {
150
+            return wpinv_clean( $vat_number );
151
+        }
152
+
153
+        // Retrieve from the invoice.
154
+        return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : '';
155
+    }
156
+
157
+    /**
158
+     * Retrieves the company.
159
+     *
160
+     * @param GetPaid_Payment_Form_Submission $submission
161
+     * @since 1.0.19
162
+     * @return string
163
+     */
164
+    public function get_company( $submission ) {
165
+
166
+        // Retrieve from the posted data.
167
+        $company = $submission->get_field( 'wpinv_company' );
168
+        if ( ! empty( $company ) ) {
169
+            return wpinv_clean( $company );
170
+        }
171
+
172
+        // Retrieve from the invoice.
173
+        return $submission->has_invoice() ? $submission->get_invoice()->get_company() : '';
174
+    }
175
+
176
+    /**
177
+     * Checks if we requires a VAT number.
178
+     *
179
+     * @param bool $ip_in_eu Whether the customer IP is from the EU
180
+     * @param bool $country_in_eu Whether the customer country is from the EU
181
+     * @since 1.0.19
182
+     * @return string
183
+     */
184
+    public function requires_vat( $ip_in_eu, $country_in_eu ) {
185
+
186
+        $prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' );
187
+        $prevent_b2c = ! empty( $prevent_b2c );
188
+        $is_eu       = $ip_in_eu || $country_in_eu;
189
+
190
+        return $prevent_b2c && $is_eu;
191
+    }
32 192
 
33 193
     /**
34
-	 * Class constructor
35
-	 *
36
-	 * @param GetPaid_Payment_Form_Submission $submission
37
-	 */
38
-	public function __construct( $submission ) {
39
-
40
-		// Validate VAT number.
41
-		$this->validate_vat( $submission );
42
-
43
-		foreach ( $submission->get_items() as $item ) {
44
-			$this->process_item_tax( $item, $submission );
45
-		}
46
-
47
-		// Process any existing invoice taxes.
48
-		if ( $submission->has_invoice() ) {
49
-			$this->taxes = $submission->get_invoice()->get_taxes();
50
-		}
51
-
52
-		// Add VAT.
53
-		$this->taxes['vat'] = array(
54
-			'name'          => 'vat',
55
-			'initial_tax'   => $this->initial_tax,
56
-			'recurring_tax' => $this->recurring_tax,
57
-		);
58
-
59
-	}
60
-
61
-	/**
62
-	 * Maybe process tax.
63
-	 *
64
-	 * @since 1.0.19
65
-	 * @param GetPaid_Form_Item $item
66
-	 * @param GetPaid_Payment_Form_Submission $submission
67
-	 */
68
-	public function process_item_tax( $item, $submission ) {
69
-
70
-		$rate     = wpinv_get_tax_rate( $submission->country, $submission->state, $item->get_id() );
71
-		$price    = $item->get_sub_total();
72
-		$item_tax = $price * $rate * 0.01;
73
-
74
-		if ( wpinv_prices_include_tax() ) {
75
-			$item_tax = $price - ( $price - $price * $rate * 0.01 );
76
-		}
77
-
78
-		$this->initial_tax += $item_tax;
79
-
80
-		if ( $item->is_recurring() ) {
81
-			$this->recurring_tax += $item_tax;
82
-		}
83
-
84
-	}
85
-
86
-	/**
87
-	 * Checks if the submission has a digital item.
88
-	 *
89
-	 * @param GetPaid_Payment_Form_Submission $submission
90
-	 * @since 1.0.19
91
-	 * @return bool
92
-	 */
93
-	public function has_digital_item( $submission ) {
94
-
95
-		foreach ( $submission->get_items() as $item ) {
96
-
97
-			if ( 'digital' == $item->get_vat_rule() ) {
98
-				return true;
99
-			}
100
-
101
-		}
102
-
103
-		return false;
104
-	}
105
-
106
-	/**
107
-	 * Checks if this is an eu store.
108
-	 *
109
-	 * @since 1.0.19
110
-	 * @return bool
111
-	 */
112
-	public function is_eu_store() {
113
-		return $this->is_eu_country( wpinv_get_default_country() );
114
-	}
115
-
116
-	/**
117
-	 * Checks if this is an eu country.
118
-	 *
119
-	 * @param string $country
120
-	 * @since 1.0.19
121
-	 * @return bool
122
-	 */
123
-	public function is_eu_country( $country ) {
124
-		return getpaid_is_eu_state( $country ) || getpaid_is_gst_country( $country );
125
-	}
126
-
127
-	/**
128
-	 * Checks if this is an eu purchase.
129
-	 *
130
-	 * @param string $customer_country
131
-	 * @since 1.0.19
132
-	 * @return bool
133
-	 */
134
-	public function is_eu_transaction( $customer_country ) {
135
-		return $this->is_eu_country( $customer_country ) && $this->is_eu_store();
136
-	}
137
-
138
-	/**
139
-	 * Retrieves the vat number.
140
-	 *
141
-	 * @param GetPaid_Payment_Form_Submission $submission
142
-	 * @since 1.0.19
143
-	 * @return string
144
-	 */
145
-	public function get_vat_number( $submission ) {
146
-
147
-		// Retrieve from the posted number.
148
-		$vat_number = $submission->get_field( 'wpinv_vat_number' );
149
-		if ( ! empty( $vat_number ) ) {
150
-			return wpinv_clean( $vat_number );
151
-		}
152
-
153
-		// Retrieve from the invoice.
154
-		return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : '';
155
-	}
156
-
157
-	/**
158
-	 * Retrieves the company.
159
-	 *
160
-	 * @param GetPaid_Payment_Form_Submission $submission
161
-	 * @since 1.0.19
162
-	 * @return string
163
-	 */
164
-	public function get_company( $submission ) {
165
-
166
-		// Retrieve from the posted data.
167
-		$company = $submission->get_field( 'wpinv_company' );
168
-		if ( ! empty( $company ) ) {
169
-			return wpinv_clean( $company );
170
-		}
171
-
172
-		// Retrieve from the invoice.
173
-		return $submission->has_invoice() ? $submission->get_invoice()->get_company() : '';
174
-	}
175
-
176
-	/**
177
-	 * Checks if we requires a VAT number.
178
-	 *
179
-	 * @param bool $ip_in_eu Whether the customer IP is from the EU
180
-	 * @param bool $country_in_eu Whether the customer country is from the EU
181
-	 * @since 1.0.19
182
-	 * @return string
183
-	 */
184
-	public function requires_vat( $ip_in_eu, $country_in_eu ) {
185
-
186
-		$prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' );
187
-		$prevent_b2c = ! empty( $prevent_b2c );
188
-		$is_eu       = $ip_in_eu || $country_in_eu;
189
-
190
-		return $prevent_b2c && $is_eu;
191
-	}
192
-
193
-	/**
194
-	 * Validate VAT data.
195
-	 *
196
-	 * @param GetPaid_Payment_Form_Submission $submission
197
-	 * @since 1.0.19
198
-	 */
199
-	public function validate_vat( $submission ) {
200
-
201
-		$has_digital = $this->has_digital_item( $submission );
202
-		$in_eu       = $this->is_eu_transaction( $submission->country );
203
-
204
-		// Abort if we are not validating vat numbers.
205
-		if ( ! $has_digital && ! $in_eu ) {
194
+     * Validate VAT data.
195
+     *
196
+     * @param GetPaid_Payment_Form_Submission $submission
197
+     * @since 1.0.19
198
+     */
199
+    public function validate_vat( $submission ) {
200
+
201
+        $has_digital = $this->has_digital_item( $submission );
202
+        $in_eu       = $this->is_eu_transaction( $submission->country );
203
+
204
+        // Abort if we are not validating vat numbers.
205
+        if ( ! $has_digital && ! $in_eu ) {
206 206
             return;
207
-		}
207
+        }
208 208
 
209
-		// Prepare variables.
210
-		$vat_number  = $this->get_vat_number( $submission );
211
-		$company     = $this->get_company( $submission );
212
-		$ip_country  = WPInv_EUVat::get_country_by_ip();
209
+        // Prepare variables.
210
+        $vat_number  = $this->get_vat_number( $submission );
211
+        $company     = $this->get_company( $submission );
212
+        $ip_country  = WPInv_EUVat::get_country_by_ip();
213 213
         $is_eu       = $this->is_eu_country( $submission->country );
214 214
         $is_ip_eu    = $this->is_eu_country( $ip_country );
215 215
 
216
-		// If we're preventing business to consumer purchases, ensure
217
-		if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) {
216
+        // If we're preventing business to consumer purchases, ensure
217
+        if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) {
218 218
 
219
-			// Ensure that a vat number has been specified.
220
-			throw new Exception(
221
-				wp_sprintf(
222
-					__( 'Please enter your %s number to verify your purchase is by an EU business.', 'invoicing' ),
223
-					getpaid_vat_name()
224
-				)
225
-			);
219
+            // Ensure that a vat number has been specified.
220
+            throw new Exception(
221
+                wp_sprintf(
222
+                    __( 'Please enter your %s number to verify your purchase is by an EU business.', 'invoicing' ),
223
+                    getpaid_vat_name()
224
+                )
225
+            );
226 226
 
227
-		}
227
+        }
228 228
 
229
-		// Abort if we are not validating vat (vat number should exist, user should be in eu and business too).
230
-		if ( ! $is_eu || ! $in_eu || empty( $vat_number ) ) {
229
+        // Abort if we are not validating vat (vat number should exist, user should be in eu and business too).
230
+        if ( ! $is_eu || ! $in_eu || empty( $vat_number ) ) {
231 231
             return;
232
-		}
232
+        }
233 233
 
234
-		$is_valid = WPInv_EUVat::validate_vat_number( $vat_number, $company, $submission->country );
234
+        $is_valid = WPInv_EUVat::validate_vat_number( $vat_number, $company, $submission->country );
235 235
 
236
-		if ( is_string( $is_valid ) ) {
237
-			throw new Exception( $is_valid );
238
-		}
236
+        if ( is_string( $is_valid ) ) {
237
+            throw new Exception( $is_valid );
238
+        }
239 239
 
240
-	}
240
+    }
241 241
 
242 242
 }
Please login to merge, or discard this patch.
includes/payments/class-getpaid-payment-form-submission-items.php 1 patch
Indentation   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -12,77 +12,77 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Payment_Form_Submission_Items {
14 14
 
15
-	/**
16
-	 * Submission items.
17
-	 * @var GetPaid_Form_Item[]
18
-	 */
19
-	public $items = array();
15
+    /**
16
+     * Submission items.
17
+     * @var GetPaid_Form_Item[]
18
+     */
19
+    public $items = array();
20 20
 
21 21
     /**
22
-	 * Class constructor
23
-	 *
24
-	 * @param GetPaid_Payment_Form_Submission $submission
25
-	 */
26
-	public function __construct( $submission ) {
22
+     * Class constructor
23
+     *
24
+     * @param GetPaid_Payment_Form_Submission $submission
25
+     */
26
+    public function __construct( $submission ) {
27 27
 
28
-		$data         = $submission->get_data();
29
-		$payment_form = $submission->get_payment_form();
28
+        $data         = $submission->get_data();
29
+        $payment_form = $submission->get_payment_form();
30 30
 
31
-		// Prepare the selected items.
32
-		$selected_items = array();
33
-		if ( ! empty( $data['getpaid-items'] ) ) {
34
-			$selected_items = wpinv_clean( $data['getpaid-items'] );
35
-		}
31
+        // Prepare the selected items.
32
+        $selected_items = array();
33
+        if ( ! empty( $data['getpaid-items'] ) ) {
34
+            $selected_items = wpinv_clean( $data['getpaid-items'] );
35
+        }
36 36
 
37
-		// For default forms, ensure that an item has been set.
38
-		if ( $payment_form->is_default() && ! $submission->has_invoice() ) {
39
-			$payment_form->set_items( $selected_items );
40
-		}
37
+        // For default forms, ensure that an item has been set.
38
+        if ( $payment_form->is_default() && ! $submission->has_invoice() ) {
39
+            $payment_form->set_items( $selected_items );
40
+        }
41 41
 
42
-		// Process each individual item.
43
-		foreach ( $payment_form->get_items() as $item ) {
44
-			$this->process_item( $item, $selected_items );
45
-		}
42
+        // Process each individual item.
43
+        foreach ( $payment_form->get_items() as $item ) {
44
+            $this->process_item( $item, $selected_items );
45
+        }
46 46
 
47
-	}
47
+    }
48 48
 
49
-	/**
50
-	 * Process a single item.
51
-	 *
52
-	 * @param GetPaid_Form_Item $item
53
-	 * @param array $selected_items
54
-	 */
55
-	public function process_item( $item, $selected_items ) {
49
+    /**
50
+     * Process a single item.
51
+     *
52
+     * @param GetPaid_Form_Item $item
53
+     * @param array $selected_items
54
+     */
55
+    public function process_item( $item, $selected_items ) {
56 56
 
57
-		// Abort if this is an optional item and it has not been selected.
58
-		if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) {
59
-			return;
60
-		}
57
+        // Abort if this is an optional item and it has not been selected.
58
+        if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) {
59
+            return;
60
+        }
61 61
 
62
-		// (maybe) let customers change the quantities and prices.
63
-		if ( isset( $selected_items[ $item->get_id() ] ) ) {
62
+        // (maybe) let customers change the quantities and prices.
63
+        if ( isset( $selected_items[ $item->get_id() ] ) ) {
64 64
 
65
-			// Maybe change the quantities.
66
-			if ( $item->allows_quantities() ) {
67
-				$item->set_quantity( (int) $selected_items[ $item->get_id() ]['quantity'] );
68
-			}
65
+            // Maybe change the quantities.
66
+            if ( $item->allows_quantities() ) {
67
+                $item->set_quantity( (int) $selected_items[ $item->get_id() ]['quantity'] );
68
+            }
69 69
 
70
-			// Maybe change the price.
71
-			if ( $item->user_can_set_their_price() ) {
72
-				$price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] );
70
+            // Maybe change the price.
71
+            if ( $item->user_can_set_their_price() ) {
72
+                $price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] );
73 73
 
74
-				// But don't get lower than the minimum price.
75
-				$price = max( $price, $item->get_minimum_price() );
74
+                // But don't get lower than the minimum price.
75
+                $price = max( $price, $item->get_minimum_price() );
76 76
 
77
-				$item->set_price( $price );
77
+                $item->set_price( $price );
78 78
 
79
-			}
79
+            }
80 80
 
81
-		}
81
+        }
82 82
 
83
-		// Save the item.
84
-		$this->items[] = $item;
83
+        // Save the item.
84
+        $this->items[] = $item;
85 85
 
86
-	}
86
+    }
87 87
 
88 88
 }
Please login to merge, or discard this patch.
includes/payments/class-getpaid-payment-form.php 1 patch
Indentation   +520 added lines, -520 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 if ( ! defined( 'ABSPATH' ) ) {
3
-	exit;
3
+    exit;
4 4
 }
5 5
 
6 6
 /**
@@ -10,55 +10,55 @@  discard block
 block discarded – undo
10 10
 class GetPaid_Payment_Form extends GetPaid_Data {
11 11
 
12 12
     /**
13
-	 * Which data store to load.
14
-	 *
15
-	 * @var string
16
-	 */
13
+     * Which data store to load.
14
+     *
15
+     * @var string
16
+     */
17 17
     protected $data_store_name = 'payment_form';
18 18
 
19 19
     /**
20
-	 * This is the name of this object type.
21
-	 *
22
-	 * @var string
23
-	 */
24
-	protected $object_type = 'payment_form';
20
+     * This is the name of this object type.
21
+     *
22
+     * @var string
23
+     */
24
+    protected $object_type = 'payment_form';
25 25
 
26 26
     /**
27
-	 * Form Data array. This is the core form data exposed in APIs.
28
-	 *
29
-	 * @since 1.0.19
30
-	 * @var array
31
-	 */
32
-	protected $data = array(
33
-		'status'               => 'draft',
34
-		'version'              => '',
35
-		'date_created'         => null,
27
+     * Form Data array. This is the core form data exposed in APIs.
28
+     *
29
+     * @since 1.0.19
30
+     * @var array
31
+     */
32
+    protected $data = array(
33
+        'status'               => 'draft',
34
+        'version'              => '',
35
+        'date_created'         => null,
36 36
         'date_modified'        => null,
37 37
         'name'                 => '',
38 38
         'author'               => 1,
39 39
         'elements'             => null,
40
-		'items'                => null,
41
-		'earned'               => 0,
42
-		'refunded'             => 0,
43
-		'cancelled'            => 0,
44
-		'failed'               => 0,
45
-	);
46
-
47
-    /**
48
-	 * Stores meta in cache for future reads.
49
-	 *
50
-	 * A group must be set to to enable caching.
51
-	 *
52
-	 * @var string
53
-	 */
54
-	protected $cache_group = 'getpaid_forms';
55
-
56
-	/**
57
-	 * Stores a reference to the invoice if the form is for an invoice..
58
-	 *
59
-	 * @var WPInv_Invoice
60
-	 */
61
-	public $invoice = 0;
40
+        'items'                => null,
41
+        'earned'               => 0,
42
+        'refunded'             => 0,
43
+        'cancelled'            => 0,
44
+        'failed'               => 0,
45
+    );
46
+
47
+    /**
48
+     * Stores meta in cache for future reads.
49
+     *
50
+     * A group must be set to to enable caching.
51
+     *
52
+     * @var string
53
+     */
54
+    protected $cache_group = 'getpaid_forms';
55
+
56
+    /**
57
+     * Stores a reference to the invoice if the form is for an invoice..
58
+     *
59
+     * @var WPInv_Invoice
60
+     */
61
+    public $invoice = 0;
62 62
 
63 63
     /**
64 64
      * Stores a reference to the original WP_Post object
@@ -68,35 +68,35 @@  discard block
 block discarded – undo
68 68
     protected $post = null;
69 69
 
70 70
     /**
71
-	 * Get the form if ID is passed, otherwise the form is new and empty.
72
-	 *
73
-	 * @param  int|object|GetPaid_Payment_Form|WP_Post $form Form to read.
74
-	 */
75
-	public function __construct( $form = 0 ) {
76
-		parent::__construct( $form );
71
+     * Get the form if ID is passed, otherwise the form is new and empty.
72
+     *
73
+     * @param  int|object|GetPaid_Payment_Form|WP_Post $form Form to read.
74
+     */
75
+    public function __construct( $form = 0 ) {
76
+        parent::__construct( $form );
77 77
 
78
-		if ( is_numeric( $form ) && $form > 0 ) {
79
-			$this->set_id( $form );
80
-		} elseif ( $form instanceof self ) {
78
+        if ( is_numeric( $form ) && $form > 0 ) {
79
+            $this->set_id( $form );
80
+        } elseif ( $form instanceof self ) {
81 81
 
82
-			$this->set_id( $form->get_id() );
83
-			$this->invoice = $form->invoice;
82
+            $this->set_id( $form->get_id() );
83
+            $this->invoice = $form->invoice;
84 84
 
85
-		} elseif ( ! empty( $form->ID ) ) {
86
-			$this->set_id( $form->ID );
87
-		} else {
88
-			$this->set_object_read( true );
89
-		}
85
+        } elseif ( ! empty( $form->ID ) ) {
86
+            $this->set_id( $form->ID );
87
+        } else {
88
+            $this->set_object_read( true );
89
+        }
90 90
 
91 91
         // Load the datastore.
92
-		$this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
92
+        $this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
93 93
 
94
-		if ( $this->get_id() > 0 ) {
94
+        if ( $this->get_id() > 0 ) {
95 95
             $this->post = get_post( $this->get_id() );
96
-			$this->data_store->read( $this );
96
+            $this->data_store->read( $this );
97 97
         }
98 98
 
99
-	}
99
+    }
100 100
 
101 101
     /*
102 102
 	|--------------------------------------------------------------------------
@@ -114,351 +114,351 @@  discard block
 block discarded – undo
114 114
     */
115 115
 
116 116
     /**
117
-	 * Get plugin version when the form was created.
118
-	 *
119
-	 * @since 1.0.19
120
-	 * @param  string $context View or edit context.
121
-	 * @return string
122
-	 */
123
-	public function get_version( $context = 'view' ) {
124
-		return $this->get_prop( 'version', $context );
117
+     * Get plugin version when the form was created.
118
+     *
119
+     * @since 1.0.19
120
+     * @param  string $context View or edit context.
121
+     * @return string
122
+     */
123
+    public function get_version( $context = 'view' ) {
124
+        return $this->get_prop( 'version', $context );
125 125
     }
126 126
 
127 127
     /**
128
-	 * Get date when the form was created.
129
-	 *
130
-	 * @since 1.0.19
131
-	 * @param  string $context View or edit context.
132
-	 * @return string
133
-	 */
134
-	public function get_date_created( $context = 'view' ) {
135
-		return $this->get_prop( 'date_created', $context );
128
+     * Get date when the form was created.
129
+     *
130
+     * @since 1.0.19
131
+     * @param  string $context View or edit context.
132
+     * @return string
133
+     */
134
+    public function get_date_created( $context = 'view' ) {
135
+        return $this->get_prop( 'date_created', $context );
136 136
     }
137 137
 
138 138
     /**
139
-	 * Get GMT date when the form was created.
140
-	 *
141
-	 * @since 1.0.19
142
-	 * @param  string $context View or edit context.
143
-	 * @return string
144
-	 */
145
-	public function get_date_created_gmt( $context = 'view' ) {
139
+     * Get GMT date when the form was created.
140
+     *
141
+     * @since 1.0.19
142
+     * @param  string $context View or edit context.
143
+     * @return string
144
+     */
145
+    public function get_date_created_gmt( $context = 'view' ) {
146 146
         $date = $this->get_date_created( $context );
147 147
 
148 148
         if ( $date ) {
149 149
             $date = get_gmt_from_date( $date );
150 150
         }
151
-		return $date;
151
+        return $date;
152 152
     }
153 153
 
154 154
     /**
155
-	 * Get date when the form was last modified.
156
-	 *
157
-	 * @since 1.0.19
158
-	 * @param  string $context View or edit context.
159
-	 * @return string
160
-	 */
161
-	public function get_date_modified( $context = 'view' ) {
162
-		return $this->get_prop( 'date_modified', $context );
155
+     * Get date when the form was last modified.
156
+     *
157
+     * @since 1.0.19
158
+     * @param  string $context View or edit context.
159
+     * @return string
160
+     */
161
+    public function get_date_modified( $context = 'view' ) {
162
+        return $this->get_prop( 'date_modified', $context );
163 163
     }
164 164
 
165 165
     /**
166
-	 * Get GMT date when the form was last modified.
167
-	 *
168
-	 * @since 1.0.19
169
-	 * @param  string $context View or edit context.
170
-	 * @return string
171
-	 */
172
-	public function get_date_modified_gmt( $context = 'view' ) {
166
+     * Get GMT date when the form was last modified.
167
+     *
168
+     * @since 1.0.19
169
+     * @param  string $context View or edit context.
170
+     * @return string
171
+     */
172
+    public function get_date_modified_gmt( $context = 'view' ) {
173 173
         $date = $this->get_date_modified( $context );
174 174
 
175 175
         if ( $date ) {
176 176
             $date = get_gmt_from_date( $date );
177 177
         }
178
-		return $date;
178
+        return $date;
179 179
     }
180 180
 
181 181
     /**
182
-	 * Get the form name.
183
-	 *
184
-	 * @since 1.0.19
185
-	 * @param  string $context View or edit context.
186
-	 * @return string
187
-	 */
188
-	public function get_name( $context = 'view' ) {
189
-		return $this->get_prop( 'name', $context );
182
+     * Get the form name.
183
+     *
184
+     * @since 1.0.19
185
+     * @param  string $context View or edit context.
186
+     * @return string
187
+     */
188
+    public function get_name( $context = 'view' ) {
189
+        return $this->get_prop( 'name', $context );
190 190
     }
191 191
 
192 192
     /**
193
-	 * Alias of self::get_name().
194
-	 *
195
-	 * @since 1.0.19
196
-	 * @param  string $context View or edit context.
197
-	 * @return string
198
-	 */
199
-	public function get_title( $context = 'view' ) {
200
-		return $this->get_name( $context );
201
-	}
193
+     * Alias of self::get_name().
194
+     *
195
+     * @since 1.0.19
196
+     * @param  string $context View or edit context.
197
+     * @return string
198
+     */
199
+    public function get_title( $context = 'view' ) {
200
+        return $this->get_name( $context );
201
+    }
202 202
 
203 203
     /**
204
-	 * Get the owner of the form.
205
-	 *
206
-	 * @since 1.0.19
207
-	 * @param  string $context View or edit context.
208
-	 * @return int
209
-	 */
210
-	public function get_author( $context = 'view' ) {
211
-		return (int) $this->get_prop( 'author', $context );
204
+     * Get the owner of the form.
205
+     *
206
+     * @since 1.0.19
207
+     * @param  string $context View or edit context.
208
+     * @return int
209
+     */
210
+    public function get_author( $context = 'view' ) {
211
+        return (int) $this->get_prop( 'author', $context );
212 212
     }
213 213
 
214 214
     /**
215
-	 * Get the elements that make up the form.
216
-	 *
217
-	 * @since 1.0.19
218
-	 * @param  string $context View or edit context.
219
-	 * @return array
220
-	 */
221
-	public function get_elements( $context = 'view' ) {
222
-		$elements = $this->get_prop( 'elements', $context );
215
+     * Get the elements that make up the form.
216
+     *
217
+     * @since 1.0.19
218
+     * @param  string $context View or edit context.
219
+     * @return array
220
+     */
221
+    public function get_elements( $context = 'view' ) {
222
+        $elements = $this->get_prop( 'elements', $context );
223 223
 
224
-		if ( empty( $elements ) || ! is_array( $elements ) ) {
224
+        if ( empty( $elements ) || ! is_array( $elements ) ) {
225 225
             return wpinv_get_data( 'sample-payment-form' );
226
-		}
226
+        }
227 227
 
228
-		// Ensure that all required elements exist.
229
-		$_elements = array();
230
-		foreach ( $elements as $element ) {
228
+        // Ensure that all required elements exist.
229
+        $_elements = array();
230
+        foreach ( $elements as $element ) {
231 231
 
232
-			if ( $element['type'] == 'pay_button' && ! $this->has_element_type( 'gateway_select' ) ) {
232
+            if ( $element['type'] == 'pay_button' && ! $this->has_element_type( 'gateway_select' ) ) {
233 233
 
234
-				$_elements[] = array(
235
-					'text'        => __( 'Select Payment Method', 'invoicing' ),
236
-					'id'          => 'gtscicd',
237
-					'name'        => 'gtscicd',
238
-					'type'        => 'gateway_select',
239
-					'premade'     => true
234
+                $_elements[] = array(
235
+                    'text'        => __( 'Select Payment Method', 'invoicing' ),
236
+                    'id'          => 'gtscicd',
237
+                    'name'        => 'gtscicd',
238
+                    'type'        => 'gateway_select',
239
+                    'premade'     => true
240 240
 			
241
-				);
241
+                );
242 242
 
243
-			}
243
+            }
244 244
 
245
-			$_elements[] = $element;
245
+            $_elements[] = $element;
246 246
 
247
-		}
247
+        }
248 248
 
249 249
         return $_elements;
250
-	}
251
-
252
-	/**
253
-	 * Get the items sold via the form.
254
-	 *
255
-	 * @since 1.0.19
256
-	 * @param  string $context View or edit context.
257
-	 * @param  string $return objects or arrays.
258
-	 * @return GetPaid_Form_Item[]
259
-	 */
260
-	public function get_items( $context = 'view', $return = 'objects' ) {
261
-		$items = $this->get_prop( 'items', $context );
262
-
263
-		if ( empty( $items ) || ! is_array( $items ) ) {
250
+    }
251
+
252
+    /**
253
+     * Get the items sold via the form.
254
+     *
255
+     * @since 1.0.19
256
+     * @param  string $context View or edit context.
257
+     * @param  string $return objects or arrays.
258
+     * @return GetPaid_Form_Item[]
259
+     */
260
+    public function get_items( $context = 'view', $return = 'objects' ) {
261
+        $items = $this->get_prop( 'items', $context );
262
+
263
+        if ( empty( $items ) || ! is_array( $items ) ) {
264 264
             $items = wpinv_get_data( 'sample-payment-form-items' );
265
-		}
265
+        }
266
+
267
+        // Convert the items.
268
+        $prepared = array();
266 269
 
267
-		// Convert the items.
268
-		$prepared = array();
270
+        foreach ( $items as $key => $value ) {
269 271
 
270
-		foreach ( $items as $key => $value ) {
272
+            // Form items.
273
+            if ( $value instanceof GetPaid_Form_Item ) {
271 274
 
272
-			// Form items.
273
-			if ( $value instanceof GetPaid_Form_Item ) {
275
+                if ( $value->can_purchase() ) {
276
+                    $prepared[] = $value;
277
+                }
274 278
 
275
-				if ( $value->can_purchase() ) {
276
-					$prepared[] = $value;
277
-				}
279
+                continue;
278 280
 
279
-				continue;
281
+            }
280 282
 
281
-			}
283
+            // $item_id => $quantity (buy buttons)
284
+            if ( is_numeric( $key ) && is_numeric( $value ) ) {
285
+                $item = new GetPaid_Form_Item( $key );
282 286
 
283
-			// $item_id => $quantity (buy buttons)
284
-			if ( is_numeric( $key ) && is_numeric( $value ) ) {
285
-				$item = new GetPaid_Form_Item( $key );
287
+                if ( $item->can_purchase() ) {
288
+                    $item->set_quantity( $value );
289
+                    $prepared[] = $item;
290
+                }
286 291
 
287
-				if ( $item->can_purchase() ) {
288
-					$item->set_quantity( $value );
289
-					$prepared[] = $item;
290
-				}
292
+                continue;
293
+            }
291 294
 
292
-				continue;
293
-			}
295
+            // Items saved via payment forms editor.
296
+            if ( is_array( $value ) && isset( $value['id'] ) ) {
294 297
 
295
-			// Items saved via payment forms editor.
296
-			if ( is_array( $value ) && isset( $value['id'] ) ) {
298
+                $item = new GetPaid_Form_Item( $value['id'] );
297 299
 
298
-				$item = new GetPaid_Form_Item( $value['id'] );
300
+                if ( ! $item->can_purchase() ) {
301
+                    continue;
302
+                }
299 303
 
300
-				if ( ! $item->can_purchase() ) {
301
-					continue;
302
-				}
304
+                // Sub-total (Cart items).
305
+                if ( isset( $value['subtotal'] ) ) {
306
+                    $item->set_price( $value['subtotal'] );
307
+                }
303 308
 
304
-				// Sub-total (Cart items).
305
-				if ( isset( $value['subtotal'] ) ) {
306
-					$item->set_price( $value['subtotal'] );
307
-				}
309
+                if ( isset( $value['quantity'] ) ) {
310
+                    $item->set_quantity( $value['quantity'] );
311
+                }
308 312
 
309
-				if ( isset( $value['quantity'] ) ) {
310
-					$item->set_quantity( $value['quantity'] );
311
-				}
313
+                if ( isset( $value['allow_quantities'] ) ) {
314
+                    $item->set_allow_quantities( $value['allow_quantities'] );
315
+                }
312 316
 
313
-				if ( isset( $value['allow_quantities'] ) ) {
314
-					$item->set_allow_quantities( $value['allow_quantities'] );
315
-				}
317
+                if ( isset( $value['required'] ) ) {
318
+                    $item->set_is_required( $value['required'] );
319
+                }
316 320
 
317
-				if ( isset( $value['required'] ) ) {
318
-					$item->set_is_required( $value['required'] );
319
-				}
321
+                if ( isset( $value['description'] ) ) {
322
+                    $item->set_custom_description( $value['description'] );
323
+                }
320 324
 
321
-				if ( isset( $value['description'] ) ) {
322
-					$item->set_custom_description( $value['description'] );
323
-				}
325
+                $prepared[] = $item;
326
+                continue;
324 327
 
325
-				$prepared[] = $item;
326
-				continue;
328
+            }
329
+
330
+            // $item_id => array( 'price' => 10 ) (item variations)
331
+            if ( is_numeric( $key ) && is_array( $value ) ) {
332
+                $item = new GetPaid_Form_Item( $key );
333
+
334
+                if ( isset( $value['price'] ) && $item->user_can_set_their_price() ) {
335
+                    $item->set_price( $value['price'] );
336
+                }
337
+
338
+                if ( $item->can_purchase() ) {
339
+                    $prepared[] = $item;
340
+                }
341
+
342
+                continue;
343
+            }
344
+
345
+        }
346
+
347
+        if ( 'objects' == $return && 'view' == $context ) {
348
+            return $prepared;
349
+        }
350
+
351
+        $items = array();
352
+        foreach ( $prepared as $item ) {
353
+            $items[] = $item->prepare_data_for_use();
354
+        }
355
+
356
+        return $items;
357
+    }
358
+
359
+    /**
360
+     * Get a single item belonging to the form.
361
+     *
362
+     * @since 1.0.19
363
+     * @param  int $item_id The item id to return.
364
+     * @return GetPaid_Form_Item|bool
365
+     */
366
+    public function get_item( $item_id ) {
367
+
368
+        if ( empty( $item_id ) || ! is_numeric( $item_id ) ) {
369
+            return false;
370
+        }
371
+
372
+        foreach( $this->get_items() as $item ) {
373
+            if ( $item->get_id() == (int) $item_id ) {
374
+                return $item;
375
+            }
376
+        }
377
+
378
+        return false;
379
+
380
+    }
381
+
382
+    /**
383
+     * Gets a single element.
384
+     *
385
+     * @since 1.0.19
386
+     * @param  string $element_type The element type to return.
387
+     * @return array|bool
388
+     */
389
+    public function get_element_type( $element_type ) {
390
+
391
+        if ( empty( $element_type ) || ! is_scalar( $element_type ) ) {
392
+            return false;
393
+        }
394
+
395
+        foreach ( $this->get_prop( 'elements' ) as $element ) {
396
+
397
+            if ( $element['type'] == $element_type ) {
398
+                return $element;
399
+            }
400
+
401
+        }
402
+
403
+        return false;
404
+
405
+    }
406
+
407
+    /**
408
+     * Get the total amount earned via this form.
409
+     *
410
+     * @since 1.0.19
411
+     * @param  string $context View or edit context.
412
+     * @return array
413
+     */
414
+    public function get_earned( $context = 'view' ) {
415
+        return $this->get_prop( 'earned', $context );
416
+    }
417
+
418
+    /**
419
+     * Get the total amount refunded via this form.
420
+     *
421
+     * @since 1.0.19
422
+     * @param  string $context View or edit context.
423
+     * @return array
424
+     */
425
+    public function get_refunded( $context = 'view' ) {
426
+        return $this->get_prop( 'refunded', $context );
427
+    }
327 428
 
328
-			}
429
+    /**
430
+     * Get the total amount cancelled via this form.
431
+     *
432
+     * @since 1.0.19
433
+     * @param  string $context View or edit context.
434
+     * @return array
435
+     */
436
+    public function get_cancelled( $context = 'view' ) {
437
+        return $this->get_prop( 'cancelled', $context );
438
+    }
329 439
 
330
-			// $item_id => array( 'price' => 10 ) (item variations)
331
-			if ( is_numeric( $key ) && is_array( $value ) ) {
332
-				$item = new GetPaid_Form_Item( $key );
440
+    /**
441
+     * Get the total amount failed via this form.
442
+     *
443
+     * @since 1.0.19
444
+     * @param  string $context View or edit context.
445
+     * @return array
446
+     */
447
+    public function get_failed( $context = 'view' ) {
448
+        return $this->get_prop( 'failed', $context );
449
+    }
333 450
 
334
-				if ( isset( $value['price'] ) && $item->user_can_set_their_price() ) {
335
-					$item->set_price( $value['price'] );
336
-				}
337
-
338
-				if ( $item->can_purchase() ) {
339
-					$prepared[] = $item;
340
-				}
341
-
342
-				continue;
343
-			}
344
-
345
-		}
346
-
347
-		if ( 'objects' == $return && 'view' == $context ) {
348
-			return $prepared;
349
-		}
350
-
351
-		$items = array();
352
-		foreach ( $prepared as $item ) {
353
-			$items[] = $item->prepare_data_for_use();
354
-		}
355
-
356
-		return $items;
357
-	}
358
-
359
-	/**
360
-	 * Get a single item belonging to the form.
361
-	 *
362
-	 * @since 1.0.19
363
-	 * @param  int $item_id The item id to return.
364
-	 * @return GetPaid_Form_Item|bool
365
-	 */
366
-	public function get_item( $item_id ) {
367
-
368
-		if ( empty( $item_id ) || ! is_numeric( $item_id ) ) {
369
-			return false;
370
-		}
371
-
372
-		foreach( $this->get_items() as $item ) {
373
-			if ( $item->get_id() == (int) $item_id ) {
374
-				return $item;
375
-			}
376
-		}
377
-
378
-		return false;
379
-
380
-	}
381
-
382
-	/**
383
-	 * Gets a single element.
384
-	 *
385
-	 * @since 1.0.19
386
-	 * @param  string $element_type The element type to return.
387
-	 * @return array|bool
388
-	 */
389
-	public function get_element_type( $element_type ) {
390
-
391
-		if ( empty( $element_type ) || ! is_scalar( $element_type ) ) {
392
-			return false;
393
-		}
394
-
395
-		foreach ( $this->get_prop( 'elements' ) as $element ) {
396
-
397
-			if ( $element['type'] == $element_type ) {
398
-				return $element;
399
-			}
400
-
401
-		}
402
-
403
-		return false;
404
-
405
-	}
406
-
407
-	/**
408
-	 * Get the total amount earned via this form.
409
-	 *
410
-	 * @since 1.0.19
411
-	 * @param  string $context View or edit context.
412
-	 * @return array
413
-	 */
414
-	public function get_earned( $context = 'view' ) {
415
-		return $this->get_prop( 'earned', $context );
416
-	}
417
-
418
-	/**
419
-	 * Get the total amount refunded via this form.
420
-	 *
421
-	 * @since 1.0.19
422
-	 * @param  string $context View or edit context.
423
-	 * @return array
424
-	 */
425
-	public function get_refunded( $context = 'view' ) {
426
-		return $this->get_prop( 'refunded', $context );
427
-	}
428
-
429
-	/**
430
-	 * Get the total amount cancelled via this form.
431
-	 *
432
-	 * @since 1.0.19
433
-	 * @param  string $context View or edit context.
434
-	 * @return array
435
-	 */
436
-	public function get_cancelled( $context = 'view' ) {
437
-		return $this->get_prop( 'cancelled', $context );
438
-	}
439
-
440
-	/**
441
-	 * Get the total amount failed via this form.
442
-	 *
443
-	 * @since 1.0.19
444
-	 * @param  string $context View or edit context.
445
-	 * @return array
446
-	 */
447
-	public function get_failed( $context = 'view' ) {
448
-		return $this->get_prop( 'failed', $context );
449
-	}
450
-
451
-	/**
452
-	 * Get the currency.
453
-	 *
454
-	 * @since 1.0.19
455
-	 * @param  string $context View or edit context.
456
-	 * @return string
457
-	 */
458
-	public function get_currency() {
459
-		$currency = empty( $this->invoice ) ? wpinv_get_currency() : $this->invoice->get_currency();
460
-		return apply_filters( 'getpaid-payment-form-currency', $currency, $this );
461
-	}
451
+    /**
452
+     * Get the currency.
453
+     *
454
+     * @since 1.0.19
455
+     * @param  string $context View or edit context.
456
+     * @return string
457
+     */
458
+    public function get_currency() {
459
+        $currency = empty( $this->invoice ) ? wpinv_get_currency() : $this->invoice->get_currency();
460
+        return apply_filters( 'getpaid-payment-form-currency', $currency, $this );
461
+    }
462 462
 
463 463
     /*
464 464
 	|--------------------------------------------------------------------------
@@ -471,22 +471,22 @@  discard block
 block discarded – undo
471 471
     */
472 472
 
473 473
     /**
474
-	 * Set plugin version when the item was created.
475
-	 *
476
-	 * @since 1.0.19
477
-	 */
478
-	public function set_version( $value ) {
479
-		$this->set_prop( 'version', $value );
474
+     * Set plugin version when the item was created.
475
+     *
476
+     * @since 1.0.19
477
+     */
478
+    public function set_version( $value ) {
479
+        $this->set_prop( 'version', $value );
480 480
     }
481 481
 
482 482
     /**
483
-	 * Set date when the item was created.
484
-	 *
485
-	 * @since 1.0.19
486
-	 * @param string $value Value to set.
483
+     * Set date when the item was created.
484
+     *
485
+     * @since 1.0.19
486
+     * @param string $value Value to set.
487 487
      * @return bool Whether or not the date was set.
488
-	 */
489
-	public function set_date_created( $value ) {
488
+     */
489
+    public function set_date_created( $value ) {
490 490
         $date = strtotime( $value );
491 491
 
492 492
         if ( $date ) {
@@ -499,13 +499,13 @@  discard block
 block discarded – undo
499 499
     }
500 500
 
501 501
     /**
502
-	 * Set date when the item was last modified.
503
-	 *
504
-	 * @since 1.0.19
505
-	 * @param string $value Value to set.
502
+     * Set date when the item was last modified.
503
+     *
504
+     * @since 1.0.19
505
+     * @param string $value Value to set.
506 506
      * @return bool Whether or not the date was set.
507
-	 */
508
-	public function set_date_modified( $value ) {
507
+     */
508
+    public function set_date_modified( $value ) {
509 509
         $date = strtotime( $value );
510 510
 
511 511
         if ( $date ) {
@@ -518,118 +518,118 @@  discard block
 block discarded – undo
518 518
     }
519 519
 
520 520
     /**
521
-	 * Set the item name.
522
-	 *
523
-	 * @since 1.0.19
524
-	 * @param  string $value New name.
525
-	 */
526
-	public function set_name( $value ) {
527
-		$this->set_prop( 'name', sanitize_text_field( $value ) );
528
-    }
529
-
530
-    /**
531
-	 * Alias of self::set_name().
532
-	 *
533
-	 * @since 1.0.19
534
-	 * @param  string $value New name.
535
-	 */
536
-	public function set_title( $value ) {
537
-		$this->set_name( $value );
538
-    }
539
-
540
-    /**
541
-	 * Set the owner of the item.
542
-	 *
543
-	 * @since 1.0.19
544
-	 * @param  int $value New author.
545
-	 */
546
-	public function set_author( $value ) {
547
-		$this->set_prop( 'author', (int) $value );
548
-	}
549
-
550
-	/**
551
-	 * Set the form elements.
552
-	 *
553
-	 * @since 1.0.19
554
-	 * @param  array $value Form elements.
555
-	 */
556
-	public function set_elements( $value ) {
557
-		if ( is_array( $value ) ) {
558
-			$this->set_prop( 'elements', $value );
559
-		}
560
-	}
561
-
562
-	/**
563
-	 * Set the form items.
564
-	 *
565
-	 * @since 1.0.19
566
-	 * @param  array $value Form elements.
567
-	 */
568
-	public function set_items( $value ) {
569
-		if ( is_array( $value ) ) {
570
-			$this->set_prop( 'items', $value );
571
-		}
572
-	}
573
-
574
-	/**
575
-	 * Set the total amount earned via this form.
576
-	 *
577
-	 * @since 1.0.19
578
-	 * @param  float $value Amount earned.
579
-	 */
580
-	public function set_earned( $value ) {
581
-		$value = max( (float) $value, 0 );
582
-		$this->set_prop( 'earned', $value );
583
-	}
584
-
585
-	/**
586
-	 * Set the total amount refunded via this form.
587
-	 *
588
-	 * @since 1.0.19
589
-	 * @param  float $value Amount refunded.
590
-	 */
591
-	public function set_refunded( $value ) {
592
-		$value = max( (float) $value, 0 );
593
-		$this->set_prop( 'refunded', $value );
594
-	}
595
-
596
-	/**
597
-	 * Set the total amount cancelled via this form.
598
-	 *
599
-	 * @since 1.0.19
600
-	 * @param  float $value Amount cancelled.
601
-	 */
602
-	public function set_cancelled( $value ) {
603
-		$value = max( (float) $value, 0 );
604
-		$this->set_prop( 'cancelled', $value );
605
-	}
606
-
607
-	/**
608
-	 * Set the total amount failed via this form.
609
-	 *
610
-	 * @since 1.0.19
611
-	 * @param  float $value Amount cancelled.
612
-	 */
613
-	public function set_failed( $value ) {
614
-		$value = max( (float) $value, 0 );
615
-		$this->set_prop( 'failed', $value );
616
-	}
521
+     * Set the item name.
522
+     *
523
+     * @since 1.0.19
524
+     * @param  string $value New name.
525
+     */
526
+    public function set_name( $value ) {
527
+        $this->set_prop( 'name', sanitize_text_field( $value ) );
528
+    }
529
+
530
+    /**
531
+     * Alias of self::set_name().
532
+     *
533
+     * @since 1.0.19
534
+     * @param  string $value New name.
535
+     */
536
+    public function set_title( $value ) {
537
+        $this->set_name( $value );
538
+    }
539
+
540
+    /**
541
+     * Set the owner of the item.
542
+     *
543
+     * @since 1.0.19
544
+     * @param  int $value New author.
545
+     */
546
+    public function set_author( $value ) {
547
+        $this->set_prop( 'author', (int) $value );
548
+    }
549
+
550
+    /**
551
+     * Set the form elements.
552
+     *
553
+     * @since 1.0.19
554
+     * @param  array $value Form elements.
555
+     */
556
+    public function set_elements( $value ) {
557
+        if ( is_array( $value ) ) {
558
+            $this->set_prop( 'elements', $value );
559
+        }
560
+    }
561
+
562
+    /**
563
+     * Set the form items.
564
+     *
565
+     * @since 1.0.19
566
+     * @param  array $value Form elements.
567
+     */
568
+    public function set_items( $value ) {
569
+        if ( is_array( $value ) ) {
570
+            $this->set_prop( 'items', $value );
571
+        }
572
+    }
573
+
574
+    /**
575
+     * Set the total amount earned via this form.
576
+     *
577
+     * @since 1.0.19
578
+     * @param  float $value Amount earned.
579
+     */
580
+    public function set_earned( $value ) {
581
+        $value = max( (float) $value, 0 );
582
+        $this->set_prop( 'earned', $value );
583
+    }
584
+
585
+    /**
586
+     * Set the total amount refunded via this form.
587
+     *
588
+     * @since 1.0.19
589
+     * @param  float $value Amount refunded.
590
+     */
591
+    public function set_refunded( $value ) {
592
+        $value = max( (float) $value, 0 );
593
+        $this->set_prop( 'refunded', $value );
594
+    }
595
+
596
+    /**
597
+     * Set the total amount cancelled via this form.
598
+     *
599
+     * @since 1.0.19
600
+     * @param  float $value Amount cancelled.
601
+     */
602
+    public function set_cancelled( $value ) {
603
+        $value = max( (float) $value, 0 );
604
+        $this->set_prop( 'cancelled', $value );
605
+    }
606
+
607
+    /**
608
+     * Set the total amount failed via this form.
609
+     *
610
+     * @since 1.0.19
611
+     * @param  float $value Amount cancelled.
612
+     */
613
+    public function set_failed( $value ) {
614
+        $value = max( (float) $value, 0 );
615
+        $this->set_prop( 'failed', $value );
616
+    }
617 617
 
618 618
     /**
619 619
      * Create an item. For backwards compatibilty.
620 620
      *
621 621
      * @deprecated
622
-	 * @return int item id
622
+     * @return int item id
623 623
      */
624 624
     public function create( $data = array() ) {
625 625
 
626
-		// Set the properties.
627
-		if ( is_array( $data ) ) {
628
-			$this->set_props( $data );
629
-		}
626
+        // Set the properties.
627
+        if ( is_array( $data ) ) {
628
+            $this->set_props( $data );
629
+        }
630 630
 
631
-		// Save the item.
632
-		return $this->save();
631
+        // Save the item.
632
+        return $this->save();
633 633
 
634 634
     }
635 635
 
@@ -637,7 +637,7 @@  discard block
 block discarded – undo
637 637
      * Updates an item. For backwards compatibilty.
638 638
      *
639 639
      * @deprecated
640
-	 * @return int item id
640
+     * @return int item id
641 641
      */
642 642
     public function update( $data = array() ) {
643 643
         return $this->create( $data );
@@ -653,22 +653,22 @@  discard block
 block discarded – undo
653 653
 	*/
654 654
 
655 655
     /**
656
-	 * Checks whether this is the default payment form.
657
-	 *
658
-	 * @since 1.0.19
659
-	 * @return bool
660
-	 */
656
+     * Checks whether this is the default payment form.
657
+     *
658
+     * @since 1.0.19
659
+     * @return bool
660
+     */
661 661
     public function is_default() {
662 662
         $is_default = $this->get_id() == wpinv_get_default_payment_form();
663 663
         return (bool) apply_filters( 'wpinv_is_default_payment_form', $is_default, $this->get_id(), $this );
664
-	}
664
+    }
665 665
 
666 666
     /**
667
-	 * Checks whether the form is active.
668
-	 *
669
-	 * @since 1.0.19
670
-	 * @return bool
671
-	 */
667
+     * Checks whether the form is active.
668
+     *
669
+     * @since 1.0.19
670
+     * @return bool
671
+     */
672 672
     public function is_active() {
673 673
         $is_active = 0 !== (int) $this->get_id();
674 674
 
@@ -677,70 +677,70 @@  discard block
 block discarded – undo
677 677
         }
678 678
 
679 679
         return (bool) apply_filters( 'wpinv_is_payment_form_active', $is_active, $this );
680
-	}
681
-
682
-	/**
683
-	 * Checks whether the form has a given item.
684
-	 *
685
-	 * @since 1.0.19
686
-	 * @return bool
687
-	 */
680
+    }
681
+
682
+    /**
683
+     * Checks whether the form has a given item.
684
+     *
685
+     * @since 1.0.19
686
+     * @return bool
687
+     */
688 688
     public function has_item( $item_id ) {
689 689
         return false !== $this->get_item( $item_id );
690
-	}
691
-
692
-	/**
693
-	 * Checks whether the form has a given element.
694
-	 *
695
-	 * @since 1.0.19
696
-	 * @return bool
697
-	 */
690
+    }
691
+
692
+    /**
693
+     * Checks whether the form has a given element.
694
+     *
695
+     * @since 1.0.19
696
+     * @return bool
697
+     */
698 698
     public function has_element_type( $element_type ) {
699 699
         return false !== $this->get_element_type( $element_type );
700
-	}
701
-
702
-	/**
703
-	 * Checks whether this form is recurring or not.
704
-	 *
705
-	 * @since 1.0.19
706
-	 * @return bool
707
-	 */
700
+    }
701
+
702
+    /**
703
+     * Checks whether this form is recurring or not.
704
+     *
705
+     * @since 1.0.19
706
+     * @return bool
707
+     */
708 708
     public function is_recurring() {
709 709
 
710
-		if ( ! empty( $this->invoice ) ) {
711
-			return $this->invoice->is_recurring();
712
-		}
710
+        if ( ! empty( $this->invoice ) ) {
711
+            return $this->invoice->is_recurring();
712
+        }
713 713
 
714
-		foreach ( $this->get_items() as $item ) {
714
+        foreach ( $this->get_items() as $item ) {
715 715
 
716
-			if ( $item->is_recurring() ) {
717
-				return true;
718
-			}
716
+            if ( $item->is_recurring() ) {
717
+                return true;
718
+            }
719 719
 
720
-		}
720
+        }
721 721
 
722 722
         return false;
723
-	}
723
+    }
724 724
 
725
-	/**
726
-	 * Retrieves the form's html.
727
-	 *
728
-	 * @since 1.0.19
729
-	 */
725
+    /**
726
+     * Retrieves the form's html.
727
+     *
728
+     * @since 1.0.19
729
+     */
730 730
     public function get_html() {
731 731
 
732
-		// Return the HTML.
733
-		return wpinv_get_template_html( 'payment-forms/form.php', array( 'form' => $this ) );
732
+        // Return the HTML.
733
+        return wpinv_get_template_html( 'payment-forms/form.php', array( 'form' => $this ) );
734 734
 
735
-	}
735
+    }
736 736
 
737
-	/**
738
-	 * Displays the payment form.
739
-	 *
740
-	 * @since 1.0.19
741
-	 */
737
+    /**
738
+     * Displays the payment form.
739
+     *
740
+     * @since 1.0.19
741
+     */
742 742
     public function display() {
743
-		echo $this->get_html();
743
+        echo $this->get_html();
744 744
     }
745 745
 
746 746
 }
Please login to merge, or discard this patch.
includes/payments/class-getpaid-payment-form-submission-discount.php 1 patch
Indentation   +181 added lines, -181 removed lines patch added patch discarded remove patch
@@ -12,189 +12,189 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Payment_Form_Submission_Discount {
14 14
 
15
-	/**
16
-	 * Submission discounts.
17
-	 * @var array
18
-	 */
19
-	public $discounts = array();
15
+    /**
16
+     * Submission discounts.
17
+     * @var array
18
+     */
19
+    public $discounts = array();
20
+
21
+    /**
22
+     * Class constructor
23
+     *
24
+     * @param GetPaid_Payment_Form_Submission $submission
25
+     * @param float                           $initial_total
26
+     * @param float                           $recurring_total
27
+     */
28
+    public function __construct( $submission, $initial_total, $recurring_total ) {
29
+
30
+        // Process any existing invoice discounts.
31
+        if ( $submission->has_invoice() ) {
32
+            $this->discounts = $submission->get_invoice()->get_discounts();
33
+        }
34
+
35
+        // Do we have a discount?
36
+        $discount = $submission->get_field( 'discount' );
37
+
38
+        if ( empty( $discount ) ) {
39
+
40
+            if ( isset( $this->discounts['discount_code'] ) ) {
41
+                unset( $this->discounts['discount_code'] );
42
+            }
43
+
44
+            return;
45
+        }
46
+
47
+        // Processes the discount code.
48
+        $amount = max( $initial_total, $recurring_total );
49
+        $this->process_discount( $submission, $discount, $amount );
50
+
51
+    }
20 52
 
21 53
     /**
22
-	 * Class constructor
23
-	 *
24
-	 * @param GetPaid_Payment_Form_Submission $submission
25
-	 * @param float                           $initial_total
26
-	 * @param float                           $recurring_total
27
-	 */
28
-	public function __construct( $submission, $initial_total, $recurring_total ) {
29
-
30
-		// Process any existing invoice discounts.
31
-		if ( $submission->has_invoice() ) {
32
-			$this->discounts = $submission->get_invoice()->get_discounts();
33
-		}
34
-
35
-		// Do we have a discount?
36
-		$discount = $submission->get_field( 'discount' );
37
-
38
-		if ( empty( $discount ) ) {
39
-
40
-			if ( isset( $this->discounts['discount_code'] ) ) {
41
-				unset( $this->discounts['discount_code'] );
42
-			}
43
-
44
-			return;
45
-		}
46
-
47
-		// Processes the discount code.
48
-		$amount = max( $initial_total, $recurring_total );
49
-		$this->process_discount( $submission, $discount, $amount );
50
-
51
-	}
52
-
53
-	/**
54
-	 * Processes a submission discount.
55
-	 *
56
-	 * @param GetPaid_Payment_Form_Submission $submission
57
-	 * @param string                          $discount
58
-	 * @param float                           $amount
59
-	 */
60
-	public function process_discount( $submission, $discount, $amount ) {
61
-
62
-		// Fetch the discount.
63
-		$discount = new WPInv_Discount( $discount );
64
-
65
-		// Ensure it is active.
54
+     * Processes a submission discount.
55
+     *
56
+     * @param GetPaid_Payment_Form_Submission $submission
57
+     * @param string                          $discount
58
+     * @param float                           $amount
59
+     */
60
+    public function process_discount( $submission, $discount, $amount ) {
61
+
62
+        // Fetch the discount.
63
+        $discount = new WPInv_Discount( $discount );
64
+
65
+        // Ensure it is active.
66 66
         if ( ! $this->is_discount_active( $discount ) ) {
67
-			throw new Exception( __( 'Invalid or expired discount code', 'invoicing' ) );
68
-		}
69
-
70
-		// Exceeded limit.
71
-		if ( $discount->has_exceeded_limit() ) {
72
-			throw new Exception( __( 'This discount code has been used up', 'invoicing' ) );
73
-		}
74
-
75
-		// Validate usages.
76
-		$this->validate_single_use_discount( $submission, $discount );
77
-
78
-		// Validate amount.
79
-		$this->validate_discount_amount( $submission, $discount, $amount );
80
-
81
-		// Save the discount.
82
-		$this->discounts['discount_code'] = $this->calculate_discount( $submission, $discount );
83
-	}
84
-
85
-	/**
86
-	 * Validates a single use discount.
87
-	 *
88
-	 * @param WPInv_Discount                  $discount
89
-	 * @return bool
90
-	 */
91
-	public function is_discount_active(  $discount ) {
92
-		return $discount->exists() && $discount->is_active() && $discount->has_started() && ! $discount->is_expired();
93
-	}
94
-
95
-	/**
96
-	 * Returns a user's id or email.
97
-	 *
98
-	 * @param string $email
99
-	 * @return int|string|false
100
-	 */
101
-	public function get_user_id_or_email( $email ) {
102
-
103
-		if ( is_user_logged_in() ) {
104
-			return get_current_user_id();
105
-		}
106
-
107
-		return empty( $email ) ? false : sanitize_email( $email );
108
-	}
109
-
110
-	/**
111
-	 * Validates a single use discount.
112
-	 *
113
-	 * @param GetPaid_Payment_Form_Submission $submission
114
-	 * @param WPInv_Discount                  $discount
115
-	 */
116
-	public function validate_single_use_discount( $submission, $discount ) {
117
-
118
-		// Abort if it is not a single use discount.
119
-		if ( ! $discount->is_single_use() ) {
120
-			return;
121
-		}
122
-
123
-		// Ensure there is a valid billing email.
124
-		$user = $this->get_user_id_or_email( $submission->get_billing_email() );
125
-
126
-		if ( empty( $user ) ) {
127
-			throw new Exception( __( 'You need to either log in or enter your billing email before applying this discount', 'invoicing' ) );
128
-		}
129
-
130
-		// Has the user used this discount code before?
131
-		if ( ! $discount->is_valid_for_user( $user ) ) {
132
-			throw new Exception( __( 'You have already used this discount', 'invoicing' ) );
133
-		}
134
-
135
-	}
136
-
137
-	/**
138
-	 * Validates the discount's amount.
139
-	 *
140
-	 * @param GetPaid_Payment_Form_Submission $submission
141
-	 * @param WPInv_Discount         $discount
142
-	 * @param float                  $amount
143
-	 */
144
-	public function validate_discount_amount( $submission, $discount, $amount ) {
145
-
146
-		// Validate minimum amount.
147
-		if ( ! $discount->is_minimum_amount_met( $amount ) ) {
148
-			$min = wpinv_price( $discount->get_minimum_total(), $submission->get_currency() );
149
-			throw new Exception( sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ) );
150
-		}
151
-
152
-		// Validate the maximum amount.
153
-		if ( ! $discount->is_maximum_amount_met( $amount ) ) {
154
-			$max = wpinv_price( $discount->get_maximum_total(), $submission->get_currency() );
155
-			throw new Exception( sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ) );
156
-		}
157
-
158
-	}
159
-
160
-	/**
161
-	 * Calculates the discount code's amount.
162
-	 *
163
-	 * Ensure that the discount exists and has been validated before calling this method.
164
-	 *
165
-	 * @param GetPaid_Payment_Form_Submission $submission
166
-	 * @param WPInv_Discount                  $discount
167
-	 * @return array
168
-	 */
169
-	public function calculate_discount( $submission, $discount ) {
170
-
171
-		$initial_discount   = 0;
172
-		$recurring_discount = 0;
173
-
174
-		foreach ( $submission->get_items() as $item ) {
175
-
176
-			// Abort if it is not valid for this item.
177
-			if ( ! $discount->is_valid_for_items( array( $item->get_id() ) ) ) {
178
-				continue;
179
-			}
180
-
181
-			// Calculate the initial amount...
182
-			$initial_discount += $discount->get_discounted_amount( $item->get_sub_total() );
183
-
184
-			// ... and maybe the recurring amount.
185
-			if ( $item->is_recurring() && $discount->is_recurring() ) {
186
-				$recurring_discount += $discount->get_discounted_amount( $item->get_recurring_sub_total() );
187
-			}
188
-
189
-		}
190
-
191
-		return array(
192
-			'name'               => 'discount_code',
193
-			'discount_code'      => $discount->get_code(),
194
-			'initial_discount'   => $initial_discount,
195
-			'recurring_discount' => $recurring_discount,
196
-		);
197
-
198
-	}
67
+            throw new Exception( __( 'Invalid or expired discount code', 'invoicing' ) );
68
+        }
69
+
70
+        // Exceeded limit.
71
+        if ( $discount->has_exceeded_limit() ) {
72
+            throw new Exception( __( 'This discount code has been used up', 'invoicing' ) );
73
+        }
74
+
75
+        // Validate usages.
76
+        $this->validate_single_use_discount( $submission, $discount );
77
+
78
+        // Validate amount.
79
+        $this->validate_discount_amount( $submission, $discount, $amount );
80
+
81
+        // Save the discount.
82
+        $this->discounts['discount_code'] = $this->calculate_discount( $submission, $discount );
83
+    }
84
+
85
+    /**
86
+     * Validates a single use discount.
87
+     *
88
+     * @param WPInv_Discount                  $discount
89
+     * @return bool
90
+     */
91
+    public function is_discount_active(  $discount ) {
92
+        return $discount->exists() && $discount->is_active() && $discount->has_started() && ! $discount->is_expired();
93
+    }
94
+
95
+    /**
96
+     * Returns a user's id or email.
97
+     *
98
+     * @param string $email
99
+     * @return int|string|false
100
+     */
101
+    public function get_user_id_or_email( $email ) {
102
+
103
+        if ( is_user_logged_in() ) {
104
+            return get_current_user_id();
105
+        }
106
+
107
+        return empty( $email ) ? false : sanitize_email( $email );
108
+    }
109
+
110
+    /**
111
+     * Validates a single use discount.
112
+     *
113
+     * @param GetPaid_Payment_Form_Submission $submission
114
+     * @param WPInv_Discount                  $discount
115
+     */
116
+    public function validate_single_use_discount( $submission, $discount ) {
117
+
118
+        // Abort if it is not a single use discount.
119
+        if ( ! $discount->is_single_use() ) {
120
+            return;
121
+        }
122
+
123
+        // Ensure there is a valid billing email.
124
+        $user = $this->get_user_id_or_email( $submission->get_billing_email() );
125
+
126
+        if ( empty( $user ) ) {
127
+            throw new Exception( __( 'You need to either log in or enter your billing email before applying this discount', 'invoicing' ) );
128
+        }
129
+
130
+        // Has the user used this discount code before?
131
+        if ( ! $discount->is_valid_for_user( $user ) ) {
132
+            throw new Exception( __( 'You have already used this discount', 'invoicing' ) );
133
+        }
134
+
135
+    }
136
+
137
+    /**
138
+     * Validates the discount's amount.
139
+     *
140
+     * @param GetPaid_Payment_Form_Submission $submission
141
+     * @param WPInv_Discount         $discount
142
+     * @param float                  $amount
143
+     */
144
+    public function validate_discount_amount( $submission, $discount, $amount ) {
145
+
146
+        // Validate minimum amount.
147
+        if ( ! $discount->is_minimum_amount_met( $amount ) ) {
148
+            $min = wpinv_price( $discount->get_minimum_total(), $submission->get_currency() );
149
+            throw new Exception( sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ) );
150
+        }
151
+
152
+        // Validate the maximum amount.
153
+        if ( ! $discount->is_maximum_amount_met( $amount ) ) {
154
+            $max = wpinv_price( $discount->get_maximum_total(), $submission->get_currency() );
155
+            throw new Exception( sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ) );
156
+        }
157
+
158
+    }
159
+
160
+    /**
161
+     * Calculates the discount code's amount.
162
+     *
163
+     * Ensure that the discount exists and has been validated before calling this method.
164
+     *
165
+     * @param GetPaid_Payment_Form_Submission $submission
166
+     * @param WPInv_Discount                  $discount
167
+     * @return array
168
+     */
169
+    public function calculate_discount( $submission, $discount ) {
170
+
171
+        $initial_discount   = 0;
172
+        $recurring_discount = 0;
173
+
174
+        foreach ( $submission->get_items() as $item ) {
175
+
176
+            // Abort if it is not valid for this item.
177
+            if ( ! $discount->is_valid_for_items( array( $item->get_id() ) ) ) {
178
+                continue;
179
+            }
180
+
181
+            // Calculate the initial amount...
182
+            $initial_discount += $discount->get_discounted_amount( $item->get_sub_total() );
183
+
184
+            // ... and maybe the recurring amount.
185
+            if ( $item->is_recurring() && $discount->is_recurring() ) {
186
+                $recurring_discount += $discount->get_discounted_amount( $item->get_recurring_sub_total() );
187
+            }
188
+
189
+        }
190
+
191
+        return array(
192
+            'name'               => 'discount_code',
193
+            'discount_code'      => $discount->get_code(),
194
+            'initial_discount'   => $initial_discount,
195
+            'recurring_discount' => $recurring_discount,
196
+        );
197
+
198
+    }
199 199
 
200 200
 }
Please login to merge, or discard this patch.
includes/data-stores/class-getpaid-invoice-data-store.php 1 patch
Indentation   +449 added lines, -449 removed lines patch added patch discarded remove patch
@@ -5,7 +5,7 @@  discard block
 block discarded – undo
5 5
  *
6 6
  */
7 7
 if ( ! defined( 'ABSPATH' ) ) {
8
-	exit;
8
+    exit;
9 9
 }
10 10
 
11 11
 /**
@@ -15,519 +15,519 @@  discard block
 block discarded – undo
15 15
  */
16 16
 class GetPaid_Invoice_Data_Store extends GetPaid_Data_Store_WP {
17 17
 
18
-	/**
19
-	 * Data stored in meta keys, but not considered "meta" for a discount.
20
-	 *
21
-	 * @since 1.0.19
22
-	 * @var array
23
-	 */
24
-	protected $internal_meta_keys = array(
25
-		'_wpinv_subscr_profile_id',
26
-		'_wpinv_subscription_id',
27
-		'_wpinv_taxes',
28
-		'_wpinv_fees',
29
-		'_wpinv_discounts',
30
-		'_wpinv_submission_id',
31
-		'_wpinv_payment_form',
32
-		'_wpinv_is_viewed',
33
-		'wpinv_email_cc',
34
-		'wpinv_template',
35
-		'wpinv_created_via'
36
-	);
37
-
38
-	/**
39
-	 * A map of meta keys to data props.
40
-	 *
41
-	 * @since 1.0.19
42
-	 *
43
-	 * @var array
44
-	 */
45
-	protected $meta_key_to_props = array(
46
-		'_wpinv_subscr_profile_id' => 'remote_subscription_id',
47
-		'_wpinv_subscription_id'   => 'subscription_id',
48
-		'_wpinv_taxes'             => 'taxes',
49
-		'_wpinv_fees'              => 'fees',
50
-		'_wpinv_discounts'         => 'discounts',
51
-		'_wpinv_submission_id'     => 'submission_id',
52
-		'_wpinv_payment_form'      => 'payment_form',
53
-		'_wpinv_is_viewed'         => 'is_viewed',
54
-		'wpinv_email_cc'           => 'email_cc',
55
-		'wpinv_template'           => 'template',
56
-		'wpinv_created_via'        => 'created_via',
57
-	);
58
-
59
-	/**
60
-	 * A map of database fields to data props.
61
-	 *
62
-	 * @since 1.0.19
63
-	 *
64
-	 * @var array
65
-	 */
66
-	protected $database_fields_to_props = array(
67
-		'post_id'            => 'id',
68
-		'number'             => 'number',
69
-		'currency'           => 'currency',
70
-		'key'                => 'key',
71
-		'type'               => 'type',
72
-		'mode'               => 'mode',
73
-		'user_ip'            => 'user_ip',
74
-		'first_name'         => 'first_name',
75
-		'last_name'          => 'last_name',
76
-		'address'            => 'address',
77
-		'city'               => 'city',
78
-		'state'              => 'state',
79
-		'country'            => 'country',
80
-		'zip'                => 'zip',
81
-		'zip'                => 'zip',
82
-		'adddress_confirmed' => 'address_confirmed',
83
-		'gateway'            => 'gateway',
84
-		'transaction_id'     => 'transaction_id',
85
-		'currency'           => 'currency',
86
-		'subtotal'           => 'subtotal',
87
-		'tax'                => 'total_tax',
88
-		'fees_total'         => 'total_fees',
89
-		'discount'           => 'total_discount',
90
-		'total'              => 'total',
91
-		'discount_code'      => 'discount_code',
92
-		'disable_taxes'      => 'disable_taxes',
93
-		'due_date'           => 'due_date',
94
-		'completed_date'     => 'completed_date',
95
-		'company'            => 'company',
96
-		'vat_number'         => 'vat_number',
97
-		'vat_rate'           => 'vat_rate',
98
-	);
99
-
100
-	/*
18
+    /**
19
+     * Data stored in meta keys, but not considered "meta" for a discount.
20
+     *
21
+     * @since 1.0.19
22
+     * @var array
23
+     */
24
+    protected $internal_meta_keys = array(
25
+        '_wpinv_subscr_profile_id',
26
+        '_wpinv_subscription_id',
27
+        '_wpinv_taxes',
28
+        '_wpinv_fees',
29
+        '_wpinv_discounts',
30
+        '_wpinv_submission_id',
31
+        '_wpinv_payment_form',
32
+        '_wpinv_is_viewed',
33
+        'wpinv_email_cc',
34
+        'wpinv_template',
35
+        'wpinv_created_via'
36
+    );
37
+
38
+    /**
39
+     * A map of meta keys to data props.
40
+     *
41
+     * @since 1.0.19
42
+     *
43
+     * @var array
44
+     */
45
+    protected $meta_key_to_props = array(
46
+        '_wpinv_subscr_profile_id' => 'remote_subscription_id',
47
+        '_wpinv_subscription_id'   => 'subscription_id',
48
+        '_wpinv_taxes'             => 'taxes',
49
+        '_wpinv_fees'              => 'fees',
50
+        '_wpinv_discounts'         => 'discounts',
51
+        '_wpinv_submission_id'     => 'submission_id',
52
+        '_wpinv_payment_form'      => 'payment_form',
53
+        '_wpinv_is_viewed'         => 'is_viewed',
54
+        'wpinv_email_cc'           => 'email_cc',
55
+        'wpinv_template'           => 'template',
56
+        'wpinv_created_via'        => 'created_via',
57
+    );
58
+
59
+    /**
60
+     * A map of database fields to data props.
61
+     *
62
+     * @since 1.0.19
63
+     *
64
+     * @var array
65
+     */
66
+    protected $database_fields_to_props = array(
67
+        'post_id'            => 'id',
68
+        'number'             => 'number',
69
+        'currency'           => 'currency',
70
+        'key'                => 'key',
71
+        'type'               => 'type',
72
+        'mode'               => 'mode',
73
+        'user_ip'            => 'user_ip',
74
+        'first_name'         => 'first_name',
75
+        'last_name'          => 'last_name',
76
+        'address'            => 'address',
77
+        'city'               => 'city',
78
+        'state'              => 'state',
79
+        'country'            => 'country',
80
+        'zip'                => 'zip',
81
+        'zip'                => 'zip',
82
+        'adddress_confirmed' => 'address_confirmed',
83
+        'gateway'            => 'gateway',
84
+        'transaction_id'     => 'transaction_id',
85
+        'currency'           => 'currency',
86
+        'subtotal'           => 'subtotal',
87
+        'tax'                => 'total_tax',
88
+        'fees_total'         => 'total_fees',
89
+        'discount'           => 'total_discount',
90
+        'total'              => 'total',
91
+        'discount_code'      => 'discount_code',
92
+        'disable_taxes'      => 'disable_taxes',
93
+        'due_date'           => 'due_date',
94
+        'completed_date'     => 'completed_date',
95
+        'company'            => 'company',
96
+        'vat_number'         => 'vat_number',
97
+        'vat_rate'           => 'vat_rate',
98
+    );
99
+
100
+    /*
101 101
 	|--------------------------------------------------------------------------
102 102
 	| CRUD Methods
103 103
 	|--------------------------------------------------------------------------
104 104
 	*/
105 105
 
106
-	/**
107
-	 * Method to create a new invoice in the database.
108
-	 *
109
-	 * @param WPInv_Invoice $invoice Invoice object.
110
-	 */
111
-	public function create( &$invoice ) {
112
-		$invoice->set_version( WPINV_VERSION );
113
-		$invoice->set_date_created( current_time('mysql') );
114
-
115
-		// Create a new post.
116
-		$id = wp_insert_post(
117
-			apply_filters(
118
-				'getpaid_new_invoice_data',
119
-				array(
120
-					'post_date'     => $invoice->get_date_created( 'edit' ),
121
-					'post_type'     => $invoice->get_post_type( 'edit' ),
122
-					'post_status'   => $this->get_post_status( $invoice ),
123
-					'ping_status'   => 'closed',
124
-					'post_author'   => $invoice->get_user_id( 'edit' ),
125
-					'post_title'    => $invoice->get_title( 'edit' ),
126
-					'post_excerpt'  => $invoice->get_description( 'edit' ),
127
-					'post_parent'   => $invoice->get_parent_id( 'edit' ),
128
-				)
129
-			),
130
-			true
131
-		);
132
-
133
-		if ( $id && ! is_wp_error( $id ) ) {
134
-
135
-			// Update the new id and regenerate a title.
136
-			$invoice->set_id( $id );
137
-
138
-			$invoice->maybe_set_number();
139
-
140
-			wp_update_post(
141
-				array(
142
-					'ID'         => $invoice->get_id(),
143
-					'post_title' => $invoice->get_number( 'edit' ),
144
-					'post_name'  => $invoice->get_path( 'edit' )
145
-				)
146
-			);
147
-
148
-			// Save special fields and items.
149
-			$this->save_special_fields( $invoice );
150
-			$this->save_items( $invoice );
151
-
152
-			// Update meta data.
153
-			$this->update_post_meta( $invoice );
154
-			$invoice->save_meta_data();
155
-
156
-			// Apply changes.
157
-			$invoice->apply_changes();
158
-			$this->clear_caches( $invoice );
159
-
160
-			// Fires after a new invoice is created.
161
-			do_action( 'getpaid_new_' . $invoice->get_type(), $invoice );
162
-			return true;
163
-		}
164
-
165
-		if ( is_wp_error( $id ) ) {
166
-			$invoice->last_error = $id->get_error_message();
167
-		}
168
-
169
-		return false;
170
-	}
171
-
172
-	/**
173
-	 * Method to read an invoice from the database.
174
-	 *
175
-	 * @param WPInv_Invoice $invoice Invoice object.
176
-	 *
177
-	 */
178
-	public function read( &$invoice ) {
179
-
180
-		$invoice->set_defaults();
181
-		$invoice_object = get_post( $invoice->get_id() );
182
-
183
-		if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) {
184
-			$invoice->last_error = __( 'Invalid invoice.', 'invoicing' );
185
-			$invoice->set_id( 0 );
186
-			return false;
187
-		}
188
-
189
-		$invoice->set_props(
190
-			array(
191
-				'date_created'  => 0 < $invoice_object->post_date ? $invoice_object->post_date : null,
192
-				'date_modified' => 0 < $invoice_object->post_modified ? $invoice_object->post_modified : null,
193
-				'status'        => $invoice_object->post_status,
194
-				'author'        => $invoice_object->post_author,
195
-				'description'   => $invoice_object->post_excerpt,
196
-				'parent_id'     => $invoice_object->post_parent,
197
-				'name'          => $invoice_object->post_title,
198
-				'path'          => $invoice_object->post_name,
199
-				'post_type'     => $invoice_object->post_type,
200
-			)
201
-		);
202
-
203
-		$invoice->set_type( $invoice_object->post_type );
204
-
205
-		$this->read_object_data( $invoice, $invoice_object );
206
-		$this->add_special_fields( $invoice );
207
-		$this->add_items( $invoice );
208
-		$invoice->read_meta_data();
209
-		$invoice->set_object_read( true );
210
-		do_action( 'getpaid_read_' . $invoice->get_type(), $invoice );
211
-
212
-	}
213
-
214
-	/**
215
-	 * Method to update an invoice in the database.
216
-	 *
217
-	 * @param WPInv_Invoice $invoice Invoice object.
218
-	 */
219
-	public function update( &$invoice ) {
220
-		$invoice->save_meta_data();
221
-		$invoice->set_version( WPINV_VERSION );
222
-
223
-		if ( null === $invoice->get_date_created( 'edit' ) ) {
224
-			$invoice->set_date_created(  current_time('mysql') );
225
-		}
226
-
227
-		// Ensure both the key and number are set.
228
-		$invoice->get_path();
229
-
230
-		// Grab the current status so we can compare.
231
-		$previous_status = get_post_status( $invoice->get_id() );
232
-
233
-		$changes = $invoice->get_changes();
234
-
235
-		// Only update the post when the post data changes.
236
-		if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) {
237
-			$post_data = array(
238
-				'post_date'         => $invoice->get_date_created( 'edit' ),
239
-				'post_date_gmt'     => $invoice->get_date_created_gmt( 'edit' ),
240
-				'post_status'       => $invoice->get_status( 'edit' ),
241
-				'post_title'        => $invoice->get_name( 'edit' ),
242
-				'post_author'       => $invoice->get_user_id( 'edit' ),
243
-				'post_modified'     => $invoice->get_date_modified( 'edit' ),
244
-				'post_excerpt'      => $invoice->get_description( 'edit' ),
245
-				'post_parent'       => $invoice->get_parent_id( 'edit' ),
246
-				'post_name'         => $invoice->get_path( 'edit' ),
247
-				'post_type'         => $invoice->get_post_type( 'edit' ),
248
-			);
249
-
250
-			/**
251
-			 * When updating this object, to prevent infinite loops, use $wpdb
252
-			 * to update data, since wp_update_post spawns more calls to the
253
-			 * save_post action.
254
-			 *
255
-			 * This ensures hooks are fired by either WP itself (admin screen save),
256
-			 * or an update purely from CRUD.
257
-			 */
258
-			if ( doing_action( 'save_post' ) ) {
259
-				$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) );
260
-				clean_post_cache( $invoice->get_id() );
261
-			} else {
262
-				wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) );
263
-			}
264
-			$invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
265
-		}
266
-
267
-		// Update meta data.
268
-		$this->update_post_meta( $invoice );
269
-
270
-		// Save special fields and items.
271
-		$this->save_special_fields( $invoice );
272
-		$this->save_items( $invoice );
273
-
274
-		// Apply the changes.
275
-		$invoice->apply_changes();
276
-
277
-		// Clear caches.
278
-		$this->clear_caches( $invoice );
279
-
280
-		// Fire a hook depending on the status - this should be considered a creation if it was previously draft status.
281
-		$new_status = $invoice->get_status( 'edit' );
282
-
283
-		if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) {
284
-			do_action( 'getpaid_new_' . $invoice->get_type(), $invoice );
285
-		} else {
286
-			do_action( 'getpaid_update_' . $invoice->get_type(), $invoice );
287
-		}
288
-
289
-	}
290
-
291
-	/*
106
+    /**
107
+     * Method to create a new invoice in the database.
108
+     *
109
+     * @param WPInv_Invoice $invoice Invoice object.
110
+     */
111
+    public function create( &$invoice ) {
112
+        $invoice->set_version( WPINV_VERSION );
113
+        $invoice->set_date_created( current_time('mysql') );
114
+
115
+        // Create a new post.
116
+        $id = wp_insert_post(
117
+            apply_filters(
118
+                'getpaid_new_invoice_data',
119
+                array(
120
+                    'post_date'     => $invoice->get_date_created( 'edit' ),
121
+                    'post_type'     => $invoice->get_post_type( 'edit' ),
122
+                    'post_status'   => $this->get_post_status( $invoice ),
123
+                    'ping_status'   => 'closed',
124
+                    'post_author'   => $invoice->get_user_id( 'edit' ),
125
+                    'post_title'    => $invoice->get_title( 'edit' ),
126
+                    'post_excerpt'  => $invoice->get_description( 'edit' ),
127
+                    'post_parent'   => $invoice->get_parent_id( 'edit' ),
128
+                )
129
+            ),
130
+            true
131
+        );
132
+
133
+        if ( $id && ! is_wp_error( $id ) ) {
134
+
135
+            // Update the new id and regenerate a title.
136
+            $invoice->set_id( $id );
137
+
138
+            $invoice->maybe_set_number();
139
+
140
+            wp_update_post(
141
+                array(
142
+                    'ID'         => $invoice->get_id(),
143
+                    'post_title' => $invoice->get_number( 'edit' ),
144
+                    'post_name'  => $invoice->get_path( 'edit' )
145
+                )
146
+            );
147
+
148
+            // Save special fields and items.
149
+            $this->save_special_fields( $invoice );
150
+            $this->save_items( $invoice );
151
+
152
+            // Update meta data.
153
+            $this->update_post_meta( $invoice );
154
+            $invoice->save_meta_data();
155
+
156
+            // Apply changes.
157
+            $invoice->apply_changes();
158
+            $this->clear_caches( $invoice );
159
+
160
+            // Fires after a new invoice is created.
161
+            do_action( 'getpaid_new_' . $invoice->get_type(), $invoice );
162
+            return true;
163
+        }
164
+
165
+        if ( is_wp_error( $id ) ) {
166
+            $invoice->last_error = $id->get_error_message();
167
+        }
168
+
169
+        return false;
170
+    }
171
+
172
+    /**
173
+     * Method to read an invoice from the database.
174
+     *
175
+     * @param WPInv_Invoice $invoice Invoice object.
176
+     *
177
+     */
178
+    public function read( &$invoice ) {
179
+
180
+        $invoice->set_defaults();
181
+        $invoice_object = get_post( $invoice->get_id() );
182
+
183
+        if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) {
184
+            $invoice->last_error = __( 'Invalid invoice.', 'invoicing' );
185
+            $invoice->set_id( 0 );
186
+            return false;
187
+        }
188
+
189
+        $invoice->set_props(
190
+            array(
191
+                'date_created'  => 0 < $invoice_object->post_date ? $invoice_object->post_date : null,
192
+                'date_modified' => 0 < $invoice_object->post_modified ? $invoice_object->post_modified : null,
193
+                'status'        => $invoice_object->post_status,
194
+                'author'        => $invoice_object->post_author,
195
+                'description'   => $invoice_object->post_excerpt,
196
+                'parent_id'     => $invoice_object->post_parent,
197
+                'name'          => $invoice_object->post_title,
198
+                'path'          => $invoice_object->post_name,
199
+                'post_type'     => $invoice_object->post_type,
200
+            )
201
+        );
202
+
203
+        $invoice->set_type( $invoice_object->post_type );
204
+
205
+        $this->read_object_data( $invoice, $invoice_object );
206
+        $this->add_special_fields( $invoice );
207
+        $this->add_items( $invoice );
208
+        $invoice->read_meta_data();
209
+        $invoice->set_object_read( true );
210
+        do_action( 'getpaid_read_' . $invoice->get_type(), $invoice );
211
+
212
+    }
213
+
214
+    /**
215
+     * Method to update an invoice in the database.
216
+     *
217
+     * @param WPInv_Invoice $invoice Invoice object.
218
+     */
219
+    public function update( &$invoice ) {
220
+        $invoice->save_meta_data();
221
+        $invoice->set_version( WPINV_VERSION );
222
+
223
+        if ( null === $invoice->get_date_created( 'edit' ) ) {
224
+            $invoice->set_date_created(  current_time('mysql') );
225
+        }
226
+
227
+        // Ensure both the key and number are set.
228
+        $invoice->get_path();
229
+
230
+        // Grab the current status so we can compare.
231
+        $previous_status = get_post_status( $invoice->get_id() );
232
+
233
+        $changes = $invoice->get_changes();
234
+
235
+        // Only update the post when the post data changes.
236
+        if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) {
237
+            $post_data = array(
238
+                'post_date'         => $invoice->get_date_created( 'edit' ),
239
+                'post_date_gmt'     => $invoice->get_date_created_gmt( 'edit' ),
240
+                'post_status'       => $invoice->get_status( 'edit' ),
241
+                'post_title'        => $invoice->get_name( 'edit' ),
242
+                'post_author'       => $invoice->get_user_id( 'edit' ),
243
+                'post_modified'     => $invoice->get_date_modified( 'edit' ),
244
+                'post_excerpt'      => $invoice->get_description( 'edit' ),
245
+                'post_parent'       => $invoice->get_parent_id( 'edit' ),
246
+                'post_name'         => $invoice->get_path( 'edit' ),
247
+                'post_type'         => $invoice->get_post_type( 'edit' ),
248
+            );
249
+
250
+            /**
251
+             * When updating this object, to prevent infinite loops, use $wpdb
252
+             * to update data, since wp_update_post spawns more calls to the
253
+             * save_post action.
254
+             *
255
+             * This ensures hooks are fired by either WP itself (admin screen save),
256
+             * or an update purely from CRUD.
257
+             */
258
+            if ( doing_action( 'save_post' ) ) {
259
+                $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) );
260
+                clean_post_cache( $invoice->get_id() );
261
+            } else {
262
+                wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) );
263
+            }
264
+            $invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
265
+        }
266
+
267
+        // Update meta data.
268
+        $this->update_post_meta( $invoice );
269
+
270
+        // Save special fields and items.
271
+        $this->save_special_fields( $invoice );
272
+        $this->save_items( $invoice );
273
+
274
+        // Apply the changes.
275
+        $invoice->apply_changes();
276
+
277
+        // Clear caches.
278
+        $this->clear_caches( $invoice );
279
+
280
+        // Fire a hook depending on the status - this should be considered a creation if it was previously draft status.
281
+        $new_status = $invoice->get_status( 'edit' );
282
+
283
+        if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) {
284
+            do_action( 'getpaid_new_' . $invoice->get_type(), $invoice );
285
+        } else {
286
+            do_action( 'getpaid_update_' . $invoice->get_type(), $invoice );
287
+        }
288
+
289
+    }
290
+
291
+    /*
292 292
 	|--------------------------------------------------------------------------
293 293
 	| Additional Methods
294 294
 	|--------------------------------------------------------------------------
295 295
 	*/
296 296
 
297
-	/**
297
+    /**
298 298
      * Retrieves special fields and adds to the invoice.
299
-	 *
300
-	 * @param WPInv_Invoice $invoice Invoice object.
299
+     *
300
+     * @param WPInv_Invoice $invoice Invoice object.
301 301
      */
302 302
     public function add_special_fields( &$invoice ) {
303
-		global $wpdb;
303
+        global $wpdb;
304 304
 
305
-		// Maybe retrieve from the cache.
306
-		$data   = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' );
305
+        // Maybe retrieve from the cache.
306
+        $data   = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' );
307 307
 
308
-		// If not found, retrieve from the db.
309
-		if ( false === $data ) {
310
-			$table =  $wpdb->prefix . 'getpaid_invoices';
308
+        // If not found, retrieve from the db.
309
+        if ( false === $data ) {
310
+            $table =  $wpdb->prefix . 'getpaid_invoices';
311 311
 
312
-			$data  = $wpdb->get_row(
313
-				$wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ),
314
-				ARRAY_A
315
-			);
312
+            $data  = $wpdb->get_row(
313
+                $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ),
314
+                ARRAY_A
315
+            );
316 316
 
317
-			// Update the cache with our data
318
-			wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' );
317
+            // Update the cache with our data
318
+            wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' );
319 319
 
320
-		}
320
+        }
321 321
 
322
-		// Abort if the data does not exist.
323
-		if ( empty( $data ) ) {
324
-			$invoice->set_object_read( true );
325
-			$invoice->set_props( wpinv_get_user_address( $invoice->get_user_id() ) );
326
-			return;
327
-		}
322
+        // Abort if the data does not exist.
323
+        if ( empty( $data ) ) {
324
+            $invoice->set_object_read( true );
325
+            $invoice->set_props( wpinv_get_user_address( $invoice->get_user_id() ) );
326
+            return;
327
+        }
328 328
 
329
-		$props = array();
329
+        $props = array();
330 330
 
331
-		foreach ( $this->database_fields_to_props as $db_field => $prop ) {
331
+        foreach ( $this->database_fields_to_props as $db_field => $prop ) {
332 332
 			
333
-			if ( $db_field == 'post_id' ) {
334
-				continue;
335
-			}
336
-
337
-			$props[ $prop ] = $data[ $db_field ];
338
-		}
339
-
340
-		$invoice->set_props( $props );
341
-
342
-	}
343
-
344
-	/**
345
-	 * Gets a list of special fields that need updated based on change state
346
-	 * or if they are present in the database or not.
347
-	 *
348
-	 * @param  WPInv_Invoice $invoice       The Invoice object.
349
-	 * @return array                        A mapping of field keys => prop names, filtered by ones that should be updated.
350
-	 */
351
-	protected function get_special_fields_to_update( $invoice ) {
352
-		$fields_to_update = array();
353
-		$changed_props   = $invoice->get_changes();
354
-
355
-		// Props should be updated if they are a part of the $changed array or don't exist yet.
356
-		foreach ( $this->database_fields_to_props as $database_field => $prop ) {
357
-			if ( array_key_exists( $prop, $changed_props ) ) {
358
-				$fields_to_update[ $database_field ] = $prop;
359
-			}
360
-		}
361
-
362
-		return $fields_to_update;
363
-	}
364
-
365
-	/**
366
-	 * Helper method that updates all the database fields for an invoice based on it's settings in the WPInv_Invoice class.
367
-	 *
368
-	 * @param WPInv_Invoice $invoice WPInv_Invoice object.
369
-	 * @since 1.0.19
370
-	 */
371
-	protected function update_special_fields( &$invoice ) {
372
-		global $wpdb;
373
-
374
-		$updated_props    = array();
375
-		$fields_to_update = $this->get_special_fields_to_update( $invoice );
376
-
377
-		foreach ( $fields_to_update as $database_field => $prop ) {
378
-			$value = $invoice->{"get_$prop"}( 'edit' );
379
-			$value = is_string( $value ) ? wp_slash( $value ) : $value;
380
-			$value = is_bool( $value ) ? ( int ) $value : $value;
381
-			$updated_props[ $database_field ] = maybe_serialize( $value );
382
-		}
383
-
384
-		if ( ! empty( $updated_props ) ) {
385
-
386
-			$table = $wpdb->prefix . 'getpaid_invoices';
387
-			$wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) );
388
-			wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' );
389
-			do_action( "getpaid_invoice_update_database_fields", $invoice, $updated_props );
390
-
391
-		}
392
-
393
-	}
394
-
395
-	/**
396
-	 * Helper method that inserts special fields to the database.
397
-	 *
398
-	 * @param WPInv_Invoice $invoice WPInv_Invoice object.
399
-	 * @since 1.0.19
400
-	 */
401
-	protected function insert_special_fields( &$invoice ) {
402
-		global $wpdb;
403
-
404
-		$updated_props   = array();
405
-
406
-		foreach ( $this->database_fields_to_props as $database_field => $prop ) {
407
-			$value = $invoice->{"get_$prop"}( 'edit' );
408
-			$value = is_string( $value ) ? wp_slash( $value ) : $value;
409
-			$value = is_bool( $value ) ? ( int ) $value : $value;
410
-			$updated_props[ $database_field ] = maybe_serialize( $value );
411
-		}
412
-
413
-		$table = $wpdb->prefix . 'getpaid_invoices';
414
-		$wpdb->insert( $table, $updated_props );
415
-		wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' );
416
-		do_action( "getpaid_invoice_insert_database_fields", $invoice, $updated_props );
417
-
418
-	}
419
-
420
-	/**
333
+            if ( $db_field == 'post_id' ) {
334
+                continue;
335
+            }
336
+
337
+            $props[ $prop ] = $data[ $db_field ];
338
+        }
339
+
340
+        $invoice->set_props( $props );
341
+
342
+    }
343
+
344
+    /**
345
+     * Gets a list of special fields that need updated based on change state
346
+     * or if they are present in the database or not.
347
+     *
348
+     * @param  WPInv_Invoice $invoice       The Invoice object.
349
+     * @return array                        A mapping of field keys => prop names, filtered by ones that should be updated.
350
+     */
351
+    protected function get_special_fields_to_update( $invoice ) {
352
+        $fields_to_update = array();
353
+        $changed_props   = $invoice->get_changes();
354
+
355
+        // Props should be updated if they are a part of the $changed array or don't exist yet.
356
+        foreach ( $this->database_fields_to_props as $database_field => $prop ) {
357
+            if ( array_key_exists( $prop, $changed_props ) ) {
358
+                $fields_to_update[ $database_field ] = $prop;
359
+            }
360
+        }
361
+
362
+        return $fields_to_update;
363
+    }
364
+
365
+    /**
366
+     * Helper method that updates all the database fields for an invoice based on it's settings in the WPInv_Invoice class.
367
+     *
368
+     * @param WPInv_Invoice $invoice WPInv_Invoice object.
369
+     * @since 1.0.19
370
+     */
371
+    protected function update_special_fields( &$invoice ) {
372
+        global $wpdb;
373
+
374
+        $updated_props    = array();
375
+        $fields_to_update = $this->get_special_fields_to_update( $invoice );
376
+
377
+        foreach ( $fields_to_update as $database_field => $prop ) {
378
+            $value = $invoice->{"get_$prop"}( 'edit' );
379
+            $value = is_string( $value ) ? wp_slash( $value ) : $value;
380
+            $value = is_bool( $value ) ? ( int ) $value : $value;
381
+            $updated_props[ $database_field ] = maybe_serialize( $value );
382
+        }
383
+
384
+        if ( ! empty( $updated_props ) ) {
385
+
386
+            $table = $wpdb->prefix . 'getpaid_invoices';
387
+            $wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) );
388
+            wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' );
389
+            do_action( "getpaid_invoice_update_database_fields", $invoice, $updated_props );
390
+
391
+        }
392
+
393
+    }
394
+
395
+    /**
396
+     * Helper method that inserts special fields to the database.
397
+     *
398
+     * @param WPInv_Invoice $invoice WPInv_Invoice object.
399
+     * @since 1.0.19
400
+     */
401
+    protected function insert_special_fields( &$invoice ) {
402
+        global $wpdb;
403
+
404
+        $updated_props   = array();
405
+
406
+        foreach ( $this->database_fields_to_props as $database_field => $prop ) {
407
+            $value = $invoice->{"get_$prop"}( 'edit' );
408
+            $value = is_string( $value ) ? wp_slash( $value ) : $value;
409
+            $value = is_bool( $value ) ? ( int ) $value : $value;
410
+            $updated_props[ $database_field ] = maybe_serialize( $value );
411
+        }
412
+
413
+        $table = $wpdb->prefix . 'getpaid_invoices';
414
+        $wpdb->insert( $table, $updated_props );
415
+        wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' );
416
+        do_action( "getpaid_invoice_insert_database_fields", $invoice, $updated_props );
417
+
418
+    }
419
+
420
+    /**
421 421
      * Saves all special fields.
422
-	 *
423
-	 * @param WPInv_Invoice $invoice Invoice object.
422
+     *
423
+     * @param WPInv_Invoice $invoice Invoice object.
424 424
      */
425 425
     public function save_special_fields( & $invoice ) {
426
-		global $wpdb;
426
+        global $wpdb;
427 427
 
428
-		// The invoices table.
429
-		$table = $wpdb->prefix . 'getpaid_invoices';
430
-		$id    = (int) $invoice->get_id();
431
-		$invoice->maybe_set_key();
428
+        // The invoices table.
429
+        $table = $wpdb->prefix . 'getpaid_invoices';
430
+        $id    = (int) $invoice->get_id();
431
+        $invoice->maybe_set_key();
432 432
 
433
-		if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) {
433
+        if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) {
434 434
 
435
-			$this->update_special_fields( $invoice );
435
+            $this->update_special_fields( $invoice );
436 436
 
437
-		} else {
437
+        } else {
438 438
 
439
-			$this->insert_special_fields( $invoice );
439
+            $this->insert_special_fields( $invoice );
440 440
 
441
-		}
441
+        }
442 442
 
443
-	}
443
+    }
444 444
 
445
-	/**
445
+    /**
446 446
      * Set's up cart details.
447
-	 *
448
-	 * @param WPInv_Invoice $invoice Invoice object.
447
+     *
448
+     * @param WPInv_Invoice $invoice Invoice object.
449 449
      */
450 450
     public function add_items( &$invoice ) {
451
-		global $wpdb;
451
+        global $wpdb;
452 452
 
453
-		// Maybe retrieve from the cache.
454
-		$items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' );
453
+        // Maybe retrieve from the cache.
454
+        $items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' );
455 455
 
456
-		// If not found, retrieve from the db.
457
-		if ( false === $items ) {
458
-			$table =  $wpdb->prefix . 'getpaid_invoice_items';
456
+        // If not found, retrieve from the db.
457
+        if ( false === $items ) {
458
+            $table =  $wpdb->prefix . 'getpaid_invoice_items';
459 459
 
460
-			$items = $wpdb->get_results(
461
-				$wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() )
462
-			);
460
+            $items = $wpdb->get_results(
461
+                $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() )
462
+            );
463 463
 
464
-			// Update the cache with our data
465
-			wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' );
464
+            // Update the cache with our data
465
+            wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' );
466 466
 
467
-		}
467
+        }
468 468
 
469
-		// Abort if no items found.
469
+        // Abort if no items found.
470 470
         if ( empty( $items ) ) {
471 471
             return;
472
-		}
472
+        }
473 473
 
474
-		foreach ( $items as $item_data ) {
475
-			$item = new GetPaid_Form_Item( $item_data->item_id );
474
+        foreach ( $items as $item_data ) {
475
+            $item = new GetPaid_Form_Item( $item_data->item_id );
476 476
 
477
-			// Set item data.
478
-			$item->item_tax      = wpinv_sanitize_amount( $item_data->tax );
479
-			$item->item_discount = wpinv_sanitize_amount( $item_data->discount );
480
-			$item->set_name( $item_data->item_name );
481
-			$item->set_description( $item_data->item_description );
482
-			$item->set_price( $item_data->item_price );
483
-			$item->set_quantity( $item_data->quantity );
484
-			$item->set_item_meta( $item_data->meta );
477
+            // Set item data.
478
+            $item->item_tax      = wpinv_sanitize_amount( $item_data->tax );
479
+            $item->item_discount = wpinv_sanitize_amount( $item_data->discount );
480
+            $item->set_name( $item_data->item_name );
481
+            $item->set_description( $item_data->item_description );
482
+            $item->set_price( $item_data->item_price );
483
+            $item->set_quantity( $item_data->quantity );
484
+            $item->set_item_meta( $item_data->meta );
485 485
 
486
-			$invoice->add_item( $item );
487
-		}
486
+            $invoice->add_item( $item );
487
+        }
488 488
 
489
-	}
489
+    }
490 490
 
491
-	/**
491
+    /**
492 492
      * Saves cart details.
493
-	 *
494
-	 * @param WPInv_Invoice $invoice Invoice object.
493
+     *
494
+     * @param WPInv_Invoice $invoice Invoice object.
495 495
      */
496 496
     public function save_items( $invoice ) {
497 497
 
498
-		// Delete previously existing items.
499
-		$this->delete_items( $invoice );
498
+        // Delete previously existing items.
499
+        $this->delete_items( $invoice );
500 500
 
501
-		$table   =  $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items';
501
+        $table   =  $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items';
502 502
 
503
-		foreach ( $invoice->get_cart_details() as $item_data ) {
504
-			$item_data = array_map( 'maybe_serialize', $item_data );
505
-			$GLOBALS['wpdb']->insert( $table, $item_data );
506
-		}
503
+        foreach ( $invoice->get_cart_details() as $item_data ) {
504
+            $item_data = array_map( 'maybe_serialize', $item_data );
505
+            $GLOBALS['wpdb']->insert( $table, $item_data );
506
+        }
507 507
 
508
-		wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' );
509
-		do_action( "getpaid_invoice_save_items", $invoice );
508
+        wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' );
509
+        do_action( "getpaid_invoice_save_items", $invoice );
510 510
 
511
-	}
511
+    }
512 512
 
513
-	/**
513
+    /**
514 514
      * Deletes an invoice's cart details from the database.
515
-	 *
516
-	 * @param WPInv_Invoice $invoice Invoice object.
515
+     *
516
+     * @param WPInv_Invoice $invoice Invoice object.
517 517
      */
518 518
     public function delete_items( $invoice ) {
519
-		$table =  $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items';
520
-		return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) );
521
-	}
519
+        $table =  $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items';
520
+        return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) );
521
+    }
522 522
 
523
-	/**
523
+    /**
524 524
      * Deletes an invoice's special fields from the database.
525
-	 *
526
-	 * @param WPInv_Invoice $invoice Invoice object.
525
+     *
526
+     * @param WPInv_Invoice $invoice Invoice object.
527 527
      */
528 528
     public function delete_special_fields( $invoice ) {
529
-		$table =  $GLOBALS['wpdb']->prefix . 'getpaid_invoices';
530
-		return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) );
529
+        $table =  $GLOBALS['wpdb']->prefix . 'getpaid_invoices';
530
+        return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) );
531 531
     }
532 532
 
533 533
 }
Please login to merge, or discard this patch.
includes/admin/class-wpinv-subscriptions-list-table.php 1 patch
Indentation   +369 added lines, -369 removed lines patch added patch discarded remove patch
@@ -6,7 +6,7 @@  discard block
 block discarded – undo
6 6
 if ( ! defined( 'ABSPATH' ) ) exit;
7 7
 
8 8
 if ( ! class_exists( 'WP_List_Table' ) ) {
9
-	include_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
9
+    include_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
10 10
 }
11 11
 
12 12
 /**
@@ -14,373 +14,373 @@  discard block
 block discarded – undo
14 14
  */
15 15
 class WPInv_Subscriptions_List_Table extends WP_List_Table {
16 16
 
17
-	/**
18
-	 * URL of this page
19
-	 *
20
-	 * @var   string
21
-	 * @since 1.0.19
22
-	 */
23
-	public $base_url;
24
-
25
-	/**
26
-	 * Query
27
-	 *
28
-	 * @var   GetPaid_Subscriptions_Query
29
-	 * @since 1.0.19
30
-	 */
31
-	public $query;
32
-
33
-	/**
34
-	 * Total subscriptions
35
-	 *
36
-	 * @var   string
37
-	 * @since 1.0.0
38
-	 */
39
-	public $total_count;
40
-
41
-	/**
42
-	 * Current status subscriptions
43
-	 *
44
-	 * @var   string
45
-	 * @since 1.0.0
46
-	 */
47
-	public $current_total_count;
48
-
49
-	/**
50
-	 * Status counts
51
-	 *
52
-	 * @var   array
53
-	 * @since 1.0.19
54
-	 */
55
-	public $status_counts;
56
-
57
-	/**
58
-	 * Number of results to show per page
59
-	 *
60
-	 * @var   int
61
-	 * @since 1.0.0
62
-	 */
63
-	public $per_page = 10;
64
-
65
-	/**
66
-	 *  Constructor function.
67
-	 */
68
-	public function __construct() {
69
-
70
-		parent::__construct(
71
-			array(
72
-				'singular' => 'subscription',
73
-				'plural'   => 'subscriptions',
74
-			)
75
-		);
76
-
77
-		$this->process_bulk_action();
78
-
79
-		$this->prepare_query();
80
-
81
-		$this->base_url = remove_query_arg( 'status' );
82
-
83
-	}
84
-
85
-	/**
86
-	 *  Prepares the display query
87
-	 */
88
-	public function prepare_query() {
89
-
90
-		// Prepare query args.
91
-		$query = array(
92
-			'number'  => $this->per_page,
93
-			'paged'   => $this->get_paged(),
94
-			'status'  => ( isset( $_GET['status'] ) && array_key_exists( $_GET['status'], getpaid_get_subscription_statuses() ) ) ? $_GET['status'] : 'all',
95
-			'orderby' => ( isset( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'id',
96
-			'order'   => ( isset( $_GET['order'] ) ) ? $_GET['order'] : 'DESC',
97
-		);
98
-
99
-		// Prepare class properties.
100
-		$this->query               = new GetPaid_Subscriptions_Query( $query );
101
-		$this->total_count         = $this->query->get_total();
102
-		$this->current_total_count = $this->query->get_total();
103
-		$this->items               = $this->query->get_results();
104
-		$this->status_counts       = getpaid_get_subscription_status_counts( $query );
105
-
106
-		if ( 'all' != $query['status'] ) {
107
-			unset( $query['status'] );
108
-			$this->total_count   = getpaid_get_subscriptions( $query, 'count' );
109
-		}
110
-
111
-	}
112
-
113
-	/**
114
-	 * Gets the list of views available on this table.
115
-	 *
116
-	 * The format is an associative array:
117
-	 * - `'id' => 'link'`
118
-	 *
119
-	 * @since 1.0.0
120
-	 *
121
-	 * @return array
122
-	 */
123
-	public function get_views() {
124
-
125
-		$current  = isset( $_GET['status'] ) ? $_GET['status'] : 'all';
126
-		$views    = array(
127
-
128
-			'all' => sprintf(
129
-				'<a href="%s" %s>%s&nbsp;<span class="count">(%d)</span></a>',
130
-				esc_url( add_query_arg( 'status', false, $this->base_url ) ),
131
-				$current === 'all' ? ' class="current"' : '',
132
-				__('All','invoicing' ),
133
-				$this->total_count
134
-			)
135
-
136
-		);
137
-
138
-		foreach ( array_filter( $this->status_counts ) as $status => $count ) {
139
-
140
-			$views[ $status ] = sprintf(
141
-				'<a href="%s" %s>%s&nbsp;<span class="count">(%d)</span></a>',
142
-				esc_url( add_query_arg( 'status', urlencode( $status ), $this->base_url ) ),
143
-				$current === $status ? ' class="current"' : '',
144
-				sanitize_text_field( getpaid_get_subscription_status_label( $status ) ),
145
-				$count
146
-			);
147
-
148
-		}
149
-
150
-		return $views;
151
-
152
-	}
153
-
154
-	/**
155
-	 * Render most columns
156
-	 *
157
-	 * @access      private
158
-	 * @since       1.0.0
159
-	 * @return      string
160
-	 */
161
-	public function column_default( $item, $column_name ) {
162
-		return apply_filters( "getpaid_subscriptions_table_column_$column_name", $item->$column_name );
163
-	}
164
-
165
-	/**
166
-	 * This is how checkbox column renders.
167
-	 *
168
-	 * @param WPInv_Subscription $item
169
-	 * @return string
170
-	 */
171
-	public function column_cb( $item ) {
172
-		return sprintf( '<input type="checkbox" name="id[]" value="%s" />', esc_html( $item->get_id() ) );
173
-	}
174
-
175
-	/**
176
-	 * Status column
177
-	 *
178
-	 * @param WPInv_Subscription $item
179
-	 * @since       1.0.0
180
-	 * @return      string
181
-	 */
182
-	public function column_status( $item ) {
183
-		return $item->get_status_label_html();
184
-	}
185
-
186
-	/**
187
-	 * Subscription column
188
-	 *
189
-	 * @param WPInv_Subscription $item
190
-	 * @since       1.0.0
191
-	 * @return      string
192
-	 */
193
-	public function column_subscription( $item ) {
194
-
195
-		$username = __( '(Missing User)', 'invoicing' );
196
-
197
-		$user = get_userdata( $item->get_customer_id() );
198
-		if ( $user ) {
199
-
200
-			$username = sprintf(
201
-				'<a href="user-edit.php?user_id=%s">%s</a>',
202
-				absint( $user->ID ),
203
-				! empty( $user->display_name ) ? sanitize_text_field( $user->display_name ) : sanitize_email( $user->user_email )
204
-			);
205
-
206
-		}
207
-
208
-		// translators: $1: is opening link, $2: is subscription id number, $3: is closing link tag, $4: is user's name
209
-		$column_content = sprintf(
210
-			_x( '%1$s#%2$s%3$s for %4$s', 'Subscription title on admin table. (e.g.: #211 for John Doe)', 'invoicing' ),
211
-			'<a href="' . esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&id=' . absint( $item->get_id() ) ) ) . '">',
212
-			'<strong>' . esc_attr( $item->get_id() ) . '</strong>', '</a>',
213
-			$username
214
-		);
215
-
216
-		$row_actions = array();
217
-
218
-		// View subscription.
219
-		$view_url    = esc_url( add_query_arg( 'id', $item->get_id(), admin_url( 'admin.php?page=wpinv-subscriptions' ) ));
220
-		$row_actions['view'] = '<a href="' . $view_url . '">' . __( 'View Subscription', 'invoicing' ) . '</a>';
221
-
222
-		// View invoice.
223
-		$invoice = get_post( $item->get_parent_invoice_id() );
224
-
225
-		if ( ! empty( $invoice ) ) {
226
-			$view_url    = get_edit_post_link( $invoice );
227
-			$row_actions['invoice'] = '<a href="' . $view_url . '">' . __( 'View Invoice', 'invoicing' ) . '</a>';
228
-		}
229
-
230
-		$row_actions = $this->row_actions( apply_filters( 'getpaid_subscription_table_row_actions', $row_actions, $item ) );
231
-
232
-		return "<strong>$column_content</strong>" . $this->column_amount( $item ) . $row_actions;
233
-	}
234
-
235
-	/**
236
-	 * Renewal date column
237
-	 *
238
-	 * @param WPInv_Subscription $item
239
-	 * @since       1.0.0
240
-	 * @return      string
241
-	 */
242
-	public function column_renewal_date( $item ) {
243
-		return getpaid_format_date_value( $item->get_expiration() );
244
-	}
245
-
246
-	/**
247
-	 * Start date column
248
-	 *
249
-	 * @param WPInv_Subscription $item
250
-	 * @since       1.0.0
251
-	 * @return      string
252
-	 */
253
-	public function column_start_date( $item ) {
254
-		return getpaid_format_date_value( $item->get_date_created() );
255
-	}
256
-
257
-	/**
258
-	 * Amount column
259
-	 *
260
-	 * @param WPInv_Subscription $item
261
-	 * @since       1.0.19
262
-	 * @return      string
263
-	 */
264
-	public function column_amount( $item ) {
265
-		$amount = getpaid_get_formatted_subscription_amount( $item );
266
-		return "<span class='text-muted form-text mt-2 mb-2'>$amount</span>";
267
-	}
268
-
269
-	/**
270
-	 * Billing Times column
271
-	 *
272
-	 * @param WPInv_Subscription $item
273
-	 * @since       1.0.0
274
-	 * @return      string
275
-	 */
276
-	public function column_renewals( $item ) {
277
-		$max_bills = $item->get_bill_times();
278
-		return $item->get_times_billed() . ' / ' . ( empty( $max_bills ) ? "&infin;" : $max_bills );
279
-	}
280
-
281
-	/**
282
-	 * Product ID column
283
-	 *
284
-	 * @param WPInv_Subscription $item
285
-	 * @since       1.0.0
286
-	 * @return      string
287
-	 */
288
-	public function column_item( $item ) {
289
-		$_item = get_post( $item->get_product_id() );
290
-
291
-		if ( ! empty( $_item ) ) {
292
-			$link = get_edit_post_link( $_item );
293
-			$link = esc_url( $link );
294
-			$name = esc_html( get_the_title( $_item ) );
295
-			return "<a href='$link'>$name</a>";
296
-		} else {
297
-			return sprintf( __( 'Item #%s', 'invoicing' ), $item->get_product_id() );
298
-		}
299
-
300
-	}
301
-
302
-	/**
303
-	 * Retrieve the current page number
304
-	 *
305
-	 * @return      int
306
-	 */
307
-	public function get_paged() {
308
-		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
309
-	}
310
-
311
-	/**
312
-	 * Setup the final data for the table
313
-	 *
314
-	 */
315
-	public function prepare_items() {
316
-
317
-		$columns  = $this->get_columns();
318
-		$hidden   = array();
319
-		$sortable = $this->get_sortable_columns();
320
-
321
-		$this->_column_headers = array( $columns, $hidden, $sortable );
322
-
323
-		$this->set_pagination_args(
324
-			array(
325
-			'total_items' => $this->current_total_count,
326
-			'per_page'    => $this->per_page,
327
-			'total_pages' => ceil( $this->current_total_count / $this->per_page )
328
-			)
329
-		);
330
-	}
331
-
332
-	/**
333
-	 * Table columns
334
-	 *
335
-	 * @return array
336
-	 */
337
-	public function get_columns(){
338
-		$columns = array(
339
-			'cb'                => '<input type="checkbox" />',
340
-			'subscription'      => __( 'Subscription', 'invoicing' ),
341
-			'start_date'        => __( 'Start Date', 'invoicing' ),
342
-			'renewal_date'      => __( 'Next Payment', 'invoicing' ),
343
-			'renewals'          => __( 'Payments', 'invoicing' ),
344
-			'item'              => __( 'Item', 'invoicing' ),
345
-			'status'            => __( 'Status', 'invoicing' ),
346
-		);
347
-
348
-		return apply_filters( 'manage_getpaid_subscriptions_table_columns', $columns );
349
-	}
350
-
351
-	/**
352
-	 * Sortable table columns.
353
-	 *
354
-	 * @return array
355
-	 */
356
-	public function get_sortable_columns() {
357
-		$sortable = array(
358
-			'subscription' => array( 'id', true ),
359
-			'start_date'   => array( 'created', true ),
360
-			'renewal_date' => array( 'expiration', true ),
361
-			'renewals'     => array( 'bill_times', true ),
362
-			'item'         => array( 'product_id', true ),
363
-			'status'       => array( 'status', true ),
364
-		);
365
-
366
-		return apply_filters( 'manage_getpaid_subscriptions_sortable_table_columns', $sortable );
367
-	}
368
-
369
-	/**
370
-	 * Whether the table has items to display or not
371
-	 *
372
-	 * @return bool
373
-	 */
374
-	public function has_items() {
375
-		return ! empty( $this->current_total_count );
376
-	}
377
-
378
-	/**
379
-	 * Processes bulk actions.
380
-	 *
381
-	 */
382
-	public function process_bulk_action() {
383
-
384
-	}
17
+    /**
18
+     * URL of this page
19
+     *
20
+     * @var   string
21
+     * @since 1.0.19
22
+     */
23
+    public $base_url;
24
+
25
+    /**
26
+     * Query
27
+     *
28
+     * @var   GetPaid_Subscriptions_Query
29
+     * @since 1.0.19
30
+     */
31
+    public $query;
32
+
33
+    /**
34
+     * Total subscriptions
35
+     *
36
+     * @var   string
37
+     * @since 1.0.0
38
+     */
39
+    public $total_count;
40
+
41
+    /**
42
+     * Current status subscriptions
43
+     *
44
+     * @var   string
45
+     * @since 1.0.0
46
+     */
47
+    public $current_total_count;
48
+
49
+    /**
50
+     * Status counts
51
+     *
52
+     * @var   array
53
+     * @since 1.0.19
54
+     */
55
+    public $status_counts;
56
+
57
+    /**
58
+     * Number of results to show per page
59
+     *
60
+     * @var   int
61
+     * @since 1.0.0
62
+     */
63
+    public $per_page = 10;
64
+
65
+    /**
66
+     *  Constructor function.
67
+     */
68
+    public function __construct() {
69
+
70
+        parent::__construct(
71
+            array(
72
+                'singular' => 'subscription',
73
+                'plural'   => 'subscriptions',
74
+            )
75
+        );
76
+
77
+        $this->process_bulk_action();
78
+
79
+        $this->prepare_query();
80
+
81
+        $this->base_url = remove_query_arg( 'status' );
82
+
83
+    }
84
+
85
+    /**
86
+     *  Prepares the display query
87
+     */
88
+    public function prepare_query() {
89
+
90
+        // Prepare query args.
91
+        $query = array(
92
+            'number'  => $this->per_page,
93
+            'paged'   => $this->get_paged(),
94
+            'status'  => ( isset( $_GET['status'] ) && array_key_exists( $_GET['status'], getpaid_get_subscription_statuses() ) ) ? $_GET['status'] : 'all',
95
+            'orderby' => ( isset( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'id',
96
+            'order'   => ( isset( $_GET['order'] ) ) ? $_GET['order'] : 'DESC',
97
+        );
98
+
99
+        // Prepare class properties.
100
+        $this->query               = new GetPaid_Subscriptions_Query( $query );
101
+        $this->total_count         = $this->query->get_total();
102
+        $this->current_total_count = $this->query->get_total();
103
+        $this->items               = $this->query->get_results();
104
+        $this->status_counts       = getpaid_get_subscription_status_counts( $query );
105
+
106
+        if ( 'all' != $query['status'] ) {
107
+            unset( $query['status'] );
108
+            $this->total_count   = getpaid_get_subscriptions( $query, 'count' );
109
+        }
110
+
111
+    }
112
+
113
+    /**
114
+     * Gets the list of views available on this table.
115
+     *
116
+     * The format is an associative array:
117
+     * - `'id' => 'link'`
118
+     *
119
+     * @since 1.0.0
120
+     *
121
+     * @return array
122
+     */
123
+    public function get_views() {
124
+
125
+        $current  = isset( $_GET['status'] ) ? $_GET['status'] : 'all';
126
+        $views    = array(
127
+
128
+            'all' => sprintf(
129
+                '<a href="%s" %s>%s&nbsp;<span class="count">(%d)</span></a>',
130
+                esc_url( add_query_arg( 'status', false, $this->base_url ) ),
131
+                $current === 'all' ? ' class="current"' : '',
132
+                __('All','invoicing' ),
133
+                $this->total_count
134
+            )
135
+
136
+        );
137
+
138
+        foreach ( array_filter( $this->status_counts ) as $status => $count ) {
139
+
140
+            $views[ $status ] = sprintf(
141
+                '<a href="%s" %s>%s&nbsp;<span class="count">(%d)</span></a>',
142
+                esc_url( add_query_arg( 'status', urlencode( $status ), $this->base_url ) ),
143
+                $current === $status ? ' class="current"' : '',
144
+                sanitize_text_field( getpaid_get_subscription_status_label( $status ) ),
145
+                $count
146
+            );
147
+
148
+        }
149
+
150
+        return $views;
151
+
152
+    }
153
+
154
+    /**
155
+     * Render most columns
156
+     *
157
+     * @access      private
158
+     * @since       1.0.0
159
+     * @return      string
160
+     */
161
+    public function column_default( $item, $column_name ) {
162
+        return apply_filters( "getpaid_subscriptions_table_column_$column_name", $item->$column_name );
163
+    }
164
+
165
+    /**
166
+     * This is how checkbox column renders.
167
+     *
168
+     * @param WPInv_Subscription $item
169
+     * @return string
170
+     */
171
+    public function column_cb( $item ) {
172
+        return sprintf( '<input type="checkbox" name="id[]" value="%s" />', esc_html( $item->get_id() ) );
173
+    }
174
+
175
+    /**
176
+     * Status column
177
+     *
178
+     * @param WPInv_Subscription $item
179
+     * @since       1.0.0
180
+     * @return      string
181
+     */
182
+    public function column_status( $item ) {
183
+        return $item->get_status_label_html();
184
+    }
185
+
186
+    /**
187
+     * Subscription column
188
+     *
189
+     * @param WPInv_Subscription $item
190
+     * @since       1.0.0
191
+     * @return      string
192
+     */
193
+    public function column_subscription( $item ) {
194
+
195
+        $username = __( '(Missing User)', 'invoicing' );
196
+
197
+        $user = get_userdata( $item->get_customer_id() );
198
+        if ( $user ) {
199
+
200
+            $username = sprintf(
201
+                '<a href="user-edit.php?user_id=%s">%s</a>',
202
+                absint( $user->ID ),
203
+                ! empty( $user->display_name ) ? sanitize_text_field( $user->display_name ) : sanitize_email( $user->user_email )
204
+            );
205
+
206
+        }
207
+
208
+        // translators: $1: is opening link, $2: is subscription id number, $3: is closing link tag, $4: is user's name
209
+        $column_content = sprintf(
210
+            _x( '%1$s#%2$s%3$s for %4$s', 'Subscription title on admin table. (e.g.: #211 for John Doe)', 'invoicing' ),
211
+            '<a href="' . esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&id=' . absint( $item->get_id() ) ) ) . '">',
212
+            '<strong>' . esc_attr( $item->get_id() ) . '</strong>', '</a>',
213
+            $username
214
+        );
215
+
216
+        $row_actions = array();
217
+
218
+        // View subscription.
219
+        $view_url    = esc_url( add_query_arg( 'id', $item->get_id(), admin_url( 'admin.php?page=wpinv-subscriptions' ) ));
220
+        $row_actions['view'] = '<a href="' . $view_url . '">' . __( 'View Subscription', 'invoicing' ) . '</a>';
221
+
222
+        // View invoice.
223
+        $invoice = get_post( $item->get_parent_invoice_id() );
224
+
225
+        if ( ! empty( $invoice ) ) {
226
+            $view_url    = get_edit_post_link( $invoice );
227
+            $row_actions['invoice'] = '<a href="' . $view_url . '">' . __( 'View Invoice', 'invoicing' ) . '</a>';
228
+        }
229
+
230
+        $row_actions = $this->row_actions( apply_filters( 'getpaid_subscription_table_row_actions', $row_actions, $item ) );
231
+
232
+        return "<strong>$column_content</strong>" . $this->column_amount( $item ) . $row_actions;
233
+    }
234
+
235
+    /**
236
+     * Renewal date column
237
+     *
238
+     * @param WPInv_Subscription $item
239
+     * @since       1.0.0
240
+     * @return      string
241
+     */
242
+    public function column_renewal_date( $item ) {
243
+        return getpaid_format_date_value( $item->get_expiration() );
244
+    }
245
+
246
+    /**
247
+     * Start date column
248
+     *
249
+     * @param WPInv_Subscription $item
250
+     * @since       1.0.0
251
+     * @return      string
252
+     */
253
+    public function column_start_date( $item ) {
254
+        return getpaid_format_date_value( $item->get_date_created() );
255
+    }
256
+
257
+    /**
258
+     * Amount column
259
+     *
260
+     * @param WPInv_Subscription $item
261
+     * @since       1.0.19
262
+     * @return      string
263
+     */
264
+    public function column_amount( $item ) {
265
+        $amount = getpaid_get_formatted_subscription_amount( $item );
266
+        return "<span class='text-muted form-text mt-2 mb-2'>$amount</span>";
267
+    }
268
+
269
+    /**
270
+     * Billing Times column
271
+     *
272
+     * @param WPInv_Subscription $item
273
+     * @since       1.0.0
274
+     * @return      string
275
+     */
276
+    public function column_renewals( $item ) {
277
+        $max_bills = $item->get_bill_times();
278
+        return $item->get_times_billed() . ' / ' . ( empty( $max_bills ) ? "&infin;" : $max_bills );
279
+    }
280
+
281
+    /**
282
+     * Product ID column
283
+     *
284
+     * @param WPInv_Subscription $item
285
+     * @since       1.0.0
286
+     * @return      string
287
+     */
288
+    public function column_item( $item ) {
289
+        $_item = get_post( $item->get_product_id() );
290
+
291
+        if ( ! empty( $_item ) ) {
292
+            $link = get_edit_post_link( $_item );
293
+            $link = esc_url( $link );
294
+            $name = esc_html( get_the_title( $_item ) );
295
+            return "<a href='$link'>$name</a>";
296
+        } else {
297
+            return sprintf( __( 'Item #%s', 'invoicing' ), $item->get_product_id() );
298
+        }
299
+
300
+    }
301
+
302
+    /**
303
+     * Retrieve the current page number
304
+     *
305
+     * @return      int
306
+     */
307
+    public function get_paged() {
308
+        return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
309
+    }
310
+
311
+    /**
312
+     * Setup the final data for the table
313
+     *
314
+     */
315
+    public function prepare_items() {
316
+
317
+        $columns  = $this->get_columns();
318
+        $hidden   = array();
319
+        $sortable = $this->get_sortable_columns();
320
+
321
+        $this->_column_headers = array( $columns, $hidden, $sortable );
322
+
323
+        $this->set_pagination_args(
324
+            array(
325
+            'total_items' => $this->current_total_count,
326
+            'per_page'    => $this->per_page,
327
+            'total_pages' => ceil( $this->current_total_count / $this->per_page )
328
+            )
329
+        );
330
+    }
331
+
332
+    /**
333
+     * Table columns
334
+     *
335
+     * @return array
336
+     */
337
+    public function get_columns(){
338
+        $columns = array(
339
+            'cb'                => '<input type="checkbox" />',
340
+            'subscription'      => __( 'Subscription', 'invoicing' ),
341
+            'start_date'        => __( 'Start Date', 'invoicing' ),
342
+            'renewal_date'      => __( 'Next Payment', 'invoicing' ),
343
+            'renewals'          => __( 'Payments', 'invoicing' ),
344
+            'item'              => __( 'Item', 'invoicing' ),
345
+            'status'            => __( 'Status', 'invoicing' ),
346
+        );
347
+
348
+        return apply_filters( 'manage_getpaid_subscriptions_table_columns', $columns );
349
+    }
350
+
351
+    /**
352
+     * Sortable table columns.
353
+     *
354
+     * @return array
355
+     */
356
+    public function get_sortable_columns() {
357
+        $sortable = array(
358
+            'subscription' => array( 'id', true ),
359
+            'start_date'   => array( 'created', true ),
360
+            'renewal_date' => array( 'expiration', true ),
361
+            'renewals'     => array( 'bill_times', true ),
362
+            'item'         => array( 'product_id', true ),
363
+            'status'       => array( 'status', true ),
364
+        );
365
+
366
+        return apply_filters( 'manage_getpaid_subscriptions_sortable_table_columns', $sortable );
367
+    }
368
+
369
+    /**
370
+     * Whether the table has items to display or not
371
+     *
372
+     * @return bool
373
+     */
374
+    public function has_items() {
375
+        return ! empty( $this->current_total_count );
376
+    }
377
+
378
+    /**
379
+     * Processes bulk actions.
380
+     *
381
+     */
382
+    public function process_bulk_action() {
383
+
384
+    }
385 385
 
386 386
 }
Please login to merge, or discard this patch.
includes/admin/class-getpaid-post-types-admin.php 1 patch
Indentation   +615 added lines, -615 removed lines patch added patch discarded remove patch
@@ -13,616 +13,616 @@  discard block
 block discarded – undo
13 13
 class GetPaid_Post_Types_Admin {
14 14
 
15 15
     /**
16
-	 * Hook in methods.
17
-	 */
18
-	public static function init() {
19
-
20
-		// Init metaboxes.
21
-		GetPaid_Metaboxes::init();
22
-
23
-		// Filter the post updated messages.
24
-		add_filter( 'post_updated_messages', 'GetPaid_Post_Types_Admin::post_updated_messages' );
25
-
26
-		// Filter post actions.
27
-		add_filter( 'post_row_actions', 'GetPaid_Post_Types_Admin::post_row_actions', 10, 2 );
28
-
29
-		// Invoice table columns.
30
-		add_filter( 'manage_wpi_invoice_posts_columns', array( __CLASS__, 'invoice_columns' ), 100 );
31
-		add_action( 'manage_wpi_invoice_posts_custom_column', array( __CLASS__, 'display_invoice_columns' ), 10, 2 );
32
-
33
-		// Items table columns.
34
-		add_filter( 'manage_wpi_item_posts_columns', array( __CLASS__, 'item_columns' ), 100 );
35
-		add_filter( 'manage_edit-wpi_item_sortable_columns', array( __CLASS__, 'sortable_item_columns' ), 20 );
36
-		add_action( 'manage_wpi_item_posts_custom_column', array( __CLASS__, 'display_item_columns' ), 10, 2 );
37
-		add_action( 'restrict_manage_posts', array( __CLASS__, 'add_item_filters' ), 100 );
38
-		add_action( 'parse_query', array( __CLASS__, 'filter_item_query' ), 100 );
39
-		add_action( 'request', array( __CLASS__, 'reorder_items' ), 100 );
40
-
41
-		// Payment forms columns.
42
-		add_filter( 'manage_wpi_payment_form_posts_columns', array( __CLASS__, 'payment_form_columns' ), 100 );
43
-		add_action( 'manage_wpi_payment_form_posts_custom_column', array( __CLASS__, 'display_payment_form_columns' ), 10, 2 );
44
-		add_filter( 'display_post_states', array( __CLASS__, 'filter_payment_form_state' ), 10, 2 );
45
-
46
-		// Discount table columns.
47
-		add_filter( 'manage_wpi_discount_posts_columns', array( __CLASS__, 'discount_columns' ), 100 );
48
-
49
-		// Deleting posts.
50
-		add_action( 'delete_post', array( __CLASS__, 'delete_post' ) );
51
-		add_filter( 'display_post_states', array( __CLASS__, 'filter_discount_state' ), 10, 2 );
52
-	}
53
-
54
-	/**
55
-	 * Post updated messages.
56
-	 */
57
-	public static function post_updated_messages( $messages ) {
58
-		global $post;
59
-
60
-		$messages['wpi_discount'] = array(
61
-			0   => '',
62
-			1   => __( 'Discount updated.', 'invoicing' ),
63
-			2   => __( 'Custom field updated.', 'invoicing' ),
64
-			3   => __( 'Custom field deleted.', 'invoicing' ),
65
-			4   => __( 'Discount updated.', 'invoicing' ),
66
-			5   => isset( $_GET['revision'] ) ? wp_sprintf( __( 'Discount restored to revision from %s', 'invoicing' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
67
-			6   => __( 'Discount updated.', 'invoicing' ),
68
-			7   => __( 'Discount saved.', 'invoicing' ),
69
-			8   => __( 'Discount submitted.', 'invoicing' ),
70
-			9   => wp_sprintf( __( 'Discount scheduled for: <strong>%1$s</strong>.', 'invoicing' ), date_i18n( __( 'M j, Y @ G:i', 'invoicing' ), strtotime( $post->post_date ) ) ),
71
-			10  => __( 'Discount draft updated.', 'invoicing' ),
72
-		);
73
-
74
-		$messages['wpi_payment_form'] = array(
75
-			0   => '',
76
-			1   => __( 'Payment Form updated.', 'invoicing' ),
77
-			2   => __( 'Custom field updated.', 'invoicing' ),
78
-			3   => __( 'Custom field deleted.', 'invoicing' ),
79
-			4   => __( 'Payment Form updated.', 'invoicing' ),
80
-			5   => isset( $_GET['revision'] ) ? wp_sprintf( __( 'Payment Form restored to revision from %s', 'invoicing' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
81
-			6   => __( 'Payment Form updated.', 'invoicing' ),
82
-			7   => __( 'Payment Form saved.', 'invoicing' ),
83
-			8   => __( 'Payment Form submitted.', 'invoicing' ),
84
-			9   => wp_sprintf( __( 'Payment Form scheduled for: <strong>%1$s</strong>.', 'invoicing' ), date_i18n( __( 'M j, Y @ G:i', 'invoicing' ), strtotime( $post->post_date ) ) ),
85
-			10  => __( 'Payment Form draft updated.', 'invoicing' ),
86
-		);
87
-
88
-		return $messages;
89
-
90
-	}
91
-
92
-	/**
93
-	 * Post row actions.
94
-	 */
95
-	public static function post_row_actions( $actions, $post ) {
96
-
97
-		$post = get_post( $post );
98
-
99
-		// We do not want to edit the default payment form.
100
-		if ( 'wpi_payment_form' == $post->post_type && $post->ID == wpinv_get_default_payment_form() ) {
101
-			unset( $actions['trash'] );
102
-			unset( $actions['inline hide-if-no-js'] );
103
-		}
104
-
105
-		return $actions;
106
-	}
107
-
108
-	/**
109
-	 * Returns an array of invoice table columns.
110
-	 */
111
-	public static function invoice_columns( $columns ) {
112
-
113
-		$columns = array(
114
-			'cb'                => $columns['cb'],
115
-			'number'            => __( 'Invoice', 'invoicing' ),
116
-			'customer'          => __( 'Customer', 'invoicing' ),
117
-			'invoice_date'      => __( 'Date', 'invoicing' ),
118
-			'amount'            => __( 'Amount', 'invoicing' ),
119
-			'recurring'         => __( 'Recurring', 'invoicing' ),
120
-			'status'            => __( 'Status', 'invoicing' ),
121
-			'wpi_actions'       => __( 'Actions', 'invoicing' ),
122
-		);
123
-
124
-		return apply_filters( 'wpi_invoice_table_columns', $columns );
125
-	}
126
-
127
-	/**
128
-	 * Displays invoice table columns.
129
-	 */
130
-	public static function display_invoice_columns( $column_name, $post_id ) {
131
-
132
-		$invoice = new WPInv_Invoice( $post_id );
133
-
134
-		switch ( $column_name ) {
135
-
136
-			case 'invoice_date' :
137
-				$date_time = esc_attr( $invoice->get_created_date() );
138
-				$date      = getpaid_format_date_value( $date_time );
139
-				echo "<span title='$date_time'>$date</span>";
140
-				break;
141
-
142
-			case 'amount' :
143
-
144
-				$amount = $invoice->get_total();
145
-				$formated_amount = wpinv_price( wpinv_format_amount( $amount ), $invoice->get_currency() );
146
-
147
-				if ( $invoice->is_refunded() ) {
148
-					$refunded_amount = wpinv_price( wpinv_format_amount( 0 ), $invoice->get_currency() );
149
-					echo "<del>$formated_amount</del>&nbsp;<ins>$refunded_amount</ins>";
150
-				} else {
151
-
152
-					$discount = $invoice->get_total_discount();
153
-
154
-					if ( ! empty( $discount ) ) {
155
-						$new_amount = wpinv_price( wpinv_format_amount( $amount + $discount ), $invoice->get_currency() );
156
-						echo "<del>$new_amount</del>&nbsp;<ins>$formated_amount</ins>";
157
-					} else {
158
-						echo $formated_amount;
159
-					}
160
-
161
-				}
162
-
163
-				break;
164
-
165
-			case 'status' :
166
-				$status       = sanitize_text_field( $invoice->get_status() );
167
-				$status_label = sanitize_text_field( $invoice->get_status_nicename() );
168
-
169
-				// If it is paid, show the gateway title.
170
-				if ( $invoice->is_paid() ) {
171
-					$gateway = sanitize_text_field( $invoice->get_gateway_title() );
172
-					$gateway = wp_sprintf( esc_attr__( 'Paid via %s', 'invoicing' ), $gateway );
16
+     * Hook in methods.
17
+     */
18
+    public static function init() {
173 19
 
174
-					echo "<mark class='wpi-help-tip getpaid-invoice-status $status' title='$gateway'><span>$status_label</span></mark>";
175
-				} else {
176
-					echo "<mark class='getpaid-invoice-status $status'><span>$status_label</span></mark>";
177
-				}
20
+        // Init metaboxes.
21
+        GetPaid_Metaboxes::init();
178 22
 
179
-				// If it is not paid, display the overdue and view status.
180
-				if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) {
23
+        // Filter the post updated messages.
24
+        add_filter( 'post_updated_messages', 'GetPaid_Post_Types_Admin::post_updated_messages' );
181 25
 
182
-					// Invoice view status.
183
-					if ( wpinv_is_invoice_viewed( $invoice->get_id() ) ) {
184
-						echo '&nbsp;&nbsp;<i class="fa fa-eye wpi-help-tip" title="'. esc_attr__( 'Viewed by Customer', 'invoicing' ).'"></i>';
185
-					} else {
186
-						echo '&nbsp;&nbsp;<i class="fa fa-eye-slash wpi-help-tip" title="'. esc_attr__( 'Not Viewed by Customer', 'invoicing' ).'"></i>';
187
-					}
26
+        // Filter post actions.
27
+        add_filter( 'post_row_actions', 'GetPaid_Post_Types_Admin::post_row_actions', 10, 2 );
188 28
 
189
-					// Display the overview status.
190
-					if ( wpinv_get_option( 'overdue_active' ) ) {
191
-						$due_date = $invoice->get_due_date();
192
-						$fomatted = getpaid_format_date( $due_date );
29
+        // Invoice table columns.
30
+        add_filter( 'manage_wpi_invoice_posts_columns', array( __CLASS__, 'invoice_columns' ), 100 );
31
+        add_action( 'manage_wpi_invoice_posts_custom_column', array( __CLASS__, 'display_invoice_columns' ), 10, 2 );
193 32
 
194
-						if ( ! empty( $fomatted ) ) {
195
-							$date = wp_sprintf( __( 'Due %s', 'invoicing' ), $fomatted );
196
-							echo "<p class='description' style='color: #888;' title='$due_date'>$fomatted</p>";
197
-						}
198
-					}
33
+        // Items table columns.
34
+        add_filter( 'manage_wpi_item_posts_columns', array( __CLASS__, 'item_columns' ), 100 );
35
+        add_filter( 'manage_edit-wpi_item_sortable_columns', array( __CLASS__, 'sortable_item_columns' ), 20 );
36
+        add_action( 'manage_wpi_item_posts_custom_column', array( __CLASS__, 'display_item_columns' ), 10, 2 );
37
+        add_action( 'restrict_manage_posts', array( __CLASS__, 'add_item_filters' ), 100 );
38
+        add_action( 'parse_query', array( __CLASS__, 'filter_item_query' ), 100 );
39
+        add_action( 'request', array( __CLASS__, 'reorder_items' ), 100 );
199 40
 
200
-				}
41
+        // Payment forms columns.
42
+        add_filter( 'manage_wpi_payment_form_posts_columns', array( __CLASS__, 'payment_form_columns' ), 100 );
43
+        add_action( 'manage_wpi_payment_form_posts_custom_column', array( __CLASS__, 'display_payment_form_columns' ), 10, 2 );
44
+        add_filter( 'display_post_states', array( __CLASS__, 'filter_payment_form_state' ), 10, 2 );
201 45
 
202
-				break;
46
+        // Discount table columns.
47
+        add_filter( 'manage_wpi_discount_posts_columns', array( __CLASS__, 'discount_columns' ), 100 );
203 48
 
204
-			case 'recurring':
49
+        // Deleting posts.
50
+        add_action( 'delete_post', array( __CLASS__, 'delete_post' ) );
51
+        add_filter( 'display_post_states', array( __CLASS__, 'filter_discount_state' ), 10, 2 );
52
+    }
205 53
 
206
-				if ( $invoice->is_recurring() ) {
207
-					echo '<i class="fa fa-check" style="color:#43850a;"></i>';
208
-				} else {
209
-					echo '<i class="fa fa-times" style="color:#616161;"></i>';
210
-				}
211
-				break;
54
+    /**
55
+     * Post updated messages.
56
+     */
57
+    public static function post_updated_messages( $messages ) {
58
+        global $post;
59
+
60
+        $messages['wpi_discount'] = array(
61
+            0   => '',
62
+            1   => __( 'Discount updated.', 'invoicing' ),
63
+            2   => __( 'Custom field updated.', 'invoicing' ),
64
+            3   => __( 'Custom field deleted.', 'invoicing' ),
65
+            4   => __( 'Discount updated.', 'invoicing' ),
66
+            5   => isset( $_GET['revision'] ) ? wp_sprintf( __( 'Discount restored to revision from %s', 'invoicing' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
67
+            6   => __( 'Discount updated.', 'invoicing' ),
68
+            7   => __( 'Discount saved.', 'invoicing' ),
69
+            8   => __( 'Discount submitted.', 'invoicing' ),
70
+            9   => wp_sprintf( __( 'Discount scheduled for: <strong>%1$s</strong>.', 'invoicing' ), date_i18n( __( 'M j, Y @ G:i', 'invoicing' ), strtotime( $post->post_date ) ) ),
71
+            10  => __( 'Discount draft updated.', 'invoicing' ),
72
+        );
73
+
74
+        $messages['wpi_payment_form'] = array(
75
+            0   => '',
76
+            1   => __( 'Payment Form updated.', 'invoicing' ),
77
+            2   => __( 'Custom field updated.', 'invoicing' ),
78
+            3   => __( 'Custom field deleted.', 'invoicing' ),
79
+            4   => __( 'Payment Form updated.', 'invoicing' ),
80
+            5   => isset( $_GET['revision'] ) ? wp_sprintf( __( 'Payment Form restored to revision from %s', 'invoicing' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
81
+            6   => __( 'Payment Form updated.', 'invoicing' ),
82
+            7   => __( 'Payment Form saved.', 'invoicing' ),
83
+            8   => __( 'Payment Form submitted.', 'invoicing' ),
84
+            9   => wp_sprintf( __( 'Payment Form scheduled for: <strong>%1$s</strong>.', 'invoicing' ), date_i18n( __( 'M j, Y @ G:i', 'invoicing' ), strtotime( $post->post_date ) ) ),
85
+            10  => __( 'Payment Form draft updated.', 'invoicing' ),
86
+        );
87
+
88
+        return $messages;
89
+
90
+    }
91
+
92
+    /**
93
+     * Post row actions.
94
+     */
95
+    public static function post_row_actions( $actions, $post ) {
96
+
97
+        $post = get_post( $post );
98
+
99
+        // We do not want to edit the default payment form.
100
+        if ( 'wpi_payment_form' == $post->post_type && $post->ID == wpinv_get_default_payment_form() ) {
101
+            unset( $actions['trash'] );
102
+            unset( $actions['inline hide-if-no-js'] );
103
+        }
104
+
105
+        return $actions;
106
+    }
107
+
108
+    /**
109
+     * Returns an array of invoice table columns.
110
+     */
111
+    public static function invoice_columns( $columns ) {
112
+
113
+        $columns = array(
114
+            'cb'                => $columns['cb'],
115
+            'number'            => __( 'Invoice', 'invoicing' ),
116
+            'customer'          => __( 'Customer', 'invoicing' ),
117
+            'invoice_date'      => __( 'Date', 'invoicing' ),
118
+            'amount'            => __( 'Amount', 'invoicing' ),
119
+            'recurring'         => __( 'Recurring', 'invoicing' ),
120
+            'status'            => __( 'Status', 'invoicing' ),
121
+            'wpi_actions'       => __( 'Actions', 'invoicing' ),
122
+        );
123
+
124
+        return apply_filters( 'wpi_invoice_table_columns', $columns );
125
+    }
126
+
127
+    /**
128
+     * Displays invoice table columns.
129
+     */
130
+    public static function display_invoice_columns( $column_name, $post_id ) {
131
+
132
+        $invoice = new WPInv_Invoice( $post_id );
133
+
134
+        switch ( $column_name ) {
135
+
136
+            case 'invoice_date' :
137
+                $date_time = esc_attr( $invoice->get_created_date() );
138
+                $date      = getpaid_format_date_value( $date_time );
139
+                echo "<span title='$date_time'>$date</span>";
140
+                break;
141
+
142
+            case 'amount' :
143
+
144
+                $amount = $invoice->get_total();
145
+                $formated_amount = wpinv_price( wpinv_format_amount( $amount ), $invoice->get_currency() );
146
+
147
+                if ( $invoice->is_refunded() ) {
148
+                    $refunded_amount = wpinv_price( wpinv_format_amount( 0 ), $invoice->get_currency() );
149
+                    echo "<del>$formated_amount</del>&nbsp;<ins>$refunded_amount</ins>";
150
+                } else {
151
+
152
+                    $discount = $invoice->get_total_discount();
153
+
154
+                    if ( ! empty( $discount ) ) {
155
+                        $new_amount = wpinv_price( wpinv_format_amount( $amount + $discount ), $invoice->get_currency() );
156
+                        echo "<del>$new_amount</del>&nbsp;<ins>$formated_amount</ins>";
157
+                    } else {
158
+                        echo $formated_amount;
159
+                    }
160
+
161
+                }
162
+
163
+                break;
164
+
165
+            case 'status' :
166
+                $status       = sanitize_text_field( $invoice->get_status() );
167
+                $status_label = sanitize_text_field( $invoice->get_status_nicename() );
168
+
169
+                // If it is paid, show the gateway title.
170
+                if ( $invoice->is_paid() ) {
171
+                    $gateway = sanitize_text_field( $invoice->get_gateway_title() );
172
+                    $gateway = wp_sprintf( esc_attr__( 'Paid via %s', 'invoicing' ), $gateway );
212 173
 
213
-			case 'number' :
174
+                    echo "<mark class='wpi-help-tip getpaid-invoice-status $status' title='$gateway'><span>$status_label</span></mark>";
175
+                } else {
176
+                    echo "<mark class='getpaid-invoice-status $status'><span>$status_label</span></mark>";
177
+                }
214 178
 
215
-				$edit_link       = esc_url( get_edit_post_link( $invoice->get_id() ) );
216
-				$invoice_number  = sanitize_text_field( $invoice->get_number() );
217
-				$invoice_details = esc_attr__( 'View Invoice Details', 'invoicing' );
179
+                // If it is not paid, display the overdue and view status.
180
+                if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) {
218 181
 
219
-				echo "<a href='$edit_link' title='$invoice_details'><strong>$invoice_number</strong></a>";
182
+                    // Invoice view status.
183
+                    if ( wpinv_is_invoice_viewed( $invoice->get_id() ) ) {
184
+                        echo '&nbsp;&nbsp;<i class="fa fa-eye wpi-help-tip" title="'. esc_attr__( 'Viewed by Customer', 'invoicing' ).'"></i>';
185
+                    } else {
186
+                        echo '&nbsp;&nbsp;<i class="fa fa-eye-slash wpi-help-tip" title="'. esc_attr__( 'Not Viewed by Customer', 'invoicing' ).'"></i>';
187
+                    }
220 188
 
221
-				break;
189
+                    // Display the overview status.
190
+                    if ( wpinv_get_option( 'overdue_active' ) ) {
191
+                        $due_date = $invoice->get_due_date();
192
+                        $fomatted = getpaid_format_date( $due_date );
222 193
 
223
-			case 'customer' :
194
+                        if ( ! empty( $fomatted ) ) {
195
+                            $date = wp_sprintf( __( 'Due %s', 'invoicing' ), $fomatted );
196
+                            echo "<p class='description' style='color: #888;' title='$due_date'>$fomatted</p>";
197
+                        }
198
+                    }
199
+
200
+                }
201
+
202
+                break;
203
+
204
+            case 'recurring':
205
+
206
+                if ( $invoice->is_recurring() ) {
207
+                    echo '<i class="fa fa-check" style="color:#43850a;"></i>';
208
+                } else {
209
+                    echo '<i class="fa fa-times" style="color:#616161;"></i>';
210
+                }
211
+                break;
212
+
213
+            case 'number' :
214
+
215
+                $edit_link       = esc_url( get_edit_post_link( $invoice->get_id() ) );
216
+                $invoice_number  = sanitize_text_field( $invoice->get_number() );
217
+                $invoice_details = esc_attr__( 'View Invoice Details', 'invoicing' );
218
+
219
+                echo "<a href='$edit_link' title='$invoice_details'><strong>$invoice_number</strong></a>";
220
+
221
+                break;
222
+
223
+            case 'customer' :
224 224
 	
225
-				$customer_name = $invoice->get_user_full_name();
225
+                $customer_name = $invoice->get_user_full_name();
226 226
 	
227
-				if ( empty( $customer_name ) ) {
228
-					$customer_name = $invoice->get_email();
229
-				}
227
+                if ( empty( $customer_name ) ) {
228
+                    $customer_name = $invoice->get_email();
229
+                }
230 230
 	
231
-				if ( ! empty( $customer_name ) ) {
232
-					$customer_details = esc_attr__( 'View Customer Details', 'invoicing' );
233
-					$view_link        = esc_url( add_query_arg( 'user_id', $invoice->get_user_id(), admin_url( 'user-edit.php' ) ) );
234
-					echo "<a href='$view_link' title='$customer_details'><span>$customer_name</span></a>";
235
-				} else {
236
-					echo '<div>&mdash;</div>';
237
-				}
231
+                if ( ! empty( $customer_name ) ) {
232
+                    $customer_details = esc_attr__( 'View Customer Details', 'invoicing' );
233
+                    $view_link        = esc_url( add_query_arg( 'user_id', $invoice->get_user_id(), admin_url( 'user-edit.php' ) ) );
234
+                    echo "<a href='$view_link' title='$customer_details'><span>$customer_name</span></a>";
235
+                } else {
236
+                    echo '<div>&mdash;</div>';
237
+                }
238
+
239
+                break;
240
+
241
+            case 'wpi_actions' :
242
+
243
+                if ( $invoice->is_draft() ) {
244
+                    return;
245
+                }
246
+
247
+                $url    = esc_url( $invoice->get_view_url() );
248
+                $print  = esc_attr__( 'Print invoice', 'invoicing' );
249
+                echo "&nbsp;<a href='$url' title='$print' target='_blank' style='color:#757575'><i class='fa fa-print' style='font-size: 1.4em;'></i></a>";
250
+
251
+                $url    = esc_url(
252
+                    wp_nonce_url(
253
+                        add_query_arg(
254
+                            array(
255
+                                'getpaid-admin-action' => 'send_invoice',
256
+                                'invoice_id'           => $invoice->get_id()
257
+                            )
258
+                        ),
259
+                        'getpaid-nonce',
260
+                        'getpaid-nonce'
261
+                    )
262
+                );
263
+
264
+                $send   = esc_attr__( 'Send invoice to customer', 'invoicing' );
265
+                echo "&nbsp;&nbsp;<a href='$url' title='$send' style='color:#757575'><i class='fa fa-envelope' style='font-size: 1.4em;'></i></a>";
266
+
267
+                break;
268
+        }
238 269
 
239
-				break;
270
+    }
240 271
 
241
-			case 'wpi_actions' :
242
-
243
-				if ( $invoice->is_draft() ) {
244
-					return;
245
-				}
246
-
247
-				$url    = esc_url( $invoice->get_view_url() );
248
-				$print  = esc_attr__( 'Print invoice', 'invoicing' );
249
-				echo "&nbsp;<a href='$url' title='$print' target='_blank' style='color:#757575'><i class='fa fa-print' style='font-size: 1.4em;'></i></a>";
272
+    /**
273
+     * Returns an array of payment forms table columns.
274
+     */
275
+    public static function payment_form_columns( $columns ) {
250 276
 
251
-				$url    = esc_url(
252
-					wp_nonce_url(
253
-						add_query_arg(
254
-							array(
255
-								'getpaid-admin-action' => 'send_invoice',
256
-								'invoice_id'           => $invoice->get_id()
257
-							)
258
-						),
259
-						'getpaid-nonce',
260
-						'getpaid-nonce'
261
-					)
262
-				);
277
+        $columns = array(
278
+            'cb'                => $columns['cb'],
279
+            'title'             => __( 'Name', 'invoicing' ),
280
+            'shortcode'         => __( 'Shortcode', 'invoicing' ),
281
+            'earnings'          => __( 'Revenue', 'invoicing' ),
282
+            'refunds'           => __( 'Refunded', 'invoicing' ),
283
+            'items'             => __( 'Items', 'invoicing' ),
284
+            'date'              => __( 'Date', 'invoicing' ),
285
+        );
263 286
 
264
-				$send   = esc_attr__( 'Send invoice to customer', 'invoicing' );
265
-				echo "&nbsp;&nbsp;<a href='$url' title='$send' style='color:#757575'><i class='fa fa-envelope' style='font-size: 1.4em;'></i></a>";
287
+        return apply_filters( 'wpi_payment_form_table_columns', $columns );
266 288
 
267
-				break;
268
-		}
289
+    }
269 290
 
270
-	}
291
+    /**
292
+     * Displays payment form table columns.
293
+     */
294
+    public static function display_payment_form_columns( $column_name, $post_id ) {
271 295
 
272
-	/**
273
-	 * Returns an array of payment forms table columns.
274
-	 */
275
-	public static function payment_form_columns( $columns ) {
296
+        // Retrieve the payment form.
297
+        $form = new GetPaid_Payment_Form( $post_id );
276 298
 
277
-		$columns = array(
278
-			'cb'                => $columns['cb'],
279
-			'title'             => __( 'Name', 'invoicing' ),
280
-			'shortcode'         => __( 'Shortcode', 'invoicing' ),
281
-			'earnings'          => __( 'Revenue', 'invoicing' ),
282
-			'refunds'           => __( 'Refunded', 'invoicing' ),
283
-			'items'             => __( 'Items', 'invoicing' ),
284
-			'date'              => __( 'Date', 'invoicing' ),
285
-		);
299
+        switch ( $column_name ) {
286 300
 
287
-		return apply_filters( 'wpi_payment_form_table_columns', $columns );
301
+            case 'earnings' :
302
+                echo wpinv_price( wpinv_format_amount( $form->get_earned() ) );
303
+                break;
288 304
 
289
-	}
305
+            case 'refunds' :
306
+                echo wpinv_price( wpinv_format_amount( $form->get_refunded() ) );
307
+                break;
290 308
 
291
-	/**
292
-	 * Displays payment form table columns.
293
-	 */
294
-	public static function display_payment_form_columns( $column_name, $post_id ) {
309
+            case 'refunds' :
310
+                echo wpinv_price( wpinv_format_amount( $form->get_refunded() ) );
311
+                break;
295 312
 
296
-		// Retrieve the payment form.
297
-		$form = new GetPaid_Payment_Form( $post_id );
313
+            case 'shortcode' :
298 314
 
299
-		switch ( $column_name ) {
315
+                if ( $form->is_default() ) {
316
+                    echo '&mdash;';
317
+                } else {
318
+                    echo '<input onClick="this.select()" type="text" value="[getpaid form=' . esc_attr( $form->get_id() ) . ']" style="width: 100%;" readonly/>';
319
+                }
300 320
 
301
-			case 'earnings' :
302
-				echo wpinv_price( wpinv_format_amount( $form->get_earned() ) );
303
-				break;
321
+                break;
304 322
 
305
-			case 'refunds' :
306
-				echo wpinv_price( wpinv_format_amount( $form->get_refunded() ) );
307
-				break;
323
+            case 'items' :
308 324
 
309
-			case 'refunds' :
310
-				echo wpinv_price( wpinv_format_amount( $form->get_refunded() ) );
311
-				break;
325
+                $items = $form->get_items();
312 326
 
313
-			case 'shortcode' :
327
+                if ( $form->is_default() || empty( $items ) ) {
328
+                    echo '&mdash;';
329
+                    return;
330
+                }
314 331
 
315
-				if ( $form->is_default() ) {
316
-					echo '&mdash;';
317
-				} else {
318
-					echo '<input onClick="this.select()" type="text" value="[getpaid form=' . esc_attr( $form->get_id() ) . ']" style="width: 100%;" readonly/>';
319
-				}
332
+                $_items = array();
320 333
 
321
-				break;
334
+                foreach ( $items as $item ) {
335
+                    $url = $item->get_edit_url();
322 336
 
323
-			case 'items' :
337
+                    if ( empty( $url ) ) {
338
+                        $_items[] = sanitize_text_field( $item->get_name() );
339
+                    } else {
340
+                        $_items[] = sprintf(
341
+                            '<a href="%s">%s</a>',
342
+                            esc_url( $url ),
343
+                            sanitize_text_field( $item->get_name() )
344
+                        );
345
+                    }
324 346
 
325
-				$items = $form->get_items();
347
+                }
326 348
 
327
-				if ( $form->is_default() || empty( $items ) ) {
328
-					echo '&mdash;';
329
-					return;
330
-				}
349
+                echo implode( '<br>', $_items );
331 350
 
332
-				$_items = array();
351
+                break;
333 352
 
334
-				foreach ( $items as $item ) {
335
-					$url = $item->get_edit_url();
353
+        }
336 354
 
337
-					if ( empty( $url ) ) {
338
-						$_items[] = sanitize_text_field( $item->get_name() );
339
-					} else {
340
-						$_items[] = sprintf(
341
-							'<a href="%s">%s</a>',
342
-							esc_url( $url ),
343
-							sanitize_text_field( $item->get_name() )
344
-						);
345
-					}
355
+    }
346 356
 
347
-				}
357
+    /**
358
+     * Filters post states.
359
+     */
360
+    public static function filter_payment_form_state( $post_states, $post ) {
348 361
 
349
-				echo implode( '<br>', $_items );
362
+        if ( 'wpi_payment_form' == $post->post_type && wpinv_get_default_payment_form() == $post->ID ) {
363
+            $post_states[ 'default_form' ] = __( 'Default Payment Form', 'invoicing' );
364
+        }
365
+	
366
+        return $post_states;
350 367
 
351
-				break;
368
+    }
352 369
 
353
-		}
370
+    /**
371
+     * Returns an array of coupon table columns.
372
+     */
373
+    public static function discount_columns( $columns ) {
374
+
375
+        $columns = array(
376
+            'cb'                => $columns['cb'],
377
+            'title'             => __( 'Name', 'invoicing' ),
378
+            'code'              => __( 'Code', 'invoicing' ),
379
+            'amount'            => __( 'Amount', 'invoicing' ),
380
+            'usage'             => __( 'Usage / Limit', 'invoicing' ),
381
+            'start_date'        => __( 'Start Date', 'invoicing' ),
382
+            'expiry_date'       => __( 'Expiry Date', 'invoicing' ),
383
+        );
384
+
385
+        return apply_filters( 'wpi_discount_table_columns', $columns );
386
+    }
354 387
 
355
-	}
388
+    /**
389
+     * Filters post states.
390
+     */
391
+    public static function filter_discount_state( $post_states, $post ) {
356 392
 
357
-	/**
358
-	 * Filters post states.
359
-	 */
360
-	public static function filter_payment_form_state( $post_states, $post ) {
393
+        if ( 'wpi_discount' == $post->post_type ) {
361 394
 
362
-		if ( 'wpi_payment_form' == $post->post_type && wpinv_get_default_payment_form() == $post->ID ) {
363
-			$post_states[ 'default_form' ] = __( 'Default Payment Form', 'invoicing' );
364
-		}
365
-	
366
-		return $post_states;
395
+            $discount = new WPInv_Discount( $post );
367 396
 
368
-	}
397
+            $status = $discount->is_expired() ? 'expired' : $discount->get_status();
369 398
 
370
-	/**
371
-	 * Returns an array of coupon table columns.
372
-	 */
373
-	public static function discount_columns( $columns ) {
399
+            if ( $status != 'publish' ) {
400
+                return array(
401
+                    'discount_status' => wpinv_discount_status( $status ),
402
+                );
403
+            }
374 404
 
375
-		$columns = array(
376
-			'cb'                => $columns['cb'],
377
-			'title'             => __( 'Name', 'invoicing' ),
378
-			'code'              => __( 'Code', 'invoicing' ),
379
-			'amount'            => __( 'Amount', 'invoicing' ),
380
-			'usage'             => __( 'Usage / Limit', 'invoicing' ),
381
-			'start_date'        => __( 'Start Date', 'invoicing' ),
382
-			'expiry_date'       => __( 'Expiry Date', 'invoicing' ),
383
-		);
405
+            return array();
384 406
 
385
-		return apply_filters( 'wpi_discount_table_columns', $columns );
386
-	}
407
+        }
387 408
 
388
-	/**
389
-	 * Filters post states.
390
-	 */
391
-	public static function filter_discount_state( $post_states, $post ) {
409
+        return $post_states;
392 410
 
393
-		if ( 'wpi_discount' == $post->post_type ) {
411
+    }
394 412
 
395
-			$discount = new WPInv_Discount( $post );
413
+    /**
414
+     * Returns an array of items table columns.
415
+     */
416
+    public static function item_columns( $columns ) {
417
+        global $wpinv_euvat;
418
+
419
+        $columns = array(
420
+            'cb'                => $columns['cb'],
421
+            'title'             => __( 'Name', 'invoicing' ),
422
+            'price'             => __( 'Price', 'invoicing' ),
423
+            'vat_rule'          => __( 'VAT rule', 'invoicing' ),
424
+            'vat_class'         => __( 'VAT class', 'invoicing' ),
425
+            'type'              => __( 'Type', 'invoicing' ),
426
+            'shortcode'         => __( 'Shortcode', 'invoicing' ),
427
+        );
428
+
429
+        if ( ! $wpinv_euvat->allow_vat_rules() ) {
430
+            unset( $columns['vat_rule'] );
431
+        }
396 432
 
397
-			$status = $discount->is_expired() ? 'expired' : $discount->get_status();
433
+        if ( ! $wpinv_euvat->allow_vat_classes() ) {
434
+            unset( $columns['vat_class'] );
435
+        }
398 436
 
399
-			if ( $status != 'publish' ) {
400
-				return array(
401
-					'discount_status' => wpinv_discount_status( $status ),
402
-				);
403
-			}
437
+        return apply_filters( 'wpi_item_table_columns', $columns );
438
+    }
404 439
 
405
-			return array();
440
+    /**
441
+     * Returns an array of sortable items table columns.
442
+     */
443
+    public static function sortable_item_columns( $columns ) {
444
+
445
+        return array_merge(
446
+            $columns,
447
+            array(
448
+                'price'     => 'price',
449
+                'vat_rule'  => 'vat_rule',
450
+                'vat_class' => 'vat_class',
451
+                'type'      => 'type',
452
+            )
453
+        );
454
+
455
+    }
406 456
 
407
-		}
457
+    /**
458
+     * Displays items table columns.
459
+     */
460
+    public static function display_item_columns( $column_name, $post_id ) {
461
+        global $wpinv_euvat;
408 462
 
409
-		return $post_states;
463
+        $item = new WPInv_Item( $post_id );
410 464
 
411
-	}
465
+        switch ( $column_name ) {
412 466
 
413
-	/**
414
-	 * Returns an array of items table columns.
415
-	 */
416
-	public static function item_columns( $columns ) {
417
-		global $wpinv_euvat;
467
+            case 'price' :
418 468
 
419
-		$columns = array(
420
-			'cb'                => $columns['cb'],
421
-			'title'             => __( 'Name', 'invoicing' ),
422
-			'price'             => __( 'Price', 'invoicing' ),
423
-			'vat_rule'          => __( 'VAT rule', 'invoicing' ),
424
-			'vat_class'         => __( 'VAT class', 'invoicing' ),
425
-			'type'              => __( 'Type', 'invoicing' ),
426
-			'shortcode'         => __( 'Shortcode', 'invoicing' ),
427
-		);
469
+                if ( ! $item->is_recurring() ) {
470
+                    echo $item->get_the_price();
471
+                    break;
472
+                }
428 473
 
429
-		if ( ! $wpinv_euvat->allow_vat_rules() ) {
430
-			unset( $columns['vat_rule'] );
431
-		}
474
+                $price = wp_sprintf(
475
+                    __( '%s / %s', 'invoicing' ),
476
+                    $item->get_the_price(),
477
+                    getpaid_get_subscription_period_label( $item->get_recurring_period(), $item->get_recurring_interval(), '' )
478
+                );
432 479
 
433
-		if ( ! $wpinv_euvat->allow_vat_classes() ) {
434
-			unset( $columns['vat_class'] );
435
-		}
480
+                if ( $item->get_the_price() == $item->get_the_initial_price() ) {
481
+                    echo $price;
482
+                    break;
483
+                }
436 484
 
437
-		return apply_filters( 'wpi_item_table_columns', $columns );
438
-	}
485
+                echo $item->get_the_initial_price();
439 486
 
440
-	/**
441
-	 * Returns an array of sortable items table columns.
442
-	 */
443
-	public static function sortable_item_columns( $columns ) {
487
+                echo '<span class="meta">' . wp_sprintf( __( 'then %s', 'invoicing' ), $price )  .'</span>';
488
+                break;
444 489
 
445
-		return array_merge(
446
-			$columns,
447
-			array(
448
-				'price'     => 'price',
449
-				'vat_rule'  => 'vat_rule',
450
-				'vat_class' => 'vat_class',
451
-				'type'      => 'type',
452
-			)
453
-		);
490
+            case 'vat_rule' :
491
+                echo $wpinv_euvat->item_rule_label( $item->get_id() );
492
+                break;
454 493
 
455
-	}
494
+            case 'vat_class' :
495
+                echo $wpinv_euvat->item_class_label( $item->get_id() );
496
+                break;
456 497
 
457
-	/**
458
-	 * Displays items table columns.
459
-	 */
460
-	public static function display_item_columns( $column_name, $post_id ) {
461
-		global $wpinv_euvat;
498
+            case 'shortcode' :
499
+                echo '<input onClick="this.select()" type="text" value="[getpaid item=' . esc_attr( $item->get_id() ) . ' button=\'Buy Now\']" style="width: 100%;" readonly/>';
500
+                break;
462 501
 
463
-		$item = new WPInv_Item( $post_id );
502
+            case 'type' :
503
+                echo wpinv_item_type( $item->get_id() ) . '<span class="meta">' . $item->get_custom_singular_name() . '</span>';
504
+                break;
464 505
 
465
-		switch ( $column_name ) {
506
+        }
466 507
 
467
-			case 'price' :
508
+    }
468 509
 
469
-				if ( ! $item->is_recurring() ) {
470
-					echo $item->get_the_price();
471
-					break;
472
-				}
510
+    /**
511
+     * Lets users filter items using taxes.
512
+     */
513
+    public static function add_item_filters( $post_type ) {
514
+        $wpinv_euvat = getpaid_tax();
515
+
516
+        // Abort if we're not dealing with items.
517
+        if ( $post_type != 'wpi_item' ) {
518
+            return;
519
+        }
520
+
521
+        // Filter by vat rules.
522
+        if ( $wpinv_euvat->allow_vat_rules() ) {
523
+	
524
+            // Sanitize selected vat rule.
525
+            $vat_rule   = '';
526
+            $vat_rules  = $wpinv_euvat->get_rules();
527
+            if ( isset( $_GET['vat_rule'] ) ) {
528
+                $vat_rule   =  $_GET['vat_rule'];
529
+            }
530
+
531
+            // Filter by VAT rule.
532
+            echo wpinv_html_select(
533
+                array(
534
+                    'options'          => array_merge(
535
+                        array(
536
+                            '' => __( 'All VAT rules', 'invoicing' )
537
+                        ),
538
+                        $vat_rules
539
+                    ),
540
+                    'name'             => 'vat_rule',
541
+                    'id'               => 'vat_rule',
542
+                    'selected'         => in_array( $vat_rule, array_keys( $vat_rules ) ) ? $vat_rule : '',
543
+                    'show_option_all'  => false,
544
+                    'show_option_none' => false,
545
+                    'class'            => 'gdmbx2-text-medium',
546
+                )
547
+            );
548
+
549
+            // Filter by VAT class.
550
+        }
473 551
 
474
-				$price = wp_sprintf(
475
-					__( '%s / %s', 'invoicing' ),
476
-					$item->get_the_price(),
477
-					getpaid_get_subscription_period_label( $item->get_recurring_period(), $item->get_recurring_interval(), '' )
478
-				);
552
+        // Filter by vat class.
553
+        if ( $wpinv_euvat->allow_vat_classes() ) {
554
+	
555
+            // Sanitize selected vat rule.
556
+            $vat_class   = '';
557
+            $vat_classes = $wpinv_euvat->get_all_classes();
558
+            if ( isset( $_GET['vat_class'] ) ) {
559
+                $vat_class   =  $_GET['vat_class'];
560
+            }
561
+
562
+            echo wpinv_html_select(
563
+                array(
564
+                    'options'          => array_merge(
565
+                        array(
566
+                            '' => __( 'All VAT classes', 'invoicing' )
567
+                        ),
568
+                        $vat_classes
569
+                    ),
570
+                    'name'             => 'vat_class',
571
+                    'id'               => 'vat_class',
572
+                    'selected'         => in_array( $vat_class, array_keys( $vat_classes ) ) ? $vat_class : '',
573
+                    'show_option_all'  => false,
574
+                    'show_option_none' => false,
575
+                    'class'            => 'gdmbx2-text-medium',
576
+                )
577
+            );
479 578
 
480
-				if ( $item->get_the_price() == $item->get_the_initial_price() ) {
481
-					echo $price;
482
-					break;
483
-				}
579
+        }
484 580
 
485
-				echo $item->get_the_initial_price();
581
+        // Filter by item type.
582
+        $type   = '';
583
+        if ( isset( $_GET['type'] ) ) {
584
+            $type   =  $_GET['type'];
585
+        }
486 586
 
487
-				echo '<span class="meta">' . wp_sprintf( __( 'then %s', 'invoicing' ), $price )  .'</span>';
488
-				break;
587
+        echo wpinv_html_select(
588
+            array(
589
+                'options'          => array_merge(
590
+                    array(
591
+                        '' => __( 'All item types', 'invoicing' )
592
+                    ),
593
+                    wpinv_get_item_types()
594
+                ),
595
+                'name'             => 'type',
596
+                'id'               => 'type',
597
+                'selected'         => in_array( $type, wpinv_item_types() ) ? $type : '',
598
+                'show_option_all'  => false,
599
+                'show_option_none' => false,
600
+                'class'            => 'gdmbx2-text-medium',
601
+            )
602
+        );
603
+
604
+    }
489 605
 
490
-			case 'vat_rule' :
491
-				echo $wpinv_euvat->item_rule_label( $item->get_id() );
492
-				break;
606
+    /**
607
+     * Filters the item query.
608
+     */
609
+    public static function filter_item_query( $query ) {
493 610
 
494
-			case 'vat_class' :
495
-				echo $wpinv_euvat->item_class_label( $item->get_id() );
496
-				break;
611
+        // modify the query only if it admin and main query.
612
+        if ( ! ( is_admin() && $query->is_main_query() ) ){ 
613
+            return $query;
614
+        }
497 615
 
498
-			case 'shortcode' :
499
-				echo '<input onClick="this.select()" type="text" value="[getpaid item=' . esc_attr( $item->get_id() ) . ' button=\'Buy Now\']" style="width: 100%;" readonly/>';
500
-				break;
616
+        // we want to modify the query for our items.
617
+        if ( 'wpi_item' != $query->query['post_type'] ){
618
+            return $query;
619
+        }
501 620
 
502
-			case 'type' :
503
-				echo wpinv_item_type( $item->get_id() ) . '<span class="meta">' . $item->get_custom_singular_name() . '</span>';
504
-				break;
621
+        if ( empty( $query->query_vars['meta_query'] ) ) {
622
+            $query->query_vars['meta_query'] = array();
623
+        }
505 624
 
506
-		}
507
-
508
-	}
509
-
510
-	/**
511
-	 * Lets users filter items using taxes.
512
-	 */
513
-	public static function add_item_filters( $post_type ) {
514
-		$wpinv_euvat = getpaid_tax();
515
-
516
-		// Abort if we're not dealing with items.
517
-		if ( $post_type != 'wpi_item' ) {
518
-			return;
519
-		}
520
-
521
-		// Filter by vat rules.
522
-		if ( $wpinv_euvat->allow_vat_rules() ) {
523
-	
524
-			// Sanitize selected vat rule.
525
-			$vat_rule   = '';
526
-			$vat_rules  = $wpinv_euvat->get_rules();
527
-			if ( isset( $_GET['vat_rule'] ) ) {
528
-				$vat_rule   =  $_GET['vat_rule'];
529
-			}
530
-
531
-			// Filter by VAT rule.
532
-			echo wpinv_html_select(
533
-				array(
534
-					'options'          => array_merge(
535
-						array(
536
-							'' => __( 'All VAT rules', 'invoicing' )
537
-						),
538
-						$vat_rules
539
-					),
540
-					'name'             => 'vat_rule',
541
-					'id'               => 'vat_rule',
542
-					'selected'         => in_array( $vat_rule, array_keys( $vat_rules ) ) ? $vat_rule : '',
543
-					'show_option_all'  => false,
544
-					'show_option_none' => false,
545
-					'class'            => 'gdmbx2-text-medium',
546
-				)
547
-			);
548
-
549
-			// Filter by VAT class.
550
-		}
551
-
552
-		// Filter by vat class.
553
-		if ( $wpinv_euvat->allow_vat_classes() ) {
554
-	
555
-			// Sanitize selected vat rule.
556
-			$vat_class   = '';
557
-			$vat_classes = $wpinv_euvat->get_all_classes();
558
-			if ( isset( $_GET['vat_class'] ) ) {
559
-				$vat_class   =  $_GET['vat_class'];
560
-			}
561
-
562
-			echo wpinv_html_select(
563
-				array(
564
-					'options'          => array_merge(
565
-						array(
566
-							'' => __( 'All VAT classes', 'invoicing' )
567
-						),
568
-						$vat_classes
569
-					),
570
-					'name'             => 'vat_class',
571
-					'id'               => 'vat_class',
572
-					'selected'         => in_array( $vat_class, array_keys( $vat_classes ) ) ? $vat_class : '',
573
-					'show_option_all'  => false,
574
-					'show_option_none' => false,
575
-					'class'            => 'gdmbx2-text-medium',
576
-				)
577
-			);
578
-
579
-		}
580
-
581
-		// Filter by item type.
582
-		$type   = '';
583
-		if ( isset( $_GET['type'] ) ) {
584
-			$type   =  $_GET['type'];
585
-		}
586
-
587
-		echo wpinv_html_select(
588
-			array(
589
-				'options'          => array_merge(
590
-					array(
591
-						'' => __( 'All item types', 'invoicing' )
592
-					),
593
-					wpinv_get_item_types()
594
-				),
595
-				'name'             => 'type',
596
-				'id'               => 'type',
597
-				'selected'         => in_array( $type, wpinv_item_types() ) ? $type : '',
598
-				'show_option_all'  => false,
599
-				'show_option_none' => false,
600
-				'class'            => 'gdmbx2-text-medium',
601
-			)
602
-		);
603
-
604
-	}
605
-
606
-	/**
607
-	 * Filters the item query.
608
-	 */
609
-	public static function filter_item_query( $query ) {
610
-
611
-		// modify the query only if it admin and main query.
612
-		if ( ! ( is_admin() && $query->is_main_query() ) ){ 
613
-			return $query;
614
-		}
615
-
616
-		// we want to modify the query for our items.
617
-		if ( 'wpi_item' != $query->query['post_type'] ){
618
-			return $query;
619
-		}
620
-
621
-		if ( empty( $query->query_vars['meta_query'] ) ) {
622
-			$query->query_vars['meta_query'] = array();
623
-		}
624
-
625
-		// Filter vat rule type
625
+        // Filter vat rule type
626 626
         if ( ! empty( $_GET['vat_rule'] ) ) {
627 627
             $query->query_vars['meta_query'][] = array(
628 628
                 'key'     => '_wpinv_vat_rule',
@@ -647,94 +647,94 @@  discard block
 block discarded – undo
647 647
                 'value'   => sanitize_text_field( $_GET['type'] ),
648 648
                 'compare' => '='
649 649
             );
650
-		}
651
-
652
-	}
653
-
654
-	/**
655
-	 * Reorders items.
656
-	 */
657
-	public static function reorder_items( $vars ) {
658
-		global $typenow;
659
-
660
-		if ( 'wpi_item' !== $typenow || empty( $vars['orderby'] ) ) {
661
-			return $vars;
662
-		}
663
-
664
-		// By item type.
665
-		if ( 'type' == $vars['orderby'] ) {
666
-			return array_merge(
667
-				$vars,
668
-				array(
669
-					'meta_key' => '_wpinv_type',
670
-					'orderby'  => 'meta_value'
671
-				)
672
-			);
673
-		}
674
-
675
-		// By vat class.
676
-		if ( 'vat_class' == $vars['orderby'] ) {
677
-			return array_merge(
678
-				$vars,
679
-				array(
680
-					'meta_key' => '_wpinv_vat_class',
681
-					'orderby'  => 'meta_value'
682
-				)
683
-			);
684
-		}
685
-
686
-		// By vat rule.
687
-		if ( 'vat_rule' == $vars['orderby'] ) {
688
-			return array_merge(
689
-				$vars,
690
-				array(
691
-					'meta_key' => '_wpinv_vat_rule',
692
-					'orderby'  => 'meta_value'
693
-				)
694
-			);
695
-		}
696
-
697
-		// By price.
698
-		if ( 'price' == $vars['orderby'] ) {
699
-			return array_merge(
700
-				$vars,
701
-				array(
702
-					'meta_key' => '_wpinv_price',
703
-					'orderby'  => 'meta_value_num'
704
-				)
705
-			);
706
-		}
707
-
708
-		return $vars;
709
-
710
-	}
711
-
712
-	/**
713
-	 * Fired when deleting a post.
714
-	 */
715
-	public static function delete_post( $post_id ) {
716
-
717
-		switch ( get_post_type( $post_id ) ) {
718
-
719
-			case 'wpi_item' :
720
-				do_action( "getpaid_before_delete_item", new WPInv_Item( $post_id ) );
721
-				break;
722
-
723
-			case 'wpi_payment_form' :
724
-				do_action( "getpaid_before_delete_payment_form", new GetPaid_Payment_Form( $post_id ) );
725
-				break;
726
-
727
-			case 'wpi_discount' :
728
-				do_action( "getpaid_before_delete_discount", new WPInv_Discount( $post_id ) );
729
-				break;
730
-
731
-			case 'wpi_invoice' :
732
-				$invoice = new WPInv_Invoice( $post_id );
733
-				do_action( "getpaid_before_delete_invoice", $invoice );
734
-				$invoice->get_data_store()->delete_items( $invoice );
735
-				$invoice->get_data_store()->delete_special_fields( $invoice );
736
-				break;
737
-		}
738
-	}
650
+        }
651
+
652
+    }
653
+
654
+    /**
655
+     * Reorders items.
656
+     */
657
+    public static function reorder_items( $vars ) {
658
+        global $typenow;
659
+
660
+        if ( 'wpi_item' !== $typenow || empty( $vars['orderby'] ) ) {
661
+            return $vars;
662
+        }
663
+
664
+        // By item type.
665
+        if ( 'type' == $vars['orderby'] ) {
666
+            return array_merge(
667
+                $vars,
668
+                array(
669
+                    'meta_key' => '_wpinv_type',
670
+                    'orderby'  => 'meta_value'
671
+                )
672
+            );
673
+        }
674
+
675
+        // By vat class.
676
+        if ( 'vat_class' == $vars['orderby'] ) {
677
+            return array_merge(
678
+                $vars,
679
+                array(
680
+                    'meta_key' => '_wpinv_vat_class',
681
+                    'orderby'  => 'meta_value'
682
+                )
683
+            );
684
+        }
685
+
686
+        // By vat rule.
687
+        if ( 'vat_rule' == $vars['orderby'] ) {
688
+            return array_merge(
689
+                $vars,
690
+                array(
691
+                    'meta_key' => '_wpinv_vat_rule',
692
+                    'orderby'  => 'meta_value'
693
+                )
694
+            );
695
+        }
696
+
697
+        // By price.
698
+        if ( 'price' == $vars['orderby'] ) {
699
+            return array_merge(
700
+                $vars,
701
+                array(
702
+                    'meta_key' => '_wpinv_price',
703
+                    'orderby'  => 'meta_value_num'
704
+                )
705
+            );
706
+        }
707
+
708
+        return $vars;
709
+
710
+    }
711
+
712
+    /**
713
+     * Fired when deleting a post.
714
+     */
715
+    public static function delete_post( $post_id ) {
716
+
717
+        switch ( get_post_type( $post_id ) ) {
718
+
719
+            case 'wpi_item' :
720
+                do_action( "getpaid_before_delete_item", new WPInv_Item( $post_id ) );
721
+                break;
722
+
723
+            case 'wpi_payment_form' :
724
+                do_action( "getpaid_before_delete_payment_form", new GetPaid_Payment_Form( $post_id ) );
725
+                break;
726
+
727
+            case 'wpi_discount' :
728
+                do_action( "getpaid_before_delete_discount", new WPInv_Discount( $post_id ) );
729
+                break;
730
+
731
+            case 'wpi_invoice' :
732
+                $invoice = new WPInv_Invoice( $post_id );
733
+                do_action( "getpaid_before_delete_invoice", $invoice );
734
+                $invoice->get_data_store()->delete_items( $invoice );
735
+                $invoice->get_data_store()->delete_special_fields( $invoice );
736
+                break;
737
+        }
738
+    }
739 739
 
740 740
 }
Please login to merge, or discard this patch.