Passed
Push — master ( 11ea61...3d88bf )
by Brian
05:49
created
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.
includes/payments/class-getpaid-checkout.php 1 patch
Indentation   +190 added lines, -190 removed lines patch added patch discarded remove patch
@@ -12,170 +12,170 @@  discard block
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Checkout {
14 14
 
15
-	/**
16
-	 * @var GetPaid_Payment_Form_Submission
17
-	 */
18
-	protected $payment_form_submission;
19
-
20
-	/**
21
-	 * Class constructor.
22
-	 * 
23
-	 * @param GetPaid_Payment_Form_Submission $submission
24
-	 */
25
-	public function __construct( $submission ) {
26
-		$this->payment_form_submission = $submission;
27
-	}
28
-
29
-	/**
30
-	 * Processes the checkout.
31
-	 *
32
-	 */
33
-	public function process_checkout() {
34
-
35
-		// Validate the submission.
36
-		$this->validate_submission();
37
-
38
-		// Prepare the invoice.
39
-		$items      = $this->get_submission_items();
40
-		$invoice    = $this->get_submission_invoice();
41
-		$invoice    = $this->process_submission_invoice( $invoice, $items );
42
-		$prepared   = $this->prepare_submission_data_for_saving( $invoice );
43
-
44
-		// Save the invoice.
45
-		$invoice->recalculate_total();
15
+    /**
16
+     * @var GetPaid_Payment_Form_Submission
17
+     */
18
+    protected $payment_form_submission;
19
+
20
+    /**
21
+     * Class constructor.
22
+     * 
23
+     * @param GetPaid_Payment_Form_Submission $submission
24
+     */
25
+    public function __construct( $submission ) {
26
+        $this->payment_form_submission = $submission;
27
+    }
28
+
29
+    /**
30
+     * Processes the checkout.
31
+     *
32
+     */
33
+    public function process_checkout() {
34
+
35
+        // Validate the submission.
36
+        $this->validate_submission();
37
+
38
+        // Prepare the invoice.
39
+        $items      = $this->get_submission_items();
40
+        $invoice    = $this->get_submission_invoice();
41
+        $invoice    = $this->process_submission_invoice( $invoice, $items );
42
+        $prepared   = $this->prepare_submission_data_for_saving( $invoice );
43
+
44
+        // Save the invoice.
45
+        $invoice->recalculate_total();
46 46
         $invoice->save();
47 47
 
48
-		// Send to the gateway.
49
-		$this->post_process_submission( $invoice, $prepared );
50
-	}
48
+        // Send to the gateway.
49
+        $this->post_process_submission( $invoice, $prepared );
50
+    }
51 51
 
52
-	/**
53
-	 * Validates the submission.
54
-	 *
55
-	 */
56
-	protected function validate_submission() {
52
+    /**
53
+     * Validates the submission.
54
+     *
55
+     */
56
+    protected function validate_submission() {
57 57
 
58
-		$submission = $this->payment_form_submission;
59
-		$data       = $submission->get_data();
58
+        $submission = $this->payment_form_submission;
59
+        $data       = $submission->get_data();
60 60
 
61
-		// Do we have an error?
61
+        // Do we have an error?
62 62
         if ( ! empty( $submission->last_error ) ) {
63
-			wp_send_json_error( $submission->last_error );
63
+            wp_send_json_error( $submission->last_error );
64 64
         }
65 65
 
66
-		// We need a billing email.
66
+        // We need a billing email.
67 67
         if ( ! $submission->has_billing_email() ) {
68 68
             wp_send_json_error( __( 'Provide a valid billing email.', 'invoicing' ) );
69
-		}
69
+        }
70 70
 
71
-		// Non-recurring gateways should not be allowed to process recurring invoices.
72
-		if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) {
73
-			wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) );
74
-		}
71
+        // Non-recurring gateways should not be allowed to process recurring invoices.
72
+        if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) {
73
+            wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) );
74
+        }
75 75
 
76
-		// Ensure the gateway is active.
77
-		if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) {
78
-			wpinv_set_error( 'invalid_gateway', __( 'The selected payment gateway is not active', 'invoicing' ) );
79
-		}
76
+        // Ensure the gateway is active.
77
+        if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) {
78
+            wpinv_set_error( 'invalid_gateway', __( 'The selected payment gateway is not active', 'invoicing' ) );
79
+        }
80 80
 
81
-		// Clear any existing errors.
82
-		wpinv_clear_errors();
81
+        // Clear any existing errors.
82
+        wpinv_clear_errors();
83 83
 
84
-		// Allow themes and plugins to hook to errors
85
-		do_action( 'getpaid_checkout_error_checks', $submission );
84
+        // Allow themes and plugins to hook to errors
85
+        do_action( 'getpaid_checkout_error_checks', $submission );
86 86
 
87
-		// Do we have any errors?
87
+        // Do we have any errors?
88 88
         if ( wpinv_get_errors() ) {
89 89
             wp_send_json_error( getpaid_get_errors_html() );
90
-		}
90
+        }
91 91
 
92
-	}
92
+    }
93 93
 
94
-	/**
95
-	 * Retrieves submission items.
96
-	 *
97
-	 * @return GetPaid_Form_Item[]
98
-	 */
99
-	protected function get_submission_items() {
94
+    /**
95
+     * Retrieves submission items.
96
+     *
97
+     * @return GetPaid_Form_Item[]
98
+     */
99
+    protected function get_submission_items() {
100 100
 
101
-		$items = $this->payment_form_submission->get_items();
101
+        $items = $this->payment_form_submission->get_items();
102 102
 
103 103
         // Ensure that we have items.
104 104
         if ( empty( $items ) && ! $this->payment_form_submission->has_fees() ) {
105 105
             wp_send_json_error( __( 'Please provide at least one item or amount.', 'invoicing' ) );
106
-		}
107
-
108
-		return $items;
109
-	}
110
-
111
-	/**
112
-	 * Retrieves submission invoice.
113
-	 *
114
-	 * @return WPInv_Invoice
115
-	 */
116
-	protected function get_submission_invoice() {
117
-		$submission = $this->payment_form_submission;
118
-
119
-		if ( ! $submission->has_invoice() ) {
120
-			$invoice = new WPInv_Invoice();
121
-			$invoice->created_via( 'payment_form' );
122
-			return $invoice;
123 106
         }
124 107
 
125
-		$invoice = $submission->get_invoice();
108
+        return $items;
109
+    }
110
+
111
+    /**
112
+     * Retrieves submission invoice.
113
+     *
114
+     * @return WPInv_Invoice
115
+     */
116
+    protected function get_submission_invoice() {
117
+        $submission = $this->payment_form_submission;
126 118
 
127
-		// Make sure that it is neither paid or refunded.
128
-		if ( $invoice->is_paid() || $invoice->is_refunded() ) {
129
-			wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) );
130
-		}
119
+        if ( ! $submission->has_invoice() ) {
120
+            $invoice = new WPInv_Invoice();
121
+            $invoice->created_via( 'payment_form' );
122
+            return $invoice;
123
+        }
131 124
 
132
-		return $invoice;
133
-	}
125
+        $invoice = $submission->get_invoice();
134 126
 
135
-	/**
136
-	 * Processes the submission invoice.
137
-	 *
138
-	 * @param WPInv_Invoice $invoice
139
-	 * @param GetPaid_Form_Item[] $items
140
-	 * @return WPInv_Invoice
141
-	 */
142
-	protected function process_submission_invoice( $invoice, $items ) {
127
+        // Make sure that it is neither paid or refunded.
128
+        if ( $invoice->is_paid() || $invoice->is_refunded() ) {
129
+            wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) );
130
+        }
143 131
 
144
-		$submission = $this->payment_form_submission;
145
-		$data       = $submission->get_data();
132
+        return $invoice;
133
+    }
146 134
 
147
-		// Set-up the invoice details.
148
-		$invoice->set_email( sanitize_email( $submission->get_billing_email() ) );
149
-		$invoice->set_user_id( $this->get_submission_customer() );
150
-		$invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) );
135
+    /**
136
+     * Processes the submission invoice.
137
+     *
138
+     * @param WPInv_Invoice $invoice
139
+     * @param GetPaid_Form_Item[] $items
140
+     * @return WPInv_Invoice
141
+     */
142
+    protected function process_submission_invoice( $invoice, $items ) {
143
+
144
+        $submission = $this->payment_form_submission;
145
+        $data       = $submission->get_data();
146
+
147
+        // Set-up the invoice details.
148
+        $invoice->set_email( sanitize_email( $submission->get_billing_email() ) );
149
+        $invoice->set_user_id( $this->get_submission_customer() );
150
+        $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) );
151 151
         $invoice->set_items( $items );
152 152
         $invoice->set_fees( $submission->get_fees() );
153 153
         $invoice->set_taxes( $submission->get_taxes() );
154
-		$invoice->set_discounts( $submission->get_discounts() );
155
-		$invoice->set_gateway( $data['wpi-gateway'] );
154
+        $invoice->set_discounts( $submission->get_discounts() );
155
+        $invoice->set_gateway( $data['wpi-gateway'] );
156 156
 
157
-		if ( $submission->has_discount_code() ) {
157
+        if ( $submission->has_discount_code() ) {
158 158
             $invoice->set_discount_code( $submission->get_discount_code() );
159
-		}
160
-
161
-		getpaid_maybe_add_default_address( $invoice );
162
-		return $invoice;
163
-	}
164
-
165
-	/**
166
-	 * Retrieves the submission's customer.
167
-	 *
168
-	 * @return int The customer id.
169
-	 */
170
-	protected function get_submission_customer() {
171
-		$submission = $this->payment_form_submission;
172
-
173
-		// If this is an existing invoice...
174
-		if ( $submission->has_invoice() ) {
175
-			return $submission->get_invoice()->get_user_id();
176
-		}
177
-
178
-		// (Maybe) create the user.
159
+        }
160
+
161
+        getpaid_maybe_add_default_address( $invoice );
162
+        return $invoice;
163
+    }
164
+
165
+    /**
166
+     * Retrieves the submission's customer.
167
+     *
168
+     * @return int The customer id.
169
+     */
170
+    protected function get_submission_customer() {
171
+        $submission = $this->payment_form_submission;
172
+
173
+        // If this is an existing invoice...
174
+        if ( $submission->has_invoice() ) {
175
+            return $submission->get_invoice()->get_user_id();
176
+        }
177
+
178
+        // (Maybe) create the user.
179 179
         $user = get_current_user_id();
180 180
 
181 181
         if ( empty( $user ) ) {
@@ -192,31 +192,31 @@  discard block
 block discarded – undo
192 192
 
193 193
         if ( is_numeric( $user ) ) {
194 194
             return $user;
195
-		}
195
+        }
196 196
 
197
-		return $user->ID;
197
+        return $user->ID;
198 198
 
199
-	}
199
+    }
200 200
 
201
-	/**
201
+    /**
202 202
      * Prepares submission data for saving to the database.
203 203
      *
204
-	 * @param WPInv_Invoice $invoice
204
+     * @param WPInv_Invoice $invoice
205 205
      */
206 206
     public function prepare_submission_data_for_saving( &$invoice ) {
207 207
 
208
-		$submission = $this->payment_form_submission;
208
+        $submission = $this->payment_form_submission;
209 209
 
210
-		// Prepared submission details.
210
+        // Prepared submission details.
211 211
         $prepared = array();
212 212
 
213 213
         // Raw submission details.
214
-		$data = $submission->get_data();
214
+        $data = $submission->get_data();
215 215
 		
216
-		// Loop throught the submitted details.
216
+        // Loop throught the submitted details.
217 217
         foreach ( $submission->get_payment_form()->get_elements() as $field ) {
218 218
 
219
-			// Skip premade fields.
219
+            // Skip premade fields.
220 220
             if ( ! empty( $field['premade'] ) ) {
221 221
                 continue;
222 222
             }
@@ -259,21 +259,21 @@  discard block
 block discarded – undo
259 259
                 $prepared[ wpinv_clean( $label ) ] = wpinv_clean( $data[ $field['id'] ] );
260 260
             }
261 261
 
262
-		}
262
+        }
263 263
 		
264
-		return $prepared;
264
+        return $prepared;
265 265
 
266
-	}
266
+    }
267 267
 
268
-	/**
269
-	 * Confirms the submission is valid and send users to the gateway.
270
-	 *
271
-	 * @param WPInv_Invoice $invoice
272
-	 * @param array $prepared_payment_form_data
273
-	 */
274
-	protected function post_process_submission( $invoice, $prepared_payment_form_data ) {
268
+    /**
269
+     * Confirms the submission is valid and send users to the gateway.
270
+     *
271
+     * @param WPInv_Invoice $invoice
272
+     * @param array $prepared_payment_form_data
273
+     */
274
+    protected function post_process_submission( $invoice, $prepared_payment_form_data ) {
275 275
 
276
-		// Ensure the invoice exists.
276
+        // Ensure the invoice exists.
277 277
         if ( ! $invoice->exists() ) {
278 278
             wp_send_json_error( __( 'An error occured while saving your invoice.', 'invoicing' ) );
279 279
         }
@@ -281,65 +281,65 @@  discard block
 block discarded – undo
281 281
         // Save payment form data.
282 282
         if ( ! empty( $prepared_payment_form_data ) ) {
283 283
             update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data );
284
-		}
284
+        }
285 285
 
286
-		// Backwards compatibility.
286
+        // Backwards compatibility.
287 287
         add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) );
288 288
 
289
-		$this->process_payment( $invoice );
289
+        $this->process_payment( $invoice );
290 290
 
291 291
         // If we are here, there was an error.
292
-		wpinv_send_back_to_checkout();
292
+        wpinv_send_back_to_checkout();
293 293
 
294
-	}
294
+    }
295 295
 
296
-	/**
297
-	 * Processes the actual payment.
298
-	 *
299
-	 * @param WPInv_Invoice $invoice
300
-	 */
301
-	protected function process_payment( $invoice ) {
296
+    /**
297
+     * Processes the actual payment.
298
+     *
299
+     * @param WPInv_Invoice $invoice
300
+     */
301
+    protected function process_payment( $invoice ) {
302 302
 
303
-		$submission = $this->payment_form_submission;
303
+        $submission = $this->payment_form_submission;
304 304
 
305
-		// No need to send free invoices to the gateway.
306
-		if ( $invoice->is_free() ) {
307
-			$invoice->set_gateway( 'none' );
308
-			$invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true );
309
-			$invoice->mark_paid();
310
-			wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) );
311
-		}
305
+        // No need to send free invoices to the gateway.
306
+        if ( $invoice->is_free() ) {
307
+            $invoice->set_gateway( 'none' );
308
+            $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true );
309
+            $invoice->mark_paid();
310
+            wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) );
311
+        }
312 312
 
313
-		// Clear any checkout errors.
314
-		wpinv_clear_errors();
313
+        // Clear any checkout errors.
314
+        wpinv_clear_errors();
315 315
 
316
-		// Fires before sending to the gateway.
317
-		do_action( 'getpaid_checkout_before_gateway', $invoice, $submission );
316
+        // Fires before sending to the gateway.
317
+        do_action( 'getpaid_checkout_before_gateway', $invoice, $submission );
318 318
 
319
-		// Allow the sumission data to be modified before it is sent to the gateway.
320
-		$submission_data    = $submission->get_data();
321
-		$submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice );
322
-		$submission_data    = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice );
319
+        // Allow the sumission data to be modified before it is sent to the gateway.
320
+        $submission_data    = $submission->get_data();
321
+        $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice );
322
+        $submission_data    = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice );
323 323
 
324
-		// Validate the currency.
325
-		if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) {
326
-			wpinv_set_error( 'invalid_currency', __( 'The chosen payment gateway does not support the invoice currency', 'invoicing' ) );
327
-		}
324
+        // Validate the currency.
325
+        if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) {
326
+            wpinv_set_error( 'invalid_currency', __( 'The chosen payment gateway does not support the invoice currency', 'invoicing' ) );
327
+        }
328 328
 
329
-		// Check to see if we have any errors.
330
-		if ( wpinv_get_errors() ) {
331
-			wpinv_send_back_to_checkout();
332
-		}
329
+        // Check to see if we have any errors.
330
+        if ( wpinv_get_errors() ) {
331
+            wpinv_send_back_to_checkout();
332
+        }
333 333
 
334
-		// Send info to the gateway for payment processing
335
-		do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission );
334
+        // Send info to the gateway for payment processing
335
+        do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission );
336 336
 
337
-		// Backwards compatibility.
338
-		wpinv_send_to_gateway( $submission_gateway, $invoice );
337
+        // Backwards compatibility.
338
+        wpinv_send_to_gateway( $submission_gateway, $invoice );
339 339
 
340
-	}
340
+    }
341 341
 
342
-	/**
342
+    /**
343 343
      * Sends a redrect response to payment details.
344 344
      *
345 345
      */
Please login to merge, or discard this patch.
includes/payments/class-getpaid-payment-form-submission.php 1 patch
Indentation   +736 added lines, -736 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,187 +10,187 @@  discard block
 block discarded – undo
10 10
 class GetPaid_Payment_Form_Submission {
11 11
 
12 12
     /**
13
-	 * Submission ID
14
-	 *
15
-	 * @var string
16
-	 */
17
-	public $id = null;
18
-
19
-	/**
20
-	 * The raw submission data.
21
-	 *
22
-	 * @var array
23
-	 */
24
-	protected $data = null;
25
-
26
-	/**
27
-	 * Submission totals
28
-	 *
29
-	 * @var array
30
-	 */
31
-	protected $totals = array(
32
-
33
-		'subtotal'      => array(
34
-			'initial'   => 0,
35
-			'recurring' => 0,
36
-		),
37
-
38
-		'discount'      => array(
39
-			'initial'   => 0,
40
-			'recurring' => 0,
41
-		),
42
-
43
-		'fees'          => array(
44
-			'initial'   => 0,
45
-			'recurring' => 0,
46
-		),
47
-
48
-		'taxes'         => array(
49
-			'initial'   => 0,
50
-			'recurring' => 0,
51
-		),
52
-
53
-	);
54
-
55
-	/**
56
-	 * Sets the associated payment form.
57
-	 *
58
-	 * @var GetPaid_Payment_Form
59
-	 */
13
+     * Submission ID
14
+     *
15
+     * @var string
16
+     */
17
+    public $id = null;
18
+
19
+    /**
20
+     * The raw submission data.
21
+     *
22
+     * @var array
23
+     */
24
+    protected $data = null;
25
+
26
+    /**
27
+     * Submission totals
28
+     *
29
+     * @var array
30
+     */
31
+    protected $totals = array(
32
+
33
+        'subtotal'      => array(
34
+            'initial'   => 0,
35
+            'recurring' => 0,
36
+        ),
37
+
38
+        'discount'      => array(
39
+            'initial'   => 0,
40
+            'recurring' => 0,
41
+        ),
42
+
43
+        'fees'          => array(
44
+            'initial'   => 0,
45
+            'recurring' => 0,
46
+        ),
47
+
48
+        'taxes'         => array(
49
+            'initial'   => 0,
50
+            'recurring' => 0,
51
+        ),
52
+
53
+    );
54
+
55
+    /**
56
+     * Sets the associated payment form.
57
+     *
58
+     * @var GetPaid_Payment_Form
59
+     */
60 60
     protected $payment_form = null;
61 61
 
62 62
     /**
63
-	 * The country for the submission.
64
-	 *
65
-	 * @var string
66
-	 */
67
-	public $country = null;
68
-
69
-    /**
70
-	 * The state for the submission.
71
-	 *
72
-	 * @since 1.0.19
73
-	 * @var string
74
-	 */
75
-	public $state = null;
76
-
77
-	/**
78
-	 * The invoice associated with the submission.
79
-	 *
80
-	 * @var WPInv_Invoice
81
-	 */
82
-	protected $invoice = null;
83
-
84
-	/**
85
-	 * The recurring item for the submission.
86
-	 *
87
-	 * @var int
88
-	 */
89
-	public $has_recurring = 0;
90
-
91
-	/**
92
-	 * An array of fees for the submission.
93
-	 *
94
-	 * @var array
95
-	 */
96
-	protected $fees = array();
97
-
98
-	/**
99
-	 * An array of discounts for the submission.
100
-	 *
101
-	 * @var array
102
-	 */
103
-	protected $discounts = array();
104
-
105
-	/**
106
-	 * An array of taxes for the submission.
107
-	 *
108
-	 * @var array
109
-	 */
110
-	protected $taxes = array();
111
-
112
-	/**
113
-	 * An array of items for the submission.
114
-	 *
115
-	 * @var GetPaid_Form_Item[]
116
-	 */
117
-	protected $items = array();
118
-
119
-	/**
120
-	 * The last error.
121
-	 *
122
-	 * @var string
123
-	 */
124
-	public $last_error = null;
125
-
126
-    /**
127
-	 * Class constructor.
128
-	 *
129
-	 */
130
-	public function __construct() {
131
-
132
-		// Set the state and country to the default state and country.
133
-		$this->country = wpinv_default_billing_country();
134
-		$this->state   = wpinv_get_default_state();
135
-
136
-		// Do we have an actual submission?
137
-		if ( isset( $_POST['getpaid_payment_form_submission'] ) ) {
138
-			$this->load_data( $_POST );
139
-		}
140
-
141
-	}
142
-
143
-	/**
144
-	 * Loads submission data.
145
-	 *
146
-	 * @param array $data
147
-	 */
148
-	public function load_data( $data ) {
149
-
150
-		// Remove slashes from the submitted data...
151
-		$data       = wp_unslash( $data );
152
-
153
-		// Allow plugins to filter the data.
154
-		$data       = apply_filters( 'getpaid_submission_data', $data, $this );
155
-
156
-		// Cache it...
157
-		$this->data = $data;
158
-
159
-		// Then generate a unique id from the data.
160
-		$this->id   = md5( wp_json_encode( $data ) );
161
-
162
-		// Finally, process the submission.
163
-		try {
164
-
165
-			// Each process is passed an instance of the class (with reference)
166
-			// and should throw an Exception whenever it encounters one.
167
-			$processors = apply_filters(
168
-				'getpaid_payment_form_submission_processors',
169
-				array(
170
-					array( $this, 'process_payment_form' ),
171
-					array( $this, 'process_invoice' ),
172
-					array( $this, 'process_fees' ),
173
-					array( $this, 'process_items' ),
174
-					array( $this, 'process_taxes' ),
175
-					array( $this, 'process_discount' ),
176
-				),
177
-				$this		
178
-			);
179
-
180
-			foreach ( $processors as $processor ) {
181
-				call_user_func_array( $processor, array( &$this ) );
182
-			}
183
-
184
-		} catch ( Exception $e ) {
185
-			$this->last_error = $e->getMessage();
186
-		}
187
-
188
-		// Fired when we are done processing a submission.
189
-		do_action_ref_array( 'getpaid_process_submission', array( &$this ) );
190
-
191
-	}
192
-
193
-	/*
63
+     * The country for the submission.
64
+     *
65
+     * @var string
66
+     */
67
+    public $country = null;
68
+
69
+    /**
70
+     * The state for the submission.
71
+     *
72
+     * @since 1.0.19
73
+     * @var string
74
+     */
75
+    public $state = null;
76
+
77
+    /**
78
+     * The invoice associated with the submission.
79
+     *
80
+     * @var WPInv_Invoice
81
+     */
82
+    protected $invoice = null;
83
+
84
+    /**
85
+     * The recurring item for the submission.
86
+     *
87
+     * @var int
88
+     */
89
+    public $has_recurring = 0;
90
+
91
+    /**
92
+     * An array of fees for the submission.
93
+     *
94
+     * @var array
95
+     */
96
+    protected $fees = array();
97
+
98
+    /**
99
+     * An array of discounts for the submission.
100
+     *
101
+     * @var array
102
+     */
103
+    protected $discounts = array();
104
+
105
+    /**
106
+     * An array of taxes for the submission.
107
+     *
108
+     * @var array
109
+     */
110
+    protected $taxes = array();
111
+
112
+    /**
113
+     * An array of items for the submission.
114
+     *
115
+     * @var GetPaid_Form_Item[]
116
+     */
117
+    protected $items = array();
118
+
119
+    /**
120
+     * The last error.
121
+     *
122
+     * @var string
123
+     */
124
+    public $last_error = null;
125
+
126
+    /**
127
+     * Class constructor.
128
+     *
129
+     */
130
+    public function __construct() {
131
+
132
+        // Set the state and country to the default state and country.
133
+        $this->country = wpinv_default_billing_country();
134
+        $this->state   = wpinv_get_default_state();
135
+
136
+        // Do we have an actual submission?
137
+        if ( isset( $_POST['getpaid_payment_form_submission'] ) ) {
138
+            $this->load_data( $_POST );
139
+        }
140
+
141
+    }
142
+
143
+    /**
144
+     * Loads submission data.
145
+     *
146
+     * @param array $data
147
+     */
148
+    public function load_data( $data ) {
149
+
150
+        // Remove slashes from the submitted data...
151
+        $data       = wp_unslash( $data );
152
+
153
+        // Allow plugins to filter the data.
154
+        $data       = apply_filters( 'getpaid_submission_data', $data, $this );
155
+
156
+        // Cache it...
157
+        $this->data = $data;
158
+
159
+        // Then generate a unique id from the data.
160
+        $this->id   = md5( wp_json_encode( $data ) );
161
+
162
+        // Finally, process the submission.
163
+        try {
164
+
165
+            // Each process is passed an instance of the class (with reference)
166
+            // and should throw an Exception whenever it encounters one.
167
+            $processors = apply_filters(
168
+                'getpaid_payment_form_submission_processors',
169
+                array(
170
+                    array( $this, 'process_payment_form' ),
171
+                    array( $this, 'process_invoice' ),
172
+                    array( $this, 'process_fees' ),
173
+                    array( $this, 'process_items' ),
174
+                    array( $this, 'process_taxes' ),
175
+                    array( $this, 'process_discount' ),
176
+                ),
177
+                $this		
178
+            );
179
+
180
+            foreach ( $processors as $processor ) {
181
+                call_user_func_array( $processor, array( &$this ) );
182
+            }
183
+
184
+        } catch ( Exception $e ) {
185
+            $this->last_error = $e->getMessage();
186
+        }
187
+
188
+        // Fired when we are done processing a submission.
189
+        do_action_ref_array( 'getpaid_process_submission', array( &$this ) );
190
+
191
+    }
192
+
193
+    /*
194 194
 	|--------------------------------------------------------------------------
195 195
 	| Payment Forms.
196 196
 	|--------------------------------------------------------------------------
@@ -199,39 +199,39 @@  discard block
 block discarded – undo
199 199
 	| submission has an active payment form etc.
200 200
     */
201 201
 
202
-	/**
203
-	 * Prepares the submission's payment form.
204
-	 *
205
-	 * @since 1.0.19
206
-	 */
207
-	public function process_payment_form() {
202
+    /**
203
+     * Prepares the submission's payment form.
204
+     *
205
+     * @since 1.0.19
206
+     */
207
+    public function process_payment_form() {
208 208
 
209
-		// Every submission needs an active payment form.
210
-		if ( empty( $this->data['form_id'] ) ) {
211
-			throw new Exception( __( 'Missing payment form', 'invoicing' ) );
212
-		}
209
+        // Every submission needs an active payment form.
210
+        if ( empty( $this->data['form_id'] ) ) {
211
+            throw new Exception( __( 'Missing payment form', 'invoicing' ) );
212
+        }
213 213
 
214
-		// Fetch the payment form.
215
-		$this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] );
214
+        // Fetch the payment form.
215
+        $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] );
216 216
 
217
-		if ( ! $this->payment_form->is_active() ) {
218
-			throw new Exception( __( 'Payment form not active', 'invoicing' ) );
219
-		}
217
+        if ( ! $this->payment_form->is_active() ) {
218
+            throw new Exception( __( 'Payment form not active', 'invoicing' ) );
219
+        }
220 220
 
221
-		do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) );
222
-	}
221
+        do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) );
222
+    }
223 223
 
224 224
     /**
225
-	 * Returns the payment form.
226
-	 *
227
-	 * @since 1.0.19
228
-	 * @return GetPaid_Payment_Form
229
-	 */
230
-	public function get_payment_form() {
231
-		return $this->payment_form;
232
-	}
225
+     * Returns the payment form.
226
+     *
227
+     * @since 1.0.19
228
+     * @return GetPaid_Payment_Form
229
+     */
230
+    public function get_payment_form() {
231
+        return $this->payment_form;
232
+    }
233 233
 
234
-	/*
234
+    /*
235 235
 	|--------------------------------------------------------------------------
236 236
 	| Invoices.
237 237
 	|--------------------------------------------------------------------------
@@ -240,61 +240,61 @@  discard block
 block discarded – undo
240 240
 	| might be for an existing invoice.
241 241
 	*/
242 242
 
243
-	/**
244
-	 * Prepares the submission's invoice.
245
-	 *
246
-	 * @since 1.0.19
247
-	 */
248
-	public function process_invoice() {
243
+    /**
244
+     * Prepares the submission's invoice.
245
+     *
246
+     * @since 1.0.19
247
+     */
248
+    public function process_invoice() {
249 249
 
250
-		// Abort if there is no invoice.
251
-		if ( empty( $this->data['invoice_id'] ) ) {
252
-			return;
253
-		}
250
+        // Abort if there is no invoice.
251
+        if ( empty( $this->data['invoice_id'] ) ) {
252
+            return;
253
+        }
254 254
 
255
-		// If the submission is for an existing invoice, ensure that it exists
256
-		// and that it is not paid for.
257
-		$invoice = wpinv_get_invoice( $this->data['invoice_id'] );
255
+        // If the submission is for an existing invoice, ensure that it exists
256
+        // and that it is not paid for.
257
+        $invoice = wpinv_get_invoice( $this->data['invoice_id'] );
258 258
 
259 259
         if ( empty( $invoice ) ) {
260
-			throw new Exception( __( 'Invalid invoice', 'invoicing' ) );
261
-		}
262
-
263
-		if ( $invoice->is_paid() ) {
264
-			throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) );
265
-		}
266
-
267
-		$this->payment_form->set_items( $invoice->get_items() );
268
-		$this->payment_form->invoice = $invoice;
269
-
270
-		$this->country = $invoice->get_country();
271
-		$this->state   = $invoice->get_state();
272
-		$this->invoice = $invoice;
273
-
274
-		do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) );
275
-	}
276
-
277
-	/**
278
-	 * Returns the associated invoice.
279
-	 *
280
-	 * @since 1.0.19
281
-	 * @return WPInv_Invoice
282
-	 */
283
-	public function get_invoice() {
284
-		return $this->invoice;
285
-	}
286
-
287
-	/**
288
-	 * Checks whether there is an invoice associated with this submission.
289
-	 *
290
-	 * @since 1.0.19
291
-	 * @return bool
292
-	 */
293
-	public function has_invoice() {
294
-		return ! empty( $this->invoice );
295
-	}
296
-
297
-	/*
260
+            throw new Exception( __( 'Invalid invoice', 'invoicing' ) );
261
+        }
262
+
263
+        if ( $invoice->is_paid() ) {
264
+            throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) );
265
+        }
266
+
267
+        $this->payment_form->set_items( $invoice->get_items() );
268
+        $this->payment_form->invoice = $invoice;
269
+
270
+        $this->country = $invoice->get_country();
271
+        $this->state   = $invoice->get_state();
272
+        $this->invoice = $invoice;
273
+
274
+        do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) );
275
+    }
276
+
277
+    /**
278
+     * Returns the associated invoice.
279
+     *
280
+     * @since 1.0.19
281
+     * @return WPInv_Invoice
282
+     */
283
+    public function get_invoice() {
284
+        return $this->invoice;
285
+    }
286
+
287
+    /**
288
+     * Checks whether there is an invoice associated with this submission.
289
+     *
290
+     * @since 1.0.19
291
+     * @return bool
292
+     */
293
+    public function has_invoice() {
294
+        return ! empty( $this->invoice );
295
+    }
296
+
297
+    /*
298 298
 	|--------------------------------------------------------------------------
299 299
 	| Items.
300 300
 	|--------------------------------------------------------------------------
@@ -303,105 +303,105 @@  discard block
 block discarded – undo
303 303
 	| recurring item. But can have an unlimited number of non-recurring items.
304 304
 	*/
305 305
 
306
-	/**
307
-	 * Prepares the submission's items.
308
-	 *
309
-	 * @since 1.0.19
310
-	 */
311
-	public function process_items() {
312
-
313
-		$processor = new GetPaid_Payment_Form_Submission_Items( $this );
314
-
315
-		foreach ( $processor->items as $item ) {
316
-			$this->add_item( $item );
317
-		}
318
-
319
-		do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) );
320
-	}
321
-
322
-	/**
323
-	 * Adds an item to the submission.
324
-	 *
325
-	 * @since 1.0.19
326
-	 * @param GetPaid_Form_Item $item
327
-	 */
328
-	public function add_item( $item ) {
329
-
330
-		// Make sure that it is available for purchase.
331
-		if ( ! $item->can_purchase() ) {
332
-			return;
333
-		}
334
-
335
-		// Each submission can only contain one recurring item.
336
-		if ( $item->is_recurring() ) {
337
-
338
-			if ( $this->has_recurring != 0 ) {
339
-				throw new Exception( __( 'You can only buy one recurring item at a time.', 'invoicing' ) );
340
-			}
341
-
342
-			$this->has_recurring = $item->get_id();
343
-
344
-		}
345
-
346
-		// Update the items and totals.
347
-		$this->items[ $item->get_id() ]         = $item;
348
-		$this->totals['subtotal']['initial']   += $item->get_sub_total();
349
-		$this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total();
350
-
351
-	}
352
-
353
-	/**
354
-	 * Removes a specific item.
355
-	 * 
356
-	 * You should not call this method after the discounts and taxes
357
-	 * have been calculated.
358
-	 *
359
-	 * @since 1.0.19
360
-	 */
361
-	public function remove_item( $item_id ) {
362
-
363
-		if ( isset( $this->items[ $item_id ] ) ) {
364
-			$this->totals['subtotal']['initial']   -= $this->items[ $item_id ]->get_sub_total();
365
-			$this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total();
366
-
367
-			if ( $this->items[ $item_id ]->is_recurring() ) {
368
-				$this->has_recurring = 0;
369
-			}
370
-
371
-			unset( $this->items[ $item_id ] );
372
-		}
306
+    /**
307
+     * Prepares the submission's items.
308
+     *
309
+     * @since 1.0.19
310
+     */
311
+    public function process_items() {
312
+
313
+        $processor = new GetPaid_Payment_Form_Submission_Items( $this );
314
+
315
+        foreach ( $processor->items as $item ) {
316
+            $this->add_item( $item );
317
+        }
318
+
319
+        do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) );
320
+    }
321
+
322
+    /**
323
+     * Adds an item to the submission.
324
+     *
325
+     * @since 1.0.19
326
+     * @param GetPaid_Form_Item $item
327
+     */
328
+    public function add_item( $item ) {
329
+
330
+        // Make sure that it is available for purchase.
331
+        if ( ! $item->can_purchase() ) {
332
+            return;
333
+        }
334
+
335
+        // Each submission can only contain one recurring item.
336
+        if ( $item->is_recurring() ) {
337
+
338
+            if ( $this->has_recurring != 0 ) {
339
+                throw new Exception( __( 'You can only buy one recurring item at a time.', 'invoicing' ) );
340
+            }
341
+
342
+            $this->has_recurring = $item->get_id();
343
+
344
+        }
345
+
346
+        // Update the items and totals.
347
+        $this->items[ $item->get_id() ]         = $item;
348
+        $this->totals['subtotal']['initial']   += $item->get_sub_total();
349
+        $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total();
350
+
351
+    }
352
+
353
+    /**
354
+     * Removes a specific item.
355
+     * 
356
+     * You should not call this method after the discounts and taxes
357
+     * have been calculated.
358
+     *
359
+     * @since 1.0.19
360
+     */
361
+    public function remove_item( $item_id ) {
362
+
363
+        if ( isset( $this->items[ $item_id ] ) ) {
364
+            $this->totals['subtotal']['initial']   -= $this->items[ $item_id ]->get_sub_total();
365
+            $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total();
366
+
367
+            if ( $this->items[ $item_id ]->is_recurring() ) {
368
+                $this->has_recurring = 0;
369
+            }
370
+
371
+            unset( $this->items[ $item_id ] );
372
+        }
373 373
 		
374
-	}
375
-
376
-	/**
377
-	 * Returns the subtotal.
378
-	 *
379
-	 * @since 1.0.19
380
-	 */
381
-	public function get_subtotal() {
382
-		return $this->totals['subtotal']['initial'];
383
-	}
384
-
385
-	/**
386
-	 * Returns the recurring subtotal.
387
-	 *
388
-	 * @since 1.0.19
389
-	 */
390
-	public function get_recurring_subtotal() {
391
-		return $this->totals['subtotal']['recurring'];
392
-	}
393
-
394
-	/**
395
-	 * Returns all items.
396
-	 *
397
-	 * @since 1.0.19
398
-	 * @return GetPaid_Form_Item[]
399
-	 */
400
-	public function get_items() {
401
-		return $this->items;
402
-	}
403
-
404
-	/*
374
+    }
375
+
376
+    /**
377
+     * Returns the subtotal.
378
+     *
379
+     * @since 1.0.19
380
+     */
381
+    public function get_subtotal() {
382
+        return $this->totals['subtotal']['initial'];
383
+    }
384
+
385
+    /**
386
+     * Returns the recurring subtotal.
387
+     *
388
+     * @since 1.0.19
389
+     */
390
+    public function get_recurring_subtotal() {
391
+        return $this->totals['subtotal']['recurring'];
392
+    }
393
+
394
+    /**
395
+     * Returns all items.
396
+     *
397
+     * @since 1.0.19
398
+     * @return GetPaid_Form_Item[]
399
+     */
400
+    public function get_items() {
401
+        return $this->items;
402
+    }
403
+
404
+    /*
405 405
 	|--------------------------------------------------------------------------
406 406
 	| Taxes
407 407
 	|--------------------------------------------------------------------------
@@ -410,108 +410,108 @@  discard block
 block discarded – undo
410 410
 	| or only one-time.
411 411
     */
412 412
 
413
-	/**
414
-	 * Prepares the submission's taxes.
415
-	 *
416
-	 * @since 1.0.19
417
-	 */
418
-	public function process_taxes() {
419
-
420
-		// Abort if we're not using taxes.
421
-		if ( ! $this->use_taxes() ) {
422
-			return;
423
-		}
424
-
425
-		// If a custom country && state has been passed in, use it to calculate taxes.
426
-		if ( ! empty( $this->data['wpinv_country'] ) ) {
427
-			$this->country = $this->data['wpinv_country'];
428
-		}
429
-
430
-		if ( ! empty( $this->data['wpinv_state'] ) ) {
431
-			$this->country = $this->data['wpinv_state'];
432
-		}
433
-
434
-		$processor = new GetPaid_Payment_Form_Submission_Taxes( $this );
435
-
436
-		foreach ( $processor->taxes as $tax ) {
437
-			$this->add_tax( $tax );
438
-		}
439
-
440
-		do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) );
441
-	}
442
-
443
-	/**
444
-	 * Adds a tax to the submission.
445
-	 *
446
-	 * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required.
447
-	 * @since 1.0.19
448
-	 */
449
-	public function add_tax( $tax ) {
450
-		$this->taxes[ $tax['name'] ]         = $tax;
451
-		$this->totals['taxes']['initial']   += wpinv_sanitize_amount( $tax['initial_tax'] );
452
-		$this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] );
453
-	}
454
-
455
-	/**
456
-	 * Removes a specific tax.
457
-	 *
458
-	 * @since 1.0.19
459
-	 */
460
-	public function remove_tax( $tax_name ) {
461
-
462
-		if ( isset( $this->taxes[ $tax_name ] ) ) {
463
-			$this->totals['taxes']['initial']   -= $this->taxes[ $tax_name ]['initial_tax'];
464
-			$this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax'];
465
-			unset( $this->taxes[ $tax_name ] );
466
-		}
467
-
468
-	}
469
-
470
-	/**
471
-	 * Whether or not we'll use taxes for the submission.
472
-	 *
473
-	 * @since 1.0.19
474
-	 */
475
-	public function use_taxes() {
476
-
477
-		$use_taxes = wpinv_use_taxes();
478
-
479
-		if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) {
480
-			$use_taxes = false;
481
-		}
482
-
483
-		return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this );
484
-
485
-	}
486
-
487
-	/**
488
-	 * Returns the tax.
489
-	 *
490
-	 * @since 1.0.19
491
-	 */
492
-	public function get_tax() {
493
-		return $this->totals['taxes']['initial'];
494
-	}
495
-
496
-	/**
497
-	 * Returns the recurring tax.
498
-	 *
499
-	 * @since 1.0.19
500
-	 */
501
-	public function get_recurring_tax() {
502
-		return $this->totals['taxes']['recurring'];
503
-	}
504
-
505
-	/**
506
-	 * Returns all taxes.
507
-	 *
508
-	 * @since 1.0.19
509
-	 */
510
-	public function get_taxes() {
511
-		return $this->taxes;
512
-	}
513
-
514
-	/*
413
+    /**
414
+     * Prepares the submission's taxes.
415
+     *
416
+     * @since 1.0.19
417
+     */
418
+    public function process_taxes() {
419
+
420
+        // Abort if we're not using taxes.
421
+        if ( ! $this->use_taxes() ) {
422
+            return;
423
+        }
424
+
425
+        // If a custom country && state has been passed in, use it to calculate taxes.
426
+        if ( ! empty( $this->data['wpinv_country'] ) ) {
427
+            $this->country = $this->data['wpinv_country'];
428
+        }
429
+
430
+        if ( ! empty( $this->data['wpinv_state'] ) ) {
431
+            $this->country = $this->data['wpinv_state'];
432
+        }
433
+
434
+        $processor = new GetPaid_Payment_Form_Submission_Taxes( $this );
435
+
436
+        foreach ( $processor->taxes as $tax ) {
437
+            $this->add_tax( $tax );
438
+        }
439
+
440
+        do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) );
441
+    }
442
+
443
+    /**
444
+     * Adds a tax to the submission.
445
+     *
446
+     * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required.
447
+     * @since 1.0.19
448
+     */
449
+    public function add_tax( $tax ) {
450
+        $this->taxes[ $tax['name'] ]         = $tax;
451
+        $this->totals['taxes']['initial']   += wpinv_sanitize_amount( $tax['initial_tax'] );
452
+        $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] );
453
+    }
454
+
455
+    /**
456
+     * Removes a specific tax.
457
+     *
458
+     * @since 1.0.19
459
+     */
460
+    public function remove_tax( $tax_name ) {
461
+
462
+        if ( isset( $this->taxes[ $tax_name ] ) ) {
463
+            $this->totals['taxes']['initial']   -= $this->taxes[ $tax_name ]['initial_tax'];
464
+            $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax'];
465
+            unset( $this->taxes[ $tax_name ] );
466
+        }
467
+
468
+    }
469
+
470
+    /**
471
+     * Whether or not we'll use taxes for the submission.
472
+     *
473
+     * @since 1.0.19
474
+     */
475
+    public function use_taxes() {
476
+
477
+        $use_taxes = wpinv_use_taxes();
478
+
479
+        if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) {
480
+            $use_taxes = false;
481
+        }
482
+
483
+        return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this );
484
+
485
+    }
486
+
487
+    /**
488
+     * Returns the tax.
489
+     *
490
+     * @since 1.0.19
491
+     */
492
+    public function get_tax() {
493
+        return $this->totals['taxes']['initial'];
494
+    }
495
+
496
+    /**
497
+     * Returns the recurring tax.
498
+     *
499
+     * @since 1.0.19
500
+     */
501
+    public function get_recurring_tax() {
502
+        return $this->totals['taxes']['recurring'];
503
+    }
504
+
505
+    /**
506
+     * Returns all taxes.
507
+     *
508
+     * @since 1.0.19
509
+     */
510
+    public function get_taxes() {
511
+        return $this->taxes;
512
+    }
513
+
514
+    /*
515 515
 	|--------------------------------------------------------------------------
516 516
 	| Discounts
517 517
 	|--------------------------------------------------------------------------
@@ -520,99 +520,99 @@  discard block
 block discarded – undo
520 520
 	| or only one-time. They also do not have to come from a discount code.
521 521
     */
522 522
 
523
-	/**
524
-	 * Prepares the submission's discount.
525
-	 *
526
-	 * @since 1.0.19
527
-	 */
528
-	public function process_discount() {
529
-
530
-		$initial_total    = $this->get_subtotal() + $this->get_fee() + $this->get_tax();
531
-		$recurring_total  = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax();
532
-		$processor        = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total );
533
-
534
-		foreach ( $processor->discounts as $discount ) {
535
-			$this->add_discount( $discount );
536
-		}
537
-
538
-		do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) );
539
-	}
540
-
541
-	/**
542
-	 * Adds a discount to the submission.
543
-	 *
544
-	 * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code.
545
-	 * @since 1.0.19
546
-	 */
547
-	public function add_discount( $discount ) {
548
-		$this->discounts[ $discount['name'] ]   = $discount;
549
-		$this->totals['discount']['initial']   += wpinv_sanitize_amount( $discount['initial_discount'] );
550
-		$this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] );
551
-	}
552
-
553
-	/**
554
-	 * Removes a discount from the submission.
555
-	 *
556
-	 * @since 1.0.19
557
-	 */
558
-	public function remove_discount( $name ) {
559
-
560
-		if ( isset( $this->discounts[ $name ] ) ) {
561
-			$this->totals['discount']['initial']   -= $this->discounts[ $name ]['initial_discount'];
562
-			$this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount'];
563
-			unset( $this->discounts[ $name ] );
564
-		}
565
-
566
-	}
567
-
568
-	/**
569
-	 * Checks whether there is a discount code associated with this submission.
570
-	 *
571
-	 * @since 1.0.19
572
-	 * @return bool
573
-	 */
574
-	public function has_discount_code() {
575
-		return ! empty( $this->discounts['discount_code'] );
576
-	}
577
-
578
-	/**
579
-	 * Returns the discount code.
580
-	 *
581
-	 * @since 1.0.19
582
-	 * @return string
583
-	 */
584
-	public function get_discount_code() {
585
-		return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : '';
586
-	}
587
-
588
-	/**
589
-	 * Returns the discount.
590
-	 *
591
-	 * @since 1.0.19
592
-	 */
593
-	public function get_discount() {
594
-		return $this->totals['discount']['initial'];
595
-	}
596
-
597
-	/**
598
-	 * Returns the recurring discount.
599
-	 *
600
-	 * @since 1.0.19
601
-	 */
602
-	public function get_recurring_discount() {
603
-		return $this->totals['discount']['recurring'];
604
-	}
605
-
606
-	/**
607
-	 * Returns all discounts.
608
-	 *
609
-	 * @since 1.0.19
610
-	 */
611
-	public function get_discounts() {
612
-		return $this->discounts;
613
-	}
614
-
615
-	/*
523
+    /**
524
+     * Prepares the submission's discount.
525
+     *
526
+     * @since 1.0.19
527
+     */
528
+    public function process_discount() {
529
+
530
+        $initial_total    = $this->get_subtotal() + $this->get_fee() + $this->get_tax();
531
+        $recurring_total  = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax();
532
+        $processor        = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total );
533
+
534
+        foreach ( $processor->discounts as $discount ) {
535
+            $this->add_discount( $discount );
536
+        }
537
+
538
+        do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) );
539
+    }
540
+
541
+    /**
542
+     * Adds a discount to the submission.
543
+     *
544
+     * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code.
545
+     * @since 1.0.19
546
+     */
547
+    public function add_discount( $discount ) {
548
+        $this->discounts[ $discount['name'] ]   = $discount;
549
+        $this->totals['discount']['initial']   += wpinv_sanitize_amount( $discount['initial_discount'] );
550
+        $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] );
551
+    }
552
+
553
+    /**
554
+     * Removes a discount from the submission.
555
+     *
556
+     * @since 1.0.19
557
+     */
558
+    public function remove_discount( $name ) {
559
+
560
+        if ( isset( $this->discounts[ $name ] ) ) {
561
+            $this->totals['discount']['initial']   -= $this->discounts[ $name ]['initial_discount'];
562
+            $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount'];
563
+            unset( $this->discounts[ $name ] );
564
+        }
565
+
566
+    }
567
+
568
+    /**
569
+     * Checks whether there is a discount code associated with this submission.
570
+     *
571
+     * @since 1.0.19
572
+     * @return bool
573
+     */
574
+    public function has_discount_code() {
575
+        return ! empty( $this->discounts['discount_code'] );
576
+    }
577
+
578
+    /**
579
+     * Returns the discount code.
580
+     *
581
+     * @since 1.0.19
582
+     * @return string
583
+     */
584
+    public function get_discount_code() {
585
+        return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : '';
586
+    }
587
+
588
+    /**
589
+     * Returns the discount.
590
+     *
591
+     * @since 1.0.19
592
+     */
593
+    public function get_discount() {
594
+        return $this->totals['discount']['initial'];
595
+    }
596
+
597
+    /**
598
+     * Returns the recurring discount.
599
+     *
600
+     * @since 1.0.19
601
+     */
602
+    public function get_recurring_discount() {
603
+        return $this->totals['discount']['recurring'];
604
+    }
605
+
606
+    /**
607
+     * Returns all discounts.
608
+     *
609
+     * @since 1.0.19
610
+     */
611
+    public function get_discounts() {
612
+        return $this->discounts;
613
+    }
614
+
615
+    /*
616 616
 	|--------------------------------------------------------------------------
617 617
 	| Fees
618 618
 	|--------------------------------------------------------------------------
@@ -622,89 +622,89 @@  discard block
 block discarded – undo
622 622
 	| fees.
623 623
     */
624 624
 
625
-	/**
626
-	 * Prepares the submission's fees.
627
-	 *
628
-	 * @since 1.0.19
629
-	 */
630
-	public function process_fees() {
631
-
632
-		$fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this );
633
-
634
-		foreach ( $fees_processor->fees as $fee ) {
635
-			$this->add_fee( $fee );
636
-		}
637
-
638
-		do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) );
639
-	}
640
-
641
-	/**
642
-	 * Adds a fee to the submission.
643
-	 *
644
-	 * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required.
645
-	 * @since 1.0.19
646
-	 */
647
-	public function add_fee( $fee ) {
648
-
649
-		$this->fees[ $fee['name'] ]         = $fee;
650
-		$this->totals['fees']['initial']   += wpinv_sanitize_amount( $fee['initial_fee'] );
651
-		$this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] );
652
-
653
-	}
654
-
655
-	/**
656
-	 * Removes a fee from the submission.
657
-	 *
658
-	 * @since 1.0.19
659
-	 */
660
-	public function remove_fee( $name ) {
661
-
662
-		if ( isset( $this->fees[ $name ] ) ) {
663
-			$this->totals['fees']['initial']   -= $this->fees[ $name ]['initial_fee'];
664
-			$this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee'];
665
-			unset( $this->fees[ $name ] );
666
-		}
667
-
668
-	}
669
-
670
-	/**
671
-	 * Returns the fees.
672
-	 *
673
-	 * @since 1.0.19
674
-	 */
675
-	public function get_fee() {
676
-		return $this->totals['fees']['initial'];
677
-	}
678
-
679
-	/**
680
-	 * Returns the recurring fees.
681
-	 *
682
-	 * @since 1.0.19
683
-	 */
684
-	public function get_recurring_fee() {
685
-		return $this->totals['fees']['recurring'];
686
-	}
687
-
688
-	/**
689
-	 * Returns all fees.
690
-	 *
691
-	 * @since 1.0.19
692
-	 */
693
-	public function get_fees() {
694
-		return $this->fees;
695
-	}
696
-
697
-	/**
698
-	 * Checks if there are any fees for the form.
699
-	 *
700
-	 * @return bool
701
-	 * @since 1.0.19
702
-	 */
703
-	public function has_fees() {
704
-		return count( $this->fees ) !== 0;
705
-	}
706
-
707
-	/*
625
+    /**
626
+     * Prepares the submission's fees.
627
+     *
628
+     * @since 1.0.19
629
+     */
630
+    public function process_fees() {
631
+
632
+        $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this );
633
+
634
+        foreach ( $fees_processor->fees as $fee ) {
635
+            $this->add_fee( $fee );
636
+        }
637
+
638
+        do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) );
639
+    }
640
+
641
+    /**
642
+     * Adds a fee to the submission.
643
+     *
644
+     * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required.
645
+     * @since 1.0.19
646
+     */
647
+    public function add_fee( $fee ) {
648
+
649
+        $this->fees[ $fee['name'] ]         = $fee;
650
+        $this->totals['fees']['initial']   += wpinv_sanitize_amount( $fee['initial_fee'] );
651
+        $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] );
652
+
653
+    }
654
+
655
+    /**
656
+     * Removes a fee from the submission.
657
+     *
658
+     * @since 1.0.19
659
+     */
660
+    public function remove_fee( $name ) {
661
+
662
+        if ( isset( $this->fees[ $name ] ) ) {
663
+            $this->totals['fees']['initial']   -= $this->fees[ $name ]['initial_fee'];
664
+            $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee'];
665
+            unset( $this->fees[ $name ] );
666
+        }
667
+
668
+    }
669
+
670
+    /**
671
+     * Returns the fees.
672
+     *
673
+     * @since 1.0.19
674
+     */
675
+    public function get_fee() {
676
+        return $this->totals['fees']['initial'];
677
+    }
678
+
679
+    /**
680
+     * Returns the recurring fees.
681
+     *
682
+     * @since 1.0.19
683
+     */
684
+    public function get_recurring_fee() {
685
+        return $this->totals['fees']['recurring'];
686
+    }
687
+
688
+    /**
689
+     * Returns all fees.
690
+     *
691
+     * @since 1.0.19
692
+     */
693
+    public function get_fees() {
694
+        return $this->fees;
695
+    }
696
+
697
+    /**
698
+     * Checks if there are any fees for the form.
699
+     *
700
+     * @return bool
701
+     * @since 1.0.19
702
+     */
703
+    public function has_fees() {
704
+        return count( $this->fees ) !== 0;
705
+    }
706
+
707
+    /*
708 708
 	|--------------------------------------------------------------------------
709 709
 	| MISC
710 710
 	|--------------------------------------------------------------------------
@@ -712,109 +712,109 @@  discard block
 block discarded – undo
712 712
 	| Extra submission functions.
713 713
     */
714 714
 
715
-	/**
716
-	 * Returns the total amount to collect for this submission.
717
-	 *
718
-	 * @since 1.0.19
719
-	 */
720
-	public function get_total() {
721
-		$total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() - $this->get_discount();
722
-		return max( $total, 0 );
723
-	}
724
-
725
-	/**
726
-	 * Returns the recurring total amount to collect for this submission.
727
-	 *
728
-	 * @since 1.0.19
729
-	 */
730
-	public function get_recurring_total() {
731
-		$total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() - $this->get_recurring_discount();
732
-		return max( $total, 0 );
733
-	}
734
-
735
-	/**
736
-	 * Whether payment details should be collected for this submission.
737
-	 *
738
-	 * @since 1.0.19
739
-	 */
740
-	public function should_collect_payment_details() {
741
-		$initial   = $this->get_total();
742
-		$recurring = $this->get_recurring_total();
743
-
744
-		if ( $this->has_recurring == 0 ) {
745
-			$recurring = 0;
746
-		}
747
-
748
-		$collect = $initial > 0 || $recurring > 0;
749
-		return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this  );
750
-	}
751
-
752
-	/**
753
-	 * Returns the billing email of the user.
754
-	 *
755
-	 * @since 1.0.19
756
-	 */
757
-	public function get_billing_email() {
758
-		return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this  );
759
-	}
760
-
761
-	/**
762
-	 * Checks if the submitter has a billing email.
763
-	 *
764
-	 * @since 1.0.19
765
-	 */
766
-	public function has_billing_email() {
767
-		$billing_email = $this->get_billing_email();
768
-		return ! empty( $billing_email ) && is_email( $billing_email );
769
-	}
770
-
771
-	/**
772
-	 * Returns the appropriate currency for the submission.
773
-	 *
774
-	 * @since 1.0.19
775
-	 * @return string
776
-	 */
777
-	public function get_currency() {
778
-		return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency();
779
-    }
780
-
781
-    /**
782
-	 * Returns the raw submission data.
783
-	 *
784
-	 * @since 1.0.19
785
-	 * @return array
786
-	 */
787
-	public function get_data() {
788
-		return $this->data;
789
-	}
790
-
791
-	/**
792
-	 * Returns a field from the submission data
793
-	 *
794
-	 * @param string $field
795
-	 * @since 1.0.19
796
-	 * @return mixed
797
-	 */
798
-	public function get_field( $field ) {
799
-		return isset( $this->data[ $field ] ) ? $this->data[ $field ] : '';
800
-	}
801
-
802
-	/**
803
-	 * Checks if a required field is set.
804
-	 *
805
-	 * @since 1.0.19
806
-	 */
807
-	public function is_required_field_set( $field ) {
808
-		return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] );
809
-	}
810
-
811
-	/**
812
-	 * Formats an amount
813
-	 *
814
-	 * @since 1.0.19
815
-	 */
816
-	public function format_amount( $amount ) {
817
-		return wpinv_price( $amount, $this->get_currency() );
818
-	}
715
+    /**
716
+     * Returns the total amount to collect for this submission.
717
+     *
718
+     * @since 1.0.19
719
+     */
720
+    public function get_total() {
721
+        $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() - $this->get_discount();
722
+        return max( $total, 0 );
723
+    }
724
+
725
+    /**
726
+     * Returns the recurring total amount to collect for this submission.
727
+     *
728
+     * @since 1.0.19
729
+     */
730
+    public function get_recurring_total() {
731
+        $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() - $this->get_recurring_discount();
732
+        return max( $total, 0 );
733
+    }
734
+
735
+    /**
736
+     * Whether payment details should be collected for this submission.
737
+     *
738
+     * @since 1.0.19
739
+     */
740
+    public function should_collect_payment_details() {
741
+        $initial   = $this->get_total();
742
+        $recurring = $this->get_recurring_total();
743
+
744
+        if ( $this->has_recurring == 0 ) {
745
+            $recurring = 0;
746
+        }
747
+
748
+        $collect = $initial > 0 || $recurring > 0;
749
+        return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this  );
750
+    }
751
+
752
+    /**
753
+     * Returns the billing email of the user.
754
+     *
755
+     * @since 1.0.19
756
+     */
757
+    public function get_billing_email() {
758
+        return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this  );
759
+    }
760
+
761
+    /**
762
+     * Checks if the submitter has a billing email.
763
+     *
764
+     * @since 1.0.19
765
+     */
766
+    public function has_billing_email() {
767
+        $billing_email = $this->get_billing_email();
768
+        return ! empty( $billing_email ) && is_email( $billing_email );
769
+    }
770
+
771
+    /**
772
+     * Returns the appropriate currency for the submission.
773
+     *
774
+     * @since 1.0.19
775
+     * @return string
776
+     */
777
+    public function get_currency() {
778
+        return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency();
779
+    }
780
+
781
+    /**
782
+     * Returns the raw submission data.
783
+     *
784
+     * @since 1.0.19
785
+     * @return array
786
+     */
787
+    public function get_data() {
788
+        return $this->data;
789
+    }
790
+
791
+    /**
792
+     * Returns a field from the submission data
793
+     *
794
+     * @param string $field
795
+     * @since 1.0.19
796
+     * @return mixed
797
+     */
798
+    public function get_field( $field ) {
799
+        return isset( $this->data[ $field ] ) ? $this->data[ $field ] : '';
800
+    }
801
+
802
+    /**
803
+     * Checks if a required field is set.
804
+     *
805
+     * @since 1.0.19
806
+     */
807
+    public function is_required_field_set( $field ) {
808
+        return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] );
809
+    }
810
+
811
+    /**
812
+     * Formats an amount
813
+     *
814
+     * @since 1.0.19
815
+     */
816
+    public function format_amount( $amount ) {
817
+        return wpinv_price( $amount, $this->get_currency() );
818
+    }
819 819
 
820 820
 }
Please login to merge, or discard this patch.
includes/class-getpaid-daily-maintenance.php 1 patch
Indentation   +88 added lines, -88 removed lines patch added patch discarded remove patch
@@ -12,93 +12,93 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Daily_Maintenance {
14 14
 
15
-	/**
16
-	 * Class constructor.
17
-	 */
18
-	public function __construct(){
19
-
20
-		// Clear deprecated events.
21
-		add_action( 'wp', array( $this, 'maybe_clear_deprecated_events' ) );
22
-
23
-		// (Maybe) schedule a cron that runs daily.
24
-		add_action( 'wp', array( $this, 'maybe_create_scheduled_event' ) );
25
-
26
-		// Fired everyday at 7 a.m (this might vary for sites with few visitors)
27
-		add_action( 'getpaid_daily_maintenance', array( $this, 'log_cron_run' ) );
28
-		add_action( 'getpaid_daily_maintenance', array( $this, 'backwards_compat' ) );
29
-		add_action( 'getpaid_daily_maintenance', array( $this, 'maybe_expire_subscriptions' ) );
30
-
31
-	}
32
-
33
-	/**
34
-	 * Schedules a cron to run every day at 7 a.m
35
-	 *
36
-	 */
37
-	public function maybe_create_scheduled_event() {
38
-
39
-		if ( ! wp_next_scheduled( 'getpaid_daily_maintenance' ) ) {
40
-			$timestamp = strtotime( 'tomorrow 07:00:00', current_time( 'timestamp' ) );
41
-			wp_schedule_event( $timestamp, 'daily', 'getpaid_daily_maintenance' );
42
-		}
43
-
44
-	}
45
-
46
-	/**
47
-	 * Clears deprecated events.
48
-	 *
49
-	 */
50
-	public function maybe_clear_deprecated_events() {
51
-
52
-		if ( ! get_option( 'wpinv_cleared_old_events' ) ) {
53
-			wp_clear_scheduled_hook( 'wpinv_register_schedule_event_twicedaily' );
54
-			wp_clear_scheduled_hook( 'wpinv_register_schedule_event_daily' );
55
-			update_option( 'wpinv_cleared_old_events', 1 );
56
-		}
57
-
58
-	}
59
-
60
-	/**
61
-	 * Fires the old hook for backwards compatibility.
62
-	 *
63
-	 */
64
-	public function backwards_compat() {
65
-		do_action( 'wpinv_register_schedule_event_daily' );
66
-	}
67
-
68
-	/**
69
-	 * Expires expired subscriptions.
70
-	 *
71
-	 */
72
-	public function maybe_expire_subscriptions() {
73
-
74
-		// Fetch expired subscriptions (skips those that expire today).
75
-		$args  = array(
76
-			'number'             => -1,
77
-			'count_total'        => false,
78
-			'status'             => 'trialling active failing cancelled',
79
-			'date_expires_query' => array(
80
-				'before'    => 'today',
81
-				'inclusive' => false,
82
-			),
83
-		);
84
-
85
-		$subscriptions = new GetPaid_Subscriptions_Query( $args );
86
-
87
-		foreach ( $subscriptions->get_results() as $subscription ) {
88
-			if ( apply_filters( 'getpaid_daily_maintenance_should_expire_subscription', true, $subscription ) ) {
89
-				$subscription->set_status( 'expired' );
90
-				$subscription->save();
91
-			}
92
-		}
93
-
94
-	}
95
-
96
-	/**
97
-	 * Logs cron runs.
98
-	 *
99
-	 */
100
-	public function log_cron_run() {
101
-		wpinv_error_log( 'GetPaid Daily Cron' );
102
-	}
15
+    /**
16
+     * Class constructor.
17
+     */
18
+    public function __construct(){
19
+
20
+        // Clear deprecated events.
21
+        add_action( 'wp', array( $this, 'maybe_clear_deprecated_events' ) );
22
+
23
+        // (Maybe) schedule a cron that runs daily.
24
+        add_action( 'wp', array( $this, 'maybe_create_scheduled_event' ) );
25
+
26
+        // Fired everyday at 7 a.m (this might vary for sites with few visitors)
27
+        add_action( 'getpaid_daily_maintenance', array( $this, 'log_cron_run' ) );
28
+        add_action( 'getpaid_daily_maintenance', array( $this, 'backwards_compat' ) );
29
+        add_action( 'getpaid_daily_maintenance', array( $this, 'maybe_expire_subscriptions' ) );
30
+
31
+    }
32
+
33
+    /**
34
+     * Schedules a cron to run every day at 7 a.m
35
+     *
36
+     */
37
+    public function maybe_create_scheduled_event() {
38
+
39
+        if ( ! wp_next_scheduled( 'getpaid_daily_maintenance' ) ) {
40
+            $timestamp = strtotime( 'tomorrow 07:00:00', current_time( 'timestamp' ) );
41
+            wp_schedule_event( $timestamp, 'daily', 'getpaid_daily_maintenance' );
42
+        }
43
+
44
+    }
45
+
46
+    /**
47
+     * Clears deprecated events.
48
+     *
49
+     */
50
+    public function maybe_clear_deprecated_events() {
51
+
52
+        if ( ! get_option( 'wpinv_cleared_old_events' ) ) {
53
+            wp_clear_scheduled_hook( 'wpinv_register_schedule_event_twicedaily' );
54
+            wp_clear_scheduled_hook( 'wpinv_register_schedule_event_daily' );
55
+            update_option( 'wpinv_cleared_old_events', 1 );
56
+        }
57
+
58
+    }
59
+
60
+    /**
61
+     * Fires the old hook for backwards compatibility.
62
+     *
63
+     */
64
+    public function backwards_compat() {
65
+        do_action( 'wpinv_register_schedule_event_daily' );
66
+    }
67
+
68
+    /**
69
+     * Expires expired subscriptions.
70
+     *
71
+     */
72
+    public function maybe_expire_subscriptions() {
73
+
74
+        // Fetch expired subscriptions (skips those that expire today).
75
+        $args  = array(
76
+            'number'             => -1,
77
+            'count_total'        => false,
78
+            'status'             => 'trialling active failing cancelled',
79
+            'date_expires_query' => array(
80
+                'before'    => 'today',
81
+                'inclusive' => false,
82
+            ),
83
+        );
84
+
85
+        $subscriptions = new GetPaid_Subscriptions_Query( $args );
86
+
87
+        foreach ( $subscriptions->get_results() as $subscription ) {
88
+            if ( apply_filters( 'getpaid_daily_maintenance_should_expire_subscription', true, $subscription ) ) {
89
+                $subscription->set_status( 'expired' );
90
+                $subscription->save();
91
+            }
92
+        }
93
+
94
+    }
95
+
96
+    /**
97
+     * Logs cron runs.
98
+     *
99
+     */
100
+    public function log_cron_run() {
101
+        wpinv_error_log( 'GetPaid Daily Cron' );
102
+    }
103 103
 
104 104
 }
Please login to merge, or discard this patch.
includes/class-wpinv-discount.php 1 patch
Indentation   +1261 added lines, -1261 removed lines patch added patch discarded remove patch
@@ -15,30 +15,30 @@  discard block
 block discarded – undo
15 15
  */
16 16
 class WPInv_Discount extends GetPaid_Data  {
17 17
 
18
-	/**
19
-	 * Which data store to load.
20
-	 *
21
-	 * @var string
22
-	 */
18
+    /**
19
+     * Which data store to load.
20
+     *
21
+     * @var string
22
+     */
23 23
     protected $data_store_name = 'discount';
24 24
 
25 25
     /**
26
-	 * This is the name of this object type.
27
-	 *
28
-	 * @var string
29
-	 */
30
-	protected $object_type = 'discount';
31
-
32
-	/**
33
-	 * Discount Data array. This is the core item data exposed in APIs.
34
-	 *
35
-	 * @since 1.0.19
36
-	 * @var array
37
-	 */
38
-	protected $data = array(
39
-		'status'               => 'draft',
40
-		'version'              => '',
41
-		'date_created'         => null,
26
+     * This is the name of this object type.
27
+     *
28
+     * @var string
29
+     */
30
+    protected $object_type = 'discount';
31
+
32
+    /**
33
+     * Discount Data array. This is the core item data exposed in APIs.
34
+     *
35
+     * @since 1.0.19
36
+     * @var array
37
+     */
38
+    protected $data = array(
39
+        'status'               => 'draft',
40
+        'version'              => '',
41
+        'date_created'         => null,
42 42
         'date_modified'        => null,
43 43
         'name'                 => 'no-name',
44 44
         'description'          => null,
@@ -58,144 +58,144 @@  discard block
 block discarded – undo
58 58
         'amount'               => null,
59 59
     );
60 60
 
61
-	/**
62
-	 * Stores meta in cache for future reads.
63
-	 *
64
-	 * A group must be set to to enable caching.
65
-	 *
66
-	 * @var string
67
-	 */
68
-	protected $cache_group = 'getpaid_discounts';
61
+    /**
62
+     * Stores meta in cache for future reads.
63
+     *
64
+     * A group must be set to to enable caching.
65
+     *
66
+     * @var string
67
+     */
68
+    protected $cache_group = 'getpaid_discounts';
69 69
 
70 70
     /**
71 71
      * Stores a reference to the original WP_Post object
72 72
      *
73 73
      * @var WP_Post
74 74
      */
75
-	protected $post = null;
76
-
77
-	/**
78
-	 * Get the discount if ID is passed, otherwise the discount is new and empty.
79
-	 *
80
-	 * @param int|array|string|WPInv_Discount|WP_Post $discount discount data, object, ID or code.
81
-	 */
82
-	public function __construct( $discount = 0 ) {
83
-		parent::__construct( $discount );
84
-
85
-		if ( is_numeric( $discount ) && 'wpi_discount' === get_post_type( $discount ) ) {
86
-			$this->set_id( $discount );
87
-		} elseif ( $discount instanceof self ) {
88
-			$this->set_id( $discount->get_id() );
89
-		} elseif ( ! empty( $discount->ID ) ) {
90
-			$this->set_id( $discount->ID );
91
-		} elseif ( is_array( $discount ) ) {
92
-			$this->set_props( $discount );
93
-
94
-			if ( isset( $discount['ID'] ) ) {
95
-				$this->set_id( $discount['ID'] );
96
-			}
97
-
98
-		} elseif ( is_scalar( $discount ) && $discount = self::get_discount_id_by_code( $discount ) ) {
99
-			$this->set_id( $discount );
100
-		} else {
101
-			$this->set_object_read( true );
102
-		}
75
+    protected $post = null;
76
+
77
+    /**
78
+     * Get the discount if ID is passed, otherwise the discount is new and empty.
79
+     *
80
+     * @param int|array|string|WPInv_Discount|WP_Post $discount discount data, object, ID or code.
81
+     */
82
+    public function __construct( $discount = 0 ) {
83
+        parent::__construct( $discount );
84
+
85
+        if ( is_numeric( $discount ) && 'wpi_discount' === get_post_type( $discount ) ) {
86
+            $this->set_id( $discount );
87
+        } elseif ( $discount instanceof self ) {
88
+            $this->set_id( $discount->get_id() );
89
+        } elseif ( ! empty( $discount->ID ) ) {
90
+            $this->set_id( $discount->ID );
91
+        } elseif ( is_array( $discount ) ) {
92
+            $this->set_props( $discount );
93
+
94
+            if ( isset( $discount['ID'] ) ) {
95
+                $this->set_id( $discount['ID'] );
96
+            }
97
+
98
+        } elseif ( is_scalar( $discount ) && $discount = self::get_discount_id_by_code( $discount ) ) {
99
+            $this->set_id( $discount );
100
+        } else {
101
+            $this->set_object_read( true );
102
+        }
103 103
 
104 104
         // Load the datastore.
105
-		$this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
105
+        $this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
106 106
 
107
-		if ( $this->get_id() > 0 ) {
107
+        if ( $this->get_id() > 0 ) {
108 108
             $this->post = get_post( $this->get_id() );
109 109
             $this->ID   = $this->get_id();
110
-			$this->data_store->read( $this );
110
+            $this->data_store->read( $this );
111
+        }
112
+
113
+    }
114
+
115
+    /**
116
+     * Fetch a discount from the db/cache
117
+     *
118
+     *
119
+     * @static
120
+     * @param string $field The field to query against: 'ID', 'discount_code'
121
+     * @param string|int $value The field value
122
+     * @deprecated
123
+     * @since 1.0.15
124
+     * @return array|bool array of discount details on success. False otherwise.
125
+     */
126
+    public static function get_data_by( $field, $value ) {
127
+
128
+        if ( 'id' == strtolower( $field ) ) {
129
+            // Make sure the value is numeric to avoid casting objects, for example,
130
+            // to int 1.
131
+            if ( ! is_numeric( $value ) )
132
+                return false;
133
+            $value = intval( $value );
134
+            if ( $value < 1 )
135
+                return false;
136
+        }
137
+
138
+        if ( ! $value || ! is_string( $field ) ) {
139
+            return false;
140
+        }
141
+
142
+        $field = trim( $field );
143
+
144
+        // prepare query args
145
+        switch ( strtolower( $field ) ) {
146
+            case 'id':
147
+                $discount_id = $value;
148
+                $args		 = array( 'include' => array( $value ) );
149
+                break;
150
+            case 'discount_code':
151
+            case 'code':
152
+                $value       = trim( $value );
153
+                $discount_id = wp_cache_get( $value, 'WPInv_Discount_Codes' );
154
+                $args		 = array( 'meta_key' => '_wpi_discount_code', 'meta_value' => $value );
155
+                break;
156
+            case 'name':
157
+                $discount_id = 0;
158
+                $args		 = array( 'name' => trim( $value ) );
159
+                break;
160
+            default:
161
+                $args		 = apply_filters( "wpinv_discount_get_data_by_{$field}_args", null, $value );
162
+                if ( ! is_array( $args ) ) {
163
+                    return apply_filters( "wpinv_discount_get_data_by_$field", false, $value );
164
+                }
165
+
166
+        }
167
+
168
+        // Check if there is a cached value.
169
+        if ( ! empty( $discount_id ) && $discount = wp_cache_get( (int) $discount_id, 'WPInv_Discounts' ) ) {
170
+            return $discount;
171
+        }
172
+
173
+        $args = array_merge(
174
+            $args,
175
+            array(
176
+                'post_type'      => 'wpi_discount',
177
+                'posts_per_page' => 1,
178
+                'post_status'    => array( 'publish', 'pending', 'draft', 'expired' )
179
+            )
180
+        );
181
+
182
+        $discount = get_posts( $args );
183
+
184
+        if( empty( $discount ) ) {
185
+            return false;
111 186
         }
112 187
 
113
-	}
114
-
115
-	/**
116
-	 * Fetch a discount from the db/cache
117
-	 *
118
-	 *
119
-	 * @static
120
-	 * @param string $field The field to query against: 'ID', 'discount_code'
121
-	 * @param string|int $value The field value
122
-	 * @deprecated
123
-	 * @since 1.0.15
124
-	 * @return array|bool array of discount details on success. False otherwise.
125
-	 */
126
-	public static function get_data_by( $field, $value ) {
127
-
128
-		if ( 'id' == strtolower( $field ) ) {
129
-			// Make sure the value is numeric to avoid casting objects, for example,
130
-			// to int 1.
131
-			if ( ! is_numeric( $value ) )
132
-				return false;
133
-			$value = intval( $value );
134
-			if ( $value < 1 )
135
-				return false;
136
-		}
137
-
138
-		if ( ! $value || ! is_string( $field ) ) {
139
-			return false;
140
-		}
141
-
142
-		$field = trim( $field );
143
-
144
-		// prepare query args
145
-		switch ( strtolower( $field ) ) {
146
-			case 'id':
147
-				$discount_id = $value;
148
-				$args		 = array( 'include' => array( $value ) );
149
-				break;
150
-			case 'discount_code':
151
-			case 'code':
152
-				$value       = trim( $value );
153
-				$discount_id = wp_cache_get( $value, 'WPInv_Discount_Codes' );
154
-				$args		 = array( 'meta_key' => '_wpi_discount_code', 'meta_value' => $value );
155
-				break;
156
-			case 'name':
157
-				$discount_id = 0;
158
-				$args		 = array( 'name' => trim( $value ) );
159
-				break;
160
-			default:
161
-				$args		 = apply_filters( "wpinv_discount_get_data_by_{$field}_args", null, $value );
162
-				if ( ! is_array( $args ) ) {
163
-					return apply_filters( "wpinv_discount_get_data_by_$field", false, $value );
164
-				}
165
-
166
-		}
167
-
168
-		// Check if there is a cached value.
169
-		if ( ! empty( $discount_id ) && $discount = wp_cache_get( (int) $discount_id, 'WPInv_Discounts' ) ) {
170
-			return $discount;
171
-		}
172
-
173
-		$args = array_merge(
174
-			$args,
175
-			array(
176
-				'post_type'      => 'wpi_discount',
177
-				'posts_per_page' => 1,
178
-				'post_status'    => array( 'publish', 'pending', 'draft', 'expired' )
179
-			)
180
-		);
181
-
182
-		$discount = get_posts( $args );
183
-
184
-		if( empty( $discount ) ) {
185
-			return false;
186
-		}
187
-
188
-		$discount = $discount[0];
189
-
190
-		// Prepare the return data.
191
-		$return = array(
188
+        $discount = $discount[0];
189
+
190
+        // Prepare the return data.
191
+        $return = array(
192 192
             'ID'                          => $discount->ID,
193 193
             'code'                        => get_post_meta( $discount->ID, '_wpi_discount_code', true ),
194 194
             'amount'                      => get_post_meta( $discount->ID, '_wpi_discount_amount', true ),
195 195
             'date_created'                => $discount->post_date,
196
-			'date_modified'               => $discount->post_modified,
197
-			'status'               		  => $discount->post_status,
198
-			'start'                  	  => get_post_meta( $discount->ID, '_wpi_discount_start', true ),
196
+            'date_modified'               => $discount->post_modified,
197
+            'status'               		  => $discount->post_status,
198
+            'start'                  	  => get_post_meta( $discount->ID, '_wpi_discount_start', true ),
199 199
             'expiration'                  => get_post_meta( $discount->ID, '_wpi_discount_expiration', true ),
200 200
             'type'               		  => get_post_meta( $discount->ID, '_wpi_discount_type', true ),
201 201
             'description'                 => $discount->post_excerpt,
@@ -209,77 +209,77 @@  discard block
 block discarded – undo
209 209
             'max_total'                   => get_post_meta( $discount->ID, '_wpi_discount_max_total', true ),
210 210
         );
211 211
 
212
-		$return = apply_filters( 'wpinv_discount_properties', $return );
213
-
214
-		// Update the cache with our data
215
-		wp_cache_add( $discount->ID, $return, 'WPInv_Discounts' );
216
-		wp_cache_add( $return['code'], $discount->ID, 'WPInv_Discount_Codes' );
217
-
218
-		return $return;
219
-	}
220
-
221
-	/**
222
-	 * Given a discount code, it returns a discount id.
223
-	 *
224
-	 *
225
-	 * @static
226
-	 * @param string $discount_code
227
-	 * @since 1.0.15
228
-	 * @return int
229
-	 */
230
-	public static function get_discount_id_by_code( $discount_code ) {
231
-
232
-		// Trim the code.
233
-		$discount_code = trim( $discount_code );
234
-
235
-		// Ensure a value has been passed.
236
-		if ( empty( $discount_code ) ) {
237
-			return 0;
238
-		}
239
-
240
-		// Maybe retrieve from the cache.
241
-		$discount_id   = wp_cache_get( $discount_code, 'getpaid_discount_codes' );
242
-		if ( ! empty( $discount_id ) ) {
243
-			return $discount_id;
244
-		}
245
-
246
-		// Fetch the first discount codes.
247
-		$discounts = get_posts(
248
-			array(
249
-				'meta_key'       => '_wpi_discount_code',
250
-				'meta_value'     => $discount_code,
251
-				'post_type'      => 'wpi_discount',
252
-				'posts_per_page' => 1,
253
-				'post_status'    => array( 'publish', 'pending', 'draft', 'expired' ),
254
-				'fields'         => 'ids',
255
-			)
256
-		);
257
-
258
-		if ( empty( $discounts ) ) {
259
-			return 0;
260
-		}
261
-
262
-		$discount_id = $discounts[0];
263
-
264
-		// Update the cache with our data
265
-		wp_cache_add( get_post_meta( $discount_id, '_wpi_discount_code', true ), $discount_id, 'getpaid_discount_codes' );
266
-
267
-		return $discount_id;
268
-	}
269
-
270
-	/**
271
-	 * Magic method for checking the existence of a certain custom field.
272
-	 *
273
-	 * @since 1.0.15
274
-	 * @access public
275
-	 *
276
-	 * @return bool Whether the given discount field is set.
277
-	 */
278
-	public function __isset( $key ){
279
-		return isset( $this->data[$key] ) || method_exists( $this, "get_$key");
280
-	}
281
-
282
-	/*
212
+        $return = apply_filters( 'wpinv_discount_properties', $return );
213
+
214
+        // Update the cache with our data
215
+        wp_cache_add( $discount->ID, $return, 'WPInv_Discounts' );
216
+        wp_cache_add( $return['code'], $discount->ID, 'WPInv_Discount_Codes' );
217
+
218
+        return $return;
219
+    }
220
+
221
+    /**
222
+     * Given a discount code, it returns a discount id.
223
+     *
224
+     *
225
+     * @static
226
+     * @param string $discount_code
227
+     * @since 1.0.15
228
+     * @return int
229
+     */
230
+    public static function get_discount_id_by_code( $discount_code ) {
231
+
232
+        // Trim the code.
233
+        $discount_code = trim( $discount_code );
234
+
235
+        // Ensure a value has been passed.
236
+        if ( empty( $discount_code ) ) {
237
+            return 0;
238
+        }
239
+
240
+        // Maybe retrieve from the cache.
241
+        $discount_id   = wp_cache_get( $discount_code, 'getpaid_discount_codes' );
242
+        if ( ! empty( $discount_id ) ) {
243
+            return $discount_id;
244
+        }
245
+
246
+        // Fetch the first discount codes.
247
+        $discounts = get_posts(
248
+            array(
249
+                'meta_key'       => '_wpi_discount_code',
250
+                'meta_value'     => $discount_code,
251
+                'post_type'      => 'wpi_discount',
252
+                'posts_per_page' => 1,
253
+                'post_status'    => array( 'publish', 'pending', 'draft', 'expired' ),
254
+                'fields'         => 'ids',
255
+            )
256
+        );
257
+
258
+        if ( empty( $discounts ) ) {
259
+            return 0;
260
+        }
261
+
262
+        $discount_id = $discounts[0];
263
+
264
+        // Update the cache with our data
265
+        wp_cache_add( get_post_meta( $discount_id, '_wpi_discount_code', true ), $discount_id, 'getpaid_discount_codes' );
266
+
267
+        return $discount_id;
268
+    }
269
+
270
+    /**
271
+     * Magic method for checking the existence of a certain custom field.
272
+     *
273
+     * @since 1.0.15
274
+     * @access public
275
+     *
276
+     * @return bool Whether the given discount field is set.
277
+     */
278
+    public function __isset( $key ){
279
+        return isset( $this->data[$key] ) || method_exists( $this, "get_$key");
280
+    }
281
+
282
+    /*
283 283
 	|--------------------------------------------------------------------------
284 284
 	| CRUD methods
285 285
 	|--------------------------------------------------------------------------
@@ -294,430 +294,430 @@  discard block
 block discarded – undo
294 294
 	|--------------------------------------------------------------------------
295 295
 	*/
296 296
 
297
-	/**
298
-	 * Get discount status.
299
-	 *
300
-	 * @since 1.0.19
301
-	 * @param  string $context View or edit context.
302
-	 * @return string
303
-	 */
304
-	public function get_status( $context = 'view' ) {
305
-		return $this->get_prop( 'status', $context );
297
+    /**
298
+     * Get discount status.
299
+     *
300
+     * @since 1.0.19
301
+     * @param  string $context View or edit context.
302
+     * @return string
303
+     */
304
+    public function get_status( $context = 'view' ) {
305
+        return $this->get_prop( 'status', $context );
306 306
     }
307 307
 
308 308
     /**
309
-	 * Get plugin version when the discount was created.
310
-	 *
311
-	 * @since 1.0.19
312
-	 * @param  string $context View or edit context.
313
-	 * @return string
314
-	 */
315
-	public function get_version( $context = 'view' ) {
316
-		return $this->get_prop( 'version', $context );
309
+     * Get plugin version when the discount was created.
310
+     *
311
+     * @since 1.0.19
312
+     * @param  string $context View or edit context.
313
+     * @return string
314
+     */
315
+    public function get_version( $context = 'view' ) {
316
+        return $this->get_prop( 'version', $context );
317 317
     }
318 318
 
319 319
     /**
320
-	 * Get date when the discount was created.
321
-	 *
322
-	 * @since 1.0.19
323
-	 * @param  string $context View or edit context.
324
-	 * @return string
325
-	 */
326
-	public function get_date_created( $context = 'view' ) {
327
-		return $this->get_prop( 'date_created', $context );
320
+     * Get date when the discount was created.
321
+     *
322
+     * @since 1.0.19
323
+     * @param  string $context View or edit context.
324
+     * @return string
325
+     */
326
+    public function get_date_created( $context = 'view' ) {
327
+        return $this->get_prop( 'date_created', $context );
328 328
     }
329 329
 
330 330
     /**
331
-	 * Get GMT date when the discount was created.
332
-	 *
333
-	 * @since 1.0.19
334
-	 * @param  string $context View or edit context.
335
-	 * @return string
336
-	 */
337
-	public function get_date_created_gmt( $context = 'view' ) {
331
+     * Get GMT date when the discount was created.
332
+     *
333
+     * @since 1.0.19
334
+     * @param  string $context View or edit context.
335
+     * @return string
336
+     */
337
+    public function get_date_created_gmt( $context = 'view' ) {
338 338
         $date = $this->get_date_created( $context );
339 339
 
340 340
         if ( $date ) {
341 341
             $date = get_gmt_from_date( $date );
342 342
         }
343
-		return $date;
343
+        return $date;
344 344
     }
345 345
 
346 346
     /**
347
-	 * Get date when the discount was last modified.
348
-	 *
349
-	 * @since 1.0.19
350
-	 * @param  string $context View or edit context.
351
-	 * @return string
352
-	 */
353
-	public function get_date_modified( $context = 'view' ) {
354
-		return $this->get_prop( 'date_modified', $context );
347
+     * Get date when the discount was last modified.
348
+     *
349
+     * @since 1.0.19
350
+     * @param  string $context View or edit context.
351
+     * @return string
352
+     */
353
+    public function get_date_modified( $context = 'view' ) {
354
+        return $this->get_prop( 'date_modified', $context );
355 355
     }
356 356
 
357 357
     /**
358
-	 * Get GMT date when the discount was last modified.
359
-	 *
360
-	 * @since 1.0.19
361
-	 * @param  string $context View or edit context.
362
-	 * @return string
363
-	 */
364
-	public function get_date_modified_gmt( $context = 'view' ) {
358
+     * Get GMT date when the discount was last modified.
359
+     *
360
+     * @since 1.0.19
361
+     * @param  string $context View or edit context.
362
+     * @return string
363
+     */
364
+    public function get_date_modified_gmt( $context = 'view' ) {
365 365
         $date = $this->get_date_modified( $context );
366 366
 
367 367
         if ( $date ) {
368 368
             $date = get_gmt_from_date( $date );
369 369
         }
370
-		return $date;
370
+        return $date;
371 371
     }
372 372
 
373 373
     /**
374
-	 * Get the discount name.
375
-	 *
376
-	 * @since 1.0.19
377
-	 * @param  string $context View or edit context.
378
-	 * @return string
379
-	 */
380
-	public function get_name( $context = 'view' ) {
381
-		return $this->get_prop( 'name', $context );
374
+     * Get the discount name.
375
+     *
376
+     * @since 1.0.19
377
+     * @param  string $context View or edit context.
378
+     * @return string
379
+     */
380
+    public function get_name( $context = 'view' ) {
381
+        return $this->get_prop( 'name', $context );
382 382
     }
383 383
 
384 384
     /**
385
-	 * Alias of self::get_name().
386
-	 *
387
-	 * @since 1.0.19
388
-	 * @param  string $context View or edit context.
389
-	 * @return string
390
-	 */
391
-	public function get_title( $context = 'view' ) {
392
-		return $this->get_name( $context );
385
+     * Alias of self::get_name().
386
+     *
387
+     * @since 1.0.19
388
+     * @param  string $context View or edit context.
389
+     * @return string
390
+     */
391
+    public function get_title( $context = 'view' ) {
392
+        return $this->get_name( $context );
393 393
     }
394 394
 
395 395
     /**
396
-	 * Get the discount description.
397
-	 *
398
-	 * @since 1.0.19
399
-	 * @param  string $context View or edit context.
400
-	 * @return string
401
-	 */
402
-	public function get_description( $context = 'view' ) {
403
-		return $this->get_prop( 'description', $context );
396
+     * Get the discount description.
397
+     *
398
+     * @since 1.0.19
399
+     * @param  string $context View or edit context.
400
+     * @return string
401
+     */
402
+    public function get_description( $context = 'view' ) {
403
+        return $this->get_prop( 'description', $context );
404 404
     }
405 405
 
406 406
     /**
407
-	 * Alias of self::get_description().
408
-	 *
409
-	 * @since 1.0.19
410
-	 * @param  string $context View or edit context.
411
-	 * @return string
412
-	 */
413
-	public function get_excerpt( $context = 'view' ) {
414
-		return $this->get_description( $context );
407
+     * Alias of self::get_description().
408
+     *
409
+     * @since 1.0.19
410
+     * @param  string $context View or edit context.
411
+     * @return string
412
+     */
413
+    public function get_excerpt( $context = 'view' ) {
414
+        return $this->get_description( $context );
415 415
     }
416 416
 
417 417
     /**
418
-	 * Alias of self::get_description().
419
-	 *
420
-	 * @since 1.0.19
421
-	 * @param  string $context View or edit context.
422
-	 * @return string
423
-	 */
424
-	public function get_summary( $context = 'view' ) {
425
-		return $this->get_description( $context );
418
+     * Alias of self::get_description().
419
+     *
420
+     * @since 1.0.19
421
+     * @param  string $context View or edit context.
422
+     * @return string
423
+     */
424
+    public function get_summary( $context = 'view' ) {
425
+        return $this->get_description( $context );
426 426
     }
427 427
 
428 428
     /**
429
-	 * Get the owner of the discount.
430
-	 *
431
-	 * @since 1.0.19
432
-	 * @param  string $context View or edit context.
433
-	 * @return string
434
-	 */
435
-	public function get_author( $context = 'view' ) {
436
-		return (int) $this->get_prop( 'author', $context );
437
-	}
429
+     * Get the owner of the discount.
430
+     *
431
+     * @since 1.0.19
432
+     * @param  string $context View or edit context.
433
+     * @return string
434
+     */
435
+    public function get_author( $context = 'view' ) {
436
+        return (int) $this->get_prop( 'author', $context );
437
+    }
438 438
 	
439
-	/**
440
-	 * Get the discount code.
441
-	 *
442
-	 * @since 1.0.19
443
-	 * @param  string $context View or edit context.
444
-	 * @return string
445
-	 */
446
-	public function get_code( $context = 'view' ) {
447
-		return $this->get_prop( 'code', $context );
448
-	}
439
+    /**
440
+     * Get the discount code.
441
+     *
442
+     * @since 1.0.19
443
+     * @param  string $context View or edit context.
444
+     * @return string
445
+     */
446
+    public function get_code( $context = 'view' ) {
447
+        return $this->get_prop( 'code', $context );
448
+    }
449 449
 	
450
-	/**
451
-	 * Alias for self::get_code().
452
-	 *
453
-	 * @since 1.0.19
454
-	 * @param  string $context View or edit context.
455
-	 * @return string
456
-	 */
457
-	public function get_coupon_code( $context = 'view' ) {
458
-		return $this->get_code( $context );
459
-	}
450
+    /**
451
+     * Alias for self::get_code().
452
+     *
453
+     * @since 1.0.19
454
+     * @param  string $context View or edit context.
455
+     * @return string
456
+     */
457
+    public function get_coupon_code( $context = 'view' ) {
458
+        return $this->get_code( $context );
459
+    }
460 460
 	
461
-	/**
462
-	 * Alias for self::get_code().
463
-	 *
464
-	 * @since 1.0.19
465
-	 * @param  string $context View or edit context.
466
-	 * @return string
467
-	 */
468
-	public function get_discount_code( $context = 'view' ) {
469
-		return $this->get_code( $context );
470
-	}
461
+    /**
462
+     * Alias for self::get_code().
463
+     *
464
+     * @since 1.0.19
465
+     * @param  string $context View or edit context.
466
+     * @return string
467
+     */
468
+    public function get_discount_code( $context = 'view' ) {
469
+        return $this->get_code( $context );
470
+    }
471 471
 	
472
-	/**
473
-	 * Get the discount's amount.
474
-	 *
475
-	 * @since 1.0.19
476
-	 * @param  string $context View or edit context.
477
-	 * @return float
478
-	 */
479
-	public function get_amount( $context = 'view' ) {
480
-		return $this->get_prop( 'amount', $context );
481
-	}
482
-
483
-	/**
484
-	 * Get the discount's formated amount/rate.
485
-	 *
486
-	 * @since 1.0.19
487
-	 * @return string
488
-	 */
489
-	public function get_formatted_amount() {
490
-
491
-		if ( $this->is_type( 'flat' ) ) {
492
-			$rate = wpinv_price( wpinv_format_amount( $this->get_amount() ) );
493
-		} else {
494
-			$rate = $this->get_amount() . '%';
495
-		}
496
-
497
-		return apply_filters( 'wpinv_format_discount_rate', $rate, $this->get_type(), $this->get_amount() );
498
-	}
472
+    /**
473
+     * Get the discount's amount.
474
+     *
475
+     * @since 1.0.19
476
+     * @param  string $context View or edit context.
477
+     * @return float
478
+     */
479
+    public function get_amount( $context = 'view' ) {
480
+        return $this->get_prop( 'amount', $context );
481
+    }
482
+
483
+    /**
484
+     * Get the discount's formated amount/rate.
485
+     *
486
+     * @since 1.0.19
487
+     * @return string
488
+     */
489
+    public function get_formatted_amount() {
490
+
491
+        if ( $this->is_type( 'flat' ) ) {
492
+            $rate = wpinv_price( wpinv_format_amount( $this->get_amount() ) );
493
+        } else {
494
+            $rate = $this->get_amount() . '%';
495
+        }
496
+
497
+        return apply_filters( 'wpinv_format_discount_rate', $rate, $this->get_type(), $this->get_amount() );
498
+    }
499 499
 	
500
-	/**
501
-	 * Get the discount's start date.
502
-	 *
503
-	 * @since 1.0.19
504
-	 * @param  string $context View or edit context.
505
-	 * @return string
506
-	 */
507
-	public function get_start( $context = 'view' ) {
508
-		return $this->get_prop( 'start', $context );
509
-	}
500
+    /**
501
+     * Get the discount's start date.
502
+     *
503
+     * @since 1.0.19
504
+     * @param  string $context View or edit context.
505
+     * @return string
506
+     */
507
+    public function get_start( $context = 'view' ) {
508
+        return $this->get_prop( 'start', $context );
509
+    }
510 510
 	
511
-	/**
512
-	 * Alias for self::get_start().
513
-	 *
514
-	 * @since 1.0.19
515
-	 * @param  string $context View or edit context.
516
-	 * @return string
517
-	 */
518
-	public function get_start_date( $context = 'view' ) {
519
-		return $this->get_start( $context );
520
-	}
511
+    /**
512
+     * Alias for self::get_start().
513
+     *
514
+     * @since 1.0.19
515
+     * @param  string $context View or edit context.
516
+     * @return string
517
+     */
518
+    public function get_start_date( $context = 'view' ) {
519
+        return $this->get_start( $context );
520
+    }
521 521
 	
522
-	/**
523
-	 * Get the discount's expiration date.
524
-	 *
525
-	 * @since 1.0.19
526
-	 * @param  string $context View or edit context.
527
-	 * @return string
528
-	 */
529
-	public function get_expiration( $context = 'view' ) {
530
-		return $this->get_prop( 'expiration', $context );
531
-	}
522
+    /**
523
+     * Get the discount's expiration date.
524
+     *
525
+     * @since 1.0.19
526
+     * @param  string $context View or edit context.
527
+     * @return string
528
+     */
529
+    public function get_expiration( $context = 'view' ) {
530
+        return $this->get_prop( 'expiration', $context );
531
+    }
532 532
 	
533
-	/**
534
-	 * Alias for self::get_expiration().
535
-	 *
536
-	 * @since 1.0.19
537
-	 * @param  string $context View or edit context.
538
-	 * @return string
539
-	 */
540
-	public function get_expiration_date( $context = 'view' ) {
541
-		return $this->get_expiration( $context );
542
-	}
543
-
544
-	/**
545
-	 * Alias for self::get_expiration().
546
-	 *
547
-	 * @since 1.0.19
548
-	 * @param  string $context View or edit context.
549
-	 * @return string
550
-	 */
551
-	public function get_end_date( $context = 'view' ) {
552
-		return $this->get_expiration( $context );
553
-	}
533
+    /**
534
+     * Alias for self::get_expiration().
535
+     *
536
+     * @since 1.0.19
537
+     * @param  string $context View or edit context.
538
+     * @return string
539
+     */
540
+    public function get_expiration_date( $context = 'view' ) {
541
+        return $this->get_expiration( $context );
542
+    }
543
+
544
+    /**
545
+     * Alias for self::get_expiration().
546
+     *
547
+     * @since 1.0.19
548
+     * @param  string $context View or edit context.
549
+     * @return string
550
+     */
551
+    public function get_end_date( $context = 'view' ) {
552
+        return $this->get_expiration( $context );
553
+    }
554 554
 	
555
-	/**
556
-	 * Get the discount's type.
557
-	 *
558
-	 * @since 1.0.19
559
-	 * @param  string $context View or edit context.
560
-	 * @return string
561
-	 */
562
-	public function get_type( $context = 'view' ) {
563
-		return $this->get_prop( 'type', $context );
564
-	}
565
-
566
-	/**
567
-	 * Get the number of times a discount has been used.
568
-	 *
569
-	 * @since 1.0.19
570
-	 * @param  string $context View or edit context.
571
-	 * @return int
572
-	 */
573
-	public function get_uses( $context = 'view' ) {
574
-		return (int) $this->get_prop( 'uses', $context );
575
-	}
576
-
577
-	/**
578
-	 * Get the discount's usage, i.e uses / max uses.
579
-	 *
580
-	 * @since 1.0.19
581
-	 * @return string
582
-	 */
583
-	public function get_usage() {
584
-
585
-		if ( ! $this->has_limit() ) {
586
-			return $this->get_uses() . ' / ' . ' &infin;';
587
-		}
588
-
589
-		return $this->get_uses() . ' / ' . (int) $this->get_max_uses();
590
-
591
-	}
592
-
593
-	/**
594
-	 * Get the maximum number of time a discount can be used.
595
-	 *
596
-	 * @since 1.0.19
597
-	 * @param  string $context View or edit context.
598
-	 * @return int
599
-	 */
600
-	public function get_max_uses( $context = 'view' ) {
601
-		$max_uses = $this->get_prop( 'max_uses', $context );
602
-		return empty( $max_uses ) ? null : $max_uses;
603
-	}
604
-
605
-	/**
606
-	 * Checks if this is a single use discount or not.
607
-	 *
608
-	 * @since 1.0.19
609
-	 * @param  string $context View or edit context.
610
-	 * @return bool
611
-	 */
612
-	public function get_is_single_use( $context = 'view' ) {
613
-		return $this->get_prop( 'is_single_use', $context );
614
-	}
615
-
616
-	/**
617
-	 * Get the items that can be used with this discount.
618
-	 *
619
-	 * @since 1.0.19
620
-	 * @param  string $context View or edit context.
621
-	 * @return array
622
-	 */
623
-	public function get_items( $context = 'view' ) {
624
-		return wpinv_parse_list( $this->get_prop( 'items', $context ) );
625
-	}
626
-
627
-	/**
628
-	 * Alias for self::get_items().
629
-	 *
630
-	 * @since 1.0.19
631
-	 * @param  string $context View or edit context.
632
-	 * @return array
633
-	 */
634
-	public function get_allowed_items( $context = 'view' ) {
635
-		return $this->get_items( $context );
636
-	}
637
-
638
-	/**
639
-	 * Get the items that are not allowed to use this discount.
640
-	 *
641
-	 * @since 1.0.19
642
-	 * @param  string $context View or edit context.
643
-	 * @return array
644
-	 */
645
-	public function get_excluded_items( $context = 'view' ) {
646
-		return wpinv_parse_list( $this->get_prop( 'excluded_items', $context ) );
647
-	}
648
-
649
-	/**
650
-	 * Checks if this is a recurring discount or not.
651
-	 *
652
-	 * @since 1.0.19
653
-	 * @param  string $context View or edit context.
654
-	 * @return int|string|bool
655
-	 */
656
-	public function get_is_recurring( $context = 'view' ) {
657
-		return $this->get_prop( 'is_recurring', $context );
658
-	}
659
-
660
-	/**
661
-	 * Get's the minimum total amount allowed for this discount.
662
-	 *
663
-	 * @since 1.0.19
664
-	 * @param  string $context View or edit context.
665
-	 * @return float
666
-	 */
667
-	public function get_min_total( $context = 'view' ) {
668
-		$minimum = $this->get_prop( 'min_total', $context );
669
-		return empty( $minimum ) ? null : $minimum;
670
-	}
671
-
672
-	/**
673
-	 * Alias for self::get_min_total().
674
-	 *
675
-	 * @since 1.0.19
676
-	 * @param  string $context View or edit context.
677
-	 * @return float
678
-	 */
679
-	public function get_minimum_total( $context = 'view' ) {
680
-		return $this->get_min_total( $context );
681
-	}
682
-
683
-	/**
684
-	 * Get's the maximum total amount allowed for this discount.
685
-	 *
686
-	 * @since 1.0.19
687
-	 * @param  string $context View or edit context.
688
-	 * @return float
689
-	 */
690
-	public function get_max_total( $context = 'view' ) {
691
-		$maximum = $this->get_prop( 'max_total', $context );
692
-		return empty( $maximum ) ? null : $maximum;
693
-	}
694
-
695
-	/**
696
-	 * Alias for self::get_max_total().
697
-	 *
698
-	 * @since 1.0.19
699
-	 * @param  string $context View or edit context.
700
-	 * @return float
701
-	 */
702
-	public function get_maximum_total( $context = 'view' ) {
703
-		return $this->get_max_total( $context );
704
-	}
705
-
706
-	/**
707
-	 * Magic method for accessing discount properties.
708
-	 *
709
-	 * @since 1.0.15
710
-	 * @access public
711
-	 *
712
-	 * @param string $key Discount data to retrieve
713
-	 * @param  string $context View or edit context.
714
-	 * @return mixed Value of the given discount property (if set).
715
-	 */
716
-	public function get( $key, $context = 'view' ) {
555
+    /**
556
+     * Get the discount's type.
557
+     *
558
+     * @since 1.0.19
559
+     * @param  string $context View or edit context.
560
+     * @return string
561
+     */
562
+    public function get_type( $context = 'view' ) {
563
+        return $this->get_prop( 'type', $context );
564
+    }
565
+
566
+    /**
567
+     * Get the number of times a discount has been used.
568
+     *
569
+     * @since 1.0.19
570
+     * @param  string $context View or edit context.
571
+     * @return int
572
+     */
573
+    public function get_uses( $context = 'view' ) {
574
+        return (int) $this->get_prop( 'uses', $context );
575
+    }
576
+
577
+    /**
578
+     * Get the discount's usage, i.e uses / max uses.
579
+     *
580
+     * @since 1.0.19
581
+     * @return string
582
+     */
583
+    public function get_usage() {
584
+
585
+        if ( ! $this->has_limit() ) {
586
+            return $this->get_uses() . ' / ' . ' &infin;';
587
+        }
588
+
589
+        return $this->get_uses() . ' / ' . (int) $this->get_max_uses();
590
+
591
+    }
592
+
593
+    /**
594
+     * Get the maximum number of time a discount can be used.
595
+     *
596
+     * @since 1.0.19
597
+     * @param  string $context View or edit context.
598
+     * @return int
599
+     */
600
+    public function get_max_uses( $context = 'view' ) {
601
+        $max_uses = $this->get_prop( 'max_uses', $context );
602
+        return empty( $max_uses ) ? null : $max_uses;
603
+    }
604
+
605
+    /**
606
+     * Checks if this is a single use discount or not.
607
+     *
608
+     * @since 1.0.19
609
+     * @param  string $context View or edit context.
610
+     * @return bool
611
+     */
612
+    public function get_is_single_use( $context = 'view' ) {
613
+        return $this->get_prop( 'is_single_use', $context );
614
+    }
615
+
616
+    /**
617
+     * Get the items that can be used with this discount.
618
+     *
619
+     * @since 1.0.19
620
+     * @param  string $context View or edit context.
621
+     * @return array
622
+     */
623
+    public function get_items( $context = 'view' ) {
624
+        return wpinv_parse_list( $this->get_prop( 'items', $context ) );
625
+    }
626
+
627
+    /**
628
+     * Alias for self::get_items().
629
+     *
630
+     * @since 1.0.19
631
+     * @param  string $context View or edit context.
632
+     * @return array
633
+     */
634
+    public function get_allowed_items( $context = 'view' ) {
635
+        return $this->get_items( $context );
636
+    }
637
+
638
+    /**
639
+     * Get the items that are not allowed to use this discount.
640
+     *
641
+     * @since 1.0.19
642
+     * @param  string $context View or edit context.
643
+     * @return array
644
+     */
645
+    public function get_excluded_items( $context = 'view' ) {
646
+        return wpinv_parse_list( $this->get_prop( 'excluded_items', $context ) );
647
+    }
648
+
649
+    /**
650
+     * Checks if this is a recurring discount or not.
651
+     *
652
+     * @since 1.0.19
653
+     * @param  string $context View or edit context.
654
+     * @return int|string|bool
655
+     */
656
+    public function get_is_recurring( $context = 'view' ) {
657
+        return $this->get_prop( 'is_recurring', $context );
658
+    }
659
+
660
+    /**
661
+     * Get's the minimum total amount allowed for this discount.
662
+     *
663
+     * @since 1.0.19
664
+     * @param  string $context View or edit context.
665
+     * @return float
666
+     */
667
+    public function get_min_total( $context = 'view' ) {
668
+        $minimum = $this->get_prop( 'min_total', $context );
669
+        return empty( $minimum ) ? null : $minimum;
670
+    }
671
+
672
+    /**
673
+     * Alias for self::get_min_total().
674
+     *
675
+     * @since 1.0.19
676
+     * @param  string $context View or edit context.
677
+     * @return float
678
+     */
679
+    public function get_minimum_total( $context = 'view' ) {
680
+        return $this->get_min_total( $context );
681
+    }
682
+
683
+    /**
684
+     * Get's the maximum total amount allowed for this discount.
685
+     *
686
+     * @since 1.0.19
687
+     * @param  string $context View or edit context.
688
+     * @return float
689
+     */
690
+    public function get_max_total( $context = 'view' ) {
691
+        $maximum = $this->get_prop( 'max_total', $context );
692
+        return empty( $maximum ) ? null : $maximum;
693
+    }
694
+
695
+    /**
696
+     * Alias for self::get_max_total().
697
+     *
698
+     * @since 1.0.19
699
+     * @param  string $context View or edit context.
700
+     * @return float
701
+     */
702
+    public function get_maximum_total( $context = 'view' ) {
703
+        return $this->get_max_total( $context );
704
+    }
705
+
706
+    /**
707
+     * Magic method for accessing discount properties.
708
+     *
709
+     * @since 1.0.15
710
+     * @access public
711
+     *
712
+     * @param string $key Discount data to retrieve
713
+     * @param  string $context View or edit context.
714
+     * @return mixed Value of the given discount property (if set).
715
+     */
716
+    public function get( $key, $context = 'view' ) {
717 717
         return $this->get_prop( $key, $context );
718
-	}
718
+    }
719 719
 
720
-	/*
720
+    /*
721 721
 	|--------------------------------------------------------------------------
722 722
 	| Setters
723 723
 	|--------------------------------------------------------------------------
@@ -727,41 +727,41 @@  discard block
 block discarded – undo
727 727
 	| object.
728 728
 	*/
729 729
 	
730
-	/**
731
-	 * Sets discount status.
732
-	 *
733
-	 * @since 1.0.19
734
-	 * @param  string $status New status.
735
-	 * @return array details of change.
736
-	 */
737
-	public function set_status( $status ) {
730
+    /**
731
+     * Sets discount status.
732
+     *
733
+     * @since 1.0.19
734
+     * @param  string $status New status.
735
+     * @return array details of change.
736
+     */
737
+    public function set_status( $status ) {
738 738
         $old_status = $this->get_status();
739 739
 
740 740
         $this->set_prop( 'status', $status );
741 741
 
742
-		return array(
743
-			'from' => $old_status,
744
-			'to'   => $status,
745
-		);
742
+        return array(
743
+            'from' => $old_status,
744
+            'to'   => $status,
745
+        );
746 746
     }
747 747
 
748 748
     /**
749
-	 * Set plugin version when the discount was created.
750
-	 *
751
-	 * @since 1.0.19
752
-	 */
753
-	public function set_version( $value ) {
754
-		$this->set_prop( 'version', $value );
749
+     * Set plugin version when the discount was created.
750
+     *
751
+     * @since 1.0.19
752
+     */
753
+    public function set_version( $value ) {
754
+        $this->set_prop( 'version', $value );
755 755
     }
756 756
 
757 757
     /**
758
-	 * Set date when the discount was created.
759
-	 *
760
-	 * @since 1.0.19
761
-	 * @param string $value Value to set.
758
+     * Set date when the discount was created.
759
+     *
760
+     * @since 1.0.19
761
+     * @param string $value Value to set.
762 762
      * @return bool Whether or not the date was set.
763
-	 */
764
-	public function set_date_created( $value ) {
763
+     */
764
+    public function set_date_created( $value ) {
765 765
         $date = strtotime( $value );
766 766
 
767 767
         if ( $date ) {
@@ -774,13 +774,13 @@  discard block
 block discarded – undo
774 774
     }
775 775
 
776 776
     /**
777
-	 * Set date when the discount was last modified.
778
-	 *
779
-	 * @since 1.0.19
780
-	 * @param string $value Value to set.
777
+     * Set date when the discount was last modified.
778
+     *
779
+     * @since 1.0.19
780
+     * @param string $value Value to set.
781 781
      * @return bool Whether or not the date was set.
782
-	 */
783
-	public function set_date_modified( $value ) {
782
+     */
783
+    public function set_date_modified( $value ) {
784 784
         $date = strtotime( $value );
785 785
 
786 786
         if ( $date ) {
@@ -793,324 +793,324 @@  discard block
 block discarded – undo
793 793
     }
794 794
 
795 795
     /**
796
-	 * Set the discount name.
797
-	 *
798
-	 * @since 1.0.19
799
-	 * @param  string $value New name.
800
-	 */
801
-	public function set_name( $value ) {
796
+     * Set the discount name.
797
+     *
798
+     * @since 1.0.19
799
+     * @param  string $value New name.
800
+     */
801
+    public function set_name( $value ) {
802 802
         $name = sanitize_text_field( $value );
803
-		$this->set_prop( 'name', $name );
803
+        $this->set_prop( 'name', $name );
804 804
     }
805 805
 
806 806
     /**
807
-	 * Alias of self::set_name().
808
-	 *
809
-	 * @since 1.0.19
810
-	 * @param  string $value New name.
811
-	 */
812
-	public function set_title( $value ) {
813
-		$this->set_name( $value );
807
+     * Alias of self::set_name().
808
+     *
809
+     * @since 1.0.19
810
+     * @param  string $value New name.
811
+     */
812
+    public function set_title( $value ) {
813
+        $this->set_name( $value );
814 814
     }
815 815
 
816 816
     /**
817
-	 * Set the discount description.
818
-	 *
819
-	 * @since 1.0.19
820
-	 * @param  string $value New description.
821
-	 */
822
-	public function set_description( $value ) {
817
+     * Set the discount description.
818
+     *
819
+     * @since 1.0.19
820
+     * @param  string $value New description.
821
+     */
822
+    public function set_description( $value ) {
823 823
         $description = wp_kses_post( $value );
824
-		return $this->set_prop( 'description', $description );
824
+        return $this->set_prop( 'description', $description );
825 825
     }
826 826
 
827 827
     /**
828
-	 * Alias of self::set_description().
829
-	 *
830
-	 * @since 1.0.19
831
-	 * @param  string $value New description.
832
-	 */
833
-	public function set_excerpt( $value ) {
834
-		$this->set_description( $value );
828
+     * Alias of self::set_description().
829
+     *
830
+     * @since 1.0.19
831
+     * @param  string $value New description.
832
+     */
833
+    public function set_excerpt( $value ) {
834
+        $this->set_description( $value );
835 835
     }
836 836
 
837 837
     /**
838
-	 * Alias of self::set_description().
839
-	 *
840
-	 * @since 1.0.19
841
-	 * @param  string $value New description.
842
-	 */
843
-	public function set_summary( $value ) {
844
-		$this->set_description( $value );
838
+     * Alias of self::set_description().
839
+     *
840
+     * @since 1.0.19
841
+     * @param  string $value New description.
842
+     */
843
+    public function set_summary( $value ) {
844
+        $this->set_description( $value );
845 845
     }
846 846
 
847 847
     /**
848
-	 * Set the owner of the discount.
849
-	 *
850
-	 * @since 1.0.19
851
-	 * @param  int $value New author.
852
-	 */
853
-	public function set_author( $value ) {
854
-		$this->set_prop( 'author', (int) $value );
855
-	}
848
+     * Set the owner of the discount.
849
+     *
850
+     * @since 1.0.19
851
+     * @param  int $value New author.
852
+     */
853
+    public function set_author( $value ) {
854
+        $this->set_prop( 'author', (int) $value );
855
+    }
856 856
 	
857
-	/**
858
-	 * Sets the discount code.
859
-	 *
860
-	 * @since 1.0.19
861
-	 * @param string $value New discount code.
862
-	 */
863
-	public function set_code( $value ) {
864
-		$code = sanitize_text_field( $value );
865
-		$this->set_prop( 'code', $code );
866
-	}
857
+    /**
858
+     * Sets the discount code.
859
+     *
860
+     * @since 1.0.19
861
+     * @param string $value New discount code.
862
+     */
863
+    public function set_code( $value ) {
864
+        $code = sanitize_text_field( $value );
865
+        $this->set_prop( 'code', $code );
866
+    }
867 867
 	
868
-	/**
869
-	 * Alias of self::set_code().
870
-	 *
871
-	 * @since 1.0.19
872
-	 * @param string $value New discount code.
873
-	 */
874
-	public function set_coupon_code( $value ) {
875
-		$this->set_code( $value );
876
-	}
868
+    /**
869
+     * Alias of self::set_code().
870
+     *
871
+     * @since 1.0.19
872
+     * @param string $value New discount code.
873
+     */
874
+    public function set_coupon_code( $value ) {
875
+        $this->set_code( $value );
876
+    }
877 877
 	
878
-	/**
879
-	 * Alias of self::set_code().
880
-	 *
881
-	 * @since 1.0.19
882
-	 * @param string $value New discount code.
883
-	 */
884
-	public function set_discount_code( $value ) {
885
-		$this->set_code( $value );
886
-	}
878
+    /**
879
+     * Alias of self::set_code().
880
+     *
881
+     * @since 1.0.19
882
+     * @param string $value New discount code.
883
+     */
884
+    public function set_discount_code( $value ) {
885
+        $this->set_code( $value );
886
+    }
887 887
 	
888
-	/**
889
-	 * Sets the discount amount.
890
-	 *
891
-	 * @since 1.0.19
892
-	 * @param float $value New discount code.
893
-	 */
894
-	public function set_amount( $value ) {
895
-		$amount = floatval( wpinv_sanitize_amount( $value ) );
896
-		$this->set_prop( 'amount', $amount );
897
-	}
898
-
899
-	/**
900
-	 * Sets the discount's start date.
901
-	 *
902
-	 * @since 1.0.19
903
-	 * @param float $value New start date.
904
-	 */
905
-	public function set_start( $value ) {
906
-		$date = strtotime( $value );
888
+    /**
889
+     * Sets the discount amount.
890
+     *
891
+     * @since 1.0.19
892
+     * @param float $value New discount code.
893
+     */
894
+    public function set_amount( $value ) {
895
+        $amount = floatval( wpinv_sanitize_amount( $value ) );
896
+        $this->set_prop( 'amount', $amount );
897
+    }
907 898
 
908
-        if ( $date ) {
909
-            $this->set_prop( 'start', date( 'Y-m-d H:i', $date ) );
910
-            return true;
911
-		}
899
+    /**
900
+     * Sets the discount's start date.
901
+     *
902
+     * @since 1.0.19
903
+     * @param float $value New start date.
904
+     */
905
+    public function set_start( $value ) {
906
+        $date = strtotime( $value );
907
+
908
+        if ( $date ) {
909
+            $this->set_prop( 'start', date( 'Y-m-d H:i', $date ) );
910
+            return true;
911
+        }
912 912
 		
913
-		$this->set_prop( 'start', '' );
913
+        $this->set_prop( 'start', '' );
914 914
 
915 915
         return false;
916
-	}
917
-
918
-	/**
919
-	 * Alias of self::set_start().
920
-	 *
921
-	 * @since 1.0.19
922
-	 * @param string $value New start date.
923
-	 */
924
-	public function set_start_date( $value ) {
925
-		$this->set_start( $value );
926
-	}
927
-
928
-	/**
929
-	 * Sets the discount's expiration date.
930
-	 *
931
-	 * @since 1.0.19
932
-	 * @param float $value New expiration date.
933
-	 */
934
-	public function set_expiration( $value ) {
935
-		$date = strtotime( $value );
916
+    }
917
+
918
+    /**
919
+     * Alias of self::set_start().
920
+     *
921
+     * @since 1.0.19
922
+     * @param string $value New start date.
923
+     */
924
+    public function set_start_date( $value ) {
925
+        $this->set_start( $value );
926
+    }
927
+
928
+    /**
929
+     * Sets the discount's expiration date.
930
+     *
931
+     * @since 1.0.19
932
+     * @param float $value New expiration date.
933
+     */
934
+    public function set_expiration( $value ) {
935
+        $date = strtotime( $value );
936 936
 
937 937
         if ( $date ) {
938 938
             $this->set_prop( 'expiration', date( 'Y-m-d H:i', $date ) );
939 939
             return true;
940 940
         }
941 941
 
942
-		$this->set_prop( 'expiration', '' );
942
+        $this->set_prop( 'expiration', '' );
943 943
         return false;
944
-	}
945
-
946
-	/**
947
-	 * Alias of self::set_expiration().
948
-	 *
949
-	 * @since 1.0.19
950
-	 * @param string $value New expiration date.
951
-	 */
952
-	public function set_expiration_date( $value ) {
953
-		$this->set_expiration( $value );
954
-	}
955
-
956
-	/**
957
-	 * Alias of self::set_expiration().
958
-	 *
959
-	 * @since 1.0.19
960
-	 * @param string $value New expiration date.
961
-	 */
962
-	public function set_end_date( $value ) {
963
-		$this->set_expiration( $value );
964
-	}
965
-
966
-	/**
967
-	 * Sets the discount type.
968
-	 *
969
-	 * @since 1.0.19
970
-	 * @param string $value New discount type.
971
-	 */
972
-	public function set_type( $value ) {
973
-		if ( $value && array_key_exists( sanitize_text_field( $value ), wpinv_get_discount_types() ) ) {
974
-			$this->set_prop( 'type', sanitize_text_field( $value ) );
975
-		}
976
-	}
977
-
978
-	/**
979
-	 * Sets the number of times a discount has been used.
980
-	 *
981
-	 * @since 1.0.19
982
-	 * @param int $value usage count.
983
-	 */
984
-	public function set_uses( $value ) {
985
-
986
-		$value = (int) $value;
987
-
988
-		if ( $value < 0 ) {
989
-			$value = 0;
990
-		}
991
-
992
-		$this->set_prop( 'uses', (int) $value );
993
-	}
994
-
995
-	/**
996
-	 * Sets the maximum number of times a discount can be used.
997
-	 *
998
-	 * @since 1.0.19
999
-	 * @param int $value maximum usage count.
1000
-	 */
1001
-	public function set_max_uses( $value ) {
1002
-		$this->set_prop( 'max_uses', absint( $value ) );
1003
-	}
1004
-
1005
-	/**
1006
-	 * Sets if this is a single use discount or not.
1007
-	 *
1008
-	 * @since 1.0.19
1009
-	 * @param int|bool $value is single use.
1010
-	 */
1011
-	public function set_is_single_use( $value ) {
1012
-		$this->set_prop( 'is_single_use', (bool) $value );
1013
-	}
1014
-
1015
-	/**
1016
-	 * Sets the items that can be used with this discount.
1017
-	 *
1018
-	 * @since 1.0.19
1019
-	 * @param array $value items.
1020
-	 */
1021
-	public function set_items( $value ) {
1022
-		$this->set_prop( 'items', wpinv_parse_list( $value ) );
1023
-	}
1024
-
1025
-	/**
1026
-	 * Alias for self::set_items().
1027
-	 *
1028
-	 * @since 1.0.19
1029
-	 * @param array $value items.
1030
-	 */
1031
-	public function set_allowed_items( $value ) {
1032
-		$this->set_items( $value );
1033
-	}
1034
-
1035
-	/**
1036
-	 * Sets the items that can not be used with this discount.
1037
-	 *
1038
-	 * @since 1.0.19
1039
-	 * @param array $value items.
1040
-	 */
1041
-	public function set_excluded_items( $value ) {
1042
-		$this->set_prop( 'excluded_items', wpinv_parse_list( $value ) );
1043
-	}
1044
-
1045
-	/**
1046
-	 * Sets if this is a recurring discounts or not.
1047
-	 *
1048
-	 * @since 1.0.19
1049
-	 * @param int|bool $value is recurring.
1050
-	 */
1051
-	public function set_is_recurring( $value ) {
1052
-		$this->set_prop( 'is_recurring', (bool) $value );
1053
-	}
1054
-
1055
-	/**
1056
-	 * Sets the minimum total that can not be used with this discount.
1057
-	 *
1058
-	 * @since 1.0.19
1059
-	 * @param float $value minimum total.
1060
-	 */
1061
-	public function set_min_total( $value ) {
1062
-		$this->set_prop( 'min_total', (float) wpinv_sanitize_amount( $value ) );
1063
-	}
1064
-
1065
-	/**
1066
-	 * Alias for self::set_min_total().
1067
-	 *
1068
-	 * @since 1.0.19
1069
-	 * @param float $value minimum total.
1070
-	 */
1071
-	public function set_minimum_total( $value ) {
1072
-		$this->set_min_total( $value );
1073
-	}
1074
-
1075
-	/**
1076
-	 * Sets the maximum total that can not be used with this discount.
1077
-	 *
1078
-	 * @since 1.0.19
1079
-	 * @param float $value maximum total.
1080
-	 */
1081
-	public function set_max_total( $value ) {
1082
-		$this->set_prop( 'max_total', (float) wpinv_sanitize_amount( $value ) );
1083
-	}
1084
-
1085
-	/**
1086
-	 * Alias for self::set_max_total().
1087
-	 *
1088
-	 * @since 1.0.19
1089
-	 * @param float $value maximum total.
1090
-	 */
1091
-	public function set_maximum_total( $value ) {
1092
-		$this->set_max_total( $value );
1093
-	}
1094
-
1095
-	/**
1096
-	 * @deprecated
1097
-	 */
1098
-	public function refresh(){}
1099
-
1100
-	/**
1101
-	 * @deprecated
1102
-	 *
1103
-	 */
1104
-	public function update_status( $status = 'publish' ){
1105
-
1106
-		if ( $this->exists() && $this->get_status() != $status ) {
1107
-			$this->set_status( $status );
1108
-			$this->save();
1109
-		}
1110
-
1111
-	}
1112
-
1113
-	/*
944
+    }
945
+
946
+    /**
947
+     * Alias of self::set_expiration().
948
+     *
949
+     * @since 1.0.19
950
+     * @param string $value New expiration date.
951
+     */
952
+    public function set_expiration_date( $value ) {
953
+        $this->set_expiration( $value );
954
+    }
955
+
956
+    /**
957
+     * Alias of self::set_expiration().
958
+     *
959
+     * @since 1.0.19
960
+     * @param string $value New expiration date.
961
+     */
962
+    public function set_end_date( $value ) {
963
+        $this->set_expiration( $value );
964
+    }
965
+
966
+    /**
967
+     * Sets the discount type.
968
+     *
969
+     * @since 1.0.19
970
+     * @param string $value New discount type.
971
+     */
972
+    public function set_type( $value ) {
973
+        if ( $value && array_key_exists( sanitize_text_field( $value ), wpinv_get_discount_types() ) ) {
974
+            $this->set_prop( 'type', sanitize_text_field( $value ) );
975
+        }
976
+    }
977
+
978
+    /**
979
+     * Sets the number of times a discount has been used.
980
+     *
981
+     * @since 1.0.19
982
+     * @param int $value usage count.
983
+     */
984
+    public function set_uses( $value ) {
985
+
986
+        $value = (int) $value;
987
+
988
+        if ( $value < 0 ) {
989
+            $value = 0;
990
+        }
991
+
992
+        $this->set_prop( 'uses', (int) $value );
993
+    }
994
+
995
+    /**
996
+     * Sets the maximum number of times a discount can be used.
997
+     *
998
+     * @since 1.0.19
999
+     * @param int $value maximum usage count.
1000
+     */
1001
+    public function set_max_uses( $value ) {
1002
+        $this->set_prop( 'max_uses', absint( $value ) );
1003
+    }
1004
+
1005
+    /**
1006
+     * Sets if this is a single use discount or not.
1007
+     *
1008
+     * @since 1.0.19
1009
+     * @param int|bool $value is single use.
1010
+     */
1011
+    public function set_is_single_use( $value ) {
1012
+        $this->set_prop( 'is_single_use', (bool) $value );
1013
+    }
1014
+
1015
+    /**
1016
+     * Sets the items that can be used with this discount.
1017
+     *
1018
+     * @since 1.0.19
1019
+     * @param array $value items.
1020
+     */
1021
+    public function set_items( $value ) {
1022
+        $this->set_prop( 'items', wpinv_parse_list( $value ) );
1023
+    }
1024
+
1025
+    /**
1026
+     * Alias for self::set_items().
1027
+     *
1028
+     * @since 1.0.19
1029
+     * @param array $value items.
1030
+     */
1031
+    public function set_allowed_items( $value ) {
1032
+        $this->set_items( $value );
1033
+    }
1034
+
1035
+    /**
1036
+     * Sets the items that can not be used with this discount.
1037
+     *
1038
+     * @since 1.0.19
1039
+     * @param array $value items.
1040
+     */
1041
+    public function set_excluded_items( $value ) {
1042
+        $this->set_prop( 'excluded_items', wpinv_parse_list( $value ) );
1043
+    }
1044
+
1045
+    /**
1046
+     * Sets if this is a recurring discounts or not.
1047
+     *
1048
+     * @since 1.0.19
1049
+     * @param int|bool $value is recurring.
1050
+     */
1051
+    public function set_is_recurring( $value ) {
1052
+        $this->set_prop( 'is_recurring', (bool) $value );
1053
+    }
1054
+
1055
+    /**
1056
+     * Sets the minimum total that can not be used with this discount.
1057
+     *
1058
+     * @since 1.0.19
1059
+     * @param float $value minimum total.
1060
+     */
1061
+    public function set_min_total( $value ) {
1062
+        $this->set_prop( 'min_total', (float) wpinv_sanitize_amount( $value ) );
1063
+    }
1064
+
1065
+    /**
1066
+     * Alias for self::set_min_total().
1067
+     *
1068
+     * @since 1.0.19
1069
+     * @param float $value minimum total.
1070
+     */
1071
+    public function set_minimum_total( $value ) {
1072
+        $this->set_min_total( $value );
1073
+    }
1074
+
1075
+    /**
1076
+     * Sets the maximum total that can not be used with this discount.
1077
+     *
1078
+     * @since 1.0.19
1079
+     * @param float $value maximum total.
1080
+     */
1081
+    public function set_max_total( $value ) {
1082
+        $this->set_prop( 'max_total', (float) wpinv_sanitize_amount( $value ) );
1083
+    }
1084
+
1085
+    /**
1086
+     * Alias for self::set_max_total().
1087
+     *
1088
+     * @since 1.0.19
1089
+     * @param float $value maximum total.
1090
+     */
1091
+    public function set_maximum_total( $value ) {
1092
+        $this->set_max_total( $value );
1093
+    }
1094
+
1095
+    /**
1096
+     * @deprecated
1097
+     */
1098
+    public function refresh(){}
1099
+
1100
+    /**
1101
+     * @deprecated
1102
+     *
1103
+     */
1104
+    public function update_status( $status = 'publish' ){
1105
+
1106
+        if ( $this->exists() && $this->get_status() != $status ) {
1107
+            $this->set_status( $status );
1108
+            $this->save();
1109
+        }
1110
+
1111
+    }
1112
+
1113
+    /*
1114 1114
 	|--------------------------------------------------------------------------
1115 1115
 	| Conditionals
1116 1116
 	|--------------------------------------------------------------------------
@@ -1119,263 +1119,263 @@  discard block
 block discarded – undo
1119 1119
 	|
1120 1120
 	*/
1121 1121
 
1122
-	/**
1123
-	 * Checks whether a discount exists in the database or not
1124
-	 *
1125
-	 * @since 1.0.15
1126
-	 */
1127
-	public function exists(){
1128
-		$id = $this->get_id();
1129
-		return ! empty( $id );
1130
-	}
1131
-
1132
-	/**
1133
-	 * Checks the discount type.
1134
-	 *
1135
-	 *
1136
-	 * @param  string $type the discount type to check against
1137
-	 * @since 1.0.15
1138
-	 * @return bool
1139
-	 */
1140
-	public function is_type( $type ) {
1141
-		return $this->get_type() == $type;
1142
-	}
1143
-
1144
-	/**
1145
-	 * Checks whether the discount is published or not
1146
-	 *
1147
-	 * @since 1.0.15
1148
-	 * @return bool
1149
-	 */
1150
-	public function is_active() {
1151
-		return $this->get_status() == 'publish';
1152
-	}
1153
-
1154
-	/**
1155
-	 * Checks whether the discount has max uses
1156
-	 *
1157
-	 * @since 1.0.15
1158
-	 * @return bool
1159
-	 */
1160
-	public function has_limit() {
1161
-		$limit = $this->get_max_uses();
1162
-		return ! empty( $limit );
1163
-	}
1164
-
1165
-	/**
1166
-	 * Checks whether the discount has ever been used.
1167
-	 *
1168
-	 * @since 1.0.15
1169
-	 * @return bool
1170
-	 */
1171
-	public function has_uses() {
1172
-		return $this->get_uses() > 0;
1173
-	}
1174
-
1175
-	/**
1176
-	 * Checks whether the discount is has exided the usage limit or not
1177
-	 *
1178
-	 * @since 1.0.15
1179
-	 * @return bool
1180
-	 */
1181
-	public function has_exceeded_limit() {
1182
-
1183
-		if ( ! $this->has_limit() || ! $this->has_uses() ) {
1184
-			$exceeded = false ;
1185
-		} else {
1186
-			$exceeded = ! ( (int) $this->get_max_uses() < $this->get_uses() );
1187
-		}
1188
-
1189
-		return apply_filters( 'wpinv_is_discount_maxed_out', $exceeded, $this->get_id(), $this, $this->get_code() );
1190
-	}
1191
-
1192
-	/**
1193
-	 * Checks whether the discount has an expiration date.
1194
-	 *
1195
-	 * @since 1.0.15
1196
-	 * @return bool
1197
-	 */
1198
-	public function has_expiration_date() {
1199
-		$date = $this->get_expiration_date();
1200
-		return ! empty( $date );
1201
-	}
1202
-
1203
-	/**
1204
-	 * Checks if the discount is expired
1205
-	 *
1206
-	 * @since 1.0.15
1207
-	 * @return bool
1208
-	 */
1209
-	public function is_expired() {
1210
-		$expired = $this->has_expiration_date() ? current_time( 'timestamp' ) > strtotime( $this->get_expiration_date() ) : false;
1211
-		return apply_filters( 'wpinv_is_discount_expired', $expired, $this->get_id(), $this, $this->get_code() );
1212
-	}
1213
-
1214
-	/**
1215
-	 * Checks whether the discount has a start date.
1216
-	 *
1217
-	 * @since 1.0.15
1218
-	 * @return bool
1219
-	 */
1220
-	public function has_start_date() {
1221
-		$date = $this->get_start_date();
1222
-		return ! empty( $date );
1223
-	}
1224
-
1225
-	/**
1226
-	 * Checks the discount start date.
1227
-	 *
1228
-	 * @since 1.0.15
1229
-	 * @return bool
1230
-	 */
1231
-	public function has_started() {
1232
-		$started = $this->has_start_date() ? true : current_time( 'timestamp' ) > strtotime( $this->get_start_date() );
1233
-		return apply_filters( 'wpinv_is_discount_started', $started, $this->get_id(), $this, $this->get_code() );
1234
-	}
1235
-
1236
-	/**
1237
-	 * Checks the discount has allowed items or not.
1238
-	 *
1239
-	 * @since 1.0.15
1240
-	 * @return bool
1241
-	 */
1242
-	public function has_allowed_items() {
1243
-		$allowed_items = $this->get_allowed_items();
1244
-		return ! empty( $allowed_items );
1245
-	}
1246
-
1247
-	/**
1248
-	 * Checks the discount has excluded items or not.
1249
-	 *
1250
-	 * @since 1.0.15
1251
-	 * @return bool
1252
-	 */
1253
-	public function has_excluded_items() {
1254
-		$excluded_items = $this->get_excluded_items();
1255
-		return ! empty( $excluded_items );
1256
-	}
1257
-
1258
-	/**
1259
-	 * Check if a discount is valid for a given item id.
1260
-	 *
1261
-	 * @param  int|int[]  $item_ids
1262
-	 * @since 1.0.15
1263
-	 * @return boolean
1264
-	 */
1265
-	public function is_valid_for_items( $item_ids ) {
1266
-
1267
-		$item_ids = wp_parse_id_list( $item_ids );
1268
-		$included = array_intersect( $item_ids, $this->get_allowed_items() );
1269
-		$excluded = array_intersect( $item_ids, $this->get_excluded_items() );
1270
-
1271
-		if ( $this->has_excluded_items() && ! empty( $excluded ) ) {
1272
-			return false;
1273
-		}
1274
-
1275
-		if ( $this->has_allowed_items() && empty( $included ) ) {
1276
-			return false;
1277
-		}
1278
-
1279
-		return true;
1280
-	}
1281
-
1282
-	/**
1283
-	 * Check if a discount is valid for the given amount
1284
-	 *
1285
-	 * @param  float  $amount The amount to check against
1286
-	 * @since 1.0.15
1287
-	 * @return boolean
1288
-	 */
1289
-	public function is_valid_for_amount( $amount ) {
1290
-		return $this->is_minimum_amount_met( $amount ) && $this->is_maximum_amount_met( $amount );
1291
-	}
1292
-
1293
-	/**
1294
-	 * Checks if the minimum amount is set
1295
-	 *
1296
-	 * @since 1.0.15
1297
-	 * @return boolean
1298
-	 */
1299
-	public function has_minimum_amount() {
1300
-		$minimum = $this->get_minimum_total();
1301
-		return ! empty( $minimum );
1302
-	}
1303
-
1304
-	/**
1305
-	 * Checks if the minimum amount is met
1306
-	 *
1307
-	 * @param  float  $amount The amount to check against
1308
-	 * @since 1.0.15
1309
-	 * @return boolean
1310
-	 */
1311
-	public function is_minimum_amount_met( $amount ) {
1312
-		$amount = floatval( wpinv_sanitize_amount( $amount ) );
1313
-		$min_met= ! ( $this->has_minimum_amount() && $amount < floatval( wpinv_sanitize_amount( $this->get_minimum_total() ) ) );
1314
-		return apply_filters( 'wpinv_is_discount_min_met', $min_met, $this->get_id(), $this, $this->get_code(), $amount );
1315
-	}
1316
-
1317
-	/**
1318
-	 * Checks if the maximum amount is set
1319
-	 *
1320
-	 * @since 1.0.15
1321
-	 * @return boolean
1322
-	 */
1323
-	public function has_maximum_amount() {
1324
-		$maximum = $this->get_maximum_total();
1325
-		return ! empty( $maximum );
1326
-	}
1327
-
1328
-	/**
1329
-	 * Checks if the maximum amount is met
1330
-	 *
1331
-	 * @param  float  $amount The amount to check against
1332
-	 * @since 1.0.15
1333
-	 * @return boolean
1334
-	 */
1335
-	public function is_maximum_amount_met( $amount ) {
1336
-		$amount = floatval( wpinv_sanitize_amount( $amount ) );
1337
-		$max_met= ! ( $this->has_maximum_amount() && $amount > floatval( wpinv_sanitize_amount( $this->get_maximum_total() ) ) );
1338
-		return apply_filters( 'wpinv_is_discount_max_met', $max_met, $this->get_id(), $this, $this->get_code(), $amount );
1339
-	}
1340
-
1341
-	/**
1342
-	 * Checks if the discount is recurring.
1343
-	 *
1344
-	 * @since 1.0.15
1345
-	 * @return boolean
1346
-	 */
1347
-	public function is_recurring() {
1348
-		$recurring = $this->get_is_recurring();
1349
-		return ! empty( $recurring );
1350
-	}
1351
-
1352
-	/**
1353
-	 * Checks if the discount is single use.
1354
-	 *
1355
-	 * @since 1.0.15
1356
-	 * @return boolean
1357
-	 */
1358
-	public function is_single_use() {
1359
-		$usage = $this->get_is_single_use();
1360
-		return ! empty( $usage );
1361
-	}
1362
-
1363
-	/**
1364
-	 * Check if a discount is valid for the given user
1365
-	 *
1366
-	 * @param  int|string  $user
1367
-	 * @since 1.0.15
1368
-	 * @return boolean
1369
-	 */
1370
-	public function is_valid_for_user( $user ) {
1371
-
1372
-		// Ensure that the discount is single use.
1373
-		if ( empty( $user ) || ! $this->is_single_use() ) {
1374
-			return true;
1375
-		}
1376
-
1377
-		// Prepare the user id.
1378
-		$user_id = 0;
1122
+    /**
1123
+     * Checks whether a discount exists in the database or not
1124
+     *
1125
+     * @since 1.0.15
1126
+     */
1127
+    public function exists(){
1128
+        $id = $this->get_id();
1129
+        return ! empty( $id );
1130
+    }
1131
+
1132
+    /**
1133
+     * Checks the discount type.
1134
+     *
1135
+     *
1136
+     * @param  string $type the discount type to check against
1137
+     * @since 1.0.15
1138
+     * @return bool
1139
+     */
1140
+    public function is_type( $type ) {
1141
+        return $this->get_type() == $type;
1142
+    }
1143
+
1144
+    /**
1145
+     * Checks whether the discount is published or not
1146
+     *
1147
+     * @since 1.0.15
1148
+     * @return bool
1149
+     */
1150
+    public function is_active() {
1151
+        return $this->get_status() == 'publish';
1152
+    }
1153
+
1154
+    /**
1155
+     * Checks whether the discount has max uses
1156
+     *
1157
+     * @since 1.0.15
1158
+     * @return bool
1159
+     */
1160
+    public function has_limit() {
1161
+        $limit = $this->get_max_uses();
1162
+        return ! empty( $limit );
1163
+    }
1164
+
1165
+    /**
1166
+     * Checks whether the discount has ever been used.
1167
+     *
1168
+     * @since 1.0.15
1169
+     * @return bool
1170
+     */
1171
+    public function has_uses() {
1172
+        return $this->get_uses() > 0;
1173
+    }
1174
+
1175
+    /**
1176
+     * Checks whether the discount is has exided the usage limit or not
1177
+     *
1178
+     * @since 1.0.15
1179
+     * @return bool
1180
+     */
1181
+    public function has_exceeded_limit() {
1182
+
1183
+        if ( ! $this->has_limit() || ! $this->has_uses() ) {
1184
+            $exceeded = false ;
1185
+        } else {
1186
+            $exceeded = ! ( (int) $this->get_max_uses() < $this->get_uses() );
1187
+        }
1188
+
1189
+        return apply_filters( 'wpinv_is_discount_maxed_out', $exceeded, $this->get_id(), $this, $this->get_code() );
1190
+    }
1191
+
1192
+    /**
1193
+     * Checks whether the discount has an expiration date.
1194
+     *
1195
+     * @since 1.0.15
1196
+     * @return bool
1197
+     */
1198
+    public function has_expiration_date() {
1199
+        $date = $this->get_expiration_date();
1200
+        return ! empty( $date );
1201
+    }
1202
+
1203
+    /**
1204
+     * Checks if the discount is expired
1205
+     *
1206
+     * @since 1.0.15
1207
+     * @return bool
1208
+     */
1209
+    public function is_expired() {
1210
+        $expired = $this->has_expiration_date() ? current_time( 'timestamp' ) > strtotime( $this->get_expiration_date() ) : false;
1211
+        return apply_filters( 'wpinv_is_discount_expired', $expired, $this->get_id(), $this, $this->get_code() );
1212
+    }
1213
+
1214
+    /**
1215
+     * Checks whether the discount has a start date.
1216
+     *
1217
+     * @since 1.0.15
1218
+     * @return bool
1219
+     */
1220
+    public function has_start_date() {
1221
+        $date = $this->get_start_date();
1222
+        return ! empty( $date );
1223
+    }
1224
+
1225
+    /**
1226
+     * Checks the discount start date.
1227
+     *
1228
+     * @since 1.0.15
1229
+     * @return bool
1230
+     */
1231
+    public function has_started() {
1232
+        $started = $this->has_start_date() ? true : current_time( 'timestamp' ) > strtotime( $this->get_start_date() );
1233
+        return apply_filters( 'wpinv_is_discount_started', $started, $this->get_id(), $this, $this->get_code() );
1234
+    }
1235
+
1236
+    /**
1237
+     * Checks the discount has allowed items or not.
1238
+     *
1239
+     * @since 1.0.15
1240
+     * @return bool
1241
+     */
1242
+    public function has_allowed_items() {
1243
+        $allowed_items = $this->get_allowed_items();
1244
+        return ! empty( $allowed_items );
1245
+    }
1246
+
1247
+    /**
1248
+     * Checks the discount has excluded items or not.
1249
+     *
1250
+     * @since 1.0.15
1251
+     * @return bool
1252
+     */
1253
+    public function has_excluded_items() {
1254
+        $excluded_items = $this->get_excluded_items();
1255
+        return ! empty( $excluded_items );
1256
+    }
1257
+
1258
+    /**
1259
+     * Check if a discount is valid for a given item id.
1260
+     *
1261
+     * @param  int|int[]  $item_ids
1262
+     * @since 1.0.15
1263
+     * @return boolean
1264
+     */
1265
+    public function is_valid_for_items( $item_ids ) {
1266
+
1267
+        $item_ids = wp_parse_id_list( $item_ids );
1268
+        $included = array_intersect( $item_ids, $this->get_allowed_items() );
1269
+        $excluded = array_intersect( $item_ids, $this->get_excluded_items() );
1270
+
1271
+        if ( $this->has_excluded_items() && ! empty( $excluded ) ) {
1272
+            return false;
1273
+        }
1274
+
1275
+        if ( $this->has_allowed_items() && empty( $included ) ) {
1276
+            return false;
1277
+        }
1278
+
1279
+        return true;
1280
+    }
1281
+
1282
+    /**
1283
+     * Check if a discount is valid for the given amount
1284
+     *
1285
+     * @param  float  $amount The amount to check against
1286
+     * @since 1.0.15
1287
+     * @return boolean
1288
+     */
1289
+    public function is_valid_for_amount( $amount ) {
1290
+        return $this->is_minimum_amount_met( $amount ) && $this->is_maximum_amount_met( $amount );
1291
+    }
1292
+
1293
+    /**
1294
+     * Checks if the minimum amount is set
1295
+     *
1296
+     * @since 1.0.15
1297
+     * @return boolean
1298
+     */
1299
+    public function has_minimum_amount() {
1300
+        $minimum = $this->get_minimum_total();
1301
+        return ! empty( $minimum );
1302
+    }
1303
+
1304
+    /**
1305
+     * Checks if the minimum amount is met
1306
+     *
1307
+     * @param  float  $amount The amount to check against
1308
+     * @since 1.0.15
1309
+     * @return boolean
1310
+     */
1311
+    public function is_minimum_amount_met( $amount ) {
1312
+        $amount = floatval( wpinv_sanitize_amount( $amount ) );
1313
+        $min_met= ! ( $this->has_minimum_amount() && $amount < floatval( wpinv_sanitize_amount( $this->get_minimum_total() ) ) );
1314
+        return apply_filters( 'wpinv_is_discount_min_met', $min_met, $this->get_id(), $this, $this->get_code(), $amount );
1315
+    }
1316
+
1317
+    /**
1318
+     * Checks if the maximum amount is set
1319
+     *
1320
+     * @since 1.0.15
1321
+     * @return boolean
1322
+     */
1323
+    public function has_maximum_amount() {
1324
+        $maximum = $this->get_maximum_total();
1325
+        return ! empty( $maximum );
1326
+    }
1327
+
1328
+    /**
1329
+     * Checks if the maximum amount is met
1330
+     *
1331
+     * @param  float  $amount The amount to check against
1332
+     * @since 1.0.15
1333
+     * @return boolean
1334
+     */
1335
+    public function is_maximum_amount_met( $amount ) {
1336
+        $amount = floatval( wpinv_sanitize_amount( $amount ) );
1337
+        $max_met= ! ( $this->has_maximum_amount() && $amount > floatval( wpinv_sanitize_amount( $this->get_maximum_total() ) ) );
1338
+        return apply_filters( 'wpinv_is_discount_max_met', $max_met, $this->get_id(), $this, $this->get_code(), $amount );
1339
+    }
1340
+
1341
+    /**
1342
+     * Checks if the discount is recurring.
1343
+     *
1344
+     * @since 1.0.15
1345
+     * @return boolean
1346
+     */
1347
+    public function is_recurring() {
1348
+        $recurring = $this->get_is_recurring();
1349
+        return ! empty( $recurring );
1350
+    }
1351
+
1352
+    /**
1353
+     * Checks if the discount is single use.
1354
+     *
1355
+     * @since 1.0.15
1356
+     * @return boolean
1357
+     */
1358
+    public function is_single_use() {
1359
+        $usage = $this->get_is_single_use();
1360
+        return ! empty( $usage );
1361
+    }
1362
+
1363
+    /**
1364
+     * Check if a discount is valid for the given user
1365
+     *
1366
+     * @param  int|string  $user
1367
+     * @since 1.0.15
1368
+     * @return boolean
1369
+     */
1370
+    public function is_valid_for_user( $user ) {
1371
+
1372
+        // Ensure that the discount is single use.
1373
+        if ( empty( $user ) || ! $this->is_single_use() ) {
1374
+            return true;
1375
+        }
1376
+
1377
+        // Prepare the user id.
1378
+        $user_id = 0;
1379 1379
         if ( is_numeric( $user ) ) {
1380 1380
             $user_id = absint( $user );
1381 1381
         } else if ( is_email( $user ) && $user_data = get_user_by( 'email', $user ) ) {
@@ -1384,117 +1384,117 @@  discard block
 block discarded – undo
1384 1384
             $user_id = $user_data->ID;
1385 1385
         }
1386 1386
 
1387
-		// Ensure that we have a user.
1388
-		if ( empty( $user_id ) ) {
1389
-			return true;
1390
-		}
1387
+        // Ensure that we have a user.
1388
+        if ( empty( $user_id ) ) {
1389
+            return true;
1390
+        }
1391 1391
 
1392
-		// Get all payments with matching user id.
1392
+        // Get all payments with matching user id.
1393 1393
         $payments = wpinv_get_invoices( array( 'user' => $user_id, 'limit' => false, 'paginate' => false ) );
1394
-		$code     = strtolower( $this->get_code() );
1395
-
1396
-		// For each payment...
1397
-		foreach ( $payments as $payment ) {
1398
-
1399
-			// Only check for paid invoices.
1400
-			if ( $payment->is_paid() && strtolower( $payment->get_discount_code() ) == $code ) {
1401
-				return false;
1402
-			}
1403
-
1404
-		}
1405
-
1406
-		return true;
1407
-	}
1408
-
1409
-	/**
1410
-	 * Deletes the discount from the database
1411
-	 *
1412
-	 * @since 1.0.15
1413
-	 * @return boolean
1414
-	 */
1415
-	public function remove() {
1416
-		return $this->delete();
1417
-	}
1418
-
1419
-	/**
1420
-	 * Increases a discount's usage.
1421
-	 *
1422
-	 * @since 1.0.15
1423
-	 * @param int $by The number of usages to increas by.
1424
-	 * @return int
1425
-	 */
1426
-	public function increase_usage( $by = 1 ) {
1427
-
1428
-		// Abort if zero.
1429
-		if ( empty( $by ) ) {
1430
-			return;
1431
-		}
1432
-
1433
-		// Increase the usage.
1434
-		$this->set_uses( $this->get_uses() + (int) $by );
1435
-
1436
-		// Save the discount.
1437
-		$this->save();
1438
-
1439
-		// Fire relevant hooks.
1440
-		if( (int) $by > 0 ) {
1441
-			do_action( 'wpinv_discount_increase_use_count', $this->get_uses(), $this->get_id(), $this->get_code(),  absint( $by ) );
1442
-		} else {
1443
-			do_action( 'wpinv_discount_decrease_use_count', $this->get_uses(), $this->get_id(), $this->get_code(), absint( $by ) );
1444
-		}
1445
-
1446
-		// Return the number of times the discount has been used.
1447
-		return $this->get_uses();
1448
-	}
1449
-
1450
-	/**
1451
-	 * Alias of self::__toString()
1452
-	 *
1453
-	 * @since 1.0.15
1454
-	 * @return string|false
1455
-	 */
1456
-	public function get_data_as_json() {
1457
-		return $this->__toString();
1458
-	}
1459
-
1460
-	/**
1461
-	 * Returns a discount's discounted amount.
1462
-	 *
1463
-	 * @since 1.0.15
1464
-	 * @param float $amount
1465
-	 * @return float
1466
-	 */
1467
-	public function get_discounted_amount( $amount ) {
1468
-
1469
-		// Convert amount to float.
1470
-		$amount = (float) $amount;
1471
-
1472
-		// Get discount amount.
1473
-		$discount_amount = $this->get_amount();
1474
-
1475
-		if ( empty( $discount_amount ) ) {
1476
-			return 0;
1477
-		}
1478
-
1479
-		// Format the amount.
1480
-		$discount_amount = floatval( wpinv_sanitize_amount( $discount_amount ) );
1394
+        $code     = strtolower( $this->get_code() );
1395
+
1396
+        // For each payment...
1397
+        foreach ( $payments as $payment ) {
1398
+
1399
+            // Only check for paid invoices.
1400
+            if ( $payment->is_paid() && strtolower( $payment->get_discount_code() ) == $code ) {
1401
+                return false;
1402
+            }
1403
+
1404
+        }
1405
+
1406
+        return true;
1407
+    }
1408
+
1409
+    /**
1410
+     * Deletes the discount from the database
1411
+     *
1412
+     * @since 1.0.15
1413
+     * @return boolean
1414
+     */
1415
+    public function remove() {
1416
+        return $this->delete();
1417
+    }
1418
+
1419
+    /**
1420
+     * Increases a discount's usage.
1421
+     *
1422
+     * @since 1.0.15
1423
+     * @param int $by The number of usages to increas by.
1424
+     * @return int
1425
+     */
1426
+    public function increase_usage( $by = 1 ) {
1427
+
1428
+        // Abort if zero.
1429
+        if ( empty( $by ) ) {
1430
+            return;
1431
+        }
1432
+
1433
+        // Increase the usage.
1434
+        $this->set_uses( $this->get_uses() + (int) $by );
1435
+
1436
+        // Save the discount.
1437
+        $this->save();
1438
+
1439
+        // Fire relevant hooks.
1440
+        if( (int) $by > 0 ) {
1441
+            do_action( 'wpinv_discount_increase_use_count', $this->get_uses(), $this->get_id(), $this->get_code(),  absint( $by ) );
1442
+        } else {
1443
+            do_action( 'wpinv_discount_decrease_use_count', $this->get_uses(), $this->get_id(), $this->get_code(), absint( $by ) );
1444
+        }
1445
+
1446
+        // Return the number of times the discount has been used.
1447
+        return $this->get_uses();
1448
+    }
1449
+
1450
+    /**
1451
+     * Alias of self::__toString()
1452
+     *
1453
+     * @since 1.0.15
1454
+     * @return string|false
1455
+     */
1456
+    public function get_data_as_json() {
1457
+        return $this->__toString();
1458
+    }
1459
+
1460
+    /**
1461
+     * Returns a discount's discounted amount.
1462
+     *
1463
+     * @since 1.0.15
1464
+     * @param float $amount
1465
+     * @return float
1466
+     */
1467
+    public function get_discounted_amount( $amount ) {
1468
+
1469
+        // Convert amount to float.
1470
+        $amount = (float) $amount;
1471
+
1472
+        // Get discount amount.
1473
+        $discount_amount = $this->get_amount();
1474
+
1475
+        if ( empty( $discount_amount ) ) {
1476
+            return 0;
1477
+        }
1478
+
1479
+        // Format the amount.
1480
+        $discount_amount = floatval( wpinv_sanitize_amount( $discount_amount ) );
1481 1481
 		
1482
-		// If this is a percentage discount.
1483
-		if ( $this->is_type( 'percent' ) ) {
1482
+        // If this is a percentage discount.
1483
+        if ( $this->is_type( 'percent' ) ) {
1484 1484
             $discount_amount = $amount * ( $discount_amount / 100 );
1485
-		}
1485
+        }
1486 1486
 
1487
-		// Discount can not be less than zero...
1488
-		if ( $discount_amount < 0 ) {
1489
-			$discount_amount = 0;
1490
-		}
1487
+        // Discount can not be less than zero...
1488
+        if ( $discount_amount < 0 ) {
1489
+            $discount_amount = 0;
1490
+        }
1491 1491
 
1492
-		// ... or more than the amount.
1493
-		if ( $discount_amount > $amount ) {
1494
-			$discount_amount = $amount;
1495
-		}
1492
+        // ... or more than the amount.
1493
+        if ( $discount_amount > $amount ) {
1494
+            $discount_amount = $amount;
1495
+        }
1496 1496
 
1497
-		return apply_filters( 'wpinv_discount_total_discount_amount', $discount_amount, $amount, $this );
1498
-	}
1497
+        return apply_filters( 'wpinv_discount_total_discount_amount', $discount_amount, $amount, $this );
1498
+    }
1499 1499
 
1500 1500
 }
Please login to merge, or discard this patch.
vendor/ayecode/wp-super-duper/wp-super-duper.php 1 patch
Indentation   +1791 added lines, -1791 removed lines patch added patch discarded remove patch
@@ -1,262 +1,262 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 if ( ! defined( 'ABSPATH' ) ) {
3
-	exit;
3
+    exit;
4 4
 }
5 5
 
6 6
 if ( ! class_exists( 'WP_Super_Duper' ) ) {
7 7
 
8 8
 
9
-	/**
10
-	 * A Class to be able to create a Widget, Shortcode or Block to be able to output content for WordPress.
11
-	 *
12
-	 * Should not be called direct but extended instead.
13
-	 *
14
-	 * Class WP_Super_Duper
15
-	 * @since 1.0.16 change log moved to file change-log.txt - CHANGED
16
-	 * @ver 1.0.19
17
-	 */
18
-	class WP_Super_Duper extends WP_Widget {
19
-
20
-		public $version = "1.0.22";
21
-		public $font_awesome_icon_version = "5.11.2";
22
-		public $block_code;
23
-		public $options;
24
-		public $base_id;
25
-		public $settings_hash;
26
-		public $arguments = array();
27
-		public $instance = array();
28
-		private $class_name;
29
-
30
-		/**
31
-		 * The relative url to the current folder.
32
-		 *
33
-		 * @var string
34
-		 */
35
-		public $url = '';
36
-
37
-		/**
38
-		 * Take the array options and use them to build.
39
-		 */
40
-		public function __construct( $options ) {
41
-			global $sd_widgets;
42
-
43
-			$sd_widgets[ $options['base_id'] ] = array(
44
-				'name'       => $options['name'],
45
-				'class_name' => $options['class_name']
46
-			);
47
-			$this->base_id                     = $options['base_id'];
48
-			// lets filter the options before we do anything
49
-			$options       = apply_filters( "wp_super_duper_options", $options );
50
-			$options       = apply_filters( "wp_super_duper_options_{$this->base_id}", $options );
51
-			$options       = $this->add_name_from_key( $options );
52
-			$this->options = $options;
53
-
54
-			$this->base_id   = $options['base_id'];
55
-			$this->arguments = isset( $options['arguments'] ) ? $options['arguments'] : array();
56
-
57
-			// init parent
58
-			parent::__construct( $options['base_id'], $options['name'], $options['widget_ops'] );
59
-
60
-			if ( isset( $options['class_name'] ) ) {
61
-				// register widget
62
-				$this->class_name = $options['class_name'];
63
-
64
-				// register shortcode
65
-				$this->register_shortcode();
66
-
67
-				// Fusion Builder (avada) support
68
-				if ( function_exists( 'fusion_builder_map' ) ) {
69
-					add_action( 'init', array( $this, 'register_fusion_element' ) );
70
-				}
71
-
72
-				// register block
73
-				add_action( 'admin_enqueue_scripts', array( $this, 'register_block' ) );
74
-			}
75
-
76
-			// add the CSS and JS we need ONCE
77
-			global $sd_widget_scripts;
78
-
79
-			if ( ! $sd_widget_scripts ) {
80
-				wp_add_inline_script( 'admin-widgets', $this->widget_js() );
81
-				wp_add_inline_script( 'customize-controls', $this->widget_js() );
82
-				wp_add_inline_style( 'widgets', $this->widget_css() );
83
-
84
-				// maybe add elementor editor styles
85
-				add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'elementor_editor_styles' ) );
86
-
87
-				$sd_widget_scripts = true;
88
-
89
-				// add shortcode insert button once
90
-				add_action( 'media_buttons', array( $this, 'shortcode_insert_button' ) );
91
-				// generatepress theme sections compatibility
92
-				if ( function_exists( 'generate_sections_sections_metabox' ) ) {
93
-					add_action( 'generate_sections_metabox', array( $this, 'shortcode_insert_button_script' ) );
94
-				}
95
-				if ( $this->is_preview() ) {
96
-					add_action( 'wp_footer', array( $this, 'shortcode_insert_button_script' ) );
97
-					// this makes the insert button work for elementor
98
-					add_action( 'elementor/editor/after_enqueue_scripts', array(
99
-						$this,
100
-						'shortcode_insert_button_script'
101
-					) ); // for elementor
102
-				}
103
-				// this makes the insert button work for cornerstone
104
-				add_action( 'wp_print_footer_scripts', array( __CLASS__, 'maybe_cornerstone_builder' ) );
105
-
106
-				add_action( 'wp_ajax_super_duper_get_widget_settings', array( __CLASS__, 'get_widget_settings' ) );
107
-				add_action( 'wp_ajax_super_duper_get_picker', array( __CLASS__, 'get_picker' ) );
108
-
109
-				// add generator text to admin head
110
-				add_action( 'admin_head', array( $this, 'generator' ) );
111
-			}
112
-
113
-			do_action( 'wp_super_duper_widget_init', $options, $this );
114
-		}
115
-
116
-		/**
117
-		 * Add our widget CSS to elementor editor.
118
-		 */
119
-		public function elementor_editor_styles() {
120
-			wp_add_inline_style( 'elementor-editor', $this->widget_css( false ) );
121
-		}
122
-
123
-		public function register_fusion_element() {
124
-
125
-			$options = $this->options;
126
-
127
-			if ( $this->base_id ) {
128
-
129
-				$params = $this->get_fusion_params();
130
-
131
-				$args = array(
132
-					'name'            => $options['name'],
133
-					'shortcode'       => $this->base_id,
134
-					'icon'            => $options['block-icon'] ? $options['block-icon'] : 'far fa-square',
135
-					'allow_generator' => true,
136
-				);
137
-
138
-				if ( ! empty( $params ) ) {
139
-					$args['params'] = $params;
140
-				}
141
-
142
-				fusion_builder_map( $args );
143
-			}
144
-
145
-		}
146
-
147
-		public function get_fusion_params() {
148
-			$params    = array();
149
-			$arguments = $this->get_arguments();
150
-
151
-			if ( ! empty( $arguments ) ) {
152
-				foreach ( $arguments as $key => $val ) {
153
-					$param = array();
154
-					// type
155
-					$param['type'] = str_replace(
156
-						array(
157
-							"text",
158
-							"number",
159
-							"email",
160
-							"color",
161
-							"checkbox"
162
-						),
163
-						array(
164
-							"textfield",
165
-							"textfield",
166
-							"textfield",
167
-							"colorpicker",
168
-							"select",
169
-
170
-						),
171
-						$val['type'] );
172
-
173
-					// multiselect
174
-					if ( $val['type'] == 'multiselect' || ( ( $param['type'] == 'select' || $val['type'] == 'select' ) && ! empty( $val['multiple'] ) ) ) {
175
-						$param['type']     = 'multiple_select';
176
-						$param['multiple'] = true;
177
-					}
178
-
179
-					// heading
180
-					$param['heading'] = $val['title'];
181
-
182
-					// description
183
-					$param['description'] = isset( $val['desc'] ) ? $val['desc'] : '';
184
-
185
-					// param_name
186
-					$param['param_name'] = $key;
187
-
188
-					// Default
189
-					$param['default'] = isset( $val['default'] ) ? $val['default'] : '';
190
-
191
-					// Group
192
-					if ( isset( $val['group'] ) ) {
193
-						$param['group'] = $val['group'];
194
-					}
195
-
196
-					// value
197
-					if ( $val['type'] == 'checkbox' ) {
198
-						if ( isset( $val['default'] ) && $val['default'] == '0' ) {
199
-							unset( $param['default'] );
200
-						}
201
-						$param['value'] = array( '' => __( "No" ), '1' => __( "Yes" ) );
202
-					} elseif ( $param['type'] == 'select' || $param['type'] == 'multiple_select' ) {
203
-						$param['value'] = isset( $val['options'] ) ? $val['options'] : array();
204
-					} else {
205
-						$param['value'] = isset( $val['default'] ) ? $val['default'] : '';
206
-					}
207
-
208
-					// setup the param
209
-					$params[] = $param;
210
-
211
-				}
212
-			}
213
-
214
-
215
-			return $params;
216
-		}
217
-
218
-		/**
219
-		 * Maybe insert the shortcode inserter button in the footer if we are in the cornerstone builder
220
-		 */
221
-		public static function maybe_cornerstone_builder() {
222
-			if ( did_action( 'cornerstone_before_boot_app' ) ) {
223
-				self::shortcode_insert_button_script();
224
-			}
225
-		}
226
-
227
-		/**
228
-		 * A function to ge the shortcode builder picker html.
229
-		 *
230
-		 * @param string $editor_id
231
-		 *
232
-		 * @return string
233
-		 */
234
-		public static function get_picker( $editor_id = '' ) {
235
-
236
-			ob_start();
237
-			if ( isset( $_POST['editor_id'] ) ) {
238
-				$editor_id = esc_attr( $_POST['editor_id'] );
239
-			} elseif ( isset( $_REQUEST['et_fb'] ) ) {
240
-				$editor_id = 'main_content_content_vb_tiny_mce';
241
-			}
242
-
243
-			global $sd_widgets;
244
-			?>
9
+    /**
10
+     * A Class to be able to create a Widget, Shortcode or Block to be able to output content for WordPress.
11
+     *
12
+     * Should not be called direct but extended instead.
13
+     *
14
+     * Class WP_Super_Duper
15
+     * @since 1.0.16 change log moved to file change-log.txt - CHANGED
16
+     * @ver 1.0.19
17
+     */
18
+    class WP_Super_Duper extends WP_Widget {
19
+
20
+        public $version = "1.0.22";
21
+        public $font_awesome_icon_version = "5.11.2";
22
+        public $block_code;
23
+        public $options;
24
+        public $base_id;
25
+        public $settings_hash;
26
+        public $arguments = array();
27
+        public $instance = array();
28
+        private $class_name;
29
+
30
+        /**
31
+         * The relative url to the current folder.
32
+         *
33
+         * @var string
34
+         */
35
+        public $url = '';
36
+
37
+        /**
38
+         * Take the array options and use them to build.
39
+         */
40
+        public function __construct( $options ) {
41
+            global $sd_widgets;
42
+
43
+            $sd_widgets[ $options['base_id'] ] = array(
44
+                'name'       => $options['name'],
45
+                'class_name' => $options['class_name']
46
+            );
47
+            $this->base_id                     = $options['base_id'];
48
+            // lets filter the options before we do anything
49
+            $options       = apply_filters( "wp_super_duper_options", $options );
50
+            $options       = apply_filters( "wp_super_duper_options_{$this->base_id}", $options );
51
+            $options       = $this->add_name_from_key( $options );
52
+            $this->options = $options;
53
+
54
+            $this->base_id   = $options['base_id'];
55
+            $this->arguments = isset( $options['arguments'] ) ? $options['arguments'] : array();
56
+
57
+            // init parent
58
+            parent::__construct( $options['base_id'], $options['name'], $options['widget_ops'] );
59
+
60
+            if ( isset( $options['class_name'] ) ) {
61
+                // register widget
62
+                $this->class_name = $options['class_name'];
63
+
64
+                // register shortcode
65
+                $this->register_shortcode();
66
+
67
+                // Fusion Builder (avada) support
68
+                if ( function_exists( 'fusion_builder_map' ) ) {
69
+                    add_action( 'init', array( $this, 'register_fusion_element' ) );
70
+                }
71
+
72
+                // register block
73
+                add_action( 'admin_enqueue_scripts', array( $this, 'register_block' ) );
74
+            }
75
+
76
+            // add the CSS and JS we need ONCE
77
+            global $sd_widget_scripts;
78
+
79
+            if ( ! $sd_widget_scripts ) {
80
+                wp_add_inline_script( 'admin-widgets', $this->widget_js() );
81
+                wp_add_inline_script( 'customize-controls', $this->widget_js() );
82
+                wp_add_inline_style( 'widgets', $this->widget_css() );
83
+
84
+                // maybe add elementor editor styles
85
+                add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'elementor_editor_styles' ) );
86
+
87
+                $sd_widget_scripts = true;
88
+
89
+                // add shortcode insert button once
90
+                add_action( 'media_buttons', array( $this, 'shortcode_insert_button' ) );
91
+                // generatepress theme sections compatibility
92
+                if ( function_exists( 'generate_sections_sections_metabox' ) ) {
93
+                    add_action( 'generate_sections_metabox', array( $this, 'shortcode_insert_button_script' ) );
94
+                }
95
+                if ( $this->is_preview() ) {
96
+                    add_action( 'wp_footer', array( $this, 'shortcode_insert_button_script' ) );
97
+                    // this makes the insert button work for elementor
98
+                    add_action( 'elementor/editor/after_enqueue_scripts', array(
99
+                        $this,
100
+                        'shortcode_insert_button_script'
101
+                    ) ); // for elementor
102
+                }
103
+                // this makes the insert button work for cornerstone
104
+                add_action( 'wp_print_footer_scripts', array( __CLASS__, 'maybe_cornerstone_builder' ) );
105
+
106
+                add_action( 'wp_ajax_super_duper_get_widget_settings', array( __CLASS__, 'get_widget_settings' ) );
107
+                add_action( 'wp_ajax_super_duper_get_picker', array( __CLASS__, 'get_picker' ) );
108
+
109
+                // add generator text to admin head
110
+                add_action( 'admin_head', array( $this, 'generator' ) );
111
+            }
112
+
113
+            do_action( 'wp_super_duper_widget_init', $options, $this );
114
+        }
115
+
116
+        /**
117
+         * Add our widget CSS to elementor editor.
118
+         */
119
+        public function elementor_editor_styles() {
120
+            wp_add_inline_style( 'elementor-editor', $this->widget_css( false ) );
121
+        }
122
+
123
+        public function register_fusion_element() {
124
+
125
+            $options = $this->options;
126
+
127
+            if ( $this->base_id ) {
128
+
129
+                $params = $this->get_fusion_params();
130
+
131
+                $args = array(
132
+                    'name'            => $options['name'],
133
+                    'shortcode'       => $this->base_id,
134
+                    'icon'            => $options['block-icon'] ? $options['block-icon'] : 'far fa-square',
135
+                    'allow_generator' => true,
136
+                );
137
+
138
+                if ( ! empty( $params ) ) {
139
+                    $args['params'] = $params;
140
+                }
141
+
142
+                fusion_builder_map( $args );
143
+            }
144
+
145
+        }
146
+
147
+        public function get_fusion_params() {
148
+            $params    = array();
149
+            $arguments = $this->get_arguments();
150
+
151
+            if ( ! empty( $arguments ) ) {
152
+                foreach ( $arguments as $key => $val ) {
153
+                    $param = array();
154
+                    // type
155
+                    $param['type'] = str_replace(
156
+                        array(
157
+                            "text",
158
+                            "number",
159
+                            "email",
160
+                            "color",
161
+                            "checkbox"
162
+                        ),
163
+                        array(
164
+                            "textfield",
165
+                            "textfield",
166
+                            "textfield",
167
+                            "colorpicker",
168
+                            "select",
169
+
170
+                        ),
171
+                        $val['type'] );
172
+
173
+                    // multiselect
174
+                    if ( $val['type'] == 'multiselect' || ( ( $param['type'] == 'select' || $val['type'] == 'select' ) && ! empty( $val['multiple'] ) ) ) {
175
+                        $param['type']     = 'multiple_select';
176
+                        $param['multiple'] = true;
177
+                    }
178
+
179
+                    // heading
180
+                    $param['heading'] = $val['title'];
181
+
182
+                    // description
183
+                    $param['description'] = isset( $val['desc'] ) ? $val['desc'] : '';
184
+
185
+                    // param_name
186
+                    $param['param_name'] = $key;
187
+
188
+                    // Default
189
+                    $param['default'] = isset( $val['default'] ) ? $val['default'] : '';
190
+
191
+                    // Group
192
+                    if ( isset( $val['group'] ) ) {
193
+                        $param['group'] = $val['group'];
194
+                    }
195
+
196
+                    // value
197
+                    if ( $val['type'] == 'checkbox' ) {
198
+                        if ( isset( $val['default'] ) && $val['default'] == '0' ) {
199
+                            unset( $param['default'] );
200
+                        }
201
+                        $param['value'] = array( '' => __( "No" ), '1' => __( "Yes" ) );
202
+                    } elseif ( $param['type'] == 'select' || $param['type'] == 'multiple_select' ) {
203
+                        $param['value'] = isset( $val['options'] ) ? $val['options'] : array();
204
+                    } else {
205
+                        $param['value'] = isset( $val['default'] ) ? $val['default'] : '';
206
+                    }
207
+
208
+                    // setup the param
209
+                    $params[] = $param;
210
+
211
+                }
212
+            }
213
+
214
+
215
+            return $params;
216
+        }
217
+
218
+        /**
219
+         * Maybe insert the shortcode inserter button in the footer if we are in the cornerstone builder
220
+         */
221
+        public static function maybe_cornerstone_builder() {
222
+            if ( did_action( 'cornerstone_before_boot_app' ) ) {
223
+                self::shortcode_insert_button_script();
224
+            }
225
+        }
226
+
227
+        /**
228
+         * A function to ge the shortcode builder picker html.
229
+         *
230
+         * @param string $editor_id
231
+         *
232
+         * @return string
233
+         */
234
+        public static function get_picker( $editor_id = '' ) {
235
+
236
+            ob_start();
237
+            if ( isset( $_POST['editor_id'] ) ) {
238
+                $editor_id = esc_attr( $_POST['editor_id'] );
239
+            } elseif ( isset( $_REQUEST['et_fb'] ) ) {
240
+                $editor_id = 'main_content_content_vb_tiny_mce';
241
+            }
242
+
243
+            global $sd_widgets;
244
+            ?>
245 245
 
246 246
 			<div class="sd-shortcode-left-wrap">
247 247
 				<?php
248
-				ksort( $sd_widgets );
249
-				//				print_r($sd_widgets);exit;
250
-				if ( ! empty( $sd_widgets ) ) {
251
-					echo '<select class="widefat" onchange="sd_get_shortcode_options(this);">';
252
-					echo "<option>" . __( 'Select shortcode' ) . "</option>";
253
-					foreach ( $sd_widgets as $shortcode => $class ) {
254
-						echo "<option value='" . esc_attr( $shortcode ) . "'>" . esc_attr( $shortcode ) . " (" . esc_attr( $class['name'] ) . ")</option>";
255
-					}
256
-					echo "</select>";
257
-
258
-				}
259
-				?>
248
+                ksort( $sd_widgets );
249
+                //				print_r($sd_widgets);exit;
250
+                if ( ! empty( $sd_widgets ) ) {
251
+                    echo '<select class="widefat" onchange="sd_get_shortcode_options(this);">';
252
+                    echo "<option>" . __( 'Select shortcode' ) . "</option>";
253
+                    foreach ( $sd_widgets as $shortcode => $class ) {
254
+                        echo "<option value='" . esc_attr( $shortcode ) . "'>" . esc_attr( $shortcode ) . " (" . esc_attr( $class['name'] ) . ")</option>";
255
+                    }
256
+                    echo "</select>";
257
+
258
+                }
259
+                ?>
260 260
 				<div class="sd-shortcode-settings"></div>
261 261
 
262 262
 			</div>
@@ -267,8 +267,8 @@  discard block
 block discarded – undo
267 267
 					<?php if ( $editor_id != '' ) { ?>
268 268
 						<button class="button sd-insert-shortcode-button"
269 269
 						        onclick="sd_insert_shortcode(<?php if ( ! empty( $editor_id ) ) {
270
-							        echo "'" . $editor_id . "'";
271
-						        } ?>)"><?php _e( 'Insert shortcode' ); ?></button>
270
+                                    echo "'" . $editor_id . "'";
271
+                                } ?>)"><?php _e( 'Insert shortcode' ); ?></button>
272 272
 					<?php } ?>
273 273
 					<button class="button"
274 274
 					        onclick="sd_copy_to_clipboard()"><?php _e( 'Copy shortcode' ); ?></button>
@@ -276,134 +276,134 @@  discard block
 block discarded – undo
276 276
 			</div>
277 277
 			<?php
278 278
 
279
-			$html = ob_get_clean();
280
-
281
-			if ( wp_doing_ajax() ) {
282
-				echo $html;
283
-				$should_die = true;
284
-
285
-				// some builder get the editor via ajax so we should not die on those ocasions
286
-				$dont_die = array(
287
-					'parent_tag',// WP Bakery
288
-					'avia_request' // enfold
289
-				);
290
-
291
-				foreach ( $dont_die as $request ) {
292
-					if ( isset( $_REQUEST[ $request ] ) ) {
293
-						$should_die = false;
294
-					}
295
-				}
296
-
297
-				if ( $should_die ) {
298
-					wp_die();
299
-				}
300
-
301
-			} else {
302
-				return $html;
303
-			}
304
-
305
-			return '';
306
-
307
-		}
308
-
309
-		/**
310
-		 * Output the version in the admin header.
311
-		 */
312
-		public function generator() {
313
-			echo '<meta name="generator" content="WP Super Duper v' . $this->version . '" />';
314
-		}
315
-
316
-		/**
317
-		 * Get widget settings.
318
-		 *
319
-		 * @since 1.0.0
320
-		 */
321
-		public static function get_widget_settings() {
322
-			global $sd_widgets;
323
-
324
-			$shortcode = isset( $_REQUEST['shortcode'] ) && $_REQUEST['shortcode'] ? sanitize_title_with_dashes( $_REQUEST['shortcode'] ) : '';
325
-			if ( ! $shortcode ) {
326
-				wp_die();
327
-			}
328
-			$widget_args = isset( $sd_widgets[ $shortcode ] ) ? $sd_widgets[ $shortcode ] : '';
329
-			if ( ! $widget_args ) {
330
-				wp_die();
331
-			}
332
-			$class_name = isset( $widget_args['class_name'] ) && $widget_args['class_name'] ? $widget_args['class_name'] : '';
333
-			if ( ! $class_name ) {
334
-				wp_die();
335
-			}
336
-
337
-			// invoke an instance method
338
-			$widget = new $class_name;
339
-
340
-			ob_start();
341
-			$widget->form( array() );
342
-			$form = ob_get_clean();
343
-			echo "<form id='$shortcode'>" . $form . "<div class=\"widget-control-save\"></div></form>";
344
-			echo "<style>" . $widget->widget_css() . "</style>";
345
-			echo "<script>" . $widget->widget_js() . "</script>";
346
-			?>
279
+            $html = ob_get_clean();
280
+
281
+            if ( wp_doing_ajax() ) {
282
+                echo $html;
283
+                $should_die = true;
284
+
285
+                // some builder get the editor via ajax so we should not die on those ocasions
286
+                $dont_die = array(
287
+                    'parent_tag',// WP Bakery
288
+                    'avia_request' // enfold
289
+                );
290
+
291
+                foreach ( $dont_die as $request ) {
292
+                    if ( isset( $_REQUEST[ $request ] ) ) {
293
+                        $should_die = false;
294
+                    }
295
+                }
296
+
297
+                if ( $should_die ) {
298
+                    wp_die();
299
+                }
300
+
301
+            } else {
302
+                return $html;
303
+            }
304
+
305
+            return '';
306
+
307
+        }
308
+
309
+        /**
310
+         * Output the version in the admin header.
311
+         */
312
+        public function generator() {
313
+            echo '<meta name="generator" content="WP Super Duper v' . $this->version . '" />';
314
+        }
315
+
316
+        /**
317
+         * Get widget settings.
318
+         *
319
+         * @since 1.0.0
320
+         */
321
+        public static function get_widget_settings() {
322
+            global $sd_widgets;
323
+
324
+            $shortcode = isset( $_REQUEST['shortcode'] ) && $_REQUEST['shortcode'] ? sanitize_title_with_dashes( $_REQUEST['shortcode'] ) : '';
325
+            if ( ! $shortcode ) {
326
+                wp_die();
327
+            }
328
+            $widget_args = isset( $sd_widgets[ $shortcode ] ) ? $sd_widgets[ $shortcode ] : '';
329
+            if ( ! $widget_args ) {
330
+                wp_die();
331
+            }
332
+            $class_name = isset( $widget_args['class_name'] ) && $widget_args['class_name'] ? $widget_args['class_name'] : '';
333
+            if ( ! $class_name ) {
334
+                wp_die();
335
+            }
336
+
337
+            // invoke an instance method
338
+            $widget = new $class_name;
339
+
340
+            ob_start();
341
+            $widget->form( array() );
342
+            $form = ob_get_clean();
343
+            echo "<form id='$shortcode'>" . $form . "<div class=\"widget-control-save\"></div></form>";
344
+            echo "<style>" . $widget->widget_css() . "</style>";
345
+            echo "<script>" . $widget->widget_js() . "</script>";
346
+            ?>
347 347
 			<?php
348
-			wp_die();
349
-		}
350
-
351
-		/**
352
-		 * Insert shortcode builder button to classic editor (not inside Gutenberg, not needed).
353
-		 *
354
-		 * @since 1.0.0
355
-		 *
356
-		 * @param string $editor_id Optional. Shortcode editor id. Default null.
357
-		 * @param string $insert_shortcode_function Optional. Insert shotcode function. Default null.
358
-		 */
359
-		public static function shortcode_insert_button( $editor_id = '', $insert_shortcode_function = '' ) {
360
-			global $sd_widgets, $shortcode_insert_button_once;
361
-			if ( $shortcode_insert_button_once ) {
362
-				return;
363
-			}
364
-			add_thickbox();
365
-
366
-
367
-			/**
368
-			 * Cornerstone makes us play dirty tricks :/
369
-			 * All media_buttons are removed via JS unless they are two specific id's so we wrap our content in this ID so it is not removed.
370
-			 */
371
-			if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) {
372
-				echo '<span id="insert-media-button">';
373
-			}
374
-
375
-			echo self::shortcode_button( 'this', 'true' );
376
-
377
-			// see opening note
378
-			if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) {
379
-				echo '</span>'; // end #insert-media-button
380
-			}
381
-
382
-			// Add separate script for generatepress theme sections
383
-			if ( function_exists( 'generate_sections_sections_metabox' ) && did_action( 'generate_sections_metabox' ) ) {
384
-			} else {
385
-				self::shortcode_insert_button_script( $editor_id, $insert_shortcode_function );
386
-			}
387
-
388
-			$shortcode_insert_button_once = true;
389
-		}
390
-
391
-		/**
392
-		 * Gets the shortcode insert button html.
393
-		 *
394
-		 * @param string $id
395
-		 * @param string $search_for_id
396
-		 *
397
-		 * @return mixed
398
-		 */
399
-		public static function shortcode_button( $id = '', $search_for_id = '' ) {
400
-			ob_start();
401
-			?>
348
+            wp_die();
349
+        }
350
+
351
+        /**
352
+         * Insert shortcode builder button to classic editor (not inside Gutenberg, not needed).
353
+         *
354
+         * @since 1.0.0
355
+         *
356
+         * @param string $editor_id Optional. Shortcode editor id. Default null.
357
+         * @param string $insert_shortcode_function Optional. Insert shotcode function. Default null.
358
+         */
359
+        public static function shortcode_insert_button( $editor_id = '', $insert_shortcode_function = '' ) {
360
+            global $sd_widgets, $shortcode_insert_button_once;
361
+            if ( $shortcode_insert_button_once ) {
362
+                return;
363
+            }
364
+            add_thickbox();
365
+
366
+
367
+            /**
368
+             * Cornerstone makes us play dirty tricks :/
369
+             * All media_buttons are removed via JS unless they are two specific id's so we wrap our content in this ID so it is not removed.
370
+             */
371
+            if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) {
372
+                echo '<span id="insert-media-button">';
373
+            }
374
+
375
+            echo self::shortcode_button( 'this', 'true' );
376
+
377
+            // see opening note
378
+            if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) {
379
+                echo '</span>'; // end #insert-media-button
380
+            }
381
+
382
+            // Add separate script for generatepress theme sections
383
+            if ( function_exists( 'generate_sections_sections_metabox' ) && did_action( 'generate_sections_metabox' ) ) {
384
+            } else {
385
+                self::shortcode_insert_button_script( $editor_id, $insert_shortcode_function );
386
+            }
387
+
388
+            $shortcode_insert_button_once = true;
389
+        }
390
+
391
+        /**
392
+         * Gets the shortcode insert button html.
393
+         *
394
+         * @param string $id
395
+         * @param string $search_for_id
396
+         *
397
+         * @return mixed
398
+         */
399
+        public static function shortcode_button( $id = '', $search_for_id = '' ) {
400
+            ob_start();
401
+            ?>
402 402
 			<span class="sd-lable-shortcode-inserter">
403 403
 				<a onclick="sd_ajax_get_picker(<?php echo $id;
404
-				if ( $search_for_id ) {
405
-					echo "," . $search_for_id;
406
-				} ?>);" href="#TB_inline?width=100%&height=550&inlineId=super-duper-content-ajaxed"
404
+                if ( $search_for_id ) {
405
+                    echo "," . $search_for_id;
406
+                } ?>);" href="#TB_inline?width=100%&height=550&inlineId=super-duper-content-ajaxed"
407 407
 				   class="thickbox button super-duper-content-open" title="Add Shortcode">
408 408
 					<span style="vertical-align: middle;line-height: 18px;font-size: 20px;"
409 409
 					      class="dashicons dashicons-screenoptions"></span>
@@ -414,21 +414,21 @@  discard block
 block discarded – undo
414 414
 			</span>
415 415
 
416 416
 			<?php
417
-			$html = ob_get_clean();
418
-
419
-			// remove line breaks so we can use it in js
420
-			return preg_replace( "/\r|\n/", "", trim( $html ) );
421
-		}
422
-
423
-		/**
424
-		 * Makes SD work with the siteOrigin page builder.
425
-		 *
426
-		 * @since 1.0.6
427
-		 * @return mixed
428
-		 */
429
-		public static function siteorigin_js() {
430
-			ob_start();
431
-			?>
417
+            $html = ob_get_clean();
418
+
419
+            // remove line breaks so we can use it in js
420
+            return preg_replace( "/\r|\n/", "", trim( $html ) );
421
+        }
422
+
423
+        /**
424
+         * Makes SD work with the siteOrigin page builder.
425
+         *
426
+         * @since 1.0.6
427
+         * @return mixed
428
+         */
429
+        public static function siteorigin_js() {
430
+            ob_start();
431
+            ?>
432 432
 			<script>
433 433
 				/**
434 434
 				 * Check a form to see what items shoudl be shown or hidden.
@@ -506,28 +506,28 @@  discard block
 block discarded – undo
506 506
 				});
507 507
 			</script>
508 508
 			<?php
509
-			$output = ob_get_clean();
509
+            $output = ob_get_clean();
510 510
 
511
-			/*
511
+            /*
512 512
 			 * We only add the <script> tags for code highlighting, so we strip them from the output.
513 513
 			 */
514 514
 
515
-			return str_replace( array(
516
-				'<script>',
517
-				'</script>'
518
-			), '', $output );
519
-		}
520
-
521
-		/**
522
-		 * Output the JS and CSS for the shortcode insert button.
523
-		 *
524
-		 * @since 1.0.6
525
-		 *
526
-		 * @param string $editor_id
527
-		 * @param string $insert_shortcode_function
528
-		 */
529
-		public static function shortcode_insert_button_script( $editor_id = '', $insert_shortcode_function = '' ) {
530
-			?>
515
+            return str_replace( array(
516
+                '<script>',
517
+                '</script>'
518
+            ), '', $output );
519
+        }
520
+
521
+        /**
522
+         * Output the JS and CSS for the shortcode insert button.
523
+         *
524
+         * @since 1.0.6
525
+         *
526
+         * @param string $editor_id
527
+         * @param string $insert_shortcode_function
528
+         */
529
+        public static function shortcode_insert_button_script( $editor_id = '', $insert_shortcode_function = '' ) {
530
+            ?>
531 531
 			<style>
532 532
 				.sd-shortcode-left-wrap {
533 533
 					float: left;
@@ -653,35 +653,35 @@  discard block
 block discarded – undo
653 653
 				<?php } ?>
654 654
 			</style>
655 655
 			<?php
656
-			if ( class_exists( 'SiteOrigin_Panels' ) ) {
657
-				echo "<script>" . self::siteorigin_js() . "</script>";
658
-			}
659
-			?>
656
+            if ( class_exists( 'SiteOrigin_Panels' ) ) {
657
+                echo "<script>" . self::siteorigin_js() . "</script>";
658
+            }
659
+            ?>
660 660
 			<script>
661 661
 				<?php
662
-				if(! empty( $insert_shortcode_function )){
663
-					echo $insert_shortcode_function;
664
-				}else{
665
-
666
-				/**
667
-				 * Function for super duper insert shortcode.
668
-				 *
669
-				 * @since 1.0.0
670
-				 */
671
-				?>
662
+                if(! empty( $insert_shortcode_function )){
663
+                    echo $insert_shortcode_function;
664
+                }else{
665
+
666
+                /**
667
+                 * Function for super duper insert shortcode.
668
+                 *
669
+                 * @since 1.0.0
670
+                 */
671
+                ?>
672 672
 				function sd_insert_shortcode($editor_id) {
673 673
 					$shortcode = jQuery('#TB_ajaxContent #sd-shortcode-output').val();
674 674
 					if ($shortcode) {
675 675
 						if (!$editor_id) {
676 676
 							<?php
677
-							if ( isset( $_REQUEST['et_fb'] ) ) {
678
-								echo '$editor_id = "#main_content_content_vb_tiny_mce";';
679
-							} elseif ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) {
680
-								echo '$editor_id = "#elementor-controls .wp-editor-container textarea";';
681
-							} else {
682
-								echo '$editor_id = "#wp-content-editor-container textarea";';
683
-							}
684
-							?>
677
+                            if ( isset( $_REQUEST['et_fb'] ) ) {
678
+                                echo '$editor_id = "#main_content_content_vb_tiny_mce";';
679
+                            } elseif ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) {
680
+                                echo '$editor_id = "#elementor-controls .wp-editor-container textarea";';
681
+                            } else {
682
+                                echo '$editor_id = "#wp-content-editor-container textarea";';
683
+                            }
684
+                            ?>
685 685
 						} else {
686 686
 							$editor_id = '#' + $editor_id;
687 687
 						}
@@ -1007,18 +1007,18 @@  discard block
 block discarded – undo
1007 1007
 
1008 1008
 			</script>
1009 1009
 			<?php
1010
-		}
1011
-
1012
-		/**
1013
-		 * Gets some CSS for the widgets screen.
1014
-		 *
1015
-		 * @param bool $advanced If we should include advanced CSS.
1016
-		 *
1017
-		 * @return mixed
1018
-		 */
1019
-		public function widget_css( $advanced = true ) {
1020
-			ob_start();
1021
-			?>
1010
+        }
1011
+
1012
+        /**
1013
+         * Gets some CSS for the widgets screen.
1014
+         *
1015
+         * @param bool $advanced If we should include advanced CSS.
1016
+         *
1017
+         * @return mixed
1018
+         */
1019
+        public function widget_css( $advanced = true ) {
1020
+            ob_start();
1021
+            ?>
1022 1022
 			<style>
1023 1023
 				<?php if( $advanced ){ ?>
1024 1024
 				.sd-advanced-setting {
@@ -1056,26 +1056,26 @@  discard block
 block discarded – undo
1056 1056
 				}
1057 1057
 			</style>
1058 1058
 			<?php
1059
-			$output = ob_get_clean();
1059
+            $output = ob_get_clean();
1060 1060
 
1061
-			/*
1061
+            /*
1062 1062
 			 * We only add the <script> tags for code highlighting, so we strip them from the output.
1063 1063
 			 */
1064 1064
 
1065
-			return str_replace( array(
1066
-				'<style>',
1067
-				'</style>'
1068
-			), '', $output );
1069
-		}
1070
-
1071
-		/**
1072
-		 * Gets some JS for the widgets screen.
1073
-		 *
1074
-		 * @return mixed
1075
-		 */
1076
-		public function widget_js() {
1077
-			ob_start();
1078
-			?>
1065
+            return str_replace( array(
1066
+                '<style>',
1067
+                '</style>'
1068
+            ), '', $output );
1069
+        }
1070
+
1071
+        /**
1072
+         * Gets some JS for the widgets screen.
1073
+         *
1074
+         * @return mixed
1075
+         */
1076
+        public function widget_js() {
1077
+            ob_start();
1078
+            ?>
1079 1079
 			<script>
1080 1080
 
1081 1081
 				/**
@@ -1230,402 +1230,402 @@  discard block
 block discarded – undo
1230 1230
 				<?php do_action( 'wp_super_duper_widget_js', $this ); ?>
1231 1231
 			</script>
1232 1232
 			<?php
1233
-			$output = ob_get_clean();
1233
+            $output = ob_get_clean();
1234 1234
 
1235
-			/*
1235
+            /*
1236 1236
 			 * We only add the <script> tags for code highlighting, so we strip them from the output.
1237 1237
 			 */
1238 1238
 
1239
-			return str_replace( array(
1240
-				'<script>',
1241
-				'</script>'
1242
-			), '', $output );
1243
-		}
1244
-
1245
-
1246
-		/**
1247
-		 * Set the name from the argument key.
1248
-		 *
1249
-		 * @param $options
1250
-		 *
1251
-		 * @return mixed
1252
-		 */
1253
-		private function add_name_from_key( $options, $arguments = false ) {
1254
-			if ( ! empty( $options['arguments'] ) ) {
1255
-				foreach ( $options['arguments'] as $key => $val ) {
1256
-					$options['arguments'][ $key ]['name'] = $key;
1257
-				}
1258
-			} elseif ( $arguments && is_array( $options ) && ! empty( $options ) ) {
1259
-				foreach ( $options as $key => $val ) {
1260
-					$options[ $key ]['name'] = $key;
1261
-				}
1262
-			}
1263
-
1264
-			return $options;
1265
-		}
1266
-
1267
-		/**
1268
-		 * Register the parent shortcode.
1269
-		 *
1270
-		 * @since 1.0.0
1271
-		 */
1272
-		public function register_shortcode() {
1273
-			add_shortcode( $this->base_id, array( $this, 'shortcode_output' ) );
1274
-			add_action( 'wp_ajax_super_duper_output_shortcode', array( __CLASS__, 'render_shortcode' ) );
1275
-		}
1276
-
1277
-		/**
1278
-		 * Render the shortcode via ajax so we can return it to Gutenberg.
1279
-		 *
1280
-		 * @since 1.0.0
1281
-		 */
1282
-		public static function render_shortcode() {
1283
-
1284
-			check_ajax_referer( 'super_duper_output_shortcode', '_ajax_nonce', true );
1285
-			if ( ! current_user_can( 'manage_options' ) ) {
1286
-				wp_die();
1287
-			}
1288
-
1289
-			// we might need the $post value here so lets set it.
1290
-			if ( isset( $_POST['post_id'] ) && $_POST['post_id'] ) {
1291
-				$post_obj = get_post( absint( $_POST['post_id'] ) );
1292
-				if ( ! empty( $post_obj ) && empty( $post ) ) {
1293
-					global $post;
1294
-					$post = $post_obj;
1295
-				}
1296
-			}
1297
-
1298
-			if ( isset( $_POST['shortcode'] ) && $_POST['shortcode'] ) {
1299
-				$shortcode_name   = sanitize_title_with_dashes( $_POST['shortcode'] );
1300
-				$attributes_array = isset( $_POST['attributes'] ) && $_POST['attributes'] ? $_POST['attributes'] : array();
1301
-				$attributes       = '';
1302
-				if ( ! empty( $attributes_array ) ) {
1303
-					foreach ( $attributes_array as $key => $value ) {
1304
-						$attributes .= " " . sanitize_title_with_dashes( $key ) . "='" . wp_slash( $value ) . "' ";
1305
-					}
1306
-				}
1307
-
1308
-				$shortcode = "[" . $shortcode_name . " " . $attributes . "]";
1309
-
1310
-				echo do_shortcode( $shortcode );
1311
-
1312
-			}
1313
-			wp_die();
1314
-		}
1315
-
1316
-		/**
1317
-		 * Output the shortcode.
1318
-		 *
1319
-		 * @param array $args
1320
-		 * @param string $content
1321
-		 *
1322
-		 * @return string
1323
-		 */
1324
-		public function shortcode_output( $args = array(), $content = '' ) {
1325
-			$args = $this->argument_values( $args );
1326
-
1327
-			// add extra argument so we know its a output to gutenberg
1328
-			//$args
1329
-			$args = $this->string_to_bool( $args );
1330
-
1331
-			// if we have a enclosed shortcode we add it to the special `html` argument
1332
-			if ( ! empty( $content ) ) {
1333
-				$args['html'] = $content;
1334
-			}
1335
-
1336
-			$class = isset( $this->options['widget_ops']['classname'] ) ? esc_attr( $this->options['widget_ops']['classname'] ) : '';
1337
-			$class .= " sdel-".$this->get_instance_hash();
1338
-
1339
-			$class = apply_filters( 'wp_super_duper_div_classname', $class, $args, $this );
1340
-			$class = apply_filters( 'wp_super_duper_div_classname_' . $this->base_id, $class, $args, $this );
1341
-
1342
-			$attrs = apply_filters( 'wp_super_duper_div_attrs', '', $args, $this );
1343
-			$attrs = apply_filters( 'wp_super_duper_div_attrs_' . $this->base_id, '', $args, $this ); //@todo this does not seem right @kiran?
1344
-
1345
-			$shortcode_args = array();
1346
-			$output         = '';
1347
-			$no_wrap        = isset( $this->options['no_wrap'] ) && $this->options['no_wrap'] ? true : false;
1348
-			if ( isset( $args['no_wrap'] ) && $args['no_wrap'] ) {
1349
-				$no_wrap = true;
1350
-			}
1351
-			$main_content = $this->output( $args, $shortcode_args, $content );
1352
-			if ( $main_content && ! $no_wrap ) {
1353
-				// wrap the shortcode in a div with the same class as the widget
1354
-				$output .= '<div class="' . $class . '" ' . $attrs . '>';
1355
-				if ( ! empty( $args['title'] ) ) {
1356
-					// if its a shortcode and there is a title try to grab the title wrappers
1357
-					$shortcode_args = array( 'before_title' => '', 'after_title' => '' );
1358
-					if ( empty( $instance ) ) {
1359
-						global $wp_registered_sidebars;
1360
-						if ( ! empty( $wp_registered_sidebars ) ) {
1361
-							foreach ( $wp_registered_sidebars as $sidebar ) {
1362
-								if ( ! empty( $sidebar['before_title'] ) ) {
1363
-									$shortcode_args['before_title'] = $sidebar['before_title'];
1364
-									$shortcode_args['after_title']  = $sidebar['after_title'];
1365
-									break;
1366
-								}
1367
-							}
1368
-						}
1369
-					}
1370
-					$output .= $this->output_title( $shortcode_args, $args );
1371
-				}
1372
-				$output .= $main_content;
1373
-				$output .= '</div>';
1374
-			} elseif ( $main_content && $no_wrap ) {
1375
-				$output .= $main_content;
1376
-			}
1377
-
1378
-			// if preview show a placeholder if empty
1379
-			if ( $this->is_preview() && $output == '' ) {
1380
-				$output = $this->preview_placeholder_text( "{{" . $this->base_id . "}}" );
1381
-			}
1382
-
1383
-			return apply_filters( 'wp_super_duper_widget_output', $output, $args, $shortcode_args, $this );
1384
-		}
1385
-
1386
-		/**
1387
-		 * Placeholder text to show if output is empty and we are on a preview/builder page.
1388
-		 *
1389
-		 * @param string $name
1390
-		 *
1391
-		 * @return string
1392
-		 */
1393
-		public function preview_placeholder_text( $name = '' ) {
1394
-			return "<div style='background:#0185ba33;padding: 10px;border: 4px #ccc dashed;'>" . sprintf( __( 'Placeholder for: %s' ), $name ) . "</div>";
1395
-		}
1396
-
1397
-		/**
1398
-		 * Sometimes booleans values can be turned to strings, so we fix that.
1399
-		 *
1400
-		 * @param $options
1401
-		 *
1402
-		 * @return mixed
1403
-		 */
1404
-		public function string_to_bool( $options ) {
1405
-			// convert bool strings to booleans
1406
-			foreach ( $options as $key => $val ) {
1407
-				if ( $val == 'false' ) {
1408
-					$options[ $key ] = false;
1409
-				} elseif ( $val == 'true' ) {
1410
-					$options[ $key ] = true;
1411
-				}
1412
-			}
1413
-
1414
-			return $options;
1415
-		}
1416
-
1417
-		/**
1418
-		 * Get the argument values that are also filterable.
1419
-		 *
1420
-		 * @param $instance
1421
-		 *
1422
-		 * @since 1.0.12 Don't set checkbox default value if the value is empty.
1423
-		 *
1424
-		 * @return array
1425
-		 */
1426
-		public function argument_values( $instance ) {
1427
-			$argument_values = array();
1428
-
1429
-			// set widget instance
1430
-			$this->instance = $instance;
1431
-
1432
-			if ( empty( $this->arguments ) ) {
1433
-				$this->arguments = $this->get_arguments();
1434
-			}
1435
-
1436
-			if ( ! empty( $this->arguments ) ) {
1437
-				foreach ( $this->arguments as $key => $args ) {
1438
-					// set the input name from the key
1439
-					$args['name'] = $key;
1440
-					//
1441
-					$argument_values[ $key ] = isset( $instance[ $key ] ) ? $instance[ $key ] : '';
1442
-					if ( $args['type'] == 'checkbox' && $argument_values[ $key ] == '' ) {
1443
-						// don't set default for an empty checkbox
1444
-					} elseif ( $argument_values[ $key ] == '' && isset( $args['default'] ) ) {
1445
-						$argument_values[ $key ] = $args['default'];
1446
-					}
1447
-				}
1448
-			}
1449
-
1450
-			return $argument_values;
1451
-		}
1452
-
1453
-		/**
1454
-		 * Set arguments in super duper.
1455
-		 *
1456
-		 * @since 1.0.0
1457
-		 *
1458
-		 * @return array Set arguments.
1459
-		 */
1460
-		public function set_arguments() {
1461
-			return $this->arguments;
1462
-		}
1463
-
1464
-		/**
1465
-		 * Get arguments in super duper.
1466
-		 *
1467
-		 * @since 1.0.0
1468
-		 *
1469
-		 * @return array Get arguments.
1470
-		 */
1471
-		public function get_arguments() {
1472
-			if ( empty( $this->arguments ) ) {
1473
-				$this->arguments = $this->set_arguments();
1474
-			}
1475
-
1476
-			$this->arguments = apply_filters( 'wp_super_duper_arguments', $this->arguments, $this->options, $this->instance );
1477
-			$this->arguments = $this->add_name_from_key( $this->arguments, true );
1478
-
1479
-			return $this->arguments;
1480
-		}
1481
-
1482
-		/**
1483
-		 * This is the main output class for all 3 items, widget, shortcode and block, it is extended in the calling class.
1484
-		 *
1485
-		 * @param array $args
1486
-		 * @param array $widget_args
1487
-		 * @param string $content
1488
-		 */
1489
-		public function output( $args = array(), $widget_args = array(), $content = '' ) {
1490
-
1491
-		}
1492
-
1493
-		/**
1494
-		 * Add the dynamic block code inline when the wp-block in enqueued.
1495
-		 */
1496
-		public function register_block() {
1497
-			wp_add_inline_script( 'wp-blocks', $this->block() );
1498
-			if ( class_exists( 'SiteOrigin_Panels' ) ) {
1499
-				wp_add_inline_script( 'wp-blocks', $this->siteorigin_js() );
1500
-			}
1501
-		}
1502
-
1503
-		/**
1504
-		 * Check if we need to show advanced options.
1505
-		 *
1506
-		 * @return bool
1507
-		 */
1508
-		public function block_show_advanced() {
1509
-
1510
-			$show      = false;
1511
-			$arguments = $this->arguments;
1512
-
1513
-			if ( empty( $arguments ) ) {
1514
-				$arguments = $this->get_arguments();
1515
-			}
1516
-
1517
-			if ( ! empty( $arguments ) ) {
1518
-				foreach ( $arguments as $argument ) {
1519
-					if ( isset( $argument['advanced'] ) && $argument['advanced'] ) {
1520
-						$show = true;
1521
-						break; // no need to continue if we know we have it
1522
-					}
1523
-				}
1524
-			}
1525
-
1526
-			return $show;
1527
-		}
1528
-
1529
-		/**
1530
-		 * Get the url path to the current folder.
1531
-		 *
1532
-		 * @return string
1533
-		 */
1534
-		public function get_url() {
1535
-
1536
-			$url = $this->url;
1537
-
1538
-			if ( ! $url ) {
1539
-				// check if we are inside a plugin
1540
-				$file_dir = str_replace( "/includes", "", dirname( __FILE__ ) );
1541
-
1542
-				$dir_parts = explode( "/wp-content/", $file_dir );
1543
-				$url_parts = explode( "/wp-content/", plugins_url() );
1544
-
1545
-				if ( ! empty( $url_parts[0] ) && ! empty( $dir_parts[1] ) ) {
1546
-					$url       = trailingslashit( $url_parts[0] . "/wp-content/" . $dir_parts[1] );
1547
-					$this->url = $url;
1548
-				}
1549
-			}
1550
-
1551
-
1552
-			return $url;
1553
-		}
1554
-
1555
-		/**
1556
-		 * Generate the block icon.
1557
-		 *
1558
-		 * Enables the use of Font Awesome icons.
1559
-		 *
1560
-		 * @note xlink:href is actually deprecated but href is not supported by all so we use both.
1561
-		 *
1562
-		 * @param $icon
1563
-		 *
1564
-		 * @since 1.1.0
1565
-		 * @return string
1566
-		 */
1567
-		public function get_block_icon( $icon ) {
1568
-
1569
-			// check if we have a Font Awesome icon
1570
-			$fa_type = '';
1571
-			if ( substr( $icon, 0, 7 ) === "fas fa-" ) {
1572
-				$fa_type = 'solid';
1573
-			} elseif ( substr( $icon, 0, 7 ) === "far fa-" ) {
1574
-				$fa_type = 'regular';
1575
-			} elseif ( substr( $icon, 0, 7 ) === "fab fa-" ) {
1576
-				$fa_type = 'brands';
1577
-			} else {
1578
-				$icon = "'" . $icon . "'";
1579
-			}
1580
-
1581
-			// set the icon if we found one
1582
-			if ( $fa_type ) {
1583
-				$fa_icon = str_replace( array( "fas fa-", "far fa-", "fab fa-" ), "", $icon );
1584
-				$icon    = "el('svg',{width: 20, height: 20, viewBox: '0 0 20 20'},el('use', {'xlink:href': '" . $this->get_url() . "icons/" . $fa_type . ".svg#" . $fa_icon . "','href': '" . $this->get_url() . "icons/" . $fa_type . ".svg#" . $fa_icon . "'}))";
1585
-			}
1586
-
1587
-			return $icon;
1588
-		}
1589
-
1590
-		public function group_arguments( $arguments ) {
1239
+            return str_replace( array(
1240
+                '<script>',
1241
+                '</script>'
1242
+            ), '', $output );
1243
+        }
1244
+
1245
+
1246
+        /**
1247
+         * Set the name from the argument key.
1248
+         *
1249
+         * @param $options
1250
+         *
1251
+         * @return mixed
1252
+         */
1253
+        private function add_name_from_key( $options, $arguments = false ) {
1254
+            if ( ! empty( $options['arguments'] ) ) {
1255
+                foreach ( $options['arguments'] as $key => $val ) {
1256
+                    $options['arguments'][ $key ]['name'] = $key;
1257
+                }
1258
+            } elseif ( $arguments && is_array( $options ) && ! empty( $options ) ) {
1259
+                foreach ( $options as $key => $val ) {
1260
+                    $options[ $key ]['name'] = $key;
1261
+                }
1262
+            }
1263
+
1264
+            return $options;
1265
+        }
1266
+
1267
+        /**
1268
+         * Register the parent shortcode.
1269
+         *
1270
+         * @since 1.0.0
1271
+         */
1272
+        public function register_shortcode() {
1273
+            add_shortcode( $this->base_id, array( $this, 'shortcode_output' ) );
1274
+            add_action( 'wp_ajax_super_duper_output_shortcode', array( __CLASS__, 'render_shortcode' ) );
1275
+        }
1276
+
1277
+        /**
1278
+         * Render the shortcode via ajax so we can return it to Gutenberg.
1279
+         *
1280
+         * @since 1.0.0
1281
+         */
1282
+        public static function render_shortcode() {
1283
+
1284
+            check_ajax_referer( 'super_duper_output_shortcode', '_ajax_nonce', true );
1285
+            if ( ! current_user_can( 'manage_options' ) ) {
1286
+                wp_die();
1287
+            }
1288
+
1289
+            // we might need the $post value here so lets set it.
1290
+            if ( isset( $_POST['post_id'] ) && $_POST['post_id'] ) {
1291
+                $post_obj = get_post( absint( $_POST['post_id'] ) );
1292
+                if ( ! empty( $post_obj ) && empty( $post ) ) {
1293
+                    global $post;
1294
+                    $post = $post_obj;
1295
+                }
1296
+            }
1297
+
1298
+            if ( isset( $_POST['shortcode'] ) && $_POST['shortcode'] ) {
1299
+                $shortcode_name   = sanitize_title_with_dashes( $_POST['shortcode'] );
1300
+                $attributes_array = isset( $_POST['attributes'] ) && $_POST['attributes'] ? $_POST['attributes'] : array();
1301
+                $attributes       = '';
1302
+                if ( ! empty( $attributes_array ) ) {
1303
+                    foreach ( $attributes_array as $key => $value ) {
1304
+                        $attributes .= " " . sanitize_title_with_dashes( $key ) . "='" . wp_slash( $value ) . "' ";
1305
+                    }
1306
+                }
1307
+
1308
+                $shortcode = "[" . $shortcode_name . " " . $attributes . "]";
1309
+
1310
+                echo do_shortcode( $shortcode );
1311
+
1312
+            }
1313
+            wp_die();
1314
+        }
1315
+
1316
+        /**
1317
+         * Output the shortcode.
1318
+         *
1319
+         * @param array $args
1320
+         * @param string $content
1321
+         *
1322
+         * @return string
1323
+         */
1324
+        public function shortcode_output( $args = array(), $content = '' ) {
1325
+            $args = $this->argument_values( $args );
1326
+
1327
+            // add extra argument so we know its a output to gutenberg
1328
+            //$args
1329
+            $args = $this->string_to_bool( $args );
1330
+
1331
+            // if we have a enclosed shortcode we add it to the special `html` argument
1332
+            if ( ! empty( $content ) ) {
1333
+                $args['html'] = $content;
1334
+            }
1335
+
1336
+            $class = isset( $this->options['widget_ops']['classname'] ) ? esc_attr( $this->options['widget_ops']['classname'] ) : '';
1337
+            $class .= " sdel-".$this->get_instance_hash();
1338
+
1339
+            $class = apply_filters( 'wp_super_duper_div_classname', $class, $args, $this );
1340
+            $class = apply_filters( 'wp_super_duper_div_classname_' . $this->base_id, $class, $args, $this );
1341
+
1342
+            $attrs = apply_filters( 'wp_super_duper_div_attrs', '', $args, $this );
1343
+            $attrs = apply_filters( 'wp_super_duper_div_attrs_' . $this->base_id, '', $args, $this ); //@todo this does not seem right @kiran?
1344
+
1345
+            $shortcode_args = array();
1346
+            $output         = '';
1347
+            $no_wrap        = isset( $this->options['no_wrap'] ) && $this->options['no_wrap'] ? true : false;
1348
+            if ( isset( $args['no_wrap'] ) && $args['no_wrap'] ) {
1349
+                $no_wrap = true;
1350
+            }
1351
+            $main_content = $this->output( $args, $shortcode_args, $content );
1352
+            if ( $main_content && ! $no_wrap ) {
1353
+                // wrap the shortcode in a div with the same class as the widget
1354
+                $output .= '<div class="' . $class . '" ' . $attrs . '>';
1355
+                if ( ! empty( $args['title'] ) ) {
1356
+                    // if its a shortcode and there is a title try to grab the title wrappers
1357
+                    $shortcode_args = array( 'before_title' => '', 'after_title' => '' );
1358
+                    if ( empty( $instance ) ) {
1359
+                        global $wp_registered_sidebars;
1360
+                        if ( ! empty( $wp_registered_sidebars ) ) {
1361
+                            foreach ( $wp_registered_sidebars as $sidebar ) {
1362
+                                if ( ! empty( $sidebar['before_title'] ) ) {
1363
+                                    $shortcode_args['before_title'] = $sidebar['before_title'];
1364
+                                    $shortcode_args['after_title']  = $sidebar['after_title'];
1365
+                                    break;
1366
+                                }
1367
+                            }
1368
+                        }
1369
+                    }
1370
+                    $output .= $this->output_title( $shortcode_args, $args );
1371
+                }
1372
+                $output .= $main_content;
1373
+                $output .= '</div>';
1374
+            } elseif ( $main_content && $no_wrap ) {
1375
+                $output .= $main_content;
1376
+            }
1377
+
1378
+            // if preview show a placeholder if empty
1379
+            if ( $this->is_preview() && $output == '' ) {
1380
+                $output = $this->preview_placeholder_text( "{{" . $this->base_id . "}}" );
1381
+            }
1382
+
1383
+            return apply_filters( 'wp_super_duper_widget_output', $output, $args, $shortcode_args, $this );
1384
+        }
1385
+
1386
+        /**
1387
+         * Placeholder text to show if output is empty and we are on a preview/builder page.
1388
+         *
1389
+         * @param string $name
1390
+         *
1391
+         * @return string
1392
+         */
1393
+        public function preview_placeholder_text( $name = '' ) {
1394
+            return "<div style='background:#0185ba33;padding: 10px;border: 4px #ccc dashed;'>" . sprintf( __( 'Placeholder for: %s' ), $name ) . "</div>";
1395
+        }
1396
+
1397
+        /**
1398
+         * Sometimes booleans values can be turned to strings, so we fix that.
1399
+         *
1400
+         * @param $options
1401
+         *
1402
+         * @return mixed
1403
+         */
1404
+        public function string_to_bool( $options ) {
1405
+            // convert bool strings to booleans
1406
+            foreach ( $options as $key => $val ) {
1407
+                if ( $val == 'false' ) {
1408
+                    $options[ $key ] = false;
1409
+                } elseif ( $val == 'true' ) {
1410
+                    $options[ $key ] = true;
1411
+                }
1412
+            }
1413
+
1414
+            return $options;
1415
+        }
1416
+
1417
+        /**
1418
+         * Get the argument values that are also filterable.
1419
+         *
1420
+         * @param $instance
1421
+         *
1422
+         * @since 1.0.12 Don't set checkbox default value if the value is empty.
1423
+         *
1424
+         * @return array
1425
+         */
1426
+        public function argument_values( $instance ) {
1427
+            $argument_values = array();
1428
+
1429
+            // set widget instance
1430
+            $this->instance = $instance;
1431
+
1432
+            if ( empty( $this->arguments ) ) {
1433
+                $this->arguments = $this->get_arguments();
1434
+            }
1435
+
1436
+            if ( ! empty( $this->arguments ) ) {
1437
+                foreach ( $this->arguments as $key => $args ) {
1438
+                    // set the input name from the key
1439
+                    $args['name'] = $key;
1440
+                    //
1441
+                    $argument_values[ $key ] = isset( $instance[ $key ] ) ? $instance[ $key ] : '';
1442
+                    if ( $args['type'] == 'checkbox' && $argument_values[ $key ] == '' ) {
1443
+                        // don't set default for an empty checkbox
1444
+                    } elseif ( $argument_values[ $key ] == '' && isset( $args['default'] ) ) {
1445
+                        $argument_values[ $key ] = $args['default'];
1446
+                    }
1447
+                }
1448
+            }
1449
+
1450
+            return $argument_values;
1451
+        }
1452
+
1453
+        /**
1454
+         * Set arguments in super duper.
1455
+         *
1456
+         * @since 1.0.0
1457
+         *
1458
+         * @return array Set arguments.
1459
+         */
1460
+        public function set_arguments() {
1461
+            return $this->arguments;
1462
+        }
1463
+
1464
+        /**
1465
+         * Get arguments in super duper.
1466
+         *
1467
+         * @since 1.0.0
1468
+         *
1469
+         * @return array Get arguments.
1470
+         */
1471
+        public function get_arguments() {
1472
+            if ( empty( $this->arguments ) ) {
1473
+                $this->arguments = $this->set_arguments();
1474
+            }
1475
+
1476
+            $this->arguments = apply_filters( 'wp_super_duper_arguments', $this->arguments, $this->options, $this->instance );
1477
+            $this->arguments = $this->add_name_from_key( $this->arguments, true );
1478
+
1479
+            return $this->arguments;
1480
+        }
1481
+
1482
+        /**
1483
+         * This is the main output class for all 3 items, widget, shortcode and block, it is extended in the calling class.
1484
+         *
1485
+         * @param array $args
1486
+         * @param array $widget_args
1487
+         * @param string $content
1488
+         */
1489
+        public function output( $args = array(), $widget_args = array(), $content = '' ) {
1490
+
1491
+        }
1492
+
1493
+        /**
1494
+         * Add the dynamic block code inline when the wp-block in enqueued.
1495
+         */
1496
+        public function register_block() {
1497
+            wp_add_inline_script( 'wp-blocks', $this->block() );
1498
+            if ( class_exists( 'SiteOrigin_Panels' ) ) {
1499
+                wp_add_inline_script( 'wp-blocks', $this->siteorigin_js() );
1500
+            }
1501
+        }
1502
+
1503
+        /**
1504
+         * Check if we need to show advanced options.
1505
+         *
1506
+         * @return bool
1507
+         */
1508
+        public function block_show_advanced() {
1509
+
1510
+            $show      = false;
1511
+            $arguments = $this->arguments;
1512
+
1513
+            if ( empty( $arguments ) ) {
1514
+                $arguments = $this->get_arguments();
1515
+            }
1516
+
1517
+            if ( ! empty( $arguments ) ) {
1518
+                foreach ( $arguments as $argument ) {
1519
+                    if ( isset( $argument['advanced'] ) && $argument['advanced'] ) {
1520
+                        $show = true;
1521
+                        break; // no need to continue if we know we have it
1522
+                    }
1523
+                }
1524
+            }
1525
+
1526
+            return $show;
1527
+        }
1528
+
1529
+        /**
1530
+         * Get the url path to the current folder.
1531
+         *
1532
+         * @return string
1533
+         */
1534
+        public function get_url() {
1535
+
1536
+            $url = $this->url;
1537
+
1538
+            if ( ! $url ) {
1539
+                // check if we are inside a plugin
1540
+                $file_dir = str_replace( "/includes", "", dirname( __FILE__ ) );
1541
+
1542
+                $dir_parts = explode( "/wp-content/", $file_dir );
1543
+                $url_parts = explode( "/wp-content/", plugins_url() );
1544
+
1545
+                if ( ! empty( $url_parts[0] ) && ! empty( $dir_parts[1] ) ) {
1546
+                    $url       = trailingslashit( $url_parts[0] . "/wp-content/" . $dir_parts[1] );
1547
+                    $this->url = $url;
1548
+                }
1549
+            }
1550
+
1551
+
1552
+            return $url;
1553
+        }
1554
+
1555
+        /**
1556
+         * Generate the block icon.
1557
+         *
1558
+         * Enables the use of Font Awesome icons.
1559
+         *
1560
+         * @note xlink:href is actually deprecated but href is not supported by all so we use both.
1561
+         *
1562
+         * @param $icon
1563
+         *
1564
+         * @since 1.1.0
1565
+         * @return string
1566
+         */
1567
+        public function get_block_icon( $icon ) {
1568
+
1569
+            // check if we have a Font Awesome icon
1570
+            $fa_type = '';
1571
+            if ( substr( $icon, 0, 7 ) === "fas fa-" ) {
1572
+                $fa_type = 'solid';
1573
+            } elseif ( substr( $icon, 0, 7 ) === "far fa-" ) {
1574
+                $fa_type = 'regular';
1575
+            } elseif ( substr( $icon, 0, 7 ) === "fab fa-" ) {
1576
+                $fa_type = 'brands';
1577
+            } else {
1578
+                $icon = "'" . $icon . "'";
1579
+            }
1580
+
1581
+            // set the icon if we found one
1582
+            if ( $fa_type ) {
1583
+                $fa_icon = str_replace( array( "fas fa-", "far fa-", "fab fa-" ), "", $icon );
1584
+                $icon    = "el('svg',{width: 20, height: 20, viewBox: '0 0 20 20'},el('use', {'xlink:href': '" . $this->get_url() . "icons/" . $fa_type . ".svg#" . $fa_icon . "','href': '" . $this->get_url() . "icons/" . $fa_type . ".svg#" . $fa_icon . "'}))";
1585
+            }
1586
+
1587
+            return $icon;
1588
+        }
1589
+
1590
+        public function group_arguments( $arguments ) {
1591 1591
 //			echo '###';print_r($arguments);
1592
-			if ( ! empty( $arguments ) ) {
1593
-				$temp_arguments = array();
1594
-				$general        = __( "General" );
1595
-				$add_sections   = false;
1596
-				foreach ( $arguments as $key => $args ) {
1597
-					if ( isset( $args['group'] ) ) {
1598
-						$temp_arguments[ $args['group'] ][ $key ] = $args;
1599
-						$add_sections                             = true;
1600
-					} else {
1601
-						$temp_arguments[ $general ][ $key ] = $args;
1602
-					}
1603
-				}
1604
-
1605
-				// only add sections if more than one
1606
-				if ( $add_sections ) {
1607
-					$arguments = $temp_arguments;
1608
-				}
1609
-			}
1592
+            if ( ! empty( $arguments ) ) {
1593
+                $temp_arguments = array();
1594
+                $general        = __( "General" );
1595
+                $add_sections   = false;
1596
+                foreach ( $arguments as $key => $args ) {
1597
+                    if ( isset( $args['group'] ) ) {
1598
+                        $temp_arguments[ $args['group'] ][ $key ] = $args;
1599
+                        $add_sections                             = true;
1600
+                    } else {
1601
+                        $temp_arguments[ $general ][ $key ] = $args;
1602
+                    }
1603
+                }
1604
+
1605
+                // only add sections if more than one
1606
+                if ( $add_sections ) {
1607
+                    $arguments = $temp_arguments;
1608
+                }
1609
+            }
1610 1610
 
1611 1611
 //			echo '###';print_r($arguments);
1612
-			return $arguments;
1613
-		}
1614
-
1615
-
1616
-		/**
1617
-		 * Output the JS for building the dynamic Guntenberg block.
1618
-		 *
1619
-		 * @since 1.0.4 Added block_wrap property which will set the block wrapping output element ie: div, span, p or empty for no wrap.
1620
-		 * @since 1.0.9 Save numbers as numbers and not strings.
1621
-		 * @since 1.1.0 Font Awesome classes can be used for icons.
1622
-		 * @return mixed
1623
-		 */
1624
-		public function block() {
1625
-			ob_start();
1626
-
1627
-			$show_advanced = $this->block_show_advanced();
1628
-			?>
1612
+            return $arguments;
1613
+        }
1614
+
1615
+
1616
+        /**
1617
+         * Output the JS for building the dynamic Guntenberg block.
1618
+         *
1619
+         * @since 1.0.4 Added block_wrap property which will set the block wrapping output element ie: div, span, p or empty for no wrap.
1620
+         * @since 1.0.9 Save numbers as numbers and not strings.
1621
+         * @since 1.1.0 Font Awesome classes can be used for icons.
1622
+         * @return mixed
1623
+         */
1624
+        public function block() {
1625
+            ob_start();
1626
+
1627
+            $show_advanced = $this->block_show_advanced();
1628
+            ?>
1629 1629
 			<script>
1630 1630
 				/**
1631 1631
 				 * BLOCK: Basic
@@ -1669,97 +1669,97 @@  discard block
 block discarded – undo
1669 1669
 						icon: <?php echo $this->get_block_icon( $this->options['block-icon'] );?>,//'<?php echo isset( $this->options['block-icon'] ) ? esc_attr( $this->options['block-icon'] ) : 'shield-alt';?>', // Block icon from Dashicons → https://developer.wordpress.org/resource/dashicons/.
1670 1670
 						supports: {
1671 1671
 							<?php
1672
-							if ( isset( $this->options['block-supports'] ) ) {
1673
-								echo $this->array_to_attributes( $this->options['block-supports'] );
1674
-							}
1675
-							?>
1672
+                            if ( isset( $this->options['block-supports'] ) ) {
1673
+                                echo $this->array_to_attributes( $this->options['block-supports'] );
1674
+                            }
1675
+                            ?>
1676 1676
 						},
1677 1677
 						category: '<?php echo isset( $this->options['block-category'] ) ? esc_attr( $this->options['block-category'] ) : 'common';?>', // Block category — Group blocks together based on common traits E.g. common, formatting, layout widgets, embed.
1678 1678
 						<?php if ( isset( $this->options['block-keywords'] ) ) {
1679
-						echo "keywords : " . $this->options['block-keywords'] . ",";
1680
-					}?>
1679
+                        echo "keywords : " . $this->options['block-keywords'] . ",";
1680
+                    }?>
1681 1681
 
1682 1682
 						<?php
1683 1683
 
1684
-						// maybe set no_wrap
1685
-						$no_wrap = isset( $this->options['no_wrap'] ) && $this->options['no_wrap'] ? true : false;
1686
-						if ( isset( $this->arguments['no_wrap'] ) && $this->arguments['no_wrap'] ) {
1687
-							$no_wrap = true;
1688
-						}
1689
-						if ( $no_wrap ) {
1690
-							$this->options['block-wrap'] = '';
1691
-						}
1684
+                        // maybe set no_wrap
1685
+                        $no_wrap = isset( $this->options['no_wrap'] ) && $this->options['no_wrap'] ? true : false;
1686
+                        if ( isset( $this->arguments['no_wrap'] ) && $this->arguments['no_wrap'] ) {
1687
+                            $no_wrap = true;
1688
+                        }
1689
+                        if ( $no_wrap ) {
1690
+                            $this->options['block-wrap'] = '';
1691
+                        }
1692 1692
 
1693 1693
 
1694 1694
 
1695
-						$show_alignment = false;
1696
-						// align feature
1697
-						/*echo "supports: {";
1695
+                        $show_alignment = false;
1696
+                        // align feature
1697
+                        /*echo "supports: {";
1698 1698
 						echo "	align: true,";
1699 1699
 						echo "  html: false";
1700 1700
 						echo "},";*/
1701 1701
 
1702
-						if ( ! empty( $this->arguments ) ) {
1703
-							echo "attributes : {";
1704
-
1705
-							if ( $show_advanced ) {
1706
-								echo "show_advanced: {";
1707
-								echo "	type: 'boolean',";
1708
-								echo "  default: false,";
1709
-								echo "},";
1710
-							}
1711
-
1712
-							// block wrap element
1713
-							if ( ! empty( $this->options['block-wrap'] ) ) { //@todo we should validate this?
1714
-								echo "block_wrap: {";
1715
-								echo "	type: 'string',";
1716
-								echo "  default: '" . esc_attr( $this->options['block-wrap'] ) . "',";
1717
-								echo "},";
1718
-							}
1719
-
1720
-							foreach ( $this->arguments as $key => $args ) {
1721
-
1722
-								// set if we should show alignment
1723
-								if ( $key == 'alignment' ) {
1724
-									$show_alignment = true;
1725
-								}
1726
-
1727
-								$extra = '';
1728
-
1729
-								if ( $args['type'] == 'checkbox' ) {
1730
-									$type    = 'boolean';
1731
-									$default = isset( $args['default'] ) && $args['default'] ? 'true' : 'false';
1732
-								} elseif ( $args['type'] == 'number' ) {
1733
-									$type    = 'number';
1734
-									$default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''";
1735
-								} elseif ( $args['type'] == 'select' && ! empty( $args['multiple'] ) ) {
1736
-									$type = 'array';
1737
-									if ( is_array( $args['default'] ) ) {
1738
-										$default = isset( $args['default'] ) ? "['" . implode( "','", $args['default'] ) . "']" : "[]";
1739
-									} else {
1740
-										$default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''";
1741
-									}
1742
-								} elseif ( $args['type'] == 'multiselect' ) {
1743
-									$type    = 'array';
1744
-									$default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''";
1745
-								} else {
1746
-									$type    = 'string';
1747
-									$default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''";
1748
-								}
1749
-								echo $key . " : {";
1750
-								echo "type : '$type',";
1751
-								echo "default : $default,";
1752
-								echo "},";
1753
-							}
1754
-
1755
-							echo "content : {type : 'string',default: 'Please select the attributes in the block settings'},";
1756
-							echo "className: { type: 'string', default: '' },";
1757
-
1758
-							echo "},";
1759
-
1760
-						}
1761
-
1762
-						?>
1702
+                        if ( ! empty( $this->arguments ) ) {
1703
+                            echo "attributes : {";
1704
+
1705
+                            if ( $show_advanced ) {
1706
+                                echo "show_advanced: {";
1707
+                                echo "	type: 'boolean',";
1708
+                                echo "  default: false,";
1709
+                                echo "},";
1710
+                            }
1711
+
1712
+                            // block wrap element
1713
+                            if ( ! empty( $this->options['block-wrap'] ) ) { //@todo we should validate this?
1714
+                                echo "block_wrap: {";
1715
+                                echo "	type: 'string',";
1716
+                                echo "  default: '" . esc_attr( $this->options['block-wrap'] ) . "',";
1717
+                                echo "},";
1718
+                            }
1719
+
1720
+                            foreach ( $this->arguments as $key => $args ) {
1721
+
1722
+                                // set if we should show alignment
1723
+                                if ( $key == 'alignment' ) {
1724
+                                    $show_alignment = true;
1725
+                                }
1726
+
1727
+                                $extra = '';
1728
+
1729
+                                if ( $args['type'] == 'checkbox' ) {
1730
+                                    $type    = 'boolean';
1731
+                                    $default = isset( $args['default'] ) && $args['default'] ? 'true' : 'false';
1732
+                                } elseif ( $args['type'] == 'number' ) {
1733
+                                    $type    = 'number';
1734
+                                    $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''";
1735
+                                } elseif ( $args['type'] == 'select' && ! empty( $args['multiple'] ) ) {
1736
+                                    $type = 'array';
1737
+                                    if ( is_array( $args['default'] ) ) {
1738
+                                        $default = isset( $args['default'] ) ? "['" . implode( "','", $args['default'] ) . "']" : "[]";
1739
+                                    } else {
1740
+                                        $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''";
1741
+                                    }
1742
+                                } elseif ( $args['type'] == 'multiselect' ) {
1743
+                                    $type    = 'array';
1744
+                                    $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''";
1745
+                                } else {
1746
+                                    $type    = 'string';
1747
+                                    $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''";
1748
+                                }
1749
+                                echo $key . " : {";
1750
+                                echo "type : '$type',";
1751
+                                echo "default : $default,";
1752
+                                echo "},";
1753
+                            }
1754
+
1755
+                            echo "content : {type : 'string',default: 'Please select the attributes in the block settings'},";
1756
+                            echo "className: { type: 'string', default: '' },";
1757
+
1758
+                            echo "},";
1759
+
1760
+                        }
1761
+
1762
+                        ?>
1763 1763
 
1764 1764
 						// The "edit" property must be a valid function.
1765 1765
 						edit: function (props) {
@@ -1767,9 +1767,9 @@  discard block
 block discarded – undo
1767 1767
 
1768 1768
 							var $value = '';
1769 1769
 							<?php
1770
-							// if we have a post_type and a category then link them
1771
-							if( isset($this->arguments['post_type']) && isset($this->arguments['category']) && !empty($this->arguments['category']['post_type_linked']) ){
1772
-							?>
1770
+                            // if we have a post_type and a category then link them
1771
+                            if( isset($this->arguments['post_type']) && isset($this->arguments['category']) && !empty($this->arguments['category']['post_type_linked']) ){
1772
+                            ?>
1773 1773
 							if(typeof(prev_attributes[props.id]) != 'undefined' ){
1774 1774
 								$pt = props.attributes.post_type;
1775 1775
 								if(post_type_rest_slugs.length){
@@ -1853,8 +1853,8 @@  discard block
 block discarded – undo
1853 1853
 										'shortcode': '<?php echo $this->options['base_id'];?>',
1854 1854
 										'attributes': props.attributes,
1855 1855
 										'post_id': <?php global $post; if ( isset( $post->ID ) ) {
1856
-										echo $post->ID;
1857
-									}?>,
1856
+                                        echo $post->ID;
1857
+                                    }?>,
1858 1858
 										'_ajax_nonce': '<?php echo wp_create_nonce( 'super_duper_output_shortcode' );?>'
1859 1859
 									};
1860 1860
 
@@ -1906,10 +1906,10 @@  discard block
 block discarded – undo
1906 1906
 
1907 1907
 									<?php
1908 1908
 
1909
-									if(! empty( $this->arguments )){
1909
+                                    if(! empty( $this->arguments )){
1910 1910
 
1911
-									if ( $show_advanced ) {
1912
-									?>
1911
+                                    if ( $show_advanced ) {
1912
+                                    ?>
1913 1913
 									el('div', {
1914 1914
 											style: {'padding-left': '16px','padding-right': '16px'}
1915 1915
 										},
@@ -1927,72 +1927,72 @@  discard block
 block discarded – undo
1927 1927
 									,
1928 1928
 									<?php
1929 1929
 
1930
-									}
1930
+                                    }
1931 1931
 
1932
-									$arguments = $this->group_arguments( $this->arguments );
1932
+                                    $arguments = $this->group_arguments( $this->arguments );
1933 1933
 
1934
-									// Do we have sections?
1935
-									$has_sections = $arguments == $this->arguments ? false : true;
1934
+                                    // Do we have sections?
1935
+                                    $has_sections = $arguments == $this->arguments ? false : true;
1936 1936
 
1937 1937
 
1938
-									if($has_sections){
1939
-									$panel_count = 0;
1940
-									foreach($arguments as $key => $args){
1941
-									?>
1938
+                                    if($has_sections){
1939
+                                    $panel_count = 0;
1940
+                                    foreach($arguments as $key => $args){
1941
+                                    ?>
1942 1942
 									el(wp.components.PanelBody, {
1943 1943
 											title: '<?php esc_attr_e( $key ); ?>',
1944 1944
 											initialOpen: <?php if ( $panel_count ) {
1945
-											echo "false";
1946
-										} else {
1947
-											echo "true";
1948
-										}?>
1945
+                                            echo "false";
1946
+                                        } else {
1947
+                                            echo "true";
1948
+                                        }?>
1949 1949
 										},
1950 1950
 										<?php
1951 1951
 
1952
-										foreach ( $args as $k => $a ) {
1953
-											$this->build_block_arguments( $k, $a );
1954
-										}
1955
-										?>
1952
+                                        foreach ( $args as $k => $a ) {
1953
+                                            $this->build_block_arguments( $k, $a );
1954
+                                        }
1955
+                                        ?>
1956 1956
 									),
1957 1957
 									<?php
1958
-									$panel_count ++;
1958
+                                    $panel_count ++;
1959 1959
 
1960
-									}
1961
-									}else {
1962
-									?>
1960
+                                    }
1961
+                                    }else {
1962
+                                    ?>
1963 1963
 									el(wp.components.PanelBody, {
1964 1964
 											title: '<?php esc_attr_e( "Settings" ); ?>',
1965 1965
 											initialOpen: true
1966 1966
 										},
1967 1967
 										<?php
1968
-										foreach ( $this->arguments as $key => $args ) {
1969
-											$this->build_block_arguments( $key, $args );
1970
-										}
1971
-										?>
1968
+                                        foreach ( $this->arguments as $key => $args ) {
1969
+                                            $this->build_block_arguments( $key, $args );
1970
+                                        }
1971
+                                        ?>
1972 1972
 									),
1973 1973
 									<?php
1974
-									}
1974
+                                    }
1975 1975
 
1976
-									}
1977
-									?>
1976
+                                    }
1977
+                                    ?>
1978 1978
 
1979 1979
 								),
1980 1980
 
1981 1981
 								<?php
1982
-								// If the user sets block-output array then build it
1983
-								if ( ! empty( $this->options['block-output'] ) ) {
1984
-								$this->block_element( $this->options['block-output'] );
1985
-							}else{
1986
-								// if no block-output is set then we try and get the shortcode html output via ajax.
1987
-								?>
1982
+                                // If the user sets block-output array then build it
1983
+                                if ( ! empty( $this->options['block-output'] ) ) {
1984
+                                $this->block_element( $this->options['block-output'] );
1985
+                            }else{
1986
+                                // if no block-output is set then we try and get the shortcode html output via ajax.
1987
+                                ?>
1988 1988
 								el('div', {
1989 1989
 									dangerouslySetInnerHTML: {__html: onChangeContent()},
1990 1990
 									className: props.className,
1991 1991
 									style: {'minHeight': '30px'}
1992 1992
 								})
1993 1993
 								<?php
1994
-								}
1995
-								?>
1994
+                                }
1995
+                                ?>
1996 1996
 							]; // end return
1997 1997
 						},
1998 1998
 
@@ -2010,10 +2010,10 @@  discard block
 block discarded – undo
2010 2010
 							$html = '';
2011 2011
 							<?php
2012 2012
 
2013
-							if(! empty( $this->arguments )){
2013
+                            if(! empty( $this->arguments )){
2014 2014
 
2015
-							foreach($this->arguments as $key => $args){
2016
-							?>
2015
+                            foreach($this->arguments as $key => $args){
2016
+                            ?>
2017 2017
 							if (attr.hasOwnProperty("<?php echo esc_attr( $key );?>")) {
2018 2018
 								if ('<?php echo esc_attr( $key );?>' == 'html') {
2019 2019
 									$html = attr.<?php echo esc_attr( $key );?>;
@@ -2022,10 +2022,10 @@  discard block
 block discarded – undo
2022 2022
 								}
2023 2023
 							}
2024 2024
 							<?php
2025
-							}
2026
-							}
2025
+                            }
2026
+                            }
2027 2027
 
2028
-							?>
2028
+                            ?>
2029 2029
 							content += "]";
2030 2030
 
2031 2031
 							// if has html element
@@ -2048,20 +2048,20 @@  discard block
 block discarded – undo
2048 2048
 							}
2049 2049
 
2050 2050
 							<?php
2051
-							if(isset( $this->options['block-wrap'] ) && $this->options['block-wrap'] == ''){
2052
-							?>
2051
+                            if(isset( $this->options['block-wrap'] ) && $this->options['block-wrap'] == ''){
2052
+                            ?>
2053 2053
 							return content;
2054 2054
 							<?php
2055
-							}else{
2056
-							?>
2055
+                            }else{
2056
+                            ?>
2057 2057
 							var block_wrap = 'div';
2058 2058
 							if (attr.hasOwnProperty("block_wrap")) {
2059 2059
 								block_wrap = attr.block_wrap;
2060 2060
 							}
2061 2061
 							return el(block_wrap, {dangerouslySetInnerHTML: {__html: content}, className: align});
2062 2062
 							<?php
2063
-							}
2064
-							?>
2063
+                            }
2064
+                            ?>
2065 2065
 
2066 2066
 
2067 2067
 						}
@@ -2069,48 +2069,48 @@  discard block
 block discarded – undo
2069 2069
 				})();
2070 2070
 			</script>
2071 2071
 			<?php
2072
-			$output = ob_get_clean();
2072
+            $output = ob_get_clean();
2073 2073
 
2074
-			/*
2074
+            /*
2075 2075
 			 * We only add the <script> tags for code highlighting, so we strip them from the output.
2076 2076
 			 */
2077 2077
 
2078
-			return str_replace( array(
2079
-				'<script>',
2080
-				'</script>'
2081
-			), '', $output );
2082
-		}
2083
-
2084
-		public function build_block_arguments( $key, $args ) {
2085
-			$custom_attributes = ! empty( $args['custom_attributes'] ) ? $this->array_to_attributes( $args['custom_attributes'] ) : '';
2086
-			$options           = '';
2087
-			$extra             = '';
2088
-			$require           = '';
2089
-
2090
-			// `content` is a protected and special argument
2091
-			if ( $key == 'content' ) {
2092
-				return;
2093
-			}
2094
-
2095
-			// require advanced
2096
-			$require_advanced = ! empty( $args['advanced'] ) ? "props.attributes.show_advanced && " : "";
2097
-
2098
-			// element require
2099
-			$element_require = ! empty( $args['element_require'] ) ? $this->block_props_replace( $args['element_require'], true ) . " && " : "";
2100
-
2101
-
2102
-			$onchange  = "props.setAttributes({ $key: $key } )";
2103
-			$onchangecomplete  = "";
2104
-			$value     = "props.attributes.$key";
2105
-			$text_type = array( 'text', 'password', 'number', 'email', 'tel', 'url', 'colorx' );
2106
-			if ( in_array( $args['type'], $text_type ) ) {
2107
-				$type = 'TextControl';
2108
-				// Save numbers as numbers and not strings
2109
-				if ( $args['type'] == 'number' ) {
2110
-					$onchange = "props.setAttributes({ $key: Number($key) } )";
2111
-				}
2112
-			}
2113
-			/*
2078
+            return str_replace( array(
2079
+                '<script>',
2080
+                '</script>'
2081
+            ), '', $output );
2082
+        }
2083
+
2084
+        public function build_block_arguments( $key, $args ) {
2085
+            $custom_attributes = ! empty( $args['custom_attributes'] ) ? $this->array_to_attributes( $args['custom_attributes'] ) : '';
2086
+            $options           = '';
2087
+            $extra             = '';
2088
+            $require           = '';
2089
+
2090
+            // `content` is a protected and special argument
2091
+            if ( $key == 'content' ) {
2092
+                return;
2093
+            }
2094
+
2095
+            // require advanced
2096
+            $require_advanced = ! empty( $args['advanced'] ) ? "props.attributes.show_advanced && " : "";
2097
+
2098
+            // element require
2099
+            $element_require = ! empty( $args['element_require'] ) ? $this->block_props_replace( $args['element_require'], true ) . " && " : "";
2100
+
2101
+
2102
+            $onchange  = "props.setAttributes({ $key: $key } )";
2103
+            $onchangecomplete  = "";
2104
+            $value     = "props.attributes.$key";
2105
+            $text_type = array( 'text', 'password', 'number', 'email', 'tel', 'url', 'colorx' );
2106
+            if ( in_array( $args['type'], $text_type ) ) {
2107
+                $type = 'TextControl';
2108
+                // Save numbers as numbers and not strings
2109
+                if ( $args['type'] == 'number' ) {
2110
+                    $onchange = "props.setAttributes({ $key: Number($key) } )";
2111
+                }
2112
+            }
2113
+            /*
2114 2114
 			 * https://www.wptricks.com/question/set-current-tab-on-a-gutenberg-tabpanel-component-from-outside-that-component/ es5 layout
2115 2115
 						elseif($args['type']=='tabs'){
2116 2116
 							?>
@@ -2144,80 +2144,80 @@  discard block
 block discarded – undo
2144 2144
 							return;
2145 2145
 						}
2146 2146
 			*/
2147
-			elseif ( $args['type'] == 'color' ) {
2148
-				$type = 'ColorPicker';
2149
-				$onchange = "";
2150
-				$extra = "color: $value,";
2151
-				if(!empty($args['disable_alpha'])){
2152
-					$extra .= "disableAlpha: true,";
2153
-				}
2154
-				$onchangecomplete = "onChangeComplete: function($key) {
2147
+            elseif ( $args['type'] == 'color' ) {
2148
+                $type = 'ColorPicker';
2149
+                $onchange = "";
2150
+                $extra = "color: $value,";
2151
+                if(!empty($args['disable_alpha'])){
2152
+                    $extra .= "disableAlpha: true,";
2153
+                }
2154
+                $onchangecomplete = "onChangeComplete: function($key) {
2155 2155
 				value =  $key.rgb.a && $key.rgb.a < 1 ? \"rgba(\"+$key.rgb.r+\",\"+$key.rgb.g+\",\"+$key.rgb.b+\",\"+$key.rgb.a+\")\" : $key.hex;
2156 2156
                         props.setAttributes({
2157 2157
                             $key: value
2158 2158
                         });
2159 2159
                     },";
2160
-			}
2161
-			elseif ( $args['type'] == 'checkbox' ) {
2162
-				$type = 'CheckboxControl';
2163
-				$extra .= "checked: props.attributes.$key,";
2164
-				$onchange = "props.setAttributes({ $key: ! props.attributes.$key } )";
2165
-			} elseif ( $args['type'] == 'textarea' ) {
2166
-				$type = 'TextareaControl';
2167
-			} elseif ( $args['type'] == 'select' || $args['type'] == 'multiselect' ) {
2168
-				$type = 'SelectControl';
2169
-
2170
-				if($args['name'] == 'category' && !empty($args['post_type_linked'])){
2171
-					$options .= "options: taxonomies_".str_replace("-","_", $this->id).",";
2172
-				}elseif($args['name'] == 'sort_by' && !empty($args['post_type_linked'])){
2173
-					$options .= "options: sort_by_".str_replace("-","_", $this->id).",";
2174
-				}else {
2175
-
2176
-					if ( ! empty( $args['options'] ) ) {
2177
-						$options .= "options: [";
2178
-						foreach ( $args['options'] as $option_val => $option_label ) {
2179
-							$options .= "{ value: '" . esc_attr( $option_val ) . "', label: '" . addslashes( $option_label ) . "' },";
2180
-						}
2181
-						$options .= "],";
2182
-					}
2183
-				}
2184
-				if ( isset( $args['multiple'] ) && $args['multiple'] ) { //@todo multiselect does not work at the moment: https://github.com/WordPress/gutenberg/issues/5550
2185
-					$extra .= ' multiple: true, ';
2186
-				}
2187
-			} elseif ( $args['type'] == 'alignment' ) {
2188
-				$type = 'AlignmentToolbar'; // @todo this does not seem to work but cant find a example
2189
-			} else {
2190
-				return;// if we have not implemented the control then don't break the JS.
2191
-			}
2192
-
2193
-
2194
-
2195
-			// color input does not show the labels so we add them
2196
-			if($args['type']=='color'){
2197
-				// add show only if advanced
2198
-				echo $require_advanced;
2199
-				// add setting require if defined
2200
-				echo $element_require;
2201
-				echo "el('div', {style: {'marginBottom': '8px'}}, '".addslashes( $args['title'] )."'),";
2202
-			}
2203
-
2204
-			// add show only if advanced
2205
-			echo $require_advanced;
2206
-			// add setting require if defined
2207
-			echo $element_require;
2208
-			?>
2160
+            }
2161
+            elseif ( $args['type'] == 'checkbox' ) {
2162
+                $type = 'CheckboxControl';
2163
+                $extra .= "checked: props.attributes.$key,";
2164
+                $onchange = "props.setAttributes({ $key: ! props.attributes.$key } )";
2165
+            } elseif ( $args['type'] == 'textarea' ) {
2166
+                $type = 'TextareaControl';
2167
+            } elseif ( $args['type'] == 'select' || $args['type'] == 'multiselect' ) {
2168
+                $type = 'SelectControl';
2169
+
2170
+                if($args['name'] == 'category' && !empty($args['post_type_linked'])){
2171
+                    $options .= "options: taxonomies_".str_replace("-","_", $this->id).",";
2172
+                }elseif($args['name'] == 'sort_by' && !empty($args['post_type_linked'])){
2173
+                    $options .= "options: sort_by_".str_replace("-","_", $this->id).",";
2174
+                }else {
2175
+
2176
+                    if ( ! empty( $args['options'] ) ) {
2177
+                        $options .= "options: [";
2178
+                        foreach ( $args['options'] as $option_val => $option_label ) {
2179
+                            $options .= "{ value: '" . esc_attr( $option_val ) . "', label: '" . addslashes( $option_label ) . "' },";
2180
+                        }
2181
+                        $options .= "],";
2182
+                    }
2183
+                }
2184
+                if ( isset( $args['multiple'] ) && $args['multiple'] ) { //@todo multiselect does not work at the moment: https://github.com/WordPress/gutenberg/issues/5550
2185
+                    $extra .= ' multiple: true, ';
2186
+                }
2187
+            } elseif ( $args['type'] == 'alignment' ) {
2188
+                $type = 'AlignmentToolbar'; // @todo this does not seem to work but cant find a example
2189
+            } else {
2190
+                return;// if we have not implemented the control then don't break the JS.
2191
+            }
2192
+
2193
+
2194
+
2195
+            // color input does not show the labels so we add them
2196
+            if($args['type']=='color'){
2197
+                // add show only if advanced
2198
+                echo $require_advanced;
2199
+                // add setting require if defined
2200
+                echo $element_require;
2201
+                echo "el('div', {style: {'marginBottom': '8px'}}, '".addslashes( $args['title'] )."'),";
2202
+            }
2203
+
2204
+            // add show only if advanced
2205
+            echo $require_advanced;
2206
+            // add setting require if defined
2207
+            echo $element_require;
2208
+            ?>
2209 2209
 			el( wp.components.<?php echo $type; ?>, {
2210 2210
 			label: '<?php echo addslashes( $args['title'] ); ?>',
2211 2211
 			help: '<?php if ( isset( $args['desc'] ) ) {
2212
-				echo addslashes( $args['desc'] );
2213
-			} ?>',
2212
+                echo addslashes( $args['desc'] );
2213
+            } ?>',
2214 2214
 			value: <?php echo $value; ?>,
2215 2215
 			<?php if ( $type == 'TextControl' && $args['type'] != 'text' ) {
2216
-				echo "type: '" . addslashes( $args['type'] ) . "',";
2217
-			} ?>
2216
+                echo "type: '" . addslashes( $args['type'] ) . "',";
2217
+            } ?>
2218 2218
 			<?php if ( ! empty( $args['placeholder'] ) ) {
2219
-				echo "placeholder: '" . addslashes( $args['placeholder'] ) . "',";
2220
-			} ?>
2219
+                echo "placeholder: '" . addslashes( $args['placeholder'] ) . "',";
2220
+            } ?>
2221 2221
 			<?php echo $options; ?>
2222 2222
 			<?php echo $extra; ?>
2223 2223
 			<?php echo $custom_attributes; ?>
@@ -2228,534 +2228,534 @@  discard block
 block discarded – undo
2228 2228
 			} ),
2229 2229
 			<?php
2230 2230
 
2231
-		}
2232
-
2233
-		/**
2234
-		 * Convert an array of attributes to block string.
2235
-		 *
2236
-		 * @todo there is prob a faster way to do this, also we could add some validation here.
2237
-		 *
2238
-		 * @param $custom_attributes
2239
-		 *
2240
-		 * @return string
2241
-		 */
2242
-		public function array_to_attributes( $custom_attributes, $html = false ) {
2243
-			$attributes = '';
2244
-			if ( ! empty( $custom_attributes ) ) {
2245
-
2246
-				if ( $html ) {
2247
-					foreach ( $custom_attributes as $key => $val ) {
2248
-						$attributes .= " $key='$val' ";
2249
-					}
2250
-				} else {
2251
-					foreach ( $custom_attributes as $key => $val ) {
2252
-						$attributes .= "'$key': '$val',";
2253
-					}
2254
-				}
2255
-			}
2256
-
2257
-			return $attributes;
2258
-		}
2259
-
2260
-		/**
2261
-		 * A self looping function to create the output for JS block elements.
2262
-		 *
2263
-		 * This is what is output in the WP Editor visual view.
2264
-		 *
2265
-		 * @param $args
2266
-		 */
2267
-		public function block_element( $args ) {
2268
-
2269
-
2270
-			if ( ! empty( $args ) ) {
2271
-				foreach ( $args as $element => $new_args ) {
2272
-
2273
-					if ( is_array( $new_args ) ) { // its an element
2274
-
2275
-
2276
-						if ( isset( $new_args['element'] ) ) {
2277
-
2278
-							if ( isset( $new_args['element_require'] ) ) {
2279
-								echo str_replace( array(
2280
-										"'+",
2281
-										"+'"
2282
-									), '', $this->block_props_replace( $new_args['element_require'] ) ) . " &&  ";
2283
-								unset( $new_args['element_require'] );
2284
-							}
2285
-
2286
-							echo "\n el( '" . $new_args['element'] . "', {";
2287
-
2288
-							// get the attributes
2289
-							foreach ( $new_args as $new_key => $new_value ) {
2290
-
2291
-
2292
-								if ( $new_key == 'element' || $new_key == 'content' || $new_key == 'element_require' || $new_key == 'element_repeat' || is_array( $new_value ) ) {
2293
-									// do nothing
2294
-								} else {
2295
-									echo $this->block_element( array( $new_key => $new_value ) );
2296
-								}
2297
-							}
2298
-
2299
-							echo "},";// end attributes
2300
-
2301
-							// get the content
2302
-							$first_item = 0;
2303
-							foreach ( $new_args as $new_key => $new_value ) {
2304
-								if ( $new_key === 'content' || is_array( $new_value ) ) {
2305
-
2306
-									if ( $new_key === 'content' ) {
2307
-										echo "'" . $this->block_props_replace( wp_slash( $new_value ) ) . "'";
2308
-									}
2309
-
2310
-									if ( is_array( $new_value ) ) {
2311
-
2312
-										if ( isset( $new_value['element_require'] ) ) {
2313
-											echo str_replace( array(
2314
-													"'+",
2315
-													"+'"
2316
-												), '', $this->block_props_replace( $new_value['element_require'] ) ) . " &&  ";
2317
-											unset( $new_value['element_require'] );
2318
-										}
2319
-
2320
-										if ( isset( $new_value['element_repeat'] ) ) {
2321
-											$x = 1;
2322
-											while ( $x <= absint( $new_value['element_repeat'] ) ) {
2323
-												$this->block_element( array( '' => $new_value ) );
2324
-												$x ++;
2325
-											}
2326
-										} else {
2327
-											$this->block_element( array( '' => $new_value ) );
2328
-										}
2329
-									}
2330
-									$first_item ++;
2331
-								}
2332
-							}
2333
-
2334
-							echo ")";// end content
2335
-
2336
-							echo ", \n";
2337
-
2338
-						}
2339
-					} else {
2340
-
2341
-						if ( substr( $element, 0, 3 ) === "if_" ) {
2342
-							echo str_replace( "if_", "", $element ) . ": " . $this->block_props_replace( $new_args, true ) . ",";
2343
-						} elseif ( $element == 'style' ) {
2344
-							echo $element . ": " . $this->block_props_replace( $new_args ) . ",";
2345
-						} else {
2346
-							echo $element . ": '" . $this->block_props_replace( $new_args ) . "',";
2347
-						}
2348
-
2349
-					}
2350
-				}
2351
-			}
2352
-		}
2353
-
2354
-		/**
2355
-		 * Replace block attributes placeholders with the proper naming.
2356
-		 *
2357
-		 * @param $string
2358
-		 *
2359
-		 * @return mixed
2360
-		 */
2361
-		public function block_props_replace( $string, $no_wrap = false ) {
2362
-
2363
-			if ( $no_wrap ) {
2364
-				$string = str_replace( array( "[%", "%]" ), array( "props.attributes.", "" ), $string );
2365
-			} else {
2366
-				$string = str_replace( array( "[%", "%]" ), array( "'+props.attributes.", "+'" ), $string );
2367
-			}
2368
-
2369
-			return $string;
2370
-		}
2371
-
2372
-		/**
2373
-		 * Outputs the content of the widget
2374
-		 *
2375
-		 * @param array $args
2376
-		 * @param array $instance
2377
-		 */
2378
-		public function widget( $args, $instance ) {
2379
-
2380
-			// get the filtered values
2381
-			$argument_values = $this->argument_values( $instance );
2382
-			$argument_values = $this->string_to_bool( $argument_values );
2383
-			$output          = $this->output( $argument_values, $args );
2384
-
2385
-			$no_wrap = false;
2386
-			if ( isset( $argument_values['no_wrap'] ) && $argument_values['no_wrap'] ) {
2387
-				$no_wrap = true;
2388
-			}
2389
-
2390
-			ob_start();
2391
-			if ( $output && ! $no_wrap ) {
2392
-
2393
-				$class_original = $this->options['widget_ops']['classname'];
2394
-				$class = $this->options['widget_ops']['classname']." sdel-".$this->get_instance_hash();
2395
-
2396
-				// Before widget
2397
-				$before_widget = $args['before_widget'];
2398
-				$before_widget = str_replace($class_original,$class,$before_widget);
2399
-				$before_widget = apply_filters( 'wp_super_duper_before_widget', $before_widget, $args, $instance, $this );
2400
-				$before_widget = apply_filters( 'wp_super_duper_before_widget_' . $this->base_id, $before_widget, $args, $instance, $this );
2401
-
2402
-				// After widget
2403
-				$after_widget = $args['after_widget'];
2404
-				$after_widget = apply_filters( 'wp_super_duper_after_widget', $after_widget, $args, $instance, $this );
2405
-				$after_widget = apply_filters( 'wp_super_duper_after_widget_' . $this->base_id, $after_widget, $args, $instance, $this );
2406
-
2407
-				echo $before_widget;
2408
-				// elementor strips the widget wrapping div so we check for and add it back if needed
2409
-				if ( $this->is_elementor_widget_output() ) {
2410
-					echo ! empty( $this->options['widget_ops']['classname'] ) ? "<span class='" . esc_attr( $class  ) . "'>" : '';
2411
-				}
2412
-				echo $this->output_title( $args, $instance );
2413
-				echo $output;
2414
-				if ( $this->is_elementor_widget_output() ) {
2415
-					echo ! empty( $this->options['widget_ops']['classname'] ) ? "</span>" : '';
2416
-				}
2417
-				echo $after_widget;
2418
-			} elseif ( $this->is_preview() && $output == '' ) {// if preview show a placeholder if empty
2419
-				$output = $this->preview_placeholder_text( "{{" . $this->base_id . "}}" );
2420
-				echo $output;
2421
-			} elseif ( $output && $no_wrap ) {
2422
-				echo $output;
2423
-			}
2424
-			$output = ob_get_clean();
2425
-
2426
-			$output = apply_filters( 'wp_super_duper_widget_output', $output, $instance, $args, $this );
2427
-
2428
-			echo $output;
2429
-		}
2430
-
2431
-		/**
2432
-		 * Tests if the current output is inside a elementor container.
2433
-		 *
2434
-		 * @since 1.0.4
2435
-		 * @return bool
2436
-		 */
2437
-		public function is_elementor_widget_output() {
2438
-			$result = false;
2439
-			if ( defined( 'ELEMENTOR_VERSION' ) && isset( $this->number ) && $this->number == 'REPLACE_TO_ID' ) {
2440
-				$result = true;
2441
-			}
2442
-
2443
-			return $result;
2444
-		}
2445
-
2446
-		/**
2447
-		 * Tests if the current output is inside a elementor preview.
2448
-		 *
2449
-		 * @since 1.0.4
2450
-		 * @return bool
2451
-		 */
2452
-		public function is_elementor_preview() {
2453
-			$result = false;
2454
-			if ( isset( $_REQUEST['elementor-preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) || ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor_ajax' ) ) {
2455
-				$result = true;
2456
-			}
2457
-
2458
-			return $result;
2459
-		}
2460
-
2461
-		/**
2462
-		 * Tests if the current output is inside a Divi preview.
2463
-		 *
2464
-		 * @since 1.0.6
2465
-		 * @return bool
2466
-		 */
2467
-		public function is_divi_preview() {
2468
-			$result = false;
2469
-			if ( isset( $_REQUEST['et_fb'] ) || isset( $_REQUEST['et_pb_preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) ) {
2470
-				$result = true;
2471
-			}
2472
-
2473
-			return $result;
2474
-		}
2475
-
2476
-		/**
2477
-		 * Tests if the current output is inside a Beaver builder preview.
2478
-		 *
2479
-		 * @since 1.0.6
2480
-		 * @return bool
2481
-		 */
2482
-		public function is_beaver_preview() {
2483
-			$result = false;
2484
-			if ( isset( $_REQUEST['fl_builder'] ) ) {
2485
-				$result = true;
2486
-			}
2487
-
2488
-			return $result;
2489
-		}
2490
-
2491
-		/**
2492
-		 * Tests if the current output is inside a siteorigin builder preview.
2493
-		 *
2494
-		 * @since 1.0.6
2495
-		 * @return bool
2496
-		 */
2497
-		public function is_siteorigin_preview() {
2498
-			$result = false;
2499
-			if ( ! empty( $_REQUEST['siteorigin_panels_live_editor'] ) ) {
2500
-				$result = true;
2501
-			}
2502
-
2503
-			return $result;
2504
-		}
2505
-
2506
-		/**
2507
-		 * Tests if the current output is inside a cornerstone builder preview.
2508
-		 *
2509
-		 * @since 1.0.8
2510
-		 * @return bool
2511
-		 */
2512
-		public function is_cornerstone_preview() {
2513
-			$result = false;
2514
-			if ( ! empty( $_REQUEST['cornerstone_preview'] ) || basename( $_SERVER['REQUEST_URI'] ) == 'cornerstone-endpoint' ) {
2515
-				$result = true;
2516
-			}
2517
-
2518
-			return $result;
2519
-		}
2520
-
2521
-		/**
2522
-		 * Tests if the current output is inside a fusion builder preview.
2523
-		 *
2524
-		 * @since 1.1.0
2525
-		 * @return bool
2526
-		 */
2527
-		public function is_fusion_preview() {
2528
-			$result = false;
2529
-			if ( ! empty( $_REQUEST['fb-edit'] ) || ! empty( $_REQUEST['fusion_load_nonce'] ) ) {
2530
-				$result = true;
2531
-			}
2532
-
2533
-			return $result;
2534
-		}
2535
-
2536
-		/**
2537
-		 * Tests if the current output is inside a Oxygen builder preview.
2538
-		 *
2539
-		 * @since 1.0.18
2540
-		 * @return bool
2541
-		 */
2542
-		public function is_oxygen_preview() {
2543
-			$result = false;
2544
-			if ( ! empty( $_REQUEST['ct_builder'] ) || ( ! empty( $_REQUEST['action'] ) && ( substr( $_REQUEST['action'], 0, 11 ) === "oxy_render_" || substr( $_REQUEST['action'], 0, 10 ) === "ct_render_" ) ) ) {
2545
-				$result = true;
2546
-			}
2547
-
2548
-			return $result;
2549
-		}
2550
-
2551
-		/**
2552
-		 * General function to check if we are in a preview situation.
2553
-		 *
2554
-		 * @since 1.0.6
2555
-		 * @return bool
2556
-		 */
2557
-		public function is_preview() {
2558
-			$preview = false;
2559
-			if ( $this->is_divi_preview() ) {
2560
-				$preview = true;
2561
-			} elseif ( $this->is_elementor_preview() ) {
2562
-				$preview = true;
2563
-			} elseif ( $this->is_beaver_preview() ) {
2564
-				$preview = true;
2565
-			} elseif ( $this->is_siteorigin_preview() ) {
2566
-				$preview = true;
2567
-			} elseif ( $this->is_cornerstone_preview() ) {
2568
-				$preview = true;
2569
-			} elseif ( $this->is_fusion_preview() ) {
2570
-				$preview = true;
2571
-			} elseif ( $this->is_oxygen_preview() ) {
2572
-				$preview = true;
2573
-			} elseif( $this->is_block_content_call() ) {
2574
-				$preview = true;
2575
-			}
2576
-
2577
-			return $preview;
2578
-		}
2579
-
2580
-		/**
2581
-		 * Output the super title.
2582
-		 *
2583
-		 * @param $args
2584
-		 * @param array $instance
2585
-		 *
2586
-		 * @return string
2587
-		 */
2588
-		public function output_title( $args, $instance = array() ) {
2589
-			$output = '';
2590
-			if ( ! empty( $instance['title'] ) ) {
2591
-				/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
2592
-				$title  = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
2593
-				$output = $args['before_title'] . $title . $args['after_title'];
2594
-			}
2595
-
2596
-			return $output;
2597
-		}
2598
-
2599
-		/**
2600
-		 * Outputs the options form inputs for the widget.
2601
-		 *
2602
-		 * @param array $instance The widget options.
2603
-		 */
2604
-		public function form( $instance ) {
2605
-
2606
-			// set widget instance
2607
-			$this->instance = $instance;
2608
-
2609
-			// set it as a SD widget
2610
-			echo $this->widget_advanced_toggle();
2611
-
2612
-			echo "<p>" . esc_attr( $this->options['widget_ops']['description'] ) . "</p>";
2613
-			$arguments_raw = $this->get_arguments();
2614
-
2615
-			if ( is_array( $arguments_raw ) ) {
2616
-
2617
-				$arguments = $this->group_arguments( $arguments_raw );
2618
-
2619
-				// Do we have sections?
2620
-				$has_sections = $arguments == $arguments_raw ? false : true;
2621
-
2622
-
2623
-				if ( $has_sections ) {
2624
-					$panel_count = 0;
2625
-					foreach ( $arguments as $key => $args ) {
2626
-
2627
-						?>
2231
+        }
2232
+
2233
+        /**
2234
+         * Convert an array of attributes to block string.
2235
+         *
2236
+         * @todo there is prob a faster way to do this, also we could add some validation here.
2237
+         *
2238
+         * @param $custom_attributes
2239
+         *
2240
+         * @return string
2241
+         */
2242
+        public function array_to_attributes( $custom_attributes, $html = false ) {
2243
+            $attributes = '';
2244
+            if ( ! empty( $custom_attributes ) ) {
2245
+
2246
+                if ( $html ) {
2247
+                    foreach ( $custom_attributes as $key => $val ) {
2248
+                        $attributes .= " $key='$val' ";
2249
+                    }
2250
+                } else {
2251
+                    foreach ( $custom_attributes as $key => $val ) {
2252
+                        $attributes .= "'$key': '$val',";
2253
+                    }
2254
+                }
2255
+            }
2256
+
2257
+            return $attributes;
2258
+        }
2259
+
2260
+        /**
2261
+         * A self looping function to create the output for JS block elements.
2262
+         *
2263
+         * This is what is output in the WP Editor visual view.
2264
+         *
2265
+         * @param $args
2266
+         */
2267
+        public function block_element( $args ) {
2268
+
2269
+
2270
+            if ( ! empty( $args ) ) {
2271
+                foreach ( $args as $element => $new_args ) {
2272
+
2273
+                    if ( is_array( $new_args ) ) { // its an element
2274
+
2275
+
2276
+                        if ( isset( $new_args['element'] ) ) {
2277
+
2278
+                            if ( isset( $new_args['element_require'] ) ) {
2279
+                                echo str_replace( array(
2280
+                                        "'+",
2281
+                                        "+'"
2282
+                                    ), '', $this->block_props_replace( $new_args['element_require'] ) ) . " &&  ";
2283
+                                unset( $new_args['element_require'] );
2284
+                            }
2285
+
2286
+                            echo "\n el( '" . $new_args['element'] . "', {";
2287
+
2288
+                            // get the attributes
2289
+                            foreach ( $new_args as $new_key => $new_value ) {
2290
+
2291
+
2292
+                                if ( $new_key == 'element' || $new_key == 'content' || $new_key == 'element_require' || $new_key == 'element_repeat' || is_array( $new_value ) ) {
2293
+                                    // do nothing
2294
+                                } else {
2295
+                                    echo $this->block_element( array( $new_key => $new_value ) );
2296
+                                }
2297
+                            }
2298
+
2299
+                            echo "},";// end attributes
2300
+
2301
+                            // get the content
2302
+                            $first_item = 0;
2303
+                            foreach ( $new_args as $new_key => $new_value ) {
2304
+                                if ( $new_key === 'content' || is_array( $new_value ) ) {
2305
+
2306
+                                    if ( $new_key === 'content' ) {
2307
+                                        echo "'" . $this->block_props_replace( wp_slash( $new_value ) ) . "'";
2308
+                                    }
2309
+
2310
+                                    if ( is_array( $new_value ) ) {
2311
+
2312
+                                        if ( isset( $new_value['element_require'] ) ) {
2313
+                                            echo str_replace( array(
2314
+                                                    "'+",
2315
+                                                    "+'"
2316
+                                                ), '', $this->block_props_replace( $new_value['element_require'] ) ) . " &&  ";
2317
+                                            unset( $new_value['element_require'] );
2318
+                                        }
2319
+
2320
+                                        if ( isset( $new_value['element_repeat'] ) ) {
2321
+                                            $x = 1;
2322
+                                            while ( $x <= absint( $new_value['element_repeat'] ) ) {
2323
+                                                $this->block_element( array( '' => $new_value ) );
2324
+                                                $x ++;
2325
+                                            }
2326
+                                        } else {
2327
+                                            $this->block_element( array( '' => $new_value ) );
2328
+                                        }
2329
+                                    }
2330
+                                    $first_item ++;
2331
+                                }
2332
+                            }
2333
+
2334
+                            echo ")";// end content
2335
+
2336
+                            echo ", \n";
2337
+
2338
+                        }
2339
+                    } else {
2340
+
2341
+                        if ( substr( $element, 0, 3 ) === "if_" ) {
2342
+                            echo str_replace( "if_", "", $element ) . ": " . $this->block_props_replace( $new_args, true ) . ",";
2343
+                        } elseif ( $element == 'style' ) {
2344
+                            echo $element . ": " . $this->block_props_replace( $new_args ) . ",";
2345
+                        } else {
2346
+                            echo $element . ": '" . $this->block_props_replace( $new_args ) . "',";
2347
+                        }
2348
+
2349
+                    }
2350
+                }
2351
+            }
2352
+        }
2353
+
2354
+        /**
2355
+         * Replace block attributes placeholders with the proper naming.
2356
+         *
2357
+         * @param $string
2358
+         *
2359
+         * @return mixed
2360
+         */
2361
+        public function block_props_replace( $string, $no_wrap = false ) {
2362
+
2363
+            if ( $no_wrap ) {
2364
+                $string = str_replace( array( "[%", "%]" ), array( "props.attributes.", "" ), $string );
2365
+            } else {
2366
+                $string = str_replace( array( "[%", "%]" ), array( "'+props.attributes.", "+'" ), $string );
2367
+            }
2368
+
2369
+            return $string;
2370
+        }
2371
+
2372
+        /**
2373
+         * Outputs the content of the widget
2374
+         *
2375
+         * @param array $args
2376
+         * @param array $instance
2377
+         */
2378
+        public function widget( $args, $instance ) {
2379
+
2380
+            // get the filtered values
2381
+            $argument_values = $this->argument_values( $instance );
2382
+            $argument_values = $this->string_to_bool( $argument_values );
2383
+            $output          = $this->output( $argument_values, $args );
2384
+
2385
+            $no_wrap = false;
2386
+            if ( isset( $argument_values['no_wrap'] ) && $argument_values['no_wrap'] ) {
2387
+                $no_wrap = true;
2388
+            }
2389
+
2390
+            ob_start();
2391
+            if ( $output && ! $no_wrap ) {
2392
+
2393
+                $class_original = $this->options['widget_ops']['classname'];
2394
+                $class = $this->options['widget_ops']['classname']." sdel-".$this->get_instance_hash();
2395
+
2396
+                // Before widget
2397
+                $before_widget = $args['before_widget'];
2398
+                $before_widget = str_replace($class_original,$class,$before_widget);
2399
+                $before_widget = apply_filters( 'wp_super_duper_before_widget', $before_widget, $args, $instance, $this );
2400
+                $before_widget = apply_filters( 'wp_super_duper_before_widget_' . $this->base_id, $before_widget, $args, $instance, $this );
2401
+
2402
+                // After widget
2403
+                $after_widget = $args['after_widget'];
2404
+                $after_widget = apply_filters( 'wp_super_duper_after_widget', $after_widget, $args, $instance, $this );
2405
+                $after_widget = apply_filters( 'wp_super_duper_after_widget_' . $this->base_id, $after_widget, $args, $instance, $this );
2406
+
2407
+                echo $before_widget;
2408
+                // elementor strips the widget wrapping div so we check for and add it back if needed
2409
+                if ( $this->is_elementor_widget_output() ) {
2410
+                    echo ! empty( $this->options['widget_ops']['classname'] ) ? "<span class='" . esc_attr( $class  ) . "'>" : '';
2411
+                }
2412
+                echo $this->output_title( $args, $instance );
2413
+                echo $output;
2414
+                if ( $this->is_elementor_widget_output() ) {
2415
+                    echo ! empty( $this->options['widget_ops']['classname'] ) ? "</span>" : '';
2416
+                }
2417
+                echo $after_widget;
2418
+            } elseif ( $this->is_preview() && $output == '' ) {// if preview show a placeholder if empty
2419
+                $output = $this->preview_placeholder_text( "{{" . $this->base_id . "}}" );
2420
+                echo $output;
2421
+            } elseif ( $output && $no_wrap ) {
2422
+                echo $output;
2423
+            }
2424
+            $output = ob_get_clean();
2425
+
2426
+            $output = apply_filters( 'wp_super_duper_widget_output', $output, $instance, $args, $this );
2427
+
2428
+            echo $output;
2429
+        }
2430
+
2431
+        /**
2432
+         * Tests if the current output is inside a elementor container.
2433
+         *
2434
+         * @since 1.0.4
2435
+         * @return bool
2436
+         */
2437
+        public function is_elementor_widget_output() {
2438
+            $result = false;
2439
+            if ( defined( 'ELEMENTOR_VERSION' ) && isset( $this->number ) && $this->number == 'REPLACE_TO_ID' ) {
2440
+                $result = true;
2441
+            }
2442
+
2443
+            return $result;
2444
+        }
2445
+
2446
+        /**
2447
+         * Tests if the current output is inside a elementor preview.
2448
+         *
2449
+         * @since 1.0.4
2450
+         * @return bool
2451
+         */
2452
+        public function is_elementor_preview() {
2453
+            $result = false;
2454
+            if ( isset( $_REQUEST['elementor-preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) || ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor_ajax' ) ) {
2455
+                $result = true;
2456
+            }
2457
+
2458
+            return $result;
2459
+        }
2460
+
2461
+        /**
2462
+         * Tests if the current output is inside a Divi preview.
2463
+         *
2464
+         * @since 1.0.6
2465
+         * @return bool
2466
+         */
2467
+        public function is_divi_preview() {
2468
+            $result = false;
2469
+            if ( isset( $_REQUEST['et_fb'] ) || isset( $_REQUEST['et_pb_preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) ) {
2470
+                $result = true;
2471
+            }
2472
+
2473
+            return $result;
2474
+        }
2475
+
2476
+        /**
2477
+         * Tests if the current output is inside a Beaver builder preview.
2478
+         *
2479
+         * @since 1.0.6
2480
+         * @return bool
2481
+         */
2482
+        public function is_beaver_preview() {
2483
+            $result = false;
2484
+            if ( isset( $_REQUEST['fl_builder'] ) ) {
2485
+                $result = true;
2486
+            }
2487
+
2488
+            return $result;
2489
+        }
2490
+
2491
+        /**
2492
+         * Tests if the current output is inside a siteorigin builder preview.
2493
+         *
2494
+         * @since 1.0.6
2495
+         * @return bool
2496
+         */
2497
+        public function is_siteorigin_preview() {
2498
+            $result = false;
2499
+            if ( ! empty( $_REQUEST['siteorigin_panels_live_editor'] ) ) {
2500
+                $result = true;
2501
+            }
2502
+
2503
+            return $result;
2504
+        }
2505
+
2506
+        /**
2507
+         * Tests if the current output is inside a cornerstone builder preview.
2508
+         *
2509
+         * @since 1.0.8
2510
+         * @return bool
2511
+         */
2512
+        public function is_cornerstone_preview() {
2513
+            $result = false;
2514
+            if ( ! empty( $_REQUEST['cornerstone_preview'] ) || basename( $_SERVER['REQUEST_URI'] ) == 'cornerstone-endpoint' ) {
2515
+                $result = true;
2516
+            }
2517
+
2518
+            return $result;
2519
+        }
2520
+
2521
+        /**
2522
+         * Tests if the current output is inside a fusion builder preview.
2523
+         *
2524
+         * @since 1.1.0
2525
+         * @return bool
2526
+         */
2527
+        public function is_fusion_preview() {
2528
+            $result = false;
2529
+            if ( ! empty( $_REQUEST['fb-edit'] ) || ! empty( $_REQUEST['fusion_load_nonce'] ) ) {
2530
+                $result = true;
2531
+            }
2532
+
2533
+            return $result;
2534
+        }
2535
+
2536
+        /**
2537
+         * Tests if the current output is inside a Oxygen builder preview.
2538
+         *
2539
+         * @since 1.0.18
2540
+         * @return bool
2541
+         */
2542
+        public function is_oxygen_preview() {
2543
+            $result = false;
2544
+            if ( ! empty( $_REQUEST['ct_builder'] ) || ( ! empty( $_REQUEST['action'] ) && ( substr( $_REQUEST['action'], 0, 11 ) === "oxy_render_" || substr( $_REQUEST['action'], 0, 10 ) === "ct_render_" ) ) ) {
2545
+                $result = true;
2546
+            }
2547
+
2548
+            return $result;
2549
+        }
2550
+
2551
+        /**
2552
+         * General function to check if we are in a preview situation.
2553
+         *
2554
+         * @since 1.0.6
2555
+         * @return bool
2556
+         */
2557
+        public function is_preview() {
2558
+            $preview = false;
2559
+            if ( $this->is_divi_preview() ) {
2560
+                $preview = true;
2561
+            } elseif ( $this->is_elementor_preview() ) {
2562
+                $preview = true;
2563
+            } elseif ( $this->is_beaver_preview() ) {
2564
+                $preview = true;
2565
+            } elseif ( $this->is_siteorigin_preview() ) {
2566
+                $preview = true;
2567
+            } elseif ( $this->is_cornerstone_preview() ) {
2568
+                $preview = true;
2569
+            } elseif ( $this->is_fusion_preview() ) {
2570
+                $preview = true;
2571
+            } elseif ( $this->is_oxygen_preview() ) {
2572
+                $preview = true;
2573
+            } elseif( $this->is_block_content_call() ) {
2574
+                $preview = true;
2575
+            }
2576
+
2577
+            return $preview;
2578
+        }
2579
+
2580
+        /**
2581
+         * Output the super title.
2582
+         *
2583
+         * @param $args
2584
+         * @param array $instance
2585
+         *
2586
+         * @return string
2587
+         */
2588
+        public function output_title( $args, $instance = array() ) {
2589
+            $output = '';
2590
+            if ( ! empty( $instance['title'] ) ) {
2591
+                /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
2592
+                $title  = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
2593
+                $output = $args['before_title'] . $title . $args['after_title'];
2594
+            }
2595
+
2596
+            return $output;
2597
+        }
2598
+
2599
+        /**
2600
+         * Outputs the options form inputs for the widget.
2601
+         *
2602
+         * @param array $instance The widget options.
2603
+         */
2604
+        public function form( $instance ) {
2605
+
2606
+            // set widget instance
2607
+            $this->instance = $instance;
2608
+
2609
+            // set it as a SD widget
2610
+            echo $this->widget_advanced_toggle();
2611
+
2612
+            echo "<p>" . esc_attr( $this->options['widget_ops']['description'] ) . "</p>";
2613
+            $arguments_raw = $this->get_arguments();
2614
+
2615
+            if ( is_array( $arguments_raw ) ) {
2616
+
2617
+                $arguments = $this->group_arguments( $arguments_raw );
2618
+
2619
+                // Do we have sections?
2620
+                $has_sections = $arguments == $arguments_raw ? false : true;
2621
+
2622
+
2623
+                if ( $has_sections ) {
2624
+                    $panel_count = 0;
2625
+                    foreach ( $arguments as $key => $args ) {
2626
+
2627
+                        ?>
2628 2628
 						<script>
2629 2629
 							//							jQuery(this).find("i").toggleClass("fas fa-chevron-up fas fa-chevron-down");jQuery(this).next().toggle();
2630 2630
 						</script>
2631 2631
 						<?php
2632 2632
 
2633
-						$hide       = $panel_count ? ' style="display:none;" ' : '';
2634
-						$icon_class = $panel_count ? 'fas fa-chevron-up' : 'fas fa-chevron-down';
2635
-						echo "<button onclick='jQuery(this).find(\"i\").toggleClass(\"fas fa-chevron-up fas fa-chevron-down\");jQuery(this).next().slideToggle();' type='button' class='sd-toggle-group-button sd-input-group-toggle" . sanitize_title_with_dashes( $key ) . "'>" . esc_attr( $key ) . " <i style='float:right;' class='" . $icon_class . "'></i></button>";
2636
-						echo "<div class='sd-toggle-group sd-input-group-" . sanitize_title_with_dashes( $key ) . "' $hide>";
2637
-
2638
-						foreach ( $args as $k => $a ) {
2639
-							$this->widget_inputs( $a, $instance );
2640
-						}
2641
-
2642
-						echo "</div>";
2643
-
2644
-						$panel_count ++;
2645
-
2646
-					}
2647
-				} else {
2648
-					foreach ( $arguments as $key => $args ) {
2649
-						$this->widget_inputs( $args, $instance );
2650
-					}
2651
-				}
2652
-
2653
-			}
2654
-		}
2655
-
2656
-		/**
2657
-		 * Get the hidden input that when added makes the advanced button show on widget settings.
2658
-		 *
2659
-		 * @return string
2660
-		 */
2661
-		public function widget_advanced_toggle() {
2662
-
2663
-			$output = '';
2664
-			if ( $this->block_show_advanced() ) {
2665
-				$val = 1;
2666
-			} else {
2667
-				$val = 0;
2668
-			}
2669
-
2670
-			$output .= "<input type='hidden'  class='sd-show-advanced' value='$val' />";
2671
-
2672
-			return $output;
2673
-		}
2674
-
2675
-		/**
2676
-		 * Convert require element.
2677
-		 *
2678
-		 * @since 1.0.0
2679
-		 *
2680
-		 * @param string $input Input element.
2681
-		 *
2682
-		 * @return string $output
2683
-		 */
2684
-		public function convert_element_require( $input ) {
2685
-
2686
-			$input = str_replace( "'", '"', $input );// we only want double quotes
2687
-
2688
-			$output = esc_attr( str_replace( array( "[%", "%]" ), array(
2689
-				"jQuery(form).find('[data-argument=\"",
2690
-				"\"]').find('input,select,textarea').val()"
2691
-			), $input ) );
2692
-
2693
-			return $output;
2694
-		}
2695
-
2696
-		/**
2697
-		 * Builds the inputs for the widget options.
2698
-		 *
2699
-		 * @param $args
2700
-		 * @param $instance
2701
-		 */
2702
-		public function widget_inputs( $args, $instance ) {
2703
-
2704
-			$class             = "";
2705
-			$element_require   = "";
2706
-			$custom_attributes = "";
2707
-
2708
-			// get value
2709
-			if ( isset( $instance[ $args['name'] ] ) ) {
2710
-				$value = $instance[ $args['name'] ];
2711
-			} elseif ( ! isset( $instance[ $args['name'] ] ) && ! empty( $args['default'] ) ) {
2712
-				$value = is_array( $args['default'] ) ? array_map( "esc_html", $args['default'] ) : esc_html( $args['default'] );
2713
-			} else {
2714
-				$value = '';
2715
-			}
2716
-
2717
-			// get placeholder
2718
-			if ( ! empty( $args['placeholder'] ) ) {
2719
-				$placeholder = "placeholder='" . esc_html( $args['placeholder'] ) . "'";
2720
-			} else {
2721
-				$placeholder = '';
2722
-			}
2723
-
2724
-			// get if advanced
2725
-			if ( isset( $args['advanced'] ) && $args['advanced'] ) {
2726
-				$class .= " sd-advanced-setting ";
2727
-			}
2728
-
2729
-			// element_require
2730
-			if ( isset( $args['element_require'] ) && $args['element_require'] ) {
2731
-				$element_require = $args['element_require'];
2732
-			}
2733
-
2734
-			// custom_attributes
2735
-			if ( isset( $args['custom_attributes'] ) && $args['custom_attributes'] ) {
2736
-				$custom_attributes = $this->array_to_attributes( $args['custom_attributes'], true );
2737
-			}
2738
-
2739
-			// before wrapper
2740
-			?>
2633
+                        $hide       = $panel_count ? ' style="display:none;" ' : '';
2634
+                        $icon_class = $panel_count ? 'fas fa-chevron-up' : 'fas fa-chevron-down';
2635
+                        echo "<button onclick='jQuery(this).find(\"i\").toggleClass(\"fas fa-chevron-up fas fa-chevron-down\");jQuery(this).next().slideToggle();' type='button' class='sd-toggle-group-button sd-input-group-toggle" . sanitize_title_with_dashes( $key ) . "'>" . esc_attr( $key ) . " <i style='float:right;' class='" . $icon_class . "'></i></button>";
2636
+                        echo "<div class='sd-toggle-group sd-input-group-" . sanitize_title_with_dashes( $key ) . "' $hide>";
2637
+
2638
+                        foreach ( $args as $k => $a ) {
2639
+                            $this->widget_inputs( $a, $instance );
2640
+                        }
2641
+
2642
+                        echo "</div>";
2643
+
2644
+                        $panel_count ++;
2645
+
2646
+                    }
2647
+                } else {
2648
+                    foreach ( $arguments as $key => $args ) {
2649
+                        $this->widget_inputs( $args, $instance );
2650
+                    }
2651
+                }
2652
+
2653
+            }
2654
+        }
2655
+
2656
+        /**
2657
+         * Get the hidden input that when added makes the advanced button show on widget settings.
2658
+         *
2659
+         * @return string
2660
+         */
2661
+        public function widget_advanced_toggle() {
2662
+
2663
+            $output = '';
2664
+            if ( $this->block_show_advanced() ) {
2665
+                $val = 1;
2666
+            } else {
2667
+                $val = 0;
2668
+            }
2669
+
2670
+            $output .= "<input type='hidden'  class='sd-show-advanced' value='$val' />";
2671
+
2672
+            return $output;
2673
+        }
2674
+
2675
+        /**
2676
+         * Convert require element.
2677
+         *
2678
+         * @since 1.0.0
2679
+         *
2680
+         * @param string $input Input element.
2681
+         *
2682
+         * @return string $output
2683
+         */
2684
+        public function convert_element_require( $input ) {
2685
+
2686
+            $input = str_replace( "'", '"', $input );// we only want double quotes
2687
+
2688
+            $output = esc_attr( str_replace( array( "[%", "%]" ), array(
2689
+                "jQuery(form).find('[data-argument=\"",
2690
+                "\"]').find('input,select,textarea').val()"
2691
+            ), $input ) );
2692
+
2693
+            return $output;
2694
+        }
2695
+
2696
+        /**
2697
+         * Builds the inputs for the widget options.
2698
+         *
2699
+         * @param $args
2700
+         * @param $instance
2701
+         */
2702
+        public function widget_inputs( $args, $instance ) {
2703
+
2704
+            $class             = "";
2705
+            $element_require   = "";
2706
+            $custom_attributes = "";
2707
+
2708
+            // get value
2709
+            if ( isset( $instance[ $args['name'] ] ) ) {
2710
+                $value = $instance[ $args['name'] ];
2711
+            } elseif ( ! isset( $instance[ $args['name'] ] ) && ! empty( $args['default'] ) ) {
2712
+                $value = is_array( $args['default'] ) ? array_map( "esc_html", $args['default'] ) : esc_html( $args['default'] );
2713
+            } else {
2714
+                $value = '';
2715
+            }
2716
+
2717
+            // get placeholder
2718
+            if ( ! empty( $args['placeholder'] ) ) {
2719
+                $placeholder = "placeholder='" . esc_html( $args['placeholder'] ) . "'";
2720
+            } else {
2721
+                $placeholder = '';
2722
+            }
2723
+
2724
+            // get if advanced
2725
+            if ( isset( $args['advanced'] ) && $args['advanced'] ) {
2726
+                $class .= " sd-advanced-setting ";
2727
+            }
2728
+
2729
+            // element_require
2730
+            if ( isset( $args['element_require'] ) && $args['element_require'] ) {
2731
+                $element_require = $args['element_require'];
2732
+            }
2733
+
2734
+            // custom_attributes
2735
+            if ( isset( $args['custom_attributes'] ) && $args['custom_attributes'] ) {
2736
+                $custom_attributes = $this->array_to_attributes( $args['custom_attributes'], true );
2737
+            }
2738
+
2739
+            // before wrapper
2740
+            ?>
2741 2741
 			<p class="sd-argument <?php echo esc_attr( $class ); ?>"
2742 2742
 			   data-argument='<?php echo esc_attr( $args['name'] ); ?>'
2743 2743
 			   data-element_require='<?php if ( $element_require ) {
2744
-				   echo $this->convert_element_require( $element_require );
2745
-			   } ?>'
2744
+                    echo $this->convert_element_require( $element_require );
2745
+                } ?>'
2746 2746
 			>
2747 2747
 				<?php
2748 2748
 
2749
-				switch ( $args['type'] ) {
2750
-					//array('text','password','number','email','tel','url','color')
2751
-					case "text":
2752
-					case "password":
2753
-					case "number":
2754
-					case "email":
2755
-					case "tel":
2756
-					case "url":
2757
-					case "color":
2758
-						?>
2749
+                switch ( $args['type'] ) {
2750
+                    //array('text','password','number','email','tel','url','color')
2751
+                    case "text":
2752
+                    case "password":
2753
+                    case "number":
2754
+                    case "email":
2755
+                    case "tel":
2756
+                    case "url":
2757
+                    case "color":
2758
+                        ?>
2759 2759
 						<label
2760 2760
 							for="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>"><?php echo esc_attr( $args['title'] ); ?><?php echo $this->widget_field_desc( $args ); ?></label>
2761 2761
 						<input <?php echo $placeholder; ?> class="widefat"
@@ -2766,47 +2766,47 @@  discard block
 block discarded – undo
2766 2766
 							                               value="<?php echo esc_attr( $value ); ?>">
2767 2767
 						<?php
2768 2768
 
2769
-						break;
2770
-					case "select":
2771
-						$multiple = isset( $args['multiple'] ) && $args['multiple'] ? true : false;
2772
-						if ( $multiple ) {
2773
-							if ( empty( $value ) ) {
2774
-								$value = array();
2775
-							}
2776
-						}
2777
-						?>
2769
+                        break;
2770
+                    case "select":
2771
+                        $multiple = isset( $args['multiple'] ) && $args['multiple'] ? true : false;
2772
+                        if ( $multiple ) {
2773
+                            if ( empty( $value ) ) {
2774
+                                $value = array();
2775
+                            }
2776
+                        }
2777
+                        ?>
2778 2778
 						<label
2779 2779
 							for="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>"><?php echo esc_attr( $args['title'] ); ?><?php echo $this->widget_field_desc( $args ); ?></label>
2780 2780
 						<select <?php echo $placeholder; ?> class="widefat"
2781 2781
 							<?php echo $custom_attributes; ?>
2782 2782
 							                                id="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>"
2783 2783
 							                                name="<?php echo esc_attr( $this->get_field_name( $args['name'] ) );
2784
-							                                if ( $multiple ) {
2785
-								                                echo "[]";
2786
-							                                } ?>"
2784
+                                                            if ( $multiple ) {
2785
+                                                                echo "[]";
2786
+                                                            } ?>"
2787 2787
 							<?php if ( $multiple ) {
2788
-								echo "multiple";
2789
-							} //@todo not implemented yet due to gutenberg not supporting it
2790
-							?>
2788
+                                echo "multiple";
2789
+                            } //@todo not implemented yet due to gutenberg not supporting it
2790
+                            ?>
2791 2791
 						>
2792 2792
 							<?php
2793 2793
 
2794
-							if ( ! empty( $args['options'] ) ) {
2795
-								foreach ( $args['options'] as $val => $label ) {
2796
-									if ( $multiple ) {
2797
-										$selected = in_array( $val, $value ) ? 'selected="selected"' : '';
2798
-									} else {
2799
-										$selected = selected( $value, $val, false );
2800
-									}
2801
-									echo "<option value='$val' " . $selected . ">$label</option>";
2802
-								}
2803
-							}
2804
-							?>
2794
+                            if ( ! empty( $args['options'] ) ) {
2795
+                                foreach ( $args['options'] as $val => $label ) {
2796
+                                    if ( $multiple ) {
2797
+                                        $selected = in_array( $val, $value ) ? 'selected="selected"' : '';
2798
+                                    } else {
2799
+                                        $selected = selected( $value, $val, false );
2800
+                                    }
2801
+                                    echo "<option value='$val' " . $selected . ">$label</option>";
2802
+                                }
2803
+                            }
2804
+                            ?>
2805 2805
 						</select>
2806 2806
 						<?php
2807
-						break;
2808
-					case "checkbox":
2809
-						?>
2807
+                        break;
2808
+                    case "checkbox":
2809
+                        ?>
2810 2810
 						<input <?php echo $placeholder; ?>
2811 2811
 							<?php checked( 1, $value, true ) ?>
2812 2812
 							<?php echo $custom_attributes; ?>
@@ -2816,9 +2816,9 @@  discard block
 block discarded – undo
2816 2816
 						<label
2817 2817
 							for="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>"><?php echo esc_attr( $args['title'] ); ?><?php echo $this->widget_field_desc( $args ); ?></label>
2818 2818
 						<?php
2819
-						break;
2820
-					case "textarea":
2821
-						?>
2819
+                        break;
2820
+                    case "textarea":
2821
+                        ?>
2822 2822
 						<label
2823 2823
 							for="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>"><?php echo esc_attr( $args['title'] ); ?><?php echo $this->widget_field_desc( $args ); ?></label>
2824 2824
 						<textarea <?php echo $placeholder; ?> class="widefat"
@@ -2828,173 +2828,173 @@  discard block
 block discarded – undo
2828 2828
 						><?php echo esc_attr( $value ); ?></textarea>
2829 2829
 						<?php
2830 2830
 
2831
-						break;
2832
-					case "hidden":
2833
-						?>
2831
+                        break;
2832
+                    case "hidden":
2833
+                        ?>
2834 2834
 						<input id="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>"
2835 2835
 						       name="<?php echo esc_attr( $this->get_field_name( $args['name'] ) ); ?>" type="hidden"
2836 2836
 						       value="<?php echo esc_attr( $value ); ?>">
2837 2837
 						<?php
2838
-						break;
2839
-					default:
2840
-						echo "No input type found!"; // @todo we need to add more input types.
2841
-				}
2838
+                        break;
2839
+                    default:
2840
+                        echo "No input type found!"; // @todo we need to add more input types.
2841
+                }
2842 2842
 
2843
-				// after wrapper
2844
-				?>
2843
+                // after wrapper
2844
+                ?>
2845 2845
 			</p>
2846 2846
 			<?php
2847 2847
 
2848
-		}
2849
-
2850
-		/**
2851
-		 * Get the widget input description html.
2852
-		 *
2853
-		 * @param $args
2854
-		 *
2855
-		 * @return string
2856
-		 * @todo, need to make its own tooltip script
2857
-		 */
2858
-		public function widget_field_desc( $args ) {
2859
-
2860
-			$description = '';
2861
-			if ( isset( $args['desc'] ) && $args['desc'] ) {
2862
-				if ( isset( $args['desc_tip'] ) && $args['desc_tip'] ) {
2863
-					$description = $this->desc_tip( $args['desc'] );
2864
-				} else {
2865
-					$description = '<span class="description">' . wp_kses_post( $args['desc'] ) . '</span>';
2866
-				}
2867
-			}
2868
-
2869
-			return $description;
2870
-		}
2871
-
2872
-		/**
2873
-		 * Get the tool tip html.
2874
-		 *
2875
-		 * @param $tip
2876
-		 * @param bool $allow_html
2877
-		 *
2878
-		 * @return string
2879
-		 */
2880
-		function desc_tip( $tip, $allow_html = false ) {
2881
-			if ( $allow_html ) {
2882
-				$tip = $this->sanitize_tooltip( $tip );
2883
-			} else {
2884
-				$tip = esc_attr( $tip );
2885
-			}
2886
-
2887
-			return '<span class="gd-help-tip dashicons dashicons-editor-help" title="' . $tip . '"></span>';
2888
-		}
2889
-
2890
-		/**
2891
-		 * Sanitize a string destined to be a tooltip.
2892
-		 *
2893
-		 * @param string $var
2894
-		 *
2895
-		 * @return string
2896
-		 */
2897
-		public function sanitize_tooltip( $var ) {
2898
-			return htmlspecialchars( wp_kses( html_entity_decode( $var ), array(
2899
-				'br'     => array(),
2900
-				'em'     => array(),
2901
-				'strong' => array(),
2902
-				'small'  => array(),
2903
-				'span'   => array(),
2904
-				'ul'     => array(),
2905
-				'li'     => array(),
2906
-				'ol'     => array(),
2907
-				'p'      => array(),
2908
-			) ) );
2909
-		}
2910
-
2911
-		/**
2912
-		 * Processing widget options on save
2913
-		 *
2914
-		 * @param array $new_instance The new options
2915
-		 * @param array $old_instance The previous options
2916
-		 *
2917
-		 * @return array
2918
-		 * @todo we should add some sanitation here.
2919
-		 */
2920
-		public function update( $new_instance, $old_instance ) {
2921
-
2922
-			//save the widget
2923
-			$instance = array_merge( (array) $old_instance, (array) $new_instance );
2924
-
2925
-			// set widget instance
2926
-			$this->instance = $instance;
2927
-
2928
-			if ( empty( $this->arguments ) ) {
2929
-				$this->get_arguments();
2930
-			}
2931
-
2932
-			// check for checkboxes
2933
-			if ( ! empty( $this->arguments ) ) {
2934
-				foreach ( $this->arguments as $argument ) {
2935
-					if ( isset( $argument['type'] ) && $argument['type'] == 'checkbox' && ! isset( $new_instance[ $argument['name'] ] ) ) {
2936
-						$instance[ $argument['name'] ] = '0';
2937
-					}
2938
-				}
2939
-			}
2940
-
2941
-			return $instance;
2942
-		}
2943
-
2944
-		/**
2945
-		 * Checks if the current call is a ajax call to get the block content.
2946
-		 *
2947
-		 * This can be used in your widget to return different content as the block content.
2948
-		 *
2949
-		 * @since 1.0.3
2950
-		 * @return bool
2951
-		 */
2952
-		public function is_block_content_call() {
2953
-			$result = false;
2954
-			if ( wp_doing_ajax() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'super_duper_output_shortcode' ) {
2955
-				$result = true;
2956
-			}
2957
-
2958
-			return $result;
2959
-		}
2960
-
2961
-		/**
2962
-		 * Get an instance hash that will be unique to the type and settings.
2963
-		 *
2964
-		 * @since 1.0.20
2965
-		 * @return string
2966
-		 */
2967
-		public function get_instance_hash(){
2968
-			$instance_string = $this->base_id.serialize($this->instance);
2969
-			return hash('crc32b',$instance_string);
2970
-		}
2971
-
2972
-		/**
2973
-		 * Generate and return inline styles from CSS rules that will match the unique class of the instance.
2974
-		 *
2975
-		 * @param array $rules
2976
-		 *
2977
-		 * @since 1.0.20
2978
-		 * @return string
2979
-		 */
2980
-		public function get_instance_style($rules = array()){
2981
-			$css = '';
2982
-
2983
-			if(!empty($rules)){
2984
-				$rules = array_unique($rules);
2985
-				$instance_hash = $this->get_instance_hash();
2986
-				$css .= "<style>";
2987
-				foreach($rules as $rule){
2988
-					$css .= ".sdel-$instance_hash $rule";
2989
-				}
2990
-				$css .= "</style>";
2991
-			}
2992
-
2993
-
2994
-			return $css;
2995
-
2996
-		}
2997
-
2998
-	}
2848
+        }
2849
+
2850
+        /**
2851
+         * Get the widget input description html.
2852
+         *
2853
+         * @param $args
2854
+         *
2855
+         * @return string
2856
+         * @todo, need to make its own tooltip script
2857
+         */
2858
+        public function widget_field_desc( $args ) {
2859
+
2860
+            $description = '';
2861
+            if ( isset( $args['desc'] ) && $args['desc'] ) {
2862
+                if ( isset( $args['desc_tip'] ) && $args['desc_tip'] ) {
2863
+                    $description = $this->desc_tip( $args['desc'] );
2864
+                } else {
2865
+                    $description = '<span class="description">' . wp_kses_post( $args['desc'] ) . '</span>';
2866
+                }
2867
+            }
2868
+
2869
+            return $description;
2870
+        }
2871
+
2872
+        /**
2873
+         * Get the tool tip html.
2874
+         *
2875
+         * @param $tip
2876
+         * @param bool $allow_html
2877
+         *
2878
+         * @return string
2879
+         */
2880
+        function desc_tip( $tip, $allow_html = false ) {
2881
+            if ( $allow_html ) {
2882
+                $tip = $this->sanitize_tooltip( $tip );
2883
+            } else {
2884
+                $tip = esc_attr( $tip );
2885
+            }
2886
+
2887
+            return '<span class="gd-help-tip dashicons dashicons-editor-help" title="' . $tip . '"></span>';
2888
+        }
2889
+
2890
+        /**
2891
+         * Sanitize a string destined to be a tooltip.
2892
+         *
2893
+         * @param string $var
2894
+         *
2895
+         * @return string
2896
+         */
2897
+        public function sanitize_tooltip( $var ) {
2898
+            return htmlspecialchars( wp_kses( html_entity_decode( $var ), array(
2899
+                'br'     => array(),
2900
+                'em'     => array(),
2901
+                'strong' => array(),
2902
+                'small'  => array(),
2903
+                'span'   => array(),
2904
+                'ul'     => array(),
2905
+                'li'     => array(),
2906
+                'ol'     => array(),
2907
+                'p'      => array(),
2908
+            ) ) );
2909
+        }
2910
+
2911
+        /**
2912
+         * Processing widget options on save
2913
+         *
2914
+         * @param array $new_instance The new options
2915
+         * @param array $old_instance The previous options
2916
+         *
2917
+         * @return array
2918
+         * @todo we should add some sanitation here.
2919
+         */
2920
+        public function update( $new_instance, $old_instance ) {
2921
+
2922
+            //save the widget
2923
+            $instance = array_merge( (array) $old_instance, (array) $new_instance );
2924
+
2925
+            // set widget instance
2926
+            $this->instance = $instance;
2927
+
2928
+            if ( empty( $this->arguments ) ) {
2929
+                $this->get_arguments();
2930
+            }
2931
+
2932
+            // check for checkboxes
2933
+            if ( ! empty( $this->arguments ) ) {
2934
+                foreach ( $this->arguments as $argument ) {
2935
+                    if ( isset( $argument['type'] ) && $argument['type'] == 'checkbox' && ! isset( $new_instance[ $argument['name'] ] ) ) {
2936
+                        $instance[ $argument['name'] ] = '0';
2937
+                    }
2938
+                }
2939
+            }
2940
+
2941
+            return $instance;
2942
+        }
2943
+
2944
+        /**
2945
+         * Checks if the current call is a ajax call to get the block content.
2946
+         *
2947
+         * This can be used in your widget to return different content as the block content.
2948
+         *
2949
+         * @since 1.0.3
2950
+         * @return bool
2951
+         */
2952
+        public function is_block_content_call() {
2953
+            $result = false;
2954
+            if ( wp_doing_ajax() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'super_duper_output_shortcode' ) {
2955
+                $result = true;
2956
+            }
2957
+
2958
+            return $result;
2959
+        }
2960
+
2961
+        /**
2962
+         * Get an instance hash that will be unique to the type and settings.
2963
+         *
2964
+         * @since 1.0.20
2965
+         * @return string
2966
+         */
2967
+        public function get_instance_hash(){
2968
+            $instance_string = $this->base_id.serialize($this->instance);
2969
+            return hash('crc32b',$instance_string);
2970
+        }
2971
+
2972
+        /**
2973
+         * Generate and return inline styles from CSS rules that will match the unique class of the instance.
2974
+         *
2975
+         * @param array $rules
2976
+         *
2977
+         * @since 1.0.20
2978
+         * @return string
2979
+         */
2980
+        public function get_instance_style($rules = array()){
2981
+            $css = '';
2982
+
2983
+            if(!empty($rules)){
2984
+                $rules = array_unique($rules);
2985
+                $instance_hash = $this->get_instance_hash();
2986
+                $css .= "<style>";
2987
+                foreach($rules as $rule){
2988
+                    $css .= ".sdel-$instance_hash $rule";
2989
+                }
2990
+                $css .= "</style>";
2991
+            }
2992
+
2993
+
2994
+            return $css;
2995
+
2996
+        }
2997
+
2998
+    }
2999 2999
 
3000 3000
 }
Please login to merge, or discard this patch.