@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | - exit; |
|
| 3 | + exit; |
|
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -10,747 +10,747 @@ discard block |
||
| 10 | 10 | class GetPaid_Payment_Form_Submission { |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | - * Submission ID |
|
| 14 | - * |
|
| 15 | - * @var string |
|
| 16 | - */ |
|
| 17 | - public $id = null; |
|
| 13 | + * Submission ID |
|
| 14 | + * |
|
| 15 | + * @var string |
|
| 16 | + */ |
|
| 17 | + public $id = null; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Sets the associated payment form. |
|
| 21 | - * |
|
| 22 | - * @var GetPaid_Payment_Form |
|
| 23 | - */ |
|
| 19 | + /** |
|
| 20 | + * Sets the associated payment form. |
|
| 21 | + * |
|
| 22 | + * @var GetPaid_Payment_Form |
|
| 23 | + */ |
|
| 24 | 24 | protected $payment_form = null; |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | - * The country for the submission. |
|
| 28 | - * |
|
| 29 | - * @var string |
|
| 30 | - */ |
|
| 31 | - public $country = null; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * The state for the submission. |
|
| 35 | - * |
|
| 36 | - * @since 1.0.19 |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - public $state = null; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The invoice associated with the submission. |
|
| 43 | - * |
|
| 44 | - * @var WPInv_Invoice |
|
| 45 | - */ |
|
| 46 | - protected $invoice = null; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The discount associated with the submission. |
|
| 50 | - * |
|
| 51 | - * @var WPInv_Discount |
|
| 52 | - */ |
|
| 53 | - protected $discount = null; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * The raw submission data. |
|
| 57 | - * |
|
| 58 | - * @var array |
|
| 59 | - */ |
|
| 60 | - protected $data = null; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Whether this submission contains a recurring item. |
|
| 64 | - * |
|
| 65 | - * @var bool |
|
| 66 | - */ |
|
| 67 | - public $has_recurring = false; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The sub total amount for the submission. |
|
| 71 | - * |
|
| 72 | - * @var float |
|
| 73 | - */ |
|
| 74 | - public $subtotal_amount = 0; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * The total discount amount for the submission. |
|
| 78 | - * |
|
| 79 | - * @var float |
|
| 80 | - */ |
|
| 81 | - protected $total_discount_amount = 0; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * The total tax amount for the submission. |
|
| 85 | - * |
|
| 86 | - * @var float |
|
| 87 | - */ |
|
| 88 | - protected $total_tax_amount = 0; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * The total fees amount for the submission. |
|
| 92 | - * |
|
| 93 | - * @var float |
|
| 94 | - */ |
|
| 95 | - protected $total_fees_amount = 0; |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * An array of fees for the submission. |
|
| 99 | - * |
|
| 100 | - * @var array |
|
| 101 | - */ |
|
| 102 | - protected $fees = array(); |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * An array of discounts for the submission. |
|
| 106 | - * |
|
| 107 | - * @var array |
|
| 108 | - */ |
|
| 109 | - protected $discounts = array(); |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * An array of taxes for the submission. |
|
| 113 | - * |
|
| 114 | - * @var array |
|
| 115 | - */ |
|
| 116 | - protected $taxes = array(); |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * An array of items for the submission. |
|
| 120 | - * |
|
| 121 | - * @var GetPaid_Form_Item[] |
|
| 122 | - */ |
|
| 123 | - protected $items = array(); |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * The last error. |
|
| 127 | - * |
|
| 128 | - * @var string |
|
| 129 | - */ |
|
| 130 | - public $last_error = null; |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Is the discount valid? |
|
| 134 | - * |
|
| 135 | - * @var string |
|
| 136 | - */ |
|
| 137 | - public $is_discount_valid = true; |
|
| 27 | + * The country for the submission. |
|
| 28 | + * |
|
| 29 | + * @var string |
|
| 30 | + */ |
|
| 31 | + public $country = null; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * The state for the submission. |
|
| 35 | + * |
|
| 36 | + * @since 1.0.19 |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + public $state = null; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The invoice associated with the submission. |
|
| 43 | + * |
|
| 44 | + * @var WPInv_Invoice |
|
| 45 | + */ |
|
| 46 | + protected $invoice = null; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The discount associated with the submission. |
|
| 50 | + * |
|
| 51 | + * @var WPInv_Discount |
|
| 52 | + */ |
|
| 53 | + protected $discount = null; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * The raw submission data. |
|
| 57 | + * |
|
| 58 | + * @var array |
|
| 59 | + */ |
|
| 60 | + protected $data = null; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Whether this submission contains a recurring item. |
|
| 64 | + * |
|
| 65 | + * @var bool |
|
| 66 | + */ |
|
| 67 | + public $has_recurring = false; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The sub total amount for the submission. |
|
| 71 | + * |
|
| 72 | + * @var float |
|
| 73 | + */ |
|
| 74 | + public $subtotal_amount = 0; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * The total discount amount for the submission. |
|
| 78 | + * |
|
| 79 | + * @var float |
|
| 80 | + */ |
|
| 81 | + protected $total_discount_amount = 0; |
|
| 138 | 82 | |
| 139 | 83 | /** |
| 140 | - * Class constructor. |
|
| 141 | - * |
|
| 142 | - */ |
|
| 143 | - public function __construct() { |
|
| 84 | + * The total tax amount for the submission. |
|
| 85 | + * |
|
| 86 | + * @var float |
|
| 87 | + */ |
|
| 88 | + protected $total_tax_amount = 0; |
|
| 144 | 89 | |
| 145 | - // Set the state and country to the default state and country. |
|
| 146 | - $this->country = wpinv_default_billing_country(); |
|
| 147 | - $this->state = wpinv_get_default_state(); |
|
| 90 | + /** |
|
| 91 | + * The total fees amount for the submission. |
|
| 92 | + * |
|
| 93 | + * @var float |
|
| 94 | + */ |
|
| 95 | + protected $total_fees_amount = 0; |
|
| 148 | 96 | |
| 149 | - // Do we have an actual submission? |
|
| 150 | - if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
|
| 151 | - $this->load_data( $_POST ); |
|
| 152 | - } |
|
| 153 | - } |
|
| 97 | + /** |
|
| 98 | + * An array of fees for the submission. |
|
| 99 | + * |
|
| 100 | + * @var array |
|
| 101 | + */ |
|
| 102 | + protected $fees = array(); |
|
| 154 | 103 | |
| 155 | - /** |
|
| 156 | - * Loads submission data. |
|
| 157 | - * |
|
| 158 | - * @param array $data |
|
| 159 | - */ |
|
| 160 | - public function load_data( $data ) { |
|
| 104 | + /** |
|
| 105 | + * An array of discounts for the submission. |
|
| 106 | + * |
|
| 107 | + * @var array |
|
| 108 | + */ |
|
| 109 | + protected $discounts = array(); |
|
| 161 | 110 | |
| 162 | - // Prepare submitted data... |
|
| 163 | - $data = wp_unslash( $data ); |
|
| 111 | + /** |
|
| 112 | + * An array of taxes for the submission. |
|
| 113 | + * |
|
| 114 | + * @var array |
|
| 115 | + */ |
|
| 116 | + protected $taxes = array(); |
|
| 164 | 117 | |
| 165 | - // Fitter the data. |
|
| 166 | - $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
|
| 118 | + /** |
|
| 119 | + * An array of items for the submission. |
|
| 120 | + * |
|
| 121 | + * @var GetPaid_Form_Item[] |
|
| 122 | + */ |
|
| 123 | + protected $items = array(); |
|
| 167 | 124 | |
| 168 | - $this->data = $data; |
|
| 125 | + /** |
|
| 126 | + * The last error. |
|
| 127 | + * |
|
| 128 | + * @var string |
|
| 129 | + */ |
|
| 130 | + public $last_error = null; |
|
| 169 | 131 | |
| 170 | - $this->id = md5( wp_json_encode( $data ) ); |
|
| 132 | + /** |
|
| 133 | + * Is the discount valid? |
|
| 134 | + * |
|
| 135 | + * @var string |
|
| 136 | + */ |
|
| 137 | + public $is_discount_valid = true; |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Class constructor. |
|
| 141 | + * |
|
| 142 | + */ |
|
| 143 | + public function __construct() { |
|
| 144 | + |
|
| 145 | + // Set the state and country to the default state and country. |
|
| 146 | + $this->country = wpinv_default_billing_country(); |
|
| 147 | + $this->state = wpinv_get_default_state(); |
|
| 148 | + |
|
| 149 | + // Do we have an actual submission? |
|
| 150 | + if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
|
| 151 | + $this->load_data( $_POST ); |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Loads submission data. |
|
| 157 | + * |
|
| 158 | + * @param array $data |
|
| 159 | + */ |
|
| 160 | + public function load_data( $data ) { |
|
| 171 | 161 | |
| 172 | - // Every submission needs an active payment form. |
|
| 173 | - if ( empty( $data['form_id'] ) ) { |
|
| 174 | - $this->last_error = __( 'Missing payment form', 'invoicing' ); |
|
| 162 | + // Prepare submitted data... |
|
| 163 | + $data = wp_unslash( $data ); |
|
| 164 | + |
|
| 165 | + // Fitter the data. |
|
| 166 | + $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
|
| 167 | + |
|
| 168 | + $this->data = $data; |
|
| 169 | + |
|
| 170 | + $this->id = md5( wp_json_encode( $data ) ); |
|
| 171 | + |
|
| 172 | + // Every submission needs an active payment form. |
|
| 173 | + if ( empty( $data['form_id'] ) ) { |
|
| 174 | + $this->last_error = __( 'Missing payment form', 'invoicing' ); |
|
| 175 | 175 | return; |
| 176 | - } |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - // Fetch the payment form. |
|
| 179 | - $form = new GetPaid_Payment_Form( $data['form_id'] ); |
|
| 178 | + // Fetch the payment form. |
|
| 179 | + $form = new GetPaid_Payment_Form( $data['form_id'] ); |
|
| 180 | 180 | |
| 181 | - if ( ! $form->is_active() ) { |
|
| 182 | - $this->last_error = __( 'Payment form not active', 'invoicing' ); |
|
| 183 | - return; |
|
| 184 | - } |
|
| 181 | + if ( ! $form->is_active() ) { |
|
| 182 | + $this->last_error = __( 'Payment form not active', 'invoicing' ); |
|
| 183 | + return; |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - // Fetch the payment form. |
|
| 187 | - $this->payment_form = $form; |
|
| 186 | + // Fetch the payment form. |
|
| 187 | + $this->payment_form = $form; |
|
| 188 | 188 | |
| 189 | - // For existing invoices, make sure that it is valid. |
|
| 189 | + // For existing invoices, make sure that it is valid. |
|
| 190 | 190 | if ( ! empty( $data['invoice_id'] ) ) { |
| 191 | 191 | $invoice = wpinv_get_invoice( $data['invoice_id'] ); |
| 192 | 192 | |
| 193 | 193 | if ( empty( $invoice ) ) { |
| 194 | - $this->last_error = __( 'Invalid invoice', 'invoicing' ); |
|
| 194 | + $this->last_error = __( 'Invalid invoice', 'invoicing' ); |
|
| 195 | 195 | return; |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - $this->payment_form->set_items( $invoice->cart_details ); |
|
| 198 | + $this->payment_form->set_items( $invoice->cart_details ); |
|
| 199 | 199 | |
| 200 | - $this->country = $invoice->country; |
|
| 201 | - $this->state = $invoice->state; |
|
| 200 | + $this->country = $invoice->country; |
|
| 201 | + $this->state = $invoice->state; |
|
| 202 | 202 | |
| 203 | - // Default forms do not have items. |
|
| 203 | + // Default forms do not have items. |
|
| 204 | 204 | } else if ( $form->is_default() && isset( $data['form_items'] ) ) { |
| 205 | - $this->payment_form->set_items( $data['form_items'] ); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - // User's country. |
|
| 209 | - if ( ! empty( $data['wpinv_country'] ) ) { |
|
| 210 | - $this->country = $data['wpinv_country']; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - // User's state. |
|
| 214 | - if ( ! empty( $data['wpinv_state'] ) ) { |
|
| 215 | - $this->country = $data['wpinv_state']; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - // Handle discounts. |
|
| 219 | - $this->maybe_prepare_discount(); |
|
| 220 | - |
|
| 221 | - // Handle items. |
|
| 222 | - $selected_items = array(); |
|
| 223 | - if ( ! empty( $data['getpaid-items'] ) ) { |
|
| 224 | - $selected_items = wpinv_clean( $data['getpaid-items'] ); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - foreach ( $this->payment_form->get_items() as $item ) { |
|
| 228 | - |
|
| 229 | - // Continue if this is an optional item and it has not been selected. |
|
| 230 | - if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) { |
|
| 231 | - continue; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - // (maybe) let customers change the quantities and prices. |
|
| 235 | - if ( isset( $selected_items[ $item->get_id() ] ) ) { |
|
| 236 | - |
|
| 237 | - // Maybe change the quantities. |
|
| 238 | - if ( $item->allows_quantities() && is_numeric( $selected_items[ $item->get_id() ]['quantity'] ) ) { |
|
| 239 | - $item->set_quantity( (int) $selected_items[ $item->get_id() ]['quantity'] ); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - // Maybe change the price. |
|
| 243 | - if ( $item->user_can_set_their_price() ) { |
|
| 244 | - $price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] ); |
|
| 245 | - |
|
| 246 | - // But don't get lower than the minimum price. |
|
| 247 | - if ( $price < $item->get_minimum_price() ) { |
|
| 248 | - $price = $item->get_minimum_price(); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - $item->set_price( $price ); |
|
| 252 | - |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - // Add the item to the form. |
|
| 258 | - $this->add_item( $item ); |
|
| 259 | - |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - // Fired when we are done processing a submission. |
|
| 263 | - do_action( 'getpaid_process_submission', $this ); |
|
| 264 | - |
|
| 265 | - // Remove invalid discount. |
|
| 266 | - $this->maybe_remove_discount(); |
|
| 267 | - |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Returns the payment form. |
|
| 272 | - * |
|
| 273 | - * @since 1.0.19 |
|
| 274 | - * @return GetPaid_Payment_Form |
|
| 275 | - */ |
|
| 276 | - public function get_payment_form() { |
|
| 277 | - return $this->payment_form; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Returns the associated invoice. |
|
| 282 | - * |
|
| 283 | - * @since 1.0.19 |
|
| 284 | - * @return WPInv_Invoice |
|
| 285 | - */ |
|
| 286 | - public function get_invoice() { |
|
| 287 | - return $this->invoice; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Checks whether there is an invoice associated with this submission. |
|
| 292 | - * |
|
| 293 | - * @since 1.0.19 |
|
| 294 | - * @return bool |
|
| 295 | - */ |
|
| 296 | - public function has_invoice() { |
|
| 297 | - return ! empty( $this->invoice ); |
|
| 298 | - } |
|
| 205 | + $this->payment_form->set_items( $data['form_items'] ); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + // User's country. |
|
| 209 | + if ( ! empty( $data['wpinv_country'] ) ) { |
|
| 210 | + $this->country = $data['wpinv_country']; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + // User's state. |
|
| 214 | + if ( ! empty( $data['wpinv_state'] ) ) { |
|
| 215 | + $this->country = $data['wpinv_state']; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + // Handle discounts. |
|
| 219 | + $this->maybe_prepare_discount(); |
|
| 220 | + |
|
| 221 | + // Handle items. |
|
| 222 | + $selected_items = array(); |
|
| 223 | + if ( ! empty( $data['getpaid-items'] ) ) { |
|
| 224 | + $selected_items = wpinv_clean( $data['getpaid-items'] ); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + foreach ( $this->payment_form->get_items() as $item ) { |
|
| 228 | + |
|
| 229 | + // Continue if this is an optional item and it has not been selected. |
|
| 230 | + if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) { |
|
| 231 | + continue; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + // (maybe) let customers change the quantities and prices. |
|
| 235 | + if ( isset( $selected_items[ $item->get_id() ] ) ) { |
|
| 236 | + |
|
| 237 | + // Maybe change the quantities. |
|
| 238 | + if ( $item->allows_quantities() && is_numeric( $selected_items[ $item->get_id() ]['quantity'] ) ) { |
|
| 239 | + $item->set_quantity( (int) $selected_items[ $item->get_id() ]['quantity'] ); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + // Maybe change the price. |
|
| 243 | + if ( $item->user_can_set_their_price() ) { |
|
| 244 | + $price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] ); |
|
| 245 | + |
|
| 246 | + // But don't get lower than the minimum price. |
|
| 247 | + if ( $price < $item->get_minimum_price() ) { |
|
| 248 | + $price = $item->get_minimum_price(); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + $item->set_price( $price ); |
|
| 252 | + |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + // Add the item to the form. |
|
| 258 | + $this->add_item( $item ); |
|
| 259 | + |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + // Fired when we are done processing a submission. |
|
| 263 | + do_action( 'getpaid_process_submission', $this ); |
|
| 264 | + |
|
| 265 | + // Remove invalid discount. |
|
| 266 | + $this->maybe_remove_discount(); |
|
| 267 | + |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Returns the payment form. |
|
| 272 | + * |
|
| 273 | + * @since 1.0.19 |
|
| 274 | + * @return GetPaid_Payment_Form |
|
| 275 | + */ |
|
| 276 | + public function get_payment_form() { |
|
| 277 | + return $this->payment_form; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Returns the associated invoice. |
|
| 282 | + * |
|
| 283 | + * @since 1.0.19 |
|
| 284 | + * @return WPInv_Invoice |
|
| 285 | + */ |
|
| 286 | + public function get_invoice() { |
|
| 287 | + return $this->invoice; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Checks whether there is an invoice associated with this submission. |
|
| 292 | + * |
|
| 293 | + * @since 1.0.19 |
|
| 294 | + * @return bool |
|
| 295 | + */ |
|
| 296 | + public function has_invoice() { |
|
| 297 | + return ! empty( $this->invoice ); |
|
| 298 | + } |
|
| 299 | 299 | |
| 300 | - /** |
|
| 301 | - * Returns the appropriate currency for the submission. |
|
| 302 | - * |
|
| 303 | - * @since 1.0.19 |
|
| 304 | - * @return string |
|
| 305 | - */ |
|
| 306 | - public function get_currency() { |
|
| 307 | - if ( $this->has_invoice() ) { |
|
| 308 | - return $this->invoice->get_currency(); |
|
| 309 | - } |
|
| 310 | - return wpinv_get_currency(); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Returns the raw submission data. |
|
| 315 | - * |
|
| 316 | - * @since 1.0.19 |
|
| 317 | - * @return array |
|
| 318 | - */ |
|
| 319 | - public function get_data() { |
|
| 320 | - return $this->data; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Checks if a required field is set. |
|
| 325 | - * |
|
| 326 | - * @since 1.0.19 |
|
| 327 | - */ |
|
| 328 | - public function is_required_field_set( $field ) { |
|
| 329 | - return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - ///////// Items ////////////// |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Adds an item to the submission. |
|
| 336 | - * |
|
| 337 | - * @since 1.0.19 |
|
| 338 | - * @param GetPaid_Form_Item $item |
|
| 339 | - */ |
|
| 340 | - public function add_item( $item ) { |
|
| 341 | - |
|
| 342 | - // Make sure that it is available for purchase. |
|
| 343 | - if ( ! $item->can_purchase() ) { |
|
| 344 | - return; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - // Do we have a recurring item? |
|
| 348 | - if ( $item->is_recurring() ) { |
|
| 349 | - $this->has_recurring = true; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - $this->items[ $item->get_id() ] = $item; |
|
| 353 | - |
|
| 354 | - $this->subtotal_amount += $item->get_sub_total(); |
|
| 355 | - |
|
| 356 | - $this->process_item_discount( $item ); |
|
| 357 | - |
|
| 358 | - $this->process_item_tax( $item ); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Retrieves a specific item. |
|
| 363 | - * |
|
| 364 | - * @since 1.0.19 |
|
| 365 | - */ |
|
| 366 | - public function get_item( $item_id ) { |
|
| 367 | - return isset( $this->items[ $item_id ] ) ? $this->items[ $item_id ] : null; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Returns all items. |
|
| 372 | - * |
|
| 373 | - * @since 1.0.19 |
|
| 374 | - * @return GetPaid_Form_Item[] |
|
| 375 | - */ |
|
| 376 | - public function get_items() { |
|
| 377 | - return $this->items; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - ///////// TAXES ////////////// |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Adds a tax to the submission. |
|
| 384 | - * |
|
| 385 | - * @since 1.0.19 |
|
| 386 | - */ |
|
| 387 | - public function add_tax( $name, $amount ) { |
|
| 388 | - $amount = (float) wpinv_sanitize_amount( $amount ); |
|
| 389 | - |
|
| 390 | - $this->total_tax_amount += $amount; |
|
| 391 | - |
|
| 392 | - if ( isset( $this->taxes[ $name ] ) ) { |
|
| 393 | - $this->taxes[ $name ] += $amount; |
|
| 394 | - } else { |
|
| 395 | - $this->taxes[ $name ] = $amount; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Whether or not we'll use taxes for the submission. |
|
| 402 | - * |
|
| 403 | - * @since 1.0.19 |
|
| 404 | - */ |
|
| 405 | - public function use_taxes() { |
|
| 406 | - |
|
| 407 | - $use_taxes = wpinv_use_taxes(); |
|
| 408 | - |
|
| 409 | - if ( $this->has_invoice() && $this->invoice->disable_taxes ) { |
|
| 410 | - $use_taxes = false; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
|
| 414 | - |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * Maybe process tax. |
|
| 419 | - * |
|
| 420 | - * @since 1.0.19 |
|
| 421 | - * @param GetPaid_Form_Item $item |
|
| 422 | - */ |
|
| 423 | - public function process_item_tax( $item ) { |
|
| 424 | - |
|
| 425 | - // Abort early if we're not using taxes. |
|
| 426 | - if ( ! $this->use_taxes() ) { |
|
| 427 | - return; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - $rate = wpinv_get_tax_rate( $this->country, $this->state, $item->get_id() ); |
|
| 431 | - $price = $item->get_sub_total(); |
|
| 432 | - |
|
| 433 | - if ( wpinv_prices_include_tax() ) { |
|
| 434 | - $item_tax = $price - ( $price - $price * $rate * 0.01 ); |
|
| 435 | - } else { |
|
| 436 | - $item_tax = $price * $rate * 0.01; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - $this->add_tax( 'Tax', $item_tax ); |
|
| 440 | - |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * Returns the total tax amount. |
|
| 445 | - * |
|
| 446 | - * @since 1.0.19 |
|
| 447 | - */ |
|
| 448 | - public function get_total_tax() { |
|
| 449 | - return $this->total_tax_amount; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Retrieves a specific tax. |
|
| 454 | - * |
|
| 455 | - * @since 1.0.19 |
|
| 456 | - */ |
|
| 457 | - public function get_tax( $name ) { |
|
| 458 | - return isset( $this->taxes[ $name ] ) ? $this->taxes[ $name ] : 0; |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - /** |
|
| 462 | - * Returns all taxes. |
|
| 463 | - * |
|
| 464 | - * @since 1.0.19 |
|
| 465 | - */ |
|
| 466 | - public function get_taxes() { |
|
| 467 | - return $this->taxes; |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - ///////// DISCOUNTS ////////////// |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Adds a discount to the submission. |
|
| 474 | - * |
|
| 475 | - * @since 1.0.19 |
|
| 476 | - */ |
|
| 477 | - public function add_discount( $name, $amount ) { |
|
| 478 | - $amount = wpinv_sanitize_amount( $amount ); |
|
| 479 | - |
|
| 480 | - $this->total_discount_amount += $amount; |
|
| 481 | - |
|
| 482 | - if ( isset( $this->discounts[ $name ] ) ) { |
|
| 483 | - $this->discounts[ $name ] += $amount; |
|
| 484 | - } else { |
|
| 485 | - $this->discounts[ $name ] = $amount; |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * Removes a discount from the submission. |
|
| 492 | - * |
|
| 493 | - * @since 1.0.19 |
|
| 494 | - */ |
|
| 495 | - public function remove_discount( $name ) { |
|
| 496 | - |
|
| 497 | - if ( isset( $this->discounts[ $name ] ) ) { |
|
| 498 | - $this->total_discount_amount -= $this->discounts[ $name ]; |
|
| 499 | - unset( $this->discounts[ $name ] ); |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Checks whether there is a discount code associated with this submission. |
|
| 506 | - * |
|
| 507 | - * @since 1.0.19 |
|
| 508 | - * @return bool |
|
| 509 | - */ |
|
| 510 | - public function has_discount_code() { |
|
| 511 | - return ! empty( $this->discount ); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * Returns the discount code. |
|
| 516 | - * |
|
| 517 | - * @since 1.0.19 |
|
| 518 | - * @return bool |
|
| 519 | - */ |
|
| 520 | - public function get_discount_code() { |
|
| 521 | - return $this->has_discount_code() ? $this->discount->code : ''; |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - /** |
|
| 525 | - * Prepares an item discount. |
|
| 526 | - * |
|
| 527 | - * @since 1.0.19 |
|
| 528 | - */ |
|
| 529 | - public function maybe_prepare_discount() { |
|
| 530 | - |
|
| 531 | - // Do we have a discount? |
|
| 532 | - if ( empty( $this->data['discount'] ) ) { |
|
| 533 | - return; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - // Fetch the discount. |
|
| 537 | - $discount = wpinv_get_discount_obj( $this->data['discount'] ); |
|
| 538 | - |
|
| 539 | - // Ensure it is active. |
|
| 300 | + /** |
|
| 301 | + * Returns the appropriate currency for the submission. |
|
| 302 | + * |
|
| 303 | + * @since 1.0.19 |
|
| 304 | + * @return string |
|
| 305 | + */ |
|
| 306 | + public function get_currency() { |
|
| 307 | + if ( $this->has_invoice() ) { |
|
| 308 | + return $this->invoice->get_currency(); |
|
| 309 | + } |
|
| 310 | + return wpinv_get_currency(); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Returns the raw submission data. |
|
| 315 | + * |
|
| 316 | + * @since 1.0.19 |
|
| 317 | + * @return array |
|
| 318 | + */ |
|
| 319 | + public function get_data() { |
|
| 320 | + return $this->data; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Checks if a required field is set. |
|
| 325 | + * |
|
| 326 | + * @since 1.0.19 |
|
| 327 | + */ |
|
| 328 | + public function is_required_field_set( $field ) { |
|
| 329 | + return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + ///////// Items ////////////// |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Adds an item to the submission. |
|
| 336 | + * |
|
| 337 | + * @since 1.0.19 |
|
| 338 | + * @param GetPaid_Form_Item $item |
|
| 339 | + */ |
|
| 340 | + public function add_item( $item ) { |
|
| 341 | + |
|
| 342 | + // Make sure that it is available for purchase. |
|
| 343 | + if ( ! $item->can_purchase() ) { |
|
| 344 | + return; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + // Do we have a recurring item? |
|
| 348 | + if ( $item->is_recurring() ) { |
|
| 349 | + $this->has_recurring = true; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + $this->items[ $item->get_id() ] = $item; |
|
| 353 | + |
|
| 354 | + $this->subtotal_amount += $item->get_sub_total(); |
|
| 355 | + |
|
| 356 | + $this->process_item_discount( $item ); |
|
| 357 | + |
|
| 358 | + $this->process_item_tax( $item ); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Retrieves a specific item. |
|
| 363 | + * |
|
| 364 | + * @since 1.0.19 |
|
| 365 | + */ |
|
| 366 | + public function get_item( $item_id ) { |
|
| 367 | + return isset( $this->items[ $item_id ] ) ? $this->items[ $item_id ] : null; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Returns all items. |
|
| 372 | + * |
|
| 373 | + * @since 1.0.19 |
|
| 374 | + * @return GetPaid_Form_Item[] |
|
| 375 | + */ |
|
| 376 | + public function get_items() { |
|
| 377 | + return $this->items; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + ///////// TAXES ////////////// |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Adds a tax to the submission. |
|
| 384 | + * |
|
| 385 | + * @since 1.0.19 |
|
| 386 | + */ |
|
| 387 | + public function add_tax( $name, $amount ) { |
|
| 388 | + $amount = (float) wpinv_sanitize_amount( $amount ); |
|
| 389 | + |
|
| 390 | + $this->total_tax_amount += $amount; |
|
| 391 | + |
|
| 392 | + if ( isset( $this->taxes[ $name ] ) ) { |
|
| 393 | + $this->taxes[ $name ] += $amount; |
|
| 394 | + } else { |
|
| 395 | + $this->taxes[ $name ] = $amount; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Whether or not we'll use taxes for the submission. |
|
| 402 | + * |
|
| 403 | + * @since 1.0.19 |
|
| 404 | + */ |
|
| 405 | + public function use_taxes() { |
|
| 406 | + |
|
| 407 | + $use_taxes = wpinv_use_taxes(); |
|
| 408 | + |
|
| 409 | + if ( $this->has_invoice() && $this->invoice->disable_taxes ) { |
|
| 410 | + $use_taxes = false; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
|
| 414 | + |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * Maybe process tax. |
|
| 419 | + * |
|
| 420 | + * @since 1.0.19 |
|
| 421 | + * @param GetPaid_Form_Item $item |
|
| 422 | + */ |
|
| 423 | + public function process_item_tax( $item ) { |
|
| 424 | + |
|
| 425 | + // Abort early if we're not using taxes. |
|
| 426 | + if ( ! $this->use_taxes() ) { |
|
| 427 | + return; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + $rate = wpinv_get_tax_rate( $this->country, $this->state, $item->get_id() ); |
|
| 431 | + $price = $item->get_sub_total(); |
|
| 432 | + |
|
| 433 | + if ( wpinv_prices_include_tax() ) { |
|
| 434 | + $item_tax = $price - ( $price - $price * $rate * 0.01 ); |
|
| 435 | + } else { |
|
| 436 | + $item_tax = $price * $rate * 0.01; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + $this->add_tax( 'Tax', $item_tax ); |
|
| 440 | + |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * Returns the total tax amount. |
|
| 445 | + * |
|
| 446 | + * @since 1.0.19 |
|
| 447 | + */ |
|
| 448 | + public function get_total_tax() { |
|
| 449 | + return $this->total_tax_amount; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Retrieves a specific tax. |
|
| 454 | + * |
|
| 455 | + * @since 1.0.19 |
|
| 456 | + */ |
|
| 457 | + public function get_tax( $name ) { |
|
| 458 | + return isset( $this->taxes[ $name ] ) ? $this->taxes[ $name ] : 0; |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * Returns all taxes. |
|
| 463 | + * |
|
| 464 | + * @since 1.0.19 |
|
| 465 | + */ |
|
| 466 | + public function get_taxes() { |
|
| 467 | + return $this->taxes; |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + ///////// DISCOUNTS ////////////// |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Adds a discount to the submission. |
|
| 474 | + * |
|
| 475 | + * @since 1.0.19 |
|
| 476 | + */ |
|
| 477 | + public function add_discount( $name, $amount ) { |
|
| 478 | + $amount = wpinv_sanitize_amount( $amount ); |
|
| 479 | + |
|
| 480 | + $this->total_discount_amount += $amount; |
|
| 481 | + |
|
| 482 | + if ( isset( $this->discounts[ $name ] ) ) { |
|
| 483 | + $this->discounts[ $name ] += $amount; |
|
| 484 | + } else { |
|
| 485 | + $this->discounts[ $name ] = $amount; |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * Removes a discount from the submission. |
|
| 492 | + * |
|
| 493 | + * @since 1.0.19 |
|
| 494 | + */ |
|
| 495 | + public function remove_discount( $name ) { |
|
| 496 | + |
|
| 497 | + if ( isset( $this->discounts[ $name ] ) ) { |
|
| 498 | + $this->total_discount_amount -= $this->discounts[ $name ]; |
|
| 499 | + unset( $this->discounts[ $name ] ); |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Checks whether there is a discount code associated with this submission. |
|
| 506 | + * |
|
| 507 | + * @since 1.0.19 |
|
| 508 | + * @return bool |
|
| 509 | + */ |
|
| 510 | + public function has_discount_code() { |
|
| 511 | + return ! empty( $this->discount ); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * Returns the discount code. |
|
| 516 | + * |
|
| 517 | + * @since 1.0.19 |
|
| 518 | + * @return bool |
|
| 519 | + */ |
|
| 520 | + public function get_discount_code() { |
|
| 521 | + return $this->has_discount_code() ? $this->discount->code : ''; |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + /** |
|
| 525 | + * Prepares an item discount. |
|
| 526 | + * |
|
| 527 | + * @since 1.0.19 |
|
| 528 | + */ |
|
| 529 | + public function maybe_prepare_discount() { |
|
| 530 | + |
|
| 531 | + // Do we have a discount? |
|
| 532 | + if ( empty( $this->data['discount'] ) ) { |
|
| 533 | + return; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + // Fetch the discount. |
|
| 537 | + $discount = wpinv_get_discount_obj( $this->data['discount'] ); |
|
| 538 | + |
|
| 539 | + // Ensure it is active. |
|
| 540 | 540 | if ( ! $discount->exists() || ! $discount->is_active() || ! $discount->has_started() || $discount->is_expired() ) { |
| 541 | - $this->is_discount_valid = false; |
|
| 542 | - $this->last_error = __( 'Invalid or expired discount code', 'invoicing' ); |
|
| 543 | - return; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - // For single use discounts... |
|
| 547 | - if ( $discount->is_single_use ) { |
|
| 548 | - |
|
| 549 | - if ( ! $this->has_billing_email() ) { |
|
| 550 | - $this->is_discount_valid = false; |
|
| 551 | - $this->last_error = __( 'You need to enter your billing email before applying this discount', 'invoicing' ); |
|
| 552 | - return; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - if ( ! $discount->is_valid_for_user( $this->get_billing_email() ) ) { |
|
| 556 | - $this->is_discount_valid = false; |
|
| 557 | - $this->last_error = __( 'You have already used this discount', 'invoicing' ); |
|
| 558 | - return; |
|
| 559 | - } |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - // Set the discount. |
|
| 563 | - $this->discount = $discount; |
|
| 564 | - |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - /** |
|
| 568 | - * Removes an invalid discount code. |
|
| 569 | - * |
|
| 570 | - * @since 1.0.19 |
|
| 571 | - */ |
|
| 572 | - public function maybe_remove_discount() { |
|
| 573 | - |
|
| 574 | - // Do we have a discount? |
|
| 575 | - if ( empty( $this->has_discount_code() ) ) { |
|
| 576 | - return; |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - // Fetch the discount amount. |
|
| 580 | - $amount = $this->get_discount( 'Discount' ); |
|
| 581 | - |
|
| 582 | - // Abort early if this is a "zero" discount. |
|
| 583 | - if ( empty( $amount ) ) { |
|
| 584 | - return; |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - $total = $this->subtotal_amount + $this->get_total_fees() + $this->get_total_tax(); |
|
| 588 | - |
|
| 589 | - if ( ! $this->discount->is_minimum_amount_met( $total ) ) { |
|
| 590 | - $this->is_discount_valid = false; |
|
| 541 | + $this->is_discount_valid = false; |
|
| 542 | + $this->last_error = __( 'Invalid or expired discount code', 'invoicing' ); |
|
| 543 | + return; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + // For single use discounts... |
|
| 547 | + if ( $discount->is_single_use ) { |
|
| 548 | + |
|
| 549 | + if ( ! $this->has_billing_email() ) { |
|
| 550 | + $this->is_discount_valid = false; |
|
| 551 | + $this->last_error = __( 'You need to enter your billing email before applying this discount', 'invoicing' ); |
|
| 552 | + return; |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + if ( ! $discount->is_valid_for_user( $this->get_billing_email() ) ) { |
|
| 556 | + $this->is_discount_valid = false; |
|
| 557 | + $this->last_error = __( 'You have already used this discount', 'invoicing' ); |
|
| 558 | + return; |
|
| 559 | + } |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + // Set the discount. |
|
| 563 | + $this->discount = $discount; |
|
| 564 | + |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + /** |
|
| 568 | + * Removes an invalid discount code. |
|
| 569 | + * |
|
| 570 | + * @since 1.0.19 |
|
| 571 | + */ |
|
| 572 | + public function maybe_remove_discount() { |
|
| 573 | + |
|
| 574 | + // Do we have a discount? |
|
| 575 | + if ( empty( $this->has_discount_code() ) ) { |
|
| 576 | + return; |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + // Fetch the discount amount. |
|
| 580 | + $amount = $this->get_discount( 'Discount' ); |
|
| 581 | + |
|
| 582 | + // Abort early if this is a "zero" discount. |
|
| 583 | + if ( empty( $amount ) ) { |
|
| 584 | + return; |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + $total = $this->subtotal_amount + $this->get_total_fees() + $this->get_total_tax(); |
|
| 588 | + |
|
| 589 | + if ( ! $this->discount->is_minimum_amount_met( $total ) ) { |
|
| 590 | + $this->is_discount_valid = false; |
|
| 591 | 591 | $min = wpinv_price( wpinv_format_amount( $$this->discount->min_total ) ); |
| 592 | - $this->last_error = sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ); |
|
| 592 | + $this->last_error = sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ); |
|
| 593 | 593 | } |
| 594 | 594 | |
| 595 | 595 | if ( ! $$this->discount->is_maximum_amount_met( $total ) ) { |
| 596 | - $this->is_discount_valid = false; |
|
| 596 | + $this->is_discount_valid = false; |
|
| 597 | 597 | $max = wpinv_price( wpinv_format_amount( $$this->discount->max_total ) ); |
| 598 | - $this->last_error = sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ); |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - if ( ! $this->is_discount_valid ) { |
|
| 602 | - $this->discount = null; |
|
| 603 | - $this->remove_discount( 'Discount' ); |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - } |
|
| 607 | - |
|
| 608 | - /** |
|
| 609 | - * Maybe process discount. |
|
| 610 | - * |
|
| 611 | - * @since 1.0.19 |
|
| 612 | - * @param GetPaid_Form_Item $item |
|
| 613 | - */ |
|
| 614 | - public function process_item_discount( $item ) { |
|
| 615 | - |
|
| 616 | - // Abort early if there is no discount. |
|
| 617 | - if ( ! $this->has_discount_code() ) { |
|
| 618 | - return; |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - // Ensure that it is valid for this item. |
|
| 622 | - if ( ! $this->discount->is_valid_for_items( array( $item->get_id() ) ) ) { |
|
| 623 | - return; |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - // Fetch the discounted amount. |
|
| 627 | - $discount = $this->discount->get_discounted_amount( $item->get_price() * $item->get_qantity() ); |
|
| 628 | - |
|
| 629 | - $this->add_discount( 'Discount', $discount ); |
|
| 630 | - |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - /** |
|
| 634 | - * Returns the total discount amount. |
|
| 635 | - * |
|
| 636 | - * @since 1.0.19 |
|
| 637 | - */ |
|
| 638 | - public function get_total_discount() { |
|
| 639 | - return $this->total_discount_amount; |
|
| 640 | - } |
|
| 641 | - |
|
| 642 | - /** |
|
| 643 | - * Gets a specific discount. |
|
| 644 | - * |
|
| 645 | - * @since 1.0.19 |
|
| 646 | - */ |
|
| 647 | - public function get_discount( $name ) { |
|
| 648 | - return isset( $this->discounts[ $name ] ) ? $this->discounts[ $name ] : 0; |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - /** |
|
| 652 | - * Returns all discounts. |
|
| 653 | - * |
|
| 654 | - * @since 1.0.19 |
|
| 655 | - */ |
|
| 656 | - public function get_discounts() { |
|
| 657 | - return $this->discounts; |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - ///////// FEES ////////////// |
|
| 661 | - |
|
| 662 | - /** |
|
| 663 | - * Adds a fee to the submission. |
|
| 664 | - * |
|
| 665 | - * @since 1.0.19 |
|
| 666 | - */ |
|
| 667 | - public function add_fee( $name, $amount ) { |
|
| 668 | - $amount = wpinv_sanitize_amount( $amount ); |
|
| 669 | - |
|
| 670 | - $this->total_fees_amount += $amount; |
|
| 671 | - |
|
| 672 | - if ( isset( $this->fees[ $name ] ) ) { |
|
| 673 | - $this->fees[ $name ] += $amount; |
|
| 674 | - } else { |
|
| 675 | - $this->fees[ $name ] = $amount; |
|
| 676 | - } |
|
| 677 | - |
|
| 678 | - } |
|
| 679 | - |
|
| 680 | - /** |
|
| 681 | - * Returns the total fees amount. |
|
| 682 | - * |
|
| 683 | - * @since 1.0.19 |
|
| 684 | - */ |
|
| 685 | - public function get_total_fees() { |
|
| 686 | - return $this->total_fees_amount; |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - /** |
|
| 690 | - * Retrieves a specific fee. |
|
| 691 | - * |
|
| 692 | - * @since 1.0.19 |
|
| 693 | - */ |
|
| 694 | - public function get_fee( $name ) { |
|
| 695 | - return isset( $this->fees[ $name ] ) ? $this->fees[ $name ] : 0; |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - /** |
|
| 699 | - * Returns all fees. |
|
| 700 | - * |
|
| 701 | - * @since 1.0.19 |
|
| 702 | - */ |
|
| 703 | - public function get_fees() { |
|
| 704 | - return $this->fees; |
|
| 705 | - } |
|
| 706 | - |
|
| 707 | - // MISC // |
|
| 708 | - |
|
| 709 | - /** |
|
| 710 | - * Returns the total amount to collect for this submission. |
|
| 711 | - * |
|
| 712 | - * @since 1.0.19 |
|
| 713 | - */ |
|
| 714 | - public function get_total() { |
|
| 715 | - $total = $this->subtotal_amount + $this->get_total_fees() - $this->get_total_discount() + $this->get_total_tax(); |
|
| 716 | - $total = apply_filters( 'getpaid_get_submission_total_amount', $total, $this ); |
|
| 717 | - return wpinv_sanitize_amount( $total ); |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - /** |
|
| 721 | - * Whether payment details should be collected for this submission. |
|
| 722 | - * |
|
| 723 | - * @since 1.0.19 |
|
| 724 | - */ |
|
| 725 | - public function get_payment_details() { |
|
| 726 | - $collect = $this->subtotal_amount + $this->get_total_fees() - $this->get_total_discount() + $this->get_total_tax(); |
|
| 727 | - |
|
| 728 | - if ( $this->has_recurring ) { |
|
| 729 | - $collect = true; |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - $collect = apply_filters( 'getpaid_submission_collect_payment_details', $collect, $this ); |
|
| 733 | - return $collect; |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - /** |
|
| 737 | - * Returns the billing email of the user. |
|
| 738 | - * |
|
| 739 | - * @since 1.0.19 |
|
| 740 | - */ |
|
| 741 | - public function get_billing_email() { |
|
| 742 | - $billing_email = empty( $this->data['billing_email'] ) ? '' : $this->data['billing_email']; |
|
| 743 | - return apply_filters( 'getpaid_get_submission_billing_email', $billing_email, $this ); |
|
| 744 | - } |
|
| 745 | - |
|
| 746 | - /** |
|
| 747 | - * Checks if the submitter has a billing email. |
|
| 748 | - * |
|
| 749 | - * @since 1.0.19 |
|
| 750 | - */ |
|
| 751 | - public function has_billing_email() { |
|
| 752 | - $billing_email = $this->get_billing_email(); |
|
| 753 | - return ! empty( $billing_email ); |
|
| 754 | - } |
|
| 598 | + $this->last_error = sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ); |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + if ( ! $this->is_discount_valid ) { |
|
| 602 | + $this->discount = null; |
|
| 603 | + $this->remove_discount( 'Discount' ); |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + } |
|
| 607 | + |
|
| 608 | + /** |
|
| 609 | + * Maybe process discount. |
|
| 610 | + * |
|
| 611 | + * @since 1.0.19 |
|
| 612 | + * @param GetPaid_Form_Item $item |
|
| 613 | + */ |
|
| 614 | + public function process_item_discount( $item ) { |
|
| 615 | + |
|
| 616 | + // Abort early if there is no discount. |
|
| 617 | + if ( ! $this->has_discount_code() ) { |
|
| 618 | + return; |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + // Ensure that it is valid for this item. |
|
| 622 | + if ( ! $this->discount->is_valid_for_items( array( $item->get_id() ) ) ) { |
|
| 623 | + return; |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + // Fetch the discounted amount. |
|
| 627 | + $discount = $this->discount->get_discounted_amount( $item->get_price() * $item->get_qantity() ); |
|
| 628 | + |
|
| 629 | + $this->add_discount( 'Discount', $discount ); |
|
| 630 | + |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + /** |
|
| 634 | + * Returns the total discount amount. |
|
| 635 | + * |
|
| 636 | + * @since 1.0.19 |
|
| 637 | + */ |
|
| 638 | + public function get_total_discount() { |
|
| 639 | + return $this->total_discount_amount; |
|
| 640 | + } |
|
| 641 | + |
|
| 642 | + /** |
|
| 643 | + * Gets a specific discount. |
|
| 644 | + * |
|
| 645 | + * @since 1.0.19 |
|
| 646 | + */ |
|
| 647 | + public function get_discount( $name ) { |
|
| 648 | + return isset( $this->discounts[ $name ] ) ? $this->discounts[ $name ] : 0; |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + /** |
|
| 652 | + * Returns all discounts. |
|
| 653 | + * |
|
| 654 | + * @since 1.0.19 |
|
| 655 | + */ |
|
| 656 | + public function get_discounts() { |
|
| 657 | + return $this->discounts; |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + ///////// FEES ////////////// |
|
| 661 | + |
|
| 662 | + /** |
|
| 663 | + * Adds a fee to the submission. |
|
| 664 | + * |
|
| 665 | + * @since 1.0.19 |
|
| 666 | + */ |
|
| 667 | + public function add_fee( $name, $amount ) { |
|
| 668 | + $amount = wpinv_sanitize_amount( $amount ); |
|
| 669 | + |
|
| 670 | + $this->total_fees_amount += $amount; |
|
| 671 | + |
|
| 672 | + if ( isset( $this->fees[ $name ] ) ) { |
|
| 673 | + $this->fees[ $name ] += $amount; |
|
| 674 | + } else { |
|
| 675 | + $this->fees[ $name ] = $amount; |
|
| 676 | + } |
|
| 677 | + |
|
| 678 | + } |
|
| 679 | + |
|
| 680 | + /** |
|
| 681 | + * Returns the total fees amount. |
|
| 682 | + * |
|
| 683 | + * @since 1.0.19 |
|
| 684 | + */ |
|
| 685 | + public function get_total_fees() { |
|
| 686 | + return $this->total_fees_amount; |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + /** |
|
| 690 | + * Retrieves a specific fee. |
|
| 691 | + * |
|
| 692 | + * @since 1.0.19 |
|
| 693 | + */ |
|
| 694 | + public function get_fee( $name ) { |
|
| 695 | + return isset( $this->fees[ $name ] ) ? $this->fees[ $name ] : 0; |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + /** |
|
| 699 | + * Returns all fees. |
|
| 700 | + * |
|
| 701 | + * @since 1.0.19 |
|
| 702 | + */ |
|
| 703 | + public function get_fees() { |
|
| 704 | + return $this->fees; |
|
| 705 | + } |
|
| 706 | + |
|
| 707 | + // MISC // |
|
| 708 | + |
|
| 709 | + /** |
|
| 710 | + * Returns the total amount to collect for this submission. |
|
| 711 | + * |
|
| 712 | + * @since 1.0.19 |
|
| 713 | + */ |
|
| 714 | + public function get_total() { |
|
| 715 | + $total = $this->subtotal_amount + $this->get_total_fees() - $this->get_total_discount() + $this->get_total_tax(); |
|
| 716 | + $total = apply_filters( 'getpaid_get_submission_total_amount', $total, $this ); |
|
| 717 | + return wpinv_sanitize_amount( $total ); |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + /** |
|
| 721 | + * Whether payment details should be collected for this submission. |
|
| 722 | + * |
|
| 723 | + * @since 1.0.19 |
|
| 724 | + */ |
|
| 725 | + public function get_payment_details() { |
|
| 726 | + $collect = $this->subtotal_amount + $this->get_total_fees() - $this->get_total_discount() + $this->get_total_tax(); |
|
| 727 | + |
|
| 728 | + if ( $this->has_recurring ) { |
|
| 729 | + $collect = true; |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + $collect = apply_filters( 'getpaid_submission_collect_payment_details', $collect, $this ); |
|
| 733 | + return $collect; |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + /** |
|
| 737 | + * Returns the billing email of the user. |
|
| 738 | + * |
|
| 739 | + * @since 1.0.19 |
|
| 740 | + */ |
|
| 741 | + public function get_billing_email() { |
|
| 742 | + $billing_email = empty( $this->data['billing_email'] ) ? '' : $this->data['billing_email']; |
|
| 743 | + return apply_filters( 'getpaid_get_submission_billing_email', $billing_email, $this ); |
|
| 744 | + } |
|
| 745 | + |
|
| 746 | + /** |
|
| 747 | + * Checks if the submitter has a billing email. |
|
| 748 | + * |
|
| 749 | + * @since 1.0.19 |
|
| 750 | + */ |
|
| 751 | + public function has_billing_email() { |
|
| 752 | + $billing_email = $this->get_billing_email(); |
|
| 753 | + return ! empty( $billing_email ); |
|
| 754 | + } |
|
| 755 | 755 | |
| 756 | 756 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 2 | +if (!defined('ABSPATH')) { |
|
| 3 | 3 | exit; |
| 4 | 4 | } |
| 5 | 5 | |
@@ -147,8 +147,8 @@ discard block |
||
| 147 | 147 | $this->state = wpinv_get_default_state(); |
| 148 | 148 | |
| 149 | 149 | // Do we have an actual submission? |
| 150 | - if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
|
| 151 | - $this->load_data( $_POST ); |
|
| 150 | + if (isset($_POST['getpaid_payment_form_submission'])) { |
|
| 151 | + $this->load_data($_POST); |
|
| 152 | 152 | } |
| 153 | 153 | } |
| 154 | 154 | |
@@ -157,29 +157,29 @@ discard block |
||
| 157 | 157 | * |
| 158 | 158 | * @param array $data |
| 159 | 159 | */ |
| 160 | - public function load_data( $data ) { |
|
| 160 | + public function load_data($data) { |
|
| 161 | 161 | |
| 162 | 162 | // Prepare submitted data... |
| 163 | - $data = wp_unslash( $data ); |
|
| 163 | + $data = wp_unslash($data); |
|
| 164 | 164 | |
| 165 | 165 | // Fitter the data. |
| 166 | - $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
|
| 166 | + $data = apply_filters('getpaid_submission_data', $data, $this); |
|
| 167 | 167 | |
| 168 | 168 | $this->data = $data; |
| 169 | 169 | |
| 170 | - $this->id = md5( wp_json_encode( $data ) ); |
|
| 170 | + $this->id = md5(wp_json_encode($data)); |
|
| 171 | 171 | |
| 172 | 172 | // Every submission needs an active payment form. |
| 173 | - if ( empty( $data['form_id'] ) ) { |
|
| 174 | - $this->last_error = __( 'Missing payment form', 'invoicing' ); |
|
| 173 | + if (empty($data['form_id'])) { |
|
| 174 | + $this->last_error = __('Missing payment form', 'invoicing'); |
|
| 175 | 175 | return; |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | // Fetch the payment form. |
| 179 | - $form = new GetPaid_Payment_Form( $data['form_id'] ); |
|
| 179 | + $form = new GetPaid_Payment_Form($data['form_id']); |
|
| 180 | 180 | |
| 181 | - if ( ! $form->is_active() ) { |
|
| 182 | - $this->last_error = __( 'Payment form not active', 'invoicing' ); |
|
| 181 | + if (!$form->is_active()) { |
|
| 182 | + $this->last_error = __('Payment form not active', 'invoicing'); |
|
| 183 | 183 | return; |
| 184 | 184 | } |
| 185 | 185 | |
@@ -187,31 +187,31 @@ discard block |
||
| 187 | 187 | $this->payment_form = $form; |
| 188 | 188 | |
| 189 | 189 | // For existing invoices, make sure that it is valid. |
| 190 | - if ( ! empty( $data['invoice_id'] ) ) { |
|
| 191 | - $invoice = wpinv_get_invoice( $data['invoice_id'] ); |
|
| 190 | + if (!empty($data['invoice_id'])) { |
|
| 191 | + $invoice = wpinv_get_invoice($data['invoice_id']); |
|
| 192 | 192 | |
| 193 | - if ( empty( $invoice ) ) { |
|
| 194 | - $this->last_error = __( 'Invalid invoice', 'invoicing' ); |
|
| 193 | + if (empty($invoice)) { |
|
| 194 | + $this->last_error = __('Invalid invoice', 'invoicing'); |
|
| 195 | 195 | return; |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - $this->payment_form->set_items( $invoice->cart_details ); |
|
| 198 | + $this->payment_form->set_items($invoice->cart_details); |
|
| 199 | 199 | |
| 200 | 200 | $this->country = $invoice->country; |
| 201 | 201 | $this->state = $invoice->state; |
| 202 | 202 | |
| 203 | 203 | // Default forms do not have items. |
| 204 | - } else if ( $form->is_default() && isset( $data['form_items'] ) ) { |
|
| 205 | - $this->payment_form->set_items( $data['form_items'] ); |
|
| 204 | + } else if ($form->is_default() && isset($data['form_items'])) { |
|
| 205 | + $this->payment_form->set_items($data['form_items']); |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | // User's country. |
| 209 | - if ( ! empty( $data['wpinv_country'] ) ) { |
|
| 209 | + if (!empty($data['wpinv_country'])) { |
|
| 210 | 210 | $this->country = $data['wpinv_country']; |
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | // User's state. |
| 214 | - if ( ! empty( $data['wpinv_state'] ) ) { |
|
| 214 | + if (!empty($data['wpinv_state'])) { |
|
| 215 | 215 | $this->country = $data['wpinv_state']; |
| 216 | 216 | } |
| 217 | 217 | |
@@ -220,47 +220,47 @@ discard block |
||
| 220 | 220 | |
| 221 | 221 | // Handle items. |
| 222 | 222 | $selected_items = array(); |
| 223 | - if ( ! empty( $data['getpaid-items'] ) ) { |
|
| 224 | - $selected_items = wpinv_clean( $data['getpaid-items'] ); |
|
| 223 | + if (!empty($data['getpaid-items'])) { |
|
| 224 | + $selected_items = wpinv_clean($data['getpaid-items']); |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | - foreach ( $this->payment_form->get_items() as $item ) { |
|
| 227 | + foreach ($this->payment_form->get_items() as $item) { |
|
| 228 | 228 | |
| 229 | 229 | // Continue if this is an optional item and it has not been selected. |
| 230 | - if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) { |
|
| 230 | + if (!$item->is_required() && !isset($selected_items[$item->get_id()])) { |
|
| 231 | 231 | continue; |
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | // (maybe) let customers change the quantities and prices. |
| 235 | - if ( isset( $selected_items[ $item->get_id() ] ) ) { |
|
| 235 | + if (isset($selected_items[$item->get_id()])) { |
|
| 236 | 236 | |
| 237 | 237 | // Maybe change the quantities. |
| 238 | - if ( $item->allows_quantities() && is_numeric( $selected_items[ $item->get_id() ]['quantity'] ) ) { |
|
| 239 | - $item->set_quantity( (int) $selected_items[ $item->get_id() ]['quantity'] ); |
|
| 238 | + if ($item->allows_quantities() && is_numeric($selected_items[$item->get_id()]['quantity'])) { |
|
| 239 | + $item->set_quantity((int) $selected_items[$item->get_id()]['quantity']); |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | // Maybe change the price. |
| 243 | - if ( $item->user_can_set_their_price() ) { |
|
| 244 | - $price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] ); |
|
| 243 | + if ($item->user_can_set_their_price()) { |
|
| 244 | + $price = (float) wpinv_sanitize_amount($selected_items[$item->get_id()]['price']); |
|
| 245 | 245 | |
| 246 | 246 | // But don't get lower than the minimum price. |
| 247 | - if ( $price < $item->get_minimum_price() ) { |
|
| 247 | + if ($price < $item->get_minimum_price()) { |
|
| 248 | 248 | $price = $item->get_minimum_price(); |
| 249 | 249 | } |
| 250 | 250 | |
| 251 | - $item->set_price( $price ); |
|
| 251 | + $item->set_price($price); |
|
| 252 | 252 | |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | // Add the item to the form. |
| 258 | - $this->add_item( $item ); |
|
| 258 | + $this->add_item($item); |
|
| 259 | 259 | |
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | // Fired when we are done processing a submission. |
| 263 | - do_action( 'getpaid_process_submission', $this ); |
|
| 263 | + do_action('getpaid_process_submission', $this); |
|
| 264 | 264 | |
| 265 | 265 | // Remove invalid discount. |
| 266 | 266 | $this->maybe_remove_discount(); |
@@ -294,7 +294,7 @@ discard block |
||
| 294 | 294 | * @return bool |
| 295 | 295 | */ |
| 296 | 296 | public function has_invoice() { |
| 297 | - return ! empty( $this->invoice ); |
|
| 297 | + return !empty($this->invoice); |
|
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | /** |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | * @return string |
| 305 | 305 | */ |
| 306 | 306 | public function get_currency() { |
| 307 | - if ( $this->has_invoice() ) { |
|
| 307 | + if ($this->has_invoice()) { |
|
| 308 | 308 | return $this->invoice->get_currency(); |
| 309 | 309 | } |
| 310 | 310 | return wpinv_get_currency(); |
@@ -325,8 +325,8 @@ discard block |
||
| 325 | 325 | * |
| 326 | 326 | * @since 1.0.19 |
| 327 | 327 | */ |
| 328 | - public function is_required_field_set( $field ) { |
|
| 329 | - return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
| 328 | + public function is_required_field_set($field) { |
|
| 329 | + return empty($field['required']) || !empty($this->data[$field['id']]); |
|
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | ///////// Items ////////////// |
@@ -337,25 +337,25 @@ discard block |
||
| 337 | 337 | * @since 1.0.19 |
| 338 | 338 | * @param GetPaid_Form_Item $item |
| 339 | 339 | */ |
| 340 | - public function add_item( $item ) { |
|
| 340 | + public function add_item($item) { |
|
| 341 | 341 | |
| 342 | 342 | // Make sure that it is available for purchase. |
| 343 | - if ( ! $item->can_purchase() ) { |
|
| 343 | + if (!$item->can_purchase()) { |
|
| 344 | 344 | return; |
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | // Do we have a recurring item? |
| 348 | - if ( $item->is_recurring() ) { |
|
| 348 | + if ($item->is_recurring()) { |
|
| 349 | 349 | $this->has_recurring = true; |
| 350 | 350 | } |
| 351 | 351 | |
| 352 | - $this->items[ $item->get_id() ] = $item; |
|
| 352 | + $this->items[$item->get_id()] = $item; |
|
| 353 | 353 | |
| 354 | 354 | $this->subtotal_amount += $item->get_sub_total(); |
| 355 | 355 | |
| 356 | - $this->process_item_discount( $item ); |
|
| 356 | + $this->process_item_discount($item); |
|
| 357 | 357 | |
| 358 | - $this->process_item_tax( $item ); |
|
| 358 | + $this->process_item_tax($item); |
|
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | /** |
@@ -363,8 +363,8 @@ discard block |
||
| 363 | 363 | * |
| 364 | 364 | * @since 1.0.19 |
| 365 | 365 | */ |
| 366 | - public function get_item( $item_id ) { |
|
| 367 | - return isset( $this->items[ $item_id ] ) ? $this->items[ $item_id ] : null; |
|
| 366 | + public function get_item($item_id) { |
|
| 367 | + return isset($this->items[$item_id]) ? $this->items[$item_id] : null; |
|
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | /** |
@@ -384,15 +384,15 @@ discard block |
||
| 384 | 384 | * |
| 385 | 385 | * @since 1.0.19 |
| 386 | 386 | */ |
| 387 | - public function add_tax( $name, $amount ) { |
|
| 388 | - $amount = (float) wpinv_sanitize_amount( $amount ); |
|
| 387 | + public function add_tax($name, $amount) { |
|
| 388 | + $amount = (float) wpinv_sanitize_amount($amount); |
|
| 389 | 389 | |
| 390 | 390 | $this->total_tax_amount += $amount; |
| 391 | 391 | |
| 392 | - if ( isset( $this->taxes[ $name ] ) ) { |
|
| 393 | - $this->taxes[ $name ] += $amount; |
|
| 392 | + if (isset($this->taxes[$name])) { |
|
| 393 | + $this->taxes[$name] += $amount; |
|
| 394 | 394 | } else { |
| 395 | - $this->taxes[ $name ] = $amount; |
|
| 395 | + $this->taxes[$name] = $amount; |
|
| 396 | 396 | } |
| 397 | 397 | |
| 398 | 398 | } |
@@ -406,11 +406,11 @@ discard block |
||
| 406 | 406 | |
| 407 | 407 | $use_taxes = wpinv_use_taxes(); |
| 408 | 408 | |
| 409 | - if ( $this->has_invoice() && $this->invoice->disable_taxes ) { |
|
| 409 | + if ($this->has_invoice() && $this->invoice->disable_taxes) { |
|
| 410 | 410 | $use_taxes = false; |
| 411 | 411 | } |
| 412 | 412 | |
| 413 | - return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
|
| 413 | + return apply_filters('getpaid_submission_use_taxes', $use_taxes, $this); |
|
| 414 | 414 | |
| 415 | 415 | } |
| 416 | 416 | |
@@ -420,23 +420,23 @@ discard block |
||
| 420 | 420 | * @since 1.0.19 |
| 421 | 421 | * @param GetPaid_Form_Item $item |
| 422 | 422 | */ |
| 423 | - public function process_item_tax( $item ) { |
|
| 423 | + public function process_item_tax($item) { |
|
| 424 | 424 | |
| 425 | 425 | // Abort early if we're not using taxes. |
| 426 | - if ( ! $this->use_taxes() ) { |
|
| 426 | + if (!$this->use_taxes()) { |
|
| 427 | 427 | return; |
| 428 | 428 | } |
| 429 | 429 | |
| 430 | - $rate = wpinv_get_tax_rate( $this->country, $this->state, $item->get_id() ); |
|
| 430 | + $rate = wpinv_get_tax_rate($this->country, $this->state, $item->get_id()); |
|
| 431 | 431 | $price = $item->get_sub_total(); |
| 432 | 432 | |
| 433 | - if ( wpinv_prices_include_tax() ) { |
|
| 434 | - $item_tax = $price - ( $price - $price * $rate * 0.01 ); |
|
| 433 | + if (wpinv_prices_include_tax()) { |
|
| 434 | + $item_tax = $price - ($price - $price * $rate * 0.01); |
|
| 435 | 435 | } else { |
| 436 | 436 | $item_tax = $price * $rate * 0.01; |
| 437 | 437 | } |
| 438 | 438 | |
| 439 | - $this->add_tax( 'Tax', $item_tax ); |
|
| 439 | + $this->add_tax('Tax', $item_tax); |
|
| 440 | 440 | |
| 441 | 441 | } |
| 442 | 442 | |
@@ -454,8 +454,8 @@ discard block |
||
| 454 | 454 | * |
| 455 | 455 | * @since 1.0.19 |
| 456 | 456 | */ |
| 457 | - public function get_tax( $name ) { |
|
| 458 | - return isset( $this->taxes[ $name ] ) ? $this->taxes[ $name ] : 0; |
|
| 457 | + public function get_tax($name) { |
|
| 458 | + return isset($this->taxes[$name]) ? $this->taxes[$name] : 0; |
|
| 459 | 459 | } |
| 460 | 460 | |
| 461 | 461 | /** |
@@ -474,15 +474,15 @@ discard block |
||
| 474 | 474 | * |
| 475 | 475 | * @since 1.0.19 |
| 476 | 476 | */ |
| 477 | - public function add_discount( $name, $amount ) { |
|
| 478 | - $amount = wpinv_sanitize_amount( $amount ); |
|
| 477 | + public function add_discount($name, $amount) { |
|
| 478 | + $amount = wpinv_sanitize_amount($amount); |
|
| 479 | 479 | |
| 480 | 480 | $this->total_discount_amount += $amount; |
| 481 | 481 | |
| 482 | - if ( isset( $this->discounts[ $name ] ) ) { |
|
| 483 | - $this->discounts[ $name ] += $amount; |
|
| 482 | + if (isset($this->discounts[$name])) { |
|
| 483 | + $this->discounts[$name] += $amount; |
|
| 484 | 484 | } else { |
| 485 | - $this->discounts[ $name ] = $amount; |
|
| 485 | + $this->discounts[$name] = $amount; |
|
| 486 | 486 | } |
| 487 | 487 | |
| 488 | 488 | } |
@@ -492,11 +492,11 @@ discard block |
||
| 492 | 492 | * |
| 493 | 493 | * @since 1.0.19 |
| 494 | 494 | */ |
| 495 | - public function remove_discount( $name ) { |
|
| 495 | + public function remove_discount($name) { |
|
| 496 | 496 | |
| 497 | - if ( isset( $this->discounts[ $name ] ) ) { |
|
| 498 | - $this->total_discount_amount -= $this->discounts[ $name ]; |
|
| 499 | - unset( $this->discounts[ $name ] ); |
|
| 497 | + if (isset($this->discounts[$name])) { |
|
| 498 | + $this->total_discount_amount -= $this->discounts[$name]; |
|
| 499 | + unset($this->discounts[$name]); |
|
| 500 | 500 | } |
| 501 | 501 | |
| 502 | 502 | } |
@@ -508,7 +508,7 @@ discard block |
||
| 508 | 508 | * @return bool |
| 509 | 509 | */ |
| 510 | 510 | public function has_discount_code() { |
| 511 | - return ! empty( $this->discount ); |
|
| 511 | + return !empty($this->discount); |
|
| 512 | 512 | } |
| 513 | 513 | |
| 514 | 514 | /** |
@@ -529,32 +529,32 @@ discard block |
||
| 529 | 529 | public function maybe_prepare_discount() { |
| 530 | 530 | |
| 531 | 531 | // Do we have a discount? |
| 532 | - if ( empty( $this->data['discount'] ) ) { |
|
| 532 | + if (empty($this->data['discount'])) { |
|
| 533 | 533 | return; |
| 534 | 534 | } |
| 535 | 535 | |
| 536 | 536 | // Fetch the discount. |
| 537 | - $discount = wpinv_get_discount_obj( $this->data['discount'] ); |
|
| 537 | + $discount = wpinv_get_discount_obj($this->data['discount']); |
|
| 538 | 538 | |
| 539 | 539 | // Ensure it is active. |
| 540 | - if ( ! $discount->exists() || ! $discount->is_active() || ! $discount->has_started() || $discount->is_expired() ) { |
|
| 540 | + if (!$discount->exists() || !$discount->is_active() || !$discount->has_started() || $discount->is_expired()) { |
|
| 541 | 541 | $this->is_discount_valid = false; |
| 542 | - $this->last_error = __( 'Invalid or expired discount code', 'invoicing' ); |
|
| 542 | + $this->last_error = __('Invalid or expired discount code', 'invoicing'); |
|
| 543 | 543 | return; |
| 544 | 544 | } |
| 545 | 545 | |
| 546 | 546 | // For single use discounts... |
| 547 | - if ( $discount->is_single_use ) { |
|
| 547 | + if ($discount->is_single_use) { |
|
| 548 | 548 | |
| 549 | - if ( ! $this->has_billing_email() ) { |
|
| 549 | + if (!$this->has_billing_email()) { |
|
| 550 | 550 | $this->is_discount_valid = false; |
| 551 | - $this->last_error = __( 'You need to enter your billing email before applying this discount', 'invoicing' ); |
|
| 551 | + $this->last_error = __('You need to enter your billing email before applying this discount', 'invoicing'); |
|
| 552 | 552 | return; |
| 553 | 553 | } |
| 554 | 554 | |
| 555 | - if ( ! $discount->is_valid_for_user( $this->get_billing_email() ) ) { |
|
| 555 | + if (!$discount->is_valid_for_user($this->get_billing_email())) { |
|
| 556 | 556 | $this->is_discount_valid = false; |
| 557 | - $this->last_error = __( 'You have already used this discount', 'invoicing' ); |
|
| 557 | + $this->last_error = __('You have already used this discount', 'invoicing'); |
|
| 558 | 558 | return; |
| 559 | 559 | } |
| 560 | 560 | } |
@@ -572,35 +572,35 @@ discard block |
||
| 572 | 572 | public function maybe_remove_discount() { |
| 573 | 573 | |
| 574 | 574 | // Do we have a discount? |
| 575 | - if ( empty( $this->has_discount_code() ) ) { |
|
| 575 | + if (empty($this->has_discount_code())) { |
|
| 576 | 576 | return; |
| 577 | 577 | } |
| 578 | 578 | |
| 579 | 579 | // Fetch the discount amount. |
| 580 | - $amount = $this->get_discount( 'Discount' ); |
|
| 580 | + $amount = $this->get_discount('Discount'); |
|
| 581 | 581 | |
| 582 | 582 | // Abort early if this is a "zero" discount. |
| 583 | - if ( empty( $amount ) ) { |
|
| 583 | + if (empty($amount)) { |
|
| 584 | 584 | return; |
| 585 | 585 | } |
| 586 | 586 | |
| 587 | 587 | $total = $this->subtotal_amount + $this->get_total_fees() + $this->get_total_tax(); |
| 588 | 588 | |
| 589 | - if ( ! $this->discount->is_minimum_amount_met( $total ) ) { |
|
| 589 | + if (!$this->discount->is_minimum_amount_met($total)) { |
|
| 590 | 590 | $this->is_discount_valid = false; |
| 591 | - $min = wpinv_price( wpinv_format_amount( $$this->discount->min_total ) ); |
|
| 592 | - $this->last_error = sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ); |
|
| 591 | + $min = wpinv_price(wpinv_format_amount($$this->discount->min_total)); |
|
| 592 | + $this->last_error = sprintf(__('The minimum total for using this discount is %s', 'invoicing'), $min); |
|
| 593 | 593 | } |
| 594 | 594 | |
| 595 | - if ( ! $$this->discount->is_maximum_amount_met( $total ) ) { |
|
| 595 | + if (!$$this->discount->is_maximum_amount_met($total)) { |
|
| 596 | 596 | $this->is_discount_valid = false; |
| 597 | - $max = wpinv_price( wpinv_format_amount( $$this->discount->max_total ) ); |
|
| 598 | - $this->last_error = sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ); |
|
| 597 | + $max = wpinv_price(wpinv_format_amount($$this->discount->max_total)); |
|
| 598 | + $this->last_error = sprintf(__('The maximum total for using this discount is %s', 'invoicing'), $max); |
|
| 599 | 599 | } |
| 600 | 600 | |
| 601 | - if ( ! $this->is_discount_valid ) { |
|
| 601 | + if (!$this->is_discount_valid) { |
|
| 602 | 602 | $this->discount = null; |
| 603 | - $this->remove_discount( 'Discount' ); |
|
| 603 | + $this->remove_discount('Discount'); |
|
| 604 | 604 | } |
| 605 | 605 | |
| 606 | 606 | } |
@@ -611,22 +611,22 @@ discard block |
||
| 611 | 611 | * @since 1.0.19 |
| 612 | 612 | * @param GetPaid_Form_Item $item |
| 613 | 613 | */ |
| 614 | - public function process_item_discount( $item ) { |
|
| 614 | + public function process_item_discount($item) { |
|
| 615 | 615 | |
| 616 | 616 | // Abort early if there is no discount. |
| 617 | - if ( ! $this->has_discount_code() ) { |
|
| 617 | + if (!$this->has_discount_code()) { |
|
| 618 | 618 | return; |
| 619 | 619 | } |
| 620 | 620 | |
| 621 | 621 | // Ensure that it is valid for this item. |
| 622 | - if ( ! $this->discount->is_valid_for_items( array( $item->get_id() ) ) ) { |
|
| 622 | + if (!$this->discount->is_valid_for_items(array($item->get_id()))) { |
|
| 623 | 623 | return; |
| 624 | 624 | } |
| 625 | 625 | |
| 626 | 626 | // Fetch the discounted amount. |
| 627 | - $discount = $this->discount->get_discounted_amount( $item->get_price() * $item->get_qantity() ); |
|
| 627 | + $discount = $this->discount->get_discounted_amount($item->get_price() * $item->get_qantity()); |
|
| 628 | 628 | |
| 629 | - $this->add_discount( 'Discount', $discount ); |
|
| 629 | + $this->add_discount('Discount', $discount); |
|
| 630 | 630 | |
| 631 | 631 | } |
| 632 | 632 | |
@@ -644,8 +644,8 @@ discard block |
||
| 644 | 644 | * |
| 645 | 645 | * @since 1.0.19 |
| 646 | 646 | */ |
| 647 | - public function get_discount( $name ) { |
|
| 648 | - return isset( $this->discounts[ $name ] ) ? $this->discounts[ $name ] : 0; |
|
| 647 | + public function get_discount($name) { |
|
| 648 | + return isset($this->discounts[$name]) ? $this->discounts[$name] : 0; |
|
| 649 | 649 | } |
| 650 | 650 | |
| 651 | 651 | /** |
@@ -664,15 +664,15 @@ discard block |
||
| 664 | 664 | * |
| 665 | 665 | * @since 1.0.19 |
| 666 | 666 | */ |
| 667 | - public function add_fee( $name, $amount ) { |
|
| 668 | - $amount = wpinv_sanitize_amount( $amount ); |
|
| 667 | + public function add_fee($name, $amount) { |
|
| 668 | + $amount = wpinv_sanitize_amount($amount); |
|
| 669 | 669 | |
| 670 | 670 | $this->total_fees_amount += $amount; |
| 671 | 671 | |
| 672 | - if ( isset( $this->fees[ $name ] ) ) { |
|
| 673 | - $this->fees[ $name ] += $amount; |
|
| 672 | + if (isset($this->fees[$name])) { |
|
| 673 | + $this->fees[$name] += $amount; |
|
| 674 | 674 | } else { |
| 675 | - $this->fees[ $name ] = $amount; |
|
| 675 | + $this->fees[$name] = $amount; |
|
| 676 | 676 | } |
| 677 | 677 | |
| 678 | 678 | } |
@@ -691,8 +691,8 @@ discard block |
||
| 691 | 691 | * |
| 692 | 692 | * @since 1.0.19 |
| 693 | 693 | */ |
| 694 | - public function get_fee( $name ) { |
|
| 695 | - return isset( $this->fees[ $name ] ) ? $this->fees[ $name ] : 0; |
|
| 694 | + public function get_fee($name) { |
|
| 695 | + return isset($this->fees[$name]) ? $this->fees[$name] : 0; |
|
| 696 | 696 | } |
| 697 | 697 | |
| 698 | 698 | /** |
@@ -713,8 +713,8 @@ discard block |
||
| 713 | 713 | */ |
| 714 | 714 | public function get_total() { |
| 715 | 715 | $total = $this->subtotal_amount + $this->get_total_fees() - $this->get_total_discount() + $this->get_total_tax(); |
| 716 | - $total = apply_filters( 'getpaid_get_submission_total_amount', $total, $this ); |
|
| 717 | - return wpinv_sanitize_amount( $total ); |
|
| 716 | + $total = apply_filters('getpaid_get_submission_total_amount', $total, $this); |
|
| 717 | + return wpinv_sanitize_amount($total); |
|
| 718 | 718 | } |
| 719 | 719 | |
| 720 | 720 | /** |
@@ -725,11 +725,11 @@ discard block |
||
| 725 | 725 | public function get_payment_details() { |
| 726 | 726 | $collect = $this->subtotal_amount + $this->get_total_fees() - $this->get_total_discount() + $this->get_total_tax(); |
| 727 | 727 | |
| 728 | - if ( $this->has_recurring ) { |
|
| 728 | + if ($this->has_recurring) { |
|
| 729 | 729 | $collect = true; |
| 730 | 730 | } |
| 731 | 731 | |
| 732 | - $collect = apply_filters( 'getpaid_submission_collect_payment_details', $collect, $this ); |
|
| 732 | + $collect = apply_filters('getpaid_submission_collect_payment_details', $collect, $this); |
|
| 733 | 733 | return $collect; |
| 734 | 734 | } |
| 735 | 735 | |
@@ -739,8 +739,8 @@ discard block |
||
| 739 | 739 | * @since 1.0.19 |
| 740 | 740 | */ |
| 741 | 741 | public function get_billing_email() { |
| 742 | - $billing_email = empty( $this->data['billing_email'] ) ? '' : $this->data['billing_email']; |
|
| 743 | - return apply_filters( 'getpaid_get_submission_billing_email', $billing_email, $this ); |
|
| 742 | + $billing_email = empty($this->data['billing_email']) ? '' : $this->data['billing_email']; |
|
| 743 | + return apply_filters('getpaid_get_submission_billing_email', $billing_email, $this); |
|
| 744 | 744 | } |
| 745 | 745 | |
| 746 | 746 | /** |
@@ -750,7 +750,7 @@ discard block |
||
| 750 | 750 | */ |
| 751 | 751 | public function has_billing_email() { |
| 752 | 752 | $billing_email = $this->get_billing_email(); |
| 753 | - return ! empty( $billing_email ); |
|
| 753 | + return !empty($billing_email); |
|
| 754 | 754 | } |
| 755 | 755 | |
| 756 | 756 | } |
@@ -15,30 +15,30 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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,440 +294,440 @@ discard block |
||
| 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 | - } |
|
| 499 | - |
|
| 500 | - function wpinv_format_discount_rate( $type, $amount ) { |
|
| 501 | - if ( $type == 'flat' ) { |
|
| 502 | - $rate = wpinv_price( wpinv_format_amount( $amount ) ); |
|
| 503 | - } else { |
|
| 504 | - $rate = $amount . '%'; |
|
| 505 | - } |
|
| 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 | + |
|
| 500 | + function wpinv_format_discount_rate( $type, $amount ) { |
|
| 501 | + if ( $type == 'flat' ) { |
|
| 502 | + $rate = wpinv_price( wpinv_format_amount( $amount ) ); |
|
| 503 | + } else { |
|
| 504 | + $rate = $amount . '%'; |
|
| 505 | + } |
|
| 506 | 506 | |
| 507 | - return apply_filters( 'wpinv_format_discount_rate', $rate, $type, $amount ); |
|
| 508 | - } |
|
| 507 | + return apply_filters( 'wpinv_format_discount_rate', $rate, $type, $amount ); |
|
| 508 | + } |
|
| 509 | 509 | |
| 510 | - /** |
|
| 511 | - * Get the discount's start date. |
|
| 512 | - * |
|
| 513 | - * @since 1.0.19 |
|
| 514 | - * @param string $context View or edit context. |
|
| 515 | - * @return string |
|
| 516 | - */ |
|
| 517 | - public function get_start( $context = 'view' ) { |
|
| 518 | - return $this->get_prop( 'start', $context ); |
|
| 519 | - } |
|
| 510 | + /** |
|
| 511 | + * Get the discount's start date. |
|
| 512 | + * |
|
| 513 | + * @since 1.0.19 |
|
| 514 | + * @param string $context View or edit context. |
|
| 515 | + * @return string |
|
| 516 | + */ |
|
| 517 | + public function get_start( $context = 'view' ) { |
|
| 518 | + return $this->get_prop( 'start', $context ); |
|
| 519 | + } |
|
| 520 | 520 | |
| 521 | - /** |
|
| 522 | - * Alias for self::get_start(). |
|
| 523 | - * |
|
| 524 | - * @since 1.0.19 |
|
| 525 | - * @param string $context View or edit context. |
|
| 526 | - * @return string |
|
| 527 | - */ |
|
| 528 | - public function get_start_date( $context = 'view' ) { |
|
| 529 | - return $this->get_start( $context ); |
|
| 530 | - } |
|
| 521 | + /** |
|
| 522 | + * Alias for self::get_start(). |
|
| 523 | + * |
|
| 524 | + * @since 1.0.19 |
|
| 525 | + * @param string $context View or edit context. |
|
| 526 | + * @return string |
|
| 527 | + */ |
|
| 528 | + public function get_start_date( $context = 'view' ) { |
|
| 529 | + return $this->get_start( $context ); |
|
| 530 | + } |
|
| 531 | 531 | |
| 532 | - /** |
|
| 533 | - * Get the discount's expiration date. |
|
| 534 | - * |
|
| 535 | - * @since 1.0.19 |
|
| 536 | - * @param string $context View or edit context. |
|
| 537 | - * @return string |
|
| 538 | - */ |
|
| 539 | - public function get_expiration( $context = 'view' ) { |
|
| 540 | - return $this->get_prop( 'expiration', $context ); |
|
| 541 | - } |
|
| 532 | + /** |
|
| 533 | + * Get the discount's expiration date. |
|
| 534 | + * |
|
| 535 | + * @since 1.0.19 |
|
| 536 | + * @param string $context View or edit context. |
|
| 537 | + * @return string |
|
| 538 | + */ |
|
| 539 | + public function get_expiration( $context = 'view' ) { |
|
| 540 | + return $this->get_prop( 'expiration', $context ); |
|
| 541 | + } |
|
| 542 | 542 | |
| 543 | - /** |
|
| 544 | - * Alias for self::get_expiration(). |
|
| 545 | - * |
|
| 546 | - * @since 1.0.19 |
|
| 547 | - * @param string $context View or edit context. |
|
| 548 | - * @return string |
|
| 549 | - */ |
|
| 550 | - public function get_expiration_date( $context = 'view' ) { |
|
| 551 | - return $this->get_expiration( $context ); |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * Alias for self::get_expiration(). |
|
| 556 | - * |
|
| 557 | - * @since 1.0.19 |
|
| 558 | - * @param string $context View or edit context. |
|
| 559 | - * @return string |
|
| 560 | - */ |
|
| 561 | - public function get_end_date( $context = 'view' ) { |
|
| 562 | - return $this->get_expiration( $context ); |
|
| 563 | - } |
|
| 543 | + /** |
|
| 544 | + * Alias for self::get_expiration(). |
|
| 545 | + * |
|
| 546 | + * @since 1.0.19 |
|
| 547 | + * @param string $context View or edit context. |
|
| 548 | + * @return string |
|
| 549 | + */ |
|
| 550 | + public function get_expiration_date( $context = 'view' ) { |
|
| 551 | + return $this->get_expiration( $context ); |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * Alias for self::get_expiration(). |
|
| 556 | + * |
|
| 557 | + * @since 1.0.19 |
|
| 558 | + * @param string $context View or edit context. |
|
| 559 | + * @return string |
|
| 560 | + */ |
|
| 561 | + public function get_end_date( $context = 'view' ) { |
|
| 562 | + return $this->get_expiration( $context ); |
|
| 563 | + } |
|
| 564 | 564 | |
| 565 | - /** |
|
| 566 | - * Get the discount's type. |
|
| 567 | - * |
|
| 568 | - * @since 1.0.19 |
|
| 569 | - * @param string $context View or edit context. |
|
| 570 | - * @return string |
|
| 571 | - */ |
|
| 572 | - public function get_type( $context = 'view' ) { |
|
| 573 | - return $this->get_prop( 'type', $context ); |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - /** |
|
| 577 | - * Get the number of times a discount has been used. |
|
| 578 | - * |
|
| 579 | - * @since 1.0.19 |
|
| 580 | - * @param string $context View or edit context. |
|
| 581 | - * @return int |
|
| 582 | - */ |
|
| 583 | - public function get_uses( $context = 'view' ) { |
|
| 584 | - return (int) $this->get_prop( 'uses', $context ); |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * Get the discount's usage, i.e uses / max uses. |
|
| 589 | - * |
|
| 590 | - * @since 1.0.19 |
|
| 591 | - * @return string |
|
| 592 | - */ |
|
| 593 | - public function get_usage() { |
|
| 594 | - |
|
| 595 | - if ( ! $this->has_limit() ) { |
|
| 596 | - return $this->get_uses() . ' / ' . ' ∞'; |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - return $this->get_uses() . ' / ' . (int) $this->get_max_uses(); |
|
| 600 | - |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - /** |
|
| 604 | - * Get the maximum number of time a discount can be used. |
|
| 605 | - * |
|
| 606 | - * @since 1.0.19 |
|
| 607 | - * @param string $context View or edit context. |
|
| 608 | - * @return int |
|
| 609 | - */ |
|
| 610 | - public function get_max_uses( $context = 'view' ) { |
|
| 611 | - $max_uses = $this->get_prop( 'max_uses', $context ); |
|
| 612 | - return empty( $max_uses ) ? null : $max_uses; |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * Checks if this is a single use discount or not. |
|
| 617 | - * |
|
| 618 | - * @since 1.0.19 |
|
| 619 | - * @param string $context View or edit context. |
|
| 620 | - * @return bool |
|
| 621 | - */ |
|
| 622 | - public function get_is_single_use( $context = 'view' ) { |
|
| 623 | - return $this->get_prop( 'is_single_use', $context ); |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * Get the items that can be used with this discount. |
|
| 628 | - * |
|
| 629 | - * @since 1.0.19 |
|
| 630 | - * @param string $context View or edit context. |
|
| 631 | - * @return array |
|
| 632 | - */ |
|
| 633 | - public function get_items( $context = 'view' ) { |
|
| 634 | - return wpinv_parse_list( $this->get_prop( 'items', $context ) ); |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - /** |
|
| 638 | - * Alias for self::get_items(). |
|
| 639 | - * |
|
| 640 | - * @since 1.0.19 |
|
| 641 | - * @param string $context View or edit context. |
|
| 642 | - * @return array |
|
| 643 | - */ |
|
| 644 | - public function get_allowed_items( $context = 'view' ) { |
|
| 645 | - return $this->get_items( $context ); |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - /** |
|
| 649 | - * Get the items that are not allowed to use this discount. |
|
| 650 | - * |
|
| 651 | - * @since 1.0.19 |
|
| 652 | - * @param string $context View or edit context. |
|
| 653 | - * @return array |
|
| 654 | - */ |
|
| 655 | - public function get_excluded_items( $context = 'view' ) { |
|
| 656 | - return wpinv_parse_list( $this->get_prop( 'excluded_items', $context ) ); |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - /** |
|
| 660 | - * Checks if this is a recurring discount or not. |
|
| 661 | - * |
|
| 662 | - * @since 1.0.19 |
|
| 663 | - * @param string $context View or edit context. |
|
| 664 | - * @return int|string|bool |
|
| 665 | - */ |
|
| 666 | - public function get_is_recurring( $context = 'view' ) { |
|
| 667 | - return $this->get_prop( 'is_recurring', $context ); |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - /** |
|
| 671 | - * Get's the minimum total amount allowed for this discount. |
|
| 672 | - * |
|
| 673 | - * @since 1.0.19 |
|
| 674 | - * @param string $context View or edit context. |
|
| 675 | - * @return float |
|
| 676 | - */ |
|
| 677 | - public function get_min_total( $context = 'view' ) { |
|
| 678 | - $minimum = $this->get_prop( 'min_total', $context ); |
|
| 679 | - return empty( $minimum ) ? null : $minimum; |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - /** |
|
| 683 | - * Alias for self::get_min_total(). |
|
| 684 | - * |
|
| 685 | - * @since 1.0.19 |
|
| 686 | - * @param string $context View or edit context. |
|
| 687 | - * @return float |
|
| 688 | - */ |
|
| 689 | - public function get_minimum_total( $context = 'view' ) { |
|
| 690 | - return $this->get_min_total( $context ); |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - /** |
|
| 694 | - * Get's the maximum total amount allowed for this discount. |
|
| 695 | - * |
|
| 696 | - * @since 1.0.19 |
|
| 697 | - * @param string $context View or edit context. |
|
| 698 | - * @return float |
|
| 699 | - */ |
|
| 700 | - public function get_max_total( $context = 'view' ) { |
|
| 701 | - $maximum = $this->get_prop( 'max_total', $context ); |
|
| 702 | - return empty( $maximum ) ? null : $maximum; |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - /** |
|
| 706 | - * Alias for self::get_max_total(). |
|
| 707 | - * |
|
| 708 | - * @since 1.0.19 |
|
| 709 | - * @param string $context View or edit context. |
|
| 710 | - * @return float |
|
| 711 | - */ |
|
| 712 | - public function get_maximum_total( $context = 'view' ) { |
|
| 713 | - return $this->get_max_total( $context ); |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - /** |
|
| 717 | - * Magic method for accessing discount properties. |
|
| 718 | - * |
|
| 719 | - * @since 1.0.15 |
|
| 720 | - * @access public |
|
| 721 | - * |
|
| 722 | - * @param string $key Discount data to retrieve |
|
| 723 | - * @param string $context View or edit context. |
|
| 724 | - * @return mixed Value of the given discount property (if set). |
|
| 725 | - */ |
|
| 726 | - public function get( $key, $context = 'view' ) { |
|
| 565 | + /** |
|
| 566 | + * Get the discount's type. |
|
| 567 | + * |
|
| 568 | + * @since 1.0.19 |
|
| 569 | + * @param string $context View or edit context. |
|
| 570 | + * @return string |
|
| 571 | + */ |
|
| 572 | + public function get_type( $context = 'view' ) { |
|
| 573 | + return $this->get_prop( 'type', $context ); |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + /** |
|
| 577 | + * Get the number of times a discount has been used. |
|
| 578 | + * |
|
| 579 | + * @since 1.0.19 |
|
| 580 | + * @param string $context View or edit context. |
|
| 581 | + * @return int |
|
| 582 | + */ |
|
| 583 | + public function get_uses( $context = 'view' ) { |
|
| 584 | + return (int) $this->get_prop( 'uses', $context ); |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * Get the discount's usage, i.e uses / max uses. |
|
| 589 | + * |
|
| 590 | + * @since 1.0.19 |
|
| 591 | + * @return string |
|
| 592 | + */ |
|
| 593 | + public function get_usage() { |
|
| 594 | + |
|
| 595 | + if ( ! $this->has_limit() ) { |
|
| 596 | + return $this->get_uses() . ' / ' . ' ∞'; |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + return $this->get_uses() . ' / ' . (int) $this->get_max_uses(); |
|
| 600 | + |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + /** |
|
| 604 | + * Get the maximum number of time a discount can be used. |
|
| 605 | + * |
|
| 606 | + * @since 1.0.19 |
|
| 607 | + * @param string $context View or edit context. |
|
| 608 | + * @return int |
|
| 609 | + */ |
|
| 610 | + public function get_max_uses( $context = 'view' ) { |
|
| 611 | + $max_uses = $this->get_prop( 'max_uses', $context ); |
|
| 612 | + return empty( $max_uses ) ? null : $max_uses; |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * Checks if this is a single use discount or not. |
|
| 617 | + * |
|
| 618 | + * @since 1.0.19 |
|
| 619 | + * @param string $context View or edit context. |
|
| 620 | + * @return bool |
|
| 621 | + */ |
|
| 622 | + public function get_is_single_use( $context = 'view' ) { |
|
| 623 | + return $this->get_prop( 'is_single_use', $context ); |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * Get the items that can be used with this discount. |
|
| 628 | + * |
|
| 629 | + * @since 1.0.19 |
|
| 630 | + * @param string $context View or edit context. |
|
| 631 | + * @return array |
|
| 632 | + */ |
|
| 633 | + public function get_items( $context = 'view' ) { |
|
| 634 | + return wpinv_parse_list( $this->get_prop( 'items', $context ) ); |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + /** |
|
| 638 | + * Alias for self::get_items(). |
|
| 639 | + * |
|
| 640 | + * @since 1.0.19 |
|
| 641 | + * @param string $context View or edit context. |
|
| 642 | + * @return array |
|
| 643 | + */ |
|
| 644 | + public function get_allowed_items( $context = 'view' ) { |
|
| 645 | + return $this->get_items( $context ); |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + /** |
|
| 649 | + * Get the items that are not allowed to use this discount. |
|
| 650 | + * |
|
| 651 | + * @since 1.0.19 |
|
| 652 | + * @param string $context View or edit context. |
|
| 653 | + * @return array |
|
| 654 | + */ |
|
| 655 | + public function get_excluded_items( $context = 'view' ) { |
|
| 656 | + return wpinv_parse_list( $this->get_prop( 'excluded_items', $context ) ); |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + /** |
|
| 660 | + * Checks if this is a recurring discount or not. |
|
| 661 | + * |
|
| 662 | + * @since 1.0.19 |
|
| 663 | + * @param string $context View or edit context. |
|
| 664 | + * @return int|string|bool |
|
| 665 | + */ |
|
| 666 | + public function get_is_recurring( $context = 'view' ) { |
|
| 667 | + return $this->get_prop( 'is_recurring', $context ); |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + /** |
|
| 671 | + * Get's the minimum total amount allowed for this discount. |
|
| 672 | + * |
|
| 673 | + * @since 1.0.19 |
|
| 674 | + * @param string $context View or edit context. |
|
| 675 | + * @return float |
|
| 676 | + */ |
|
| 677 | + public function get_min_total( $context = 'view' ) { |
|
| 678 | + $minimum = $this->get_prop( 'min_total', $context ); |
|
| 679 | + return empty( $minimum ) ? null : $minimum; |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + /** |
|
| 683 | + * Alias for self::get_min_total(). |
|
| 684 | + * |
|
| 685 | + * @since 1.0.19 |
|
| 686 | + * @param string $context View or edit context. |
|
| 687 | + * @return float |
|
| 688 | + */ |
|
| 689 | + public function get_minimum_total( $context = 'view' ) { |
|
| 690 | + return $this->get_min_total( $context ); |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + /** |
|
| 694 | + * Get's the maximum total amount allowed for this discount. |
|
| 695 | + * |
|
| 696 | + * @since 1.0.19 |
|
| 697 | + * @param string $context View or edit context. |
|
| 698 | + * @return float |
|
| 699 | + */ |
|
| 700 | + public function get_max_total( $context = 'view' ) { |
|
| 701 | + $maximum = $this->get_prop( 'max_total', $context ); |
|
| 702 | + return empty( $maximum ) ? null : $maximum; |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + /** |
|
| 706 | + * Alias for self::get_max_total(). |
|
| 707 | + * |
|
| 708 | + * @since 1.0.19 |
|
| 709 | + * @param string $context View or edit context. |
|
| 710 | + * @return float |
|
| 711 | + */ |
|
| 712 | + public function get_maximum_total( $context = 'view' ) { |
|
| 713 | + return $this->get_max_total( $context ); |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + /** |
|
| 717 | + * Magic method for accessing discount properties. |
|
| 718 | + * |
|
| 719 | + * @since 1.0.15 |
|
| 720 | + * @access public |
|
| 721 | + * |
|
| 722 | + * @param string $key Discount data to retrieve |
|
| 723 | + * @param string $context View or edit context. |
|
| 724 | + * @return mixed Value of the given discount property (if set). |
|
| 725 | + */ |
|
| 726 | + public function get( $key, $context = 'view' ) { |
|
| 727 | 727 | return $this->get_prop( $key, $context ); |
| 728 | - } |
|
| 728 | + } |
|
| 729 | 729 | |
| 730 | - /* |
|
| 730 | + /* |
|
| 731 | 731 | |-------------------------------------------------------------------------- |
| 732 | 732 | | Setters |
| 733 | 733 | |-------------------------------------------------------------------------- |
@@ -737,41 +737,41 @@ discard block |
||
| 737 | 737 | | object. |
| 738 | 738 | */ |
| 739 | 739 | |
| 740 | - /** |
|
| 741 | - * Sets discount status. |
|
| 742 | - * |
|
| 743 | - * @since 1.0.19 |
|
| 744 | - * @param string $status New status. |
|
| 745 | - * @return array details of change. |
|
| 746 | - */ |
|
| 747 | - public function set_status( $status ) { |
|
| 740 | + /** |
|
| 741 | + * Sets discount status. |
|
| 742 | + * |
|
| 743 | + * @since 1.0.19 |
|
| 744 | + * @param string $status New status. |
|
| 745 | + * @return array details of change. |
|
| 746 | + */ |
|
| 747 | + public function set_status( $status ) { |
|
| 748 | 748 | $old_status = $this->get_status(); |
| 749 | 749 | |
| 750 | 750 | $this->set_prop( 'status', $status ); |
| 751 | 751 | |
| 752 | - return array( |
|
| 753 | - 'from' => $old_status, |
|
| 754 | - 'to' => $status, |
|
| 755 | - ); |
|
| 752 | + return array( |
|
| 753 | + 'from' => $old_status, |
|
| 754 | + 'to' => $status, |
|
| 755 | + ); |
|
| 756 | 756 | } |
| 757 | 757 | |
| 758 | 758 | /** |
| 759 | - * Set plugin version when the discount was created. |
|
| 760 | - * |
|
| 761 | - * @since 1.0.19 |
|
| 762 | - */ |
|
| 763 | - public function set_version( $value ) { |
|
| 764 | - $this->set_prop( 'version', $value ); |
|
| 759 | + * Set plugin version when the discount was created. |
|
| 760 | + * |
|
| 761 | + * @since 1.0.19 |
|
| 762 | + */ |
|
| 763 | + public function set_version( $value ) { |
|
| 764 | + $this->set_prop( 'version', $value ); |
|
| 765 | 765 | } |
| 766 | 766 | |
| 767 | 767 | /** |
| 768 | - * Set date when the discount was created. |
|
| 769 | - * |
|
| 770 | - * @since 1.0.19 |
|
| 771 | - * @param string $value Value to set. |
|
| 768 | + * Set date when the discount was created. |
|
| 769 | + * |
|
| 770 | + * @since 1.0.19 |
|
| 771 | + * @param string $value Value to set. |
|
| 772 | 772 | * @return bool Whether or not the date was set. |
| 773 | - */ |
|
| 774 | - public function set_date_created( $value ) { |
|
| 773 | + */ |
|
| 774 | + public function set_date_created( $value ) { |
|
| 775 | 775 | $date = strtotime( $value ); |
| 776 | 776 | |
| 777 | 777 | if ( $date ) { |
@@ -784,13 +784,13 @@ discard block |
||
| 784 | 784 | } |
| 785 | 785 | |
| 786 | 786 | /** |
| 787 | - * Set date when the discount was last modified. |
|
| 788 | - * |
|
| 789 | - * @since 1.0.19 |
|
| 790 | - * @param string $value Value to set. |
|
| 787 | + * Set date when the discount was last modified. |
|
| 788 | + * |
|
| 789 | + * @since 1.0.19 |
|
| 790 | + * @param string $value Value to set. |
|
| 791 | 791 | * @return bool Whether or not the date was set. |
| 792 | - */ |
|
| 793 | - public function set_date_modified( $value ) { |
|
| 792 | + */ |
|
| 793 | + public function set_date_modified( $value ) { |
|
| 794 | 794 | $date = strtotime( $value ); |
| 795 | 795 | |
| 796 | 796 | if ( $date ) { |
@@ -803,324 +803,324 @@ discard block |
||
| 803 | 803 | } |
| 804 | 804 | |
| 805 | 805 | /** |
| 806 | - * Set the discount name. |
|
| 807 | - * |
|
| 808 | - * @since 1.0.19 |
|
| 809 | - * @param string $value New name. |
|
| 810 | - */ |
|
| 811 | - public function set_name( $value ) { |
|
| 806 | + * Set the discount name. |
|
| 807 | + * |
|
| 808 | + * @since 1.0.19 |
|
| 809 | + * @param string $value New name. |
|
| 810 | + */ |
|
| 811 | + public function set_name( $value ) { |
|
| 812 | 812 | $name = sanitize_text_field( $value ); |
| 813 | - $this->set_prop( 'name', $name ); |
|
| 813 | + $this->set_prop( 'name', $name ); |
|
| 814 | 814 | } |
| 815 | 815 | |
| 816 | 816 | /** |
| 817 | - * Alias of self::set_name(). |
|
| 818 | - * |
|
| 819 | - * @since 1.0.19 |
|
| 820 | - * @param string $value New name. |
|
| 821 | - */ |
|
| 822 | - public function set_title( $value ) { |
|
| 823 | - $this->set_name( $value ); |
|
| 817 | + * Alias of self::set_name(). |
|
| 818 | + * |
|
| 819 | + * @since 1.0.19 |
|
| 820 | + * @param string $value New name. |
|
| 821 | + */ |
|
| 822 | + public function set_title( $value ) { |
|
| 823 | + $this->set_name( $value ); |
|
| 824 | 824 | } |
| 825 | 825 | |
| 826 | 826 | /** |
| 827 | - * Set the discount description. |
|
| 828 | - * |
|
| 829 | - * @since 1.0.19 |
|
| 830 | - * @param string $value New description. |
|
| 831 | - */ |
|
| 832 | - public function set_description( $value ) { |
|
| 827 | + * Set the discount description. |
|
| 828 | + * |
|
| 829 | + * @since 1.0.19 |
|
| 830 | + * @param string $value New description. |
|
| 831 | + */ |
|
| 832 | + public function set_description( $value ) { |
|
| 833 | 833 | $description = wp_kses_post( $value ); |
| 834 | - return $this->set_prop( 'description', $description ); |
|
| 834 | + return $this->set_prop( 'description', $description ); |
|
| 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_excerpt( $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_excerpt( $value ) { |
|
| 844 | + $this->set_description( $value ); |
|
| 845 | 845 | } |
| 846 | 846 | |
| 847 | 847 | /** |
| 848 | - * Alias of self::set_description(). |
|
| 849 | - * |
|
| 850 | - * @since 1.0.19 |
|
| 851 | - * @param string $value New description. |
|
| 852 | - */ |
|
| 853 | - public function set_summary( $value ) { |
|
| 854 | - $this->set_description( $value ); |
|
| 848 | + * Alias of self::set_description(). |
|
| 849 | + * |
|
| 850 | + * @since 1.0.19 |
|
| 851 | + * @param string $value New description. |
|
| 852 | + */ |
|
| 853 | + public function set_summary( $value ) { |
|
| 854 | + $this->set_description( $value ); |
|
| 855 | 855 | } |
| 856 | 856 | |
| 857 | 857 | /** |
| 858 | - * Set the owner of the discount. |
|
| 859 | - * |
|
| 860 | - * @since 1.0.19 |
|
| 861 | - * @param int $value New author. |
|
| 862 | - */ |
|
| 863 | - public function set_author( $value ) { |
|
| 864 | - $this->set_prop( 'author', (int) $value ); |
|
| 865 | - } |
|
| 858 | + * Set the owner of the discount. |
|
| 859 | + * |
|
| 860 | + * @since 1.0.19 |
|
| 861 | + * @param int $value New author. |
|
| 862 | + */ |
|
| 863 | + public function set_author( $value ) { |
|
| 864 | + $this->set_prop( 'author', (int) $value ); |
|
| 865 | + } |
|
| 866 | 866 | |
| 867 | - /** |
|
| 868 | - * Sets the discount code. |
|
| 869 | - * |
|
| 870 | - * @since 1.0.19 |
|
| 871 | - * @param string $value New discount code. |
|
| 872 | - */ |
|
| 873 | - public function set_code( $value ) { |
|
| 874 | - $code = sanitize_text_field( $value ); |
|
| 875 | - $this->set_prop( 'code', $code ); |
|
| 876 | - } |
|
| 867 | + /** |
|
| 868 | + * Sets the discount code. |
|
| 869 | + * |
|
| 870 | + * @since 1.0.19 |
|
| 871 | + * @param string $value New discount code. |
|
| 872 | + */ |
|
| 873 | + public function set_code( $value ) { |
|
| 874 | + $code = sanitize_text_field( $value ); |
|
| 875 | + $this->set_prop( 'code', $code ); |
|
| 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_coupon_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_coupon_code( $value ) { |
|
| 885 | + $this->set_code( $value ); |
|
| 886 | + } |
|
| 887 | 887 | |
| 888 | - /** |
|
| 889 | - * Alias of self::set_code(). |
|
| 890 | - * |
|
| 891 | - * @since 1.0.19 |
|
| 892 | - * @param string $value New discount code. |
|
| 893 | - */ |
|
| 894 | - public function set_discount_code( $value ) { |
|
| 895 | - $this->set_code( $value ); |
|
| 896 | - } |
|
| 888 | + /** |
|
| 889 | + * Alias of self::set_code(). |
|
| 890 | + * |
|
| 891 | + * @since 1.0.19 |
|
| 892 | + * @param string $value New discount code. |
|
| 893 | + */ |
|
| 894 | + public function set_discount_code( $value ) { |
|
| 895 | + $this->set_code( $value ); |
|
| 896 | + } |
|
| 897 | 897 | |
| 898 | - /** |
|
| 899 | - * Sets the discount amount. |
|
| 900 | - * |
|
| 901 | - * @since 1.0.19 |
|
| 902 | - * @param float $value New discount code. |
|
| 903 | - */ |
|
| 904 | - public function set_amount( $value ) { |
|
| 905 | - $amount = floatval( wpinv_sanitize_amount( $value ) ); |
|
| 906 | - $this->set_prop( 'amount', $amount ); |
|
| 907 | - } |
|
| 908 | - |
|
| 909 | - /** |
|
| 910 | - * Sets the discount's start date. |
|
| 911 | - * |
|
| 912 | - * @since 1.0.19 |
|
| 913 | - * @param float $value New start date. |
|
| 914 | - */ |
|
| 915 | - public function set_start( $value ) { |
|
| 916 | - $date = strtotime( $value ); |
|
| 898 | + /** |
|
| 899 | + * Sets the discount amount. |
|
| 900 | + * |
|
| 901 | + * @since 1.0.19 |
|
| 902 | + * @param float $value New discount code. |
|
| 903 | + */ |
|
| 904 | + public function set_amount( $value ) { |
|
| 905 | + $amount = floatval( wpinv_sanitize_amount( $value ) ); |
|
| 906 | + $this->set_prop( 'amount', $amount ); |
|
| 907 | + } |
|
| 908 | + |
|
| 909 | + /** |
|
| 910 | + * Sets the discount's start date. |
|
| 911 | + * |
|
| 912 | + * @since 1.0.19 |
|
| 913 | + * @param float $value New start date. |
|
| 914 | + */ |
|
| 915 | + public function set_start( $value ) { |
|
| 916 | + $date = strtotime( $value ); |
|
| 917 | 917 | |
| 918 | 918 | if ( $date ) { |
| 919 | 919 | $this->set_prop( 'start', date( 'Y-m-d H:i', $date ) ); |
| 920 | 920 | return true; |
| 921 | - } |
|
| 921 | + } |
|
| 922 | 922 | |
| 923 | - $this->set_prop( 'start', '' ); |
|
| 923 | + $this->set_prop( 'start', '' ); |
|
| 924 | 924 | |
| 925 | 925 | return false; |
| 926 | - } |
|
| 927 | - |
|
| 928 | - /** |
|
| 929 | - * Alias of self::set_start(). |
|
| 930 | - * |
|
| 931 | - * @since 1.0.19 |
|
| 932 | - * @param string $value New start date. |
|
| 933 | - */ |
|
| 934 | - public function set_start_date( $value ) { |
|
| 935 | - $this->set_start( $value ); |
|
| 936 | - } |
|
| 937 | - |
|
| 938 | - /** |
|
| 939 | - * Sets the discount's expiration date. |
|
| 940 | - * |
|
| 941 | - * @since 1.0.19 |
|
| 942 | - * @param float $value New expiration date. |
|
| 943 | - */ |
|
| 944 | - public function set_expiration( $value ) { |
|
| 945 | - $date = strtotime( $value ); |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + /** |
|
| 929 | + * Alias of self::set_start(). |
|
| 930 | + * |
|
| 931 | + * @since 1.0.19 |
|
| 932 | + * @param string $value New start date. |
|
| 933 | + */ |
|
| 934 | + public function set_start_date( $value ) { |
|
| 935 | + $this->set_start( $value ); |
|
| 936 | + } |
|
| 937 | + |
|
| 938 | + /** |
|
| 939 | + * Sets the discount's expiration date. |
|
| 940 | + * |
|
| 941 | + * @since 1.0.19 |
|
| 942 | + * @param float $value New expiration date. |
|
| 943 | + */ |
|
| 944 | + public function set_expiration( $value ) { |
|
| 945 | + $date = strtotime( $value ); |
|
| 946 | 946 | |
| 947 | 947 | if ( $date ) { |
| 948 | 948 | $this->set_prop( 'expiration', date( 'Y-m-d H:i', $date ) ); |
| 949 | 949 | return true; |
| 950 | 950 | } |
| 951 | 951 | |
| 952 | - $this->set_prop( 'expiration', '' ); |
|
| 952 | + $this->set_prop( 'expiration', '' ); |
|
| 953 | 953 | return false; |
| 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_expiration_date( $value ) { |
|
| 963 | - $this->set_expiration( $value ); |
|
| 964 | - } |
|
| 965 | - |
|
| 966 | - /** |
|
| 967 | - * Alias of self::set_expiration(). |
|
| 968 | - * |
|
| 969 | - * @since 1.0.19 |
|
| 970 | - * @param string $value New expiration date. |
|
| 971 | - */ |
|
| 972 | - public function set_end_date( $value ) { |
|
| 973 | - $this->set_expiration( $value ); |
|
| 974 | - } |
|
| 975 | - |
|
| 976 | - /** |
|
| 977 | - * Sets the discount type. |
|
| 978 | - * |
|
| 979 | - * @since 1.0.19 |
|
| 980 | - * @param string $value New discount type. |
|
| 981 | - */ |
|
| 982 | - public function set_type( $value ) { |
|
| 983 | - if ( $value && array_key_exists( sanitize_text_field( $value ), wpinv_get_discount_types() ) ) { |
|
| 984 | - $this->set_prop( 'type', sanitize_text_field( $value ) ); |
|
| 985 | - } |
|
| 986 | - } |
|
| 987 | - |
|
| 988 | - /** |
|
| 989 | - * Sets the number of times a discount has been used. |
|
| 990 | - * |
|
| 991 | - * @since 1.0.19 |
|
| 992 | - * @param int $value usage count. |
|
| 993 | - */ |
|
| 994 | - public function set_uses( $value ) { |
|
| 995 | - |
|
| 996 | - $value = (int) $value; |
|
| 997 | - |
|
| 998 | - if ( $value < 0 ) { |
|
| 999 | - $value = 0; |
|
| 1000 | - } |
|
| 1001 | - |
|
| 1002 | - $this->set_prop( 'uses', (int) $value ); |
|
| 1003 | - } |
|
| 1004 | - |
|
| 1005 | - /** |
|
| 1006 | - * Sets the maximum number of times a discount can be used. |
|
| 1007 | - * |
|
| 1008 | - * @since 1.0.19 |
|
| 1009 | - * @param int $value maximum usage count. |
|
| 1010 | - */ |
|
| 1011 | - public function set_max_uses( $value ) { |
|
| 1012 | - $this->set_prop( 'max_uses', absint( $value ) ); |
|
| 1013 | - } |
|
| 1014 | - |
|
| 1015 | - /** |
|
| 1016 | - * Sets if this is a single use discount or not. |
|
| 1017 | - * |
|
| 1018 | - * @since 1.0.19 |
|
| 1019 | - * @param int|bool $value is single use. |
|
| 1020 | - */ |
|
| 1021 | - public function set_is_single_use( $value ) { |
|
| 1022 | - $this->set_prop( 'is_single_use', (bool) $value ); |
|
| 1023 | - } |
|
| 1024 | - |
|
| 1025 | - /** |
|
| 1026 | - * Sets the items that can be used with this discount. |
|
| 1027 | - * |
|
| 1028 | - * @since 1.0.19 |
|
| 1029 | - * @param array $value items. |
|
| 1030 | - */ |
|
| 1031 | - public function set_items( $value ) { |
|
| 1032 | - $this->set_prop( 'items', wpinv_parse_list( $value ) ); |
|
| 1033 | - } |
|
| 1034 | - |
|
| 1035 | - /** |
|
| 1036 | - * Alias for self::set_items(). |
|
| 1037 | - * |
|
| 1038 | - * @since 1.0.19 |
|
| 1039 | - * @param array $value items. |
|
| 1040 | - */ |
|
| 1041 | - public function set_allowed_items( $value ) { |
|
| 1042 | - $this->set_items( $value ); |
|
| 1043 | - } |
|
| 1044 | - |
|
| 1045 | - /** |
|
| 1046 | - * Sets the items that can not be used with this discount. |
|
| 1047 | - * |
|
| 1048 | - * @since 1.0.19 |
|
| 1049 | - * @param array $value items. |
|
| 1050 | - */ |
|
| 1051 | - public function set_excluded_items( $value ) { |
|
| 1052 | - $this->set_prop( 'excluded_items', wpinv_parse_list( $value ) ); |
|
| 1053 | - } |
|
| 1054 | - |
|
| 1055 | - /** |
|
| 1056 | - * Sets if this is a recurring discounts or not. |
|
| 1057 | - * |
|
| 1058 | - * @since 1.0.19 |
|
| 1059 | - * @param int|bool $value is recurring. |
|
| 1060 | - */ |
|
| 1061 | - public function set_is_recurring( $value ) { |
|
| 1062 | - $this->set_prop( 'is_recurring', (bool) $value ); |
|
| 1063 | - } |
|
| 1064 | - |
|
| 1065 | - /** |
|
| 1066 | - * Sets the minimum total that can not be used with this discount. |
|
| 1067 | - * |
|
| 1068 | - * @since 1.0.19 |
|
| 1069 | - * @param float $value minimum total. |
|
| 1070 | - */ |
|
| 1071 | - public function set_min_total( $value ) { |
|
| 1072 | - $this->set_prop( 'min_total', (float) wpinv_sanitize_amount( $value ) ); |
|
| 1073 | - } |
|
| 1074 | - |
|
| 1075 | - /** |
|
| 1076 | - * Alias for self::set_min_total(). |
|
| 1077 | - * |
|
| 1078 | - * @since 1.0.19 |
|
| 1079 | - * @param float $value minimum total. |
|
| 1080 | - */ |
|
| 1081 | - public function set_minimum_total( $value ) { |
|
| 1082 | - $this->set_min_total( $value ); |
|
| 1083 | - } |
|
| 1084 | - |
|
| 1085 | - /** |
|
| 1086 | - * Sets the maximum total that can not be used with this discount. |
|
| 1087 | - * |
|
| 1088 | - * @since 1.0.19 |
|
| 1089 | - * @param float $value maximum total. |
|
| 1090 | - */ |
|
| 1091 | - public function set_max_total( $value ) { |
|
| 1092 | - $this->set_prop( 'max_total', (float) wpinv_sanitize_amount( $value ) ); |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - /** |
|
| 1096 | - * Alias for self::set_max_total(). |
|
| 1097 | - * |
|
| 1098 | - * @since 1.0.19 |
|
| 1099 | - * @param float $value maximum total. |
|
| 1100 | - */ |
|
| 1101 | - public function set_maximum_total( $value ) { |
|
| 1102 | - $this->set_max_total( $value ); |
|
| 1103 | - } |
|
| 1104 | - |
|
| 1105 | - /** |
|
| 1106 | - * @deprecated |
|
| 1107 | - */ |
|
| 1108 | - public function refresh(){} |
|
| 1109 | - |
|
| 1110 | - /** |
|
| 1111 | - * @deprecated |
|
| 1112 | - * |
|
| 1113 | - */ |
|
| 1114 | - public function update_status( $status = 'publish' ){ |
|
| 1115 | - |
|
| 1116 | - if ( $this->exists() && $this->get_status() != $status ) { |
|
| 1117 | - $this->set_status( $status ); |
|
| 1118 | - $this->save(); |
|
| 1119 | - } |
|
| 1120 | - |
|
| 1121 | - } |
|
| 1122 | - |
|
| 1123 | - /* |
|
| 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_expiration_date( $value ) { |
|
| 963 | + $this->set_expiration( $value ); |
|
| 964 | + } |
|
| 965 | + |
|
| 966 | + /** |
|
| 967 | + * Alias of self::set_expiration(). |
|
| 968 | + * |
|
| 969 | + * @since 1.0.19 |
|
| 970 | + * @param string $value New expiration date. |
|
| 971 | + */ |
|
| 972 | + public function set_end_date( $value ) { |
|
| 973 | + $this->set_expiration( $value ); |
|
| 974 | + } |
|
| 975 | + |
|
| 976 | + /** |
|
| 977 | + * Sets the discount type. |
|
| 978 | + * |
|
| 979 | + * @since 1.0.19 |
|
| 980 | + * @param string $value New discount type. |
|
| 981 | + */ |
|
| 982 | + public function set_type( $value ) { |
|
| 983 | + if ( $value && array_key_exists( sanitize_text_field( $value ), wpinv_get_discount_types() ) ) { |
|
| 984 | + $this->set_prop( 'type', sanitize_text_field( $value ) ); |
|
| 985 | + } |
|
| 986 | + } |
|
| 987 | + |
|
| 988 | + /** |
|
| 989 | + * Sets the number of times a discount has been used. |
|
| 990 | + * |
|
| 991 | + * @since 1.0.19 |
|
| 992 | + * @param int $value usage count. |
|
| 993 | + */ |
|
| 994 | + public function set_uses( $value ) { |
|
| 995 | + |
|
| 996 | + $value = (int) $value; |
|
| 997 | + |
|
| 998 | + if ( $value < 0 ) { |
|
| 999 | + $value = 0; |
|
| 1000 | + } |
|
| 1001 | + |
|
| 1002 | + $this->set_prop( 'uses', (int) $value ); |
|
| 1003 | + } |
|
| 1004 | + |
|
| 1005 | + /** |
|
| 1006 | + * Sets the maximum number of times a discount can be used. |
|
| 1007 | + * |
|
| 1008 | + * @since 1.0.19 |
|
| 1009 | + * @param int $value maximum usage count. |
|
| 1010 | + */ |
|
| 1011 | + public function set_max_uses( $value ) { |
|
| 1012 | + $this->set_prop( 'max_uses', absint( $value ) ); |
|
| 1013 | + } |
|
| 1014 | + |
|
| 1015 | + /** |
|
| 1016 | + * Sets if this is a single use discount or not. |
|
| 1017 | + * |
|
| 1018 | + * @since 1.0.19 |
|
| 1019 | + * @param int|bool $value is single use. |
|
| 1020 | + */ |
|
| 1021 | + public function set_is_single_use( $value ) { |
|
| 1022 | + $this->set_prop( 'is_single_use', (bool) $value ); |
|
| 1023 | + } |
|
| 1024 | + |
|
| 1025 | + /** |
|
| 1026 | + * Sets the items that can be used with this discount. |
|
| 1027 | + * |
|
| 1028 | + * @since 1.0.19 |
|
| 1029 | + * @param array $value items. |
|
| 1030 | + */ |
|
| 1031 | + public function set_items( $value ) { |
|
| 1032 | + $this->set_prop( 'items', wpinv_parse_list( $value ) ); |
|
| 1033 | + } |
|
| 1034 | + |
|
| 1035 | + /** |
|
| 1036 | + * Alias for self::set_items(). |
|
| 1037 | + * |
|
| 1038 | + * @since 1.0.19 |
|
| 1039 | + * @param array $value items. |
|
| 1040 | + */ |
|
| 1041 | + public function set_allowed_items( $value ) { |
|
| 1042 | + $this->set_items( $value ); |
|
| 1043 | + } |
|
| 1044 | + |
|
| 1045 | + /** |
|
| 1046 | + * Sets the items that can not be used with this discount. |
|
| 1047 | + * |
|
| 1048 | + * @since 1.0.19 |
|
| 1049 | + * @param array $value items. |
|
| 1050 | + */ |
|
| 1051 | + public function set_excluded_items( $value ) { |
|
| 1052 | + $this->set_prop( 'excluded_items', wpinv_parse_list( $value ) ); |
|
| 1053 | + } |
|
| 1054 | + |
|
| 1055 | + /** |
|
| 1056 | + * Sets if this is a recurring discounts or not. |
|
| 1057 | + * |
|
| 1058 | + * @since 1.0.19 |
|
| 1059 | + * @param int|bool $value is recurring. |
|
| 1060 | + */ |
|
| 1061 | + public function set_is_recurring( $value ) { |
|
| 1062 | + $this->set_prop( 'is_recurring', (bool) $value ); |
|
| 1063 | + } |
|
| 1064 | + |
|
| 1065 | + /** |
|
| 1066 | + * Sets the minimum total that can not be used with this discount. |
|
| 1067 | + * |
|
| 1068 | + * @since 1.0.19 |
|
| 1069 | + * @param float $value minimum total. |
|
| 1070 | + */ |
|
| 1071 | + public function set_min_total( $value ) { |
|
| 1072 | + $this->set_prop( 'min_total', (float) wpinv_sanitize_amount( $value ) ); |
|
| 1073 | + } |
|
| 1074 | + |
|
| 1075 | + /** |
|
| 1076 | + * Alias for self::set_min_total(). |
|
| 1077 | + * |
|
| 1078 | + * @since 1.0.19 |
|
| 1079 | + * @param float $value minimum total. |
|
| 1080 | + */ |
|
| 1081 | + public function set_minimum_total( $value ) { |
|
| 1082 | + $this->set_min_total( $value ); |
|
| 1083 | + } |
|
| 1084 | + |
|
| 1085 | + /** |
|
| 1086 | + * Sets the maximum total that can not be used with this discount. |
|
| 1087 | + * |
|
| 1088 | + * @since 1.0.19 |
|
| 1089 | + * @param float $value maximum total. |
|
| 1090 | + */ |
|
| 1091 | + public function set_max_total( $value ) { |
|
| 1092 | + $this->set_prop( 'max_total', (float) wpinv_sanitize_amount( $value ) ); |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + /** |
|
| 1096 | + * Alias for self::set_max_total(). |
|
| 1097 | + * |
|
| 1098 | + * @since 1.0.19 |
|
| 1099 | + * @param float $value maximum total. |
|
| 1100 | + */ |
|
| 1101 | + public function set_maximum_total( $value ) { |
|
| 1102 | + $this->set_max_total( $value ); |
|
| 1103 | + } |
|
| 1104 | + |
|
| 1105 | + /** |
|
| 1106 | + * @deprecated |
|
| 1107 | + */ |
|
| 1108 | + public function refresh(){} |
|
| 1109 | + |
|
| 1110 | + /** |
|
| 1111 | + * @deprecated |
|
| 1112 | + * |
|
| 1113 | + */ |
|
| 1114 | + public function update_status( $status = 'publish' ){ |
|
| 1115 | + |
|
| 1116 | + if ( $this->exists() && $this->get_status() != $status ) { |
|
| 1117 | + $this->set_status( $status ); |
|
| 1118 | + $this->save(); |
|
| 1119 | + } |
|
| 1120 | + |
|
| 1121 | + } |
|
| 1122 | + |
|
| 1123 | + /* |
|
| 1124 | 1124 | |-------------------------------------------------------------------------- |
| 1125 | 1125 | | Conditionals |
| 1126 | 1126 | |-------------------------------------------------------------------------- |
@@ -1129,263 +1129,263 @@ discard block |
||
| 1129 | 1129 | | |
| 1130 | 1130 | */ |
| 1131 | 1131 | |
| 1132 | - /** |
|
| 1133 | - * Checks whether a discount exists in the database or not |
|
| 1134 | - * |
|
| 1135 | - * @since 1.0.15 |
|
| 1136 | - */ |
|
| 1137 | - public function exists(){ |
|
| 1138 | - $id = $this->get_id(); |
|
| 1139 | - return ! empty( $id ); |
|
| 1140 | - } |
|
| 1141 | - |
|
| 1142 | - /** |
|
| 1143 | - * Checks the discount type. |
|
| 1144 | - * |
|
| 1145 | - * |
|
| 1146 | - * @param string $type the discount type to check against |
|
| 1147 | - * @since 1.0.15 |
|
| 1148 | - * @return bool |
|
| 1149 | - */ |
|
| 1150 | - public function is_type( $type ) { |
|
| 1151 | - return $this->get_type() == $type; |
|
| 1152 | - } |
|
| 1153 | - |
|
| 1154 | - /** |
|
| 1155 | - * Checks whether the discount is published or not |
|
| 1156 | - * |
|
| 1157 | - * @since 1.0.15 |
|
| 1158 | - * @return bool |
|
| 1159 | - */ |
|
| 1160 | - public function is_active() { |
|
| 1161 | - return $this->get_status() == 'publish'; |
|
| 1162 | - } |
|
| 1163 | - |
|
| 1164 | - /** |
|
| 1165 | - * Checks whether the discount has max uses |
|
| 1166 | - * |
|
| 1167 | - * @since 1.0.15 |
|
| 1168 | - * @return bool |
|
| 1169 | - */ |
|
| 1170 | - public function has_limit() { |
|
| 1171 | - $limit = $this->get_max_uses(); |
|
| 1172 | - return ! empty( $limit ); |
|
| 1173 | - } |
|
| 1174 | - |
|
| 1175 | - /** |
|
| 1176 | - * Checks whether the discount has ever been used. |
|
| 1177 | - * |
|
| 1178 | - * @since 1.0.15 |
|
| 1179 | - * @return bool |
|
| 1180 | - */ |
|
| 1181 | - public function has_uses() { |
|
| 1182 | - return $this->get_uses() > 0; |
|
| 1183 | - } |
|
| 1184 | - |
|
| 1185 | - /** |
|
| 1186 | - * Checks whether the discount is has exided the usage limit or not |
|
| 1187 | - * |
|
| 1188 | - * @since 1.0.15 |
|
| 1189 | - * @return bool |
|
| 1190 | - */ |
|
| 1191 | - public function has_exceeded_limit() { |
|
| 1192 | - |
|
| 1193 | - if ( ! $this->has_limit() || ! $this->has_uses() ) { |
|
| 1194 | - $exceeded = false ; |
|
| 1195 | - } else { |
|
| 1196 | - $exceeded = ! ( (int) $this->get_max_uses() < $this->get_uses() ); |
|
| 1197 | - } |
|
| 1198 | - |
|
| 1199 | - return apply_filters( 'wpinv_is_discount_maxed_out', $exceeded, $this->ID, $this, $this->code ); |
|
| 1200 | - } |
|
| 1201 | - |
|
| 1202 | - /** |
|
| 1203 | - * Checks whether the discount has an expiration date. |
|
| 1204 | - * |
|
| 1205 | - * @since 1.0.15 |
|
| 1206 | - * @return bool |
|
| 1207 | - */ |
|
| 1208 | - public function has_expiration_date() { |
|
| 1209 | - $date = $this->get_expiration_date(); |
|
| 1210 | - return ! empty( $date ); |
|
| 1211 | - } |
|
| 1212 | - |
|
| 1213 | - /** |
|
| 1214 | - * Checks if the discount is expired |
|
| 1215 | - * |
|
| 1216 | - * @since 1.0.15 |
|
| 1217 | - * @return bool |
|
| 1218 | - */ |
|
| 1219 | - public function is_expired() { |
|
| 1220 | - $expired = $this->has_expiration_date() ? current_time( 'timestamp' ) > strtotime( $this->get_expiration_date() ) : false; |
|
| 1221 | - return apply_filters( 'wpinv_is_discount_expired', $expired, $this->ID, $this, $this->code ); |
|
| 1222 | - } |
|
| 1223 | - |
|
| 1224 | - /** |
|
| 1225 | - * Checks whether the discount has a start date. |
|
| 1226 | - * |
|
| 1227 | - * @since 1.0.15 |
|
| 1228 | - * @return bool |
|
| 1229 | - */ |
|
| 1230 | - public function has_start_date() { |
|
| 1231 | - $date = $this->get_start_date(); |
|
| 1232 | - return ! empty( $date ); |
|
| 1233 | - } |
|
| 1234 | - |
|
| 1235 | - /** |
|
| 1236 | - * Checks the discount start date. |
|
| 1237 | - * |
|
| 1238 | - * @since 1.0.15 |
|
| 1239 | - * @return bool |
|
| 1240 | - */ |
|
| 1241 | - public function has_started() { |
|
| 1242 | - $started = $this->has_start_date() ? true : current_time( 'timestamp' ) > strtotime( $this->get_start_date() ); |
|
| 1243 | - return apply_filters( 'wpinv_is_discount_started', $started, $this->ID, $this, $this->code ); |
|
| 1244 | - } |
|
| 1245 | - |
|
| 1246 | - /** |
|
| 1247 | - * Checks the discount has allowed items or not. |
|
| 1248 | - * |
|
| 1249 | - * @since 1.0.15 |
|
| 1250 | - * @return bool |
|
| 1251 | - */ |
|
| 1252 | - public function has_allowed_items() { |
|
| 1253 | - $allowed_items = $this->get_allowed_items(); |
|
| 1254 | - return empty( $allowed_items ); |
|
| 1255 | - } |
|
| 1256 | - |
|
| 1257 | - /** |
|
| 1258 | - * Checks the discount has excluded items or not. |
|
| 1259 | - * |
|
| 1260 | - * @since 1.0.15 |
|
| 1261 | - * @return bool |
|
| 1262 | - */ |
|
| 1263 | - public function has_excluded_items() { |
|
| 1264 | - $excluded_items = $this->get_excluded_items(); |
|
| 1265 | - return empty( $excluded_items ); |
|
| 1266 | - } |
|
| 1267 | - |
|
| 1268 | - /** |
|
| 1269 | - * Check if a discount is valid for a given item id. |
|
| 1270 | - * |
|
| 1271 | - * @param int|int[] $item_ids |
|
| 1272 | - * @since 1.0.15 |
|
| 1273 | - * @return boolean |
|
| 1274 | - */ |
|
| 1275 | - public function is_valid_for_items( $item_ids ) { |
|
| 1276 | - |
|
| 1277 | - $item_ids = array_map( 'intval', wpinv_parse_list( $item_ids ) ); |
|
| 1278 | - $included = array_intersect( $item_ids, $this->get_allowed_items() ); |
|
| 1279 | - $excluded = array_intersect( $item_ids, $this->get_excluded_items() ); |
|
| 1280 | - |
|
| 1281 | - if ( $this->has_excluded_items() && ! empty( $excluded ) ) { |
|
| 1282 | - return false; |
|
| 1283 | - } |
|
| 1284 | - |
|
| 1285 | - if ( $this->has_allowed_items() && empty( $included ) ) { |
|
| 1286 | - return false; |
|
| 1287 | - } |
|
| 1288 | - return true; |
|
| 1289 | - } |
|
| 1290 | - |
|
| 1291 | - /** |
|
| 1292 | - * Check if a discount is valid for the given amount |
|
| 1293 | - * |
|
| 1294 | - * @param float $amount The amount to check against |
|
| 1295 | - * @since 1.0.15 |
|
| 1296 | - * @return boolean |
|
| 1297 | - */ |
|
| 1298 | - public function is_valid_for_amount( $amount ) { |
|
| 1299 | - return $this->is_minimum_amount_met( $amount ) && $this->is_maximum_amount_met( $amount ); |
|
| 1300 | - } |
|
| 1301 | - |
|
| 1302 | - /** |
|
| 1303 | - * Checks if the minimum amount is set |
|
| 1304 | - * |
|
| 1305 | - * @since 1.0.15 |
|
| 1306 | - * @return boolean |
|
| 1307 | - */ |
|
| 1308 | - public function has_minimum_amount() { |
|
| 1309 | - $minimum = $this->get_minimum_total(); |
|
| 1310 | - return ! empty( $minimum ); |
|
| 1311 | - } |
|
| 1312 | - |
|
| 1313 | - /** |
|
| 1314 | - * Checks if the minimum amount is met |
|
| 1315 | - * |
|
| 1316 | - * @param float $amount The amount to check against |
|
| 1317 | - * @since 1.0.15 |
|
| 1318 | - * @return boolean |
|
| 1319 | - */ |
|
| 1320 | - public function is_minimum_amount_met( $amount ) { |
|
| 1321 | - $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
| 1322 | - $min_met= ! ( $this->has_minimum_amount() && $amount < floatval( wpinv_sanitize_amount( $this->get_minimum_total() ) ) ); |
|
| 1323 | - return apply_filters( 'wpinv_is_discount_min_met', $min_met, $this->ID, $this, $this->code, $amount ); |
|
| 1324 | - } |
|
| 1325 | - |
|
| 1326 | - /** |
|
| 1327 | - * Checks if the maximum amount is set |
|
| 1328 | - * |
|
| 1329 | - * @since 1.0.15 |
|
| 1330 | - * @return boolean |
|
| 1331 | - */ |
|
| 1332 | - public function has_maximum_amount() { |
|
| 1333 | - $maximum = $this->get_maximum_total(); |
|
| 1334 | - return ! empty( $maximum ); |
|
| 1335 | - } |
|
| 1336 | - |
|
| 1337 | - /** |
|
| 1338 | - * Checks if the maximum amount is met |
|
| 1339 | - * |
|
| 1340 | - * @param float $amount The amount to check against |
|
| 1341 | - * @since 1.0.15 |
|
| 1342 | - * @return boolean |
|
| 1343 | - */ |
|
| 1344 | - public function is_maximum_amount_met( $amount ) { |
|
| 1345 | - $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
| 1346 | - $max_met= ! ( $this->has_maximum_amount() && $amount > floatval( wpinv_sanitize_amount( $this->get_maximum_total() ) ) ); |
|
| 1347 | - return apply_filters( 'wpinv_is_discount_max_met', $max_met, $this->ID, $this, $this->code, $amount ); |
|
| 1348 | - } |
|
| 1349 | - |
|
| 1350 | - /** |
|
| 1351 | - * Checks if the discount is recurring. |
|
| 1352 | - * |
|
| 1353 | - * @since 1.0.15 |
|
| 1354 | - * @return boolean |
|
| 1355 | - */ |
|
| 1356 | - public function is_recurring() { |
|
| 1357 | - $recurring = $this->get_is_recurring(); |
|
| 1358 | - return ! empty( $recurring ); |
|
| 1359 | - } |
|
| 1360 | - |
|
| 1361 | - /** |
|
| 1362 | - * Checks if the discount is single use. |
|
| 1363 | - * |
|
| 1364 | - * @since 1.0.15 |
|
| 1365 | - * @return boolean |
|
| 1366 | - */ |
|
| 1367 | - public function is_single_use() { |
|
| 1368 | - $usage = $this->get_is_single_use(); |
|
| 1369 | - return ! empty( $usage ); |
|
| 1370 | - } |
|
| 1371 | - |
|
| 1372 | - /** |
|
| 1373 | - * Check if a discount is valid for the given user |
|
| 1374 | - * |
|
| 1375 | - * @param int|string $user |
|
| 1376 | - * @since 1.0.15 |
|
| 1377 | - * @return boolean |
|
| 1378 | - */ |
|
| 1379 | - public function is_valid_for_user( $user ) { |
|
| 1380 | - global $wpi_checkout_id; |
|
| 1381 | - |
|
| 1382 | - // Ensure that the discount is single use. |
|
| 1383 | - if ( empty( $user ) || ! $this->is_single_use() ) { |
|
| 1384 | - return true; |
|
| 1385 | - } |
|
| 1386 | - |
|
| 1387 | - // Prepare the user id. |
|
| 1388 | - $user_id = 0; |
|
| 1132 | + /** |
|
| 1133 | + * Checks whether a discount exists in the database or not |
|
| 1134 | + * |
|
| 1135 | + * @since 1.0.15 |
|
| 1136 | + */ |
|
| 1137 | + public function exists(){ |
|
| 1138 | + $id = $this->get_id(); |
|
| 1139 | + return ! empty( $id ); |
|
| 1140 | + } |
|
| 1141 | + |
|
| 1142 | + /** |
|
| 1143 | + * Checks the discount type. |
|
| 1144 | + * |
|
| 1145 | + * |
|
| 1146 | + * @param string $type the discount type to check against |
|
| 1147 | + * @since 1.0.15 |
|
| 1148 | + * @return bool |
|
| 1149 | + */ |
|
| 1150 | + public function is_type( $type ) { |
|
| 1151 | + return $this->get_type() == $type; |
|
| 1152 | + } |
|
| 1153 | + |
|
| 1154 | + /** |
|
| 1155 | + * Checks whether the discount is published or not |
|
| 1156 | + * |
|
| 1157 | + * @since 1.0.15 |
|
| 1158 | + * @return bool |
|
| 1159 | + */ |
|
| 1160 | + public function is_active() { |
|
| 1161 | + return $this->get_status() == 'publish'; |
|
| 1162 | + } |
|
| 1163 | + |
|
| 1164 | + /** |
|
| 1165 | + * Checks whether the discount has max uses |
|
| 1166 | + * |
|
| 1167 | + * @since 1.0.15 |
|
| 1168 | + * @return bool |
|
| 1169 | + */ |
|
| 1170 | + public function has_limit() { |
|
| 1171 | + $limit = $this->get_max_uses(); |
|
| 1172 | + return ! empty( $limit ); |
|
| 1173 | + } |
|
| 1174 | + |
|
| 1175 | + /** |
|
| 1176 | + * Checks whether the discount has ever been used. |
|
| 1177 | + * |
|
| 1178 | + * @since 1.0.15 |
|
| 1179 | + * @return bool |
|
| 1180 | + */ |
|
| 1181 | + public function has_uses() { |
|
| 1182 | + return $this->get_uses() > 0; |
|
| 1183 | + } |
|
| 1184 | + |
|
| 1185 | + /** |
|
| 1186 | + * Checks whether the discount is has exided the usage limit or not |
|
| 1187 | + * |
|
| 1188 | + * @since 1.0.15 |
|
| 1189 | + * @return bool |
|
| 1190 | + */ |
|
| 1191 | + public function has_exceeded_limit() { |
|
| 1192 | + |
|
| 1193 | + if ( ! $this->has_limit() || ! $this->has_uses() ) { |
|
| 1194 | + $exceeded = false ; |
|
| 1195 | + } else { |
|
| 1196 | + $exceeded = ! ( (int) $this->get_max_uses() < $this->get_uses() ); |
|
| 1197 | + } |
|
| 1198 | + |
|
| 1199 | + return apply_filters( 'wpinv_is_discount_maxed_out', $exceeded, $this->ID, $this, $this->code ); |
|
| 1200 | + } |
|
| 1201 | + |
|
| 1202 | + /** |
|
| 1203 | + * Checks whether the discount has an expiration date. |
|
| 1204 | + * |
|
| 1205 | + * @since 1.0.15 |
|
| 1206 | + * @return bool |
|
| 1207 | + */ |
|
| 1208 | + public function has_expiration_date() { |
|
| 1209 | + $date = $this->get_expiration_date(); |
|
| 1210 | + return ! empty( $date ); |
|
| 1211 | + } |
|
| 1212 | + |
|
| 1213 | + /** |
|
| 1214 | + * Checks if the discount is expired |
|
| 1215 | + * |
|
| 1216 | + * @since 1.0.15 |
|
| 1217 | + * @return bool |
|
| 1218 | + */ |
|
| 1219 | + public function is_expired() { |
|
| 1220 | + $expired = $this->has_expiration_date() ? current_time( 'timestamp' ) > strtotime( $this->get_expiration_date() ) : false; |
|
| 1221 | + return apply_filters( 'wpinv_is_discount_expired', $expired, $this->ID, $this, $this->code ); |
|
| 1222 | + } |
|
| 1223 | + |
|
| 1224 | + /** |
|
| 1225 | + * Checks whether the discount has a start date. |
|
| 1226 | + * |
|
| 1227 | + * @since 1.0.15 |
|
| 1228 | + * @return bool |
|
| 1229 | + */ |
|
| 1230 | + public function has_start_date() { |
|
| 1231 | + $date = $this->get_start_date(); |
|
| 1232 | + return ! empty( $date ); |
|
| 1233 | + } |
|
| 1234 | + |
|
| 1235 | + /** |
|
| 1236 | + * Checks the discount start date. |
|
| 1237 | + * |
|
| 1238 | + * @since 1.0.15 |
|
| 1239 | + * @return bool |
|
| 1240 | + */ |
|
| 1241 | + public function has_started() { |
|
| 1242 | + $started = $this->has_start_date() ? true : current_time( 'timestamp' ) > strtotime( $this->get_start_date() ); |
|
| 1243 | + return apply_filters( 'wpinv_is_discount_started', $started, $this->ID, $this, $this->code ); |
|
| 1244 | + } |
|
| 1245 | + |
|
| 1246 | + /** |
|
| 1247 | + * Checks the discount has allowed items or not. |
|
| 1248 | + * |
|
| 1249 | + * @since 1.0.15 |
|
| 1250 | + * @return bool |
|
| 1251 | + */ |
|
| 1252 | + public function has_allowed_items() { |
|
| 1253 | + $allowed_items = $this->get_allowed_items(); |
|
| 1254 | + return empty( $allowed_items ); |
|
| 1255 | + } |
|
| 1256 | + |
|
| 1257 | + /** |
|
| 1258 | + * Checks the discount has excluded items or not. |
|
| 1259 | + * |
|
| 1260 | + * @since 1.0.15 |
|
| 1261 | + * @return bool |
|
| 1262 | + */ |
|
| 1263 | + public function has_excluded_items() { |
|
| 1264 | + $excluded_items = $this->get_excluded_items(); |
|
| 1265 | + return empty( $excluded_items ); |
|
| 1266 | + } |
|
| 1267 | + |
|
| 1268 | + /** |
|
| 1269 | + * Check if a discount is valid for a given item id. |
|
| 1270 | + * |
|
| 1271 | + * @param int|int[] $item_ids |
|
| 1272 | + * @since 1.0.15 |
|
| 1273 | + * @return boolean |
|
| 1274 | + */ |
|
| 1275 | + public function is_valid_for_items( $item_ids ) { |
|
| 1276 | + |
|
| 1277 | + $item_ids = array_map( 'intval', wpinv_parse_list( $item_ids ) ); |
|
| 1278 | + $included = array_intersect( $item_ids, $this->get_allowed_items() ); |
|
| 1279 | + $excluded = array_intersect( $item_ids, $this->get_excluded_items() ); |
|
| 1280 | + |
|
| 1281 | + if ( $this->has_excluded_items() && ! empty( $excluded ) ) { |
|
| 1282 | + return false; |
|
| 1283 | + } |
|
| 1284 | + |
|
| 1285 | + if ( $this->has_allowed_items() && empty( $included ) ) { |
|
| 1286 | + return false; |
|
| 1287 | + } |
|
| 1288 | + return true; |
|
| 1289 | + } |
|
| 1290 | + |
|
| 1291 | + /** |
|
| 1292 | + * Check if a discount is valid for the given amount |
|
| 1293 | + * |
|
| 1294 | + * @param float $amount The amount to check against |
|
| 1295 | + * @since 1.0.15 |
|
| 1296 | + * @return boolean |
|
| 1297 | + */ |
|
| 1298 | + public function is_valid_for_amount( $amount ) { |
|
| 1299 | + return $this->is_minimum_amount_met( $amount ) && $this->is_maximum_amount_met( $amount ); |
|
| 1300 | + } |
|
| 1301 | + |
|
| 1302 | + /** |
|
| 1303 | + * Checks if the minimum amount is set |
|
| 1304 | + * |
|
| 1305 | + * @since 1.0.15 |
|
| 1306 | + * @return boolean |
|
| 1307 | + */ |
|
| 1308 | + public function has_minimum_amount() { |
|
| 1309 | + $minimum = $this->get_minimum_total(); |
|
| 1310 | + return ! empty( $minimum ); |
|
| 1311 | + } |
|
| 1312 | + |
|
| 1313 | + /** |
|
| 1314 | + * Checks if the minimum amount is met |
|
| 1315 | + * |
|
| 1316 | + * @param float $amount The amount to check against |
|
| 1317 | + * @since 1.0.15 |
|
| 1318 | + * @return boolean |
|
| 1319 | + */ |
|
| 1320 | + public function is_minimum_amount_met( $amount ) { |
|
| 1321 | + $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
| 1322 | + $min_met= ! ( $this->has_minimum_amount() && $amount < floatval( wpinv_sanitize_amount( $this->get_minimum_total() ) ) ); |
|
| 1323 | + return apply_filters( 'wpinv_is_discount_min_met', $min_met, $this->ID, $this, $this->code, $amount ); |
|
| 1324 | + } |
|
| 1325 | + |
|
| 1326 | + /** |
|
| 1327 | + * Checks if the maximum amount is set |
|
| 1328 | + * |
|
| 1329 | + * @since 1.0.15 |
|
| 1330 | + * @return boolean |
|
| 1331 | + */ |
|
| 1332 | + public function has_maximum_amount() { |
|
| 1333 | + $maximum = $this->get_maximum_total(); |
|
| 1334 | + return ! empty( $maximum ); |
|
| 1335 | + } |
|
| 1336 | + |
|
| 1337 | + /** |
|
| 1338 | + * Checks if the maximum amount is met |
|
| 1339 | + * |
|
| 1340 | + * @param float $amount The amount to check against |
|
| 1341 | + * @since 1.0.15 |
|
| 1342 | + * @return boolean |
|
| 1343 | + */ |
|
| 1344 | + public function is_maximum_amount_met( $amount ) { |
|
| 1345 | + $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
| 1346 | + $max_met= ! ( $this->has_maximum_amount() && $amount > floatval( wpinv_sanitize_amount( $this->get_maximum_total() ) ) ); |
|
| 1347 | + return apply_filters( 'wpinv_is_discount_max_met', $max_met, $this->ID, $this, $this->code, $amount ); |
|
| 1348 | + } |
|
| 1349 | + |
|
| 1350 | + /** |
|
| 1351 | + * Checks if the discount is recurring. |
|
| 1352 | + * |
|
| 1353 | + * @since 1.0.15 |
|
| 1354 | + * @return boolean |
|
| 1355 | + */ |
|
| 1356 | + public function is_recurring() { |
|
| 1357 | + $recurring = $this->get_is_recurring(); |
|
| 1358 | + return ! empty( $recurring ); |
|
| 1359 | + } |
|
| 1360 | + |
|
| 1361 | + /** |
|
| 1362 | + * Checks if the discount is single use. |
|
| 1363 | + * |
|
| 1364 | + * @since 1.0.15 |
|
| 1365 | + * @return boolean |
|
| 1366 | + */ |
|
| 1367 | + public function is_single_use() { |
|
| 1368 | + $usage = $this->get_is_single_use(); |
|
| 1369 | + return ! empty( $usage ); |
|
| 1370 | + } |
|
| 1371 | + |
|
| 1372 | + /** |
|
| 1373 | + * Check if a discount is valid for the given user |
|
| 1374 | + * |
|
| 1375 | + * @param int|string $user |
|
| 1376 | + * @since 1.0.15 |
|
| 1377 | + * @return boolean |
|
| 1378 | + */ |
|
| 1379 | + public function is_valid_for_user( $user ) { |
|
| 1380 | + global $wpi_checkout_id; |
|
| 1381 | + |
|
| 1382 | + // Ensure that the discount is single use. |
|
| 1383 | + if ( empty( $user ) || ! $this->is_single_use() ) { |
|
| 1384 | + return true; |
|
| 1385 | + } |
|
| 1386 | + |
|
| 1387 | + // Prepare the user id. |
|
| 1388 | + $user_id = 0; |
|
| 1389 | 1389 | if ( is_int( $user ) ) { |
| 1390 | 1390 | $user_id = absint( $user ); |
| 1391 | 1391 | } else if ( is_email( $user ) && $user_data = get_user_by( 'email', $user ) ) { |
@@ -1394,135 +1394,135 @@ discard block |
||
| 1394 | 1394 | $user_id = $user_data->ID; |
| 1395 | 1395 | } else if ( absint( $user ) > 0 ) { |
| 1396 | 1396 | $user_id = absint( $user ); |
| 1397 | - } |
|
| 1397 | + } |
|
| 1398 | 1398 | |
| 1399 | - // Ensure that we have a user. |
|
| 1400 | - if ( empty( $user_id ) ) { |
|
| 1401 | - return true; |
|
| 1402 | - } |
|
| 1399 | + // Ensure that we have a user. |
|
| 1400 | + if ( empty( $user_id ) ) { |
|
| 1401 | + return true; |
|
| 1402 | + } |
|
| 1403 | 1403 | |
| 1404 | - // Get all payments with matching user id. |
|
| 1404 | + // Get all payments with matching user id. |
|
| 1405 | 1405 | $payments = wpinv_get_invoices( array( 'user' => $user_id, 'limit' => false ) ); |
| 1406 | - $code = strtolower( $this->get_code() ); |
|
| 1407 | - |
|
| 1408 | - // For each payment... |
|
| 1409 | - foreach ( $payments as $payment ) { |
|
| 1410 | - |
|
| 1411 | - // ... skip the current payment. |
|
| 1412 | - if ( ! empty( $wpi_checkout_id ) && $wpi_checkout_id == $payment->ID ) { |
|
| 1413 | - continue; |
|
| 1414 | - } |
|
| 1415 | - |
|
| 1416 | - // And failed payments. |
|
| 1417 | - if ( $payment->has_status( array( 'wpi-cancelled', 'wpi-failed' ) ) ) { |
|
| 1418 | - continue; |
|
| 1419 | - } |
|
| 1420 | - |
|
| 1421 | - // Retrieve all the discounts for the payment. |
|
| 1422 | - $discounts = $payment->get_discounts( true ); |
|
| 1423 | - if ( empty( $discounts ) ) { |
|
| 1424 | - continue; |
|
| 1425 | - } |
|
| 1426 | - |
|
| 1427 | - // And check if the current discount is amongst them. |
|
| 1428 | - $discounts = array_map( 'strtolower', wpinv_parse_list( $discounts ) ); |
|
| 1429 | - if ( ! empty( $discounts ) && in_array( $code, $discounts ) ) { |
|
| 1430 | - return false; |
|
| 1431 | - } |
|
| 1432 | - } |
|
| 1433 | - |
|
| 1434 | - return true; |
|
| 1435 | - } |
|
| 1436 | - |
|
| 1437 | - /** |
|
| 1438 | - * Deletes the discount from the database |
|
| 1439 | - * |
|
| 1440 | - * @since 1.0.15 |
|
| 1441 | - * @return boolean |
|
| 1442 | - */ |
|
| 1443 | - public function remove() { |
|
| 1444 | - return $this->delete(); |
|
| 1445 | - } |
|
| 1446 | - |
|
| 1447 | - /** |
|
| 1448 | - * Increases a discount's usage. |
|
| 1449 | - * |
|
| 1450 | - * @since 1.0.15 |
|
| 1451 | - * @param int $by The number of usages to increas by. |
|
| 1452 | - * @return int |
|
| 1453 | - */ |
|
| 1454 | - public function increase_usage( $by = 1 ) { |
|
| 1455 | - |
|
| 1456 | - // Abort if zero. |
|
| 1457 | - if ( empty( $by ) ) { |
|
| 1458 | - return; |
|
| 1459 | - } |
|
| 1460 | - |
|
| 1461 | - // Increase the usage. |
|
| 1462 | - $this->set_uses( $this->get_uses() + (int) $by ); |
|
| 1463 | - |
|
| 1464 | - // Save the discount. |
|
| 1465 | - $this->save(); |
|
| 1466 | - |
|
| 1467 | - // Fire relevant hooks. |
|
| 1468 | - if( (int) $by > 0 ) { |
|
| 1469 | - do_action( 'wpinv_discount_increase_use_count', $this->uses, $this->ID, $this->code, absint( $by ) ); |
|
| 1470 | - } else { |
|
| 1471 | - do_action( 'wpinv_discount_decrease_use_count', $this->uses, $this->ID, $this->code, absint( $by ) ); |
|
| 1472 | - } |
|
| 1473 | - |
|
| 1474 | - // Return the number of times the discount has been used. |
|
| 1475 | - return $this->get_uses(); |
|
| 1476 | - } |
|
| 1477 | - |
|
| 1478 | - /** |
|
| 1479 | - * Alias of self::__toString() |
|
| 1480 | - * |
|
| 1481 | - * @since 1.0.15 |
|
| 1482 | - * @return string|false |
|
| 1483 | - */ |
|
| 1484 | - public function get_data_as_json() { |
|
| 1485 | - return $this->__toString(); |
|
| 1486 | - } |
|
| 1487 | - |
|
| 1488 | - /** |
|
| 1489 | - * Returns a discount's discounted amount. |
|
| 1490 | - * |
|
| 1491 | - * @since 1.0.15 |
|
| 1492 | - * @param float $amount |
|
| 1493 | - * @return float |
|
| 1494 | - */ |
|
| 1495 | - public function get_discounted_amount( $amount ) { |
|
| 1496 | - |
|
| 1497 | - // Convert amount to float. |
|
| 1498 | - $amount = (float) $amount; |
|
| 1499 | - |
|
| 1500 | - // Get discount amount. |
|
| 1501 | - $discount_amount = $this->get_amount(); |
|
| 1502 | - |
|
| 1503 | - if ( empty( $discount_amount ) ) { |
|
| 1504 | - return 0; |
|
| 1505 | - } |
|
| 1506 | - |
|
| 1507 | - // Format the amount. |
|
| 1508 | - $discount_amount = floatval( wpinv_sanitize_amount( $discount_amount ) ); |
|
| 1406 | + $code = strtolower( $this->get_code() ); |
|
| 1407 | + |
|
| 1408 | + // For each payment... |
|
| 1409 | + foreach ( $payments as $payment ) { |
|
| 1410 | + |
|
| 1411 | + // ... skip the current payment. |
|
| 1412 | + if ( ! empty( $wpi_checkout_id ) && $wpi_checkout_id == $payment->ID ) { |
|
| 1413 | + continue; |
|
| 1414 | + } |
|
| 1415 | + |
|
| 1416 | + // And failed payments. |
|
| 1417 | + if ( $payment->has_status( array( 'wpi-cancelled', 'wpi-failed' ) ) ) { |
|
| 1418 | + continue; |
|
| 1419 | + } |
|
| 1420 | + |
|
| 1421 | + // Retrieve all the discounts for the payment. |
|
| 1422 | + $discounts = $payment->get_discounts( true ); |
|
| 1423 | + if ( empty( $discounts ) ) { |
|
| 1424 | + continue; |
|
| 1425 | + } |
|
| 1426 | + |
|
| 1427 | + // And check if the current discount is amongst them. |
|
| 1428 | + $discounts = array_map( 'strtolower', wpinv_parse_list( $discounts ) ); |
|
| 1429 | + if ( ! empty( $discounts ) && in_array( $code, $discounts ) ) { |
|
| 1430 | + return false; |
|
| 1431 | + } |
|
| 1432 | + } |
|
| 1433 | + |
|
| 1434 | + return true; |
|
| 1435 | + } |
|
| 1436 | + |
|
| 1437 | + /** |
|
| 1438 | + * Deletes the discount from the database |
|
| 1439 | + * |
|
| 1440 | + * @since 1.0.15 |
|
| 1441 | + * @return boolean |
|
| 1442 | + */ |
|
| 1443 | + public function remove() { |
|
| 1444 | + return $this->delete(); |
|
| 1445 | + } |
|
| 1446 | + |
|
| 1447 | + /** |
|
| 1448 | + * Increases a discount's usage. |
|
| 1449 | + * |
|
| 1450 | + * @since 1.0.15 |
|
| 1451 | + * @param int $by The number of usages to increas by. |
|
| 1452 | + * @return int |
|
| 1453 | + */ |
|
| 1454 | + public function increase_usage( $by = 1 ) { |
|
| 1455 | + |
|
| 1456 | + // Abort if zero. |
|
| 1457 | + if ( empty( $by ) ) { |
|
| 1458 | + return; |
|
| 1459 | + } |
|
| 1460 | + |
|
| 1461 | + // Increase the usage. |
|
| 1462 | + $this->set_uses( $this->get_uses() + (int) $by ); |
|
| 1463 | + |
|
| 1464 | + // Save the discount. |
|
| 1465 | + $this->save(); |
|
| 1466 | + |
|
| 1467 | + // Fire relevant hooks. |
|
| 1468 | + if( (int) $by > 0 ) { |
|
| 1469 | + do_action( 'wpinv_discount_increase_use_count', $this->uses, $this->ID, $this->code, absint( $by ) ); |
|
| 1470 | + } else { |
|
| 1471 | + do_action( 'wpinv_discount_decrease_use_count', $this->uses, $this->ID, $this->code, absint( $by ) ); |
|
| 1472 | + } |
|
| 1473 | + |
|
| 1474 | + // Return the number of times the discount has been used. |
|
| 1475 | + return $this->get_uses(); |
|
| 1476 | + } |
|
| 1477 | + |
|
| 1478 | + /** |
|
| 1479 | + * Alias of self::__toString() |
|
| 1480 | + * |
|
| 1481 | + * @since 1.0.15 |
|
| 1482 | + * @return string|false |
|
| 1483 | + */ |
|
| 1484 | + public function get_data_as_json() { |
|
| 1485 | + return $this->__toString(); |
|
| 1486 | + } |
|
| 1487 | + |
|
| 1488 | + /** |
|
| 1489 | + * Returns a discount's discounted amount. |
|
| 1490 | + * |
|
| 1491 | + * @since 1.0.15 |
|
| 1492 | + * @param float $amount |
|
| 1493 | + * @return float |
|
| 1494 | + */ |
|
| 1495 | + public function get_discounted_amount( $amount ) { |
|
| 1496 | + |
|
| 1497 | + // Convert amount to float. |
|
| 1498 | + $amount = (float) $amount; |
|
| 1499 | + |
|
| 1500 | + // Get discount amount. |
|
| 1501 | + $discount_amount = $this->get_amount(); |
|
| 1502 | + |
|
| 1503 | + if ( empty( $discount_amount ) ) { |
|
| 1504 | + return 0; |
|
| 1505 | + } |
|
| 1506 | + |
|
| 1507 | + // Format the amount. |
|
| 1508 | + $discount_amount = floatval( wpinv_sanitize_amount( $discount_amount ) ); |
|
| 1509 | 1509 | |
| 1510 | - // If this is a percentage discount. |
|
| 1511 | - if ( $this->is_type( 'percent' ) ) { |
|
| 1510 | + // If this is a percentage discount. |
|
| 1511 | + if ( $this->is_type( 'percent' ) ) { |
|
| 1512 | 1512 | $discount_amount = $amount * ( $discount_amount / 100 ); |
| 1513 | - } |
|
| 1513 | + } |
|
| 1514 | 1514 | |
| 1515 | - // Discount can not be less than zero... |
|
| 1516 | - if ( $discount_amount < 0 ) { |
|
| 1517 | - $discount_amount = 0; |
|
| 1518 | - } |
|
| 1515 | + // Discount can not be less than zero... |
|
| 1516 | + if ( $discount_amount < 0 ) { |
|
| 1517 | + $discount_amount = 0; |
|
| 1518 | + } |
|
| 1519 | 1519 | |
| 1520 | - // ... or more than the amount. |
|
| 1521 | - if ( $discount_amount > $amount ) { |
|
| 1522 | - $discount_amount = $amount; |
|
| 1523 | - } |
|
| 1520 | + // ... or more than the amount. |
|
| 1521 | + if ( $discount_amount > $amount ) { |
|
| 1522 | + $discount_amount = $amount; |
|
| 1523 | + } |
|
| 1524 | 1524 | |
| 1525 | - return apply_filters( 'wpinv_discount_total_discount_amount', $discount_amount, $amount, $this ); |
|
| 1526 | - } |
|
| 1525 | + return apply_filters( 'wpinv_discount_total_discount_amount', $discount_amount, $amount, $this ); |
|
| 1526 | + } |
|
| 1527 | 1527 | |
| 1528 | 1528 | } |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | * @since 1.0.15 |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | -defined( 'ABSPATH' ) || exit; |
|
| 8 | +defined('ABSPATH') || exit; |
|
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * Discount class. |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | * @since 1.0.15 |
| 14 | 14 | * |
| 15 | 15 | */ |
| 16 | -class WPInv_Discount extends GetPaid_Data { |
|
| 16 | +class WPInv_Discount extends GetPaid_Data { |
|
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * Which data store to load. |
@@ -79,35 +79,35 @@ discard block |
||
| 79 | 79 | * |
| 80 | 80 | * @param int|array|string|WPInv_Discount|WP_Post $discount discount data, object, ID or code. |
| 81 | 81 | */ |
| 82 | - public function __construct( $discount = 0 ) { |
|
| 83 | - parent::__construct( $discount ); |
|
| 82 | + public function __construct($discount = 0) { |
|
| 83 | + parent::__construct($discount); |
|
| 84 | 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 ); |
|
| 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 | 93 | |
| 94 | - if ( isset( $discount['ID'] ) ) { |
|
| 95 | - $this->set_id( $discount['ID'] ); |
|
| 94 | + if (isset($discount['ID'])) { |
|
| 95 | + $this->set_id($discount['ID']); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - } elseif ( is_scalar( $discount ) && $discount = self::get_discount_id_by_code( $discount ) ) { |
|
| 99 | - $this->set_id( $discount ); |
|
| 98 | + } elseif (is_scalar($discount) && $discount = self::get_discount_id_by_code($discount)) { |
|
| 99 | + $this->set_id($discount); |
|
| 100 | 100 | } else { |
| 101 | - $this->set_object_read( true ); |
|
| 101 | + $this->set_object_read(true); |
|
| 102 | 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 ) { |
|
| 108 | - $this->post = get_post( $this->get_id() ); |
|
| 107 | + if ($this->get_id() > 0) { |
|
| 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 | 111 | } |
| 112 | 112 | |
| 113 | 113 | } |
@@ -123,50 +123,50 @@ discard block |
||
| 123 | 123 | * @since 1.0.15 |
| 124 | 124 | * @return array|bool array of discount details on success. False otherwise. |
| 125 | 125 | */ |
| 126 | - public static function get_data_by( $field, $value ) { |
|
| 126 | + public static function get_data_by($field, $value) { |
|
| 127 | 127 | |
| 128 | - if ( 'id' == strtolower( $field ) ) { |
|
| 128 | + if ('id' == strtolower($field)) { |
|
| 129 | 129 | // Make sure the value is numeric to avoid casting objects, for example, |
| 130 | 130 | // to int 1. |
| 131 | - if ( ! is_numeric( $value ) ) |
|
| 131 | + if (!is_numeric($value)) |
|
| 132 | 132 | return false; |
| 133 | - $value = intval( $value ); |
|
| 134 | - if ( $value < 1 ) |
|
| 133 | + $value = intval($value); |
|
| 134 | + if ($value < 1) |
|
| 135 | 135 | return false; |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - if ( ! $value || ! is_string( $field ) ) { |
|
| 138 | + if (!$value || !is_string($field)) { |
|
| 139 | 139 | return false; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - $field = trim( $field ); |
|
| 142 | + $field = trim($field); |
|
| 143 | 143 | |
| 144 | 144 | // prepare query args |
| 145 | - switch ( strtolower( $field ) ) { |
|
| 145 | + switch (strtolower($field)) { |
|
| 146 | 146 | case 'id': |
| 147 | 147 | $discount_id = $value; |
| 148 | - $args = array( 'include' => array( $value ) ); |
|
| 148 | + $args = array('include' => array($value)); |
|
| 149 | 149 | break; |
| 150 | 150 | case 'discount_code': |
| 151 | 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 ); |
|
| 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 | 155 | break; |
| 156 | 156 | case 'name': |
| 157 | 157 | $discount_id = 0; |
| 158 | - $args = array( 'name' => trim( $value ) ); |
|
| 158 | + $args = array('name' => trim($value)); |
|
| 159 | 159 | break; |
| 160 | 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 ); |
|
| 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 | 164 | } |
| 165 | 165 | |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | // Check if there is a cached value. |
| 169 | - if ( ! empty( $discount_id ) && $discount = wp_cache_get( (int) $discount_id, 'WPInv_Discounts' ) ) { |
|
| 169 | + if (!empty($discount_id) && $discount = wp_cache_get((int) $discount_id, 'WPInv_Discounts')) { |
|
| 170 | 170 | return $discount; |
| 171 | 171 | } |
| 172 | 172 | |
@@ -175,13 +175,13 @@ discard block |
||
| 175 | 175 | array( |
| 176 | 176 | 'post_type' => 'wpi_discount', |
| 177 | 177 | 'posts_per_page' => 1, |
| 178 | - 'post_status' => array( 'publish', 'pending', 'draft', 'expired' ) |
|
| 178 | + 'post_status' => array('publish', 'pending', 'draft', 'expired') |
|
| 179 | 179 | ) |
| 180 | 180 | ); |
| 181 | 181 | |
| 182 | - $discount = get_posts( $args ); |
|
| 182 | + $discount = get_posts($args); |
|
| 183 | 183 | |
| 184 | - if( empty( $discount ) ) { |
|
| 184 | + if (empty($discount)) { |
|
| 185 | 185 | return false; |
| 186 | 186 | } |
| 187 | 187 | |
@@ -190,30 +190,30 @@ discard block |
||
| 190 | 190 | // Prepare the return data. |
| 191 | 191 | $return = array( |
| 192 | 192 | 'ID' => $discount->ID, |
| 193 | - 'code' => get_post_meta( $discount->ID, '_wpi_discount_code', true ), |
|
| 194 | - 'amount' => get_post_meta( $discount->ID, '_wpi_discount_amount', true ), |
|
| 193 | + 'code' => get_post_meta($discount->ID, '_wpi_discount_code', true), |
|
| 194 | + 'amount' => get_post_meta($discount->ID, '_wpi_discount_amount', true), |
|
| 195 | 195 | 'date_created' => $discount->post_date, |
| 196 | 196 | 'date_modified' => $discount->post_modified, |
| 197 | 197 | 'status' => $discount->post_status, |
| 198 | - 'start' => get_post_meta( $discount->ID, '_wpi_discount_start', true ), |
|
| 199 | - 'expiration' => get_post_meta( $discount->ID, '_wpi_discount_expiration', true ), |
|
| 200 | - 'type' => get_post_meta( $discount->ID, '_wpi_discount_type', true ), |
|
| 198 | + 'start' => get_post_meta($discount->ID, '_wpi_discount_start', true), |
|
| 199 | + 'expiration' => get_post_meta($discount->ID, '_wpi_discount_expiration', true), |
|
| 200 | + 'type' => get_post_meta($discount->ID, '_wpi_discount_type', true), |
|
| 201 | 201 | 'description' => $discount->post_excerpt, |
| 202 | - 'uses' => get_post_meta( $discount->ID, '_wpi_discount_uses', true ), |
|
| 203 | - 'is_single_use' => get_post_meta( $discount->ID, '_wpi_discount_is_single_use', true ), |
|
| 204 | - 'items' => get_post_meta( $discount->ID, '_wpi_discount_items', true ), |
|
| 205 | - 'excluded_items' => get_post_meta( $discount->ID, '_wpi_discount_excluded_items', true ), |
|
| 206 | - 'max_uses' => get_post_meta( $discount->ID, '_wpi_discount_max_uses', true ), |
|
| 207 | - 'is_recurring' => get_post_meta( $discount->ID, '_wpi_discount_is_recurring', true ), |
|
| 208 | - 'min_total' => get_post_meta( $discount->ID, '_wpi_discount_min_total', true ), |
|
| 209 | - 'max_total' => get_post_meta( $discount->ID, '_wpi_discount_max_total', true ), |
|
| 202 | + 'uses' => get_post_meta($discount->ID, '_wpi_discount_uses', true), |
|
| 203 | + 'is_single_use' => get_post_meta($discount->ID, '_wpi_discount_is_single_use', true), |
|
| 204 | + 'items' => get_post_meta($discount->ID, '_wpi_discount_items', true), |
|
| 205 | + 'excluded_items' => get_post_meta($discount->ID, '_wpi_discount_excluded_items', true), |
|
| 206 | + 'max_uses' => get_post_meta($discount->ID, '_wpi_discount_max_uses', true), |
|
| 207 | + 'is_recurring' => get_post_meta($discount->ID, '_wpi_discount_is_recurring', true), |
|
| 208 | + 'min_total' => get_post_meta($discount->ID, '_wpi_discount_min_total', true), |
|
| 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 ); |
|
| 212 | + $return = apply_filters('wpinv_discount_properties', $return); |
|
| 213 | 213 | |
| 214 | 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' ); |
|
| 215 | + wp_cache_add($discount->ID, $return, 'WPInv_Discounts'); |
|
| 216 | + wp_cache_add($return['code'], $discount->ID, 'WPInv_Discount_Codes'); |
|
| 217 | 217 | |
| 218 | 218 | return $return; |
| 219 | 219 | } |
@@ -227,19 +227,19 @@ discard block |
||
| 227 | 227 | * @since 1.0.15 |
| 228 | 228 | * @return int |
| 229 | 229 | */ |
| 230 | - public static function get_discount_id_by_code( $discount_code ) { |
|
| 230 | + public static function get_discount_id_by_code($discount_code) { |
|
| 231 | 231 | |
| 232 | 232 | // Trim the code. |
| 233 | - $discount_code = trim( $discount_code ); |
|
| 233 | + $discount_code = trim($discount_code); |
|
| 234 | 234 | |
| 235 | 235 | // Ensure a value has been passed. |
| 236 | - if ( empty( $discount_code ) ) { |
|
| 236 | + if (empty($discount_code)) { |
|
| 237 | 237 | return 0; |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | // Maybe retrieve from the cache. |
| 241 | - $discount_id = wp_cache_get( $discount_code, 'getpaid_discount_codes' ); |
|
| 242 | - if ( ! empty( $discount_id ) ) { |
|
| 241 | + $discount_id = wp_cache_get($discount_code, 'getpaid_discount_codes'); |
|
| 242 | + if (!empty($discount_id)) { |
|
| 243 | 243 | return $discount_id; |
| 244 | 244 | } |
| 245 | 245 | |
@@ -250,19 +250,19 @@ discard block |
||
| 250 | 250 | 'meta_value' => $discount_code, |
| 251 | 251 | 'post_type' => 'wpi_discount', |
| 252 | 252 | 'posts_per_page' => 1, |
| 253 | - 'post_status' => array( 'publish', 'pending', 'draft', 'expired' ), |
|
| 253 | + 'post_status' => array('publish', 'pending', 'draft', 'expired'), |
|
| 254 | 254 | 'fields' => 'ids', |
| 255 | 255 | ) |
| 256 | 256 | ); |
| 257 | 257 | |
| 258 | - if ( empty( $discounts ) ) { |
|
| 258 | + if (empty($discounts)) { |
|
| 259 | 259 | return 0; |
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | $discount_id = $discounts[0]; |
| 263 | 263 | |
| 264 | 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' ); |
|
| 265 | + wp_cache_add(get_post_meta($discount_id, '_wpi_discount_code', true), $discount_id, 'getpaid_discount_codes'); |
|
| 266 | 266 | |
| 267 | 267 | return $discount_id; |
| 268 | 268 | } |
@@ -275,8 +275,8 @@ discard block |
||
| 275 | 275 | * |
| 276 | 276 | * @return bool Whether the given discount field is set. |
| 277 | 277 | */ |
| 278 | - public function __isset( $key ){ |
|
| 279 | - return isset( $this->data[$key] ) || method_exists( $this, "get_$key"); |
|
| 278 | + public function __isset($key) { |
|
| 279 | + return isset($this->data[$key]) || method_exists($this, "get_$key"); |
|
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | /* |
@@ -301,8 +301,8 @@ discard block |
||
| 301 | 301 | * @param string $context View or edit context. |
| 302 | 302 | * @return string |
| 303 | 303 | */ |
| 304 | - public function get_status( $context = 'view' ) { |
|
| 305 | - return $this->get_prop( 'status', $context ); |
|
| 304 | + public function get_status($context = 'view') { |
|
| 305 | + return $this->get_prop('status', $context); |
|
| 306 | 306 | } |
| 307 | 307 | |
| 308 | 308 | /** |
@@ -312,8 +312,8 @@ discard block |
||
| 312 | 312 | * @param string $context View or edit context. |
| 313 | 313 | * @return string |
| 314 | 314 | */ |
| 315 | - public function get_version( $context = 'view' ) { |
|
| 316 | - return $this->get_prop( 'version', $context ); |
|
| 315 | + public function get_version($context = 'view') { |
|
| 316 | + return $this->get_prop('version', $context); |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | /** |
@@ -323,8 +323,8 @@ discard block |
||
| 323 | 323 | * @param string $context View or edit context. |
| 324 | 324 | * @return string |
| 325 | 325 | */ |
| 326 | - public function get_date_created( $context = 'view' ) { |
|
| 327 | - return $this->get_prop( 'date_created', $context ); |
|
| 326 | + public function get_date_created($context = 'view') { |
|
| 327 | + return $this->get_prop('date_created', $context); |
|
| 328 | 328 | } |
| 329 | 329 | |
| 330 | 330 | /** |
@@ -334,11 +334,11 @@ discard block |
||
| 334 | 334 | * @param string $context View or edit context. |
| 335 | 335 | * @return string |
| 336 | 336 | */ |
| 337 | - public function get_date_created_gmt( $context = 'view' ) { |
|
| 338 | - $date = $this->get_date_created( $context ); |
|
| 337 | + public function get_date_created_gmt($context = 'view') { |
|
| 338 | + $date = $this->get_date_created($context); |
|
| 339 | 339 | |
| 340 | - if ( $date ) { |
|
| 341 | - $date = get_gmt_from_date( $date ); |
|
| 340 | + if ($date) { |
|
| 341 | + $date = get_gmt_from_date($date); |
|
| 342 | 342 | } |
| 343 | 343 | return $date; |
| 344 | 344 | } |
@@ -350,8 +350,8 @@ discard block |
||
| 350 | 350 | * @param string $context View or edit context. |
| 351 | 351 | * @return string |
| 352 | 352 | */ |
| 353 | - public function get_date_modified( $context = 'view' ) { |
|
| 354 | - return $this->get_prop( 'date_modified', $context ); |
|
| 353 | + public function get_date_modified($context = 'view') { |
|
| 354 | + return $this->get_prop('date_modified', $context); |
|
| 355 | 355 | } |
| 356 | 356 | |
| 357 | 357 | /** |
@@ -361,11 +361,11 @@ discard block |
||
| 361 | 361 | * @param string $context View or edit context. |
| 362 | 362 | * @return string |
| 363 | 363 | */ |
| 364 | - public function get_date_modified_gmt( $context = 'view' ) { |
|
| 365 | - $date = $this->get_date_modified( $context ); |
|
| 364 | + public function get_date_modified_gmt($context = 'view') { |
|
| 365 | + $date = $this->get_date_modified($context); |
|
| 366 | 366 | |
| 367 | - if ( $date ) { |
|
| 368 | - $date = get_gmt_from_date( $date ); |
|
| 367 | + if ($date) { |
|
| 368 | + $date = get_gmt_from_date($date); |
|
| 369 | 369 | } |
| 370 | 370 | return $date; |
| 371 | 371 | } |
@@ -377,8 +377,8 @@ discard block |
||
| 377 | 377 | * @param string $context View or edit context. |
| 378 | 378 | * @return string |
| 379 | 379 | */ |
| 380 | - public function get_name( $context = 'view' ) { |
|
| 381 | - return $this->get_prop( 'name', $context ); |
|
| 380 | + public function get_name($context = 'view') { |
|
| 381 | + return $this->get_prop('name', $context); |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -388,8 +388,8 @@ discard block |
||
| 388 | 388 | * @param string $context View or edit context. |
| 389 | 389 | * @return string |
| 390 | 390 | */ |
| 391 | - public function get_title( $context = 'view' ) { |
|
| 392 | - return $this->get_name( $context ); |
|
| 391 | + public function get_title($context = 'view') { |
|
| 392 | + return $this->get_name($context); |
|
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | /** |
@@ -399,8 +399,8 @@ discard block |
||
| 399 | 399 | * @param string $context View or edit context. |
| 400 | 400 | * @return string |
| 401 | 401 | */ |
| 402 | - public function get_description( $context = 'view' ) { |
|
| 403 | - return $this->get_prop( 'description', $context ); |
|
| 402 | + public function get_description($context = 'view') { |
|
| 403 | + return $this->get_prop('description', $context); |
|
| 404 | 404 | } |
| 405 | 405 | |
| 406 | 406 | /** |
@@ -410,8 +410,8 @@ discard block |
||
| 410 | 410 | * @param string $context View or edit context. |
| 411 | 411 | * @return string |
| 412 | 412 | */ |
| 413 | - public function get_excerpt( $context = 'view' ) { |
|
| 414 | - return $this->get_description( $context ); |
|
| 413 | + public function get_excerpt($context = 'view') { |
|
| 414 | + return $this->get_description($context); |
|
| 415 | 415 | } |
| 416 | 416 | |
| 417 | 417 | /** |
@@ -421,8 +421,8 @@ discard block |
||
| 421 | 421 | * @param string $context View or edit context. |
| 422 | 422 | * @return string |
| 423 | 423 | */ |
| 424 | - public function get_summary( $context = 'view' ) { |
|
| 425 | - return $this->get_description( $context ); |
|
| 424 | + public function get_summary($context = 'view') { |
|
| 425 | + return $this->get_description($context); |
|
| 426 | 426 | } |
| 427 | 427 | |
| 428 | 428 | /** |
@@ -432,8 +432,8 @@ discard block |
||
| 432 | 432 | * @param string $context View or edit context. |
| 433 | 433 | * @return string |
| 434 | 434 | */ |
| 435 | - public function get_author( $context = 'view' ) { |
|
| 436 | - return (int) $this->get_prop( 'author', $context ); |
|
| 435 | + public function get_author($context = 'view') { |
|
| 436 | + return (int) $this->get_prop('author', $context); |
|
| 437 | 437 | } |
| 438 | 438 | |
| 439 | 439 | /** |
@@ -443,8 +443,8 @@ discard block |
||
| 443 | 443 | * @param string $context View or edit context. |
| 444 | 444 | * @return string |
| 445 | 445 | */ |
| 446 | - public function get_code( $context = 'view' ) { |
|
| 447 | - return $this->get_prop( 'code', $context ); |
|
| 446 | + public function get_code($context = 'view') { |
|
| 447 | + return $this->get_prop('code', $context); |
|
| 448 | 448 | } |
| 449 | 449 | |
| 450 | 450 | /** |
@@ -454,8 +454,8 @@ discard block |
||
| 454 | 454 | * @param string $context View or edit context. |
| 455 | 455 | * @return string |
| 456 | 456 | */ |
| 457 | - public function get_coupon_code( $context = 'view' ) { |
|
| 458 | - return $this->get_code( $context ); |
|
| 457 | + public function get_coupon_code($context = 'view') { |
|
| 458 | + return $this->get_code($context); |
|
| 459 | 459 | } |
| 460 | 460 | |
| 461 | 461 | /** |
@@ -465,8 +465,8 @@ discard block |
||
| 465 | 465 | * @param string $context View or edit context. |
| 466 | 466 | * @return string |
| 467 | 467 | */ |
| 468 | - public function get_discount_code( $context = 'view' ) { |
|
| 469 | - return $this->get_code( $context ); |
|
| 468 | + public function get_discount_code($context = 'view') { |
|
| 469 | + return $this->get_code($context); |
|
| 470 | 470 | } |
| 471 | 471 | |
| 472 | 472 | /** |
@@ -476,8 +476,8 @@ discard block |
||
| 476 | 476 | * @param string $context View or edit context. |
| 477 | 477 | * @return float |
| 478 | 478 | */ |
| 479 | - public function get_amount( $context = 'view' ) { |
|
| 480 | - return $this->get_prop( 'amount', $context ); |
|
| 479 | + public function get_amount($context = 'view') { |
|
| 480 | + return $this->get_prop('amount', $context); |
|
| 481 | 481 | } |
| 482 | 482 | |
| 483 | 483 | /** |
@@ -488,23 +488,23 @@ discard block |
||
| 488 | 488 | */ |
| 489 | 489 | public function get_formatted_amount() { |
| 490 | 490 | |
| 491 | - if ( $this->is_type( 'flat' ) ) { |
|
| 492 | - $rate = wpinv_price( wpinv_format_amount( $this->get_amount() ) ); |
|
| 491 | + if ($this->is_type('flat')) { |
|
| 492 | + $rate = wpinv_price(wpinv_format_amount($this->get_amount())); |
|
| 493 | 493 | } else { |
| 494 | 494 | $rate = $this->get_amount() . '%'; |
| 495 | 495 | } |
| 496 | 496 | |
| 497 | - return apply_filters( 'wpinv_format_discount_rate', $rate, $this->get_type(), $this->get_amount() ); |
|
| 497 | + return apply_filters('wpinv_format_discount_rate', $rate, $this->get_type(), $this->get_amount()); |
|
| 498 | 498 | } |
| 499 | 499 | |
| 500 | - function wpinv_format_discount_rate( $type, $amount ) { |
|
| 501 | - if ( $type == 'flat' ) { |
|
| 502 | - $rate = wpinv_price( wpinv_format_amount( $amount ) ); |
|
| 500 | + function wpinv_format_discount_rate($type, $amount) { |
|
| 501 | + if ($type == 'flat') { |
|
| 502 | + $rate = wpinv_price(wpinv_format_amount($amount)); |
|
| 503 | 503 | } else { |
| 504 | 504 | $rate = $amount . '%'; |
| 505 | 505 | } |
| 506 | 506 | |
| 507 | - return apply_filters( 'wpinv_format_discount_rate', $rate, $type, $amount ); |
|
| 507 | + return apply_filters('wpinv_format_discount_rate', $rate, $type, $amount); |
|
| 508 | 508 | } |
| 509 | 509 | |
| 510 | 510 | /** |
@@ -514,8 +514,8 @@ discard block |
||
| 514 | 514 | * @param string $context View or edit context. |
| 515 | 515 | * @return string |
| 516 | 516 | */ |
| 517 | - public function get_start( $context = 'view' ) { |
|
| 518 | - return $this->get_prop( 'start', $context ); |
|
| 517 | + public function get_start($context = 'view') { |
|
| 518 | + return $this->get_prop('start', $context); |
|
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | /** |
@@ -525,8 +525,8 @@ discard block |
||
| 525 | 525 | * @param string $context View or edit context. |
| 526 | 526 | * @return string |
| 527 | 527 | */ |
| 528 | - public function get_start_date( $context = 'view' ) { |
|
| 529 | - return $this->get_start( $context ); |
|
| 528 | + public function get_start_date($context = 'view') { |
|
| 529 | + return $this->get_start($context); |
|
| 530 | 530 | } |
| 531 | 531 | |
| 532 | 532 | /** |
@@ -536,8 +536,8 @@ discard block |
||
| 536 | 536 | * @param string $context View or edit context. |
| 537 | 537 | * @return string |
| 538 | 538 | */ |
| 539 | - public function get_expiration( $context = 'view' ) { |
|
| 540 | - return $this->get_prop( 'expiration', $context ); |
|
| 539 | + public function get_expiration($context = 'view') { |
|
| 540 | + return $this->get_prop('expiration', $context); |
|
| 541 | 541 | } |
| 542 | 542 | |
| 543 | 543 | /** |
@@ -547,8 +547,8 @@ discard block |
||
| 547 | 547 | * @param string $context View or edit context. |
| 548 | 548 | * @return string |
| 549 | 549 | */ |
| 550 | - public function get_expiration_date( $context = 'view' ) { |
|
| 551 | - return $this->get_expiration( $context ); |
|
| 550 | + public function get_expiration_date($context = 'view') { |
|
| 551 | + return $this->get_expiration($context); |
|
| 552 | 552 | } |
| 553 | 553 | |
| 554 | 554 | /** |
@@ -558,8 +558,8 @@ discard block |
||
| 558 | 558 | * @param string $context View or edit context. |
| 559 | 559 | * @return string |
| 560 | 560 | */ |
| 561 | - public function get_end_date( $context = 'view' ) { |
|
| 562 | - return $this->get_expiration( $context ); |
|
| 561 | + public function get_end_date($context = 'view') { |
|
| 562 | + return $this->get_expiration($context); |
|
| 563 | 563 | } |
| 564 | 564 | |
| 565 | 565 | /** |
@@ -569,8 +569,8 @@ discard block |
||
| 569 | 569 | * @param string $context View or edit context. |
| 570 | 570 | * @return string |
| 571 | 571 | */ |
| 572 | - public function get_type( $context = 'view' ) { |
|
| 573 | - return $this->get_prop( 'type', $context ); |
|
| 572 | + public function get_type($context = 'view') { |
|
| 573 | + return $this->get_prop('type', $context); |
|
| 574 | 574 | } |
| 575 | 575 | |
| 576 | 576 | /** |
@@ -580,8 +580,8 @@ discard block |
||
| 580 | 580 | * @param string $context View or edit context. |
| 581 | 581 | * @return int |
| 582 | 582 | */ |
| 583 | - public function get_uses( $context = 'view' ) { |
|
| 584 | - return (int) $this->get_prop( 'uses', $context ); |
|
| 583 | + public function get_uses($context = 'view') { |
|
| 584 | + return (int) $this->get_prop('uses', $context); |
|
| 585 | 585 | } |
| 586 | 586 | |
| 587 | 587 | /** |
@@ -592,7 +592,7 @@ discard block |
||
| 592 | 592 | */ |
| 593 | 593 | public function get_usage() { |
| 594 | 594 | |
| 595 | - if ( ! $this->has_limit() ) { |
|
| 595 | + if (!$this->has_limit()) { |
|
| 596 | 596 | return $this->get_uses() . ' / ' . ' ∞'; |
| 597 | 597 | } |
| 598 | 598 | |
@@ -607,9 +607,9 @@ discard block |
||
| 607 | 607 | * @param string $context View or edit context. |
| 608 | 608 | * @return int |
| 609 | 609 | */ |
| 610 | - public function get_max_uses( $context = 'view' ) { |
|
| 611 | - $max_uses = $this->get_prop( 'max_uses', $context ); |
|
| 612 | - return empty( $max_uses ) ? null : $max_uses; |
|
| 610 | + public function get_max_uses($context = 'view') { |
|
| 611 | + $max_uses = $this->get_prop('max_uses', $context); |
|
| 612 | + return empty($max_uses) ? null : $max_uses; |
|
| 613 | 613 | } |
| 614 | 614 | |
| 615 | 615 | /** |
@@ -619,8 +619,8 @@ discard block |
||
| 619 | 619 | * @param string $context View or edit context. |
| 620 | 620 | * @return bool |
| 621 | 621 | */ |
| 622 | - public function get_is_single_use( $context = 'view' ) { |
|
| 623 | - return $this->get_prop( 'is_single_use', $context ); |
|
| 622 | + public function get_is_single_use($context = 'view') { |
|
| 623 | + return $this->get_prop('is_single_use', $context); |
|
| 624 | 624 | } |
| 625 | 625 | |
| 626 | 626 | /** |
@@ -630,8 +630,8 @@ discard block |
||
| 630 | 630 | * @param string $context View or edit context. |
| 631 | 631 | * @return array |
| 632 | 632 | */ |
| 633 | - public function get_items( $context = 'view' ) { |
|
| 634 | - return wpinv_parse_list( $this->get_prop( 'items', $context ) ); |
|
| 633 | + public function get_items($context = 'view') { |
|
| 634 | + return wpinv_parse_list($this->get_prop('items', $context)); |
|
| 635 | 635 | } |
| 636 | 636 | |
| 637 | 637 | /** |
@@ -641,8 +641,8 @@ discard block |
||
| 641 | 641 | * @param string $context View or edit context. |
| 642 | 642 | * @return array |
| 643 | 643 | */ |
| 644 | - public function get_allowed_items( $context = 'view' ) { |
|
| 645 | - return $this->get_items( $context ); |
|
| 644 | + public function get_allowed_items($context = 'view') { |
|
| 645 | + return $this->get_items($context); |
|
| 646 | 646 | } |
| 647 | 647 | |
| 648 | 648 | /** |
@@ -652,8 +652,8 @@ discard block |
||
| 652 | 652 | * @param string $context View or edit context. |
| 653 | 653 | * @return array |
| 654 | 654 | */ |
| 655 | - public function get_excluded_items( $context = 'view' ) { |
|
| 656 | - return wpinv_parse_list( $this->get_prop( 'excluded_items', $context ) ); |
|
| 655 | + public function get_excluded_items($context = 'view') { |
|
| 656 | + return wpinv_parse_list($this->get_prop('excluded_items', $context)); |
|
| 657 | 657 | } |
| 658 | 658 | |
| 659 | 659 | /** |
@@ -663,8 +663,8 @@ discard block |
||
| 663 | 663 | * @param string $context View or edit context. |
| 664 | 664 | * @return int|string|bool |
| 665 | 665 | */ |
| 666 | - public function get_is_recurring( $context = 'view' ) { |
|
| 667 | - return $this->get_prop( 'is_recurring', $context ); |
|
| 666 | + public function get_is_recurring($context = 'view') { |
|
| 667 | + return $this->get_prop('is_recurring', $context); |
|
| 668 | 668 | } |
| 669 | 669 | |
| 670 | 670 | /** |
@@ -674,9 +674,9 @@ discard block |
||
| 674 | 674 | * @param string $context View or edit context. |
| 675 | 675 | * @return float |
| 676 | 676 | */ |
| 677 | - public function get_min_total( $context = 'view' ) { |
|
| 678 | - $minimum = $this->get_prop( 'min_total', $context ); |
|
| 679 | - return empty( $minimum ) ? null : $minimum; |
|
| 677 | + public function get_min_total($context = 'view') { |
|
| 678 | + $minimum = $this->get_prop('min_total', $context); |
|
| 679 | + return empty($minimum) ? null : $minimum; |
|
| 680 | 680 | } |
| 681 | 681 | |
| 682 | 682 | /** |
@@ -686,8 +686,8 @@ discard block |
||
| 686 | 686 | * @param string $context View or edit context. |
| 687 | 687 | * @return float |
| 688 | 688 | */ |
| 689 | - public function get_minimum_total( $context = 'view' ) { |
|
| 690 | - return $this->get_min_total( $context ); |
|
| 689 | + public function get_minimum_total($context = 'view') { |
|
| 690 | + return $this->get_min_total($context); |
|
| 691 | 691 | } |
| 692 | 692 | |
| 693 | 693 | /** |
@@ -697,9 +697,9 @@ discard block |
||
| 697 | 697 | * @param string $context View or edit context. |
| 698 | 698 | * @return float |
| 699 | 699 | */ |
| 700 | - public function get_max_total( $context = 'view' ) { |
|
| 701 | - $maximum = $this->get_prop( 'max_total', $context ); |
|
| 702 | - return empty( $maximum ) ? null : $maximum; |
|
| 700 | + public function get_max_total($context = 'view') { |
|
| 701 | + $maximum = $this->get_prop('max_total', $context); |
|
| 702 | + return empty($maximum) ? null : $maximum; |
|
| 703 | 703 | } |
| 704 | 704 | |
| 705 | 705 | /** |
@@ -709,8 +709,8 @@ discard block |
||
| 709 | 709 | * @param string $context View or edit context. |
| 710 | 710 | * @return float |
| 711 | 711 | */ |
| 712 | - public function get_maximum_total( $context = 'view' ) { |
|
| 713 | - return $this->get_max_total( $context ); |
|
| 712 | + public function get_maximum_total($context = 'view') { |
|
| 713 | + return $this->get_max_total($context); |
|
| 714 | 714 | } |
| 715 | 715 | |
| 716 | 716 | /** |
@@ -723,8 +723,8 @@ discard block |
||
| 723 | 723 | * @param string $context View or edit context. |
| 724 | 724 | * @return mixed Value of the given discount property (if set). |
| 725 | 725 | */ |
| 726 | - public function get( $key, $context = 'view' ) { |
|
| 727 | - return $this->get_prop( $key, $context ); |
|
| 726 | + public function get($key, $context = 'view') { |
|
| 727 | + return $this->get_prop($key, $context); |
|
| 728 | 728 | } |
| 729 | 729 | |
| 730 | 730 | /* |
@@ -744,10 +744,10 @@ discard block |
||
| 744 | 744 | * @param string $status New status. |
| 745 | 745 | * @return array details of change. |
| 746 | 746 | */ |
| 747 | - public function set_status( $status ) { |
|
| 747 | + public function set_status($status) { |
|
| 748 | 748 | $old_status = $this->get_status(); |
| 749 | 749 | |
| 750 | - $this->set_prop( 'status', $status ); |
|
| 750 | + $this->set_prop('status', $status); |
|
| 751 | 751 | |
| 752 | 752 | return array( |
| 753 | 753 | 'from' => $old_status, |
@@ -760,8 +760,8 @@ discard block |
||
| 760 | 760 | * |
| 761 | 761 | * @since 1.0.19 |
| 762 | 762 | */ |
| 763 | - public function set_version( $value ) { |
|
| 764 | - $this->set_prop( 'version', $value ); |
|
| 763 | + public function set_version($value) { |
|
| 764 | + $this->set_prop('version', $value); |
|
| 765 | 765 | } |
| 766 | 766 | |
| 767 | 767 | /** |
@@ -771,11 +771,11 @@ discard block |
||
| 771 | 771 | * @param string $value Value to set. |
| 772 | 772 | * @return bool Whether or not the date was set. |
| 773 | 773 | */ |
| 774 | - public function set_date_created( $value ) { |
|
| 775 | - $date = strtotime( $value ); |
|
| 774 | + public function set_date_created($value) { |
|
| 775 | + $date = strtotime($value); |
|
| 776 | 776 | |
| 777 | - if ( $date ) { |
|
| 778 | - $this->set_prop( 'date_created', date( 'Y-m-d H:i:s', $date ) ); |
|
| 777 | + if ($date) { |
|
| 778 | + $this->set_prop('date_created', date('Y-m-d H:i:s', $date)); |
|
| 779 | 779 | return true; |
| 780 | 780 | } |
| 781 | 781 | |
@@ -790,11 +790,11 @@ discard block |
||
| 790 | 790 | * @param string $value Value to set. |
| 791 | 791 | * @return bool Whether or not the date was set. |
| 792 | 792 | */ |
| 793 | - public function set_date_modified( $value ) { |
|
| 794 | - $date = strtotime( $value ); |
|
| 793 | + public function set_date_modified($value) { |
|
| 794 | + $date = strtotime($value); |
|
| 795 | 795 | |
| 796 | - if ( $date ) { |
|
| 797 | - $this->set_prop( 'date_modified', date( 'Y-m-d H:i:s', $date ) ); |
|
| 796 | + if ($date) { |
|
| 797 | + $this->set_prop('date_modified', date('Y-m-d H:i:s', $date)); |
|
| 798 | 798 | return true; |
| 799 | 799 | } |
| 800 | 800 | |
@@ -808,9 +808,9 @@ discard block |
||
| 808 | 808 | * @since 1.0.19 |
| 809 | 809 | * @param string $value New name. |
| 810 | 810 | */ |
| 811 | - public function set_name( $value ) { |
|
| 812 | - $name = sanitize_text_field( $value ); |
|
| 813 | - $this->set_prop( 'name', $name ); |
|
| 811 | + public function set_name($value) { |
|
| 812 | + $name = sanitize_text_field($value); |
|
| 813 | + $this->set_prop('name', $name); |
|
| 814 | 814 | } |
| 815 | 815 | |
| 816 | 816 | /** |
@@ -819,8 +819,8 @@ discard block |
||
| 819 | 819 | * @since 1.0.19 |
| 820 | 820 | * @param string $value New name. |
| 821 | 821 | */ |
| 822 | - public function set_title( $value ) { |
|
| 823 | - $this->set_name( $value ); |
|
| 822 | + public function set_title($value) { |
|
| 823 | + $this->set_name($value); |
|
| 824 | 824 | } |
| 825 | 825 | |
| 826 | 826 | /** |
@@ -829,9 +829,9 @@ discard block |
||
| 829 | 829 | * @since 1.0.19 |
| 830 | 830 | * @param string $value New description. |
| 831 | 831 | */ |
| 832 | - public function set_description( $value ) { |
|
| 833 | - $description = wp_kses_post( $value ); |
|
| 834 | - return $this->set_prop( 'description', $description ); |
|
| 832 | + public function set_description($value) { |
|
| 833 | + $description = wp_kses_post($value); |
|
| 834 | + return $this->set_prop('description', $description); |
|
| 835 | 835 | } |
| 836 | 836 | |
| 837 | 837 | /** |
@@ -840,8 +840,8 @@ discard block |
||
| 840 | 840 | * @since 1.0.19 |
| 841 | 841 | * @param string $value New description. |
| 842 | 842 | */ |
| 843 | - public function set_excerpt( $value ) { |
|
| 844 | - $this->set_description( $value ); |
|
| 843 | + public function set_excerpt($value) { |
|
| 844 | + $this->set_description($value); |
|
| 845 | 845 | } |
| 846 | 846 | |
| 847 | 847 | /** |
@@ -850,8 +850,8 @@ discard block |
||
| 850 | 850 | * @since 1.0.19 |
| 851 | 851 | * @param string $value New description. |
| 852 | 852 | */ |
| 853 | - public function set_summary( $value ) { |
|
| 854 | - $this->set_description( $value ); |
|
| 853 | + public function set_summary($value) { |
|
| 854 | + $this->set_description($value); |
|
| 855 | 855 | } |
| 856 | 856 | |
| 857 | 857 | /** |
@@ -860,8 +860,8 @@ discard block |
||
| 860 | 860 | * @since 1.0.19 |
| 861 | 861 | * @param int $value New author. |
| 862 | 862 | */ |
| 863 | - public function set_author( $value ) { |
|
| 864 | - $this->set_prop( 'author', (int) $value ); |
|
| 863 | + public function set_author($value) { |
|
| 864 | + $this->set_prop('author', (int) $value); |
|
| 865 | 865 | } |
| 866 | 866 | |
| 867 | 867 | /** |
@@ -870,9 +870,9 @@ discard block |
||
| 870 | 870 | * @since 1.0.19 |
| 871 | 871 | * @param string $value New discount code. |
| 872 | 872 | */ |
| 873 | - public function set_code( $value ) { |
|
| 874 | - $code = sanitize_text_field( $value ); |
|
| 875 | - $this->set_prop( 'code', $code ); |
|
| 873 | + public function set_code($value) { |
|
| 874 | + $code = sanitize_text_field($value); |
|
| 875 | + $this->set_prop('code', $code); |
|
| 876 | 876 | } |
| 877 | 877 | |
| 878 | 878 | /** |
@@ -881,8 +881,8 @@ discard block |
||
| 881 | 881 | * @since 1.0.19 |
| 882 | 882 | * @param string $value New discount code. |
| 883 | 883 | */ |
| 884 | - public function set_coupon_code( $value ) { |
|
| 885 | - $this->set_code( $value ); |
|
| 884 | + public function set_coupon_code($value) { |
|
| 885 | + $this->set_code($value); |
|
| 886 | 886 | } |
| 887 | 887 | |
| 888 | 888 | /** |
@@ -891,8 +891,8 @@ discard block |
||
| 891 | 891 | * @since 1.0.19 |
| 892 | 892 | * @param string $value New discount code. |
| 893 | 893 | */ |
| 894 | - public function set_discount_code( $value ) { |
|
| 895 | - $this->set_code( $value ); |
|
| 894 | + public function set_discount_code($value) { |
|
| 895 | + $this->set_code($value); |
|
| 896 | 896 | } |
| 897 | 897 | |
| 898 | 898 | /** |
@@ -901,9 +901,9 @@ discard block |
||
| 901 | 901 | * @since 1.0.19 |
| 902 | 902 | * @param float $value New discount code. |
| 903 | 903 | */ |
| 904 | - public function set_amount( $value ) { |
|
| 905 | - $amount = floatval( wpinv_sanitize_amount( $value ) ); |
|
| 906 | - $this->set_prop( 'amount', $amount ); |
|
| 904 | + public function set_amount($value) { |
|
| 905 | + $amount = floatval(wpinv_sanitize_amount($value)); |
|
| 906 | + $this->set_prop('amount', $amount); |
|
| 907 | 907 | } |
| 908 | 908 | |
| 909 | 909 | /** |
@@ -912,15 +912,15 @@ discard block |
||
| 912 | 912 | * @since 1.0.19 |
| 913 | 913 | * @param float $value New start date. |
| 914 | 914 | */ |
| 915 | - public function set_start( $value ) { |
|
| 916 | - $date = strtotime( $value ); |
|
| 915 | + public function set_start($value) { |
|
| 916 | + $date = strtotime($value); |
|
| 917 | 917 | |
| 918 | - if ( $date ) { |
|
| 919 | - $this->set_prop( 'start', date( 'Y-m-d H:i', $date ) ); |
|
| 918 | + if ($date) { |
|
| 919 | + $this->set_prop('start', date('Y-m-d H:i', $date)); |
|
| 920 | 920 | return true; |
| 921 | 921 | } |
| 922 | 922 | |
| 923 | - $this->set_prop( 'start', '' ); |
|
| 923 | + $this->set_prop('start', ''); |
|
| 924 | 924 | |
| 925 | 925 | return false; |
| 926 | 926 | } |
@@ -931,8 +931,8 @@ discard block |
||
| 931 | 931 | * @since 1.0.19 |
| 932 | 932 | * @param string $value New start date. |
| 933 | 933 | */ |
| 934 | - public function set_start_date( $value ) { |
|
| 935 | - $this->set_start( $value ); |
|
| 934 | + public function set_start_date($value) { |
|
| 935 | + $this->set_start($value); |
|
| 936 | 936 | } |
| 937 | 937 | |
| 938 | 938 | /** |
@@ -941,15 +941,15 @@ discard block |
||
| 941 | 941 | * @since 1.0.19 |
| 942 | 942 | * @param float $value New expiration date. |
| 943 | 943 | */ |
| 944 | - public function set_expiration( $value ) { |
|
| 945 | - $date = strtotime( $value ); |
|
| 944 | + public function set_expiration($value) { |
|
| 945 | + $date = strtotime($value); |
|
| 946 | 946 | |
| 947 | - if ( $date ) { |
|
| 948 | - $this->set_prop( 'expiration', date( 'Y-m-d H:i', $date ) ); |
|
| 947 | + if ($date) { |
|
| 948 | + $this->set_prop('expiration', date('Y-m-d H:i', $date)); |
|
| 949 | 949 | return true; |
| 950 | 950 | } |
| 951 | 951 | |
| 952 | - $this->set_prop( 'expiration', '' ); |
|
| 952 | + $this->set_prop('expiration', ''); |
|
| 953 | 953 | return false; |
| 954 | 954 | } |
| 955 | 955 | |
@@ -959,8 +959,8 @@ discard block |
||
| 959 | 959 | * @since 1.0.19 |
| 960 | 960 | * @param string $value New expiration date. |
| 961 | 961 | */ |
| 962 | - public function set_expiration_date( $value ) { |
|
| 963 | - $this->set_expiration( $value ); |
|
| 962 | + public function set_expiration_date($value) { |
|
| 963 | + $this->set_expiration($value); |
|
| 964 | 964 | } |
| 965 | 965 | |
| 966 | 966 | /** |
@@ -969,8 +969,8 @@ discard block |
||
| 969 | 969 | * @since 1.0.19 |
| 970 | 970 | * @param string $value New expiration date. |
| 971 | 971 | */ |
| 972 | - public function set_end_date( $value ) { |
|
| 973 | - $this->set_expiration( $value ); |
|
| 972 | + public function set_end_date($value) { |
|
| 973 | + $this->set_expiration($value); |
|
| 974 | 974 | } |
| 975 | 975 | |
| 976 | 976 | /** |
@@ -979,9 +979,9 @@ discard block |
||
| 979 | 979 | * @since 1.0.19 |
| 980 | 980 | * @param string $value New discount type. |
| 981 | 981 | */ |
| 982 | - public function set_type( $value ) { |
|
| 983 | - if ( $value && array_key_exists( sanitize_text_field( $value ), wpinv_get_discount_types() ) ) { |
|
| 984 | - $this->set_prop( 'type', sanitize_text_field( $value ) ); |
|
| 982 | + public function set_type($value) { |
|
| 983 | + if ($value && array_key_exists(sanitize_text_field($value), wpinv_get_discount_types())) { |
|
| 984 | + $this->set_prop('type', sanitize_text_field($value)); |
|
| 985 | 985 | } |
| 986 | 986 | } |
| 987 | 987 | |
@@ -991,15 +991,15 @@ discard block |
||
| 991 | 991 | * @since 1.0.19 |
| 992 | 992 | * @param int $value usage count. |
| 993 | 993 | */ |
| 994 | - public function set_uses( $value ) { |
|
| 994 | + public function set_uses($value) { |
|
| 995 | 995 | |
| 996 | 996 | $value = (int) $value; |
| 997 | 997 | |
| 998 | - if ( $value < 0 ) { |
|
| 998 | + if ($value < 0) { |
|
| 999 | 999 | $value = 0; |
| 1000 | 1000 | } |
| 1001 | 1001 | |
| 1002 | - $this->set_prop( 'uses', (int) $value ); |
|
| 1002 | + $this->set_prop('uses', (int) $value); |
|
| 1003 | 1003 | } |
| 1004 | 1004 | |
| 1005 | 1005 | /** |
@@ -1008,8 +1008,8 @@ discard block |
||
| 1008 | 1008 | * @since 1.0.19 |
| 1009 | 1009 | * @param int $value maximum usage count. |
| 1010 | 1010 | */ |
| 1011 | - public function set_max_uses( $value ) { |
|
| 1012 | - $this->set_prop( 'max_uses', absint( $value ) ); |
|
| 1011 | + public function set_max_uses($value) { |
|
| 1012 | + $this->set_prop('max_uses', absint($value)); |
|
| 1013 | 1013 | } |
| 1014 | 1014 | |
| 1015 | 1015 | /** |
@@ -1018,8 +1018,8 @@ discard block |
||
| 1018 | 1018 | * @since 1.0.19 |
| 1019 | 1019 | * @param int|bool $value is single use. |
| 1020 | 1020 | */ |
| 1021 | - public function set_is_single_use( $value ) { |
|
| 1022 | - $this->set_prop( 'is_single_use', (bool) $value ); |
|
| 1021 | + public function set_is_single_use($value) { |
|
| 1022 | + $this->set_prop('is_single_use', (bool) $value); |
|
| 1023 | 1023 | } |
| 1024 | 1024 | |
| 1025 | 1025 | /** |
@@ -1028,8 +1028,8 @@ discard block |
||
| 1028 | 1028 | * @since 1.0.19 |
| 1029 | 1029 | * @param array $value items. |
| 1030 | 1030 | */ |
| 1031 | - public function set_items( $value ) { |
|
| 1032 | - $this->set_prop( 'items', wpinv_parse_list( $value ) ); |
|
| 1031 | + public function set_items($value) { |
|
| 1032 | + $this->set_prop('items', wpinv_parse_list($value)); |
|
| 1033 | 1033 | } |
| 1034 | 1034 | |
| 1035 | 1035 | /** |
@@ -1038,8 +1038,8 @@ discard block |
||
| 1038 | 1038 | * @since 1.0.19 |
| 1039 | 1039 | * @param array $value items. |
| 1040 | 1040 | */ |
| 1041 | - public function set_allowed_items( $value ) { |
|
| 1042 | - $this->set_items( $value ); |
|
| 1041 | + public function set_allowed_items($value) { |
|
| 1042 | + $this->set_items($value); |
|
| 1043 | 1043 | } |
| 1044 | 1044 | |
| 1045 | 1045 | /** |
@@ -1048,8 +1048,8 @@ discard block |
||
| 1048 | 1048 | * @since 1.0.19 |
| 1049 | 1049 | * @param array $value items. |
| 1050 | 1050 | */ |
| 1051 | - public function set_excluded_items( $value ) { |
|
| 1052 | - $this->set_prop( 'excluded_items', wpinv_parse_list( $value ) ); |
|
| 1051 | + public function set_excluded_items($value) { |
|
| 1052 | + $this->set_prop('excluded_items', wpinv_parse_list($value)); |
|
| 1053 | 1053 | } |
| 1054 | 1054 | |
| 1055 | 1055 | /** |
@@ -1058,8 +1058,8 @@ discard block |
||
| 1058 | 1058 | * @since 1.0.19 |
| 1059 | 1059 | * @param int|bool $value is recurring. |
| 1060 | 1060 | */ |
| 1061 | - public function set_is_recurring( $value ) { |
|
| 1062 | - $this->set_prop( 'is_recurring', (bool) $value ); |
|
| 1061 | + public function set_is_recurring($value) { |
|
| 1062 | + $this->set_prop('is_recurring', (bool) $value); |
|
| 1063 | 1063 | } |
| 1064 | 1064 | |
| 1065 | 1065 | /** |
@@ -1068,8 +1068,8 @@ discard block |
||
| 1068 | 1068 | * @since 1.0.19 |
| 1069 | 1069 | * @param float $value minimum total. |
| 1070 | 1070 | */ |
| 1071 | - public function set_min_total( $value ) { |
|
| 1072 | - $this->set_prop( 'min_total', (float) wpinv_sanitize_amount( $value ) ); |
|
| 1071 | + public function set_min_total($value) { |
|
| 1072 | + $this->set_prop('min_total', (float) wpinv_sanitize_amount($value)); |
|
| 1073 | 1073 | } |
| 1074 | 1074 | |
| 1075 | 1075 | /** |
@@ -1078,8 +1078,8 @@ discard block |
||
| 1078 | 1078 | * @since 1.0.19 |
| 1079 | 1079 | * @param float $value minimum total. |
| 1080 | 1080 | */ |
| 1081 | - public function set_minimum_total( $value ) { |
|
| 1082 | - $this->set_min_total( $value ); |
|
| 1081 | + public function set_minimum_total($value) { |
|
| 1082 | + $this->set_min_total($value); |
|
| 1083 | 1083 | } |
| 1084 | 1084 | |
| 1085 | 1085 | /** |
@@ -1088,8 +1088,8 @@ discard block |
||
| 1088 | 1088 | * @since 1.0.19 |
| 1089 | 1089 | * @param float $value maximum total. |
| 1090 | 1090 | */ |
| 1091 | - public function set_max_total( $value ) { |
|
| 1092 | - $this->set_prop( 'max_total', (float) wpinv_sanitize_amount( $value ) ); |
|
| 1091 | + public function set_max_total($value) { |
|
| 1092 | + $this->set_prop('max_total', (float) wpinv_sanitize_amount($value)); |
|
| 1093 | 1093 | } |
| 1094 | 1094 | |
| 1095 | 1095 | /** |
@@ -1098,23 +1098,23 @@ discard block |
||
| 1098 | 1098 | * @since 1.0.19 |
| 1099 | 1099 | * @param float $value maximum total. |
| 1100 | 1100 | */ |
| 1101 | - public function set_maximum_total( $value ) { |
|
| 1102 | - $this->set_max_total( $value ); |
|
| 1101 | + public function set_maximum_total($value) { |
|
| 1102 | + $this->set_max_total($value); |
|
| 1103 | 1103 | } |
| 1104 | 1104 | |
| 1105 | 1105 | /** |
| 1106 | 1106 | * @deprecated |
| 1107 | 1107 | */ |
| 1108 | - public function refresh(){} |
|
| 1108 | + public function refresh() {} |
|
| 1109 | 1109 | |
| 1110 | 1110 | /** |
| 1111 | 1111 | * @deprecated |
| 1112 | 1112 | * |
| 1113 | 1113 | */ |
| 1114 | - public function update_status( $status = 'publish' ){ |
|
| 1114 | + public function update_status($status = 'publish') { |
|
| 1115 | 1115 | |
| 1116 | - if ( $this->exists() && $this->get_status() != $status ) { |
|
| 1117 | - $this->set_status( $status ); |
|
| 1116 | + if ($this->exists() && $this->get_status() != $status) { |
|
| 1117 | + $this->set_status($status); |
|
| 1118 | 1118 | $this->save(); |
| 1119 | 1119 | } |
| 1120 | 1120 | |
@@ -1134,9 +1134,9 @@ discard block |
||
| 1134 | 1134 | * |
| 1135 | 1135 | * @since 1.0.15 |
| 1136 | 1136 | */ |
| 1137 | - public function exists(){ |
|
| 1137 | + public function exists() { |
|
| 1138 | 1138 | $id = $this->get_id(); |
| 1139 | - return ! empty( $id ); |
|
| 1139 | + return !empty($id); |
|
| 1140 | 1140 | } |
| 1141 | 1141 | |
| 1142 | 1142 | /** |
@@ -1147,7 +1147,7 @@ discard block |
||
| 1147 | 1147 | * @since 1.0.15 |
| 1148 | 1148 | * @return bool |
| 1149 | 1149 | */ |
| 1150 | - public function is_type( $type ) { |
|
| 1150 | + public function is_type($type) { |
|
| 1151 | 1151 | return $this->get_type() == $type; |
| 1152 | 1152 | } |
| 1153 | 1153 | |
@@ -1169,7 +1169,7 @@ discard block |
||
| 1169 | 1169 | */ |
| 1170 | 1170 | public function has_limit() { |
| 1171 | 1171 | $limit = $this->get_max_uses(); |
| 1172 | - return ! empty( $limit ); |
|
| 1172 | + return !empty($limit); |
|
| 1173 | 1173 | } |
| 1174 | 1174 | |
| 1175 | 1175 | /** |
@@ -1190,13 +1190,13 @@ discard block |
||
| 1190 | 1190 | */ |
| 1191 | 1191 | public function has_exceeded_limit() { |
| 1192 | 1192 | |
| 1193 | - if ( ! $this->has_limit() || ! $this->has_uses() ) { |
|
| 1194 | - $exceeded = false ; |
|
| 1193 | + if (!$this->has_limit() || !$this->has_uses()) { |
|
| 1194 | + $exceeded = false; |
|
| 1195 | 1195 | } else { |
| 1196 | - $exceeded = ! ( (int) $this->get_max_uses() < $this->get_uses() ); |
|
| 1196 | + $exceeded = !((int) $this->get_max_uses() < $this->get_uses()); |
|
| 1197 | 1197 | } |
| 1198 | 1198 | |
| 1199 | - return apply_filters( 'wpinv_is_discount_maxed_out', $exceeded, $this->ID, $this, $this->code ); |
|
| 1199 | + return apply_filters('wpinv_is_discount_maxed_out', $exceeded, $this->ID, $this, $this->code); |
|
| 1200 | 1200 | } |
| 1201 | 1201 | |
| 1202 | 1202 | /** |
@@ -1207,7 +1207,7 @@ discard block |
||
| 1207 | 1207 | */ |
| 1208 | 1208 | public function has_expiration_date() { |
| 1209 | 1209 | $date = $this->get_expiration_date(); |
| 1210 | - return ! empty( $date ); |
|
| 1210 | + return !empty($date); |
|
| 1211 | 1211 | } |
| 1212 | 1212 | |
| 1213 | 1213 | /** |
@@ -1217,8 +1217,8 @@ discard block |
||
| 1217 | 1217 | * @return bool |
| 1218 | 1218 | */ |
| 1219 | 1219 | public function is_expired() { |
| 1220 | - $expired = $this->has_expiration_date() ? current_time( 'timestamp' ) > strtotime( $this->get_expiration_date() ) : false; |
|
| 1221 | - return apply_filters( 'wpinv_is_discount_expired', $expired, $this->ID, $this, $this->code ); |
|
| 1220 | + $expired = $this->has_expiration_date() ? current_time('timestamp') > strtotime($this->get_expiration_date()) : false; |
|
| 1221 | + return apply_filters('wpinv_is_discount_expired', $expired, $this->ID, $this, $this->code); |
|
| 1222 | 1222 | } |
| 1223 | 1223 | |
| 1224 | 1224 | /** |
@@ -1229,7 +1229,7 @@ discard block |
||
| 1229 | 1229 | */ |
| 1230 | 1230 | public function has_start_date() { |
| 1231 | 1231 | $date = $this->get_start_date(); |
| 1232 | - return ! empty( $date ); |
|
| 1232 | + return !empty($date); |
|
| 1233 | 1233 | } |
| 1234 | 1234 | |
| 1235 | 1235 | /** |
@@ -1239,8 +1239,8 @@ discard block |
||
| 1239 | 1239 | * @return bool |
| 1240 | 1240 | */ |
| 1241 | 1241 | public function has_started() { |
| 1242 | - $started = $this->has_start_date() ? true : current_time( 'timestamp' ) > strtotime( $this->get_start_date() ); |
|
| 1243 | - return apply_filters( 'wpinv_is_discount_started', $started, $this->ID, $this, $this->code ); |
|
| 1242 | + $started = $this->has_start_date() ? true : current_time('timestamp') > strtotime($this->get_start_date()); |
|
| 1243 | + return apply_filters('wpinv_is_discount_started', $started, $this->ID, $this, $this->code); |
|
| 1244 | 1244 | } |
| 1245 | 1245 | |
| 1246 | 1246 | /** |
@@ -1251,7 +1251,7 @@ discard block |
||
| 1251 | 1251 | */ |
| 1252 | 1252 | public function has_allowed_items() { |
| 1253 | 1253 | $allowed_items = $this->get_allowed_items(); |
| 1254 | - return empty( $allowed_items ); |
|
| 1254 | + return empty($allowed_items); |
|
| 1255 | 1255 | } |
| 1256 | 1256 | |
| 1257 | 1257 | /** |
@@ -1262,7 +1262,7 @@ discard block |
||
| 1262 | 1262 | */ |
| 1263 | 1263 | public function has_excluded_items() { |
| 1264 | 1264 | $excluded_items = $this->get_excluded_items(); |
| 1265 | - return empty( $excluded_items ); |
|
| 1265 | + return empty($excluded_items); |
|
| 1266 | 1266 | } |
| 1267 | 1267 | |
| 1268 | 1268 | /** |
@@ -1272,17 +1272,17 @@ discard block |
||
| 1272 | 1272 | * @since 1.0.15 |
| 1273 | 1273 | * @return boolean |
| 1274 | 1274 | */ |
| 1275 | - public function is_valid_for_items( $item_ids ) { |
|
| 1275 | + public function is_valid_for_items($item_ids) { |
|
| 1276 | 1276 | |
| 1277 | - $item_ids = array_map( 'intval', wpinv_parse_list( $item_ids ) ); |
|
| 1278 | - $included = array_intersect( $item_ids, $this->get_allowed_items() ); |
|
| 1279 | - $excluded = array_intersect( $item_ids, $this->get_excluded_items() ); |
|
| 1277 | + $item_ids = array_map('intval', wpinv_parse_list($item_ids)); |
|
| 1278 | + $included = array_intersect($item_ids, $this->get_allowed_items()); |
|
| 1279 | + $excluded = array_intersect($item_ids, $this->get_excluded_items()); |
|
| 1280 | 1280 | |
| 1281 | - if ( $this->has_excluded_items() && ! empty( $excluded ) ) { |
|
| 1281 | + if ($this->has_excluded_items() && !empty($excluded)) { |
|
| 1282 | 1282 | return false; |
| 1283 | 1283 | } |
| 1284 | 1284 | |
| 1285 | - if ( $this->has_allowed_items() && empty( $included ) ) { |
|
| 1285 | + if ($this->has_allowed_items() && empty($included)) { |
|
| 1286 | 1286 | return false; |
| 1287 | 1287 | } |
| 1288 | 1288 | return true; |
@@ -1295,8 +1295,8 @@ discard block |
||
| 1295 | 1295 | * @since 1.0.15 |
| 1296 | 1296 | * @return boolean |
| 1297 | 1297 | */ |
| 1298 | - public function is_valid_for_amount( $amount ) { |
|
| 1299 | - return $this->is_minimum_amount_met( $amount ) && $this->is_maximum_amount_met( $amount ); |
|
| 1298 | + public function is_valid_for_amount($amount) { |
|
| 1299 | + return $this->is_minimum_amount_met($amount) && $this->is_maximum_amount_met($amount); |
|
| 1300 | 1300 | } |
| 1301 | 1301 | |
| 1302 | 1302 | /** |
@@ -1307,7 +1307,7 @@ discard block |
||
| 1307 | 1307 | */ |
| 1308 | 1308 | public function has_minimum_amount() { |
| 1309 | 1309 | $minimum = $this->get_minimum_total(); |
| 1310 | - return ! empty( $minimum ); |
|
| 1310 | + return !empty($minimum); |
|
| 1311 | 1311 | } |
| 1312 | 1312 | |
| 1313 | 1313 | /** |
@@ -1317,10 +1317,10 @@ discard block |
||
| 1317 | 1317 | * @since 1.0.15 |
| 1318 | 1318 | * @return boolean |
| 1319 | 1319 | */ |
| 1320 | - public function is_minimum_amount_met( $amount ) { |
|
| 1321 | - $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
| 1322 | - $min_met= ! ( $this->has_minimum_amount() && $amount < floatval( wpinv_sanitize_amount( $this->get_minimum_total() ) ) ); |
|
| 1323 | - return apply_filters( 'wpinv_is_discount_min_met', $min_met, $this->ID, $this, $this->code, $amount ); |
|
| 1320 | + public function is_minimum_amount_met($amount) { |
|
| 1321 | + $amount = floatval(wpinv_sanitize_amount($amount)); |
|
| 1322 | + $min_met = !($this->has_minimum_amount() && $amount < floatval(wpinv_sanitize_amount($this->get_minimum_total()))); |
|
| 1323 | + return apply_filters('wpinv_is_discount_min_met', $min_met, $this->ID, $this, $this->code, $amount); |
|
| 1324 | 1324 | } |
| 1325 | 1325 | |
| 1326 | 1326 | /** |
@@ -1331,7 +1331,7 @@ discard block |
||
| 1331 | 1331 | */ |
| 1332 | 1332 | public function has_maximum_amount() { |
| 1333 | 1333 | $maximum = $this->get_maximum_total(); |
| 1334 | - return ! empty( $maximum ); |
|
| 1334 | + return !empty($maximum); |
|
| 1335 | 1335 | } |
| 1336 | 1336 | |
| 1337 | 1337 | /** |
@@ -1341,10 +1341,10 @@ discard block |
||
| 1341 | 1341 | * @since 1.0.15 |
| 1342 | 1342 | * @return boolean |
| 1343 | 1343 | */ |
| 1344 | - public function is_maximum_amount_met( $amount ) { |
|
| 1345 | - $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
| 1346 | - $max_met= ! ( $this->has_maximum_amount() && $amount > floatval( wpinv_sanitize_amount( $this->get_maximum_total() ) ) ); |
|
| 1347 | - return apply_filters( 'wpinv_is_discount_max_met', $max_met, $this->ID, $this, $this->code, $amount ); |
|
| 1344 | + public function is_maximum_amount_met($amount) { |
|
| 1345 | + $amount = floatval(wpinv_sanitize_amount($amount)); |
|
| 1346 | + $max_met = !($this->has_maximum_amount() && $amount > floatval(wpinv_sanitize_amount($this->get_maximum_total()))); |
|
| 1347 | + return apply_filters('wpinv_is_discount_max_met', $max_met, $this->ID, $this, $this->code, $amount); |
|
| 1348 | 1348 | } |
| 1349 | 1349 | |
| 1350 | 1350 | /** |
@@ -1355,7 +1355,7 @@ discard block |
||
| 1355 | 1355 | */ |
| 1356 | 1356 | public function is_recurring() { |
| 1357 | 1357 | $recurring = $this->get_is_recurring(); |
| 1358 | - return ! empty( $recurring ); |
|
| 1358 | + return !empty($recurring); |
|
| 1359 | 1359 | } |
| 1360 | 1360 | |
| 1361 | 1361 | /** |
@@ -1366,7 +1366,7 @@ discard block |
||
| 1366 | 1366 | */ |
| 1367 | 1367 | public function is_single_use() { |
| 1368 | 1368 | $usage = $this->get_is_single_use(); |
| 1369 | - return ! empty( $usage ); |
|
| 1369 | + return !empty($usage); |
|
| 1370 | 1370 | } |
| 1371 | 1371 | |
| 1372 | 1372 | /** |
@@ -1376,57 +1376,57 @@ discard block |
||
| 1376 | 1376 | * @since 1.0.15 |
| 1377 | 1377 | * @return boolean |
| 1378 | 1378 | */ |
| 1379 | - public function is_valid_for_user( $user ) { |
|
| 1379 | + public function is_valid_for_user($user) { |
|
| 1380 | 1380 | global $wpi_checkout_id; |
| 1381 | 1381 | |
| 1382 | 1382 | // Ensure that the discount is single use. |
| 1383 | - if ( empty( $user ) || ! $this->is_single_use() ) { |
|
| 1383 | + if (empty($user) || !$this->is_single_use()) { |
|
| 1384 | 1384 | return true; |
| 1385 | 1385 | } |
| 1386 | 1386 | |
| 1387 | 1387 | // Prepare the user id. |
| 1388 | 1388 | $user_id = 0; |
| 1389 | - if ( is_int( $user ) ) { |
|
| 1390 | - $user_id = absint( $user ); |
|
| 1391 | - } else if ( is_email( $user ) && $user_data = get_user_by( 'email', $user ) ) { |
|
| 1389 | + if (is_int($user)) { |
|
| 1390 | + $user_id = absint($user); |
|
| 1391 | + } else if (is_email($user) && $user_data = get_user_by('email', $user)) { |
|
| 1392 | 1392 | $user_id = $user_data->ID; |
| 1393 | - } else if ( $user_data = get_user_by( 'login', $user ) ) { |
|
| 1393 | + } else if ($user_data = get_user_by('login', $user)) { |
|
| 1394 | 1394 | $user_id = $user_data->ID; |
| 1395 | - } else if ( absint( $user ) > 0 ) { |
|
| 1396 | - $user_id = absint( $user ); |
|
| 1395 | + } else if (absint($user) > 0) { |
|
| 1396 | + $user_id = absint($user); |
|
| 1397 | 1397 | } |
| 1398 | 1398 | |
| 1399 | 1399 | // Ensure that we have a user. |
| 1400 | - if ( empty( $user_id ) ) { |
|
| 1400 | + if (empty($user_id)) { |
|
| 1401 | 1401 | return true; |
| 1402 | 1402 | } |
| 1403 | 1403 | |
| 1404 | 1404 | // Get all payments with matching user id. |
| 1405 | - $payments = wpinv_get_invoices( array( 'user' => $user_id, 'limit' => false ) ); |
|
| 1406 | - $code = strtolower( $this->get_code() ); |
|
| 1405 | + $payments = wpinv_get_invoices(array('user' => $user_id, 'limit' => false)); |
|
| 1406 | + $code = strtolower($this->get_code()); |
|
| 1407 | 1407 | |
| 1408 | 1408 | // For each payment... |
| 1409 | - foreach ( $payments as $payment ) { |
|
| 1409 | + foreach ($payments as $payment) { |
|
| 1410 | 1410 | |
| 1411 | 1411 | // ... skip the current payment. |
| 1412 | - if ( ! empty( $wpi_checkout_id ) && $wpi_checkout_id == $payment->ID ) { |
|
| 1412 | + if (!empty($wpi_checkout_id) && $wpi_checkout_id == $payment->ID) { |
|
| 1413 | 1413 | continue; |
| 1414 | 1414 | } |
| 1415 | 1415 | |
| 1416 | 1416 | // And failed payments. |
| 1417 | - if ( $payment->has_status( array( 'wpi-cancelled', 'wpi-failed' ) ) ) { |
|
| 1417 | + if ($payment->has_status(array('wpi-cancelled', 'wpi-failed'))) { |
|
| 1418 | 1418 | continue; |
| 1419 | 1419 | } |
| 1420 | 1420 | |
| 1421 | 1421 | // Retrieve all the discounts for the payment. |
| 1422 | - $discounts = $payment->get_discounts( true ); |
|
| 1423 | - if ( empty( $discounts ) ) { |
|
| 1422 | + $discounts = $payment->get_discounts(true); |
|
| 1423 | + if (empty($discounts)) { |
|
| 1424 | 1424 | continue; |
| 1425 | 1425 | } |
| 1426 | 1426 | |
| 1427 | 1427 | // And check if the current discount is amongst them. |
| 1428 | - $discounts = array_map( 'strtolower', wpinv_parse_list( $discounts ) ); |
|
| 1429 | - if ( ! empty( $discounts ) && in_array( $code, $discounts ) ) { |
|
| 1428 | + $discounts = array_map('strtolower', wpinv_parse_list($discounts)); |
|
| 1429 | + if (!empty($discounts) && in_array($code, $discounts)) { |
|
| 1430 | 1430 | return false; |
| 1431 | 1431 | } |
| 1432 | 1432 | } |
@@ -1451,24 +1451,24 @@ discard block |
||
| 1451 | 1451 | * @param int $by The number of usages to increas by. |
| 1452 | 1452 | * @return int |
| 1453 | 1453 | */ |
| 1454 | - public function increase_usage( $by = 1 ) { |
|
| 1454 | + public function increase_usage($by = 1) { |
|
| 1455 | 1455 | |
| 1456 | 1456 | // Abort if zero. |
| 1457 | - if ( empty( $by ) ) { |
|
| 1457 | + if (empty($by)) { |
|
| 1458 | 1458 | return; |
| 1459 | 1459 | } |
| 1460 | 1460 | |
| 1461 | 1461 | // Increase the usage. |
| 1462 | - $this->set_uses( $this->get_uses() + (int) $by ); |
|
| 1462 | + $this->set_uses($this->get_uses() + (int) $by); |
|
| 1463 | 1463 | |
| 1464 | 1464 | // Save the discount. |
| 1465 | 1465 | $this->save(); |
| 1466 | 1466 | |
| 1467 | 1467 | // Fire relevant hooks. |
| 1468 | - if( (int) $by > 0 ) { |
|
| 1469 | - do_action( 'wpinv_discount_increase_use_count', $this->uses, $this->ID, $this->code, absint( $by ) ); |
|
| 1468 | + if ((int) $by > 0) { |
|
| 1469 | + do_action('wpinv_discount_increase_use_count', $this->uses, $this->ID, $this->code, absint($by)); |
|
| 1470 | 1470 | } else { |
| 1471 | - do_action( 'wpinv_discount_decrease_use_count', $this->uses, $this->ID, $this->code, absint( $by ) ); |
|
| 1471 | + do_action('wpinv_discount_decrease_use_count', $this->uses, $this->ID, $this->code, absint($by)); |
|
| 1472 | 1472 | } |
| 1473 | 1473 | |
| 1474 | 1474 | // Return the number of times the discount has been used. |
@@ -1492,7 +1492,7 @@ discard block |
||
| 1492 | 1492 | * @param float $amount |
| 1493 | 1493 | * @return float |
| 1494 | 1494 | */ |
| 1495 | - public function get_discounted_amount( $amount ) { |
|
| 1495 | + public function get_discounted_amount($amount) { |
|
| 1496 | 1496 | |
| 1497 | 1497 | // Convert amount to float. |
| 1498 | 1498 | $amount = (float) $amount; |
@@ -1500,29 +1500,29 @@ discard block |
||
| 1500 | 1500 | // Get discount amount. |
| 1501 | 1501 | $discount_amount = $this->get_amount(); |
| 1502 | 1502 | |
| 1503 | - if ( empty( $discount_amount ) ) { |
|
| 1503 | + if (empty($discount_amount)) { |
|
| 1504 | 1504 | return 0; |
| 1505 | 1505 | } |
| 1506 | 1506 | |
| 1507 | 1507 | // Format the amount. |
| 1508 | - $discount_amount = floatval( wpinv_sanitize_amount( $discount_amount ) ); |
|
| 1508 | + $discount_amount = floatval(wpinv_sanitize_amount($discount_amount)); |
|
| 1509 | 1509 | |
| 1510 | 1510 | // If this is a percentage discount. |
| 1511 | - if ( $this->is_type( 'percent' ) ) { |
|
| 1512 | - $discount_amount = $amount * ( $discount_amount / 100 ); |
|
| 1511 | + if ($this->is_type('percent')) { |
|
| 1512 | + $discount_amount = $amount * ($discount_amount / 100); |
|
| 1513 | 1513 | } |
| 1514 | 1514 | |
| 1515 | 1515 | // Discount can not be less than zero... |
| 1516 | - if ( $discount_amount < 0 ) { |
|
| 1516 | + if ($discount_amount < 0) { |
|
| 1517 | 1517 | $discount_amount = 0; |
| 1518 | 1518 | } |
| 1519 | 1519 | |
| 1520 | 1520 | // ... or more than the amount. |
| 1521 | - if ( $discount_amount > $amount ) { |
|
| 1521 | + if ($discount_amount > $amount) { |
|
| 1522 | 1522 | $discount_amount = $amount; |
| 1523 | 1523 | } |
| 1524 | 1524 | |
| 1525 | - return apply_filters( 'wpinv_discount_total_discount_amount', $discount_amount, $amount, $this ); |
|
| 1525 | + return apply_filters('wpinv_discount_total_discount_amount', $discount_amount, $amount, $this); |
|
| 1526 | 1526 | } |
| 1527 | 1527 | |
| 1528 | 1528 | } |
@@ -11,185 +11,185 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class GetPaid_Data_Store { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Contains an instance of the data store class that we are working with. |
|
| 16 | - * |
|
| 17 | - * @var GetPaid_Data_Store |
|
| 18 | - */ |
|
| 19 | - private $instance = null; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * Contains an array of default supported data stores. |
|
| 23 | - * Format of object name => class name. |
|
| 24 | - * Example: 'item' => 'GetPaid_Item_Data_Store' |
|
| 25 | - * You can also pass something like item-<type> for item stores and |
|
| 26 | - * that type will be used first when available, if a store is requested like |
|
| 27 | - * this and doesn't exist, then the store would fall back to 'item'. |
|
| 28 | - * Ran through `getpaid_data_stores`. |
|
| 29 | - * |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - private $stores = array( |
|
| 33 | - 'item' => 'GetPaid_Item_Data_Store', |
|
| 34 | - 'payment_form' => 'GetPaid_Payment_Form_Data_Store', |
|
| 35 | - 'discount' => 'GetPaid_Discount_Data_Store', |
|
| 36 | - 'invoice' => 'GetPaid_Invoice_Data_Store', |
|
| 37 | - ); |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Contains the name of the current data store's class name. |
|
| 41 | - * |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - private $current_class_name = ''; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The object type this store works with. |
|
| 48 | - * |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - private $object_type = ''; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Tells GetPaid_Data_Store which object |
|
| 55 | - * store we want to work with. |
|
| 56 | - * |
|
| 57 | - * @param string $object_type Name of object. |
|
| 58 | - */ |
|
| 59 | - public function __construct( $object_type ) { |
|
| 60 | - $this->object_type = $object_type; |
|
| 61 | - $this->stores = apply_filters( 'getpaid_data_stores', $this->stores ); |
|
| 62 | - |
|
| 63 | - // If this object type can't be found, check to see if we can load one |
|
| 64 | - // level up (so if item-type isn't found, we try item). |
|
| 65 | - if ( ! array_key_exists( $object_type, $this->stores ) ) { |
|
| 66 | - $pieces = explode( '-', $object_type ); |
|
| 67 | - $object_type = $pieces[0]; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - if ( array_key_exists( $object_type, $this->stores ) ) { |
|
| 71 | - $store = apply_filters( 'getpaid_' . $object_type . '_data_store', $this->stores[ $object_type ] ); |
|
| 72 | - if ( is_object( $store ) ) { |
|
| 73 | - $this->current_class_name = get_class( $store ); |
|
| 74 | - $this->instance = $store; |
|
| 75 | - } else { |
|
| 76 | - if ( ! class_exists( $store ) ) { |
|
| 77 | - throw new Exception( __( 'Data store class does not exist.', 'invoicing' ) ); |
|
| 78 | - } |
|
| 79 | - $this->current_class_name = $store; |
|
| 80 | - $this->instance = new $store(); |
|
| 81 | - } |
|
| 82 | - } else { |
|
| 83 | - throw new Exception( __( 'Invalid data store.', 'invoicing' ) ); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Only store the object type to avoid serializing the data store instance. |
|
| 89 | - * |
|
| 90 | - * @return array |
|
| 91 | - */ |
|
| 92 | - public function __sleep() { |
|
| 93 | - return array( 'object_type' ); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Re-run the constructor with the object type. |
|
| 98 | - * |
|
| 99 | - * @throws Exception When validation fails. |
|
| 100 | - */ |
|
| 101 | - public function __wakeup() { |
|
| 102 | - $this->__construct( $this->object_type ); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Loads a data store. |
|
| 107 | - * |
|
| 108 | - * @param string $object_type Name of object. |
|
| 109 | - * |
|
| 110 | - * @since 1.0.19 |
|
| 111 | - * @throws Exception When validation fails. |
|
| 112 | - * @return GetPaid_Data_Store |
|
| 113 | - */ |
|
| 114 | - public static function load( $object_type ) { |
|
| 115 | - return new GetPaid_Data_Store( $object_type ); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Returns the class name of the current data store. |
|
| 120 | - * |
|
| 121 | - * @since 1.0.19 |
|
| 122 | - * @return string |
|
| 123 | - */ |
|
| 124 | - public function get_current_class_name() { |
|
| 125 | - return $this->current_class_name; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Returns the object type of the current data store. |
|
| 130 | - * |
|
| 131 | - * @since 1.0.19 |
|
| 132 | - * @return string |
|
| 133 | - */ |
|
| 134 | - public function get_object_type() { |
|
| 135 | - return $this->object_type; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Reads an object from the data store. |
|
| 140 | - * |
|
| 141 | - * @since 1.0.19 |
|
| 142 | - * @param GetPaid_Data $data GetPaid data instance. |
|
| 143 | - */ |
|
| 144 | - public function read( &$data ) { |
|
| 145 | - $this->instance->read( $data ); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Create an object in the data store. |
|
| 150 | - * |
|
| 151 | - * @since 1.0.19 |
|
| 152 | - * @param GetPaid_Data $data GetPaid data instance. |
|
| 153 | - */ |
|
| 154 | - public function create( &$data ) { |
|
| 155 | - $this->instance->create( $data ); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Update an object in the data store. |
|
| 160 | - * |
|
| 161 | - * @since 1.0.19 |
|
| 162 | - * @param GetPaid_Data $data GetPaid data instance. |
|
| 163 | - */ |
|
| 164 | - public function update( &$data ) { |
|
| 165 | - $this->instance->update( $data ); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Delete an object from the data store. |
|
| 170 | - * |
|
| 171 | - * @since 1.0.19 |
|
| 172 | - * @param GetPaid_Data $data GetPaid data instance. |
|
| 173 | - * @param array $args Array of args to pass to the delete method. |
|
| 174 | - */ |
|
| 175 | - public function delete( &$data, $args = array() ) { |
|
| 176 | - $this->instance->delete( $data, $args ); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Data stores can define additional function. This passes |
|
| 181 | - * through to the instance if that function exists. |
|
| 182 | - * |
|
| 183 | - * @since 1.0.19 |
|
| 184 | - * @param string $method Method. |
|
| 185 | - * @return mixed |
|
| 186 | - */ |
|
| 187 | - public function __call( $method, $parameters ) { |
|
| 188 | - if ( is_callable( array( $this->instance, $method ) ) ) { |
|
| 189 | - $object = array_shift( $parameters ); |
|
| 190 | - $parameters = array_merge( array( &$object ), $parameters ); |
|
| 191 | - return call_user_func_array( array( $this->instance, $method ), $parameters ); |
|
| 192 | - } |
|
| 193 | - } |
|
| 14 | + /** |
|
| 15 | + * Contains an instance of the data store class that we are working with. |
|
| 16 | + * |
|
| 17 | + * @var GetPaid_Data_Store |
|
| 18 | + */ |
|
| 19 | + private $instance = null; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * Contains an array of default supported data stores. |
|
| 23 | + * Format of object name => class name. |
|
| 24 | + * Example: 'item' => 'GetPaid_Item_Data_Store' |
|
| 25 | + * You can also pass something like item-<type> for item stores and |
|
| 26 | + * that type will be used first when available, if a store is requested like |
|
| 27 | + * this and doesn't exist, then the store would fall back to 'item'. |
|
| 28 | + * Ran through `getpaid_data_stores`. |
|
| 29 | + * |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + private $stores = array( |
|
| 33 | + 'item' => 'GetPaid_Item_Data_Store', |
|
| 34 | + 'payment_form' => 'GetPaid_Payment_Form_Data_Store', |
|
| 35 | + 'discount' => 'GetPaid_Discount_Data_Store', |
|
| 36 | + 'invoice' => 'GetPaid_Invoice_Data_Store', |
|
| 37 | + ); |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Contains the name of the current data store's class name. |
|
| 41 | + * |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + private $current_class_name = ''; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The object type this store works with. |
|
| 48 | + * |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + private $object_type = ''; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Tells GetPaid_Data_Store which object |
|
| 55 | + * store we want to work with. |
|
| 56 | + * |
|
| 57 | + * @param string $object_type Name of object. |
|
| 58 | + */ |
|
| 59 | + public function __construct( $object_type ) { |
|
| 60 | + $this->object_type = $object_type; |
|
| 61 | + $this->stores = apply_filters( 'getpaid_data_stores', $this->stores ); |
|
| 62 | + |
|
| 63 | + // If this object type can't be found, check to see if we can load one |
|
| 64 | + // level up (so if item-type isn't found, we try item). |
|
| 65 | + if ( ! array_key_exists( $object_type, $this->stores ) ) { |
|
| 66 | + $pieces = explode( '-', $object_type ); |
|
| 67 | + $object_type = $pieces[0]; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + if ( array_key_exists( $object_type, $this->stores ) ) { |
|
| 71 | + $store = apply_filters( 'getpaid_' . $object_type . '_data_store', $this->stores[ $object_type ] ); |
|
| 72 | + if ( is_object( $store ) ) { |
|
| 73 | + $this->current_class_name = get_class( $store ); |
|
| 74 | + $this->instance = $store; |
|
| 75 | + } else { |
|
| 76 | + if ( ! class_exists( $store ) ) { |
|
| 77 | + throw new Exception( __( 'Data store class does not exist.', 'invoicing' ) ); |
|
| 78 | + } |
|
| 79 | + $this->current_class_name = $store; |
|
| 80 | + $this->instance = new $store(); |
|
| 81 | + } |
|
| 82 | + } else { |
|
| 83 | + throw new Exception( __( 'Invalid data store.', 'invoicing' ) ); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Only store the object type to avoid serializing the data store instance. |
|
| 89 | + * |
|
| 90 | + * @return array |
|
| 91 | + */ |
|
| 92 | + public function __sleep() { |
|
| 93 | + return array( 'object_type' ); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Re-run the constructor with the object type. |
|
| 98 | + * |
|
| 99 | + * @throws Exception When validation fails. |
|
| 100 | + */ |
|
| 101 | + public function __wakeup() { |
|
| 102 | + $this->__construct( $this->object_type ); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Loads a data store. |
|
| 107 | + * |
|
| 108 | + * @param string $object_type Name of object. |
|
| 109 | + * |
|
| 110 | + * @since 1.0.19 |
|
| 111 | + * @throws Exception When validation fails. |
|
| 112 | + * @return GetPaid_Data_Store |
|
| 113 | + */ |
|
| 114 | + public static function load( $object_type ) { |
|
| 115 | + return new GetPaid_Data_Store( $object_type ); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Returns the class name of the current data store. |
|
| 120 | + * |
|
| 121 | + * @since 1.0.19 |
|
| 122 | + * @return string |
|
| 123 | + */ |
|
| 124 | + public function get_current_class_name() { |
|
| 125 | + return $this->current_class_name; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Returns the object type of the current data store. |
|
| 130 | + * |
|
| 131 | + * @since 1.0.19 |
|
| 132 | + * @return string |
|
| 133 | + */ |
|
| 134 | + public function get_object_type() { |
|
| 135 | + return $this->object_type; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Reads an object from the data store. |
|
| 140 | + * |
|
| 141 | + * @since 1.0.19 |
|
| 142 | + * @param GetPaid_Data $data GetPaid data instance. |
|
| 143 | + */ |
|
| 144 | + public function read( &$data ) { |
|
| 145 | + $this->instance->read( $data ); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Create an object in the data store. |
|
| 150 | + * |
|
| 151 | + * @since 1.0.19 |
|
| 152 | + * @param GetPaid_Data $data GetPaid data instance. |
|
| 153 | + */ |
|
| 154 | + public function create( &$data ) { |
|
| 155 | + $this->instance->create( $data ); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Update an object in the data store. |
|
| 160 | + * |
|
| 161 | + * @since 1.0.19 |
|
| 162 | + * @param GetPaid_Data $data GetPaid data instance. |
|
| 163 | + */ |
|
| 164 | + public function update( &$data ) { |
|
| 165 | + $this->instance->update( $data ); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Delete an object from the data store. |
|
| 170 | + * |
|
| 171 | + * @since 1.0.19 |
|
| 172 | + * @param GetPaid_Data $data GetPaid data instance. |
|
| 173 | + * @param array $args Array of args to pass to the delete method. |
|
| 174 | + */ |
|
| 175 | + public function delete( &$data, $args = array() ) { |
|
| 176 | + $this->instance->delete( $data, $args ); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Data stores can define additional function. This passes |
|
| 181 | + * through to the instance if that function exists. |
|
| 182 | + * |
|
| 183 | + * @since 1.0.19 |
|
| 184 | + * @param string $method Method. |
|
| 185 | + * @return mixed |
|
| 186 | + */ |
|
| 187 | + public function __call( $method, $parameters ) { |
|
| 188 | + if ( is_callable( array( $this->instance, $method ) ) ) { |
|
| 189 | + $object = array_shift( $parameters ); |
|
| 190 | + $parameters = array_merge( array( &$object ), $parameters ); |
|
| 191 | + return call_user_func_array( array( $this->instance, $method ), $parameters ); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | 195 | } |
@@ -14,346 +14,346 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class GetPaid_Data_Store_WP { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Meta type. This should match up with |
|
| 19 | - * the types available at https://developer.wordpress.org/reference/functions/add_metadata/. |
|
| 20 | - * WP defines 'post', 'user', 'comment', and 'term'. |
|
| 21 | - * |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - protected $meta_type = 'post'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * This only needs set if you are using a custom metadata type. |
|
| 28 | - * |
|
| 29 | - * @var string |
|
| 30 | - */ |
|
| 31 | - protected $object_id_field_for_meta = ''; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Data stored in meta keys, but not considered "meta" for an object. |
|
| 35 | - * |
|
| 36 | - * @since 1.0.19 |
|
| 37 | - * |
|
| 38 | - * @var array |
|
| 39 | - */ |
|
| 40 | - protected $internal_meta_keys = array(); |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Meta data which should exist in the DB, even if empty. |
|
| 44 | - * |
|
| 45 | - * @since 1.0.19 |
|
| 46 | - * |
|
| 47 | - * @var array |
|
| 48 | - */ |
|
| 49 | - protected $must_exist_meta_keys = array(); |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * A map of meta keys to data props. |
|
| 53 | - * |
|
| 54 | - * @since 1.0.19 |
|
| 55 | - * |
|
| 56 | - * @var array |
|
| 57 | - */ |
|
| 58 | - protected $meta_key_to_props = array(); |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Returns an array of meta for an object. |
|
| 62 | - * |
|
| 63 | - * @since 1.0.19 |
|
| 64 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 65 | - * @return array |
|
| 66 | - */ |
|
| 67 | - public function read_meta( &$object ) { |
|
| 68 | - global $wpdb; |
|
| 69 | - $db_info = $this->get_db_info(); |
|
| 70 | - $raw_meta_data = $wpdb->get_results( |
|
| 71 | - $wpdb->prepare( |
|
| 72 | - "SELECT {$db_info['meta_id_field']} as meta_id, meta_key, meta_value |
|
| 17 | + /** |
|
| 18 | + * Meta type. This should match up with |
|
| 19 | + * the types available at https://developer.wordpress.org/reference/functions/add_metadata/. |
|
| 20 | + * WP defines 'post', 'user', 'comment', and 'term'. |
|
| 21 | + * |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + protected $meta_type = 'post'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * This only needs set if you are using a custom metadata type. |
|
| 28 | + * |
|
| 29 | + * @var string |
|
| 30 | + */ |
|
| 31 | + protected $object_id_field_for_meta = ''; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Data stored in meta keys, but not considered "meta" for an object. |
|
| 35 | + * |
|
| 36 | + * @since 1.0.19 |
|
| 37 | + * |
|
| 38 | + * @var array |
|
| 39 | + */ |
|
| 40 | + protected $internal_meta_keys = array(); |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Meta data which should exist in the DB, even if empty. |
|
| 44 | + * |
|
| 45 | + * @since 1.0.19 |
|
| 46 | + * |
|
| 47 | + * @var array |
|
| 48 | + */ |
|
| 49 | + protected $must_exist_meta_keys = array(); |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * A map of meta keys to data props. |
|
| 53 | + * |
|
| 54 | + * @since 1.0.19 |
|
| 55 | + * |
|
| 56 | + * @var array |
|
| 57 | + */ |
|
| 58 | + protected $meta_key_to_props = array(); |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Returns an array of meta for an object. |
|
| 62 | + * |
|
| 63 | + * @since 1.0.19 |
|
| 64 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 65 | + * @return array |
|
| 66 | + */ |
|
| 67 | + public function read_meta( &$object ) { |
|
| 68 | + global $wpdb; |
|
| 69 | + $db_info = $this->get_db_info(); |
|
| 70 | + $raw_meta_data = $wpdb->get_results( |
|
| 71 | + $wpdb->prepare( |
|
| 72 | + "SELECT {$db_info['meta_id_field']} as meta_id, meta_key, meta_value |
|
| 73 | 73 | FROM {$db_info['table']} |
| 74 | 74 | WHERE {$db_info['object_id_field']} = %d |
| 75 | 75 | ORDER BY {$db_info['meta_id_field']}", |
| 76 | - $object->get_id() |
|
| 77 | - ) |
|
| 78 | - ); |
|
| 79 | - |
|
| 80 | - $this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys ); |
|
| 81 | - $meta_data = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) ); |
|
| 82 | - return apply_filters( "getpaid_data_store_wp_{$this->meta_type}_read_meta", $meta_data, $object, $this ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Deletes meta based on meta ID. |
|
| 87 | - * |
|
| 88 | - * @since 1.0.19 |
|
| 89 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 90 | - * @param stdClass $meta (containing at least ->id). |
|
| 91 | - */ |
|
| 92 | - public function delete_meta( &$object, $meta ) { |
|
| 93 | - delete_metadata_by_mid( $this->meta_type, $meta->id ); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Add new piece of meta. |
|
| 98 | - * |
|
| 99 | - * @since 1.0.19 |
|
| 100 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 101 | - * @param stdClass $meta (containing ->key and ->value). |
|
| 102 | - * @return int meta ID |
|
| 103 | - */ |
|
| 104 | - public function add_meta( &$object, $meta ) { |
|
| 105 | - return add_metadata( $this->meta_type, $object->get_id(), $meta->key, is_string( $meta->value ) ? wp_slash( $meta->value ) : $meta->value, false ); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Update meta. |
|
| 110 | - * |
|
| 111 | - * @since 1.0.19 |
|
| 112 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 113 | - * @param stdClass $meta (containing ->id, ->key and ->value). |
|
| 114 | - */ |
|
| 115 | - public function update_meta( &$object, $meta ) { |
|
| 116 | - update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Table structure is slightly different between meta types, this function will return what we need to know. |
|
| 121 | - * |
|
| 122 | - * @since 1.0.19 |
|
| 123 | - * @return array Array elements: table, object_id_field, meta_id_field |
|
| 124 | - */ |
|
| 125 | - protected function get_db_info() { |
|
| 126 | - global $wpdb; |
|
| 127 | - |
|
| 128 | - $meta_id_field = 'meta_id'; // users table calls this umeta_id so we need to track this as well. |
|
| 129 | - $table = $wpdb->prefix; |
|
| 130 | - |
|
| 131 | - // If we are dealing with a type of metadata that is not a core type, the table should be prefixed. |
|
| 132 | - if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ), true ) ) { |
|
| 133 | - $table .= 'getpaid_'; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $table .= $this->meta_type . 'meta'; |
|
| 137 | - $object_id_field = $this->meta_type . '_id'; |
|
| 138 | - |
|
| 139 | - // Figure out our field names. |
|
| 140 | - if ( 'user' === $this->meta_type ) { |
|
| 141 | - $meta_id_field = 'umeta_id'; |
|
| 142 | - $table = $wpdb->usermeta; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - if ( ! empty( $this->object_id_field_for_meta ) ) { |
|
| 146 | - $object_id_field = $this->object_id_field_for_meta; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - return array( |
|
| 150 | - 'table' => $table, |
|
| 151 | - 'object_id_field' => $object_id_field, |
|
| 152 | - 'meta_id_field' => $meta_id_field, |
|
| 153 | - ); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Internal meta keys we don't want exposed as part of meta_data. This is in |
|
| 158 | - * addition to all data props with _ prefix. |
|
| 159 | - * |
|
| 160 | - * @since 1.0.19 |
|
| 161 | - * |
|
| 162 | - * @param string $key Prefix to be added to meta keys. |
|
| 163 | - * @return string |
|
| 164 | - */ |
|
| 165 | - protected function prefix_key( $key ) { |
|
| 166 | - return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Callback to remove unwanted meta data. |
|
| 171 | - * |
|
| 172 | - * @param object $meta Meta object to check if it should be excluded or not. |
|
| 173 | - * @return bool |
|
| 174 | - */ |
|
| 175 | - protected function exclude_internal_meta_keys( $meta ) { |
|
| 176 | - return ! in_array( $meta->meta_key, $this->internal_meta_keys, true ) && 0 !== stripos( $meta->meta_key, 'wp_' ); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Gets a list of props and meta keys that need updated based on change state |
|
| 181 | - * or if they are present in the database or not. |
|
| 182 | - * |
|
| 183 | - * @param GetPaid_Data $object The GetPaid_Data object. |
|
| 184 | - * @param array $meta_key_to_props A mapping of meta keys => prop names. |
|
| 185 | - * @param string $meta_type The internal WP meta type (post, user, etc). |
|
| 186 | - * @return array A mapping of meta keys => prop names, filtered by ones that should be updated. |
|
| 187 | - */ |
|
| 188 | - protected function get_props_to_update( $object, $meta_key_to_props, $meta_type = 'post' ) { |
|
| 189 | - $props_to_update = array(); |
|
| 190 | - $changed_props = $object->get_changes(); |
|
| 191 | - |
|
| 192 | - // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
| 193 | - foreach ( $meta_key_to_props as $meta_key => $prop ) { |
|
| 194 | - if ( array_key_exists( $prop, $changed_props ) || ! metadata_exists( $meta_type, $object->get_id(), $meta_key ) ) { |
|
| 195 | - $props_to_update[ $meta_key ] = $prop; |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return $props_to_update; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Read object data. |
|
| 204 | - * |
|
| 205 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 206 | - * @param WP_Post $post_object Post object. |
|
| 207 | - * @since 1.0.19 |
|
| 208 | - */ |
|
| 209 | - protected function read_object_data( &$object, $post_object ) { |
|
| 210 | - $id = $object->get_id(); |
|
| 211 | - $props = array(); |
|
| 212 | - |
|
| 213 | - foreach ( $this->meta_key_to_props as $meta_key => $prop ) { |
|
| 214 | - $props[ $prop ] = get_post_meta( $id, $meta_key, true ); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - // Set object properties. |
|
| 218 | - $object->set_props( $props ); |
|
| 219 | - |
|
| 220 | - // Gets extra data associated with the object if needed. |
|
| 221 | - foreach ( $object->get_extra_data_keys() as $key ) { |
|
| 222 | - $function = 'set_' . $key; |
|
| 223 | - if ( is_callable( array( $object, $function ) ) ) { |
|
| 224 | - $object->{$function}( get_post_meta( $object->get_id(), $key, true ) ); |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Helper method that updates all the post meta for an object based on it's settings in the GetPaid_Data class. |
|
| 231 | - * |
|
| 232 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 233 | - * @since 1.0.19 |
|
| 234 | - */ |
|
| 235 | - protected function update_post_meta( &$object ) { |
|
| 236 | - |
|
| 237 | - $updated_props = array(); |
|
| 238 | - $props_to_update = $this->get_props_to_update( $object, $this->meta_key_to_props ); |
|
| 239 | - $object_type = $object->get_object_type(); |
|
| 240 | - |
|
| 241 | - foreach ( $props_to_update as $meta_key => $prop ) { |
|
| 242 | - $value = $object->{"get_$prop"}( 'edit' ); |
|
| 243 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
| 244 | - |
|
| 245 | - $updated = $this->update_or_delete_post_meta( $object, $meta_key, $value ); |
|
| 246 | - |
|
| 247 | - if ( $updated ) { |
|
| 248 | - $updated_props[] = $prop; |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - do_action( "getpaid_{$object_type}_object_updated_props", $object, $updated_props ); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Update meta data in, or delete it from, the database. |
|
| 257 | - * |
|
| 258 | - * Avoids storing meta when it's either an empty string or empty array or null. |
|
| 259 | - * Other empty values such as numeric 0 should still be stored. |
|
| 260 | - * Data-stores can force meta to exist using `must_exist_meta_keys`. |
|
| 261 | - * |
|
| 262 | - * Note: WordPress `get_metadata` function returns an empty string when meta data does not exist. |
|
| 263 | - * |
|
| 264 | - * @param GetPaid_Data $object The GetPaid_Data object. |
|
| 265 | - * @param string $meta_key Meta key to update. |
|
| 266 | - * @param mixed $meta_value Value to save. |
|
| 267 | - * |
|
| 268 | - * @since 1.0.19 Added to prevent empty meta being stored unless required. |
|
| 269 | - * |
|
| 270 | - * @return bool True if updated/deleted. |
|
| 271 | - */ |
|
| 272 | - protected function update_or_delete_post_meta( $object, $meta_key, $meta_value ) { |
|
| 273 | - if ( in_array( $meta_value, array( array(), '', null ), true ) && ! in_array( $meta_key, $this->must_exist_meta_keys, true ) ) { |
|
| 274 | - $updated = delete_post_meta( $object->get_id(), $meta_key ); |
|
| 275 | - } else { |
|
| 276 | - $updated = update_post_meta( $object->get_id(), $meta_key, $meta_value ); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - return (bool) $updated; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Return list of internal meta keys. |
|
| 284 | - * |
|
| 285 | - * @since 1.0.19 |
|
| 286 | - * @return array |
|
| 287 | - */ |
|
| 288 | - public function get_internal_meta_keys() { |
|
| 289 | - return $this->internal_meta_keys; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Clear any caches. |
|
| 294 | - * |
|
| 295 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 296 | - * @since 1.0.19 |
|
| 297 | - */ |
|
| 298 | - protected function clear_caches( &$object ) { |
|
| 299 | - clean_post_cache( $object->get_id() ); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Method to delete a data object from the database. |
|
| 304 | - * |
|
| 305 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 306 | - * @param array $args Array of args to pass to the delete method. |
|
| 307 | - * |
|
| 308 | - * @return void |
|
| 309 | - */ |
|
| 310 | - public function delete( &$object, $args = array() ) { |
|
| 311 | - $id = $object->get_id(); |
|
| 312 | - $object_type = $object->get_object_type(); |
|
| 313 | - |
|
| 314 | - if ( 'invoice' == $object_type ) { |
|
| 315 | - $object_type = $object->get_type(); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - $args = wp_parse_args( |
|
| 319 | - $args, |
|
| 320 | - array( |
|
| 321 | - 'force_delete' => false, |
|
| 322 | - ) |
|
| 323 | - ); |
|
| 324 | - |
|
| 325 | - if ( ! $id ) { |
|
| 326 | - return; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - if ( $args['force_delete'] ) { |
|
| 330 | - wp_delete_post( $id, true ); |
|
| 331 | - $object->set_id( 0 ); |
|
| 332 | - do_action( "getpaid_delete_$object_type", $id ); |
|
| 333 | - } else { |
|
| 334 | - wp_trash_post( $id ); |
|
| 335 | - $object->set_status( 'trash' ); |
|
| 336 | - do_action( "getpaid_trash_$object_type", $id ); |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Get the status to save to the post object. |
|
| 342 | - * |
|
| 343 | - * |
|
| 344 | - * @since 1.0.19 |
|
| 345 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
| 346 | - * @return string |
|
| 347 | - */ |
|
| 348 | - protected function get_post_status( $object ) { |
|
| 349 | - $object_status = $object->get_status( 'edit' ); |
|
| 350 | - $object_type = $object->get_object_type(); |
|
| 351 | - |
|
| 352 | - if ( ! $object_status ) { |
|
| 353 | - $object_status = apply_filters( "getpaid_default_{$object_type}_status", 'draft' ); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - return $object_status; |
|
| 357 | - } |
|
| 76 | + $object->get_id() |
|
| 77 | + ) |
|
| 78 | + ); |
|
| 79 | + |
|
| 80 | + $this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys ); |
|
| 81 | + $meta_data = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) ); |
|
| 82 | + return apply_filters( "getpaid_data_store_wp_{$this->meta_type}_read_meta", $meta_data, $object, $this ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Deletes meta based on meta ID. |
|
| 87 | + * |
|
| 88 | + * @since 1.0.19 |
|
| 89 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 90 | + * @param stdClass $meta (containing at least ->id). |
|
| 91 | + */ |
|
| 92 | + public function delete_meta( &$object, $meta ) { |
|
| 93 | + delete_metadata_by_mid( $this->meta_type, $meta->id ); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Add new piece of meta. |
|
| 98 | + * |
|
| 99 | + * @since 1.0.19 |
|
| 100 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 101 | + * @param stdClass $meta (containing ->key and ->value). |
|
| 102 | + * @return int meta ID |
|
| 103 | + */ |
|
| 104 | + public function add_meta( &$object, $meta ) { |
|
| 105 | + return add_metadata( $this->meta_type, $object->get_id(), $meta->key, is_string( $meta->value ) ? wp_slash( $meta->value ) : $meta->value, false ); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Update meta. |
|
| 110 | + * |
|
| 111 | + * @since 1.0.19 |
|
| 112 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 113 | + * @param stdClass $meta (containing ->id, ->key and ->value). |
|
| 114 | + */ |
|
| 115 | + public function update_meta( &$object, $meta ) { |
|
| 116 | + update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Table structure is slightly different between meta types, this function will return what we need to know. |
|
| 121 | + * |
|
| 122 | + * @since 1.0.19 |
|
| 123 | + * @return array Array elements: table, object_id_field, meta_id_field |
|
| 124 | + */ |
|
| 125 | + protected function get_db_info() { |
|
| 126 | + global $wpdb; |
|
| 127 | + |
|
| 128 | + $meta_id_field = 'meta_id'; // users table calls this umeta_id so we need to track this as well. |
|
| 129 | + $table = $wpdb->prefix; |
|
| 130 | + |
|
| 131 | + // If we are dealing with a type of metadata that is not a core type, the table should be prefixed. |
|
| 132 | + if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ), true ) ) { |
|
| 133 | + $table .= 'getpaid_'; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $table .= $this->meta_type . 'meta'; |
|
| 137 | + $object_id_field = $this->meta_type . '_id'; |
|
| 138 | + |
|
| 139 | + // Figure out our field names. |
|
| 140 | + if ( 'user' === $this->meta_type ) { |
|
| 141 | + $meta_id_field = 'umeta_id'; |
|
| 142 | + $table = $wpdb->usermeta; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + if ( ! empty( $this->object_id_field_for_meta ) ) { |
|
| 146 | + $object_id_field = $this->object_id_field_for_meta; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + return array( |
|
| 150 | + 'table' => $table, |
|
| 151 | + 'object_id_field' => $object_id_field, |
|
| 152 | + 'meta_id_field' => $meta_id_field, |
|
| 153 | + ); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Internal meta keys we don't want exposed as part of meta_data. This is in |
|
| 158 | + * addition to all data props with _ prefix. |
|
| 159 | + * |
|
| 160 | + * @since 1.0.19 |
|
| 161 | + * |
|
| 162 | + * @param string $key Prefix to be added to meta keys. |
|
| 163 | + * @return string |
|
| 164 | + */ |
|
| 165 | + protected function prefix_key( $key ) { |
|
| 166 | + return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Callback to remove unwanted meta data. |
|
| 171 | + * |
|
| 172 | + * @param object $meta Meta object to check if it should be excluded or not. |
|
| 173 | + * @return bool |
|
| 174 | + */ |
|
| 175 | + protected function exclude_internal_meta_keys( $meta ) { |
|
| 176 | + return ! in_array( $meta->meta_key, $this->internal_meta_keys, true ) && 0 !== stripos( $meta->meta_key, 'wp_' ); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Gets a list of props and meta keys that need updated based on change state |
|
| 181 | + * or if they are present in the database or not. |
|
| 182 | + * |
|
| 183 | + * @param GetPaid_Data $object The GetPaid_Data object. |
|
| 184 | + * @param array $meta_key_to_props A mapping of meta keys => prop names. |
|
| 185 | + * @param string $meta_type The internal WP meta type (post, user, etc). |
|
| 186 | + * @return array A mapping of meta keys => prop names, filtered by ones that should be updated. |
|
| 187 | + */ |
|
| 188 | + protected function get_props_to_update( $object, $meta_key_to_props, $meta_type = 'post' ) { |
|
| 189 | + $props_to_update = array(); |
|
| 190 | + $changed_props = $object->get_changes(); |
|
| 191 | + |
|
| 192 | + // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
| 193 | + foreach ( $meta_key_to_props as $meta_key => $prop ) { |
|
| 194 | + if ( array_key_exists( $prop, $changed_props ) || ! metadata_exists( $meta_type, $object->get_id(), $meta_key ) ) { |
|
| 195 | + $props_to_update[ $meta_key ] = $prop; |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return $props_to_update; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Read object data. |
|
| 204 | + * |
|
| 205 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 206 | + * @param WP_Post $post_object Post object. |
|
| 207 | + * @since 1.0.19 |
|
| 208 | + */ |
|
| 209 | + protected function read_object_data( &$object, $post_object ) { |
|
| 210 | + $id = $object->get_id(); |
|
| 211 | + $props = array(); |
|
| 212 | + |
|
| 213 | + foreach ( $this->meta_key_to_props as $meta_key => $prop ) { |
|
| 214 | + $props[ $prop ] = get_post_meta( $id, $meta_key, true ); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + // Set object properties. |
|
| 218 | + $object->set_props( $props ); |
|
| 219 | + |
|
| 220 | + // Gets extra data associated with the object if needed. |
|
| 221 | + foreach ( $object->get_extra_data_keys() as $key ) { |
|
| 222 | + $function = 'set_' . $key; |
|
| 223 | + if ( is_callable( array( $object, $function ) ) ) { |
|
| 224 | + $object->{$function}( get_post_meta( $object->get_id(), $key, true ) ); |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Helper method that updates all the post meta for an object based on it's settings in the GetPaid_Data class. |
|
| 231 | + * |
|
| 232 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 233 | + * @since 1.0.19 |
|
| 234 | + */ |
|
| 235 | + protected function update_post_meta( &$object ) { |
|
| 236 | + |
|
| 237 | + $updated_props = array(); |
|
| 238 | + $props_to_update = $this->get_props_to_update( $object, $this->meta_key_to_props ); |
|
| 239 | + $object_type = $object->get_object_type(); |
|
| 240 | + |
|
| 241 | + foreach ( $props_to_update as $meta_key => $prop ) { |
|
| 242 | + $value = $object->{"get_$prop"}( 'edit' ); |
|
| 243 | + $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
| 244 | + |
|
| 245 | + $updated = $this->update_or_delete_post_meta( $object, $meta_key, $value ); |
|
| 246 | + |
|
| 247 | + if ( $updated ) { |
|
| 248 | + $updated_props[] = $prop; |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + do_action( "getpaid_{$object_type}_object_updated_props", $object, $updated_props ); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Update meta data in, or delete it from, the database. |
|
| 257 | + * |
|
| 258 | + * Avoids storing meta when it's either an empty string or empty array or null. |
|
| 259 | + * Other empty values such as numeric 0 should still be stored. |
|
| 260 | + * Data-stores can force meta to exist using `must_exist_meta_keys`. |
|
| 261 | + * |
|
| 262 | + * Note: WordPress `get_metadata` function returns an empty string when meta data does not exist. |
|
| 263 | + * |
|
| 264 | + * @param GetPaid_Data $object The GetPaid_Data object. |
|
| 265 | + * @param string $meta_key Meta key to update. |
|
| 266 | + * @param mixed $meta_value Value to save. |
|
| 267 | + * |
|
| 268 | + * @since 1.0.19 Added to prevent empty meta being stored unless required. |
|
| 269 | + * |
|
| 270 | + * @return bool True if updated/deleted. |
|
| 271 | + */ |
|
| 272 | + protected function update_or_delete_post_meta( $object, $meta_key, $meta_value ) { |
|
| 273 | + if ( in_array( $meta_value, array( array(), '', null ), true ) && ! in_array( $meta_key, $this->must_exist_meta_keys, true ) ) { |
|
| 274 | + $updated = delete_post_meta( $object->get_id(), $meta_key ); |
|
| 275 | + } else { |
|
| 276 | + $updated = update_post_meta( $object->get_id(), $meta_key, $meta_value ); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + return (bool) $updated; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Return list of internal meta keys. |
|
| 284 | + * |
|
| 285 | + * @since 1.0.19 |
|
| 286 | + * @return array |
|
| 287 | + */ |
|
| 288 | + public function get_internal_meta_keys() { |
|
| 289 | + return $this->internal_meta_keys; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Clear any caches. |
|
| 294 | + * |
|
| 295 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 296 | + * @since 1.0.19 |
|
| 297 | + */ |
|
| 298 | + protected function clear_caches( &$object ) { |
|
| 299 | + clean_post_cache( $object->get_id() ); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Method to delete a data object from the database. |
|
| 304 | + * |
|
| 305 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 306 | + * @param array $args Array of args to pass to the delete method. |
|
| 307 | + * |
|
| 308 | + * @return void |
|
| 309 | + */ |
|
| 310 | + public function delete( &$object, $args = array() ) { |
|
| 311 | + $id = $object->get_id(); |
|
| 312 | + $object_type = $object->get_object_type(); |
|
| 313 | + |
|
| 314 | + if ( 'invoice' == $object_type ) { |
|
| 315 | + $object_type = $object->get_type(); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + $args = wp_parse_args( |
|
| 319 | + $args, |
|
| 320 | + array( |
|
| 321 | + 'force_delete' => false, |
|
| 322 | + ) |
|
| 323 | + ); |
|
| 324 | + |
|
| 325 | + if ( ! $id ) { |
|
| 326 | + return; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + if ( $args['force_delete'] ) { |
|
| 330 | + wp_delete_post( $id, true ); |
|
| 331 | + $object->set_id( 0 ); |
|
| 332 | + do_action( "getpaid_delete_$object_type", $id ); |
|
| 333 | + } else { |
|
| 334 | + wp_trash_post( $id ); |
|
| 335 | + $object->set_status( 'trash' ); |
|
| 336 | + do_action( "getpaid_trash_$object_type", $id ); |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Get the status to save to the post object. |
|
| 342 | + * |
|
| 343 | + * |
|
| 344 | + * @since 1.0.19 |
|
| 345 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
| 346 | + * @return string |
|
| 347 | + */ |
|
| 348 | + protected function get_post_status( $object ) { |
|
| 349 | + $object_status = $object->get_status( 'edit' ); |
|
| 350 | + $object_type = $object->get_object_type(); |
|
| 351 | + |
|
| 352 | + if ( ! $object_status ) { |
|
| 353 | + $object_status = apply_filters( "getpaid_default_{$object_type}_status", 'draft' ); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + return $object_status; |
|
| 357 | + } |
|
| 358 | 358 | |
| 359 | 359 | } |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | * @version 1.0.19 |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | -defined( 'ABSPATH' ) || exit; |
|
| 8 | +defined('ABSPATH') || exit; |
|
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * GetPaid_Data_Store_WP class. |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | * @param GetPaid_Data $object GetPaid_Data object. |
| 65 | 65 | * @return array |
| 66 | 66 | */ |
| 67 | - public function read_meta( &$object ) { |
|
| 67 | + public function read_meta(&$object) { |
|
| 68 | 68 | global $wpdb; |
| 69 | 69 | $db_info = $this->get_db_info(); |
| 70 | 70 | $raw_meta_data = $wpdb->get_results( |
@@ -77,9 +77,9 @@ discard block |
||
| 77 | 77 | ) |
| 78 | 78 | ); |
| 79 | 79 | |
| 80 | - $this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys ); |
|
| 81 | - $meta_data = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) ); |
|
| 82 | - return apply_filters( "getpaid_data_store_wp_{$this->meta_type}_read_meta", $meta_data, $object, $this ); |
|
| 80 | + $this->internal_meta_keys = array_merge(array_map(array($this, 'prefix_key'), $object->get_data_keys()), $this->internal_meta_keys); |
|
| 81 | + $meta_data = array_filter($raw_meta_data, array($this, 'exclude_internal_meta_keys')); |
|
| 82 | + return apply_filters("getpaid_data_store_wp_{$this->meta_type}_read_meta", $meta_data, $object, $this); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -89,8 +89,8 @@ discard block |
||
| 89 | 89 | * @param GetPaid_Data $object GetPaid_Data object. |
| 90 | 90 | * @param stdClass $meta (containing at least ->id). |
| 91 | 91 | */ |
| 92 | - public function delete_meta( &$object, $meta ) { |
|
| 93 | - delete_metadata_by_mid( $this->meta_type, $meta->id ); |
|
| 92 | + public function delete_meta(&$object, $meta) { |
|
| 93 | + delete_metadata_by_mid($this->meta_type, $meta->id); |
|
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | /** |
@@ -101,8 +101,8 @@ discard block |
||
| 101 | 101 | * @param stdClass $meta (containing ->key and ->value). |
| 102 | 102 | * @return int meta ID |
| 103 | 103 | */ |
| 104 | - public function add_meta( &$object, $meta ) { |
|
| 105 | - return add_metadata( $this->meta_type, $object->get_id(), $meta->key, is_string( $meta->value ) ? wp_slash( $meta->value ) : $meta->value, false ); |
|
| 104 | + public function add_meta(&$object, $meta) { |
|
| 105 | + return add_metadata($this->meta_type, $object->get_id(), $meta->key, is_string($meta->value) ? wp_slash($meta->value) : $meta->value, false); |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | /** |
@@ -112,8 +112,8 @@ discard block |
||
| 112 | 112 | * @param GetPaid_Data $object GetPaid_Data object. |
| 113 | 113 | * @param stdClass $meta (containing ->id, ->key and ->value). |
| 114 | 114 | */ |
| 115 | - public function update_meta( &$object, $meta ) { |
|
| 116 | - update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key ); |
|
| 115 | + public function update_meta(&$object, $meta) { |
|
| 116 | + update_metadata_by_mid($this->meta_type, $meta->id, $meta->value, $meta->key); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | /** |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | $table = $wpdb->prefix; |
| 130 | 130 | |
| 131 | 131 | // If we are dealing with a type of metadata that is not a core type, the table should be prefixed. |
| 132 | - if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ), true ) ) { |
|
| 132 | + if (!in_array($this->meta_type, array('post', 'user', 'comment', 'term'), true)) { |
|
| 133 | 133 | $table .= 'getpaid_'; |
| 134 | 134 | } |
| 135 | 135 | |
@@ -137,12 +137,12 @@ discard block |
||
| 137 | 137 | $object_id_field = $this->meta_type . '_id'; |
| 138 | 138 | |
| 139 | 139 | // Figure out our field names. |
| 140 | - if ( 'user' === $this->meta_type ) { |
|
| 140 | + if ('user' === $this->meta_type) { |
|
| 141 | 141 | $meta_id_field = 'umeta_id'; |
| 142 | 142 | $table = $wpdb->usermeta; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | - if ( ! empty( $this->object_id_field_for_meta ) ) { |
|
| 145 | + if (!empty($this->object_id_field_for_meta)) { |
|
| 146 | 146 | $object_id_field = $this->object_id_field_for_meta; |
| 147 | 147 | } |
| 148 | 148 | |
@@ -162,8 +162,8 @@ discard block |
||
| 162 | 162 | * @param string $key Prefix to be added to meta keys. |
| 163 | 163 | * @return string |
| 164 | 164 | */ |
| 165 | - protected function prefix_key( $key ) { |
|
| 166 | - return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key; |
|
| 165 | + protected function prefix_key($key) { |
|
| 166 | + return '_' === substr($key, 0, 1) ? $key : '_' . $key; |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | /** |
@@ -172,8 +172,8 @@ discard block |
||
| 172 | 172 | * @param object $meta Meta object to check if it should be excluded or not. |
| 173 | 173 | * @return bool |
| 174 | 174 | */ |
| 175 | - protected function exclude_internal_meta_keys( $meta ) { |
|
| 176 | - return ! in_array( $meta->meta_key, $this->internal_meta_keys, true ) && 0 !== stripos( $meta->meta_key, 'wp_' ); |
|
| 175 | + protected function exclude_internal_meta_keys($meta) { |
|
| 176 | + return !in_array($meta->meta_key, $this->internal_meta_keys, true) && 0 !== stripos($meta->meta_key, 'wp_'); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | /** |
@@ -185,14 +185,14 @@ discard block |
||
| 185 | 185 | * @param string $meta_type The internal WP meta type (post, user, etc). |
| 186 | 186 | * @return array A mapping of meta keys => prop names, filtered by ones that should be updated. |
| 187 | 187 | */ |
| 188 | - protected function get_props_to_update( $object, $meta_key_to_props, $meta_type = 'post' ) { |
|
| 188 | + protected function get_props_to_update($object, $meta_key_to_props, $meta_type = 'post') { |
|
| 189 | 189 | $props_to_update = array(); |
| 190 | 190 | $changed_props = $object->get_changes(); |
| 191 | 191 | |
| 192 | 192 | // Props should be updated if they are a part of the $changed array or don't exist yet. |
| 193 | - foreach ( $meta_key_to_props as $meta_key => $prop ) { |
|
| 194 | - if ( array_key_exists( $prop, $changed_props ) || ! metadata_exists( $meta_type, $object->get_id(), $meta_key ) ) { |
|
| 195 | - $props_to_update[ $meta_key ] = $prop; |
|
| 193 | + foreach ($meta_key_to_props as $meta_key => $prop) { |
|
| 194 | + if (array_key_exists($prop, $changed_props) || !metadata_exists($meta_type, $object->get_id(), $meta_key)) { |
|
| 195 | + $props_to_update[$meta_key] = $prop; |
|
| 196 | 196 | } |
| 197 | 197 | } |
| 198 | 198 | |
@@ -206,22 +206,22 @@ discard block |
||
| 206 | 206 | * @param WP_Post $post_object Post object. |
| 207 | 207 | * @since 1.0.19 |
| 208 | 208 | */ |
| 209 | - protected function read_object_data( &$object, $post_object ) { |
|
| 209 | + protected function read_object_data(&$object, $post_object) { |
|
| 210 | 210 | $id = $object->get_id(); |
| 211 | 211 | $props = array(); |
| 212 | 212 | |
| 213 | - foreach ( $this->meta_key_to_props as $meta_key => $prop ) { |
|
| 214 | - $props[ $prop ] = get_post_meta( $id, $meta_key, true ); |
|
| 213 | + foreach ($this->meta_key_to_props as $meta_key => $prop) { |
|
| 214 | + $props[$prop] = get_post_meta($id, $meta_key, true); |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | // Set object properties. |
| 218 | - $object->set_props( $props ); |
|
| 218 | + $object->set_props($props); |
|
| 219 | 219 | |
| 220 | 220 | // Gets extra data associated with the object if needed. |
| 221 | - foreach ( $object->get_extra_data_keys() as $key ) { |
|
| 221 | + foreach ($object->get_extra_data_keys() as $key) { |
|
| 222 | 222 | $function = 'set_' . $key; |
| 223 | - if ( is_callable( array( $object, $function ) ) ) { |
|
| 224 | - $object->{$function}( get_post_meta( $object->get_id(), $key, true ) ); |
|
| 223 | + if (is_callable(array($object, $function))) { |
|
| 224 | + $object->{$function}(get_post_meta($object->get_id(), $key, true)); |
|
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | 227 | } |
@@ -232,24 +232,24 @@ discard block |
||
| 232 | 232 | * @param GetPaid_Data $object GetPaid_Data object. |
| 233 | 233 | * @since 1.0.19 |
| 234 | 234 | */ |
| 235 | - protected function update_post_meta( &$object ) { |
|
| 235 | + protected function update_post_meta(&$object) { |
|
| 236 | 236 | |
| 237 | 237 | $updated_props = array(); |
| 238 | - $props_to_update = $this->get_props_to_update( $object, $this->meta_key_to_props ); |
|
| 238 | + $props_to_update = $this->get_props_to_update($object, $this->meta_key_to_props); |
|
| 239 | 239 | $object_type = $object->get_object_type(); |
| 240 | 240 | |
| 241 | - foreach ( $props_to_update as $meta_key => $prop ) { |
|
| 242 | - $value = $object->{"get_$prop"}( 'edit' ); |
|
| 243 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
| 241 | + foreach ($props_to_update as $meta_key => $prop) { |
|
| 242 | + $value = $object->{"get_$prop"}('edit'); |
|
| 243 | + $value = is_string($value) ? wp_slash($value) : $value; |
|
| 244 | 244 | |
| 245 | - $updated = $this->update_or_delete_post_meta( $object, $meta_key, $value ); |
|
| 245 | + $updated = $this->update_or_delete_post_meta($object, $meta_key, $value); |
|
| 246 | 246 | |
| 247 | - if ( $updated ) { |
|
| 247 | + if ($updated) { |
|
| 248 | 248 | $updated_props[] = $prop; |
| 249 | 249 | } |
| 250 | 250 | } |
| 251 | 251 | |
| 252 | - do_action( "getpaid_{$object_type}_object_updated_props", $object, $updated_props ); |
|
| 252 | + do_action("getpaid_{$object_type}_object_updated_props", $object, $updated_props); |
|
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | /** |
@@ -269,11 +269,11 @@ discard block |
||
| 269 | 269 | * |
| 270 | 270 | * @return bool True if updated/deleted. |
| 271 | 271 | */ |
| 272 | - protected function update_or_delete_post_meta( $object, $meta_key, $meta_value ) { |
|
| 273 | - if ( in_array( $meta_value, array( array(), '', null ), true ) && ! in_array( $meta_key, $this->must_exist_meta_keys, true ) ) { |
|
| 274 | - $updated = delete_post_meta( $object->get_id(), $meta_key ); |
|
| 272 | + protected function update_or_delete_post_meta($object, $meta_key, $meta_value) { |
|
| 273 | + if (in_array($meta_value, array(array(), '', null), true) && !in_array($meta_key, $this->must_exist_meta_keys, true)) { |
|
| 274 | + $updated = delete_post_meta($object->get_id(), $meta_key); |
|
| 275 | 275 | } else { |
| 276 | - $updated = update_post_meta( $object->get_id(), $meta_key, $meta_value ); |
|
| 276 | + $updated = update_post_meta($object->get_id(), $meta_key, $meta_value); |
|
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | return (bool) $updated; |
@@ -295,8 +295,8 @@ discard block |
||
| 295 | 295 | * @param GetPaid_Data $object GetPaid_Data object. |
| 296 | 296 | * @since 1.0.19 |
| 297 | 297 | */ |
| 298 | - protected function clear_caches( &$object ) { |
|
| 299 | - clean_post_cache( $object->get_id() ); |
|
| 298 | + protected function clear_caches(&$object) { |
|
| 299 | + clean_post_cache($object->get_id()); |
|
| 300 | 300 | } |
| 301 | 301 | |
| 302 | 302 | /** |
@@ -307,33 +307,33 @@ discard block |
||
| 307 | 307 | * |
| 308 | 308 | * @return void |
| 309 | 309 | */ |
| 310 | - public function delete( &$object, $args = array() ) { |
|
| 310 | + public function delete(&$object, $args = array()) { |
|
| 311 | 311 | $id = $object->get_id(); |
| 312 | 312 | $object_type = $object->get_object_type(); |
| 313 | 313 | |
| 314 | - if ( 'invoice' == $object_type ) { |
|
| 314 | + if ('invoice' == $object_type) { |
|
| 315 | 315 | $object_type = $object->get_type(); |
| 316 | 316 | } |
| 317 | 317 | |
| 318 | - $args = wp_parse_args( |
|
| 318 | + $args = wp_parse_args( |
|
| 319 | 319 | $args, |
| 320 | 320 | array( |
| 321 | 321 | 'force_delete' => false, |
| 322 | 322 | ) |
| 323 | 323 | ); |
| 324 | 324 | |
| 325 | - if ( ! $id ) { |
|
| 325 | + if (!$id) { |
|
| 326 | 326 | return; |
| 327 | 327 | } |
| 328 | 328 | |
| 329 | - if ( $args['force_delete'] ) { |
|
| 330 | - wp_delete_post( $id, true ); |
|
| 331 | - $object->set_id( 0 ); |
|
| 332 | - do_action( "getpaid_delete_$object_type", $id ); |
|
| 329 | + if ($args['force_delete']) { |
|
| 330 | + wp_delete_post($id, true); |
|
| 331 | + $object->set_id(0); |
|
| 332 | + do_action("getpaid_delete_$object_type", $id); |
|
| 333 | 333 | } else { |
| 334 | - wp_trash_post( $id ); |
|
| 335 | - $object->set_status( 'trash' ); |
|
| 336 | - do_action( "getpaid_trash_$object_type", $id ); |
|
| 334 | + wp_trash_post($id); |
|
| 335 | + $object->set_status('trash'); |
|
| 336 | + do_action("getpaid_trash_$object_type", $id); |
|
| 337 | 337 | } |
| 338 | 338 | } |
| 339 | 339 | |
@@ -345,12 +345,12 @@ discard block |
||
| 345 | 345 | * @param GetPaid_Data $object GetPaid_Data object. |
| 346 | 346 | * @return string |
| 347 | 347 | */ |
| 348 | - protected function get_post_status( $object ) { |
|
| 349 | - $object_status = $object->get_status( 'edit' ); |
|
| 348 | + protected function get_post_status($object) { |
|
| 349 | + $object_status = $object->get_status('edit'); |
|
| 350 | 350 | $object_type = $object->get_object_type(); |
| 351 | 351 | |
| 352 | - if ( ! $object_status ) { |
|
| 353 | - $object_status = apply_filters( "getpaid_default_{$object_type}_status", 'draft' ); |
|
| 352 | + if (!$object_status) { |
|
| 353 | + $object_status = apply_filters("getpaid_default_{$object_type}_status", 'draft'); |
|
| 354 | 354 | } |
| 355 | 355 | |
| 356 | 356 | return $object_status; |
@@ -96,13 +96,13 @@ discard block |
||
| 96 | 96 | * @return array |
| 97 | 97 | */ |
| 98 | 98 | function wpinv_get_invoice_statuses( $draft = false, $trashed = false, $invoice = false ) { |
| 99 | - $invoice_statuses = array( |
|
| 100 | - 'wpi-pending' => _x( 'Pending payment', 'Invoice status', 'invoicing' ), |
|
| 99 | + $invoice_statuses = array( |
|
| 100 | + 'wpi-pending' => _x( 'Pending payment', 'Invoice status', 'invoicing' ), |
|
| 101 | 101 | 'publish' => _x( 'Paid', 'Invoice status', 'invoicing' ), |
| 102 | 102 | 'wpi-processing' => _x( 'Processing', 'Invoice status', 'invoicing' ), |
| 103 | - 'wpi-onhold' => _x( 'On hold', 'Invoice status', 'invoicing' ), |
|
| 104 | - 'wpi-cancelled' => _x( 'Cancelled', 'Invoice status', 'invoicing' ), |
|
| 105 | - 'wpi-refunded' => _x( 'Refunded', 'Invoice status', 'invoicing' ), |
|
| 103 | + 'wpi-onhold' => _x( 'On hold', 'Invoice status', 'invoicing' ), |
|
| 104 | + 'wpi-cancelled' => _x( 'Cancelled', 'Invoice status', 'invoicing' ), |
|
| 105 | + 'wpi-refunded' => _x( 'Refunded', 'Invoice status', 'invoicing' ), |
|
| 106 | 106 | 'wpi-failed' => _x( 'Failed', 'Invoice status', 'invoicing' ), |
| 107 | 107 | 'wpi-renewal' => _x( 'Renewal Payment', 'Invoice status', 'invoicing' ), |
| 108 | 108 | ); |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | $invoice_statuses['trash'] = __( 'Trash', 'invoicing' ); |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | - return apply_filters( 'wpinv_statuses', $invoice_statuses, $invoice ); |
|
| 118 | + return apply_filters( 'wpinv_statuses', $invoice_statuses, $invoice ); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | function wpinv_status_nicename( $status ) { |
@@ -712,13 +712,13 @@ discard block |
||
| 712 | 712 | * @param mixed $value Value. |
| 713 | 713 | */ |
| 714 | 714 | function getpaid_maybe_define_constant( $name, $value ) { |
| 715 | - if ( ! defined( $name ) ) { |
|
| 716 | - define( $name, $value ); |
|
| 717 | - } |
|
| 715 | + if ( ! defined( $name ) ) { |
|
| 716 | + define( $name, $value ); |
|
| 717 | + } |
|
| 718 | 718 | } |
| 719 | 719 | |
| 720 | 720 | function wpinv_get_php_arg_separator_output() { |
| 721 | - return ini_get( 'arg_separator.output' ); |
|
| 721 | + return ini_get( 'arg_separator.output' ); |
|
| 722 | 722 | } |
| 723 | 723 | |
| 724 | 724 | function wpinv_rgb_from_hex( $color ) { |
@@ -1067,11 +1067,11 @@ discard block |
||
| 1067 | 1067 | $list = array(); |
| 1068 | 1068 | } |
| 1069 | 1069 | |
| 1070 | - if ( ! is_array( $list ) ) { |
|
| 1071 | - return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 1072 | - } |
|
| 1070 | + if ( ! is_array( $list ) ) { |
|
| 1071 | + return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 1072 | + } |
|
| 1073 | 1073 | |
| 1074 | - return $list; |
|
| 1074 | + return $list; |
|
| 1075 | 1075 | } |
| 1076 | 1076 | |
| 1077 | 1077 | /** |
@@ -1091,9 +1091,9 @@ discard block |
||
| 1091 | 1091 | } |
| 1092 | 1092 | |
| 1093 | 1093 | $data = apply_filters( "wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php" ); |
| 1094 | - wp_cache_set( "wpinv-$key", $data, 'wpinv' ); |
|
| 1094 | + wp_cache_set( "wpinv-$key", $data, 'wpinv' ); |
|
| 1095 | 1095 | |
| 1096 | - return $data; |
|
| 1096 | + return $data; |
|
| 1097 | 1097 | } |
| 1098 | 1098 | |
| 1099 | 1099 | /** |
@@ -1122,17 +1122,17 @@ discard block |
||
| 1122 | 1122 | */ |
| 1123 | 1123 | function wpinv_clean( $var ) { |
| 1124 | 1124 | |
| 1125 | - if ( is_array( $var ) ) { |
|
| 1126 | - return array_map( 'wpinv_clean', $var ); |
|
| 1125 | + if ( is_array( $var ) ) { |
|
| 1126 | + return array_map( 'wpinv_clean', $var ); |
|
| 1127 | 1127 | } |
| 1128 | 1128 | |
| 1129 | 1129 | if ( is_object( $var ) ) { |
| 1130 | - $object_vars = get_object_vars( $var ); |
|
| 1131 | - foreach ( $object_vars as $property_name => $property_value ) { |
|
| 1132 | - $var->$property_name = wpinv_clean( $property_value ); |
|
| 1130 | + $object_vars = get_object_vars( $var ); |
|
| 1131 | + foreach ( $object_vars as $property_name => $property_value ) { |
|
| 1132 | + $var->$property_name = wpinv_clean( $property_value ); |
|
| 1133 | 1133 | } |
| 1134 | 1134 | return $var; |
| 1135 | - } |
|
| 1135 | + } |
|
| 1136 | 1136 | |
| 1137 | 1137 | return is_string( $var ) ? sanitize_text_field( $var ) : $var; |
| 1138 | 1138 | } |
@@ -1145,7 +1145,7 @@ discard block |
||
| 1145 | 1145 | */ |
| 1146 | 1146 | function getpaid_convert_price_string_to_options( $str ) { |
| 1147 | 1147 | |
| 1148 | - $raw_options = array_map( 'trim', explode( ',', $str ) ); |
|
| 1148 | + $raw_options = array_map( 'trim', explode( ',', $str ) ); |
|
| 1149 | 1149 | $options = array(); |
| 1150 | 1150 | |
| 1151 | 1151 | foreach ( $raw_options as $option ) { |
@@ -7,86 +7,86 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | // MUST have WordPress. |
| 10 | -if ( !defined( 'WPINC' ) ) { |
|
| 11 | - exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) ); |
|
| 10 | +if (!defined('WPINC')) { |
|
| 11 | + exit('Do NOT access this file directly: ' . basename(__FILE__)); |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | function wpinv_item_quantities_enabled() { |
| 15 | - $ret = wpinv_get_option( 'item_quantities', true ); |
|
| 15 | + $ret = wpinv_get_option('item_quantities', true); |
|
| 16 | 16 | |
| 17 | - return (bool) apply_filters( 'wpinv_item_quantities_enabled', $ret ); |
|
| 17 | + return (bool) apply_filters('wpinv_item_quantities_enabled', $ret); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | function wpinv_get_ip() { |
| 21 | 21 | $ip = '127.0.0.1'; |
| 22 | 22 | |
| 23 | - if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
|
| 24 | - $ip = sanitize_text_field( $_SERVER['HTTP_CLIENT_IP'] ); |
|
| 25 | - } elseif ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
|
| 26 | - $ip = sanitize_text_field( $_SERVER['HTTP_X_FORWARDED_FOR'] ); |
|
| 27 | - } elseif( !empty( $_SERVER['REMOTE_ADDR'] ) ) { |
|
| 28 | - $ip = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ); |
|
| 23 | + if (!empty($_SERVER['HTTP_CLIENT_IP'])) { |
|
| 24 | + $ip = sanitize_text_field($_SERVER['HTTP_CLIENT_IP']); |
|
| 25 | + } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
|
| 26 | + $ip = sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']); |
|
| 27 | + } elseif (!empty($_SERVER['REMOTE_ADDR'])) { |
|
| 28 | + $ip = sanitize_text_field($_SERVER['REMOTE_ADDR']); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - return apply_filters( 'wpinv_get_ip', $ip ); |
|
| 31 | + return apply_filters('wpinv_get_ip', $ip); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | function wpinv_get_user_agent() { |
| 35 | - if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) { |
|
| 36 | - $user_agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ); |
|
| 35 | + if (!empty($_SERVER['HTTP_USER_AGENT'])) { |
|
| 36 | + $user_agent = sanitize_text_field($_SERVER['HTTP_USER_AGENT']); |
|
| 37 | 37 | } else { |
| 38 | 38 | $user_agent = ''; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - return apply_filters( 'wpinv_get_user_agent', $user_agent ); |
|
| 41 | + return apply_filters('wpinv_get_user_agent', $user_agent); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | -function wpinv_sanitize_amount( $amount, $decimals = NULL ) { |
|
| 44 | +function wpinv_sanitize_amount($amount, $decimals = NULL) { |
|
| 45 | 45 | $is_negative = false; |
| 46 | 46 | $thousands_sep = wpinv_thousands_separator(); |
| 47 | 47 | $decimal_sep = wpinv_decimal_separator(); |
| 48 | - if ( $decimals === NULL ) { |
|
| 48 | + if ($decimals === NULL) { |
|
| 49 | 49 | $decimals = wpinv_decimals(); |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | // Sanitize the amount |
| 53 | - if ( $decimal_sep == ',' && false !== ( $found = strpos( $amount, $decimal_sep ) ) ) { |
|
| 54 | - if ( ( $thousands_sep == '.' || $thousands_sep == ' ' ) && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) { |
|
| 55 | - $amount = str_replace( $thousands_sep, '', $amount ); |
|
| 56 | - } elseif( empty( $thousands_sep ) && false !== ( $found = strpos( $amount, '.' ) ) ) { |
|
| 57 | - $amount = str_replace( '.', '', $amount ); |
|
| 53 | + if ($decimal_sep == ',' && false !== ($found = strpos($amount, $decimal_sep))) { |
|
| 54 | + if (($thousands_sep == '.' || $thousands_sep == ' ') && false !== ($found = strpos($amount, $thousands_sep))) { |
|
| 55 | + $amount = str_replace($thousands_sep, '', $amount); |
|
| 56 | + } elseif (empty($thousands_sep) && false !== ($found = strpos($amount, '.'))) { |
|
| 57 | + $amount = str_replace('.', '', $amount); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - $amount = str_replace( $decimal_sep, '.', $amount ); |
|
| 61 | - } elseif( $thousands_sep == ',' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) { |
|
| 62 | - $amount = str_replace( $thousands_sep, '', $amount ); |
|
| 60 | + $amount = str_replace($decimal_sep, '.', $amount); |
|
| 61 | + } elseif ($thousands_sep == ',' && false !== ($found = strpos($amount, $thousands_sep))) { |
|
| 62 | + $amount = str_replace($thousands_sep, '', $amount); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - if( $amount < 0 ) { |
|
| 65 | + if ($amount < 0) { |
|
| 66 | 66 | $is_negative = true; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - $amount = preg_replace( '/[^0-9\.]/', '', $amount ); |
|
| 69 | + $amount = preg_replace('/[^0-9\.]/', '', $amount); |
|
| 70 | 70 | |
| 71 | - $decimals = apply_filters( 'wpinv_sanitize_amount_decimals', absint( $decimals ), $amount ); |
|
| 72 | - $amount = number_format( (double) $amount, absint( $decimals ), '.', '' ); |
|
| 71 | + $decimals = apply_filters('wpinv_sanitize_amount_decimals', absint($decimals), $amount); |
|
| 72 | + $amount = number_format((double) $amount, absint($decimals), '.', ''); |
|
| 73 | 73 | |
| 74 | - if( $is_negative ) { |
|
| 74 | + if ($is_negative) { |
|
| 75 | 75 | $amount *= -1; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - return apply_filters( 'wpinv_sanitize_amount', $amount, $decimals ); |
|
| 78 | + return apply_filters('wpinv_sanitize_amount', $amount, $decimals); |
|
| 79 | 79 | } |
| 80 | -add_filter( 'wpinv_sanitize_amount_decimals', 'wpinv_currency_decimal_filter', 10, 1 ); |
|
| 80 | +add_filter('wpinv_sanitize_amount_decimals', 'wpinv_currency_decimal_filter', 10, 1); |
|
| 81 | 81 | |
| 82 | -function wpinv_round_amount( $amount, $decimals = NULL ) { |
|
| 83 | - if ( $decimals === NULL ) { |
|
| 82 | +function wpinv_round_amount($amount, $decimals = NULL) { |
|
| 83 | + if ($decimals === NULL) { |
|
| 84 | 84 | $decimals = wpinv_decimals(); |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - $amount = round( (double)$amount, wpinv_currency_decimal_filter( absint( $decimals ) ) ); |
|
| 87 | + $amount = round((double) $amount, wpinv_currency_decimal_filter(absint($decimals))); |
|
| 88 | 88 | |
| 89 | - return apply_filters( 'wpinv_round_amount', $amount, $decimals ); |
|
| 89 | + return apply_filters('wpinv_round_amount', $amount, $decimals); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | /** |
@@ -95,48 +95,48 @@ discard block |
||
| 95 | 95 | * @since 1.0.19 |
| 96 | 96 | * @return array |
| 97 | 97 | */ |
| 98 | -function wpinv_get_invoice_statuses( $draft = false, $trashed = false, $invoice = false ) { |
|
| 98 | +function wpinv_get_invoice_statuses($draft = false, $trashed = false, $invoice = false) { |
|
| 99 | 99 | $invoice_statuses = array( |
| 100 | - 'wpi-pending' => _x( 'Pending payment', 'Invoice status', 'invoicing' ), |
|
| 101 | - 'publish' => _x( 'Paid', 'Invoice status', 'invoicing' ), |
|
| 102 | - 'wpi-processing' => _x( 'Processing', 'Invoice status', 'invoicing' ), |
|
| 103 | - 'wpi-onhold' => _x( 'On hold', 'Invoice status', 'invoicing' ), |
|
| 104 | - 'wpi-cancelled' => _x( 'Cancelled', 'Invoice status', 'invoicing' ), |
|
| 105 | - 'wpi-refunded' => _x( 'Refunded', 'Invoice status', 'invoicing' ), |
|
| 106 | - 'wpi-failed' => _x( 'Failed', 'Invoice status', 'invoicing' ), |
|
| 107 | - 'wpi-renewal' => _x( 'Renewal Payment', 'Invoice status', 'invoicing' ), |
|
| 100 | + 'wpi-pending' => _x('Pending payment', 'Invoice status', 'invoicing'), |
|
| 101 | + 'publish' => _x('Paid', 'Invoice status', 'invoicing'), |
|
| 102 | + 'wpi-processing' => _x('Processing', 'Invoice status', 'invoicing'), |
|
| 103 | + 'wpi-onhold' => _x('On hold', 'Invoice status', 'invoicing'), |
|
| 104 | + 'wpi-cancelled' => _x('Cancelled', 'Invoice status', 'invoicing'), |
|
| 105 | + 'wpi-refunded' => _x('Refunded', 'Invoice status', 'invoicing'), |
|
| 106 | + 'wpi-failed' => _x('Failed', 'Invoice status', 'invoicing'), |
|
| 107 | + 'wpi-renewal' => _x('Renewal Payment', 'Invoice status', 'invoicing'), |
|
| 108 | 108 | ); |
| 109 | 109 | |
| 110 | - if ( $draft ) { |
|
| 111 | - $invoice_statuses['draft'] = __( 'Draft', 'invoicing' ); |
|
| 110 | + if ($draft) { |
|
| 111 | + $invoice_statuses['draft'] = __('Draft', 'invoicing'); |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | - if ( $trashed ) { |
|
| 115 | - $invoice_statuses['trash'] = __( 'Trash', 'invoicing' ); |
|
| 114 | + if ($trashed) { |
|
| 115 | + $invoice_statuses['trash'] = __('Trash', 'invoicing'); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | - return apply_filters( 'wpinv_statuses', $invoice_statuses, $invoice ); |
|
| 118 | + return apply_filters('wpinv_statuses', $invoice_statuses, $invoice); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | -function wpinv_status_nicename( $status ) { |
|
| 122 | - $statuses = wpinv_get_invoice_statuses( true, true ); |
|
| 123 | - $status = isset( $statuses[$status] ) ? $statuses[$status] : __( $status, 'invoicing' ); |
|
| 121 | +function wpinv_status_nicename($status) { |
|
| 122 | + $statuses = wpinv_get_invoice_statuses(true, true); |
|
| 123 | + $status = isset($statuses[$status]) ? $statuses[$status] : __($status, 'invoicing'); |
|
| 124 | 124 | |
| 125 | 125 | return $status; |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | function wpinv_get_currency() { |
| 129 | - $currency = wpinv_get_option( 'currency', 'USD' ); |
|
| 129 | + $currency = wpinv_get_option('currency', 'USD'); |
|
| 130 | 130 | |
| 131 | - return apply_filters( 'wpinv_currency', $currency ); |
|
| 131 | + return apply_filters('wpinv_currency', $currency); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | -function wpinv_currency_symbol( $currency = '' ) { |
|
| 135 | - if ( empty( $currency ) ) { |
|
| 134 | +function wpinv_currency_symbol($currency = '') { |
|
| 135 | + if (empty($currency)) { |
|
| 136 | 136 | $currency = wpinv_get_currency(); |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | - $symbols = apply_filters( 'wpinv_currency_symbols', array( |
|
| 139 | + $symbols = apply_filters('wpinv_currency_symbols', array( |
|
| 140 | 140 | 'AED' => 'د.إ', |
| 141 | 141 | 'AFN' => '؋', |
| 142 | 142 | 'ALL' => 'L', |
@@ -299,209 +299,209 @@ discard block |
||
| 299 | 299 | 'YER' => '﷼', |
| 300 | 300 | 'ZAR' => 'R', |
| 301 | 301 | 'ZMW' => 'ZK', |
| 302 | - ) ); |
|
| 302 | + )); |
|
| 303 | 303 | |
| 304 | - $currency_symbol = isset( $symbols[$currency] ) ? $symbols[$currency] : $currency; |
|
| 304 | + $currency_symbol = isset($symbols[$currency]) ? $symbols[$currency] : $currency; |
|
| 305 | 305 | |
| 306 | - return apply_filters( 'wpinv_currency_symbol', $currency_symbol, $currency ); |
|
| 306 | + return apply_filters('wpinv_currency_symbol', $currency_symbol, $currency); |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | function wpinv_currency_position() { |
| 310 | - $position = wpinv_get_option( 'currency_position', 'left' ); |
|
| 310 | + $position = wpinv_get_option('currency_position', 'left'); |
|
| 311 | 311 | |
| 312 | - return apply_filters( 'wpinv_currency_position', $position ); |
|
| 312 | + return apply_filters('wpinv_currency_position', $position); |
|
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | function wpinv_thousands_separator() { |
| 316 | - $thousand_sep = wpinv_get_option( 'thousands_separator', ',' ); |
|
| 316 | + $thousand_sep = wpinv_get_option('thousands_separator', ','); |
|
| 317 | 317 | |
| 318 | - return apply_filters( 'wpinv_thousands_separator', $thousand_sep ); |
|
| 318 | + return apply_filters('wpinv_thousands_separator', $thousand_sep); |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | function wpinv_decimal_separator() { |
| 322 | - $decimal_sep = wpinv_get_option( 'decimal_separator', '.' ); |
|
| 322 | + $decimal_sep = wpinv_get_option('decimal_separator', '.'); |
|
| 323 | 323 | |
| 324 | - return apply_filters( 'wpinv_decimal_separator', $decimal_sep ); |
|
| 324 | + return apply_filters('wpinv_decimal_separator', $decimal_sep); |
|
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | function wpinv_decimals() { |
| 328 | - $decimals = apply_filters( 'wpinv_decimals', wpinv_get_option( 'decimals', 2 ) ); |
|
| 328 | + $decimals = apply_filters('wpinv_decimals', wpinv_get_option('decimals', 2)); |
|
| 329 | 329 | |
| 330 | - return absint( $decimals ); |
|
| 330 | + return absint($decimals); |
|
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 333 | function wpinv_get_currencies() { |
| 334 | 334 | $currencies = array( |
| 335 | - 'USD' => __( 'US Dollar', 'invoicing' ), |
|
| 336 | - 'EUR' => __( 'Euro', 'invoicing' ), |
|
| 337 | - 'GBP' => __( 'Pound Sterling', 'invoicing' ), |
|
| 338 | - 'AED' => __( 'United Arab Emirates', 'invoicing' ), |
|
| 339 | - 'AFN' => __( 'Afghan Afghani', 'invoicing' ), |
|
| 340 | - 'ALL' => __( 'Albanian Lek', 'invoicing' ), |
|
| 341 | - 'AMD' => __( 'Armenian Dram', 'invoicing' ), |
|
| 342 | - 'ANG' => __( 'Netherlands Antillean Guilder', 'invoicing' ), |
|
| 343 | - 'AOA' => __( 'Angolan Kwanza', 'invoicing' ), |
|
| 344 | - 'ARS' => __( 'Argentine Peso', 'invoicing' ), |
|
| 345 | - 'AUD' => __( 'Australian Dollar', 'invoicing' ), |
|
| 346 | - 'AWG' => __( 'Aruban Florin', 'invoicing' ), |
|
| 347 | - 'AZN' => __( 'Azerbaijani Manat', 'invoicing' ), |
|
| 348 | - 'BAM' => __( 'Bosnia and Herzegovina Convertible Marka', 'invoicing' ), |
|
| 349 | - 'BBD' => __( 'Barbadian Dollar', 'invoicing' ), |
|
| 350 | - 'BDT' => __( 'Bangladeshi Taka', 'invoicing' ), |
|
| 351 | - 'BGN' => __( 'Bulgarian Lev', 'invoicing' ), |
|
| 352 | - 'BHD' => __( 'Bahraini Dinar', 'invoicing' ), |
|
| 353 | - 'BIF' => __( 'Burundian Franc', 'invoicing' ), |
|
| 354 | - 'BMD' => __( 'Bermudian Dollar', 'invoicing' ), |
|
| 355 | - 'BND' => __( 'Brunei Dollar', 'invoicing' ), |
|
| 356 | - 'BOB' => __( 'Bolivian Boliviano', 'invoicing' ), |
|
| 357 | - 'BRL' => __( 'Brazilian Real', 'invoicing' ), |
|
| 358 | - 'BSD' => __( 'Bahamian Dollar', 'invoicing' ), |
|
| 359 | - 'BTC' => __( 'Bitcoin', 'invoicing' ), |
|
| 360 | - 'BTN' => __( 'Bhutanese Ngultrum', 'invoicing' ), |
|
| 361 | - 'BWP' => __( 'Botswana Pula', 'invoicing' ), |
|
| 362 | - 'BYN' => __( 'Belarusian Ruble', 'invoicing' ), |
|
| 363 | - 'BZD' => __( 'Belize Dollar', 'invoicing' ), |
|
| 364 | - 'CAD' => __( 'Canadian Dollar', 'invoicing' ), |
|
| 365 | - 'CDF' => __( 'Congolese Franc', 'invoicing' ), |
|
| 366 | - 'CHF' => __( 'Swiss Franc', 'invoicing' ), |
|
| 367 | - 'CLP' => __( 'Chilean Peso', 'invoicing' ), |
|
| 368 | - 'CNY' => __( 'Chinese Yuan', 'invoicing' ), |
|
| 369 | - 'COP' => __( 'Colombian Peso', 'invoicing' ), |
|
| 370 | - 'CRC' => __( 'Costa Rican Colon', 'invoicing' ), |
|
| 371 | - 'CUC' => __( 'Cuban Convertible Peso', 'invoicing' ), |
|
| 372 | - 'CUP' => __( 'Cuban Peso', 'invoicing' ), |
|
| 373 | - 'CVE' => __( 'Cape Verdean escudo', 'invoicing' ), |
|
| 374 | - 'CZK' => __( 'Czech Koruna', 'invoicing' ), |
|
| 375 | - 'DJF' => __( 'Djiboutian Franc', 'invoicing' ), |
|
| 376 | - 'DKK' => __( 'Danish Krone', 'invoicing' ), |
|
| 377 | - 'DOP' => __( 'Dominican Peso', 'invoicing' ), |
|
| 378 | - 'DZD' => __( 'Algerian Dinar', 'invoicing' ), |
|
| 379 | - 'EGP' => __( 'Egyptian Pound', 'invoicing' ), |
|
| 380 | - 'ERN' => __( 'Eritrean Nakfa', 'invoicing' ), |
|
| 381 | - 'ETB' => __( 'Ethiopian Irr', 'invoicing' ), |
|
| 382 | - 'FJD' => __( 'Fijian Dollar', 'invoicing' ), |
|
| 383 | - 'FKP' => __( 'Falkland Islands Pound', 'invoicing' ), |
|
| 384 | - 'GEL' => __( 'Georgian Lari', 'invoicing' ), |
|
| 385 | - 'GGP' => __( 'Guernsey Pound', 'invoicing' ), |
|
| 386 | - 'GHS' => __( 'Ghana Cedi', 'invoicing' ), |
|
| 387 | - 'GIP' => __( 'Gibraltar Pound', 'invoicing' ), |
|
| 388 | - 'GMD' => __( 'Gambian Dalasi', 'invoicing' ), |
|
| 389 | - 'GNF' => __( 'Guinean Franc', 'invoicing' ), |
|
| 390 | - 'GTQ' => __( 'Guatemalan Quetzal', 'invoicing' ), |
|
| 391 | - 'GYD' => __( 'Guyanese Dollar', 'invoicing' ), |
|
| 392 | - 'HKD' => __( 'Hong Kong Dollar', 'invoicing' ), |
|
| 393 | - 'HNL' => __( 'Honduran Lempira', 'invoicing' ), |
|
| 394 | - 'HRK' => __( 'Croatian Kuna', 'invoicing' ), |
|
| 395 | - 'HTG' => __( 'Haitian Gourde', 'invoicing' ), |
|
| 396 | - 'HUF' => __( 'Hungarian Forint', 'invoicing' ), |
|
| 397 | - 'IDR' => __( 'Indonesian Rupiah', 'invoicing' ), |
|
| 398 | - 'ILS' => __( 'Israeli New Shekel', 'invoicing' ), |
|
| 399 | - 'IMP' => __( 'Manx Pound', 'invoicing' ), |
|
| 400 | - 'INR' => __( 'Indian Rupee', 'invoicing' ), |
|
| 401 | - 'IQD' => __( 'Iraqi Dinar', 'invoicing' ), |
|
| 402 | - 'IRR' => __( 'Iranian Rial', 'invoicing' ), |
|
| 403 | - 'IRT' => __( 'Iranian Toman', 'invoicing' ), |
|
| 404 | - 'ISK' => __( 'Icelandic Krona', 'invoicing' ), |
|
| 405 | - 'JEP' => __( 'Jersey Pound', 'invoicing' ), |
|
| 406 | - 'JMD' => __( 'Jamaican Dollar', 'invoicing' ), |
|
| 407 | - 'JOD' => __( 'Jordanian Dinar', 'invoicing' ), |
|
| 408 | - 'JPY' => __( 'Japanese Yen', 'invoicing' ), |
|
| 409 | - 'KES' => __( 'Kenyan Shilling', 'invoicing' ), |
|
| 410 | - 'KGS' => __( 'Kyrgyzstani Som', 'invoicing' ), |
|
| 411 | - 'KHR' => __( 'Cambodian Riel', 'invoicing' ), |
|
| 412 | - 'KMF' => __( 'Comorian Franc', 'invoicing' ), |
|
| 413 | - 'KPW' => __( 'North Korean Won', 'invoicing' ), |
|
| 414 | - 'KRW' => __( 'South Korean Won', 'invoicing' ), |
|
| 415 | - 'KWD' => __( 'Kuwaiti Dinar', 'invoicing' ), |
|
| 416 | - 'KYD' => __( 'Cayman Islands Dollar', 'invoicing' ), |
|
| 417 | - 'KZT' => __( 'Kazakhstani Tenge', 'invoicing' ), |
|
| 418 | - 'LAK' => __( 'Lao Kip', 'invoicing' ), |
|
| 419 | - 'LBP' => __( 'Lebanese Pound', 'invoicing' ), |
|
| 420 | - 'LKR' => __( 'Sri Lankan Rupee', 'invoicing' ), |
|
| 421 | - 'LRD' => __( 'Liberian Dollar', 'invoicing' ), |
|
| 422 | - 'LSL' => __( 'Lesotho Loti', 'invoicing' ), |
|
| 423 | - 'LYD' => __( 'Libyan Dinar', 'invoicing' ), |
|
| 424 | - 'MAD' => __( 'Moroccan Dirham', 'invoicing' ), |
|
| 425 | - 'MDL' => __( 'Moldovan Leu', 'invoicing' ), |
|
| 426 | - 'MGA' => __( 'Malagasy Ariary', 'invoicing' ), |
|
| 427 | - 'MKD' => __( 'Macedonian Denar', 'invoicing' ), |
|
| 428 | - 'MMK' => __( 'Burmese Kyat', 'invoicing' ), |
|
| 429 | - 'MNT' => __( 'Mongolian Tughrik', 'invoicing' ), |
|
| 430 | - 'MOP' => __( 'Macanese Pataca', 'invoicing' ), |
|
| 431 | - 'MRO' => __( 'Mauritanian Ouguiya', 'invoicing' ), |
|
| 432 | - 'MUR' => __( 'Mauritian Rupee', 'invoicing' ), |
|
| 433 | - 'MVR' => __( 'Maldivian Rufiyaa', 'invoicing' ), |
|
| 434 | - 'MWK' => __( 'Malawian Kwacha', 'invoicing' ), |
|
| 435 | - 'MXN' => __( 'Mexican Peso', 'invoicing' ), |
|
| 436 | - 'MYR' => __( 'Malaysian Ringgit', 'invoicing' ), |
|
| 437 | - 'MZN' => __( 'Mozambican Metical', 'invoicing' ), |
|
| 438 | - 'NAD' => __( 'Namibian Dollar', 'invoicing' ), |
|
| 439 | - 'NGN' => __( 'Nigerian Naira', 'invoicing' ), |
|
| 440 | - 'NIO' => __( 'Nicaraguan Cordoba', 'invoicing' ), |
|
| 441 | - 'NOK' => __( 'Norwegian Krone', 'invoicing' ), |
|
| 442 | - 'NPR' => __( 'Nepalese Rupee', 'invoicing' ), |
|
| 443 | - 'NZD' => __( 'New Zealand Dollar', 'invoicing' ), |
|
| 444 | - 'OMR' => __( 'Omani Rial', 'invoicing' ), |
|
| 445 | - 'PAB' => __( 'Panamanian Balboa', 'invoicing' ), |
|
| 446 | - 'PEN' => __( 'Peruvian Nuevo Sol', 'invoicing' ), |
|
| 447 | - 'PGK' => __( 'Papua New Guinean Kina', 'invoicing' ), |
|
| 448 | - 'PHP' => __( 'Philippine Peso', 'invoicing' ), |
|
| 449 | - 'PKR' => __( 'Pakistani Rupee', 'invoicing' ), |
|
| 450 | - 'PLN' => __( 'Polish Zloty', 'invoicing' ), |
|
| 451 | - 'PRB' => __( 'Transnistrian Ruble', 'invoicing' ), |
|
| 452 | - 'PYG' => __( 'Paraguayan Guarani', 'invoicing' ), |
|
| 453 | - 'QAR' => __( 'Qatari Riyal', 'invoicing' ), |
|
| 454 | - 'RON' => __( 'Romanian Leu', 'invoicing' ), |
|
| 455 | - 'RSD' => __( 'Serbian Dinar', 'invoicing' ), |
|
| 456 | - 'RUB' => __( 'Russian Ruble', 'invoicing' ), |
|
| 457 | - 'RWF' => __( 'Rwandan Franc', 'invoicing' ), |
|
| 458 | - 'SAR' => __( 'Saudi Riyal', 'invoicing' ), |
|
| 459 | - 'SBD' => __( 'Solomon Islands Dollar', 'invoicing' ), |
|
| 460 | - 'SCR' => __( 'Seychellois Rupee', 'invoicing' ), |
|
| 461 | - 'SDG' => __( 'Sudanese Pound', 'invoicing' ), |
|
| 462 | - 'SEK' => __( 'Swedish Krona', 'invoicing' ), |
|
| 463 | - 'SGD' => __( 'Singapore Dollar', 'invoicing' ), |
|
| 464 | - 'SHP' => __( 'Saint Helena Pound', 'invoicing' ), |
|
| 465 | - 'SLL' => __( 'Sierra Leonean Leone', 'invoicing' ), |
|
| 466 | - 'SOS' => __( 'Somali Shilling', 'invoicing' ), |
|
| 467 | - 'SRD' => __( 'Surinamese Dollar', 'invoicing' ), |
|
| 468 | - 'SSP' => __( 'South Sudanese Pound', 'invoicing' ), |
|
| 469 | - 'STD' => __( 'Sao Tomean Dobra', 'invoicing' ), |
|
| 470 | - 'SYP' => __( 'Syrian Pound', 'invoicing' ), |
|
| 471 | - 'SZL' => __( 'Swazi Lilangeni', 'invoicing' ), |
|
| 472 | - 'THB' => __( 'Thai Baht', 'invoicing' ), |
|
| 473 | - 'TJS' => __( 'Tajikistani Somoni', 'invoicing' ), |
|
| 474 | - 'TMT' => __( 'Turkmenistan Manat', 'invoicing' ), |
|
| 475 | - 'TND' => __( 'Tunisian Dinar', 'invoicing' ), |
|
| 476 | - 'TOP' => __( 'Tongan Paʻanga', 'invoicing' ), |
|
| 477 | - 'TRY' => __( 'Turkish Lira', 'invoicing' ), |
|
| 478 | - 'TTD' => __( 'Trinidad and Tobago Dollar', 'invoicing' ), |
|
| 479 | - 'TWD' => __( 'New Taiwan Dollar', 'invoicing' ), |
|
| 480 | - 'TZS' => __( 'Tanzanian Shilling', 'invoicing' ), |
|
| 481 | - 'UAH' => __( 'Ukrainian Hryvnia', 'invoicing' ), |
|
| 482 | - 'UGX' => __( 'Ugandan Shilling', 'invoicing' ), |
|
| 483 | - 'UYU' => __( 'Uruguayan Peso', 'invoicing' ), |
|
| 484 | - 'UZS' => __( 'Uzbekistani Som', 'invoicing' ), |
|
| 485 | - 'VEF' => __( 'Venezuelan Bolívar', 'invoicing' ), |
|
| 486 | - 'VND' => __( 'Vietnamese Dong', 'invoicing' ), |
|
| 487 | - 'VUV' => __( 'Vanuatu Vatu', 'invoicing' ), |
|
| 488 | - 'WST' => __( 'Samoan Tala', 'invoicing' ), |
|
| 489 | - 'XAF' => __( 'Central African CFA Franc', 'invoicing' ), |
|
| 490 | - 'XCD' => __( 'East Caribbean Dollar', 'invoicing' ), |
|
| 491 | - 'XOF' => __( 'West African CFA Franc', 'invoicing' ), |
|
| 492 | - 'XPF' => __( 'CFP Franc', 'invoicing' ), |
|
| 493 | - 'YER' => __( 'Yemeni Rial', 'invoicing' ), |
|
| 494 | - 'ZAR' => __( 'South African Rand', 'invoicing' ), |
|
| 495 | - 'ZMW' => __( 'Zambian Kwacha', 'invoicing' ), |
|
| 335 | + 'USD' => __('US Dollar', 'invoicing'), |
|
| 336 | + 'EUR' => __('Euro', 'invoicing'), |
|
| 337 | + 'GBP' => __('Pound Sterling', 'invoicing'), |
|
| 338 | + 'AED' => __('United Arab Emirates', 'invoicing'), |
|
| 339 | + 'AFN' => __('Afghan Afghani', 'invoicing'), |
|
| 340 | + 'ALL' => __('Albanian Lek', 'invoicing'), |
|
| 341 | + 'AMD' => __('Armenian Dram', 'invoicing'), |
|
| 342 | + 'ANG' => __('Netherlands Antillean Guilder', 'invoicing'), |
|
| 343 | + 'AOA' => __('Angolan Kwanza', 'invoicing'), |
|
| 344 | + 'ARS' => __('Argentine Peso', 'invoicing'), |
|
| 345 | + 'AUD' => __('Australian Dollar', 'invoicing'), |
|
| 346 | + 'AWG' => __('Aruban Florin', 'invoicing'), |
|
| 347 | + 'AZN' => __('Azerbaijani Manat', 'invoicing'), |
|
| 348 | + 'BAM' => __('Bosnia and Herzegovina Convertible Marka', 'invoicing'), |
|
| 349 | + 'BBD' => __('Barbadian Dollar', 'invoicing'), |
|
| 350 | + 'BDT' => __('Bangladeshi Taka', 'invoicing'), |
|
| 351 | + 'BGN' => __('Bulgarian Lev', 'invoicing'), |
|
| 352 | + 'BHD' => __('Bahraini Dinar', 'invoicing'), |
|
| 353 | + 'BIF' => __('Burundian Franc', 'invoicing'), |
|
| 354 | + 'BMD' => __('Bermudian Dollar', 'invoicing'), |
|
| 355 | + 'BND' => __('Brunei Dollar', 'invoicing'), |
|
| 356 | + 'BOB' => __('Bolivian Boliviano', 'invoicing'), |
|
| 357 | + 'BRL' => __('Brazilian Real', 'invoicing'), |
|
| 358 | + 'BSD' => __('Bahamian Dollar', 'invoicing'), |
|
| 359 | + 'BTC' => __('Bitcoin', 'invoicing'), |
|
| 360 | + 'BTN' => __('Bhutanese Ngultrum', 'invoicing'), |
|
| 361 | + 'BWP' => __('Botswana Pula', 'invoicing'), |
|
| 362 | + 'BYN' => __('Belarusian Ruble', 'invoicing'), |
|
| 363 | + 'BZD' => __('Belize Dollar', 'invoicing'), |
|
| 364 | + 'CAD' => __('Canadian Dollar', 'invoicing'), |
|
| 365 | + 'CDF' => __('Congolese Franc', 'invoicing'), |
|
| 366 | + 'CHF' => __('Swiss Franc', 'invoicing'), |
|
| 367 | + 'CLP' => __('Chilean Peso', 'invoicing'), |
|
| 368 | + 'CNY' => __('Chinese Yuan', 'invoicing'), |
|
| 369 | + 'COP' => __('Colombian Peso', 'invoicing'), |
|
| 370 | + 'CRC' => __('Costa Rican Colon', 'invoicing'), |
|
| 371 | + 'CUC' => __('Cuban Convertible Peso', 'invoicing'), |
|
| 372 | + 'CUP' => __('Cuban Peso', 'invoicing'), |
|
| 373 | + 'CVE' => __('Cape Verdean escudo', 'invoicing'), |
|
| 374 | + 'CZK' => __('Czech Koruna', 'invoicing'), |
|
| 375 | + 'DJF' => __('Djiboutian Franc', 'invoicing'), |
|
| 376 | + 'DKK' => __('Danish Krone', 'invoicing'), |
|
| 377 | + 'DOP' => __('Dominican Peso', 'invoicing'), |
|
| 378 | + 'DZD' => __('Algerian Dinar', 'invoicing'), |
|
| 379 | + 'EGP' => __('Egyptian Pound', 'invoicing'), |
|
| 380 | + 'ERN' => __('Eritrean Nakfa', 'invoicing'), |
|
| 381 | + 'ETB' => __('Ethiopian Irr', 'invoicing'), |
|
| 382 | + 'FJD' => __('Fijian Dollar', 'invoicing'), |
|
| 383 | + 'FKP' => __('Falkland Islands Pound', 'invoicing'), |
|
| 384 | + 'GEL' => __('Georgian Lari', 'invoicing'), |
|
| 385 | + 'GGP' => __('Guernsey Pound', 'invoicing'), |
|
| 386 | + 'GHS' => __('Ghana Cedi', 'invoicing'), |
|
| 387 | + 'GIP' => __('Gibraltar Pound', 'invoicing'), |
|
| 388 | + 'GMD' => __('Gambian Dalasi', 'invoicing'), |
|
| 389 | + 'GNF' => __('Guinean Franc', 'invoicing'), |
|
| 390 | + 'GTQ' => __('Guatemalan Quetzal', 'invoicing'), |
|
| 391 | + 'GYD' => __('Guyanese Dollar', 'invoicing'), |
|
| 392 | + 'HKD' => __('Hong Kong Dollar', 'invoicing'), |
|
| 393 | + 'HNL' => __('Honduran Lempira', 'invoicing'), |
|
| 394 | + 'HRK' => __('Croatian Kuna', 'invoicing'), |
|
| 395 | + 'HTG' => __('Haitian Gourde', 'invoicing'), |
|
| 396 | + 'HUF' => __('Hungarian Forint', 'invoicing'), |
|
| 397 | + 'IDR' => __('Indonesian Rupiah', 'invoicing'), |
|
| 398 | + 'ILS' => __('Israeli New Shekel', 'invoicing'), |
|
| 399 | + 'IMP' => __('Manx Pound', 'invoicing'), |
|
| 400 | + 'INR' => __('Indian Rupee', 'invoicing'), |
|
| 401 | + 'IQD' => __('Iraqi Dinar', 'invoicing'), |
|
| 402 | + 'IRR' => __('Iranian Rial', 'invoicing'), |
|
| 403 | + 'IRT' => __('Iranian Toman', 'invoicing'), |
|
| 404 | + 'ISK' => __('Icelandic Krona', 'invoicing'), |
|
| 405 | + 'JEP' => __('Jersey Pound', 'invoicing'), |
|
| 406 | + 'JMD' => __('Jamaican Dollar', 'invoicing'), |
|
| 407 | + 'JOD' => __('Jordanian Dinar', 'invoicing'), |
|
| 408 | + 'JPY' => __('Japanese Yen', 'invoicing'), |
|
| 409 | + 'KES' => __('Kenyan Shilling', 'invoicing'), |
|
| 410 | + 'KGS' => __('Kyrgyzstani Som', 'invoicing'), |
|
| 411 | + 'KHR' => __('Cambodian Riel', 'invoicing'), |
|
| 412 | + 'KMF' => __('Comorian Franc', 'invoicing'), |
|
| 413 | + 'KPW' => __('North Korean Won', 'invoicing'), |
|
| 414 | + 'KRW' => __('South Korean Won', 'invoicing'), |
|
| 415 | + 'KWD' => __('Kuwaiti Dinar', 'invoicing'), |
|
| 416 | + 'KYD' => __('Cayman Islands Dollar', 'invoicing'), |
|
| 417 | + 'KZT' => __('Kazakhstani Tenge', 'invoicing'), |
|
| 418 | + 'LAK' => __('Lao Kip', 'invoicing'), |
|
| 419 | + 'LBP' => __('Lebanese Pound', 'invoicing'), |
|
| 420 | + 'LKR' => __('Sri Lankan Rupee', 'invoicing'), |
|
| 421 | + 'LRD' => __('Liberian Dollar', 'invoicing'), |
|
| 422 | + 'LSL' => __('Lesotho Loti', 'invoicing'), |
|
| 423 | + 'LYD' => __('Libyan Dinar', 'invoicing'), |
|
| 424 | + 'MAD' => __('Moroccan Dirham', 'invoicing'), |
|
| 425 | + 'MDL' => __('Moldovan Leu', 'invoicing'), |
|
| 426 | + 'MGA' => __('Malagasy Ariary', 'invoicing'), |
|
| 427 | + 'MKD' => __('Macedonian Denar', 'invoicing'), |
|
| 428 | + 'MMK' => __('Burmese Kyat', 'invoicing'), |
|
| 429 | + 'MNT' => __('Mongolian Tughrik', 'invoicing'), |
|
| 430 | + 'MOP' => __('Macanese Pataca', 'invoicing'), |
|
| 431 | + 'MRO' => __('Mauritanian Ouguiya', 'invoicing'), |
|
| 432 | + 'MUR' => __('Mauritian Rupee', 'invoicing'), |
|
| 433 | + 'MVR' => __('Maldivian Rufiyaa', 'invoicing'), |
|
| 434 | + 'MWK' => __('Malawian Kwacha', 'invoicing'), |
|
| 435 | + 'MXN' => __('Mexican Peso', 'invoicing'), |
|
| 436 | + 'MYR' => __('Malaysian Ringgit', 'invoicing'), |
|
| 437 | + 'MZN' => __('Mozambican Metical', 'invoicing'), |
|
| 438 | + 'NAD' => __('Namibian Dollar', 'invoicing'), |
|
| 439 | + 'NGN' => __('Nigerian Naira', 'invoicing'), |
|
| 440 | + 'NIO' => __('Nicaraguan Cordoba', 'invoicing'), |
|
| 441 | + 'NOK' => __('Norwegian Krone', 'invoicing'), |
|
| 442 | + 'NPR' => __('Nepalese Rupee', 'invoicing'), |
|
| 443 | + 'NZD' => __('New Zealand Dollar', 'invoicing'), |
|
| 444 | + 'OMR' => __('Omani Rial', 'invoicing'), |
|
| 445 | + 'PAB' => __('Panamanian Balboa', 'invoicing'), |
|
| 446 | + 'PEN' => __('Peruvian Nuevo Sol', 'invoicing'), |
|
| 447 | + 'PGK' => __('Papua New Guinean Kina', 'invoicing'), |
|
| 448 | + 'PHP' => __('Philippine Peso', 'invoicing'), |
|
| 449 | + 'PKR' => __('Pakistani Rupee', 'invoicing'), |
|
| 450 | + 'PLN' => __('Polish Zloty', 'invoicing'), |
|
| 451 | + 'PRB' => __('Transnistrian Ruble', 'invoicing'), |
|
| 452 | + 'PYG' => __('Paraguayan Guarani', 'invoicing'), |
|
| 453 | + 'QAR' => __('Qatari Riyal', 'invoicing'), |
|
| 454 | + 'RON' => __('Romanian Leu', 'invoicing'), |
|
| 455 | + 'RSD' => __('Serbian Dinar', 'invoicing'), |
|
| 456 | + 'RUB' => __('Russian Ruble', 'invoicing'), |
|
| 457 | + 'RWF' => __('Rwandan Franc', 'invoicing'), |
|
| 458 | + 'SAR' => __('Saudi Riyal', 'invoicing'), |
|
| 459 | + 'SBD' => __('Solomon Islands Dollar', 'invoicing'), |
|
| 460 | + 'SCR' => __('Seychellois Rupee', 'invoicing'), |
|
| 461 | + 'SDG' => __('Sudanese Pound', 'invoicing'), |
|
| 462 | + 'SEK' => __('Swedish Krona', 'invoicing'), |
|
| 463 | + 'SGD' => __('Singapore Dollar', 'invoicing'), |
|
| 464 | + 'SHP' => __('Saint Helena Pound', 'invoicing'), |
|
| 465 | + 'SLL' => __('Sierra Leonean Leone', 'invoicing'), |
|
| 466 | + 'SOS' => __('Somali Shilling', 'invoicing'), |
|
| 467 | + 'SRD' => __('Surinamese Dollar', 'invoicing'), |
|
| 468 | + 'SSP' => __('South Sudanese Pound', 'invoicing'), |
|
| 469 | + 'STD' => __('Sao Tomean Dobra', 'invoicing'), |
|
| 470 | + 'SYP' => __('Syrian Pound', 'invoicing'), |
|
| 471 | + 'SZL' => __('Swazi Lilangeni', 'invoicing'), |
|
| 472 | + 'THB' => __('Thai Baht', 'invoicing'), |
|
| 473 | + 'TJS' => __('Tajikistani Somoni', 'invoicing'), |
|
| 474 | + 'TMT' => __('Turkmenistan Manat', 'invoicing'), |
|
| 475 | + 'TND' => __('Tunisian Dinar', 'invoicing'), |
|
| 476 | + 'TOP' => __('Tongan Paʻanga', 'invoicing'), |
|
| 477 | + 'TRY' => __('Turkish Lira', 'invoicing'), |
|
| 478 | + 'TTD' => __('Trinidad and Tobago Dollar', 'invoicing'), |
|
| 479 | + 'TWD' => __('New Taiwan Dollar', 'invoicing'), |
|
| 480 | + 'TZS' => __('Tanzanian Shilling', 'invoicing'), |
|
| 481 | + 'UAH' => __('Ukrainian Hryvnia', 'invoicing'), |
|
| 482 | + 'UGX' => __('Ugandan Shilling', 'invoicing'), |
|
| 483 | + 'UYU' => __('Uruguayan Peso', 'invoicing'), |
|
| 484 | + 'UZS' => __('Uzbekistani Som', 'invoicing'), |
|
| 485 | + 'VEF' => __('Venezuelan Bolívar', 'invoicing'), |
|
| 486 | + 'VND' => __('Vietnamese Dong', 'invoicing'), |
|
| 487 | + 'VUV' => __('Vanuatu Vatu', 'invoicing'), |
|
| 488 | + 'WST' => __('Samoan Tala', 'invoicing'), |
|
| 489 | + 'XAF' => __('Central African CFA Franc', 'invoicing'), |
|
| 490 | + 'XCD' => __('East Caribbean Dollar', 'invoicing'), |
|
| 491 | + 'XOF' => __('West African CFA Franc', 'invoicing'), |
|
| 492 | + 'XPF' => __('CFP Franc', 'invoicing'), |
|
| 493 | + 'YER' => __('Yemeni Rial', 'invoicing'), |
|
| 494 | + 'ZAR' => __('South African Rand', 'invoicing'), |
|
| 495 | + 'ZMW' => __('Zambian Kwacha', 'invoicing'), |
|
| 496 | 496 | ); |
| 497 | 497 | |
| 498 | 498 | //asort( $currencies ); // this |
| 499 | 499 | |
| 500 | - return apply_filters( 'wpinv_currencies', $currencies ); |
|
| 500 | + return apply_filters('wpinv_currencies', $currencies); |
|
| 501 | 501 | } |
| 502 | 502 | |
| 503 | -function wpinv_price( $amount = '', $currency = '' ) { |
|
| 504 | - if( empty( $currency ) ) { |
|
| 503 | +function wpinv_price($amount = '', $currency = '') { |
|
| 504 | + if (empty($currency)) { |
|
| 505 | 505 | $currency = wpinv_get_currency(); |
| 506 | 506 | } |
| 507 | 507 | |
@@ -509,14 +509,14 @@ discard block |
||
| 509 | 509 | |
| 510 | 510 | $negative = $amount < 0; |
| 511 | 511 | |
| 512 | - if ( $negative ) { |
|
| 513 | - $amount = substr( $amount, 1 ); |
|
| 512 | + if ($negative) { |
|
| 513 | + $amount = substr($amount, 1); |
|
| 514 | 514 | } |
| 515 | 515 | |
| 516 | - $symbol = wpinv_currency_symbol( $currency ); |
|
| 516 | + $symbol = wpinv_currency_symbol($currency); |
|
| 517 | 517 | |
| 518 | - if ( $position == 'left' || $position == 'left_space' ) { |
|
| 519 | - switch ( $currency ) { |
|
| 518 | + if ($position == 'left' || $position == 'left_space') { |
|
| 519 | + switch ($currency) { |
|
| 520 | 520 | case "GBP" : |
| 521 | 521 | case "BRL" : |
| 522 | 522 | case "EUR" : |
@@ -528,15 +528,15 @@ discard block |
||
| 528 | 528 | case "NZD" : |
| 529 | 529 | case "SGD" : |
| 530 | 530 | case "JPY" : |
| 531 | - $price = $position == 'left_space' ? $symbol . ' ' . $amount : $symbol . $amount; |
|
| 531 | + $price = $position == 'left_space' ? $symbol . ' ' . $amount : $symbol . $amount; |
|
| 532 | 532 | break; |
| 533 | 533 | default : |
| 534 | 534 | //$price = $currency . ' ' . $amount; |
| 535 | - $price = $position == 'left_space' ? $symbol . ' ' . $amount : $symbol . $amount; |
|
| 535 | + $price = $position == 'left_space' ? $symbol . ' ' . $amount : $symbol . $amount; |
|
| 536 | 536 | break; |
| 537 | 537 | } |
| 538 | 538 | } else { |
| 539 | - switch ( $currency ) { |
|
| 539 | + switch ($currency) { |
|
| 540 | 540 | case "GBP" : |
| 541 | 541 | case "BRL" : |
| 542 | 542 | case "EUR" : |
@@ -547,83 +547,83 @@ discard block |
||
| 547 | 547 | case "MXN" : |
| 548 | 548 | case "SGD" : |
| 549 | 549 | case "JPY" : |
| 550 | - $price = $position == 'right_space' ? $amount . ' ' . $symbol : $amount . $symbol; |
|
| 550 | + $price = $position == 'right_space' ? $amount . ' ' . $symbol : $amount . $symbol; |
|
| 551 | 551 | break; |
| 552 | 552 | default : |
| 553 | 553 | //$price = $amount . ' ' . $currency; |
| 554 | - $price = $position == 'right_space' ? $amount . ' ' . $symbol : $amount . $symbol; |
|
| 554 | + $price = $position == 'right_space' ? $amount . ' ' . $symbol : $amount . $symbol; |
|
| 555 | 555 | break; |
| 556 | 556 | } |
| 557 | 557 | } |
| 558 | 558 | |
| 559 | - if ( $negative ) { |
|
| 559 | + if ($negative) { |
|
| 560 | 560 | $price = '-' . $price; |
| 561 | 561 | } |
| 562 | 562 | |
| 563 | - $price = apply_filters( 'wpinv_' . strtolower( $currency ) . '_currency_filter_' . $position, $price, $currency, $amount ); |
|
| 563 | + $price = apply_filters('wpinv_' . strtolower($currency) . '_currency_filter_' . $position, $price, $currency, $amount); |
|
| 564 | 564 | |
| 565 | 565 | return $price; |
| 566 | 566 | } |
| 567 | 567 | |
| 568 | -function wpinv_format_amount( $amount, $decimals = NULL, $calculate = false ) { |
|
| 568 | +function wpinv_format_amount($amount, $decimals = NULL, $calculate = false) { |
|
| 569 | 569 | $thousands_sep = wpinv_thousands_separator(); |
| 570 | 570 | $decimal_sep = wpinv_decimal_separator(); |
| 571 | 571 | |
| 572 | - if ( $decimals === NULL ) { |
|
| 572 | + if ($decimals === NULL) { |
|
| 573 | 573 | $decimals = wpinv_decimals(); |
| 574 | 574 | } |
| 575 | 575 | |
| 576 | - if ( $decimal_sep == ',' && false !== ( $sep_found = strpos( $amount, $decimal_sep ) ) ) { |
|
| 577 | - $whole = substr( $amount, 0, $sep_found ); |
|
| 578 | - $part = substr( $amount, $sep_found + 1, ( strlen( $amount ) - 1 ) ); |
|
| 576 | + if ($decimal_sep == ',' && false !== ($sep_found = strpos($amount, $decimal_sep))) { |
|
| 577 | + $whole = substr($amount, 0, $sep_found); |
|
| 578 | + $part = substr($amount, $sep_found + 1, (strlen($amount) - 1)); |
|
| 579 | 579 | $amount = $whole . '.' . $part; |
| 580 | 580 | } |
| 581 | 581 | |
| 582 | - if ( $thousands_sep == ',' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) { |
|
| 583 | - $amount = str_replace( ',', '', $amount ); |
|
| 582 | + if ($thousands_sep == ',' && false !== ($found = strpos($amount, $thousands_sep))) { |
|
| 583 | + $amount = str_replace(',', '', $amount); |
|
| 584 | 584 | } |
| 585 | 585 | |
| 586 | - if ( $thousands_sep == ' ' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) { |
|
| 587 | - $amount = str_replace( ' ', '', $amount ); |
|
| 586 | + if ($thousands_sep == ' ' && false !== ($found = strpos($amount, $thousands_sep))) { |
|
| 587 | + $amount = str_replace(' ', '', $amount); |
|
| 588 | 588 | } |
| 589 | 589 | |
| 590 | - if ( empty( $amount ) ) { |
|
| 590 | + if (empty($amount)) { |
|
| 591 | 591 | $amount = 0; |
| 592 | 592 | } |
| 593 | 593 | |
| 594 | - $decimals = apply_filters( 'wpinv_amount_format_decimals', $decimals ? $decimals : 0, $amount, $calculate ); |
|
| 595 | - $formatted = number_format( (float)$amount, $decimals, $decimal_sep, $thousands_sep ); |
|
| 594 | + $decimals = apply_filters('wpinv_amount_format_decimals', $decimals ? $decimals : 0, $amount, $calculate); |
|
| 595 | + $formatted = number_format((float) $amount, $decimals, $decimal_sep, $thousands_sep); |
|
| 596 | 596 | |
| 597 | - if ( $calculate ) { |
|
| 598 | - if ( $thousands_sep === "," ) { |
|
| 599 | - $formatted = str_replace( ",", "", $formatted ); |
|
| 597 | + if ($calculate) { |
|
| 598 | + if ($thousands_sep === ",") { |
|
| 599 | + $formatted = str_replace(",", "", $formatted); |
|
| 600 | 600 | } |
| 601 | 601 | |
| 602 | - if ( $decimal_sep === "," ) { |
|
| 603 | - $formatted = str_replace( ",", ".", $formatted ); |
|
| 602 | + if ($decimal_sep === ",") { |
|
| 603 | + $formatted = str_replace(",", ".", $formatted); |
|
| 604 | 604 | } |
| 605 | 605 | } |
| 606 | 606 | |
| 607 | - return apply_filters( 'wpinv_amount_format', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep, $calculate ); |
|
| 607 | + return apply_filters('wpinv_amount_format', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep, $calculate); |
|
| 608 | 608 | } |
| 609 | -add_filter( 'wpinv_amount_format_decimals', 'wpinv_currency_decimal_filter', 10, 1 ); |
|
| 609 | +add_filter('wpinv_amount_format_decimals', 'wpinv_currency_decimal_filter', 10, 1); |
|
| 610 | 610 | |
| 611 | -function wpinv_sanitize_key( $key ) { |
|
| 611 | +function wpinv_sanitize_key($key) { |
|
| 612 | 612 | $raw_key = $key; |
| 613 | - $key = preg_replace( '/[^a-zA-Z0-9_\-\.\:\/]/', '', $key ); |
|
| 613 | + $key = preg_replace('/[^a-zA-Z0-9_\-\.\:\/]/', '', $key); |
|
| 614 | 614 | |
| 615 | - return apply_filters( 'wpinv_sanitize_key', $key, $raw_key ); |
|
| 615 | + return apply_filters('wpinv_sanitize_key', $key, $raw_key); |
|
| 616 | 616 | } |
| 617 | 617 | |
| 618 | -function wpinv_get_file_extension( $str ) { |
|
| 619 | - $parts = explode( '.', $str ); |
|
| 620 | - return end( $parts ); |
|
| 618 | +function wpinv_get_file_extension($str) { |
|
| 619 | + $parts = explode('.', $str); |
|
| 620 | + return end($parts); |
|
| 621 | 621 | } |
| 622 | 622 | |
| 623 | -function wpinv_string_is_image_url( $str ) { |
|
| 624 | - $ext = wpinv_get_file_extension( $str ); |
|
| 623 | +function wpinv_string_is_image_url($str) { |
|
| 624 | + $ext = wpinv_get_file_extension($str); |
|
| 625 | 625 | |
| 626 | - switch ( strtolower( $ext ) ) { |
|
| 626 | + switch (strtolower($ext)) { |
|
| 627 | 627 | case 'jpeg'; |
| 628 | 628 | case 'jpg'; |
| 629 | 629 | $return = true; |
@@ -639,33 +639,33 @@ discard block |
||
| 639 | 639 | break; |
| 640 | 640 | } |
| 641 | 641 | |
| 642 | - return (bool)apply_filters( 'wpinv_string_is_image', $return, $str ); |
|
| 642 | + return (bool) apply_filters('wpinv_string_is_image', $return, $str); |
|
| 643 | 643 | } |
| 644 | 644 | |
| 645 | -function wpinv_error_log( $log, $title = '', $file = '', $line = '', $exit = false ) { |
|
| 646 | - $should_log = apply_filters( 'wpinv_log_errors', WP_DEBUG ); |
|
| 645 | +function wpinv_error_log($log, $title = '', $file = '', $line = '', $exit = false) { |
|
| 646 | + $should_log = apply_filters('wpinv_log_errors', WP_DEBUG); |
|
| 647 | 647 | |
| 648 | - if ( true === $should_log ) { |
|
| 648 | + if (true === $should_log) { |
|
| 649 | 649 | $label = ''; |
| 650 | - if ( $file && $file !== '' ) { |
|
| 651 | - $label .= basename( $file ) . ( $line ? '(' . $line . ')' : '' ); |
|
| 650 | + if ($file && $file !== '') { |
|
| 651 | + $label .= basename($file) . ($line ? '(' . $line . ')' : ''); |
|
| 652 | 652 | } |
| 653 | 653 | |
| 654 | - if ( $title && $title !== '' ) { |
|
| 654 | + if ($title && $title !== '') { |
|
| 655 | 655 | $label = $label !== '' ? $label . ' ' : ''; |
| 656 | 656 | $label .= $title . ' '; |
| 657 | 657 | } |
| 658 | 658 | |
| 659 | - $label = $label !== '' ? trim( $label ) . ' : ' : ''; |
|
| 659 | + $label = $label !== '' ? trim($label) . ' : ' : ''; |
|
| 660 | 660 | |
| 661 | - if ( is_array( $log ) || is_object( $log ) ) { |
|
| 662 | - error_log( $label . print_r( $log, true ) ); |
|
| 661 | + if (is_array($log) || is_object($log)) { |
|
| 662 | + error_log($label . print_r($log, true)); |
|
| 663 | 663 | } else { |
| 664 | - error_log( $label . $log ); |
|
| 664 | + error_log($label . $log); |
|
| 665 | 665 | } |
| 666 | 666 | |
| 667 | - error_log( wp_debug_backtrace_summary() ); |
|
| 668 | - if ( $exit ) { |
|
| 667 | + error_log(wp_debug_backtrace_summary()); |
|
| 668 | + if ($exit) { |
|
| 669 | 669 | exit; |
| 670 | 670 | } |
| 671 | 671 | } |
@@ -673,32 +673,32 @@ discard block |
||
| 673 | 673 | |
| 674 | 674 | function wpinv_is_ajax_disabled() { |
| 675 | 675 | $retval = false; |
| 676 | - return apply_filters( 'wpinv_is_ajax_disabled', $retval ); |
|
| 676 | + return apply_filters('wpinv_is_ajax_disabled', $retval); |
|
| 677 | 677 | } |
| 678 | 678 | |
| 679 | -function wpinv_get_current_page_url( $nocache = false ) { |
|
| 679 | +function wpinv_get_current_page_url($nocache = false) { |
|
| 680 | 680 | global $wp; |
| 681 | 681 | |
| 682 | - if ( get_option( 'permalink_structure' ) ) { |
|
| 683 | - $base = trailingslashit( home_url( $wp->request ) ); |
|
| 682 | + if (get_option('permalink_structure')) { |
|
| 683 | + $base = trailingslashit(home_url($wp->request)); |
|
| 684 | 684 | } else { |
| 685 | - $base = add_query_arg( $wp->query_string, '', trailingslashit( home_url( $wp->request ) ) ); |
|
| 686 | - $base = remove_query_arg( array( 'post_type', 'name' ), $base ); |
|
| 685 | + $base = add_query_arg($wp->query_string, '', trailingslashit(home_url($wp->request))); |
|
| 686 | + $base = remove_query_arg(array('post_type', 'name'), $base); |
|
| 687 | 687 | } |
| 688 | 688 | |
| 689 | 689 | $scheme = is_ssl() ? 'https' : 'http'; |
| 690 | - $uri = set_url_scheme( $base, $scheme ); |
|
| 690 | + $uri = set_url_scheme($base, $scheme); |
|
| 691 | 691 | |
| 692 | - if ( is_front_page() ) { |
|
| 693 | - $uri = home_url( '/' ); |
|
| 694 | - } elseif ( wpinv_is_checkout( array(), false ) ) { |
|
| 692 | + if (is_front_page()) { |
|
| 693 | + $uri = home_url('/'); |
|
| 694 | + } elseif (wpinv_is_checkout(array(), false)) { |
|
| 695 | 695 | $uri = wpinv_get_checkout_uri(); |
| 696 | 696 | } |
| 697 | 697 | |
| 698 | - $uri = apply_filters( 'wpinv_get_current_page_url', $uri ); |
|
| 698 | + $uri = apply_filters('wpinv_get_current_page_url', $uri); |
|
| 699 | 699 | |
| 700 | - if ( $nocache ) { |
|
| 701 | - $uri = wpinv_add_cache_busting( $uri ); |
|
| 700 | + if ($nocache) { |
|
| 701 | + $uri = wpinv_add_cache_busting($uri); |
|
| 702 | 702 | } |
| 703 | 703 | |
| 704 | 704 | return $uri; |
@@ -711,46 +711,46 @@ discard block |
||
| 711 | 711 | * @param string $name Constant name. |
| 712 | 712 | * @param mixed $value Value. |
| 713 | 713 | */ |
| 714 | -function getpaid_maybe_define_constant( $name, $value ) { |
|
| 715 | - if ( ! defined( $name ) ) { |
|
| 716 | - define( $name, $value ); |
|
| 714 | +function getpaid_maybe_define_constant($name, $value) { |
|
| 715 | + if (!defined($name)) { |
|
| 716 | + define($name, $value); |
|
| 717 | 717 | } |
| 718 | 718 | } |
| 719 | 719 | |
| 720 | 720 | function wpinv_get_php_arg_separator_output() { |
| 721 | - return ini_get( 'arg_separator.output' ); |
|
| 721 | + return ini_get('arg_separator.output'); |
|
| 722 | 722 | } |
| 723 | 723 | |
| 724 | -function wpinv_rgb_from_hex( $color ) { |
|
| 725 | - $color = str_replace( '#', '', $color ); |
|
| 724 | +function wpinv_rgb_from_hex($color) { |
|
| 725 | + $color = str_replace('#', '', $color); |
|
| 726 | 726 | |
| 727 | 727 | // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF" |
| 728 | - $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color ); |
|
| 729 | - if ( empty( $color ) ) { |
|
| 728 | + $color = preg_replace('~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color); |
|
| 729 | + if (empty($color)) { |
|
| 730 | 730 | return NULL; |
| 731 | 731 | } |
| 732 | 732 | |
| 733 | - $color = str_split( $color ); |
|
| 733 | + $color = str_split($color); |
|
| 734 | 734 | |
| 735 | 735 | $rgb = array(); |
| 736 | - $rgb['R'] = hexdec( $color[0] . $color[1] ); |
|
| 737 | - $rgb['G'] = hexdec( $color[2] . $color[3] ); |
|
| 738 | - $rgb['B'] = hexdec( $color[4] . $color[5] ); |
|
| 736 | + $rgb['R'] = hexdec($color[0] . $color[1]); |
|
| 737 | + $rgb['G'] = hexdec($color[2] . $color[3]); |
|
| 738 | + $rgb['B'] = hexdec($color[4] . $color[5]); |
|
| 739 | 739 | |
| 740 | 740 | return $rgb; |
| 741 | 741 | } |
| 742 | 742 | |
| 743 | -function wpinv_hex_darker( $color, $factor = 30 ) { |
|
| 744 | - $base = wpinv_rgb_from_hex( $color ); |
|
| 743 | +function wpinv_hex_darker($color, $factor = 30) { |
|
| 744 | + $base = wpinv_rgb_from_hex($color); |
|
| 745 | 745 | $color = '#'; |
| 746 | 746 | |
| 747 | - foreach ( $base as $k => $v ) { |
|
| 747 | + foreach ($base as $k => $v) { |
|
| 748 | 748 | $amount = $v / 100; |
| 749 | - $amount = round( $amount * $factor ); |
|
| 749 | + $amount = round($amount * $factor); |
|
| 750 | 750 | $new_decimal = $v - $amount; |
| 751 | 751 | |
| 752 | - $new_hex_component = dechex( $new_decimal ); |
|
| 753 | - if ( strlen( $new_hex_component ) < 2 ) { |
|
| 752 | + $new_hex_component = dechex($new_decimal); |
|
| 753 | + if (strlen($new_hex_component) < 2) { |
|
| 754 | 754 | $new_hex_component = "0" . $new_hex_component; |
| 755 | 755 | } |
| 756 | 756 | $color .= $new_hex_component; |
@@ -759,18 +759,18 @@ discard block |
||
| 759 | 759 | return $color; |
| 760 | 760 | } |
| 761 | 761 | |
| 762 | -function wpinv_hex_lighter( $color, $factor = 30 ) { |
|
| 763 | - $base = wpinv_rgb_from_hex( $color ); |
|
| 762 | +function wpinv_hex_lighter($color, $factor = 30) { |
|
| 763 | + $base = wpinv_rgb_from_hex($color); |
|
| 764 | 764 | $color = '#'; |
| 765 | 765 | |
| 766 | - foreach ( $base as $k => $v ) { |
|
| 766 | + foreach ($base as $k => $v) { |
|
| 767 | 767 | $amount = 255 - $v; |
| 768 | 768 | $amount = $amount / 100; |
| 769 | - $amount = round( $amount * $factor ); |
|
| 769 | + $amount = round($amount * $factor); |
|
| 770 | 770 | $new_decimal = $v + $amount; |
| 771 | 771 | |
| 772 | - $new_hex_component = dechex( $new_decimal ); |
|
| 773 | - if ( strlen( $new_hex_component ) < 2 ) { |
|
| 772 | + $new_hex_component = dechex($new_decimal); |
|
| 773 | + if (strlen($new_hex_component) < 2) { |
|
| 774 | 774 | $new_hex_component = "0" . $new_hex_component; |
| 775 | 775 | } |
| 776 | 776 | $color .= $new_hex_component; |
@@ -779,22 +779,22 @@ discard block |
||
| 779 | 779 | return $color; |
| 780 | 780 | } |
| 781 | 781 | |
| 782 | -function wpinv_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) { |
|
| 783 | - $hex = str_replace( '#', '', $color ); |
|
| 782 | +function wpinv_light_or_dark($color, $dark = '#000000', $light = '#FFFFFF') { |
|
| 783 | + $hex = str_replace('#', '', $color); |
|
| 784 | 784 | |
| 785 | - $c_r = hexdec( substr( $hex, 0, 2 ) ); |
|
| 786 | - $c_g = hexdec( substr( $hex, 2, 2 ) ); |
|
| 787 | - $c_b = hexdec( substr( $hex, 4, 2 ) ); |
|
| 785 | + $c_r = hexdec(substr($hex, 0, 2)); |
|
| 786 | + $c_g = hexdec(substr($hex, 2, 2)); |
|
| 787 | + $c_b = hexdec(substr($hex, 4, 2)); |
|
| 788 | 788 | |
| 789 | - $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000; |
|
| 789 | + $brightness = (($c_r * 299) + ($c_g * 587) + ($c_b * 114)) / 1000; |
|
| 790 | 790 | |
| 791 | 791 | return $brightness > 155 ? $dark : $light; |
| 792 | 792 | } |
| 793 | 793 | |
| 794 | -function wpinv_format_hex( $hex ) { |
|
| 795 | - $hex = trim( str_replace( '#', '', $hex ) ); |
|
| 794 | +function wpinv_format_hex($hex) { |
|
| 795 | + $hex = trim(str_replace('#', '', $hex)); |
|
| 796 | 796 | |
| 797 | - if ( strlen( $hex ) == 3 ) { |
|
| 797 | + if (strlen($hex) == 3) { |
|
| 798 | 798 | $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; |
| 799 | 799 | } |
| 800 | 800 | |
@@ -814,12 +814,12 @@ discard block |
||
| 814 | 814 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 815 | 815 | * @return string |
| 816 | 816 | */ |
| 817 | -function wpinv_utf8_strimwidth( $str, $start, $width, $trimmaker = '', $encoding = 'UTF-8' ) { |
|
| 818 | - if ( function_exists( 'mb_strimwidth' ) ) { |
|
| 819 | - return mb_strimwidth( $str, $start, $width, $trimmaker, $encoding ); |
|
| 817 | +function wpinv_utf8_strimwidth($str, $start, $width, $trimmaker = '', $encoding = 'UTF-8') { |
|
| 818 | + if (function_exists('mb_strimwidth')) { |
|
| 819 | + return mb_strimwidth($str, $start, $width, $trimmaker, $encoding); |
|
| 820 | 820 | } |
| 821 | 821 | |
| 822 | - return wpinv_utf8_substr( $str, $start, $width, $encoding ) . $trimmaker; |
|
| 822 | + return wpinv_utf8_substr($str, $start, $width, $encoding) . $trimmaker; |
|
| 823 | 823 | } |
| 824 | 824 | |
| 825 | 825 | /** |
@@ -831,28 +831,28 @@ discard block |
||
| 831 | 831 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 832 | 832 | * @return int Returns the number of characters in string. |
| 833 | 833 | */ |
| 834 | -function wpinv_utf8_strlen( $str, $encoding = 'UTF-8' ) { |
|
| 835 | - if ( function_exists( 'mb_strlen' ) ) { |
|
| 836 | - return mb_strlen( $str, $encoding ); |
|
| 834 | +function wpinv_utf8_strlen($str, $encoding = 'UTF-8') { |
|
| 835 | + if (function_exists('mb_strlen')) { |
|
| 836 | + return mb_strlen($str, $encoding); |
|
| 837 | 837 | } |
| 838 | 838 | |
| 839 | - return strlen( $str ); |
|
| 839 | + return strlen($str); |
|
| 840 | 840 | } |
| 841 | 841 | |
| 842 | -function wpinv_utf8_strtolower( $str, $encoding = 'UTF-8' ) { |
|
| 843 | - if ( function_exists( 'mb_strtolower' ) ) { |
|
| 844 | - return mb_strtolower( $str, $encoding ); |
|
| 842 | +function wpinv_utf8_strtolower($str, $encoding = 'UTF-8') { |
|
| 843 | + if (function_exists('mb_strtolower')) { |
|
| 844 | + return mb_strtolower($str, $encoding); |
|
| 845 | 845 | } |
| 846 | 846 | |
| 847 | - return strtolower( $str ); |
|
| 847 | + return strtolower($str); |
|
| 848 | 848 | } |
| 849 | 849 | |
| 850 | -function wpinv_utf8_strtoupper( $str, $encoding = 'UTF-8' ) { |
|
| 851 | - if ( function_exists( 'mb_strtoupper' ) ) { |
|
| 852 | - return mb_strtoupper( $str, $encoding ); |
|
| 850 | +function wpinv_utf8_strtoupper($str, $encoding = 'UTF-8') { |
|
| 851 | + if (function_exists('mb_strtoupper')) { |
|
| 852 | + return mb_strtoupper($str, $encoding); |
|
| 853 | 853 | } |
| 854 | 854 | |
| 855 | - return strtoupper( $str ); |
|
| 855 | + return strtoupper($str); |
|
| 856 | 856 | } |
| 857 | 857 | |
| 858 | 858 | /** |
@@ -866,12 +866,12 @@ discard block |
||
| 866 | 866 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 867 | 867 | * @return int Returns the position of the first occurrence of search in the string. |
| 868 | 868 | */ |
| 869 | -function wpinv_utf8_strpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) { |
|
| 870 | - if ( function_exists( 'mb_strpos' ) ) { |
|
| 871 | - return mb_strpos( $str, $find, $offset, $encoding ); |
|
| 869 | +function wpinv_utf8_strpos($str, $find, $offset = 0, $encoding = 'UTF-8') { |
|
| 870 | + if (function_exists('mb_strpos')) { |
|
| 871 | + return mb_strpos($str, $find, $offset, $encoding); |
|
| 872 | 872 | } |
| 873 | 873 | |
| 874 | - return strpos( $str, $find, $offset ); |
|
| 874 | + return strpos($str, $find, $offset); |
|
| 875 | 875 | } |
| 876 | 876 | |
| 877 | 877 | /** |
@@ -885,12 +885,12 @@ discard block |
||
| 885 | 885 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 886 | 886 | * @return int Returns the position of the last occurrence of search. |
| 887 | 887 | */ |
| 888 | -function wpinv_utf8_strrpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) { |
|
| 889 | - if ( function_exists( 'mb_strrpos' ) ) { |
|
| 890 | - return mb_strrpos( $str, $find, $offset, $encoding ); |
|
| 888 | +function wpinv_utf8_strrpos($str, $find, $offset = 0, $encoding = 'UTF-8') { |
|
| 889 | + if (function_exists('mb_strrpos')) { |
|
| 890 | + return mb_strrpos($str, $find, $offset, $encoding); |
|
| 891 | 891 | } |
| 892 | 892 | |
| 893 | - return strrpos( $str, $find, $offset ); |
|
| 893 | + return strrpos($str, $find, $offset); |
|
| 894 | 894 | } |
| 895 | 895 | |
| 896 | 896 | /** |
@@ -905,16 +905,16 @@ discard block |
||
| 905 | 905 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 906 | 906 | * @return string |
| 907 | 907 | */ |
| 908 | -function wpinv_utf8_substr( $str, $start, $length = null, $encoding = 'UTF-8' ) { |
|
| 909 | - if ( function_exists( 'mb_substr' ) ) { |
|
| 910 | - if ( $length === null ) { |
|
| 911 | - return mb_substr( $str, $start, wpinv_utf8_strlen( $str, $encoding ), $encoding ); |
|
| 908 | +function wpinv_utf8_substr($str, $start, $length = null, $encoding = 'UTF-8') { |
|
| 909 | + if (function_exists('mb_substr')) { |
|
| 910 | + if ($length === null) { |
|
| 911 | + return mb_substr($str, $start, wpinv_utf8_strlen($str, $encoding), $encoding); |
|
| 912 | 912 | } else { |
| 913 | - return mb_substr( $str, $start, $length, $encoding ); |
|
| 913 | + return mb_substr($str, $start, $length, $encoding); |
|
| 914 | 914 | } |
| 915 | 915 | } |
| 916 | 916 | |
| 917 | - return substr( $str, $start, $length ); |
|
| 917 | + return substr($str, $start, $length); |
|
| 918 | 918 | } |
| 919 | 919 | |
| 920 | 920 | /** |
@@ -926,48 +926,48 @@ discard block |
||
| 926 | 926 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
| 927 | 927 | * @return string The width of string. |
| 928 | 928 | */ |
| 929 | -function wpinv_utf8_strwidth( $str, $encoding = 'UTF-8' ) { |
|
| 930 | - if ( function_exists( 'mb_strwidth' ) ) { |
|
| 931 | - return mb_strwidth( $str, $encoding ); |
|
| 929 | +function wpinv_utf8_strwidth($str, $encoding = 'UTF-8') { |
|
| 930 | + if (function_exists('mb_strwidth')) { |
|
| 931 | + return mb_strwidth($str, $encoding); |
|
| 932 | 932 | } |
| 933 | 933 | |
| 934 | - return wpinv_utf8_strlen( $str, $encoding ); |
|
| 934 | + return wpinv_utf8_strlen($str, $encoding); |
|
| 935 | 935 | } |
| 936 | 936 | |
| 937 | -function wpinv_utf8_ucfirst( $str, $lower_str_end = false, $encoding = 'UTF-8' ) { |
|
| 938 | - if ( function_exists( 'mb_strlen' ) ) { |
|
| 939 | - $first_letter = wpinv_utf8_strtoupper( wpinv_utf8_substr( $str, 0, 1, $encoding ), $encoding ); |
|
| 937 | +function wpinv_utf8_ucfirst($str, $lower_str_end = false, $encoding = 'UTF-8') { |
|
| 938 | + if (function_exists('mb_strlen')) { |
|
| 939 | + $first_letter = wpinv_utf8_strtoupper(wpinv_utf8_substr($str, 0, 1, $encoding), $encoding); |
|
| 940 | 940 | $str_end = ""; |
| 941 | 941 | |
| 942 | - if ( $lower_str_end ) { |
|
| 943 | - $str_end = wpinv_utf8_strtolower( wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding ), $encoding ); |
|
| 942 | + if ($lower_str_end) { |
|
| 943 | + $str_end = wpinv_utf8_strtolower(wpinv_utf8_substr($str, 1, wpinv_utf8_strlen($str, $encoding), $encoding), $encoding); |
|
| 944 | 944 | } else { |
| 945 | - $str_end = wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding ); |
|
| 945 | + $str_end = wpinv_utf8_substr($str, 1, wpinv_utf8_strlen($str, $encoding), $encoding); |
|
| 946 | 946 | } |
| 947 | 947 | |
| 948 | 948 | return $first_letter . $str_end; |
| 949 | 949 | } |
| 950 | 950 | |
| 951 | - return ucfirst( $str ); |
|
| 951 | + return ucfirst($str); |
|
| 952 | 952 | } |
| 953 | 953 | |
| 954 | -function wpinv_utf8_ucwords( $str, $encoding = 'UTF-8' ) { |
|
| 955 | - if ( function_exists( 'mb_convert_case' ) ) { |
|
| 956 | - return mb_convert_case( $str, MB_CASE_TITLE, $encoding ); |
|
| 954 | +function wpinv_utf8_ucwords($str, $encoding = 'UTF-8') { |
|
| 955 | + if (function_exists('mb_convert_case')) { |
|
| 956 | + return mb_convert_case($str, MB_CASE_TITLE, $encoding); |
|
| 957 | 957 | } |
| 958 | 958 | |
| 959 | - return ucwords( $str ); |
|
| 959 | + return ucwords($str); |
|
| 960 | 960 | } |
| 961 | 961 | |
| 962 | -function wpinv_period_in_days( $period, $unit ) { |
|
| 963 | - $period = absint( $period ); |
|
| 962 | +function wpinv_period_in_days($period, $unit) { |
|
| 963 | + $period = absint($period); |
|
| 964 | 964 | |
| 965 | - if ( $period > 0 ) { |
|
| 966 | - if ( in_array( strtolower( $unit ), array( 'w', 'week', 'weeks' ) ) ) { |
|
| 965 | + if ($period > 0) { |
|
| 966 | + if (in_array(strtolower($unit), array('w', 'week', 'weeks'))) { |
|
| 967 | 967 | $period = $period * 7; |
| 968 | - } else if ( in_array( strtolower( $unit ), array( 'm', 'month', 'months' ) ) ) { |
|
| 968 | + } else if (in_array(strtolower($unit), array('m', 'month', 'months'))) { |
|
| 969 | 969 | $period = $period * 30; |
| 970 | - } else if ( in_array( strtolower( $unit ), array( 'y', 'year', 'years' ) ) ) { |
|
| 970 | + } else if (in_array(strtolower($unit), array('y', 'year', 'years'))) { |
|
| 971 | 971 | $period = $period * 365; |
| 972 | 972 | } |
| 973 | 973 | } |
@@ -975,14 +975,14 @@ discard block |
||
| 975 | 975 | return $period; |
| 976 | 976 | } |
| 977 | 977 | |
| 978 | -function wpinv_cal_days_in_month( $calendar, $month, $year ) { |
|
| 979 | - if ( function_exists( 'cal_days_in_month' ) ) { |
|
| 980 | - return cal_days_in_month( $calendar, $month, $year ); |
|
| 978 | +function wpinv_cal_days_in_month($calendar, $month, $year) { |
|
| 979 | + if (function_exists('cal_days_in_month')) { |
|
| 980 | + return cal_days_in_month($calendar, $month, $year); |
|
| 981 | 981 | } |
| 982 | 982 | |
| 983 | 983 | // Fallback in case the calendar extension is not loaded in PHP |
| 984 | 984 | // Only supports Gregorian calendar |
| 985 | - return date( 't', mktime( 0, 0, 0, $month, 1, $year ) ); |
|
| 985 | + return date('t', mktime(0, 0, 0, $month, 1, $year)); |
|
| 986 | 986 | } |
| 987 | 987 | |
| 988 | 988 | /** |
@@ -993,11 +993,11 @@ discard block |
||
| 993 | 993 | * |
| 994 | 994 | * @return string |
| 995 | 995 | */ |
| 996 | -function wpi_help_tip( $tip, $allow_html = false ) { |
|
| 997 | - if ( $allow_html ) { |
|
| 998 | - $tip = wpi_sanitize_tooltip( $tip ); |
|
| 996 | +function wpi_help_tip($tip, $allow_html = false) { |
|
| 997 | + if ($allow_html) { |
|
| 998 | + $tip = wpi_sanitize_tooltip($tip); |
|
| 999 | 999 | } else { |
| 1000 | - $tip = esc_attr( $tip ); |
|
| 1000 | + $tip = esc_attr($tip); |
|
| 1001 | 1001 | } |
| 1002 | 1002 | |
| 1003 | 1003 | return '<span class="wpi-help-tip dashicons dashicons-editor-help" title="' . $tip . '"></span>'; |
@@ -1011,8 +1011,8 @@ discard block |
||
| 1011 | 1011 | * @param string $var |
| 1012 | 1012 | * @return string |
| 1013 | 1013 | */ |
| 1014 | -function wpi_sanitize_tooltip( $var ) { |
|
| 1015 | - return htmlspecialchars( wp_kses( html_entity_decode( $var ), array( |
|
| 1014 | +function wpi_sanitize_tooltip($var) { |
|
| 1015 | + return htmlspecialchars(wp_kses(html_entity_decode($var), array( |
|
| 1016 | 1016 | 'br' => array(), |
| 1017 | 1017 | 'em' => array(), |
| 1018 | 1018 | 'strong' => array(), |
@@ -1022,7 +1022,7 @@ discard block |
||
| 1022 | 1022 | 'li' => array(), |
| 1023 | 1023 | 'ol' => array(), |
| 1024 | 1024 | 'p' => array(), |
| 1025 | - ) ) ); |
|
| 1025 | + ))); |
|
| 1026 | 1026 | } |
| 1027 | 1027 | |
| 1028 | 1028 | /** |
@@ -1032,7 +1032,7 @@ discard block |
||
| 1032 | 1032 | */ |
| 1033 | 1033 | function wpinv_get_screen_ids() { |
| 1034 | 1034 | |
| 1035 | - $screen_id = sanitize_title( __( 'Invoicing', 'invoicing' ) ); |
|
| 1035 | + $screen_id = sanitize_title(__('Invoicing', 'invoicing')); |
|
| 1036 | 1036 | |
| 1037 | 1037 | $screen_ids = array( |
| 1038 | 1038 | 'toplevel_page_' . $screen_id, |
@@ -1050,7 +1050,7 @@ discard block |
||
| 1050 | 1050 | 'invoicing_page_wpi-addons', |
| 1051 | 1051 | ); |
| 1052 | 1052 | |
| 1053 | - return apply_filters( 'wpinv_screen_ids', $screen_ids ); |
|
| 1053 | + return apply_filters('wpinv_screen_ids', $screen_ids); |
|
| 1054 | 1054 | } |
| 1055 | 1055 | |
| 1056 | 1056 | /** |
@@ -1061,14 +1061,14 @@ discard block |
||
| 1061 | 1061 | * @param array|string $list List of values. |
| 1062 | 1062 | * @return array Sanitized array of values. |
| 1063 | 1063 | */ |
| 1064 | -function wpinv_parse_list( $list ) { |
|
| 1064 | +function wpinv_parse_list($list) { |
|
| 1065 | 1065 | |
| 1066 | - if ( empty( $list ) ) { |
|
| 1066 | + if (empty($list)) { |
|
| 1067 | 1067 | $list = array(); |
| 1068 | 1068 | } |
| 1069 | 1069 | |
| 1070 | - if ( ! is_array( $list ) ) { |
|
| 1071 | - return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 1070 | + if (!is_array($list)) { |
|
| 1071 | + return preg_split('/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY); |
|
| 1072 | 1072 | } |
| 1073 | 1073 | |
| 1074 | 1074 | return $list; |
@@ -1082,16 +1082,16 @@ discard block |
||
| 1082 | 1082 | * @param string $key Type of data to fetch. |
| 1083 | 1083 | * @return mixed Fetched data. |
| 1084 | 1084 | */ |
| 1085 | -function wpinv_get_data( $key ) { |
|
| 1085 | +function wpinv_get_data($key) { |
|
| 1086 | 1086 | |
| 1087 | 1087 | // Try fetching it from the cache. |
| 1088 | - $data = wp_cache_get( "wpinv-$key", 'wpinv' ); |
|
| 1089 | - if( $data ) { |
|
| 1088 | + $data = wp_cache_get("wpinv-$key", 'wpinv'); |
|
| 1089 | + if ($data) { |
|
| 1090 | 1090 | return $data; |
| 1091 | 1091 | } |
| 1092 | 1092 | |
| 1093 | - $data = apply_filters( "wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php" ); |
|
| 1094 | - wp_cache_set( "wpinv-$key", $data, 'wpinv' ); |
|
| 1093 | + $data = apply_filters("wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php"); |
|
| 1094 | + wp_cache_set("wpinv-$key", $data, 'wpinv'); |
|
| 1095 | 1095 | |
| 1096 | 1096 | return $data; |
| 1097 | 1097 | } |
@@ -1105,10 +1105,10 @@ discard block |
||
| 1105 | 1105 | * @param bool $first_empty Whether or not the first item in the list should be empty |
| 1106 | 1106 | * @return mixed Fetched data. |
| 1107 | 1107 | */ |
| 1108 | -function wpinv_maybe_add_empty_option( $options, $first_empty ) { |
|
| 1108 | +function wpinv_maybe_add_empty_option($options, $first_empty) { |
|
| 1109 | 1109 | |
| 1110 | - if ( ! empty( $options ) && $first_empty ) { |
|
| 1111 | - return array_merge( array( '' => '' ), $options ); |
|
| 1110 | + if (!empty($options) && $first_empty) { |
|
| 1111 | + return array_merge(array('' => ''), $options); |
|
| 1112 | 1112 | } |
| 1113 | 1113 | return $options; |
| 1114 | 1114 | |
@@ -1120,21 +1120,21 @@ discard block |
||
| 1120 | 1120 | * @param mixed $var Data to sanitize. |
| 1121 | 1121 | * @return string|array |
| 1122 | 1122 | */ |
| 1123 | -function wpinv_clean( $var ) { |
|
| 1123 | +function wpinv_clean($var) { |
|
| 1124 | 1124 | |
| 1125 | - if ( is_array( $var ) ) { |
|
| 1126 | - return array_map( 'wpinv_clean', $var ); |
|
| 1125 | + if (is_array($var)) { |
|
| 1126 | + return array_map('wpinv_clean', $var); |
|
| 1127 | 1127 | } |
| 1128 | 1128 | |
| 1129 | - if ( is_object( $var ) ) { |
|
| 1130 | - $object_vars = get_object_vars( $var ); |
|
| 1131 | - foreach ( $object_vars as $property_name => $property_value ) { |
|
| 1132 | - $var->$property_name = wpinv_clean( $property_value ); |
|
| 1129 | + if (is_object($var)) { |
|
| 1130 | + $object_vars = get_object_vars($var); |
|
| 1131 | + foreach ($object_vars as $property_name => $property_value) { |
|
| 1132 | + $var->$property_name = wpinv_clean($property_value); |
|
| 1133 | 1133 | } |
| 1134 | 1134 | return $var; |
| 1135 | 1135 | } |
| 1136 | 1136 | |
| 1137 | - return is_string( $var ) ? sanitize_text_field( $var ) : $var; |
|
| 1137 | + return is_string($var) ? sanitize_text_field($var) : $var; |
|
| 1138 | 1138 | } |
| 1139 | 1139 | |
| 1140 | 1140 | /** |
@@ -1143,43 +1143,43 @@ discard block |
||
| 1143 | 1143 | * @param string $str Data to convert. |
| 1144 | 1144 | * @return string|array |
| 1145 | 1145 | */ |
| 1146 | -function getpaid_convert_price_string_to_options( $str ) { |
|
| 1146 | +function getpaid_convert_price_string_to_options($str) { |
|
| 1147 | 1147 | |
| 1148 | - $raw_options = array_map( 'trim', explode( ',', $str ) ); |
|
| 1149 | - $options = array(); |
|
| 1148 | + $raw_options = array_map('trim', explode(',', $str)); |
|
| 1149 | + $options = array(); |
|
| 1150 | 1150 | |
| 1151 | - foreach ( $raw_options as $option ) { |
|
| 1151 | + foreach ($raw_options as $option) { |
|
| 1152 | 1152 | |
| 1153 | - if ( '' == $option ) { |
|
| 1153 | + if ('' == $option) { |
|
| 1154 | 1154 | continue; |
| 1155 | 1155 | } |
| 1156 | 1156 | |
| 1157 | - $option = array_map( 'trim', explode( '|', $option ) ); |
|
| 1157 | + $option = array_map('trim', explode('|', $option)); |
|
| 1158 | 1158 | |
| 1159 | 1159 | $price = null; |
| 1160 | 1160 | $label = null; |
| 1161 | 1161 | |
| 1162 | - if ( isset( $option[0] ) && '' != $option[0] ) { |
|
| 1163 | - $label = $option[0]; |
|
| 1162 | + if (isset($option[0]) && '' != $option[0]) { |
|
| 1163 | + $label = $option[0]; |
|
| 1164 | 1164 | } |
| 1165 | 1165 | |
| 1166 | - if ( isset( $option[1] ) && '' != $option[1] ) { |
|
| 1166 | + if (isset($option[1]) && '' != $option[1]) { |
|
| 1167 | 1167 | $price = $option[1]; |
| 1168 | 1168 | } |
| 1169 | 1169 | |
| 1170 | - if ( ! isset( $price ) ) { |
|
| 1170 | + if (!isset($price)) { |
|
| 1171 | 1171 | $price = $label; |
| 1172 | 1172 | } |
| 1173 | 1173 | |
| 1174 | - if ( ! isset( $price ) || ! is_numeric( $price ) ) { |
|
| 1174 | + if (!isset($price) || !is_numeric($price)) { |
|
| 1175 | 1175 | continue; |
| 1176 | 1176 | } |
| 1177 | 1177 | |
| 1178 | - if ( ! isset( $label ) ) { |
|
| 1178 | + if (!isset($label)) { |
|
| 1179 | 1179 | $label = $price; |
| 1180 | 1180 | } |
| 1181 | 1181 | |
| 1182 | - $options[ $price ] = $label; |
|
| 1182 | + $options[$price] = $label; |
|
| 1183 | 1183 | } |
| 1184 | 1184 | |
| 1185 | 1185 | return $options; |
@@ -1188,22 +1188,22 @@ discard block |
||
| 1188 | 1188 | /** |
| 1189 | 1189 | * Returns the help tip. |
| 1190 | 1190 | */ |
| 1191 | -function getpaid_get_help_tip( $tip, $additional_classes = '' ) { |
|
| 1192 | - $additional_classes = sanitize_html_class( $additional_classes ); |
|
| 1193 | - $tip = esc_attr__( $tip ); |
|
| 1191 | +function getpaid_get_help_tip($tip, $additional_classes = '') { |
|
| 1192 | + $additional_classes = sanitize_html_class($additional_classes); |
|
| 1193 | + $tip = esc_attr__($tip); |
|
| 1194 | 1194 | return "<span class='wpi-help-tip dashicons dashicons-editor-help $additional_classes' title='$tip'></span>"; |
| 1195 | 1195 | } |
| 1196 | 1196 | |
| 1197 | 1197 | /** |
| 1198 | 1198 | * Formats a date |
| 1199 | 1199 | */ |
| 1200 | -function getpaid_format_date( $date ) { |
|
| 1200 | +function getpaid_format_date($date) { |
|
| 1201 | 1201 | |
| 1202 | - if ( empty( $date ) || $date == '0000-00-00 00:00:00' ) { |
|
| 1202 | + if (empty($date) || $date == '0000-00-00 00:00:00') { |
|
| 1203 | 1203 | return ''; |
| 1204 | 1204 | } |
| 1205 | 1205 | |
| 1206 | 1206 | |
| 1207 | - return date_i18n( get_option( 'date_format' ), strtotime( $date ) ); |
|
| 1207 | + return date_i18n(get_option('date_format'), strtotime($date)); |
|
| 1208 | 1208 | |
| 1209 | 1209 | } |
@@ -20,92 +20,92 @@ discard block |
||
| 20 | 20 | class WPInv_REST_Discounts_Controller extends WP_REST_Posts_Controller { |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * Post type. |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - protected $post_type = 'wpi_discount'; |
|
| 23 | + * Post type. |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + protected $post_type = 'wpi_discount'; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Cached results of get_item_schema. |
|
| 31 | - * |
|
| 32 | - * @since 1.0.13 |
|
| 33 | - * @var array |
|
| 34 | - */ |
|
| 35 | - protected $schema; |
|
| 29 | + /** |
|
| 30 | + * Cached results of get_item_schema. |
|
| 31 | + * |
|
| 32 | + * @since 1.0.13 |
|
| 33 | + * @var array |
|
| 34 | + */ |
|
| 35 | + protected $schema; |
|
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | - * Constructor. |
|
| 39 | - * |
|
| 40 | - * @since 1.0.13 |
|
| 41 | - * |
|
| 42 | - * @param string $namespace Api Namespace |
|
| 43 | - */ |
|
| 44 | - public function __construct( $namespace ) { |
|
| 38 | + * Constructor. |
|
| 39 | + * |
|
| 40 | + * @since 1.0.13 |
|
| 41 | + * |
|
| 42 | + * @param string $namespace Api Namespace |
|
| 43 | + */ |
|
| 44 | + public function __construct( $namespace ) { |
|
| 45 | 45 | |
| 46 | 46 | // Set api namespace... |
| 47 | - $this->namespace = $namespace; |
|
| 47 | + $this->namespace = $namespace; |
|
| 48 | 48 | |
| 49 | 49 | // ... and the rest base |
| 50 | 50 | $this->rest_base = 'discounts'; |
| 51 | 51 | |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Registers the routes for the objects of the controller. |
|
| 56 | - * |
|
| 57 | - * @since 1.0.13 |
|
| 58 | - * |
|
| 59 | - * @see register_rest_route() |
|
| 60 | - */ |
|
| 61 | - public function register_routes() { |
|
| 62 | - |
|
| 63 | - parent::register_routes(); |
|
| 64 | - |
|
| 65 | - register_rest_route( |
|
| 66 | - $this->namespace, |
|
| 67 | - '/' . $this->rest_base . '/discount-types', |
|
| 68 | - array( |
|
| 69 | - array( |
|
| 70 | - 'methods' => WP_REST_Server::READABLE, |
|
| 71 | - 'callback' => array( $this, 'get_discount_types' ), |
|
| 72 | - 'permission_callback' => '__return_true', |
|
| 73 | - ), |
|
| 74 | - ) |
|
| 75 | - ); |
|
| 76 | - |
|
| 77 | - } |
|
| 54 | + /** |
|
| 55 | + * Registers the routes for the objects of the controller. |
|
| 56 | + * |
|
| 57 | + * @since 1.0.13 |
|
| 58 | + * |
|
| 59 | + * @see register_rest_route() |
|
| 60 | + */ |
|
| 61 | + public function register_routes() { |
|
| 62 | + |
|
| 63 | + parent::register_routes(); |
|
| 64 | + |
|
| 65 | + register_rest_route( |
|
| 66 | + $this->namespace, |
|
| 67 | + '/' . $this->rest_base . '/discount-types', |
|
| 68 | + array( |
|
| 69 | + array( |
|
| 70 | + 'methods' => WP_REST_Server::READABLE, |
|
| 71 | + 'callback' => array( $this, 'get_discount_types' ), |
|
| 72 | + 'permission_callback' => '__return_true', |
|
| 73 | + ), |
|
| 74 | + ) |
|
| 75 | + ); |
|
| 76 | + |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | 79 | /** |
| 80 | - * Checks if a given request has access to read discounts. |
|
| 80 | + * Checks if a given request has access to read discounts. |
|
| 81 | 81 | * |
| 82 | - * |
|
| 83 | - * @since 1.0.13 |
|
| 84 | - * |
|
| 85 | - * @param WP_REST_Request $request Full details about the request. |
|
| 86 | - * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
| 87 | - */ |
|
| 88 | - public function get_items_permissions_check( $request ) { |
|
| 82 | + * |
|
| 83 | + * @since 1.0.13 |
|
| 84 | + * |
|
| 85 | + * @param WP_REST_Request $request Full details about the request. |
|
| 86 | + * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
| 87 | + */ |
|
| 88 | + public function get_items_permissions_check( $request ) { |
|
| 89 | 89 | |
| 90 | - if ( wpinv_current_user_can_manage_invoicing() ) { |
|
| 91 | - return true; |
|
| 92 | - } |
|
| 90 | + if ( wpinv_current_user_can_manage_invoicing() ) { |
|
| 91 | + return true; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view invoice discounts.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 94 | + return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view invoice discounts.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 95 | 95 | |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
| 99 | - * Retrieves a collection of discounts. |
|
| 100 | - * |
|
| 101 | - * @since 1.0.13 |
|
| 102 | - * |
|
| 103 | - * @param WP_REST_Request $request Full details about the request. |
|
| 104 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 105 | - */ |
|
| 106 | - public function get_items( $request ) { |
|
| 99 | + * Retrieves a collection of discounts. |
|
| 100 | + * |
|
| 101 | + * @since 1.0.13 |
|
| 102 | + * |
|
| 103 | + * @param WP_REST_Request $request Full details about the request. |
|
| 104 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 105 | + */ |
|
| 106 | + public function get_items( $request ) { |
|
| 107 | 107 | |
| 108 | - // Retrieve the list of registered item query parameters. |
|
| 108 | + // Retrieve the list of registered item query parameters. |
|
| 109 | 109 | $registered = $this->get_collection_params(); |
| 110 | 110 | |
| 111 | 111 | $args = array(); |
@@ -116,52 +116,52 @@ discard block |
||
| 116 | 116 | $args[ $key ] = $request[ $key]; |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Filters the wpinv_get_all_discounts arguments for discounts rest requests. |
|
| 123 | - * |
|
| 124 | - * |
|
| 125 | - * @since 1.0.13 |
|
| 126 | - * |
|
| 127 | - * |
|
| 128 | - * @param array $args Key value array of query var to query value. |
|
| 129 | - * @param WP_REST_Request $request The request used. |
|
| 130 | - */ |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Filters the wpinv_get_all_discounts arguments for discounts rest requests. |
|
| 123 | + * |
|
| 124 | + * |
|
| 125 | + * @since 1.0.13 |
|
| 126 | + * |
|
| 127 | + * |
|
| 128 | + * @param array $args Key value array of query var to query value. |
|
| 129 | + * @param WP_REST_Request $request The request used. |
|
| 130 | + */ |
|
| 131 | 131 | $args = apply_filters( "wpinv_rest_get_discounts_arguments", $args, $request, $this ); |
| 132 | 132 | |
| 133 | - // Special args |
|
| 134 | - $args[ 'return' ] = 'objects'; |
|
| 135 | - $args[ 'paginate' ] = true; |
|
| 133 | + // Special args |
|
| 134 | + $args[ 'return' ] = 'objects'; |
|
| 135 | + $args[ 'paginate' ] = true; |
|
| 136 | 136 | |
| 137 | 137 | // Run the query. |
| 138 | - $query = wpinv_get_all_discounts( $args ); |
|
| 138 | + $query = wpinv_get_all_discounts( $args ); |
|
| 139 | 139 | |
| 140 | - // Prepare the retrieved discounts |
|
| 141 | - $discounts = array(); |
|
| 142 | - foreach( $query->discounts as $discount ) { |
|
| 143 | - |
|
| 144 | - $data = $this->prepare_item_for_response( $discount, $request ); |
|
| 145 | - $discounts[] = $this->prepare_response_for_collection( $data ); |
|
| 146 | - |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - // Prepare the response. |
|
| 150 | - $response = rest_ensure_response( $discounts ); |
|
| 151 | - $response->header( 'X-WP-Total', (int) $query->total ); |
|
| 152 | - $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages ); |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Filters the responses for discount requests. |
|
| 156 | - * |
|
| 157 | - * |
|
| 158 | - * @since 1.0.13 |
|
| 159 | - * |
|
| 160 | - * |
|
| 161 | - * @param arrWP_REST_Response $response Response object. |
|
| 162 | - * @param WP_REST_Request $request The request used. |
|
| 140 | + // Prepare the retrieved discounts |
|
| 141 | + $discounts = array(); |
|
| 142 | + foreach( $query->discounts as $discount ) { |
|
| 143 | + |
|
| 144 | + $data = $this->prepare_item_for_response( $discount, $request ); |
|
| 145 | + $discounts[] = $this->prepare_response_for_collection( $data ); |
|
| 146 | + |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + // Prepare the response. |
|
| 150 | + $response = rest_ensure_response( $discounts ); |
|
| 151 | + $response->header( 'X-WP-Total', (int) $query->total ); |
|
| 152 | + $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages ); |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Filters the responses for discount requests. |
|
| 156 | + * |
|
| 157 | + * |
|
| 158 | + * @since 1.0.13 |
|
| 159 | + * |
|
| 160 | + * |
|
| 161 | + * @param arrWP_REST_Response $response Response object. |
|
| 162 | + * @param WP_REST_Request $request The request used. |
|
| 163 | 163 | * @param array $args Array of args used to retrieve the discounts |
| 164 | - */ |
|
| 164 | + */ |
|
| 165 | 165 | $response = apply_filters( "wpinv_rest_discounts_response", $response, $request, $args ); |
| 166 | 166 | |
| 167 | 167 | return rest_ensure_response( $response ); |
@@ -169,25 +169,25 @@ discard block |
||
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | /** |
| 172 | - * Get the post, if the ID is valid. |
|
| 173 | - * |
|
| 174 | - * @since 1.0.13 |
|
| 175 | - * |
|
| 176 | - * @param int $discount_id Supplied ID. |
|
| 177 | - * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. |
|
| 178 | - */ |
|
| 179 | - protected function get_post( $discount_id ) { |
|
| 172 | + * Get the post, if the ID is valid. |
|
| 173 | + * |
|
| 174 | + * @since 1.0.13 |
|
| 175 | + * |
|
| 176 | + * @param int $discount_id Supplied ID. |
|
| 177 | + * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. |
|
| 178 | + */ |
|
| 179 | + protected function get_post( $discount_id ) { |
|
| 180 | 180 | |
| 181 | - $error = new WP_Error( 'rest_item_invalid_id', __( 'Invalid discount ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 181 | + $error = new WP_Error( 'rest_item_invalid_id', __( 'Invalid discount ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 182 | 182 | |
| 183 | 183 | // Ids start from 1 |
| 184 | 184 | if ( (int) $discount_id <= 0 ) { |
| 185 | - return $error; |
|
| 186 | - } |
|
| 185 | + return $error; |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - $discount = wpinv_get_discount( (int) $discount_id ); |
|
| 189 | - if ( empty( $discount ) ) { |
|
| 190 | - return $error; |
|
| 188 | + $discount = wpinv_get_discount( (int) $discount_id ); |
|
| 189 | + if ( empty( $discount ) ) { |
|
| 190 | + return $error; |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | return $discount; |
@@ -195,25 +195,25 @@ discard block |
||
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | /** |
| 198 | - * Checks if a given request has access to read a discount. |
|
| 199 | - * |
|
| 200 | - * @since 1.0.13 |
|
| 201 | - * |
|
| 202 | - * @param WP_REST_Request $request Full details about the request. |
|
| 203 | - * @return bool|WP_Error True if the request has read access for the invoice item, WP_Error object otherwise. |
|
| 204 | - */ |
|
| 205 | - public function get_item_permissions_check( $request ) { |
|
| 198 | + * Checks if a given request has access to read a discount. |
|
| 199 | + * |
|
| 200 | + * @since 1.0.13 |
|
| 201 | + * |
|
| 202 | + * @param WP_REST_Request $request Full details about the request. |
|
| 203 | + * @return bool|WP_Error True if the request has read access for the invoice item, WP_Error object otherwise. |
|
| 204 | + */ |
|
| 205 | + public function get_item_permissions_check( $request ) { |
|
| 206 | 206 | |
| 207 | 207 | // Retrieve the discount object. |
| 208 | 208 | $discount = $this->get_post( $request['id'] ); |
| 209 | 209 | |
| 210 | 210 | // Ensure it is valid. |
| 211 | - if ( is_wp_error( $discount ) ) { |
|
| 212 | - return $discount; |
|
| 213 | - } |
|
| 211 | + if ( is_wp_error( $discount ) ) { |
|
| 212 | + return $discount; |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 216 | - return new WP_Error( |
|
| 215 | + if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 216 | + return new WP_Error( |
|
| 217 | 217 | 'rest_cannot_view', |
| 218 | 218 | __( 'Sorry, you are not allowed to view this discount.', 'invoicing' ), |
| 219 | 219 | array( |
@@ -222,40 +222,40 @@ discard block |
||
| 222 | 222 | ); |
| 223 | 223 | } |
| 224 | 224 | |
| 225 | - return true; |
|
| 225 | + return true; |
|
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | /** |
| 229 | - * Retrieves a single invoice item. |
|
| 230 | - * |
|
| 231 | - * @since 1.0.13 |
|
| 232 | - * |
|
| 233 | - * @param WP_REST_Request $request Full details about the request. |
|
| 234 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 235 | - */ |
|
| 236 | - public function get_item( $request ) { |
|
| 229 | + * Retrieves a single invoice item. |
|
| 230 | + * |
|
| 231 | + * @since 1.0.13 |
|
| 232 | + * |
|
| 233 | + * @param WP_REST_Request $request Full details about the request. |
|
| 234 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 235 | + */ |
|
| 236 | + public function get_item( $request ) { |
|
| 237 | 237 | |
| 238 | 238 | // Fetch the discount. |
| 239 | 239 | $discount = $this->get_post( $request['id'] ); |
| 240 | 240 | |
| 241 | 241 | // Abort early if it does not exist |
| 242 | - if ( is_wp_error( $discount ) ) { |
|
| 243 | - return $discount; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - // Prepare the response |
|
| 247 | - $response = $this->prepare_item_for_response( $discount, $request ); |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Filters the responses for single discount requests. |
|
| 251 | - * |
|
| 252 | - * |
|
| 253 | - * @since 1.0.13 |
|
| 254 | - * @var WP_HTTP_Response |
|
| 255 | - * |
|
| 256 | - * @param WP_HTTP_Response $response Response. |
|
| 257 | - * @param WP_REST_Request $request The request used. |
|
| 258 | - */ |
|
| 242 | + if ( is_wp_error( $discount ) ) { |
|
| 243 | + return $discount; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + // Prepare the response |
|
| 247 | + $response = $this->prepare_item_for_response( $discount, $request ); |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Filters the responses for single discount requests. |
|
| 251 | + * |
|
| 252 | + * |
|
| 253 | + * @since 1.0.13 |
|
| 254 | + * @var WP_HTTP_Response |
|
| 255 | + * |
|
| 256 | + * @param WP_HTTP_Response $response Response. |
|
| 257 | + * @param WP_REST_Request $request The request used. |
|
| 258 | + */ |
|
| 259 | 259 | $response = apply_filters( "wpinv_rest_get_discount_response", $response, $request ); |
| 260 | 260 | |
| 261 | 261 | return rest_ensure_response( $response ); |
@@ -263,26 +263,26 @@ discard block |
||
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | /** |
| 266 | - * Checks if a given request has access to create an invoice item. |
|
| 267 | - * |
|
| 268 | - * @since 1.0.13 |
|
| 269 | - * |
|
| 270 | - * @param WP_REST_Request $request Full details about the request. |
|
| 271 | - * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
|
| 272 | - */ |
|
| 273 | - public function create_item_permissions_check( $request ) { |
|
| 266 | + * Checks if a given request has access to create an invoice item. |
|
| 267 | + * |
|
| 268 | + * @since 1.0.13 |
|
| 269 | + * |
|
| 270 | + * @param WP_REST_Request $request Full details about the request. |
|
| 271 | + * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
|
| 272 | + */ |
|
| 273 | + public function create_item_permissions_check( $request ) { |
|
| 274 | 274 | |
| 275 | - if ( ! empty( $request['id'] ) ) { |
|
| 276 | - return new WP_Error( 'rest_item_exists', __( 'Cannot create existing item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 277 | - } |
|
| 275 | + if ( ! empty( $request['id'] ) ) { |
|
| 276 | + return new WP_Error( 'rest_item_exists', __( 'Cannot create existing item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | - if ( wpinv_current_user_can_manage_invoicing() ) { |
|
| 280 | - return true; |
|
| 281 | - } |
|
| 279 | + if ( wpinv_current_user_can_manage_invoicing() ) { |
|
| 280 | + return true; |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - $post_type = get_post_type_object( $this->post_type ); |
|
| 284 | - if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
| 285 | - return new WP_Error( |
|
| 283 | + $post_type = get_post_type_object( $this->post_type ); |
|
| 284 | + if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
| 285 | + return new WP_Error( |
|
| 286 | 286 | 'rest_cannot_create', |
| 287 | 287 | __( 'Sorry, you are not allowed to create discounts as this user.', 'invoicing' ), |
| 288 | 288 | array( |
@@ -291,261 +291,261 @@ discard block |
||
| 291 | 291 | ); |
| 292 | 292 | } |
| 293 | 293 | |
| 294 | - return true; |
|
| 294 | + return true; |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | /** |
| 298 | - * Creates a single discount. |
|
| 299 | - * |
|
| 300 | - * @since 1.0.13 |
|
| 301 | - * |
|
| 302 | - * @param WP_REST_Request $request Full details about the request. |
|
| 303 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 304 | - */ |
|
| 305 | - public function create_item( $request ) { |
|
| 306 | - |
|
| 307 | - if ( ! empty( $request['id'] ) ) { |
|
| 308 | - return new WP_Error( 'rest_item_exists', __( 'Cannot create existing discount.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 309 | - } |
|
| 298 | + * Creates a single discount. |
|
| 299 | + * |
|
| 300 | + * @since 1.0.13 |
|
| 301 | + * |
|
| 302 | + * @param WP_REST_Request $request Full details about the request. |
|
| 303 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 304 | + */ |
|
| 305 | + public function create_item( $request ) { |
|
| 306 | + |
|
| 307 | + if ( ! empty( $request['id'] ) ) { |
|
| 308 | + return new WP_Error( 'rest_item_exists', __( 'Cannot create existing discount.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 309 | + } |
|
| 310 | 310 | |
| 311 | - $request->set_param( 'context', 'edit' ); |
|
| 311 | + $request->set_param( 'context', 'edit' ); |
|
| 312 | 312 | |
| 313 | - // Prepare the updated data. |
|
| 314 | - $discount_data = $this->prepare_item_for_database( $request ); |
|
| 313 | + // Prepare the updated data. |
|
| 314 | + $discount_data = $this->prepare_item_for_database( $request ); |
|
| 315 | 315 | |
| 316 | - if ( is_wp_error( $discount_data ) ) { |
|
| 317 | - return $discount_data; |
|
| 318 | - } |
|
| 316 | + if ( is_wp_error( $discount_data ) ) { |
|
| 317 | + return $discount_data; |
|
| 318 | + } |
|
| 319 | 319 | |
| 320 | - $discount_data['post_type'] = $this->post_type; |
|
| 320 | + $discount_data['post_type'] = $this->post_type; |
|
| 321 | 321 | |
| 322 | - // Try creating the discount. |
|
| 322 | + // Try creating the discount. |
|
| 323 | 323 | $discount = wp_insert_post( $discount_data, true ); |
| 324 | 324 | |
| 325 | - if ( is_wp_error( $discount ) ) { |
|
| 325 | + if ( is_wp_error( $discount ) ) { |
|
| 326 | 326 | return $discount; |
| 327 | - } |
|
| 328 | - |
|
| 329 | - // Prepare the response |
|
| 330 | - $response = $this->prepare_item_for_response( $discount, $request ); |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Fires after a single discount is created or updated via the REST API. |
|
| 334 | - * |
|
| 335 | - * @since 1.0.13 |
|
| 336 | - * |
|
| 337 | - * @param WP_Post $discount Inserted or updated discount object. |
|
| 338 | - * @param WP_REST_Request $request Request object. |
|
| 339 | - * @param bool $creating True when creating a post, false when updating. |
|
| 340 | - */ |
|
| 341 | - do_action( "wpinv_rest_insert_discount", $discount, $request, true ); |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Filters the responses for creating single item requests. |
|
| 345 | - * |
|
| 346 | - * |
|
| 347 | - * @since 1.0.13 |
|
| 348 | - * |
|
| 349 | - * |
|
| 350 | - * @param array $response Invoice properties. |
|
| 351 | - * @param WP_REST_Request $request The request used. |
|
| 352 | - */ |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + // Prepare the response |
|
| 330 | + $response = $this->prepare_item_for_response( $discount, $request ); |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Fires after a single discount is created or updated via the REST API. |
|
| 334 | + * |
|
| 335 | + * @since 1.0.13 |
|
| 336 | + * |
|
| 337 | + * @param WP_Post $discount Inserted or updated discount object. |
|
| 338 | + * @param WP_REST_Request $request Request object. |
|
| 339 | + * @param bool $creating True when creating a post, false when updating. |
|
| 340 | + */ |
|
| 341 | + do_action( "wpinv_rest_insert_discount", $discount, $request, true ); |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Filters the responses for creating single item requests. |
|
| 345 | + * |
|
| 346 | + * |
|
| 347 | + * @since 1.0.13 |
|
| 348 | + * |
|
| 349 | + * |
|
| 350 | + * @param array $response Invoice properties. |
|
| 351 | + * @param WP_REST_Request $request The request used. |
|
| 352 | + */ |
|
| 353 | 353 | $response = apply_filters( "wpinv_rest_create_discount_response", $response, $request ); |
| 354 | 354 | |
| 355 | 355 | return rest_ensure_response( $response ); |
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Checks if a given request has access to update a discount. |
|
| 360 | - * |
|
| 361 | - * @since 1.0.13 |
|
| 362 | - * |
|
| 363 | - * @param WP_REST_Request $request Full details about the request. |
|
| 364 | - * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
|
| 365 | - */ |
|
| 366 | - public function update_item_permissions_check( $request ) { |
|
| 367 | - |
|
| 368 | - // Retrieve the item. |
|
| 369 | - $item = $this->get_post( $request['id'] ); |
|
| 370 | - if ( is_wp_error( $item ) ) { |
|
| 371 | - return $item; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - if ( wpinv_current_user_can_manage_invoicing() ) { |
|
| 375 | - return true; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - return new WP_Error( |
|
| 379 | - 'rest_cannot_edit', |
|
| 380 | - __( 'Sorry, you are not allowed to update this discount.', 'invoicing' ), |
|
| 381 | - array( |
|
| 382 | - 'status' => rest_authorization_required_code(), |
|
| 383 | - ) |
|
| 384 | - ); |
|
| 385 | - |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Updates a single discount. |
|
| 390 | - * |
|
| 391 | - * @since 1.0.13 |
|
| 392 | - * |
|
| 393 | - * @param WP_REST_Request $request Full details about the request. |
|
| 394 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 395 | - */ |
|
| 396 | - public function update_item( $request ) { |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Checks if a given request has access to update a discount. |
|
| 360 | + * |
|
| 361 | + * @since 1.0.13 |
|
| 362 | + * |
|
| 363 | + * @param WP_REST_Request $request Full details about the request. |
|
| 364 | + * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
|
| 365 | + */ |
|
| 366 | + public function update_item_permissions_check( $request ) { |
|
| 367 | + |
|
| 368 | + // Retrieve the item. |
|
| 369 | + $item = $this->get_post( $request['id'] ); |
|
| 370 | + if ( is_wp_error( $item ) ) { |
|
| 371 | + return $item; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + if ( wpinv_current_user_can_manage_invoicing() ) { |
|
| 375 | + return true; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + return new WP_Error( |
|
| 379 | + 'rest_cannot_edit', |
|
| 380 | + __( 'Sorry, you are not allowed to update this discount.', 'invoicing' ), |
|
| 381 | + array( |
|
| 382 | + 'status' => rest_authorization_required_code(), |
|
| 383 | + ) |
|
| 384 | + ); |
|
| 385 | + |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Updates a single discount. |
|
| 390 | + * |
|
| 391 | + * @since 1.0.13 |
|
| 392 | + * |
|
| 393 | + * @param WP_REST_Request $request Full details about the request. |
|
| 394 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 395 | + */ |
|
| 396 | + public function update_item( $request ) { |
|
| 397 | 397 | |
| 398 | - // Ensure the item exists. |
|
| 398 | + // Ensure the item exists. |
|
| 399 | 399 | $valid_check = $this->get_post( $request['id'] ); |
| 400 | 400 | |
| 401 | 401 | // Abort early if it does not exist |
| 402 | - if ( is_wp_error( $valid_check ) ) { |
|
| 403 | - return $valid_check; |
|
| 404 | - } |
|
| 402 | + if ( is_wp_error( $valid_check ) ) { |
|
| 403 | + return $valid_check; |
|
| 404 | + } |
|
| 405 | 405 | |
| 406 | - $request->set_param( 'context', 'edit' ); |
|
| 406 | + $request->set_param( 'context', 'edit' ); |
|
| 407 | 407 | |
| 408 | - // Prepare the updated data. |
|
| 409 | - $data_to_update = $this->prepare_item_for_database( $request ); |
|
| 408 | + // Prepare the updated data. |
|
| 409 | + $data_to_update = $this->prepare_item_for_database( $request ); |
|
| 410 | 410 | |
| 411 | - if ( is_wp_error( $data_to_update ) ) { |
|
| 412 | - return $data_to_update; |
|
| 413 | - } |
|
| 411 | + if ( is_wp_error( $data_to_update ) ) { |
|
| 412 | + return $data_to_update; |
|
| 413 | + } |
|
| 414 | 414 | |
| 415 | - if( empty( $data_to_update['meta_input'] ) ) { |
|
| 416 | - unset( $data_to_update['meta_input'] ); |
|
| 417 | - } |
|
| 415 | + if( empty( $data_to_update['meta_input'] ) ) { |
|
| 416 | + unset( $data_to_update['meta_input'] ); |
|
| 417 | + } |
|
| 418 | 418 | |
| 419 | - // Abort if no item data is provided |
|
| 419 | + // Abort if no item data is provided |
|
| 420 | 420 | if( empty( $data_to_update ) ) { |
| 421 | 421 | return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) ); |
| 422 | - } |
|
| 422 | + } |
|
| 423 | 423 | |
| 424 | - // post_status |
|
| 425 | - if( ! empty( $data_to_update['post_status'] ) ) { |
|
| 426 | - wpinv_update_discount_status( $request['id'], $data_to_update['post_status'] ); |
|
| 427 | - unset( $data_to_update['post_status'] ); |
|
| 428 | - } |
|
| 424 | + // post_status |
|
| 425 | + if( ! empty( $data_to_update['post_status'] ) ) { |
|
| 426 | + wpinv_update_discount_status( $request['id'], $data_to_update['post_status'] ); |
|
| 427 | + unset( $data_to_update['post_status'] ); |
|
| 428 | + } |
|
| 429 | 429 | |
| 430 | - // Update the item |
|
| 431 | - if( ! empty( $data_to_update ) ) { |
|
| 430 | + // Update the item |
|
| 431 | + if( ! empty( $data_to_update ) ) { |
|
| 432 | 432 | |
| 433 | - // Include the item ID |
|
| 434 | - $data_to_update['ID'] = $request['id']; |
|
| 433 | + // Include the item ID |
|
| 434 | + $data_to_update['ID'] = $request['id']; |
|
| 435 | 435 | |
| 436 | - $updated = wp_update_post( $data_to_update, true ); |
|
| 436 | + $updated = wp_update_post( $data_to_update, true ); |
|
| 437 | 437 | |
| 438 | - // Incase the update operation failed... |
|
| 439 | - if ( is_wp_error( $updated ) ) { |
|
| 440 | - return $updated; |
|
| 441 | - } |
|
| 438 | + // Incase the update operation failed... |
|
| 439 | + if ( is_wp_error( $updated ) ) { |
|
| 440 | + return $updated; |
|
| 441 | + } |
|
| 442 | 442 | |
| 443 | - } |
|
| 443 | + } |
|
| 444 | 444 | |
| 445 | - $updated_discount = get_post( $request['id'] ); |
|
| 445 | + $updated_discount = get_post( $request['id'] ); |
|
| 446 | 446 | |
| 447 | - // Prepare the response |
|
| 448 | - $response = $this->prepare_item_for_response( $updated_discount, $request ); |
|
| 447 | + // Prepare the response |
|
| 448 | + $response = $this->prepare_item_for_response( $updated_discount, $request ); |
|
| 449 | 449 | |
| 450 | - /** This action is documented in includes/class-wpinv-rest-item-controller.php */ |
|
| 451 | - do_action( "wpinv_rest_insert_discount", $updated_discount, $request, false ); |
|
| 450 | + /** This action is documented in includes/class-wpinv-rest-item-controller.php */ |
|
| 451 | + do_action( "wpinv_rest_insert_discount", $updated_discount, $request, false ); |
|
| 452 | 452 | |
| 453 | - /** |
|
| 454 | - * Filters the responses for updating single discount requests. |
|
| 455 | - * |
|
| 456 | - * |
|
| 457 | - * @since 1.0.13 |
|
| 458 | - * |
|
| 459 | - * |
|
| 460 | - * @param array $data_to_update Discount properties. |
|
| 461 | - * @param WP_REST_Request $request The request used. |
|
| 462 | - */ |
|
| 453 | + /** |
|
| 454 | + * Filters the responses for updating single discount requests. |
|
| 455 | + * |
|
| 456 | + * |
|
| 457 | + * @since 1.0.13 |
|
| 458 | + * |
|
| 459 | + * |
|
| 460 | + * @param array $data_to_update Discount properties. |
|
| 461 | + * @param WP_REST_Request $request The request used. |
|
| 462 | + */ |
|
| 463 | 463 | $response = apply_filters( "wpinv_rest_update_discount_response", $response, $data_to_update, $request ); |
| 464 | 464 | |
| 465 | 465 | return rest_ensure_response( $response ); |
| 466 | - } |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * Checks if a given request has access to delete a discount. |
|
| 470 | - * |
|
| 471 | - * @since 1.0.13 |
|
| 472 | - * |
|
| 473 | - * @param WP_REST_Request $request Full details about the request. |
|
| 474 | - * @return true|WP_Error True if the request has access to delete the discount, WP_Error object otherwise. |
|
| 475 | - */ |
|
| 476 | - public function delete_item_permissions_check( $request ) { |
|
| 477 | - |
|
| 478 | - // Retrieve the discount. |
|
| 479 | - $discount = $this->get_post( $request['id'] ); |
|
| 480 | - if ( is_wp_error( $discount ) ) { |
|
| 481 | - return $discount; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - // Ensure the current user can delete the discount |
|
| 485 | - if (! wpinv_current_user_can_manage_invoicing() ) { |
|
| 486 | - return new WP_Error( |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + /** |
|
| 469 | + * Checks if a given request has access to delete a discount. |
|
| 470 | + * |
|
| 471 | + * @since 1.0.13 |
|
| 472 | + * |
|
| 473 | + * @param WP_REST_Request $request Full details about the request. |
|
| 474 | + * @return true|WP_Error True if the request has access to delete the discount, WP_Error object otherwise. |
|
| 475 | + */ |
|
| 476 | + public function delete_item_permissions_check( $request ) { |
|
| 477 | + |
|
| 478 | + // Retrieve the discount. |
|
| 479 | + $discount = $this->get_post( $request['id'] ); |
|
| 480 | + if ( is_wp_error( $discount ) ) { |
|
| 481 | + return $discount; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + // Ensure the current user can delete the discount |
|
| 485 | + if (! wpinv_current_user_can_manage_invoicing() ) { |
|
| 486 | + return new WP_Error( |
|
| 487 | 487 | 'rest_cannot_delete', |
| 488 | 488 | __( 'Sorry, you are not allowed to delete this discount.', 'invoicing' ), |
| 489 | 489 | array( |
| 490 | 490 | 'status' => rest_authorization_required_code(), |
| 491 | 491 | ) |
| 492 | 492 | ); |
| 493 | - } |
|
| 494 | - |
|
| 495 | - return true; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - /** |
|
| 499 | - * Deletes a single discount. |
|
| 500 | - * |
|
| 501 | - * @since 1.0.13 |
|
| 502 | - * |
|
| 503 | - * @param WP_REST_Request $request Full details about the request. |
|
| 504 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 505 | - */ |
|
| 506 | - public function delete_item( $request ) { |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + return true; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + /** |
|
| 499 | + * Deletes a single discount. |
|
| 500 | + * |
|
| 501 | + * @since 1.0.13 |
|
| 502 | + * |
|
| 503 | + * @param WP_REST_Request $request Full details about the request. |
|
| 504 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 505 | + */ |
|
| 506 | + public function delete_item( $request ) { |
|
| 507 | 507 | |
| 508 | - // Retrieve the discount. |
|
| 509 | - $discount = $this->get_post( $request['id'] ); |
|
| 510 | - if ( is_wp_error( $discount ) ) { |
|
| 511 | - return $discount; |
|
| 512 | - } |
|
| 508 | + // Retrieve the discount. |
|
| 509 | + $discount = $this->get_post( $request['id'] ); |
|
| 510 | + if ( is_wp_error( $discount ) ) { |
|
| 511 | + return $discount; |
|
| 512 | + } |
|
| 513 | 513 | |
| 514 | - $request->set_param( 'context', 'edit' ); |
|
| 514 | + $request->set_param( 'context', 'edit' ); |
|
| 515 | 515 | |
| 516 | - // Prepare the discount id |
|
| 517 | - $id = $discount->ID; |
|
| 516 | + // Prepare the discount id |
|
| 517 | + $id = $discount->ID; |
|
| 518 | 518 | |
| 519 | - // Prepare the response |
|
| 520 | - $response = $this->prepare_item_for_response( $discount, $request ); |
|
| 519 | + // Prepare the response |
|
| 520 | + $response = $this->prepare_item_for_response( $discount, $request ); |
|
| 521 | 521 | |
| 522 | - // Delete the discount... |
|
| 523 | - wpinv_remove_discount( $id ); |
|
| 522 | + // Delete the discount... |
|
| 523 | + wpinv_remove_discount( $id ); |
|
| 524 | 524 | |
| 525 | - /** |
|
| 526 | - * Fires immediately after a single discount is deleted via the REST API. |
|
| 527 | - * |
|
| 528 | - * |
|
| 529 | - * @since 1.0.13 |
|
| 530 | - * |
|
| 531 | - * @param WP_POST $discount The deleted discount. |
|
| 532 | - * @param WP_REST_Request $request The request sent to the API. |
|
| 533 | - */ |
|
| 534 | - do_action( "wpinv_rest_delete_discount", $discount, $request ); |
|
| 525 | + /** |
|
| 526 | + * Fires immediately after a single discount is deleted via the REST API. |
|
| 527 | + * |
|
| 528 | + * |
|
| 529 | + * @since 1.0.13 |
|
| 530 | + * |
|
| 531 | + * @param WP_POST $discount The deleted discount. |
|
| 532 | + * @param WP_REST_Request $request The request sent to the API. |
|
| 533 | + */ |
|
| 534 | + do_action( "wpinv_rest_delete_discount", $discount, $request ); |
|
| 535 | 535 | |
| 536 | - return $response; |
|
| 536 | + return $response; |
|
| 537 | 537 | |
| 538 | - } |
|
| 538 | + } |
|
| 539 | 539 | |
| 540 | 540 | |
| 541 | 541 | /** |
| 542 | - * Retrieves the query params for the discount collection. |
|
| 543 | - * |
|
| 544 | - * @since 1.0.13 |
|
| 545 | - * |
|
| 546 | - * @return array Collection parameters. |
|
| 547 | - */ |
|
| 548 | - public function get_collection_params() { |
|
| 542 | + * Retrieves the query params for the discount collection. |
|
| 543 | + * |
|
| 544 | + * @since 1.0.13 |
|
| 545 | + * |
|
| 546 | + * @return array Collection parameters. |
|
| 547 | + */ |
|
| 548 | + public function get_collection_params() { |
|
| 549 | 549 | |
| 550 | 550 | $query_params = array( |
| 551 | 551 | |
@@ -559,27 +559,27 @@ discard block |
||
| 559 | 559 | |
| 560 | 560 | // Discount types |
| 561 | 561 | 'type' => array( |
| 562 | - 'description' => __( 'Type of discounts to fetch.', 'invoicing' ), |
|
| 563 | - 'type' => 'array', |
|
| 564 | - 'default' => array_keys( wpinv_get_discount_types() ), |
|
| 565 | - 'items' => array( |
|
| 562 | + 'description' => __( 'Type of discounts to fetch.', 'invoicing' ), |
|
| 563 | + 'type' => 'array', |
|
| 564 | + 'default' => array_keys( wpinv_get_discount_types() ), |
|
| 565 | + 'items' => array( |
|
| 566 | 566 | 'enum' => array_keys( wpinv_get_discount_types() ), |
| 567 | 567 | 'type' => 'string', |
| 568 | 568 | ), |
| 569 | - ), |
|
| 569 | + ), |
|
| 570 | 570 | |
| 571 | - // Number of results per page |
|
| 571 | + // Number of results per page |
|
| 572 | 572 | 'limit' => array( |
| 573 | - 'description' => __( 'Number of discounts to fetch.', 'invoicing' ), |
|
| 574 | - 'type' => 'integer', |
|
| 575 | - 'default' => (int) get_option( 'posts_per_page' ), |
|
| 573 | + 'description' => __( 'Number of discounts to fetch.', 'invoicing' ), |
|
| 574 | + 'type' => 'integer', |
|
| 575 | + 'default' => (int) get_option( 'posts_per_page' ), |
|
| 576 | 576 | ), |
| 577 | 577 | |
| 578 | 578 | // Pagination |
| 579 | 579 | 'page' => array( |
| 580 | - 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
| 581 | - 'type' => 'integer', |
|
| 582 | - 'default' => 1, |
|
| 580 | + 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
| 581 | + 'type' => 'integer', |
|
| 582 | + 'default' => 1, |
|
| 583 | 583 | ), |
| 584 | 584 | |
| 585 | 585 | // Exclude certain items |
@@ -602,9 +602,9 @@ discard block |
||
| 602 | 602 | 'date', |
| 603 | 603 | 'ID', |
| 604 | 604 | 'modified', |
| 605 | - 'title', |
|
| 606 | - 'relevance', |
|
| 607 | - 'rand' |
|
| 605 | + 'title', |
|
| 606 | + 'relevance', |
|
| 607 | + 'rand' |
|
| 608 | 608 | ), |
| 609 | 609 | ), |
| 610 | 610 | |
@@ -614,598 +614,598 @@ discard block |
||
| 614 | 614 | 'type' => 'string', |
| 615 | 615 | 'default' => 'DESC', |
| 616 | 616 | 'enum' => array( 'ASC', 'DESC' ), |
| 617 | - ), |
|
| 617 | + ), |
|
| 618 | 618 | |
| 619 | - // Search term |
|
| 619 | + // Search term |
|
| 620 | 620 | 'search' => array( |
| 621 | - 'description' => __( 'Return discounts that match the search term.', 'invoicing' ), |
|
| 622 | - 'type' => 'string', |
|
| 621 | + 'description' => __( 'Return discounts that match the search term.', 'invoicing' ), |
|
| 622 | + 'type' => 'string', |
|
| 623 | 623 | ), |
| 624 | 624 | ); |
| 625 | 625 | |
| 626 | - /** |
|
| 627 | - * Filter collection parameters for the discounts controller. |
|
| 628 | - * |
|
| 629 | - * |
|
| 630 | - * @since 1.0.13 |
|
| 631 | - * |
|
| 632 | - * @param array $query_params JSON Schema-formatted collection parameters. |
|
| 633 | - */ |
|
| 634 | - return apply_filters( "wpinv_rest_discounts_collection_params", $query_params ); |
|
| 626 | + /** |
|
| 627 | + * Filter collection parameters for the discounts controller. |
|
| 628 | + * |
|
| 629 | + * |
|
| 630 | + * @since 1.0.13 |
|
| 631 | + * |
|
| 632 | + * @param array $query_params JSON Schema-formatted collection parameters. |
|
| 633 | + */ |
|
| 634 | + return apply_filters( "wpinv_rest_discounts_collection_params", $query_params ); |
|
| 635 | 635 | } |
| 636 | 636 | |
| 637 | 637 | /** |
| 638 | - * Checks if a given post type can be viewed or managed. |
|
| 639 | - * |
|
| 640 | - * @since 1.0.13 |
|
| 641 | - * |
|
| 642 | - * @param object|string $post_type Post type name or object. |
|
| 643 | - * @return bool Whether the post type is allowed in REST. |
|
| 644 | - */ |
|
| 645 | - protected function check_is_post_type_allowed( $post_type ) { |
|
| 646 | - return true; |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - /** |
|
| 650 | - * Prepares a single item for create or update. |
|
| 651 | - * |
|
| 652 | - * @since 1.0.13 |
|
| 653 | - * |
|
| 654 | - * @param WP_REST_Request $request Request object. |
|
| 655 | - * @return array|WP_Error Discount Properties or WP_Error. |
|
| 656 | - */ |
|
| 657 | - protected function prepare_item_for_database( $request ) { |
|
| 658 | - $prepared_item = new stdClass(); |
|
| 659 | - $prepared_item->meta_input = array(); |
|
| 660 | - |
|
| 661 | - // Post ID. |
|
| 662 | - if ( isset( $request['id'] ) ) { |
|
| 663 | - $existing_item = $this->get_post( $request['id'] ); |
|
| 664 | - if ( is_wp_error( $existing_item ) ) { |
|
| 665 | - return $existing_item; |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - $prepared_item->ID = $existing_item->ID; |
|
| 669 | - } |
|
| 670 | - |
|
| 671 | - $schema = $this->get_item_schema(); |
|
| 672 | - |
|
| 673 | - // item title. |
|
| 674 | - if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) { |
|
| 675 | - $prepared_item->post_title = sanitize_text_field( $request['title'] ); |
|
| 676 | - } |
|
| 677 | - |
|
| 678 | - // item status. |
|
| 679 | - if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) && in_array( $request['status'], array_keys( get_post_stati( array( 'internal' => false ) ) ) ) ) { |
|
| 680 | - $prepared_item->post_status = sanitize_text_field( $request['status'] ); |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - // Code. |
|
| 684 | - if ( ! empty( $schema['properties']['code'] ) && isset( $request['code'] ) ) { |
|
| 685 | - $prepared_item->meta_input['_wpi_discount_code'] = trim( $request['code'] ); |
|
| 686 | - } |
|
| 687 | - |
|
| 688 | - // Type. |
|
| 689 | - if ( ! empty( $schema['properties']['type'] ) && isset( $request['type'] ) && in_array( $request['type'], array_keys( wpinv_get_discount_types() ) ) ) { |
|
| 690 | - $prepared_item->meta_input['_wpi_discount_type'] = trim( $request['type'] ); |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - // Amount. |
|
| 694 | - if ( ! empty( $schema['properties']['amount'] ) && isset( $request['amount'] ) ) { |
|
| 695 | - $prepared_item->meta_input['_wpi_discount_amount'] = floatval( $request['amount'] ); |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - // Items. |
|
| 699 | - if ( ! empty( $schema['properties']['items'] ) && isset( $request['items'] ) ) { |
|
| 700 | - $prepared_item->meta_input['_wpi_discount_items'] = wpinv_parse_list( $request['items'] ); |
|
| 701 | - } |
|
| 702 | - |
|
| 703 | - // Excluded Items. |
|
| 704 | - if ( ! empty( $schema['properties']['exclude_items'] ) && isset( $request['exclude_items'] ) ) { |
|
| 705 | - $prepared_item->meta_input['_wpi_discount_excluded_items'] = wpinv_parse_list( $request['exclude_items'] ); |
|
| 706 | - } |
|
| 707 | - |
|
| 708 | - // Start date. |
|
| 709 | - if ( ! empty( $schema['properties']['start_date'] ) && isset( $request['start_date'] ) ) { |
|
| 710 | - $prepared_item->meta_input['_wpi_discount_start'] = trim( $request['start_date'] ); |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - // End date. |
|
| 714 | - if ( ! empty( $schema['properties']['end_date'] ) && isset( $request['end_date'] ) ) { |
|
| 715 | - $prepared_item->meta_input['_wpi_discount_expiration'] = trim( $request['end_date'] ); |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - // Minimum amount. |
|
| 719 | - if ( ! empty( $schema['properties']['minimum_amount'] ) && isset( $request['minimum_amount'] ) ) { |
|
| 720 | - $prepared_item->meta_input['_wpi_discount_min_total'] = floatval( $request['minimum_amount'] ); |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - // Maximum amount. |
|
| 724 | - if ( ! empty( $schema['properties']['maximum_amount'] ) && isset( $request['maximum_amount'] ) ) { |
|
| 725 | - $prepared_item->meta_input['_wpi_discount_max_total'] = floatval( $request['maximum_amount'] ); |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - // Recurring. |
|
| 729 | - if ( ! empty( $schema['properties']['recurring'] ) && isset( $request['recurring'] ) ) { |
|
| 730 | - $prepared_item->meta_input['_wpi_discount_is_recurring'] = empty( (int) $request['recurring'] ) ? 0 : 1; |
|
| 731 | - } |
|
| 732 | - |
|
| 733 | - // Maximum uses. |
|
| 734 | - if ( ! empty( $schema['properties']['max_uses'] ) && isset( $request['max_uses'] ) ) { |
|
| 735 | - $prepared_item->meta_input['_wpi_discount_max_uses'] = intval( $request['max_uses'] ); |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - // Single use. |
|
| 739 | - if ( ! empty( $schema['properties']['single_use'] ) && isset( $request['single_use'] ) ) { |
|
| 740 | - $prepared_item->meta_input['_wpi_discount_is_single_use'] = empty( (int) $request['single_use'] ) ? 0 : 1; |
|
| 741 | - } |
|
| 742 | - |
|
| 743 | - $discount_data = (array) wp_unslash( $prepared_item ); |
|
| 744 | - |
|
| 745 | - /** |
|
| 746 | - * Filters an item before it is inserted via the REST API. |
|
| 747 | - * |
|
| 748 | - * @since 1.0.13 |
|
| 749 | - * |
|
| 750 | - * @param array $discount_data An array of discount data |
|
| 751 | - * @param WP_REST_Request $request Request object. |
|
| 752 | - */ |
|
| 753 | - return apply_filters( "wpinv_rest_pre_insert_discount", $discount_data, $request ); |
|
| 754 | - |
|
| 755 | - } |
|
| 756 | - |
|
| 757 | - /** |
|
| 758 | - * Prepares a single discount output for response. |
|
| 759 | - * |
|
| 760 | - * @since 1.0.13 |
|
| 761 | - * |
|
| 762 | - * @param WP_Post $discount WP_Post object. |
|
| 763 | - * @param WP_REST_Request $request Request object. |
|
| 764 | - * @return WP_REST_Response Response object. |
|
| 765 | - */ |
|
| 766 | - public function prepare_item_for_response( $discount, $request ) { |
|
| 767 | - |
|
| 768 | - $GLOBALS['post'] = get_post( $discount->ID ); |
|
| 769 | - |
|
| 770 | - setup_postdata( $discount->ID ); |
|
| 771 | - |
|
| 772 | - // Fetch the fields to include in this response. |
|
| 773 | - $fields = $this->get_fields_for_response( $request ); |
|
| 774 | - |
|
| 775 | - // Base fields for every discount. |
|
| 776 | - $data = array(); |
|
| 777 | - |
|
| 778 | - // Set up ID. |
|
| 779 | - if ( rest_is_field_included( 'id', $fields ) ) { |
|
| 780 | - $data['id'] = $discount->ID; |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - // Title. |
|
| 784 | - if ( rest_is_field_included( 'title', $fields ) ) { |
|
| 785 | - $data['title'] = get_the_title( $discount->ID ); |
|
| 786 | - } |
|
| 787 | - |
|
| 788 | - // Code. |
|
| 789 | - if ( rest_is_field_included( 'code', $fields ) ) { |
|
| 790 | - $data['code'] = wpinv_get_discount_code( $discount->ID ); |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - // Type. |
|
| 794 | - if ( rest_is_field_included( 'type', $fields ) ) { |
|
| 795 | - $data['type'] = wpinv_get_discount_type( $discount->ID ); |
|
| 796 | - } |
|
| 797 | - |
|
| 798 | - // Amount. |
|
| 799 | - if ( rest_is_field_included( 'amount', $fields ) ) { |
|
| 800 | - $data['amount'] = wpinv_get_discount_amount( $discount->ID ); |
|
| 801 | - } |
|
| 802 | - |
|
| 803 | - // Status. |
|
| 804 | - if ( rest_is_field_included( 'status', $fields ) ) { |
|
| 805 | - $data['status'] = get_post_status( $discount->ID ); |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - // Items. |
|
| 809 | - if ( rest_is_field_included( 'items', $fields ) ) { |
|
| 810 | - $data['items'] = wpinv_get_discount_item_reqs( $discount->ID ); |
|
| 811 | - } |
|
| 812 | - |
|
| 813 | - // Excluded Items. |
|
| 814 | - if ( rest_is_field_included( 'exclude_items', $fields ) ) { |
|
| 815 | - $data['exclude_items'] = wpinv_get_discount_excluded_items( $discount->ID ); |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - // Start date. |
|
| 819 | - if ( rest_is_field_included( 'start_date', $fields ) ) { |
|
| 820 | - $data['start_date'] = wpinv_get_discount_start_date( $discount->ID ); |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - // End date. |
|
| 824 | - if ( rest_is_field_included( 'end_date', $fields ) ) { |
|
| 825 | - $data['end_date'] = wpinv_get_discount_expiration( $discount->ID ); |
|
| 826 | - } |
|
| 827 | - |
|
| 828 | - // Minimum amount. |
|
| 829 | - if ( rest_is_field_included( 'minimum_amount', $fields ) ) { |
|
| 830 | - $data['minimum_amount'] = wpinv_get_discount_min_total( $discount->ID ); |
|
| 831 | - } |
|
| 832 | - |
|
| 833 | - // Maximum amount. |
|
| 834 | - if ( rest_is_field_included( 'maximum_amount', $fields ) ) { |
|
| 835 | - $data['maximum_amount'] = wpinv_get_discount_max_total( $discount->ID ); |
|
| 836 | - } |
|
| 837 | - |
|
| 838 | - // Recurring. |
|
| 839 | - if ( rest_is_field_included( 'recurring', $fields ) ) { |
|
| 840 | - $data['recurring'] = wpinv_discount_is_recurring( $discount->ID ); |
|
| 841 | - } |
|
| 842 | - |
|
| 843 | - // Maximum uses. |
|
| 844 | - if ( rest_is_field_included( 'max_uses', $fields ) ) { |
|
| 845 | - $data['max_uses'] = wpinv_get_discount_max_uses( $discount->ID ); |
|
| 846 | - } |
|
| 847 | - |
|
| 848 | - // Single use. |
|
| 849 | - if ( rest_is_field_included( 'single_use', $fields ) ) { |
|
| 850 | - $data['single_use'] = wpinv_discount_is_single_use( $discount->ID ); |
|
| 851 | - } |
|
| 852 | - |
|
| 853 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 854 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 855 | - $data = $this->filter_response_by_context( $data, $context ); |
|
| 856 | - |
|
| 857 | - // Wrap the data in a response object. |
|
| 858 | - $response = rest_ensure_response( $data ); |
|
| 859 | - |
|
| 860 | - $links = $this->prepare_links( $discount ); |
|
| 861 | - $response->add_links( $links ); |
|
| 862 | - |
|
| 863 | - if ( ! empty( $links['self']['href'] ) ) { |
|
| 864 | - $actions = $this->get_available_actions( $discount, $request ); |
|
| 865 | - |
|
| 866 | - $self = $links['self']['href']; |
|
| 867 | - |
|
| 868 | - foreach ( $actions as $rel ) { |
|
| 869 | - $response->add_link( $rel, $self ); |
|
| 870 | - } |
|
| 871 | - } |
|
| 872 | - |
|
| 873 | - /** |
|
| 874 | - * Filters the discount data for a response. |
|
| 875 | - * |
|
| 876 | - * @since 1.0.13 |
|
| 877 | - * |
|
| 878 | - * @param WP_REST_Response $response The response object. |
|
| 879 | - * @param WP_Post $discount The discount post object. |
|
| 880 | - * @param WP_REST_Request $request Request object. |
|
| 881 | - */ |
|
| 882 | - return apply_filters( "wpinv_rest_prepare_discount", $response, $discount, $request ); |
|
| 883 | - } |
|
| 884 | - |
|
| 885 | - /** |
|
| 886 | - * Gets an array of fields to be included on the response. |
|
| 887 | - * |
|
| 888 | - * Included fields are based on item schema and `_fields=` request argument. |
|
| 889 | - * |
|
| 890 | - * @since 1.0.13 |
|
| 891 | - * |
|
| 892 | - * @param WP_REST_Request $request Full details about the request. |
|
| 893 | - * @return array Fields to be included in the response. |
|
| 894 | - */ |
|
| 895 | - public function get_fields_for_response( $request ) { |
|
| 896 | - $schema = $this->get_item_schema(); |
|
| 897 | - $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
| 898 | - |
|
| 899 | - $additional_fields = $this->get_additional_fields(); |
|
| 900 | - foreach ( $additional_fields as $field_name => $field_options ) { |
|
| 901 | - // For back-compat, include any field with an empty schema |
|
| 902 | - // because it won't be present in $this->get_item_schema(). |
|
| 903 | - if ( is_null( $field_options['schema'] ) ) { |
|
| 904 | - $properties[ $field_name ] = $field_options; |
|
| 905 | - } |
|
| 906 | - } |
|
| 907 | - |
|
| 908 | - // Exclude fields that specify a different context than the request context. |
|
| 909 | - $context = $request['context']; |
|
| 910 | - if ( $context ) { |
|
| 911 | - foreach ( $properties as $name => $options ) { |
|
| 912 | - if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
| 913 | - unset( $properties[ $name ] ); |
|
| 914 | - } |
|
| 915 | - } |
|
| 916 | - } |
|
| 917 | - |
|
| 918 | - $fields = array_keys( $properties ); |
|
| 919 | - |
|
| 920 | - if ( ! isset( $request['_fields'] ) ) { |
|
| 921 | - return $fields; |
|
| 922 | - } |
|
| 923 | - $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
| 924 | - if ( 0 === count( $requested_fields ) ) { |
|
| 925 | - return $fields; |
|
| 926 | - } |
|
| 927 | - // Trim off outside whitespace from the comma delimited list. |
|
| 928 | - $requested_fields = array_map( 'trim', $requested_fields ); |
|
| 929 | - // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
| 930 | - if ( in_array( 'id', $fields, true ) ) { |
|
| 931 | - $requested_fields[] = 'id'; |
|
| 932 | - } |
|
| 933 | - // Return the list of all requested fields which appear in the schema. |
|
| 934 | - return array_reduce( |
|
| 935 | - $requested_fields, |
|
| 936 | - function( $response_fields, $field ) use ( $fields ) { |
|
| 937 | - if ( in_array( $field, $fields, true ) ) { |
|
| 938 | - $response_fields[] = $field; |
|
| 939 | - return $response_fields; |
|
| 940 | - } |
|
| 941 | - // Check for nested fields if $field is not a direct match. |
|
| 942 | - $nested_fields = explode( '.', $field ); |
|
| 943 | - // A nested field is included so long as its top-level property is |
|
| 944 | - // present in the schema. |
|
| 945 | - if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
| 946 | - $response_fields[] = $field; |
|
| 947 | - } |
|
| 948 | - return $response_fields; |
|
| 949 | - }, |
|
| 950 | - array() |
|
| 951 | - ); |
|
| 952 | - } |
|
| 953 | - |
|
| 954 | - /** |
|
| 955 | - * Retrieves the discount's schema, conforming to JSON Schema. |
|
| 956 | - * |
|
| 957 | - * @since 1.0.13 |
|
| 958 | - * |
|
| 959 | - * @return array Discount schema data. |
|
| 960 | - */ |
|
| 961 | - public function get_item_schema() { |
|
| 962 | - |
|
| 963 | - // Maybe retrieve the schema from cache. |
|
| 964 | - if ( empty( $this->schema ) ) { |
|
| 965 | - return $this->add_additional_fields_schema( $this->schema ); |
|
| 966 | - } |
|
| 967 | - |
|
| 968 | - $schema = array( |
|
| 969 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
| 970 | - 'title' => $this->post_type, |
|
| 971 | - 'type' => 'object', |
|
| 972 | - |
|
| 973 | - // Base properties for every Item. |
|
| 974 | - 'properties' => array( |
|
| 975 | - |
|
| 976 | - 'id' => array( |
|
| 977 | - 'description' => __( 'Unique identifier for the discount.', 'invoicing' ), |
|
| 978 | - 'type' => 'integer', |
|
| 979 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 980 | - 'readonly' => true, |
|
| 981 | - ), |
|
| 982 | - |
|
| 983 | - 'title' => array( |
|
| 984 | - 'description' => __( 'The title for the discount.', 'invoicing' ), |
|
| 985 | - 'type' => 'string', |
|
| 986 | - 'context' => array( 'view', 'edit' ), |
|
| 987 | - ), |
|
| 988 | - |
|
| 989 | - 'code' => array( |
|
| 990 | - 'description' => __( 'The discount code.', 'invoicing' ), |
|
| 991 | - 'type' => 'string', |
|
| 992 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 993 | - 'required' => true, |
|
| 994 | - ), |
|
| 995 | - |
|
| 996 | - 'type' => array( |
|
| 997 | - 'description' => __( 'The type of discount.', 'invoicing' ), |
|
| 998 | - 'type' => 'string', |
|
| 999 | - 'enum' => array_keys( wpinv_get_discount_types() ), |
|
| 1000 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1001 | - 'default' => 'percentage', |
|
| 1002 | - ), |
|
| 1003 | - |
|
| 1004 | - 'amount' => array( |
|
| 1005 | - 'description' => __( 'The discount value.', 'invoicing' ), |
|
| 1006 | - 'type' => 'number', |
|
| 1007 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1008 | - 'required' => true, |
|
| 1009 | - ), |
|
| 1010 | - |
|
| 1011 | - 'status' => array( |
|
| 1012 | - 'description' => __( 'A named status for the discount.', 'invoicing' ), |
|
| 1013 | - 'type' => 'string', |
|
| 1014 | - 'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ), |
|
| 1015 | - 'context' => array( 'view', 'edit' ), |
|
| 1016 | - ), |
|
| 1017 | - |
|
| 1018 | - 'items' => array( |
|
| 1019 | - 'description' => __( 'Items which need to be in the cart to use this discount or, for "Item Discounts", which items are discounted. If left blank, this discount will be used on any item.', 'invoicing' ), |
|
| 1020 | - 'type' => 'array', |
|
| 1021 | - 'context' => array( 'view', 'edit' ), |
|
| 1022 | - ), |
|
| 1023 | - |
|
| 1024 | - 'exclude_items' => array( |
|
| 1025 | - 'description' => __( 'Items which are NOT allowed to use this discount.', 'invoicing' ), |
|
| 1026 | - 'type' => 'array', |
|
| 1027 | - 'context' => array( 'view', 'edit' ), |
|
| 1028 | - ), |
|
| 1029 | - |
|
| 1030 | - 'start_date' => array( |
|
| 1031 | - 'description' => __( 'The start date for the discount in the format of yyyy-mm-dd hh:mm:ss . If provided, the discount can only be used after or on this date.', 'invoicing' ), |
|
| 1032 | - 'type' => 'string', |
|
| 1033 | - 'context' => array( 'view', 'edit' ), |
|
| 1034 | - ), |
|
| 1035 | - |
|
| 1036 | - 'end_date' => array( |
|
| 1037 | - 'description' => __( 'The expiration date for the discount.', 'invoicing' ), |
|
| 1038 | - 'type' => 'string', |
|
| 1039 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1040 | - ), |
|
| 638 | + * Checks if a given post type can be viewed or managed. |
|
| 639 | + * |
|
| 640 | + * @since 1.0.13 |
|
| 641 | + * |
|
| 642 | + * @param object|string $post_type Post type name or object. |
|
| 643 | + * @return bool Whether the post type is allowed in REST. |
|
| 644 | + */ |
|
| 645 | + protected function check_is_post_type_allowed( $post_type ) { |
|
| 646 | + return true; |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + /** |
|
| 650 | + * Prepares a single item for create or update. |
|
| 651 | + * |
|
| 652 | + * @since 1.0.13 |
|
| 653 | + * |
|
| 654 | + * @param WP_REST_Request $request Request object. |
|
| 655 | + * @return array|WP_Error Discount Properties or WP_Error. |
|
| 656 | + */ |
|
| 657 | + protected function prepare_item_for_database( $request ) { |
|
| 658 | + $prepared_item = new stdClass(); |
|
| 659 | + $prepared_item->meta_input = array(); |
|
| 660 | + |
|
| 661 | + // Post ID. |
|
| 662 | + if ( isset( $request['id'] ) ) { |
|
| 663 | + $existing_item = $this->get_post( $request['id'] ); |
|
| 664 | + if ( is_wp_error( $existing_item ) ) { |
|
| 665 | + return $existing_item; |
|
| 666 | + } |
|
| 667 | + |
|
| 668 | + $prepared_item->ID = $existing_item->ID; |
|
| 669 | + } |
|
| 670 | + |
|
| 671 | + $schema = $this->get_item_schema(); |
|
| 672 | + |
|
| 673 | + // item title. |
|
| 674 | + if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) { |
|
| 675 | + $prepared_item->post_title = sanitize_text_field( $request['title'] ); |
|
| 676 | + } |
|
| 677 | + |
|
| 678 | + // item status. |
|
| 679 | + if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) && in_array( $request['status'], array_keys( get_post_stati( array( 'internal' => false ) ) ) ) ) { |
|
| 680 | + $prepared_item->post_status = sanitize_text_field( $request['status'] ); |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + // Code. |
|
| 684 | + if ( ! empty( $schema['properties']['code'] ) && isset( $request['code'] ) ) { |
|
| 685 | + $prepared_item->meta_input['_wpi_discount_code'] = trim( $request['code'] ); |
|
| 686 | + } |
|
| 687 | + |
|
| 688 | + // Type. |
|
| 689 | + if ( ! empty( $schema['properties']['type'] ) && isset( $request['type'] ) && in_array( $request['type'], array_keys( wpinv_get_discount_types() ) ) ) { |
|
| 690 | + $prepared_item->meta_input['_wpi_discount_type'] = trim( $request['type'] ); |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + // Amount. |
|
| 694 | + if ( ! empty( $schema['properties']['amount'] ) && isset( $request['amount'] ) ) { |
|
| 695 | + $prepared_item->meta_input['_wpi_discount_amount'] = floatval( $request['amount'] ); |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + // Items. |
|
| 699 | + if ( ! empty( $schema['properties']['items'] ) && isset( $request['items'] ) ) { |
|
| 700 | + $prepared_item->meta_input['_wpi_discount_items'] = wpinv_parse_list( $request['items'] ); |
|
| 701 | + } |
|
| 702 | + |
|
| 703 | + // Excluded Items. |
|
| 704 | + if ( ! empty( $schema['properties']['exclude_items'] ) && isset( $request['exclude_items'] ) ) { |
|
| 705 | + $prepared_item->meta_input['_wpi_discount_excluded_items'] = wpinv_parse_list( $request['exclude_items'] ); |
|
| 706 | + } |
|
| 707 | + |
|
| 708 | + // Start date. |
|
| 709 | + if ( ! empty( $schema['properties']['start_date'] ) && isset( $request['start_date'] ) ) { |
|
| 710 | + $prepared_item->meta_input['_wpi_discount_start'] = trim( $request['start_date'] ); |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + // End date. |
|
| 714 | + if ( ! empty( $schema['properties']['end_date'] ) && isset( $request['end_date'] ) ) { |
|
| 715 | + $prepared_item->meta_input['_wpi_discount_expiration'] = trim( $request['end_date'] ); |
|
| 716 | + } |
|
| 717 | + |
|
| 718 | + // Minimum amount. |
|
| 719 | + if ( ! empty( $schema['properties']['minimum_amount'] ) && isset( $request['minimum_amount'] ) ) { |
|
| 720 | + $prepared_item->meta_input['_wpi_discount_min_total'] = floatval( $request['minimum_amount'] ); |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + // Maximum amount. |
|
| 724 | + if ( ! empty( $schema['properties']['maximum_amount'] ) && isset( $request['maximum_amount'] ) ) { |
|
| 725 | + $prepared_item->meta_input['_wpi_discount_max_total'] = floatval( $request['maximum_amount'] ); |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + // Recurring. |
|
| 729 | + if ( ! empty( $schema['properties']['recurring'] ) && isset( $request['recurring'] ) ) { |
|
| 730 | + $prepared_item->meta_input['_wpi_discount_is_recurring'] = empty( (int) $request['recurring'] ) ? 0 : 1; |
|
| 731 | + } |
|
| 732 | + |
|
| 733 | + // Maximum uses. |
|
| 734 | + if ( ! empty( $schema['properties']['max_uses'] ) && isset( $request['max_uses'] ) ) { |
|
| 735 | + $prepared_item->meta_input['_wpi_discount_max_uses'] = intval( $request['max_uses'] ); |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + // Single use. |
|
| 739 | + if ( ! empty( $schema['properties']['single_use'] ) && isset( $request['single_use'] ) ) { |
|
| 740 | + $prepared_item->meta_input['_wpi_discount_is_single_use'] = empty( (int) $request['single_use'] ) ? 0 : 1; |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + $discount_data = (array) wp_unslash( $prepared_item ); |
|
| 744 | + |
|
| 745 | + /** |
|
| 746 | + * Filters an item before it is inserted via the REST API. |
|
| 747 | + * |
|
| 748 | + * @since 1.0.13 |
|
| 749 | + * |
|
| 750 | + * @param array $discount_data An array of discount data |
|
| 751 | + * @param WP_REST_Request $request Request object. |
|
| 752 | + */ |
|
| 753 | + return apply_filters( "wpinv_rest_pre_insert_discount", $discount_data, $request ); |
|
| 754 | + |
|
| 755 | + } |
|
| 756 | + |
|
| 757 | + /** |
|
| 758 | + * Prepares a single discount output for response. |
|
| 759 | + * |
|
| 760 | + * @since 1.0.13 |
|
| 761 | + * |
|
| 762 | + * @param WP_Post $discount WP_Post object. |
|
| 763 | + * @param WP_REST_Request $request Request object. |
|
| 764 | + * @return WP_REST_Response Response object. |
|
| 765 | + */ |
|
| 766 | + public function prepare_item_for_response( $discount, $request ) { |
|
| 767 | + |
|
| 768 | + $GLOBALS['post'] = get_post( $discount->ID ); |
|
| 769 | + |
|
| 770 | + setup_postdata( $discount->ID ); |
|
| 771 | + |
|
| 772 | + // Fetch the fields to include in this response. |
|
| 773 | + $fields = $this->get_fields_for_response( $request ); |
|
| 774 | + |
|
| 775 | + // Base fields for every discount. |
|
| 776 | + $data = array(); |
|
| 777 | + |
|
| 778 | + // Set up ID. |
|
| 779 | + if ( rest_is_field_included( 'id', $fields ) ) { |
|
| 780 | + $data['id'] = $discount->ID; |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + // Title. |
|
| 784 | + if ( rest_is_field_included( 'title', $fields ) ) { |
|
| 785 | + $data['title'] = get_the_title( $discount->ID ); |
|
| 786 | + } |
|
| 787 | + |
|
| 788 | + // Code. |
|
| 789 | + if ( rest_is_field_included( 'code', $fields ) ) { |
|
| 790 | + $data['code'] = wpinv_get_discount_code( $discount->ID ); |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + // Type. |
|
| 794 | + if ( rest_is_field_included( 'type', $fields ) ) { |
|
| 795 | + $data['type'] = wpinv_get_discount_type( $discount->ID ); |
|
| 796 | + } |
|
| 797 | + |
|
| 798 | + // Amount. |
|
| 799 | + if ( rest_is_field_included( 'amount', $fields ) ) { |
|
| 800 | + $data['amount'] = wpinv_get_discount_amount( $discount->ID ); |
|
| 801 | + } |
|
| 802 | + |
|
| 803 | + // Status. |
|
| 804 | + if ( rest_is_field_included( 'status', $fields ) ) { |
|
| 805 | + $data['status'] = get_post_status( $discount->ID ); |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + // Items. |
|
| 809 | + if ( rest_is_field_included( 'items', $fields ) ) { |
|
| 810 | + $data['items'] = wpinv_get_discount_item_reqs( $discount->ID ); |
|
| 811 | + } |
|
| 812 | + |
|
| 813 | + // Excluded Items. |
|
| 814 | + if ( rest_is_field_included( 'exclude_items', $fields ) ) { |
|
| 815 | + $data['exclude_items'] = wpinv_get_discount_excluded_items( $discount->ID ); |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + // Start date. |
|
| 819 | + if ( rest_is_field_included( 'start_date', $fields ) ) { |
|
| 820 | + $data['start_date'] = wpinv_get_discount_start_date( $discount->ID ); |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + // End date. |
|
| 824 | + if ( rest_is_field_included( 'end_date', $fields ) ) { |
|
| 825 | + $data['end_date'] = wpinv_get_discount_expiration( $discount->ID ); |
|
| 826 | + } |
|
| 827 | + |
|
| 828 | + // Minimum amount. |
|
| 829 | + if ( rest_is_field_included( 'minimum_amount', $fields ) ) { |
|
| 830 | + $data['minimum_amount'] = wpinv_get_discount_min_total( $discount->ID ); |
|
| 831 | + } |
|
| 832 | + |
|
| 833 | + // Maximum amount. |
|
| 834 | + if ( rest_is_field_included( 'maximum_amount', $fields ) ) { |
|
| 835 | + $data['maximum_amount'] = wpinv_get_discount_max_total( $discount->ID ); |
|
| 836 | + } |
|
| 837 | + |
|
| 838 | + // Recurring. |
|
| 839 | + if ( rest_is_field_included( 'recurring', $fields ) ) { |
|
| 840 | + $data['recurring'] = wpinv_discount_is_recurring( $discount->ID ); |
|
| 841 | + } |
|
| 842 | + |
|
| 843 | + // Maximum uses. |
|
| 844 | + if ( rest_is_field_included( 'max_uses', $fields ) ) { |
|
| 845 | + $data['max_uses'] = wpinv_get_discount_max_uses( $discount->ID ); |
|
| 846 | + } |
|
| 847 | + |
|
| 848 | + // Single use. |
|
| 849 | + if ( rest_is_field_included( 'single_use', $fields ) ) { |
|
| 850 | + $data['single_use'] = wpinv_discount_is_single_use( $discount->ID ); |
|
| 851 | + } |
|
| 852 | + |
|
| 853 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 854 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 855 | + $data = $this->filter_response_by_context( $data, $context ); |
|
| 856 | + |
|
| 857 | + // Wrap the data in a response object. |
|
| 858 | + $response = rest_ensure_response( $data ); |
|
| 859 | + |
|
| 860 | + $links = $this->prepare_links( $discount ); |
|
| 861 | + $response->add_links( $links ); |
|
| 862 | + |
|
| 863 | + if ( ! empty( $links['self']['href'] ) ) { |
|
| 864 | + $actions = $this->get_available_actions( $discount, $request ); |
|
| 865 | + |
|
| 866 | + $self = $links['self']['href']; |
|
| 867 | + |
|
| 868 | + foreach ( $actions as $rel ) { |
|
| 869 | + $response->add_link( $rel, $self ); |
|
| 870 | + } |
|
| 871 | + } |
|
| 872 | + |
|
| 873 | + /** |
|
| 874 | + * Filters the discount data for a response. |
|
| 875 | + * |
|
| 876 | + * @since 1.0.13 |
|
| 877 | + * |
|
| 878 | + * @param WP_REST_Response $response The response object. |
|
| 879 | + * @param WP_Post $discount The discount post object. |
|
| 880 | + * @param WP_REST_Request $request Request object. |
|
| 881 | + */ |
|
| 882 | + return apply_filters( "wpinv_rest_prepare_discount", $response, $discount, $request ); |
|
| 883 | + } |
|
| 884 | + |
|
| 885 | + /** |
|
| 886 | + * Gets an array of fields to be included on the response. |
|
| 887 | + * |
|
| 888 | + * Included fields are based on item schema and `_fields=` request argument. |
|
| 889 | + * |
|
| 890 | + * @since 1.0.13 |
|
| 891 | + * |
|
| 892 | + * @param WP_REST_Request $request Full details about the request. |
|
| 893 | + * @return array Fields to be included in the response. |
|
| 894 | + */ |
|
| 895 | + public function get_fields_for_response( $request ) { |
|
| 896 | + $schema = $this->get_item_schema(); |
|
| 897 | + $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
| 898 | + |
|
| 899 | + $additional_fields = $this->get_additional_fields(); |
|
| 900 | + foreach ( $additional_fields as $field_name => $field_options ) { |
|
| 901 | + // For back-compat, include any field with an empty schema |
|
| 902 | + // because it won't be present in $this->get_item_schema(). |
|
| 903 | + if ( is_null( $field_options['schema'] ) ) { |
|
| 904 | + $properties[ $field_name ] = $field_options; |
|
| 905 | + } |
|
| 906 | + } |
|
| 907 | + |
|
| 908 | + // Exclude fields that specify a different context than the request context. |
|
| 909 | + $context = $request['context']; |
|
| 910 | + if ( $context ) { |
|
| 911 | + foreach ( $properties as $name => $options ) { |
|
| 912 | + if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
| 913 | + unset( $properties[ $name ] ); |
|
| 914 | + } |
|
| 915 | + } |
|
| 916 | + } |
|
| 917 | + |
|
| 918 | + $fields = array_keys( $properties ); |
|
| 919 | + |
|
| 920 | + if ( ! isset( $request['_fields'] ) ) { |
|
| 921 | + return $fields; |
|
| 922 | + } |
|
| 923 | + $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
| 924 | + if ( 0 === count( $requested_fields ) ) { |
|
| 925 | + return $fields; |
|
| 926 | + } |
|
| 927 | + // Trim off outside whitespace from the comma delimited list. |
|
| 928 | + $requested_fields = array_map( 'trim', $requested_fields ); |
|
| 929 | + // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
| 930 | + if ( in_array( 'id', $fields, true ) ) { |
|
| 931 | + $requested_fields[] = 'id'; |
|
| 932 | + } |
|
| 933 | + // Return the list of all requested fields which appear in the schema. |
|
| 934 | + return array_reduce( |
|
| 935 | + $requested_fields, |
|
| 936 | + function( $response_fields, $field ) use ( $fields ) { |
|
| 937 | + if ( in_array( $field, $fields, true ) ) { |
|
| 938 | + $response_fields[] = $field; |
|
| 939 | + return $response_fields; |
|
| 940 | + } |
|
| 941 | + // Check for nested fields if $field is not a direct match. |
|
| 942 | + $nested_fields = explode( '.', $field ); |
|
| 943 | + // A nested field is included so long as its top-level property is |
|
| 944 | + // present in the schema. |
|
| 945 | + if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
| 946 | + $response_fields[] = $field; |
|
| 947 | + } |
|
| 948 | + return $response_fields; |
|
| 949 | + }, |
|
| 950 | + array() |
|
| 951 | + ); |
|
| 952 | + } |
|
| 953 | + |
|
| 954 | + /** |
|
| 955 | + * Retrieves the discount's schema, conforming to JSON Schema. |
|
| 956 | + * |
|
| 957 | + * @since 1.0.13 |
|
| 958 | + * |
|
| 959 | + * @return array Discount schema data. |
|
| 960 | + */ |
|
| 961 | + public function get_item_schema() { |
|
| 962 | + |
|
| 963 | + // Maybe retrieve the schema from cache. |
|
| 964 | + if ( empty( $this->schema ) ) { |
|
| 965 | + return $this->add_additional_fields_schema( $this->schema ); |
|
| 966 | + } |
|
| 967 | + |
|
| 968 | + $schema = array( |
|
| 969 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
| 970 | + 'title' => $this->post_type, |
|
| 971 | + 'type' => 'object', |
|
| 972 | + |
|
| 973 | + // Base properties for every Item. |
|
| 974 | + 'properties' => array( |
|
| 975 | + |
|
| 976 | + 'id' => array( |
|
| 977 | + 'description' => __( 'Unique identifier for the discount.', 'invoicing' ), |
|
| 978 | + 'type' => 'integer', |
|
| 979 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 980 | + 'readonly' => true, |
|
| 981 | + ), |
|
| 982 | + |
|
| 983 | + 'title' => array( |
|
| 984 | + 'description' => __( 'The title for the discount.', 'invoicing' ), |
|
| 985 | + 'type' => 'string', |
|
| 986 | + 'context' => array( 'view', 'edit' ), |
|
| 987 | + ), |
|
| 988 | + |
|
| 989 | + 'code' => array( |
|
| 990 | + 'description' => __( 'The discount code.', 'invoicing' ), |
|
| 991 | + 'type' => 'string', |
|
| 992 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 993 | + 'required' => true, |
|
| 994 | + ), |
|
| 995 | + |
|
| 996 | + 'type' => array( |
|
| 997 | + 'description' => __( 'The type of discount.', 'invoicing' ), |
|
| 998 | + 'type' => 'string', |
|
| 999 | + 'enum' => array_keys( wpinv_get_discount_types() ), |
|
| 1000 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1001 | + 'default' => 'percentage', |
|
| 1002 | + ), |
|
| 1003 | + |
|
| 1004 | + 'amount' => array( |
|
| 1005 | + 'description' => __( 'The discount value.', 'invoicing' ), |
|
| 1006 | + 'type' => 'number', |
|
| 1007 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1008 | + 'required' => true, |
|
| 1009 | + ), |
|
| 1010 | + |
|
| 1011 | + 'status' => array( |
|
| 1012 | + 'description' => __( 'A named status for the discount.', 'invoicing' ), |
|
| 1013 | + 'type' => 'string', |
|
| 1014 | + 'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ), |
|
| 1015 | + 'context' => array( 'view', 'edit' ), |
|
| 1016 | + ), |
|
| 1017 | + |
|
| 1018 | + 'items' => array( |
|
| 1019 | + 'description' => __( 'Items which need to be in the cart to use this discount or, for "Item Discounts", which items are discounted. If left blank, this discount will be used on any item.', 'invoicing' ), |
|
| 1020 | + 'type' => 'array', |
|
| 1021 | + 'context' => array( 'view', 'edit' ), |
|
| 1022 | + ), |
|
| 1023 | + |
|
| 1024 | + 'exclude_items' => array( |
|
| 1025 | + 'description' => __( 'Items which are NOT allowed to use this discount.', 'invoicing' ), |
|
| 1026 | + 'type' => 'array', |
|
| 1027 | + 'context' => array( 'view', 'edit' ), |
|
| 1028 | + ), |
|
| 1029 | + |
|
| 1030 | + 'start_date' => array( |
|
| 1031 | + 'description' => __( 'The start date for the discount in the format of yyyy-mm-dd hh:mm:ss . If provided, the discount can only be used after or on this date.', 'invoicing' ), |
|
| 1032 | + 'type' => 'string', |
|
| 1033 | + 'context' => array( 'view', 'edit' ), |
|
| 1034 | + ), |
|
| 1035 | + |
|
| 1036 | + 'end_date' => array( |
|
| 1037 | + 'description' => __( 'The expiration date for the discount.', 'invoicing' ), |
|
| 1038 | + 'type' => 'string', |
|
| 1039 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1040 | + ), |
|
| 1041 | 1041 | |
| 1042 | - 'minimum_amount' => array( |
|
| 1043 | - 'description' => __( 'Minimum amount needed to use this invoice.', 'invoicing' ), |
|
| 1044 | - 'type' => 'number', |
|
| 1045 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1046 | - ), |
|
| 1047 | - |
|
| 1048 | - 'maximum_amount' => array( |
|
| 1049 | - 'description' => __( 'Maximum amount needed to use this invoice.', 'invoicing' ), |
|
| 1050 | - 'type' => 'number', |
|
| 1051 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1052 | - ), |
|
| 1053 | - |
|
| 1054 | - 'recurring' => array( |
|
| 1055 | - 'description' => __( 'Whether the discount is applied to all recurring payments or only the first recurring payment.', 'invoicing' ), |
|
| 1056 | - 'type' => 'integer', |
|
| 1057 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1058 | - ), |
|
| 1059 | - |
|
| 1060 | - 'max_uses' => array( |
|
| 1061 | - 'description' => __( 'The maximum number of times this discount code can be used.', 'invoicing' ), |
|
| 1062 | - 'type' => 'number', |
|
| 1063 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1064 | - ), |
|
| 1065 | - |
|
| 1066 | - 'single_use' => array( |
|
| 1067 | - 'description' => __( 'Whether or not this discount can only be used once per user.', 'invoicing' ), |
|
| 1068 | - 'type' => 'integer', |
|
| 1069 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1070 | - ) |
|
| 1071 | - |
|
| 1072 | - ), |
|
| 1073 | - ); |
|
| 1074 | - |
|
| 1075 | - // Add helpful links to the discount schem. |
|
| 1076 | - $schema['links'] = $this->get_schema_links(); |
|
| 1077 | - |
|
| 1078 | - /** |
|
| 1079 | - * Filters the discount schema for the REST API. |
|
| 1080 | - * |
|
| 1081 | - * Enables adding extra properties to discounts. |
|
| 1082 | - * |
|
| 1083 | - * @since 1.0.13 |
|
| 1084 | - * |
|
| 1085 | - * @param array $schema The discount schema. |
|
| 1086 | - */ |
|
| 1042 | + 'minimum_amount' => array( |
|
| 1043 | + 'description' => __( 'Minimum amount needed to use this invoice.', 'invoicing' ), |
|
| 1044 | + 'type' => 'number', |
|
| 1045 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1046 | + ), |
|
| 1047 | + |
|
| 1048 | + 'maximum_amount' => array( |
|
| 1049 | + 'description' => __( 'Maximum amount needed to use this invoice.', 'invoicing' ), |
|
| 1050 | + 'type' => 'number', |
|
| 1051 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1052 | + ), |
|
| 1053 | + |
|
| 1054 | + 'recurring' => array( |
|
| 1055 | + 'description' => __( 'Whether the discount is applied to all recurring payments or only the first recurring payment.', 'invoicing' ), |
|
| 1056 | + 'type' => 'integer', |
|
| 1057 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1058 | + ), |
|
| 1059 | + |
|
| 1060 | + 'max_uses' => array( |
|
| 1061 | + 'description' => __( 'The maximum number of times this discount code can be used.', 'invoicing' ), |
|
| 1062 | + 'type' => 'number', |
|
| 1063 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1064 | + ), |
|
| 1065 | + |
|
| 1066 | + 'single_use' => array( |
|
| 1067 | + 'description' => __( 'Whether or not this discount can only be used once per user.', 'invoicing' ), |
|
| 1068 | + 'type' => 'integer', |
|
| 1069 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1070 | + ) |
|
| 1071 | + |
|
| 1072 | + ), |
|
| 1073 | + ); |
|
| 1074 | + |
|
| 1075 | + // Add helpful links to the discount schem. |
|
| 1076 | + $schema['links'] = $this->get_schema_links(); |
|
| 1077 | + |
|
| 1078 | + /** |
|
| 1079 | + * Filters the discount schema for the REST API. |
|
| 1080 | + * |
|
| 1081 | + * Enables adding extra properties to discounts. |
|
| 1082 | + * |
|
| 1083 | + * @since 1.0.13 |
|
| 1084 | + * |
|
| 1085 | + * @param array $schema The discount schema. |
|
| 1086 | + */ |
|
| 1087 | 1087 | $schema = apply_filters( "wpinv_rest_discount_schema", $schema ); |
| 1088 | 1088 | |
| 1089 | - // Cache the discount schema. |
|
| 1090 | - $this->schema = $schema; |
|
| 1089 | + // Cache the discount schema. |
|
| 1090 | + $this->schema = $schema; |
|
| 1091 | 1091 | |
| 1092 | - return $this->add_additional_fields_schema( $this->schema ); |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - /** |
|
| 1096 | - * Retrieve Link Description Objects that should be added to the Schema for the discounts collection. |
|
| 1097 | - * |
|
| 1098 | - * @since 1.0.13 |
|
| 1099 | - * |
|
| 1100 | - * @return array |
|
| 1101 | - */ |
|
| 1102 | - protected function get_schema_links() { |
|
| 1103 | - |
|
| 1104 | - $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
| 1105 | - |
|
| 1106 | - $links = array(); |
|
| 1107 | - |
|
| 1108 | - $links[] = array( |
|
| 1109 | - 'rel' => 'https://api.w.org/action-publish', |
|
| 1110 | - 'title' => __( 'The current user can publish this discount.', 'invoicing' ), |
|
| 1111 | - 'href' => $href, |
|
| 1112 | - 'targetSchema' => array( |
|
| 1113 | - 'type' => 'object', |
|
| 1114 | - 'properties' => array( |
|
| 1115 | - 'status' => array( |
|
| 1116 | - 'type' => 'string', |
|
| 1117 | - 'enum' => array( 'publish', 'future' ), |
|
| 1118 | - ), |
|
| 1119 | - ), |
|
| 1120 | - ), |
|
| 1121 | - ); |
|
| 1122 | - |
|
| 1123 | - return $links; |
|
| 1124 | - } |
|
| 1125 | - |
|
| 1126 | - /** |
|
| 1127 | - * Prepares links for the request. |
|
| 1128 | - * |
|
| 1129 | - * @since 1.0.13 |
|
| 1130 | - * |
|
| 1131 | - * @param WP_Post $discount Post Object. |
|
| 1132 | - * @return array Links for the given discount. |
|
| 1133 | - */ |
|
| 1134 | - protected function prepare_links( $discount ) { |
|
| 1135 | - |
|
| 1136 | - // Prepare the base REST API endpoint for discounts. |
|
| 1137 | - $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
| 1138 | - |
|
| 1139 | - // Entity meta. |
|
| 1140 | - $links = array( |
|
| 1141 | - 'self' => array( |
|
| 1142 | - 'href' => rest_url( trailingslashit( $base ) . $discount->ID ), |
|
| 1143 | - ), |
|
| 1144 | - 'collection' => array( |
|
| 1145 | - 'href' => rest_url( $base ), |
|
| 1146 | - ), |
|
| 1147 | - ); |
|
| 1148 | - |
|
| 1149 | - /** |
|
| 1150 | - * Filters the returned discount links for the REST API. |
|
| 1151 | - * |
|
| 1152 | - * Enables adding extra links to discount API responses. |
|
| 1153 | - * |
|
| 1154 | - * @since 1.0.13 |
|
| 1155 | - * |
|
| 1156 | - * @param array $links Rest links. |
|
| 1157 | - */ |
|
| 1158 | - return apply_filters( "wpinv_rest_discount_links", $links ); |
|
| 1159 | - |
|
| 1160 | - } |
|
| 1161 | - |
|
| 1162 | - /** |
|
| 1163 | - * Get the link relations available for the post and current user. |
|
| 1164 | - * |
|
| 1165 | - * @since 1.0.13 |
|
| 1166 | - * |
|
| 1167 | - * @param WP_Post $discount WP_Post object. |
|
| 1168 | - * @param WP_REST_Request $request Request object. |
|
| 1169 | - * @return array List of link relations. |
|
| 1170 | - */ |
|
| 1171 | - protected function get_available_actions( $discount, $request ) { |
|
| 1172 | - |
|
| 1173 | - if ( 'edit' !== $request['context'] ) { |
|
| 1174 | - return array(); |
|
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - $rels = array(); |
|
| 1178 | - |
|
| 1179 | - // Retrieve the post type object. |
|
| 1180 | - $post_type = get_post_type_object( $discount->post_type ); |
|
| 1181 | - |
|
| 1182 | - // Mark discount as published. |
|
| 1183 | - if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
| 1184 | - $rels[] = 'https://api.w.org/action-publish'; |
|
| 1185 | - } |
|
| 1186 | - |
|
| 1187 | - /** |
|
| 1188 | - * Filters the available discount link relations for the REST API. |
|
| 1189 | - * |
|
| 1190 | - * Enables adding extra link relation for the current user and request to discount responses. |
|
| 1191 | - * |
|
| 1192 | - * @since 1.0.13 |
|
| 1193 | - * |
|
| 1194 | - * @param array $rels Available link relations. |
|
| 1195 | - */ |
|
| 1196 | - return apply_filters( "wpinv_rest_discount_link_relations", $rels ); |
|
| 1197 | - } |
|
| 1198 | - |
|
| 1199 | - /** |
|
| 1200 | - * Handles rest requests for discount types. |
|
| 1201 | - * |
|
| 1202 | - * @since 1.0.13 |
|
| 1203 | - * |
|
| 1204 | - * |
|
| 1205 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 1206 | - */ |
|
| 1207 | - public function get_discount_types() { |
|
| 1208 | - return rest_ensure_response( wpinv_get_discount_types() ); |
|
| 1209 | - } |
|
| 1092 | + return $this->add_additional_fields_schema( $this->schema ); |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + /** |
|
| 1096 | + * Retrieve Link Description Objects that should be added to the Schema for the discounts collection. |
|
| 1097 | + * |
|
| 1098 | + * @since 1.0.13 |
|
| 1099 | + * |
|
| 1100 | + * @return array |
|
| 1101 | + */ |
|
| 1102 | + protected function get_schema_links() { |
|
| 1103 | + |
|
| 1104 | + $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
| 1105 | + |
|
| 1106 | + $links = array(); |
|
| 1107 | + |
|
| 1108 | + $links[] = array( |
|
| 1109 | + 'rel' => 'https://api.w.org/action-publish', |
|
| 1110 | + 'title' => __( 'The current user can publish this discount.', 'invoicing' ), |
|
| 1111 | + 'href' => $href, |
|
| 1112 | + 'targetSchema' => array( |
|
| 1113 | + 'type' => 'object', |
|
| 1114 | + 'properties' => array( |
|
| 1115 | + 'status' => array( |
|
| 1116 | + 'type' => 'string', |
|
| 1117 | + 'enum' => array( 'publish', 'future' ), |
|
| 1118 | + ), |
|
| 1119 | + ), |
|
| 1120 | + ), |
|
| 1121 | + ); |
|
| 1122 | + |
|
| 1123 | + return $links; |
|
| 1124 | + } |
|
| 1125 | + |
|
| 1126 | + /** |
|
| 1127 | + * Prepares links for the request. |
|
| 1128 | + * |
|
| 1129 | + * @since 1.0.13 |
|
| 1130 | + * |
|
| 1131 | + * @param WP_Post $discount Post Object. |
|
| 1132 | + * @return array Links for the given discount. |
|
| 1133 | + */ |
|
| 1134 | + protected function prepare_links( $discount ) { |
|
| 1135 | + |
|
| 1136 | + // Prepare the base REST API endpoint for discounts. |
|
| 1137 | + $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
| 1138 | + |
|
| 1139 | + // Entity meta. |
|
| 1140 | + $links = array( |
|
| 1141 | + 'self' => array( |
|
| 1142 | + 'href' => rest_url( trailingslashit( $base ) . $discount->ID ), |
|
| 1143 | + ), |
|
| 1144 | + 'collection' => array( |
|
| 1145 | + 'href' => rest_url( $base ), |
|
| 1146 | + ), |
|
| 1147 | + ); |
|
| 1148 | + |
|
| 1149 | + /** |
|
| 1150 | + * Filters the returned discount links for the REST API. |
|
| 1151 | + * |
|
| 1152 | + * Enables adding extra links to discount API responses. |
|
| 1153 | + * |
|
| 1154 | + * @since 1.0.13 |
|
| 1155 | + * |
|
| 1156 | + * @param array $links Rest links. |
|
| 1157 | + */ |
|
| 1158 | + return apply_filters( "wpinv_rest_discount_links", $links ); |
|
| 1159 | + |
|
| 1160 | + } |
|
| 1161 | + |
|
| 1162 | + /** |
|
| 1163 | + * Get the link relations available for the post and current user. |
|
| 1164 | + * |
|
| 1165 | + * @since 1.0.13 |
|
| 1166 | + * |
|
| 1167 | + * @param WP_Post $discount WP_Post object. |
|
| 1168 | + * @param WP_REST_Request $request Request object. |
|
| 1169 | + * @return array List of link relations. |
|
| 1170 | + */ |
|
| 1171 | + protected function get_available_actions( $discount, $request ) { |
|
| 1172 | + |
|
| 1173 | + if ( 'edit' !== $request['context'] ) { |
|
| 1174 | + return array(); |
|
| 1175 | + } |
|
| 1176 | + |
|
| 1177 | + $rels = array(); |
|
| 1178 | + |
|
| 1179 | + // Retrieve the post type object. |
|
| 1180 | + $post_type = get_post_type_object( $discount->post_type ); |
|
| 1181 | + |
|
| 1182 | + // Mark discount as published. |
|
| 1183 | + if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
| 1184 | + $rels[] = 'https://api.w.org/action-publish'; |
|
| 1185 | + } |
|
| 1186 | + |
|
| 1187 | + /** |
|
| 1188 | + * Filters the available discount link relations for the REST API. |
|
| 1189 | + * |
|
| 1190 | + * Enables adding extra link relation for the current user and request to discount responses. |
|
| 1191 | + * |
|
| 1192 | + * @since 1.0.13 |
|
| 1193 | + * |
|
| 1194 | + * @param array $rels Available link relations. |
|
| 1195 | + */ |
|
| 1196 | + return apply_filters( "wpinv_rest_discount_link_relations", $rels ); |
|
| 1197 | + } |
|
| 1198 | + |
|
| 1199 | + /** |
|
| 1200 | + * Handles rest requests for discount types. |
|
| 1201 | + * |
|
| 1202 | + * @since 1.0.13 |
|
| 1203 | + * |
|
| 1204 | + * |
|
| 1205 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 1206 | + */ |
|
| 1207 | + public function get_discount_types() { |
|
| 1208 | + return rest_ensure_response( wpinv_get_discount_types() ); |
|
| 1209 | + } |
|
| 1210 | 1210 | |
| 1211 | 1211 | } |
| 1212 | 1212 | \ No newline at end of file |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | * @since 1.0.13 |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | -if ( !defined( 'WPINC' ) ) { |
|
| 11 | +if (!defined('WPINC')) { |
|
| 12 | 12 | exit; |
| 13 | 13 | } |
| 14 | 14 | |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | * |
| 42 | 42 | * @param string $namespace Api Namespace |
| 43 | 43 | */ |
| 44 | - public function __construct( $namespace ) { |
|
| 44 | + public function __construct($namespace) { |
|
| 45 | 45 | |
| 46 | 46 | // Set api namespace... |
| 47 | 47 | $this->namespace = $namespace; |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | array( |
| 69 | 69 | array( |
| 70 | 70 | 'methods' => WP_REST_Server::READABLE, |
| 71 | - 'callback' => array( $this, 'get_discount_types' ), |
|
| 71 | + 'callback' => array($this, 'get_discount_types'), |
|
| 72 | 72 | 'permission_callback' => '__return_true', |
| 73 | 73 | ), |
| 74 | 74 | ) |
@@ -85,13 +85,13 @@ discard block |
||
| 85 | 85 | * @param WP_REST_Request $request Full details about the request. |
| 86 | 86 | * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
| 87 | 87 | */ |
| 88 | - public function get_items_permissions_check( $request ) { |
|
| 88 | + public function get_items_permissions_check($request) { |
|
| 89 | 89 | |
| 90 | - if ( wpinv_current_user_can_manage_invoicing() ) { |
|
| 90 | + if (wpinv_current_user_can_manage_invoicing()) { |
|
| 91 | 91 | return true; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view invoice discounts.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 94 | + return new WP_Error('rest_forbidden_context', __('Sorry, you are not allowed to view invoice discounts.', 'invoicing'), array('status' => rest_authorization_required_code())); |
|
| 95 | 95 | |
| 96 | 96 | } |
| 97 | 97 | |
@@ -103,17 +103,17 @@ discard block |
||
| 103 | 103 | * @param WP_REST_Request $request Full details about the request. |
| 104 | 104 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 105 | 105 | */ |
| 106 | - public function get_items( $request ) { |
|
| 106 | + public function get_items($request) { |
|
| 107 | 107 | |
| 108 | 108 | // Retrieve the list of registered item query parameters. |
| 109 | 109 | $registered = $this->get_collection_params(); |
| 110 | 110 | |
| 111 | 111 | $args = array(); |
| 112 | 112 | |
| 113 | - foreach( array_keys( $registered ) as $key ) { |
|
| 113 | + foreach (array_keys($registered) as $key) { |
|
| 114 | 114 | |
| 115 | - if( isset( $request[ $key] ) ) { |
|
| 116 | - $args[ $key ] = $request[ $key]; |
|
| 115 | + if (isset($request[$key])) { |
|
| 116 | + $args[$key] = $request[$key]; |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | } |
@@ -128,28 +128,28 @@ discard block |
||
| 128 | 128 | * @param array $args Key value array of query var to query value. |
| 129 | 129 | * @param WP_REST_Request $request The request used. |
| 130 | 130 | */ |
| 131 | - $args = apply_filters( "wpinv_rest_get_discounts_arguments", $args, $request, $this ); |
|
| 131 | + $args = apply_filters("wpinv_rest_get_discounts_arguments", $args, $request, $this); |
|
| 132 | 132 | |
| 133 | 133 | // Special args |
| 134 | - $args[ 'return' ] = 'objects'; |
|
| 135 | - $args[ 'paginate' ] = true; |
|
| 134 | + $args['return'] = 'objects'; |
|
| 135 | + $args['paginate'] = true; |
|
| 136 | 136 | |
| 137 | 137 | // Run the query. |
| 138 | - $query = wpinv_get_all_discounts( $args ); |
|
| 138 | + $query = wpinv_get_all_discounts($args); |
|
| 139 | 139 | |
| 140 | 140 | // Prepare the retrieved discounts |
| 141 | 141 | $discounts = array(); |
| 142 | - foreach( $query->discounts as $discount ) { |
|
| 142 | + foreach ($query->discounts as $discount) { |
|
| 143 | 143 | |
| 144 | - $data = $this->prepare_item_for_response( $discount, $request ); |
|
| 145 | - $discounts[] = $this->prepare_response_for_collection( $data ); |
|
| 144 | + $data = $this->prepare_item_for_response($discount, $request); |
|
| 145 | + $discounts[] = $this->prepare_response_for_collection($data); |
|
| 146 | 146 | |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | // Prepare the response. |
| 150 | - $response = rest_ensure_response( $discounts ); |
|
| 151 | - $response->header( 'X-WP-Total', (int) $query->total ); |
|
| 152 | - $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages ); |
|
| 150 | + $response = rest_ensure_response($discounts); |
|
| 151 | + $response->header('X-WP-Total', (int) $query->total); |
|
| 152 | + $response->header('X-WP-TotalPages', (int) $query->max_num_pages); |
|
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | 155 | * Filters the responses for discount requests. |
@@ -162,9 +162,9 @@ discard block |
||
| 162 | 162 | * @param WP_REST_Request $request The request used. |
| 163 | 163 | * @param array $args Array of args used to retrieve the discounts |
| 164 | 164 | */ |
| 165 | - $response = apply_filters( "wpinv_rest_discounts_response", $response, $request, $args ); |
|
| 165 | + $response = apply_filters("wpinv_rest_discounts_response", $response, $request, $args); |
|
| 166 | 166 | |
| 167 | - return rest_ensure_response( $response ); |
|
| 167 | + return rest_ensure_response($response); |
|
| 168 | 168 | |
| 169 | 169 | } |
| 170 | 170 | |
@@ -176,17 +176,17 @@ discard block |
||
| 176 | 176 | * @param int $discount_id Supplied ID. |
| 177 | 177 | * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. |
| 178 | 178 | */ |
| 179 | - protected function get_post( $discount_id ) { |
|
| 179 | + protected function get_post($discount_id) { |
|
| 180 | 180 | |
| 181 | - $error = new WP_Error( 'rest_item_invalid_id', __( 'Invalid discount ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
| 181 | + $error = new WP_Error('rest_item_invalid_id', __('Invalid discount ID.', 'invoicing'), array('status' => 404)); |
|
| 182 | 182 | |
| 183 | 183 | // Ids start from 1 |
| 184 | - if ( (int) $discount_id <= 0 ) { |
|
| 184 | + if ((int) $discount_id <= 0) { |
|
| 185 | 185 | return $error; |
| 186 | 186 | } |
| 187 | 187 | |
| 188 | - $discount = wpinv_get_discount( (int) $discount_id ); |
|
| 189 | - if ( empty( $discount ) ) { |
|
| 188 | + $discount = wpinv_get_discount((int) $discount_id); |
|
| 189 | + if (empty($discount)) { |
|
| 190 | 190 | return $error; |
| 191 | 191 | } |
| 192 | 192 | |
@@ -202,20 +202,20 @@ discard block |
||
| 202 | 202 | * @param WP_REST_Request $request Full details about the request. |
| 203 | 203 | * @return bool|WP_Error True if the request has read access for the invoice item, WP_Error object otherwise. |
| 204 | 204 | */ |
| 205 | - public function get_item_permissions_check( $request ) { |
|
| 205 | + public function get_item_permissions_check($request) { |
|
| 206 | 206 | |
| 207 | 207 | // Retrieve the discount object. |
| 208 | - $discount = $this->get_post( $request['id'] ); |
|
| 208 | + $discount = $this->get_post($request['id']); |
|
| 209 | 209 | |
| 210 | 210 | // Ensure it is valid. |
| 211 | - if ( is_wp_error( $discount ) ) { |
|
| 211 | + if (is_wp_error($discount)) { |
|
| 212 | 212 | return $discount; |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 215 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
| 216 | 216 | return new WP_Error( |
| 217 | 217 | 'rest_cannot_view', |
| 218 | - __( 'Sorry, you are not allowed to view this discount.', 'invoicing' ), |
|
| 218 | + __('Sorry, you are not allowed to view this discount.', 'invoicing'), |
|
| 219 | 219 | array( |
| 220 | 220 | 'status' => rest_authorization_required_code(), |
| 221 | 221 | ) |
@@ -233,18 +233,18 @@ discard block |
||
| 233 | 233 | * @param WP_REST_Request $request Full details about the request. |
| 234 | 234 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 235 | 235 | */ |
| 236 | - public function get_item( $request ) { |
|
| 236 | + public function get_item($request) { |
|
| 237 | 237 | |
| 238 | 238 | // Fetch the discount. |
| 239 | - $discount = $this->get_post( $request['id'] ); |
|
| 239 | + $discount = $this->get_post($request['id']); |
|
| 240 | 240 | |
| 241 | 241 | // Abort early if it does not exist |
| 242 | - if ( is_wp_error( $discount ) ) { |
|
| 242 | + if (is_wp_error($discount)) { |
|
| 243 | 243 | return $discount; |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | // Prepare the response |
| 247 | - $response = $this->prepare_item_for_response( $discount, $request ); |
|
| 247 | + $response = $this->prepare_item_for_response($discount, $request); |
|
| 248 | 248 | |
| 249 | 249 | /** |
| 250 | 250 | * Filters the responses for single discount requests. |
@@ -256,9 +256,9 @@ discard block |
||
| 256 | 256 | * @param WP_HTTP_Response $response Response. |
| 257 | 257 | * @param WP_REST_Request $request The request used. |
| 258 | 258 | */ |
| 259 | - $response = apply_filters( "wpinv_rest_get_discount_response", $response, $request ); |
|
| 259 | + $response = apply_filters("wpinv_rest_get_discount_response", $response, $request); |
|
| 260 | 260 | |
| 261 | - return rest_ensure_response( $response ); |
|
| 261 | + return rest_ensure_response($response); |
|
| 262 | 262 | |
| 263 | 263 | } |
| 264 | 264 | |
@@ -270,21 +270,21 @@ discard block |
||
| 270 | 270 | * @param WP_REST_Request $request Full details about the request. |
| 271 | 271 | * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 272 | 272 | */ |
| 273 | - public function create_item_permissions_check( $request ) { |
|
| 273 | + public function create_item_permissions_check($request) { |
|
| 274 | 274 | |
| 275 | - if ( ! empty( $request['id'] ) ) { |
|
| 276 | - return new WP_Error( 'rest_item_exists', __( 'Cannot create existing item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 275 | + if (!empty($request['id'])) { |
|
| 276 | + return new WP_Error('rest_item_exists', __('Cannot create existing item.', 'invoicing'), array('status' => 400)); |
|
| 277 | 277 | } |
| 278 | 278 | |
| 279 | - if ( wpinv_current_user_can_manage_invoicing() ) { |
|
| 279 | + if (wpinv_current_user_can_manage_invoicing()) { |
|
| 280 | 280 | return true; |
| 281 | 281 | } |
| 282 | 282 | |
| 283 | - $post_type = get_post_type_object( $this->post_type ); |
|
| 284 | - if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
| 283 | + $post_type = get_post_type_object($this->post_type); |
|
| 284 | + if (!current_user_can($post_type->cap->create_posts)) { |
|
| 285 | 285 | return new WP_Error( |
| 286 | 286 | 'rest_cannot_create', |
| 287 | - __( 'Sorry, you are not allowed to create discounts as this user.', 'invoicing' ), |
|
| 287 | + __('Sorry, you are not allowed to create discounts as this user.', 'invoicing'), |
|
| 288 | 288 | array( |
| 289 | 289 | 'status' => rest_authorization_required_code(), |
| 290 | 290 | ) |
@@ -302,32 +302,32 @@ discard block |
||
| 302 | 302 | * @param WP_REST_Request $request Full details about the request. |
| 303 | 303 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 304 | 304 | */ |
| 305 | - public function create_item( $request ) { |
|
| 305 | + public function create_item($request) { |
|
| 306 | 306 | |
| 307 | - if ( ! empty( $request['id'] ) ) { |
|
| 308 | - return new WP_Error( 'rest_item_exists', __( 'Cannot create existing discount.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 307 | + if (!empty($request['id'])) { |
|
| 308 | + return new WP_Error('rest_item_exists', __('Cannot create existing discount.', 'invoicing'), array('status' => 400)); |
|
| 309 | 309 | } |
| 310 | 310 | |
| 311 | - $request->set_param( 'context', 'edit' ); |
|
| 311 | + $request->set_param('context', 'edit'); |
|
| 312 | 312 | |
| 313 | 313 | // Prepare the updated data. |
| 314 | - $discount_data = $this->prepare_item_for_database( $request ); |
|
| 314 | + $discount_data = $this->prepare_item_for_database($request); |
|
| 315 | 315 | |
| 316 | - if ( is_wp_error( $discount_data ) ) { |
|
| 316 | + if (is_wp_error($discount_data)) { |
|
| 317 | 317 | return $discount_data; |
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | $discount_data['post_type'] = $this->post_type; |
| 321 | 321 | |
| 322 | 322 | // Try creating the discount. |
| 323 | - $discount = wp_insert_post( $discount_data, true ); |
|
| 323 | + $discount = wp_insert_post($discount_data, true); |
|
| 324 | 324 | |
| 325 | - if ( is_wp_error( $discount ) ) { |
|
| 325 | + if (is_wp_error($discount)) { |
|
| 326 | 326 | return $discount; |
| 327 | 327 | } |
| 328 | 328 | |
| 329 | 329 | // Prepare the response |
| 330 | - $response = $this->prepare_item_for_response( $discount, $request ); |
|
| 330 | + $response = $this->prepare_item_for_response($discount, $request); |
|
| 331 | 331 | |
| 332 | 332 | /** |
| 333 | 333 | * Fires after a single discount is created or updated via the REST API. |
@@ -338,7 +338,7 @@ discard block |
||
| 338 | 338 | * @param WP_REST_Request $request Request object. |
| 339 | 339 | * @param bool $creating True when creating a post, false when updating. |
| 340 | 340 | */ |
| 341 | - do_action( "wpinv_rest_insert_discount", $discount, $request, true ); |
|
| 341 | + do_action("wpinv_rest_insert_discount", $discount, $request, true); |
|
| 342 | 342 | |
| 343 | 343 | /** |
| 344 | 344 | * Filters the responses for creating single item requests. |
@@ -350,9 +350,9 @@ discard block |
||
| 350 | 350 | * @param array $response Invoice properties. |
| 351 | 351 | * @param WP_REST_Request $request The request used. |
| 352 | 352 | */ |
| 353 | - $response = apply_filters( "wpinv_rest_create_discount_response", $response, $request ); |
|
| 353 | + $response = apply_filters("wpinv_rest_create_discount_response", $response, $request); |
|
| 354 | 354 | |
| 355 | - return rest_ensure_response( $response ); |
|
| 355 | + return rest_ensure_response($response); |
|
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | /** |
@@ -363,21 +363,21 @@ discard block |
||
| 363 | 363 | * @param WP_REST_Request $request Full details about the request. |
| 364 | 364 | * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
| 365 | 365 | */ |
| 366 | - public function update_item_permissions_check( $request ) { |
|
| 366 | + public function update_item_permissions_check($request) { |
|
| 367 | 367 | |
| 368 | 368 | // Retrieve the item. |
| 369 | - $item = $this->get_post( $request['id'] ); |
|
| 370 | - if ( is_wp_error( $item ) ) { |
|
| 369 | + $item = $this->get_post($request['id']); |
|
| 370 | + if (is_wp_error($item)) { |
|
| 371 | 371 | return $item; |
| 372 | 372 | } |
| 373 | 373 | |
| 374 | - if ( wpinv_current_user_can_manage_invoicing() ) { |
|
| 374 | + if (wpinv_current_user_can_manage_invoicing()) { |
|
| 375 | 375 | return true; |
| 376 | 376 | } |
| 377 | 377 | |
| 378 | 378 | return new WP_Error( |
| 379 | 379 | 'rest_cannot_edit', |
| 380 | - __( 'Sorry, you are not allowed to update this discount.', 'invoicing' ), |
|
| 380 | + __('Sorry, you are not allowed to update this discount.', 'invoicing'), |
|
| 381 | 381 | array( |
| 382 | 382 | 'status' => rest_authorization_required_code(), |
| 383 | 383 | ) |
@@ -393,62 +393,62 @@ discard block |
||
| 393 | 393 | * @param WP_REST_Request $request Full details about the request. |
| 394 | 394 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 395 | 395 | */ |
| 396 | - public function update_item( $request ) { |
|
| 396 | + public function update_item($request) { |
|
| 397 | 397 | |
| 398 | 398 | // Ensure the item exists. |
| 399 | - $valid_check = $this->get_post( $request['id'] ); |
|
| 399 | + $valid_check = $this->get_post($request['id']); |
|
| 400 | 400 | |
| 401 | 401 | // Abort early if it does not exist |
| 402 | - if ( is_wp_error( $valid_check ) ) { |
|
| 402 | + if (is_wp_error($valid_check)) { |
|
| 403 | 403 | return $valid_check; |
| 404 | 404 | } |
| 405 | 405 | |
| 406 | - $request->set_param( 'context', 'edit' ); |
|
| 406 | + $request->set_param('context', 'edit'); |
|
| 407 | 407 | |
| 408 | 408 | // Prepare the updated data. |
| 409 | - $data_to_update = $this->prepare_item_for_database( $request ); |
|
| 409 | + $data_to_update = $this->prepare_item_for_database($request); |
|
| 410 | 410 | |
| 411 | - if ( is_wp_error( $data_to_update ) ) { |
|
| 411 | + if (is_wp_error($data_to_update)) { |
|
| 412 | 412 | return $data_to_update; |
| 413 | 413 | } |
| 414 | 414 | |
| 415 | - if( empty( $data_to_update['meta_input'] ) ) { |
|
| 416 | - unset( $data_to_update['meta_input'] ); |
|
| 415 | + if (empty($data_to_update['meta_input'])) { |
|
| 416 | + unset($data_to_update['meta_input']); |
|
| 417 | 417 | } |
| 418 | 418 | |
| 419 | 419 | // Abort if no item data is provided |
| 420 | - if( empty( $data_to_update ) ) { |
|
| 421 | - return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) ); |
|
| 420 | + if (empty($data_to_update)) { |
|
| 421 | + return new WP_Error('missing_data', __('An update request cannot be empty.', 'invoicing')); |
|
| 422 | 422 | } |
| 423 | 423 | |
| 424 | 424 | // post_status |
| 425 | - if( ! empty( $data_to_update['post_status'] ) ) { |
|
| 426 | - wpinv_update_discount_status( $request['id'], $data_to_update['post_status'] ); |
|
| 427 | - unset( $data_to_update['post_status'] ); |
|
| 425 | + if (!empty($data_to_update['post_status'])) { |
|
| 426 | + wpinv_update_discount_status($request['id'], $data_to_update['post_status']); |
|
| 427 | + unset($data_to_update['post_status']); |
|
| 428 | 428 | } |
| 429 | 429 | |
| 430 | 430 | // Update the item |
| 431 | - if( ! empty( $data_to_update ) ) { |
|
| 431 | + if (!empty($data_to_update)) { |
|
| 432 | 432 | |
| 433 | 433 | // Include the item ID |
| 434 | 434 | $data_to_update['ID'] = $request['id']; |
| 435 | 435 | |
| 436 | - $updated = wp_update_post( $data_to_update, true ); |
|
| 436 | + $updated = wp_update_post($data_to_update, true); |
|
| 437 | 437 | |
| 438 | 438 | // Incase the update operation failed... |
| 439 | - if ( is_wp_error( $updated ) ) { |
|
| 439 | + if (is_wp_error($updated)) { |
|
| 440 | 440 | return $updated; |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | 443 | } |
| 444 | 444 | |
| 445 | - $updated_discount = get_post( $request['id'] ); |
|
| 445 | + $updated_discount = get_post($request['id']); |
|
| 446 | 446 | |
| 447 | 447 | // Prepare the response |
| 448 | - $response = $this->prepare_item_for_response( $updated_discount, $request ); |
|
| 448 | + $response = $this->prepare_item_for_response($updated_discount, $request); |
|
| 449 | 449 | |
| 450 | 450 | /** This action is documented in includes/class-wpinv-rest-item-controller.php */ |
| 451 | - do_action( "wpinv_rest_insert_discount", $updated_discount, $request, false ); |
|
| 451 | + do_action("wpinv_rest_insert_discount", $updated_discount, $request, false); |
|
| 452 | 452 | |
| 453 | 453 | /** |
| 454 | 454 | * Filters the responses for updating single discount requests. |
@@ -460,9 +460,9 @@ discard block |
||
| 460 | 460 | * @param array $data_to_update Discount properties. |
| 461 | 461 | * @param WP_REST_Request $request The request used. |
| 462 | 462 | */ |
| 463 | - $response = apply_filters( "wpinv_rest_update_discount_response", $response, $data_to_update, $request ); |
|
| 463 | + $response = apply_filters("wpinv_rest_update_discount_response", $response, $data_to_update, $request); |
|
| 464 | 464 | |
| 465 | - return rest_ensure_response( $response ); |
|
| 465 | + return rest_ensure_response($response); |
|
| 466 | 466 | } |
| 467 | 467 | |
| 468 | 468 | /** |
@@ -473,19 +473,19 @@ discard block |
||
| 473 | 473 | * @param WP_REST_Request $request Full details about the request. |
| 474 | 474 | * @return true|WP_Error True if the request has access to delete the discount, WP_Error object otherwise. |
| 475 | 475 | */ |
| 476 | - public function delete_item_permissions_check( $request ) { |
|
| 476 | + public function delete_item_permissions_check($request) { |
|
| 477 | 477 | |
| 478 | 478 | // Retrieve the discount. |
| 479 | - $discount = $this->get_post( $request['id'] ); |
|
| 480 | - if ( is_wp_error( $discount ) ) { |
|
| 479 | + $discount = $this->get_post($request['id']); |
|
| 480 | + if (is_wp_error($discount)) { |
|
| 481 | 481 | return $discount; |
| 482 | 482 | } |
| 483 | 483 | |
| 484 | 484 | // Ensure the current user can delete the discount |
| 485 | - if (! wpinv_current_user_can_manage_invoicing() ) { |
|
| 485 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
| 486 | 486 | return new WP_Error( |
| 487 | 487 | 'rest_cannot_delete', |
| 488 | - __( 'Sorry, you are not allowed to delete this discount.', 'invoicing' ), |
|
| 488 | + __('Sorry, you are not allowed to delete this discount.', 'invoicing'), |
|
| 489 | 489 | array( |
| 490 | 490 | 'status' => rest_authorization_required_code(), |
| 491 | 491 | ) |
@@ -503,24 +503,24 @@ discard block |
||
| 503 | 503 | * @param WP_REST_Request $request Full details about the request. |
| 504 | 504 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 505 | 505 | */ |
| 506 | - public function delete_item( $request ) { |
|
| 506 | + public function delete_item($request) { |
|
| 507 | 507 | |
| 508 | 508 | // Retrieve the discount. |
| 509 | - $discount = $this->get_post( $request['id'] ); |
|
| 510 | - if ( is_wp_error( $discount ) ) { |
|
| 509 | + $discount = $this->get_post($request['id']); |
|
| 510 | + if (is_wp_error($discount)) { |
|
| 511 | 511 | return $discount; |
| 512 | 512 | } |
| 513 | 513 | |
| 514 | - $request->set_param( 'context', 'edit' ); |
|
| 514 | + $request->set_param('context', 'edit'); |
|
| 515 | 515 | |
| 516 | 516 | // Prepare the discount id |
| 517 | - $id = $discount->ID; |
|
| 517 | + $id = $discount->ID; |
|
| 518 | 518 | |
| 519 | 519 | // Prepare the response |
| 520 | - $response = $this->prepare_item_for_response( $discount, $request ); |
|
| 520 | + $response = $this->prepare_item_for_response($discount, $request); |
|
| 521 | 521 | |
| 522 | 522 | // Delete the discount... |
| 523 | - wpinv_remove_discount( $id ); |
|
| 523 | + wpinv_remove_discount($id); |
|
| 524 | 524 | |
| 525 | 525 | /** |
| 526 | 526 | * Fires immediately after a single discount is deleted via the REST API. |
@@ -531,7 +531,7 @@ discard block |
||
| 531 | 531 | * @param WP_POST $discount The deleted discount. |
| 532 | 532 | * @param WP_REST_Request $request The request sent to the API. |
| 533 | 533 | */ |
| 534 | - do_action( "wpinv_rest_delete_discount", $discount, $request ); |
|
| 534 | + do_action("wpinv_rest_delete_discount", $discount, $request); |
|
| 535 | 535 | |
| 536 | 536 | return $response; |
| 537 | 537 | |
@@ -547,44 +547,44 @@ discard block |
||
| 547 | 547 | */ |
| 548 | 548 | public function get_collection_params() { |
| 549 | 549 | |
| 550 | - $query_params = array( |
|
| 550 | + $query_params = array( |
|
| 551 | 551 | |
| 552 | 552 | // Discount status. |
| 553 | 553 | 'status' => array( |
| 554 | 554 | 'default' => 'publish', |
| 555 | - 'description' => __( 'Limit result set to discounts assigned one or more statuses.', 'invoicing' ), |
|
| 555 | + 'description' => __('Limit result set to discounts assigned one or more statuses.', 'invoicing'), |
|
| 556 | 556 | 'type' => 'array', |
| 557 | - 'sanitize_callback' => array( $this, 'sanitize_post_statuses' ), |
|
| 557 | + 'sanitize_callback' => array($this, 'sanitize_post_statuses'), |
|
| 558 | 558 | ), |
| 559 | 559 | |
| 560 | 560 | // Discount types |
| 561 | 561 | 'type' => array( |
| 562 | - 'description' => __( 'Type of discounts to fetch.', 'invoicing' ), |
|
| 562 | + 'description' => __('Type of discounts to fetch.', 'invoicing'), |
|
| 563 | 563 | 'type' => 'array', |
| 564 | - 'default' => array_keys( wpinv_get_discount_types() ), |
|
| 564 | + 'default' => array_keys(wpinv_get_discount_types()), |
|
| 565 | 565 | 'items' => array( |
| 566 | - 'enum' => array_keys( wpinv_get_discount_types() ), |
|
| 566 | + 'enum' => array_keys(wpinv_get_discount_types()), |
|
| 567 | 567 | 'type' => 'string', |
| 568 | 568 | ), |
| 569 | 569 | ), |
| 570 | 570 | |
| 571 | 571 | // Number of results per page |
| 572 | 572 | 'limit' => array( |
| 573 | - 'description' => __( 'Number of discounts to fetch.', 'invoicing' ), |
|
| 573 | + 'description' => __('Number of discounts to fetch.', 'invoicing'), |
|
| 574 | 574 | 'type' => 'integer', |
| 575 | - 'default' => (int) get_option( 'posts_per_page' ), |
|
| 575 | + 'default' => (int) get_option('posts_per_page'), |
|
| 576 | 576 | ), |
| 577 | 577 | |
| 578 | 578 | // Pagination |
| 579 | 579 | 'page' => array( |
| 580 | - 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
| 580 | + 'description' => __('Current page to fetch.', 'invoicing'), |
|
| 581 | 581 | 'type' => 'integer', |
| 582 | 582 | 'default' => 1, |
| 583 | 583 | ), |
| 584 | 584 | |
| 585 | 585 | // Exclude certain items |
| 586 | 586 | 'exclude' => array( |
| 587 | - 'description' => __( 'Ensure result set excludes specific IDs.', 'invoicing' ), |
|
| 587 | + 'description' => __('Ensure result set excludes specific IDs.', 'invoicing'), |
|
| 588 | 588 | 'type' => 'array', |
| 589 | 589 | 'items' => array( |
| 590 | 590 | 'type' => 'integer', |
@@ -594,7 +594,7 @@ discard block |
||
| 594 | 594 | |
| 595 | 595 | // Order discounts by |
| 596 | 596 | 'orderby' => array( |
| 597 | - 'description' => __( 'Sort discounts by object attribute.', 'invoicing' ), |
|
| 597 | + 'description' => __('Sort discounts by object attribute.', 'invoicing'), |
|
| 598 | 598 | 'type' => 'string', |
| 599 | 599 | 'default' => 'date', |
| 600 | 600 | 'enum' => array( |
@@ -610,15 +610,15 @@ discard block |
||
| 610 | 610 | |
| 611 | 611 | // How to order |
| 612 | 612 | 'order' => array( |
| 613 | - 'description' => __( 'Order sort attribute ascending or descending.', 'invoicing' ), |
|
| 613 | + 'description' => __('Order sort attribute ascending or descending.', 'invoicing'), |
|
| 614 | 614 | 'type' => 'string', |
| 615 | 615 | 'default' => 'DESC', |
| 616 | - 'enum' => array( 'ASC', 'DESC' ), |
|
| 616 | + 'enum' => array('ASC', 'DESC'), |
|
| 617 | 617 | ), |
| 618 | 618 | |
| 619 | 619 | // Search term |
| 620 | 620 | 'search' => array( |
| 621 | - 'description' => __( 'Return discounts that match the search term.', 'invoicing' ), |
|
| 621 | + 'description' => __('Return discounts that match the search term.', 'invoicing'), |
|
| 622 | 622 | 'type' => 'string', |
| 623 | 623 | ), |
| 624 | 624 | ); |
@@ -631,7 +631,7 @@ discard block |
||
| 631 | 631 | * |
| 632 | 632 | * @param array $query_params JSON Schema-formatted collection parameters. |
| 633 | 633 | */ |
| 634 | - return apply_filters( "wpinv_rest_discounts_collection_params", $query_params ); |
|
| 634 | + return apply_filters("wpinv_rest_discounts_collection_params", $query_params); |
|
| 635 | 635 | } |
| 636 | 636 | |
| 637 | 637 | /** |
@@ -642,7 +642,7 @@ discard block |
||
| 642 | 642 | * @param object|string $post_type Post type name or object. |
| 643 | 643 | * @return bool Whether the post type is allowed in REST. |
| 644 | 644 | */ |
| 645 | - protected function check_is_post_type_allowed( $post_type ) { |
|
| 645 | + protected function check_is_post_type_allowed($post_type) { |
|
| 646 | 646 | return true; |
| 647 | 647 | } |
| 648 | 648 | |
@@ -654,93 +654,93 @@ discard block |
||
| 654 | 654 | * @param WP_REST_Request $request Request object. |
| 655 | 655 | * @return array|WP_Error Discount Properties or WP_Error. |
| 656 | 656 | */ |
| 657 | - protected function prepare_item_for_database( $request ) { |
|
| 658 | - $prepared_item = new stdClass(); |
|
| 657 | + protected function prepare_item_for_database($request) { |
|
| 658 | + $prepared_item = new stdClass(); |
|
| 659 | 659 | $prepared_item->meta_input = array(); |
| 660 | 660 | |
| 661 | 661 | // Post ID. |
| 662 | - if ( isset( $request['id'] ) ) { |
|
| 663 | - $existing_item = $this->get_post( $request['id'] ); |
|
| 664 | - if ( is_wp_error( $existing_item ) ) { |
|
| 662 | + if (isset($request['id'])) { |
|
| 663 | + $existing_item = $this->get_post($request['id']); |
|
| 664 | + if (is_wp_error($existing_item)) { |
|
| 665 | 665 | return $existing_item; |
| 666 | 666 | } |
| 667 | 667 | |
| 668 | - $prepared_item->ID = $existing_item->ID; |
|
| 668 | + $prepared_item->ID = $existing_item->ID; |
|
| 669 | 669 | } |
| 670 | 670 | |
| 671 | 671 | $schema = $this->get_item_schema(); |
| 672 | 672 | |
| 673 | 673 | // item title. |
| 674 | - if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) { |
|
| 675 | - $prepared_item->post_title = sanitize_text_field( $request['title'] ); |
|
| 674 | + if (!empty($schema['properties']['title']) && isset($request['title'])) { |
|
| 675 | + $prepared_item->post_title = sanitize_text_field($request['title']); |
|
| 676 | 676 | } |
| 677 | 677 | |
| 678 | 678 | // item status. |
| 679 | - if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) && in_array( $request['status'], array_keys( get_post_stati( array( 'internal' => false ) ) ) ) ) { |
|
| 680 | - $prepared_item->post_status = sanitize_text_field( $request['status'] ); |
|
| 679 | + if (!empty($schema['properties']['status']) && isset($request['status']) && in_array($request['status'], array_keys(get_post_stati(array('internal' => false))))) { |
|
| 680 | + $prepared_item->post_status = sanitize_text_field($request['status']); |
|
| 681 | 681 | } |
| 682 | 682 | |
| 683 | 683 | // Code. |
| 684 | - if ( ! empty( $schema['properties']['code'] ) && isset( $request['code'] ) ) { |
|
| 685 | - $prepared_item->meta_input['_wpi_discount_code'] = trim( $request['code'] ); |
|
| 684 | + if (!empty($schema['properties']['code']) && isset($request['code'])) { |
|
| 685 | + $prepared_item->meta_input['_wpi_discount_code'] = trim($request['code']); |
|
| 686 | 686 | } |
| 687 | 687 | |
| 688 | 688 | // Type. |
| 689 | - if ( ! empty( $schema['properties']['type'] ) && isset( $request['type'] ) && in_array( $request['type'], array_keys( wpinv_get_discount_types() ) ) ) { |
|
| 690 | - $prepared_item->meta_input['_wpi_discount_type'] = trim( $request['type'] ); |
|
| 689 | + if (!empty($schema['properties']['type']) && isset($request['type']) && in_array($request['type'], array_keys(wpinv_get_discount_types()))) { |
|
| 690 | + $prepared_item->meta_input['_wpi_discount_type'] = trim($request['type']); |
|
| 691 | 691 | } |
| 692 | 692 | |
| 693 | 693 | // Amount. |
| 694 | - if ( ! empty( $schema['properties']['amount'] ) && isset( $request['amount'] ) ) { |
|
| 695 | - $prepared_item->meta_input['_wpi_discount_amount'] = floatval( $request['amount'] ); |
|
| 694 | + if (!empty($schema['properties']['amount']) && isset($request['amount'])) { |
|
| 695 | + $prepared_item->meta_input['_wpi_discount_amount'] = floatval($request['amount']); |
|
| 696 | 696 | } |
| 697 | 697 | |
| 698 | 698 | // Items. |
| 699 | - if ( ! empty( $schema['properties']['items'] ) && isset( $request['items'] ) ) { |
|
| 700 | - $prepared_item->meta_input['_wpi_discount_items'] = wpinv_parse_list( $request['items'] ); |
|
| 699 | + if (!empty($schema['properties']['items']) && isset($request['items'])) { |
|
| 700 | + $prepared_item->meta_input['_wpi_discount_items'] = wpinv_parse_list($request['items']); |
|
| 701 | 701 | } |
| 702 | 702 | |
| 703 | 703 | // Excluded Items. |
| 704 | - if ( ! empty( $schema['properties']['exclude_items'] ) && isset( $request['exclude_items'] ) ) { |
|
| 705 | - $prepared_item->meta_input['_wpi_discount_excluded_items'] = wpinv_parse_list( $request['exclude_items'] ); |
|
| 704 | + if (!empty($schema['properties']['exclude_items']) && isset($request['exclude_items'])) { |
|
| 705 | + $prepared_item->meta_input['_wpi_discount_excluded_items'] = wpinv_parse_list($request['exclude_items']); |
|
| 706 | 706 | } |
| 707 | 707 | |
| 708 | 708 | // Start date. |
| 709 | - if ( ! empty( $schema['properties']['start_date'] ) && isset( $request['start_date'] ) ) { |
|
| 710 | - $prepared_item->meta_input['_wpi_discount_start'] = trim( $request['start_date'] ); |
|
| 709 | + if (!empty($schema['properties']['start_date']) && isset($request['start_date'])) { |
|
| 710 | + $prepared_item->meta_input['_wpi_discount_start'] = trim($request['start_date']); |
|
| 711 | 711 | } |
| 712 | 712 | |
| 713 | 713 | // End date. |
| 714 | - if ( ! empty( $schema['properties']['end_date'] ) && isset( $request['end_date'] ) ) { |
|
| 715 | - $prepared_item->meta_input['_wpi_discount_expiration'] = trim( $request['end_date'] ); |
|
| 714 | + if (!empty($schema['properties']['end_date']) && isset($request['end_date'])) { |
|
| 715 | + $prepared_item->meta_input['_wpi_discount_expiration'] = trim($request['end_date']); |
|
| 716 | 716 | } |
| 717 | 717 | |
| 718 | 718 | // Minimum amount. |
| 719 | - if ( ! empty( $schema['properties']['minimum_amount'] ) && isset( $request['minimum_amount'] ) ) { |
|
| 720 | - $prepared_item->meta_input['_wpi_discount_min_total'] = floatval( $request['minimum_amount'] ); |
|
| 719 | + if (!empty($schema['properties']['minimum_amount']) && isset($request['minimum_amount'])) { |
|
| 720 | + $prepared_item->meta_input['_wpi_discount_min_total'] = floatval($request['minimum_amount']); |
|
| 721 | 721 | } |
| 722 | 722 | |
| 723 | 723 | // Maximum amount. |
| 724 | - if ( ! empty( $schema['properties']['maximum_amount'] ) && isset( $request['maximum_amount'] ) ) { |
|
| 725 | - $prepared_item->meta_input['_wpi_discount_max_total'] = floatval( $request['maximum_amount'] ); |
|
| 724 | + if (!empty($schema['properties']['maximum_amount']) && isset($request['maximum_amount'])) { |
|
| 725 | + $prepared_item->meta_input['_wpi_discount_max_total'] = floatval($request['maximum_amount']); |
|
| 726 | 726 | } |
| 727 | 727 | |
| 728 | 728 | // Recurring. |
| 729 | - if ( ! empty( $schema['properties']['recurring'] ) && isset( $request['recurring'] ) ) { |
|
| 730 | - $prepared_item->meta_input['_wpi_discount_is_recurring'] = empty( (int) $request['recurring'] ) ? 0 : 1; |
|
| 729 | + if (!empty($schema['properties']['recurring']) && isset($request['recurring'])) { |
|
| 730 | + $prepared_item->meta_input['_wpi_discount_is_recurring'] = empty((int) $request['recurring']) ? 0 : 1; |
|
| 731 | 731 | } |
| 732 | 732 | |
| 733 | 733 | // Maximum uses. |
| 734 | - if ( ! empty( $schema['properties']['max_uses'] ) && isset( $request['max_uses'] ) ) { |
|
| 735 | - $prepared_item->meta_input['_wpi_discount_max_uses'] = intval( $request['max_uses'] ); |
|
| 734 | + if (!empty($schema['properties']['max_uses']) && isset($request['max_uses'])) { |
|
| 735 | + $prepared_item->meta_input['_wpi_discount_max_uses'] = intval($request['max_uses']); |
|
| 736 | 736 | } |
| 737 | 737 | |
| 738 | 738 | // Single use. |
| 739 | - if ( ! empty( $schema['properties']['single_use'] ) && isset( $request['single_use'] ) ) { |
|
| 740 | - $prepared_item->meta_input['_wpi_discount_is_single_use'] = empty( (int) $request['single_use'] ) ? 0 : 1; |
|
| 739 | + if (!empty($schema['properties']['single_use']) && isset($request['single_use'])) { |
|
| 740 | + $prepared_item->meta_input['_wpi_discount_is_single_use'] = empty((int) $request['single_use']) ? 0 : 1; |
|
| 741 | 741 | } |
| 742 | 742 | |
| 743 | - $discount_data = (array) wp_unslash( $prepared_item ); |
|
| 743 | + $discount_data = (array) wp_unslash($prepared_item); |
|
| 744 | 744 | |
| 745 | 745 | /** |
| 746 | 746 | * Filters an item before it is inserted via the REST API. |
@@ -750,7 +750,7 @@ discard block |
||
| 750 | 750 | * @param array $discount_data An array of discount data |
| 751 | 751 | * @param WP_REST_Request $request Request object. |
| 752 | 752 | */ |
| 753 | - return apply_filters( "wpinv_rest_pre_insert_discount", $discount_data, $request ); |
|
| 753 | + return apply_filters("wpinv_rest_pre_insert_discount", $discount_data, $request); |
|
| 754 | 754 | |
| 755 | 755 | } |
| 756 | 756 | |
@@ -763,110 +763,110 @@ discard block |
||
| 763 | 763 | * @param WP_REST_Request $request Request object. |
| 764 | 764 | * @return WP_REST_Response Response object. |
| 765 | 765 | */ |
| 766 | - public function prepare_item_for_response( $discount, $request ) { |
|
| 766 | + public function prepare_item_for_response($discount, $request) { |
|
| 767 | 767 | |
| 768 | - $GLOBALS['post'] = get_post( $discount->ID ); |
|
| 768 | + $GLOBALS['post'] = get_post($discount->ID); |
|
| 769 | 769 | |
| 770 | - setup_postdata( $discount->ID ); |
|
| 770 | + setup_postdata($discount->ID); |
|
| 771 | 771 | |
| 772 | 772 | // Fetch the fields to include in this response. |
| 773 | - $fields = $this->get_fields_for_response( $request ); |
|
| 773 | + $fields = $this->get_fields_for_response($request); |
|
| 774 | 774 | |
| 775 | 775 | // Base fields for every discount. |
| 776 | 776 | $data = array(); |
| 777 | 777 | |
| 778 | 778 | // Set up ID. |
| 779 | - if ( rest_is_field_included( 'id', $fields ) ) { |
|
| 779 | + if (rest_is_field_included('id', $fields)) { |
|
| 780 | 780 | $data['id'] = $discount->ID; |
| 781 | 781 | } |
| 782 | 782 | |
| 783 | 783 | // Title. |
| 784 | - if ( rest_is_field_included( 'title', $fields ) ) { |
|
| 785 | - $data['title'] = get_the_title( $discount->ID ); |
|
| 784 | + if (rest_is_field_included('title', $fields)) { |
|
| 785 | + $data['title'] = get_the_title($discount->ID); |
|
| 786 | 786 | } |
| 787 | 787 | |
| 788 | 788 | // Code. |
| 789 | - if ( rest_is_field_included( 'code', $fields ) ) { |
|
| 790 | - $data['code'] = wpinv_get_discount_code( $discount->ID ); |
|
| 789 | + if (rest_is_field_included('code', $fields)) { |
|
| 790 | + $data['code'] = wpinv_get_discount_code($discount->ID); |
|
| 791 | 791 | } |
| 792 | 792 | |
| 793 | 793 | // Type. |
| 794 | - if ( rest_is_field_included( 'type', $fields ) ) { |
|
| 795 | - $data['type'] = wpinv_get_discount_type( $discount->ID ); |
|
| 794 | + if (rest_is_field_included('type', $fields)) { |
|
| 795 | + $data['type'] = wpinv_get_discount_type($discount->ID); |
|
| 796 | 796 | } |
| 797 | 797 | |
| 798 | 798 | // Amount. |
| 799 | - if ( rest_is_field_included( 'amount', $fields ) ) { |
|
| 800 | - $data['amount'] = wpinv_get_discount_amount( $discount->ID ); |
|
| 799 | + if (rest_is_field_included('amount', $fields)) { |
|
| 800 | + $data['amount'] = wpinv_get_discount_amount($discount->ID); |
|
| 801 | 801 | } |
| 802 | 802 | |
| 803 | 803 | // Status. |
| 804 | - if ( rest_is_field_included( 'status', $fields ) ) { |
|
| 805 | - $data['status'] = get_post_status( $discount->ID ); |
|
| 804 | + if (rest_is_field_included('status', $fields)) { |
|
| 805 | + $data['status'] = get_post_status($discount->ID); |
|
| 806 | 806 | } |
| 807 | 807 | |
| 808 | 808 | // Items. |
| 809 | - if ( rest_is_field_included( 'items', $fields ) ) { |
|
| 810 | - $data['items'] = wpinv_get_discount_item_reqs( $discount->ID ); |
|
| 809 | + if (rest_is_field_included('items', $fields)) { |
|
| 810 | + $data['items'] = wpinv_get_discount_item_reqs($discount->ID); |
|
| 811 | 811 | } |
| 812 | 812 | |
| 813 | 813 | // Excluded Items. |
| 814 | - if ( rest_is_field_included( 'exclude_items', $fields ) ) { |
|
| 815 | - $data['exclude_items'] = wpinv_get_discount_excluded_items( $discount->ID ); |
|
| 814 | + if (rest_is_field_included('exclude_items', $fields)) { |
|
| 815 | + $data['exclude_items'] = wpinv_get_discount_excluded_items($discount->ID); |
|
| 816 | 816 | } |
| 817 | 817 | |
| 818 | 818 | // Start date. |
| 819 | - if ( rest_is_field_included( 'start_date', $fields ) ) { |
|
| 820 | - $data['start_date'] = wpinv_get_discount_start_date( $discount->ID ); |
|
| 819 | + if (rest_is_field_included('start_date', $fields)) { |
|
| 820 | + $data['start_date'] = wpinv_get_discount_start_date($discount->ID); |
|
| 821 | 821 | } |
| 822 | 822 | |
| 823 | 823 | // End date. |
| 824 | - if ( rest_is_field_included( 'end_date', $fields ) ) { |
|
| 825 | - $data['end_date'] = wpinv_get_discount_expiration( $discount->ID ); |
|
| 824 | + if (rest_is_field_included('end_date', $fields)) { |
|
| 825 | + $data['end_date'] = wpinv_get_discount_expiration($discount->ID); |
|
| 826 | 826 | } |
| 827 | 827 | |
| 828 | 828 | // Minimum amount. |
| 829 | - if ( rest_is_field_included( 'minimum_amount', $fields ) ) { |
|
| 830 | - $data['minimum_amount'] = wpinv_get_discount_min_total( $discount->ID ); |
|
| 829 | + if (rest_is_field_included('minimum_amount', $fields)) { |
|
| 830 | + $data['minimum_amount'] = wpinv_get_discount_min_total($discount->ID); |
|
| 831 | 831 | } |
| 832 | 832 | |
| 833 | 833 | // Maximum amount. |
| 834 | - if ( rest_is_field_included( 'maximum_amount', $fields ) ) { |
|
| 835 | - $data['maximum_amount'] = wpinv_get_discount_max_total( $discount->ID ); |
|
| 834 | + if (rest_is_field_included('maximum_amount', $fields)) { |
|
| 835 | + $data['maximum_amount'] = wpinv_get_discount_max_total($discount->ID); |
|
| 836 | 836 | } |
| 837 | 837 | |
| 838 | 838 | // Recurring. |
| 839 | - if ( rest_is_field_included( 'recurring', $fields ) ) { |
|
| 840 | - $data['recurring'] = wpinv_discount_is_recurring( $discount->ID ); |
|
| 839 | + if (rest_is_field_included('recurring', $fields)) { |
|
| 840 | + $data['recurring'] = wpinv_discount_is_recurring($discount->ID); |
|
| 841 | 841 | } |
| 842 | 842 | |
| 843 | 843 | // Maximum uses. |
| 844 | - if ( rest_is_field_included( 'max_uses', $fields ) ) { |
|
| 845 | - $data['max_uses'] = wpinv_get_discount_max_uses( $discount->ID ); |
|
| 844 | + if (rest_is_field_included('max_uses', $fields)) { |
|
| 845 | + $data['max_uses'] = wpinv_get_discount_max_uses($discount->ID); |
|
| 846 | 846 | } |
| 847 | 847 | |
| 848 | 848 | // Single use. |
| 849 | - if ( rest_is_field_included( 'single_use', $fields ) ) { |
|
| 850 | - $data['single_use'] = wpinv_discount_is_single_use( $discount->ID ); |
|
| 849 | + if (rest_is_field_included('single_use', $fields)) { |
|
| 850 | + $data['single_use'] = wpinv_discount_is_single_use($discount->ID); |
|
| 851 | 851 | } |
| 852 | 852 | |
| 853 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
| 854 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
| 855 | - $data = $this->filter_response_by_context( $data, $context ); |
|
| 853 | + $context = !empty($request['context']) ? $request['context'] : 'view'; |
|
| 854 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
| 855 | + $data = $this->filter_response_by_context($data, $context); |
|
| 856 | 856 | |
| 857 | 857 | // Wrap the data in a response object. |
| 858 | - $response = rest_ensure_response( $data ); |
|
| 858 | + $response = rest_ensure_response($data); |
|
| 859 | 859 | |
| 860 | - $links = $this->prepare_links( $discount ); |
|
| 861 | - $response->add_links( $links ); |
|
| 860 | + $links = $this->prepare_links($discount); |
|
| 861 | + $response->add_links($links); |
|
| 862 | 862 | |
| 863 | - if ( ! empty( $links['self']['href'] ) ) { |
|
| 864 | - $actions = $this->get_available_actions( $discount, $request ); |
|
| 863 | + if (!empty($links['self']['href'])) { |
|
| 864 | + $actions = $this->get_available_actions($discount, $request); |
|
| 865 | 865 | |
| 866 | 866 | $self = $links['self']['href']; |
| 867 | 867 | |
| 868 | - foreach ( $actions as $rel ) { |
|
| 869 | - $response->add_link( $rel, $self ); |
|
| 868 | + foreach ($actions as $rel) { |
|
| 869 | + $response->add_link($rel, $self); |
|
| 870 | 870 | } |
| 871 | 871 | } |
| 872 | 872 | |
@@ -879,7 +879,7 @@ discard block |
||
| 879 | 879 | * @param WP_Post $discount The discount post object. |
| 880 | 880 | * @param WP_REST_Request $request Request object. |
| 881 | 881 | */ |
| 882 | - return apply_filters( "wpinv_rest_prepare_discount", $response, $discount, $request ); |
|
| 882 | + return apply_filters("wpinv_rest_prepare_discount", $response, $discount, $request); |
|
| 883 | 883 | } |
| 884 | 884 | |
| 885 | 885 | /** |
@@ -892,57 +892,57 @@ discard block |
||
| 892 | 892 | * @param WP_REST_Request $request Full details about the request. |
| 893 | 893 | * @return array Fields to be included in the response. |
| 894 | 894 | */ |
| 895 | - public function get_fields_for_response( $request ) { |
|
| 895 | + public function get_fields_for_response($request) { |
|
| 896 | 896 | $schema = $this->get_item_schema(); |
| 897 | - $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
| 897 | + $properties = isset($schema['properties']) ? $schema['properties'] : array(); |
|
| 898 | 898 | |
| 899 | 899 | $additional_fields = $this->get_additional_fields(); |
| 900 | - foreach ( $additional_fields as $field_name => $field_options ) { |
|
| 900 | + foreach ($additional_fields as $field_name => $field_options) { |
|
| 901 | 901 | // For back-compat, include any field with an empty schema |
| 902 | 902 | // because it won't be present in $this->get_item_schema(). |
| 903 | - if ( is_null( $field_options['schema'] ) ) { |
|
| 904 | - $properties[ $field_name ] = $field_options; |
|
| 903 | + if (is_null($field_options['schema'])) { |
|
| 904 | + $properties[$field_name] = $field_options; |
|
| 905 | 905 | } |
| 906 | 906 | } |
| 907 | 907 | |
| 908 | 908 | // Exclude fields that specify a different context than the request context. |
| 909 | 909 | $context = $request['context']; |
| 910 | - if ( $context ) { |
|
| 911 | - foreach ( $properties as $name => $options ) { |
|
| 912 | - if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
| 913 | - unset( $properties[ $name ] ); |
|
| 910 | + if ($context) { |
|
| 911 | + foreach ($properties as $name => $options) { |
|
| 912 | + if (!empty($options['context']) && !in_array($context, $options['context'], true)) { |
|
| 913 | + unset($properties[$name]); |
|
| 914 | 914 | } |
| 915 | 915 | } |
| 916 | 916 | } |
| 917 | 917 | |
| 918 | - $fields = array_keys( $properties ); |
|
| 918 | + $fields = array_keys($properties); |
|
| 919 | 919 | |
| 920 | - if ( ! isset( $request['_fields'] ) ) { |
|
| 920 | + if (!isset($request['_fields'])) { |
|
| 921 | 921 | return $fields; |
| 922 | 922 | } |
| 923 | - $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
| 924 | - if ( 0 === count( $requested_fields ) ) { |
|
| 923 | + $requested_fields = wpinv_parse_list($request['_fields']); |
|
| 924 | + if (0 === count($requested_fields)) { |
|
| 925 | 925 | return $fields; |
| 926 | 926 | } |
| 927 | 927 | // Trim off outside whitespace from the comma delimited list. |
| 928 | - $requested_fields = array_map( 'trim', $requested_fields ); |
|
| 928 | + $requested_fields = array_map('trim', $requested_fields); |
|
| 929 | 929 | // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
| 930 | - if ( in_array( 'id', $fields, true ) ) { |
|
| 930 | + if (in_array('id', $fields, true)) { |
|
| 931 | 931 | $requested_fields[] = 'id'; |
| 932 | 932 | } |
| 933 | 933 | // Return the list of all requested fields which appear in the schema. |
| 934 | 934 | return array_reduce( |
| 935 | 935 | $requested_fields, |
| 936 | - function( $response_fields, $field ) use ( $fields ) { |
|
| 937 | - if ( in_array( $field, $fields, true ) ) { |
|
| 936 | + function($response_fields, $field) use ($fields) { |
|
| 937 | + if (in_array($field, $fields, true)) { |
|
| 938 | 938 | $response_fields[] = $field; |
| 939 | 939 | return $response_fields; |
| 940 | 940 | } |
| 941 | 941 | // Check for nested fields if $field is not a direct match. |
| 942 | - $nested_fields = explode( '.', $field ); |
|
| 942 | + $nested_fields = explode('.', $field); |
|
| 943 | 943 | // A nested field is included so long as its top-level property is |
| 944 | 944 | // present in the schema. |
| 945 | - if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
| 945 | + if (in_array($nested_fields[0], $fields, true)) { |
|
| 946 | 946 | $response_fields[] = $field; |
| 947 | 947 | } |
| 948 | 948 | return $response_fields; |
@@ -961,8 +961,8 @@ discard block |
||
| 961 | 961 | public function get_item_schema() { |
| 962 | 962 | |
| 963 | 963 | // Maybe retrieve the schema from cache. |
| 964 | - if ( empty( $this->schema ) ) { |
|
| 965 | - return $this->add_additional_fields_schema( $this->schema ); |
|
| 964 | + if (empty($this->schema)) { |
|
| 965 | + return $this->add_additional_fields_schema($this->schema); |
|
| 966 | 966 | } |
| 967 | 967 | |
| 968 | 968 | $schema = array( |
@@ -974,99 +974,99 @@ discard block |
||
| 974 | 974 | 'properties' => array( |
| 975 | 975 | |
| 976 | 976 | 'id' => array( |
| 977 | - 'description' => __( 'Unique identifier for the discount.', 'invoicing' ), |
|
| 977 | + 'description' => __('Unique identifier for the discount.', 'invoicing'), |
|
| 978 | 978 | 'type' => 'integer', |
| 979 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 979 | + 'context' => array('view', 'edit', 'embed'), |
|
| 980 | 980 | 'readonly' => true, |
| 981 | 981 | ), |
| 982 | 982 | |
| 983 | 983 | 'title' => array( |
| 984 | - 'description' => __( 'The title for the discount.', 'invoicing' ), |
|
| 984 | + 'description' => __('The title for the discount.', 'invoicing'), |
|
| 985 | 985 | 'type' => 'string', |
| 986 | - 'context' => array( 'view', 'edit' ), |
|
| 986 | + 'context' => array('view', 'edit'), |
|
| 987 | 987 | ), |
| 988 | 988 | |
| 989 | 989 | 'code' => array( |
| 990 | - 'description' => __( 'The discount code.', 'invoicing' ), |
|
| 990 | + 'description' => __('The discount code.', 'invoicing'), |
|
| 991 | 991 | 'type' => 'string', |
| 992 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 992 | + 'context' => array('view', 'edit', 'embed'), |
|
| 993 | 993 | 'required' => true, |
| 994 | 994 | ), |
| 995 | 995 | |
| 996 | 996 | 'type' => array( |
| 997 | - 'description' => __( 'The type of discount.', 'invoicing' ), |
|
| 997 | + 'description' => __('The type of discount.', 'invoicing'), |
|
| 998 | 998 | 'type' => 'string', |
| 999 | - 'enum' => array_keys( wpinv_get_discount_types() ), |
|
| 1000 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 999 | + 'enum' => array_keys(wpinv_get_discount_types()), |
|
| 1000 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1001 | 1001 | 'default' => 'percentage', |
| 1002 | 1002 | ), |
| 1003 | 1003 | |
| 1004 | 1004 | 'amount' => array( |
| 1005 | - 'description' => __( 'The discount value.', 'invoicing' ), |
|
| 1005 | + 'description' => __('The discount value.', 'invoicing'), |
|
| 1006 | 1006 | 'type' => 'number', |
| 1007 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1007 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1008 | 1008 | 'required' => true, |
| 1009 | 1009 | ), |
| 1010 | 1010 | |
| 1011 | 1011 | 'status' => array( |
| 1012 | - 'description' => __( 'A named status for the discount.', 'invoicing' ), |
|
| 1012 | + 'description' => __('A named status for the discount.', 'invoicing'), |
|
| 1013 | 1013 | 'type' => 'string', |
| 1014 | - 'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ), |
|
| 1015 | - 'context' => array( 'view', 'edit' ), |
|
| 1014 | + 'enum' => array_keys(get_post_stati(array('internal' => false))), |
|
| 1015 | + 'context' => array('view', 'edit'), |
|
| 1016 | 1016 | ), |
| 1017 | 1017 | |
| 1018 | 1018 | 'items' => array( |
| 1019 | - 'description' => __( 'Items which need to be in the cart to use this discount or, for "Item Discounts", which items are discounted. If left blank, this discount will be used on any item.', 'invoicing' ), |
|
| 1019 | + 'description' => __('Items which need to be in the cart to use this discount or, for "Item Discounts", which items are discounted. If left blank, this discount will be used on any item.', 'invoicing'), |
|
| 1020 | 1020 | 'type' => 'array', |
| 1021 | - 'context' => array( 'view', 'edit' ), |
|
| 1021 | + 'context' => array('view', 'edit'), |
|
| 1022 | 1022 | ), |
| 1023 | 1023 | |
| 1024 | 1024 | 'exclude_items' => array( |
| 1025 | - 'description' => __( 'Items which are NOT allowed to use this discount.', 'invoicing' ), |
|
| 1025 | + 'description' => __('Items which are NOT allowed to use this discount.', 'invoicing'), |
|
| 1026 | 1026 | 'type' => 'array', |
| 1027 | - 'context' => array( 'view', 'edit' ), |
|
| 1027 | + 'context' => array('view', 'edit'), |
|
| 1028 | 1028 | ), |
| 1029 | 1029 | |
| 1030 | 1030 | 'start_date' => array( |
| 1031 | - 'description' => __( 'The start date for the discount in the format of yyyy-mm-dd hh:mm:ss . If provided, the discount can only be used after or on this date.', 'invoicing' ), |
|
| 1031 | + 'description' => __('The start date for the discount in the format of yyyy-mm-dd hh:mm:ss . If provided, the discount can only be used after or on this date.', 'invoicing'), |
|
| 1032 | 1032 | 'type' => 'string', |
| 1033 | - 'context' => array( 'view', 'edit' ), |
|
| 1033 | + 'context' => array('view', 'edit'), |
|
| 1034 | 1034 | ), |
| 1035 | 1035 | |
| 1036 | 1036 | 'end_date' => array( |
| 1037 | - 'description' => __( 'The expiration date for the discount.', 'invoicing' ), |
|
| 1037 | + 'description' => __('The expiration date for the discount.', 'invoicing'), |
|
| 1038 | 1038 | 'type' => 'string', |
| 1039 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1039 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1040 | 1040 | ), |
| 1041 | 1041 | |
| 1042 | 1042 | 'minimum_amount' => array( |
| 1043 | - 'description' => __( 'Minimum amount needed to use this invoice.', 'invoicing' ), |
|
| 1043 | + 'description' => __('Minimum amount needed to use this invoice.', 'invoicing'), |
|
| 1044 | 1044 | 'type' => 'number', |
| 1045 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1045 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1046 | 1046 | ), |
| 1047 | 1047 | |
| 1048 | 1048 | 'maximum_amount' => array( |
| 1049 | - 'description' => __( 'Maximum amount needed to use this invoice.', 'invoicing' ), |
|
| 1049 | + 'description' => __('Maximum amount needed to use this invoice.', 'invoicing'), |
|
| 1050 | 1050 | 'type' => 'number', |
| 1051 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1051 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1052 | 1052 | ), |
| 1053 | 1053 | |
| 1054 | 1054 | 'recurring' => array( |
| 1055 | - 'description' => __( 'Whether the discount is applied to all recurring payments or only the first recurring payment.', 'invoicing' ), |
|
| 1055 | + 'description' => __('Whether the discount is applied to all recurring payments or only the first recurring payment.', 'invoicing'), |
|
| 1056 | 1056 | 'type' => 'integer', |
| 1057 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1057 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1058 | 1058 | ), |
| 1059 | 1059 | |
| 1060 | 1060 | 'max_uses' => array( |
| 1061 | - 'description' => __( 'The maximum number of times this discount code can be used.', 'invoicing' ), |
|
| 1061 | + 'description' => __('The maximum number of times this discount code can be used.', 'invoicing'), |
|
| 1062 | 1062 | 'type' => 'number', |
| 1063 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1063 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1064 | 1064 | ), |
| 1065 | 1065 | |
| 1066 | 1066 | 'single_use' => array( |
| 1067 | - 'description' => __( 'Whether or not this discount can only be used once per user.', 'invoicing' ), |
|
| 1067 | + 'description' => __('Whether or not this discount can only be used once per user.', 'invoicing'), |
|
| 1068 | 1068 | 'type' => 'integer', |
| 1069 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
| 1069 | + 'context' => array('view', 'edit', 'embed'), |
|
| 1070 | 1070 | ) |
| 1071 | 1071 | |
| 1072 | 1072 | ), |
@@ -1084,12 +1084,12 @@ discard block |
||
| 1084 | 1084 | * |
| 1085 | 1085 | * @param array $schema The discount schema. |
| 1086 | 1086 | */ |
| 1087 | - $schema = apply_filters( "wpinv_rest_discount_schema", $schema ); |
|
| 1087 | + $schema = apply_filters("wpinv_rest_discount_schema", $schema); |
|
| 1088 | 1088 | |
| 1089 | 1089 | // Cache the discount schema. |
| 1090 | 1090 | $this->schema = $schema; |
| 1091 | 1091 | |
| 1092 | - return $this->add_additional_fields_schema( $this->schema ); |
|
| 1092 | + return $this->add_additional_fields_schema($this->schema); |
|
| 1093 | 1093 | } |
| 1094 | 1094 | |
| 1095 | 1095 | /** |
@@ -1101,20 +1101,20 @@ discard block |
||
| 1101 | 1101 | */ |
| 1102 | 1102 | protected function get_schema_links() { |
| 1103 | 1103 | |
| 1104 | - $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
| 1104 | + $href = rest_url("{$this->namespace}/{$this->rest_base}/{id}"); |
|
| 1105 | 1105 | |
| 1106 | 1106 | $links = array(); |
| 1107 | 1107 | |
| 1108 | 1108 | $links[] = array( |
| 1109 | 1109 | 'rel' => 'https://api.w.org/action-publish', |
| 1110 | - 'title' => __( 'The current user can publish this discount.', 'invoicing' ), |
|
| 1110 | + 'title' => __('The current user can publish this discount.', 'invoicing'), |
|
| 1111 | 1111 | 'href' => $href, |
| 1112 | 1112 | 'targetSchema' => array( |
| 1113 | 1113 | 'type' => 'object', |
| 1114 | 1114 | 'properties' => array( |
| 1115 | 1115 | 'status' => array( |
| 1116 | 1116 | 'type' => 'string', |
| 1117 | - 'enum' => array( 'publish', 'future' ), |
|
| 1117 | + 'enum' => array('publish', 'future'), |
|
| 1118 | 1118 | ), |
| 1119 | 1119 | ), |
| 1120 | 1120 | ), |
@@ -1131,18 +1131,18 @@ discard block |
||
| 1131 | 1131 | * @param WP_Post $discount Post Object. |
| 1132 | 1132 | * @return array Links for the given discount. |
| 1133 | 1133 | */ |
| 1134 | - protected function prepare_links( $discount ) { |
|
| 1134 | + protected function prepare_links($discount) { |
|
| 1135 | 1135 | |
| 1136 | 1136 | // Prepare the base REST API endpoint for discounts. |
| 1137 | - $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
| 1137 | + $base = sprintf('%s/%s', $this->namespace, $this->rest_base); |
|
| 1138 | 1138 | |
| 1139 | 1139 | // Entity meta. |
| 1140 | 1140 | $links = array( |
| 1141 | 1141 | 'self' => array( |
| 1142 | - 'href' => rest_url( trailingslashit( $base ) . $discount->ID ), |
|
| 1142 | + 'href' => rest_url(trailingslashit($base) . $discount->ID), |
|
| 1143 | 1143 | ), |
| 1144 | 1144 | 'collection' => array( |
| 1145 | - 'href' => rest_url( $base ), |
|
| 1145 | + 'href' => rest_url($base), |
|
| 1146 | 1146 | ), |
| 1147 | 1147 | ); |
| 1148 | 1148 | |
@@ -1155,7 +1155,7 @@ discard block |
||
| 1155 | 1155 | * |
| 1156 | 1156 | * @param array $links Rest links. |
| 1157 | 1157 | */ |
| 1158 | - return apply_filters( "wpinv_rest_discount_links", $links ); |
|
| 1158 | + return apply_filters("wpinv_rest_discount_links", $links); |
|
| 1159 | 1159 | |
| 1160 | 1160 | } |
| 1161 | 1161 | |
@@ -1168,19 +1168,19 @@ discard block |
||
| 1168 | 1168 | * @param WP_REST_Request $request Request object. |
| 1169 | 1169 | * @return array List of link relations. |
| 1170 | 1170 | */ |
| 1171 | - protected function get_available_actions( $discount, $request ) { |
|
| 1171 | + protected function get_available_actions($discount, $request) { |
|
| 1172 | 1172 | |
| 1173 | - if ( 'edit' !== $request['context'] ) { |
|
| 1173 | + if ('edit' !== $request['context']) { |
|
| 1174 | 1174 | return array(); |
| 1175 | 1175 | } |
| 1176 | 1176 | |
| 1177 | 1177 | $rels = array(); |
| 1178 | 1178 | |
| 1179 | 1179 | // Retrieve the post type object. |
| 1180 | - $post_type = get_post_type_object( $discount->post_type ); |
|
| 1180 | + $post_type = get_post_type_object($discount->post_type); |
|
| 1181 | 1181 | |
| 1182 | 1182 | // Mark discount as published. |
| 1183 | - if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
| 1183 | + if (current_user_can($post_type->cap->publish_posts)) { |
|
| 1184 | 1184 | $rels[] = 'https://api.w.org/action-publish'; |
| 1185 | 1185 | } |
| 1186 | 1186 | |
@@ -1193,7 +1193,7 @@ discard block |
||
| 1193 | 1193 | * |
| 1194 | 1194 | * @param array $rels Available link relations. |
| 1195 | 1195 | */ |
| 1196 | - return apply_filters( "wpinv_rest_discount_link_relations", $rels ); |
|
| 1196 | + return apply_filters("wpinv_rest_discount_link_relations", $rels); |
|
| 1197 | 1197 | } |
| 1198 | 1198 | |
| 1199 | 1199 | /** |
@@ -1205,7 +1205,7 @@ discard block |
||
| 1205 | 1205 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 1206 | 1206 | */ |
| 1207 | 1207 | public function get_discount_types() { |
| 1208 | - return rest_ensure_response( wpinv_get_discount_types() ); |
|
| 1208 | + return rest_ensure_response(wpinv_get_discount_types()); |
|
| 1209 | 1209 | } |
| 1210 | 1210 | |
| 1211 | 1211 | } |
| 1212 | 1212 | \ No newline at end of file |
@@ -13,9 +13,9 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | |
| 15 | 15 | function wpinv_get_default_country() { |
| 16 | - $country = wpinv_get_option( 'default_country', 'UK' ); |
|
| 16 | + $country = wpinv_get_option( 'default_country', 'UK' ); |
|
| 17 | 17 | |
| 18 | - return apply_filters( 'wpinv_default_country', $country ); |
|
| 18 | + return apply_filters( 'wpinv_default_country', $country ); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | /** |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | */ |
| 27 | 27 | function wpinv_sanitize_country( $country ) { |
| 28 | 28 | |
| 29 | - // Enure the country is specified |
|
| 29 | + // Enure the country is specified |
|
| 30 | 30 | if ( empty( $country ) ) { |
| 31 | 31 | $country = wpinv_get_default_country(); |
| 32 | 32 | } |
@@ -56,9 +56,9 @@ discard block |
||
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | function wpinv_get_default_state() { |
| 59 | - $state = wpinv_get_option( 'default_state', false ); |
|
| 59 | + $state = wpinv_get_option( 'default_state', false ); |
|
| 60 | 60 | |
| 61 | - return apply_filters( 'wpinv_default_state', $state ); |
|
| 61 | + return apply_filters( 'wpinv_default_state', $state ); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | function wpinv_state_name( $state_code = '', $country_code = '' ) { |
@@ -166,11 +166,11 @@ discard block |
||
| 166 | 166 | |
| 167 | 167 | $country = wpinv_sanitize_country( $country ); |
| 168 | 168 | |
| 169 | - foreach ( wpinv_get_continents( 'countries' ) as $continent_code => $countries ) { |
|
| 170 | - if ( false !== array_search( $country, $countries, true ) ) { |
|
| 171 | - return $continent_code; |
|
| 172 | - } |
|
| 173 | - } |
|
| 169 | + foreach ( wpinv_get_continents( 'countries' ) as $continent_code => $countries ) { |
|
| 170 | + if ( false !== array_search( $country, $countries, true ) ) { |
|
| 171 | + return $continent_code; |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | 175 | return ''; |
| 176 | 176 | |
@@ -462,30 +462,30 @@ discard block |
||
| 462 | 462 | } |
| 463 | 463 | |
| 464 | 464 | function wpinv_get_states_field() { |
| 465 | - if( empty( $_POST['country'] ) ) { |
|
| 466 | - $_POST['country'] = wpinv_get_default_country(); |
|
| 467 | - } |
|
| 468 | - $states = wpinv_get_country_states( sanitize_text_field( $_POST['country'] ) ); |
|
| 465 | + if( empty( $_POST['country'] ) ) { |
|
| 466 | + $_POST['country'] = wpinv_get_default_country(); |
|
| 467 | + } |
|
| 468 | + $states = wpinv_get_country_states( sanitize_text_field( $_POST['country'] ) ); |
|
| 469 | 469 | |
| 470 | - if( !empty( $states ) ) { |
|
| 471 | - $sanitized_field_name = sanitize_text_field( $_POST['field_name'] ); |
|
| 470 | + if( !empty( $states ) ) { |
|
| 471 | + $sanitized_field_name = sanitize_text_field( $_POST['field_name'] ); |
|
| 472 | 472 | |
| 473 | 473 | $args = array( |
| 474 | - 'name' => $sanitized_field_name, |
|
| 475 | - 'id' => $sanitized_field_name, |
|
| 476 | - 'class' => $sanitized_field_name . 'custom-select wpinv-select wpi_select2', |
|
| 477 | - 'options' => array_merge( array( '' => '' ), $states ), |
|
| 478 | - 'show_option_all' => false, |
|
| 479 | - 'show_option_none' => false |
|
| 480 | - ); |
|
| 481 | - |
|
| 482 | - $response = wpinv_html_select( $args ); |
|
| 483 | - |
|
| 484 | - } else { |
|
| 485 | - $response = 'nostates'; |
|
| 486 | - } |
|
| 474 | + 'name' => $sanitized_field_name, |
|
| 475 | + 'id' => $sanitized_field_name, |
|
| 476 | + 'class' => $sanitized_field_name . 'custom-select wpinv-select wpi_select2', |
|
| 477 | + 'options' => array_merge( array( '' => '' ), $states ), |
|
| 478 | + 'show_option_all' => false, |
|
| 479 | + 'show_option_none' => false |
|
| 480 | + ); |
|
| 481 | + |
|
| 482 | + $response = wpinv_html_select( $args ); |
|
| 483 | + |
|
| 484 | + } else { |
|
| 485 | + $response = 'nostates'; |
|
| 486 | + } |
|
| 487 | 487 | |
| 488 | - return $response; |
|
| 488 | + return $response; |
|
| 489 | 489 | } |
| 490 | 490 | |
| 491 | 491 | function wpinv_default_billing_country( $country = '', $user_id = 0 ) { |
@@ -503,46 +503,46 @@ discard block |
||
| 503 | 503 | */ |
| 504 | 504 | function wpinv_get_address_formats() { |
| 505 | 505 | |
| 506 | - return apply_filters( 'wpinv_localisation_address_formats', |
|
| 507 | - array( |
|
| 508 | - 'default' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}\n{{zip}}\n{{country}}", |
|
| 509 | - 'AU' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}} {{zip}}\n{{country}}", |
|
| 510 | - 'AT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 511 | - 'BE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 512 | - 'CA' => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{state_code}} {{zip}}\n{{country}}", |
|
| 513 | - 'CH' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 514 | - 'CL' => "{{company}}\n{{name}}\n{{address}}\n{{state}}\n{{zip}} {{city}}\n{{country}}", |
|
| 515 | - 'CN' => "{{country}} {{zip}}\n{{state}}, {{city}}, {{address}}\n{{company}}\n{{name}}", |
|
| 516 | - 'CZ' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 517 | - 'DE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 518 | - 'EE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 519 | - 'FI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 520 | - 'DK' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 521 | - 'FR' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city_upper}}\n{{country}}", |
|
| 522 | - 'HK' => "{{company}}\n{{first_name}} {{last_name_upper}}\n{{address}}\n{{city_upper}}\n{{state_upper}}\n{{country}}", |
|
| 523 | - 'HU' => "{{name}}\n{{company}}\n{{city}}\n{{address}}\n{{zip}}\n{{country}}", |
|
| 524 | - 'IN' => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{zip}}\n{{state}}, {{country}}", |
|
| 525 | - 'IS' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 526 | - 'IT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}}\n{{city}}\n{{state_upper}}\n{{country}}", |
|
| 527 | - 'JP' => "{{zip}}\n{{state}} {{city}} {{address}}\n{{company}}\n{{last_name}} {{first_name}}\n{{country}}", |
|
| 528 | - 'TW' => "{{company}}\n{{last_name}} {{first_name}}\n{{address}}\n{{state}}, {{city}} {{zip}}\n{{country}}", |
|
| 529 | - 'LI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 530 | - 'NL' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 531 | - 'NZ' => "{{name}}\n{{company}}\n{{address}}\n{{city}} {{zip}}\n{{country}}", |
|
| 532 | - 'NO' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 533 | - 'PL' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 534 | - 'PT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 535 | - 'SK' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 536 | - 'RS' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 537 | - 'SI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 538 | - 'ES' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{state}}\n{{country}}", |
|
| 539 | - 'SE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 540 | - 'TR' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}} {{state}}\n{{country}}", |
|
| 541 | - 'UG' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}, {{country}}", |
|
| 542 | - 'US' => "{{name}}\n{{company}}\n{{address}}\n{{city}}, {{state_code}} {{zip}}\n{{country}}", |
|
| 543 | - 'VN' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{country}}", |
|
| 544 | - ) |
|
| 545 | - ); |
|
| 506 | + return apply_filters( 'wpinv_localisation_address_formats', |
|
| 507 | + array( |
|
| 508 | + 'default' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}\n{{zip}}\n{{country}}", |
|
| 509 | + 'AU' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}} {{zip}}\n{{country}}", |
|
| 510 | + 'AT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 511 | + 'BE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 512 | + 'CA' => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{state_code}} {{zip}}\n{{country}}", |
|
| 513 | + 'CH' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 514 | + 'CL' => "{{company}}\n{{name}}\n{{address}}\n{{state}}\n{{zip}} {{city}}\n{{country}}", |
|
| 515 | + 'CN' => "{{country}} {{zip}}\n{{state}}, {{city}}, {{address}}\n{{company}}\n{{name}}", |
|
| 516 | + 'CZ' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 517 | + 'DE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 518 | + 'EE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 519 | + 'FI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 520 | + 'DK' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 521 | + 'FR' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city_upper}}\n{{country}}", |
|
| 522 | + 'HK' => "{{company}}\n{{first_name}} {{last_name_upper}}\n{{address}}\n{{city_upper}}\n{{state_upper}}\n{{country}}", |
|
| 523 | + 'HU' => "{{name}}\n{{company}}\n{{city}}\n{{address}}\n{{zip}}\n{{country}}", |
|
| 524 | + 'IN' => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{zip}}\n{{state}}, {{country}}", |
|
| 525 | + 'IS' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 526 | + 'IT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}}\n{{city}}\n{{state_upper}}\n{{country}}", |
|
| 527 | + 'JP' => "{{zip}}\n{{state}} {{city}} {{address}}\n{{company}}\n{{last_name}} {{first_name}}\n{{country}}", |
|
| 528 | + 'TW' => "{{company}}\n{{last_name}} {{first_name}}\n{{address}}\n{{state}}, {{city}} {{zip}}\n{{country}}", |
|
| 529 | + 'LI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 530 | + 'NL' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 531 | + 'NZ' => "{{name}}\n{{company}}\n{{address}}\n{{city}} {{zip}}\n{{country}}", |
|
| 532 | + 'NO' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 533 | + 'PL' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 534 | + 'PT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 535 | + 'SK' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 536 | + 'RS' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 537 | + 'SI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 538 | + 'ES' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{state}}\n{{country}}", |
|
| 539 | + 'SE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 540 | + 'TR' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}} {{state}}\n{{country}}", |
|
| 541 | + 'UG' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}, {{country}}", |
|
| 542 | + 'US' => "{{name}}\n{{company}}\n{{address}}\n{{city}}, {{state_code}} {{zip}}\n{{country}}", |
|
| 543 | + 'VN' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{country}}", |
|
| 544 | + ) |
|
| 545 | + ); |
|
| 546 | 546 | } |
| 547 | 547 | |
| 548 | 548 | /** |
@@ -559,21 +559,21 @@ discard block |
||
| 559 | 559 | } |
| 560 | 560 | |
| 561 | 561 | // Get all formats. |
| 562 | - $formats = wpinv_get_address_formats(); |
|
| 562 | + $formats = wpinv_get_address_formats(); |
|
| 563 | 563 | |
| 564 | - // Get format for the specified country. |
|
| 565 | - $format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default']; |
|
| 564 | + // Get format for the specified country. |
|
| 565 | + $format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default']; |
|
| 566 | 566 | |
| 567 | 567 | /** |
| 568 | - * Filters the address format to use on Invoices. |
|
| 568 | + * Filters the address format to use on Invoices. |
|
| 569 | 569 | * |
| 570 | 570 | * New lines will be replaced by a `br` element. Double new lines will be replaced by a paragraph. HTML tags are allowed. |
| 571 | - * |
|
| 572 | - * @since 1.0.13 |
|
| 573 | - * |
|
| 574 | - * @param string $format The address format to use. |
|
| 571 | + * |
|
| 572 | + * @since 1.0.13 |
|
| 573 | + * |
|
| 574 | + * @param string $format The address format to use. |
|
| 575 | 575 | * @param string $country The country who's address format is being retrieved. |
| 576 | - */ |
|
| 576 | + */ |
|
| 577 | 577 | return apply_filters( 'wpinv_get_full_address_format', $format, $country ); |
| 578 | 578 | } |
| 579 | 579 | |
@@ -594,8 +594,8 @@ discard block |
||
| 594 | 594 | 'country' => '', |
| 595 | 595 | 'zip' => '', |
| 596 | 596 | 'first_name' => '', |
| 597 | - 'last_name' => '', |
|
| 598 | - 'company' => '', |
|
| 597 | + 'last_name' => '', |
|
| 598 | + 'company' => '', |
|
| 599 | 599 | ); |
| 600 | 600 | |
| 601 | 601 | $args = map_deep( wp_parse_args( $billing_details, $default_args ), 'trim' ); |
@@ -616,14 +616,14 @@ discard block |
||
| 616 | 616 | $args['country_code']= $country; |
| 617 | 617 | |
| 618 | 618 | /** |
| 619 | - * Filters the address format replacements to use on Invoices. |
|
| 619 | + * Filters the address format replacements to use on Invoices. |
|
| 620 | 620 | * |
| 621 | - * |
|
| 622 | - * @since 1.0.13 |
|
| 623 | - * |
|
| 624 | - * @param array $replacements The address replacements to use. |
|
| 621 | + * |
|
| 622 | + * @since 1.0.13 |
|
| 623 | + * |
|
| 624 | + * @param array $replacements The address replacements to use. |
|
| 625 | 625 | * @param array $billing_details The billing details to use. |
| 626 | - */ |
|
| 626 | + */ |
|
| 627 | 627 | $replacements = apply_filters( 'wpinv_get_invoice_address_replacements', $args, $billing_details ); |
| 628 | 628 | |
| 629 | 629 | $return = array(); |
@@ -646,5 +646,5 @@ discard block |
||
| 646 | 646 | * @return string |
| 647 | 647 | */ |
| 648 | 648 | function wpinv_trim_formatted_address_line( $line ) { |
| 649 | - return trim( $line, ', ' ); |
|
| 649 | + return trim( $line, ', ' ); |
|
| 650 | 650 | } |
| 651 | 651 | \ No newline at end of file |
@@ -7,15 +7,15 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | // MUST have WordPress. |
| 10 | -if ( !defined( 'WPINC' ) ) { |
|
| 11 | - exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) ); |
|
| 10 | +if (!defined('WPINC')) { |
|
| 11 | + exit('Do NOT access this file directly: ' . basename(__FILE__)); |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | |
| 15 | 15 | function wpinv_get_default_country() { |
| 16 | - $country = wpinv_get_option( 'default_country', 'UK' ); |
|
| 16 | + $country = wpinv_get_option('default_country', 'UK'); |
|
| 17 | 17 | |
| 18 | - return apply_filters( 'wpinv_default_country', $country ); |
|
| 18 | + return apply_filters('wpinv_default_country', $country); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | /** |
@@ -24,59 +24,59 @@ discard block |
||
| 24 | 24 | * @param string $country The country code to sanitize |
| 25 | 25 | * @return array |
| 26 | 26 | */ |
| 27 | -function wpinv_sanitize_country( $country ) { |
|
| 27 | +function wpinv_sanitize_country($country) { |
|
| 28 | 28 | |
| 29 | 29 | // Enure the country is specified |
| 30 | - if ( empty( $country ) ) { |
|
| 30 | + if (empty($country)) { |
|
| 31 | 31 | $country = wpinv_get_default_country(); |
| 32 | 32 | } |
| 33 | - return trim( wpinv_utf8_strtoupper( $country ) ); |
|
| 33 | + return trim(wpinv_utf8_strtoupper($country)); |
|
| 34 | 34 | |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | -function wpinv_is_base_country( $country ) { |
|
| 37 | +function wpinv_is_base_country($country) { |
|
| 38 | 38 | $base_country = wpinv_get_default_country(); |
| 39 | 39 | |
| 40 | - if ( $base_country === 'UK' ) { |
|
| 40 | + if ($base_country === 'UK') { |
|
| 41 | 41 | $base_country = 'GB'; |
| 42 | 42 | } |
| 43 | - if ( $country == 'UK' ) { |
|
| 43 | + if ($country == 'UK') { |
|
| 44 | 44 | $country = 'GB'; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - return ( $country && $country === $base_country ) ? true : false; |
|
| 47 | + return ($country && $country === $base_country) ? true : false; |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | -function wpinv_country_name( $country_code = '' ) { |
|
| 50 | +function wpinv_country_name($country_code = '') { |
|
| 51 | 51 | $countries = wpinv_get_country_list(); |
| 52 | 52 | $country_code = $country_code == 'UK' ? 'GB' : $country_code; |
| 53 | - $country = isset( $countries[$country_code] ) ? $countries[$country_code] : $country_code; |
|
| 53 | + $country = isset($countries[$country_code]) ? $countries[$country_code] : $country_code; |
|
| 54 | 54 | |
| 55 | - return apply_filters( 'wpinv_country_name', $country, $country_code ); |
|
| 55 | + return apply_filters('wpinv_country_name', $country, $country_code); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | function wpinv_get_default_state() { |
| 59 | - $state = wpinv_get_option( 'default_state', false ); |
|
| 59 | + $state = wpinv_get_option('default_state', false); |
|
| 60 | 60 | |
| 61 | - return apply_filters( 'wpinv_default_state', $state ); |
|
| 61 | + return apply_filters('wpinv_default_state', $state); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | -function wpinv_state_name( $state_code = '', $country_code = '' ) { |
|
| 64 | +function wpinv_state_name($state_code = '', $country_code = '') { |
|
| 65 | 65 | $state = $state_code; |
| 66 | 66 | |
| 67 | - if ( !empty( $country_code ) ) { |
|
| 68 | - $states = wpinv_get_country_states( $country_code ); |
|
| 67 | + if (!empty($country_code)) { |
|
| 68 | + $states = wpinv_get_country_states($country_code); |
|
| 69 | 69 | |
| 70 | - $state = !empty( $states ) && isset( $states[$state_code] ) ? $states[$state_code] : $state; |
|
| 70 | + $state = !empty($states) && isset($states[$state_code]) ? $states[$state_code] : $state; |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - return apply_filters( 'wpinv_state_name', $state, $state_code, $country_code ); |
|
| 73 | + return apply_filters('wpinv_state_name', $state, $state_code, $country_code); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | function wpinv_store_address() { |
| 77 | - $address = wpinv_get_option( 'store_address', '' ); |
|
| 77 | + $address = wpinv_get_option('store_address', ''); |
|
| 78 | 78 | |
| 79 | - return apply_filters( 'wpinv_store_address', $address ); |
|
| 79 | + return apply_filters('wpinv_store_address', $address); |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | /** |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | * |
| 85 | 85 | * @param WPInv_Invoice $invoice |
| 86 | 86 | */ |
| 87 | -function getpaid_save_invoice_user_address( $invoice ) { |
|
| 87 | +function getpaid_save_invoice_user_address($invoice) { |
|
| 88 | 88 | |
| 89 | 89 | $address_fields = array( |
| 90 | 90 | 'first_name', |
@@ -99,23 +99,23 @@ discard block |
||
| 99 | 99 | 'zip' |
| 100 | 100 | ); |
| 101 | 101 | |
| 102 | - foreach ( $address_fields as $field ) { |
|
| 102 | + foreach ($address_fields as $field) { |
|
| 103 | 103 | $method = "get_{$field}"; |
| 104 | 104 | $value = $invoice->$method(); |
| 105 | 105 | |
| 106 | 106 | // Only save if it is not empty. |
| 107 | - if ( ! empty( $value ) ) { |
|
| 108 | - update_user_meta( $invoice->get_user_id(), '_wpinv_' . $field, $value ); |
|
| 107 | + if (!empty($value)) { |
|
| 108 | + update_user_meta($invoice->get_user_id(), '_wpinv_' . $field, $value); |
|
| 109 | 109 | } |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | -function wpinv_get_user_address( $user_id = 0, $with_default = true ) { |
|
| 114 | +function wpinv_get_user_address($user_id = 0, $with_default = true) { |
|
| 115 | 115 | global $wpi_userID; |
| 116 | 116 | |
| 117 | - if( empty( $user_id ) ) { |
|
| 118 | - $user_id = !empty( $wpi_userID ) ? $wpi_userID : get_current_user_id(); |
|
| 117 | + if (empty($user_id)) { |
|
| 118 | + $user_id = !empty($wpi_userID) ? $wpi_userID : get_current_user_id(); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | $address_fields = array( |
@@ -131,29 +131,29 @@ discard block |
||
| 131 | 131 | 'zip', |
| 132 | 132 | ); |
| 133 | 133 | |
| 134 | - $user_info = get_userdata( $user_id ); |
|
| 134 | + $user_info = get_userdata($user_id); |
|
| 135 | 135 | |
| 136 | 136 | $address = array(); |
| 137 | 137 | $address['user_id'] = $user_id; |
| 138 | - $address['email'] = !empty( $user_info ) ? $user_info->user_email : ''; |
|
| 139 | - foreach ( $address_fields as $field ) { |
|
| 140 | - $address[$field] = get_user_meta( $user_id, '_wpinv_' . $field, true ); |
|
| 138 | + $address['email'] = !empty($user_info) ? $user_info->user_email : ''; |
|
| 139 | + foreach ($address_fields as $field) { |
|
| 140 | + $address[$field] = get_user_meta($user_id, '_wpinv_' . $field, true); |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | - if ( !empty( $user_info ) ) { |
|
| 144 | - if( empty( $address['first_name'] ) ) |
|
| 143 | + if (!empty($user_info)) { |
|
| 144 | + if (empty($address['first_name'])) |
|
| 145 | 145 | $address['first_name'] = $user_info->first_name; |
| 146 | 146 | |
| 147 | - if( empty( $address['last_name'] ) ) |
|
| 147 | + if (empty($address['last_name'])) |
|
| 148 | 148 | $address['last_name'] = $user_info->last_name; |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | - $address['name'] = trim( trim( $address['first_name'] . ' ' . $address['last_name'] ), "," ); |
|
| 151 | + $address['name'] = trim(trim($address['first_name'] . ' ' . $address['last_name']), ","); |
|
| 152 | 152 | |
| 153 | - if( empty( $address['state'] ) && $with_default ) |
|
| 153 | + if (empty($address['state']) && $with_default) |
|
| 154 | 154 | $address['state'] = wpinv_get_default_state(); |
| 155 | 155 | |
| 156 | - if( empty( $address['country'] ) && $with_default ) |
|
| 156 | + if (empty($address['country']) && $with_default) |
|
| 157 | 157 | $address['country'] = wpinv_get_default_country(); |
| 158 | 158 | |
| 159 | 159 | |
@@ -167,16 +167,16 @@ discard block |
||
| 167 | 167 | * @param string $return What to return. |
| 168 | 168 | * @return array |
| 169 | 169 | */ |
| 170 | -function wpinv_get_continents( $return = 'all' ) { |
|
| 170 | +function wpinv_get_continents($return = 'all') { |
|
| 171 | 171 | |
| 172 | - $continents = wpinv_get_data( 'continents' ); |
|
| 172 | + $continents = wpinv_get_data('continents'); |
|
| 173 | 173 | |
| 174 | - switch( $return ) { |
|
| 174 | + switch ($return) { |
|
| 175 | 175 | case 'name' : |
| 176 | - return wp_list_pluck( $continents, 'name' ); |
|
| 176 | + return wp_list_pluck($continents, 'name'); |
|
| 177 | 177 | break; |
| 178 | 178 | case 'countries' : |
| 179 | - return wp_list_pluck( $continents, 'countries' ); |
|
| 179 | + return wp_list_pluck($continents, 'countries'); |
|
| 180 | 180 | break; |
| 181 | 181 | default : |
| 182 | 182 | return $continents; |
@@ -192,12 +192,12 @@ discard block |
||
| 192 | 192 | * @param string $country Country code. If no code is specified, defaults to the default country. |
| 193 | 193 | * @return string |
| 194 | 194 | */ |
| 195 | -function wpinv_get_continent_code_for_country( $country = false ) { |
|
| 195 | +function wpinv_get_continent_code_for_country($country = false) { |
|
| 196 | 196 | |
| 197 | - $country = wpinv_sanitize_country( $country ); |
|
| 197 | + $country = wpinv_sanitize_country($country); |
|
| 198 | 198 | |
| 199 | - foreach ( wpinv_get_continents( 'countries' ) as $continent_code => $countries ) { |
|
| 200 | - if ( false !== array_search( $country, $countries, true ) ) { |
|
| 199 | + foreach (wpinv_get_continents('countries') as $continent_code => $countries) { |
|
| 200 | + if (false !== array_search($country, $countries, true)) { |
|
| 201 | 201 | return $continent_code; |
| 202 | 202 | } |
| 203 | 203 | } |
@@ -213,13 +213,13 @@ discard block |
||
| 213 | 213 | * @param string $country Country code. If no code is specified, defaults to the default country. |
| 214 | 214 | * @return array |
| 215 | 215 | */ |
| 216 | -function wpinv_get_country_calling_code( $country = null) { |
|
| 216 | +function wpinv_get_country_calling_code($country = null) { |
|
| 217 | 217 | |
| 218 | - $country = wpinv_sanitize_country( $country ); |
|
| 219 | - $codes = wpinv_get_data( 'phone-codes' ); |
|
| 220 | - $code = isset( $codes[ $country ] ) ? $codes[ $country ] : ''; |
|
| 218 | + $country = wpinv_sanitize_country($country); |
|
| 219 | + $codes = wpinv_get_data('phone-codes'); |
|
| 220 | + $code = isset($codes[$country]) ? $codes[$country] : ''; |
|
| 221 | 221 | |
| 222 | - if ( is_array( $code ) ) { |
|
| 222 | + if (is_array($code)) { |
|
| 223 | 223 | return $code[0]; |
| 224 | 224 | } |
| 225 | 225 | return $code; |
@@ -232,8 +232,8 @@ discard block |
||
| 232 | 232 | * @param bool $first_empty Whether or not the first item in the list should be empty |
| 233 | 233 | * @return array |
| 234 | 234 | */ |
| 235 | -function wpinv_get_country_list( $first_empty = false ) { |
|
| 236 | - return wpinv_maybe_add_empty_option( apply_filters( 'wpinv_countries', wpinv_get_data( 'countries' ) ), $first_empty ); |
|
| 235 | +function wpinv_get_country_list($first_empty = false) { |
|
| 236 | + return wpinv_maybe_add_empty_option(apply_filters('wpinv_countries', wpinv_get_data('countries')), $first_empty); |
|
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | /** |
@@ -243,22 +243,22 @@ discard block |
||
| 243 | 243 | * @param bool $first_empty Whether or not the first item in the list should be empty |
| 244 | 244 | * @return array |
| 245 | 245 | */ |
| 246 | -function wpinv_get_country_states( $country = null, $first_empty = false ) { |
|
| 246 | +function wpinv_get_country_states($country = null, $first_empty = false) { |
|
| 247 | 247 | |
| 248 | 248 | // Prepare the country. |
| 249 | - $country = wpinv_sanitize_country( $country ); |
|
| 249 | + $country = wpinv_sanitize_country($country); |
|
| 250 | 250 | |
| 251 | 251 | // Fetch all states. |
| 252 | - $all_states = wpinv_get_data( 'states' ); |
|
| 252 | + $all_states = wpinv_get_data('states'); |
|
| 253 | 253 | |
| 254 | 254 | // Fetch the specified country's states. |
| 255 | - $states = isset( $all_states[ $country ] ) ? $all_states[ $country ] : array() ; |
|
| 256 | - $states = apply_filters( "wpinv_{$country}_states", $states ); |
|
| 257 | - $states = apply_filters( 'wpinv_country_states', $states, $country ); |
|
| 255 | + $states = isset($all_states[$country]) ? $all_states[$country] : array(); |
|
| 256 | + $states = apply_filters("wpinv_{$country}_states", $states); |
|
| 257 | + $states = apply_filters('wpinv_country_states', $states, $country); |
|
| 258 | 258 | |
| 259 | - asort( $states ); |
|
| 259 | + asort($states); |
|
| 260 | 260 | |
| 261 | - return wpinv_maybe_add_empty_option( $states, $first_empty ); |
|
| 261 | + return wpinv_maybe_add_empty_option($states, $first_empty); |
|
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | /** |
@@ -268,7 +268,7 @@ discard block |
||
| 268 | 268 | * @return array |
| 269 | 269 | */ |
| 270 | 270 | function wpinv_get_us_states_list() { |
| 271 | - return apply_filters( 'wpinv_usa_states', wpinv_get_country_states( 'US' ) ); |
|
| 271 | + return apply_filters('wpinv_usa_states', wpinv_get_country_states('US')); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | /** |
@@ -278,7 +278,7 @@ discard block |
||
| 278 | 278 | * @return array |
| 279 | 279 | */ |
| 280 | 280 | function wpinv_get_canada_states_list() { |
| 281 | - return apply_filters( 'wpinv_canada_provinces', wpinv_get_country_states( 'CA' ) ); |
|
| 281 | + return apply_filters('wpinv_canada_provinces', wpinv_get_country_states('CA')); |
|
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | /** |
@@ -288,7 +288,7 @@ discard block |
||
| 288 | 288 | * @return array |
| 289 | 289 | */ |
| 290 | 290 | function wpinv_get_australia_states_list() { |
| 291 | - return apply_filters( 'wpinv_australia_states', wpinv_get_country_states( 'AU' ) ); |
|
| 291 | + return apply_filters('wpinv_australia_states', wpinv_get_country_states('AU')); |
|
| 292 | 292 | } |
| 293 | 293 | |
| 294 | 294 | /** |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | * @return array |
| 299 | 299 | */ |
| 300 | 300 | function wpinv_get_bangladesh_states_list() { |
| 301 | - return apply_filters( 'wpinv_bangladesh_states', wpinv_get_country_states( 'BD' ) ); |
|
| 301 | + return apply_filters('wpinv_bangladesh_states', wpinv_get_country_states('BD')); |
|
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | /** |
@@ -308,7 +308,7 @@ discard block |
||
| 308 | 308 | * @return array |
| 309 | 309 | */ |
| 310 | 310 | function wpinv_get_brazil_states_list() { |
| 311 | - return apply_filters( 'wpinv_brazil_states', wpinv_get_country_states( 'BR' ) ); |
|
| 311 | + return apply_filters('wpinv_brazil_states', wpinv_get_country_states('BR')); |
|
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | /** |
@@ -318,7 +318,7 @@ discard block |
||
| 318 | 318 | * @return array |
| 319 | 319 | */ |
| 320 | 320 | function wpinv_get_bulgaria_states_list() { |
| 321 | - return apply_filters( 'wpinv_bulgaria_states', wpinv_get_country_states( 'BG' ) ); |
|
| 321 | + return apply_filters('wpinv_bulgaria_states', wpinv_get_country_states('BG')); |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | /** |
@@ -328,7 +328,7 @@ discard block |
||
| 328 | 328 | * @return array |
| 329 | 329 | */ |
| 330 | 330 | function wpinv_get_hong_kong_states_list() { |
| 331 | - return apply_filters( 'wpinv_hong_kong_states', wpinv_get_country_states( 'HK' ) ); |
|
| 331 | + return apply_filters('wpinv_hong_kong_states', wpinv_get_country_states('HK')); |
|
| 332 | 332 | } |
| 333 | 333 | |
| 334 | 334 | /** |
@@ -338,7 +338,7 @@ discard block |
||
| 338 | 338 | * @return array |
| 339 | 339 | */ |
| 340 | 340 | function wpinv_get_hungary_states_list() { |
| 341 | - return apply_filters( 'wpinv_hungary_states', wpinv_get_country_states( 'HU' ) ); |
|
| 341 | + return apply_filters('wpinv_hungary_states', wpinv_get_country_states('HU')); |
|
| 342 | 342 | } |
| 343 | 343 | |
| 344 | 344 | /** |
@@ -348,7 +348,7 @@ discard block |
||
| 348 | 348 | * @return array |
| 349 | 349 | */ |
| 350 | 350 | function wpinv_get_japan_states_list() { |
| 351 | - return apply_filters( 'wpinv_japan_states', wpinv_get_country_states( 'JP' ) ); |
|
| 351 | + return apply_filters('wpinv_japan_states', wpinv_get_country_states('JP')); |
|
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | /** |
@@ -358,7 +358,7 @@ discard block |
||
| 358 | 358 | * @return array |
| 359 | 359 | */ |
| 360 | 360 | function wpinv_get_china_states_list() { |
| 361 | - return apply_filters( 'wpinv_china_states', wpinv_get_country_states( 'CN' ) ); |
|
| 361 | + return apply_filters('wpinv_china_states', wpinv_get_country_states('CN')); |
|
| 362 | 362 | } |
| 363 | 363 | |
| 364 | 364 | /** |
@@ -368,7 +368,7 @@ discard block |
||
| 368 | 368 | * @return array |
| 369 | 369 | */ |
| 370 | 370 | function wpinv_get_new_zealand_states_list() { |
| 371 | - return apply_filters( 'wpinv_new_zealand_states', wpinv_get_country_states( 'NZ' ) ); |
|
| 371 | + return apply_filters('wpinv_new_zealand_states', wpinv_get_country_states('NZ')); |
|
| 372 | 372 | } |
| 373 | 373 | |
| 374 | 374 | /** |
@@ -378,7 +378,7 @@ discard block |
||
| 378 | 378 | * @return array |
| 379 | 379 | */ |
| 380 | 380 | function wpinv_get_peru_states_list() { |
| 381 | - return apply_filters( 'wpinv_peru_states', wpinv_get_country_states( 'PE' ) ); |
|
| 381 | + return apply_filters('wpinv_peru_states', wpinv_get_country_states('PE')); |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -388,7 +388,7 @@ discard block |
||
| 388 | 388 | * @return array |
| 389 | 389 | */ |
| 390 | 390 | function wpinv_get_indonesia_states_list() { |
| 391 | - return apply_filters( 'wpinv_indonesia_states', wpinv_get_country_states( 'ID' ) ); |
|
| 391 | + return apply_filters('wpinv_indonesia_states', wpinv_get_country_states('ID')); |
|
| 392 | 392 | } |
| 393 | 393 | |
| 394 | 394 | /** |
@@ -398,7 +398,7 @@ discard block |
||
| 398 | 398 | * @return array |
| 399 | 399 | */ |
| 400 | 400 | function wpinv_get_india_states_list() { |
| 401 | - return apply_filters( 'wpinv_india_states', wpinv_get_country_states( 'IN' ) ); |
|
| 401 | + return apply_filters('wpinv_india_states', wpinv_get_country_states('IN')); |
|
| 402 | 402 | } |
| 403 | 403 | |
| 404 | 404 | /** |
@@ -408,7 +408,7 @@ discard block |
||
| 408 | 408 | * @return array |
| 409 | 409 | */ |
| 410 | 410 | function wpinv_get_iran_states_list() { |
| 411 | - return apply_filters( 'wpinv_iran_states', wpinv_get_country_states( 'IR' ) ); |
|
| 411 | + return apply_filters('wpinv_iran_states', wpinv_get_country_states('IR')); |
|
| 412 | 412 | } |
| 413 | 413 | |
| 414 | 414 | /** |
@@ -418,7 +418,7 @@ discard block |
||
| 418 | 418 | * @return array |
| 419 | 419 | */ |
| 420 | 420 | function wpinv_get_italy_states_list() { |
| 421 | - return apply_filters( 'wpinv_italy_states', wpinv_get_country_states( 'IT' ) ); |
|
| 421 | + return apply_filters('wpinv_italy_states', wpinv_get_country_states('IT')); |
|
| 422 | 422 | } |
| 423 | 423 | |
| 424 | 424 | /** |
@@ -428,7 +428,7 @@ discard block |
||
| 428 | 428 | * @return array |
| 429 | 429 | */ |
| 430 | 430 | function wpinv_get_malaysia_states_list() { |
| 431 | - return apply_filters( 'wpinv_malaysia_states', wpinv_get_country_states( 'MY' ) ); |
|
| 431 | + return apply_filters('wpinv_malaysia_states', wpinv_get_country_states('MY')); |
|
| 432 | 432 | } |
| 433 | 433 | |
| 434 | 434 | /** |
@@ -438,7 +438,7 @@ discard block |
||
| 438 | 438 | * @return array |
| 439 | 439 | */ |
| 440 | 440 | function wpinv_get_mexico_states_list() { |
| 441 | - return apply_filters( 'wpinv_mexico_states', wpinv_get_country_states( 'MX' ) ); |
|
| 441 | + return apply_filters('wpinv_mexico_states', wpinv_get_country_states('MX')); |
|
| 442 | 442 | } |
| 443 | 443 | |
| 444 | 444 | /** |
@@ -448,7 +448,7 @@ discard block |
||
| 448 | 448 | * @return array |
| 449 | 449 | */ |
| 450 | 450 | function wpinv_get_nepal_states_list() { |
| 451 | - return apply_filters( 'wpinv_nepal_states', wpinv_get_country_states( 'NP' ) ); |
|
| 451 | + return apply_filters('wpinv_nepal_states', wpinv_get_country_states('NP')); |
|
| 452 | 452 | } |
| 453 | 453 | |
| 454 | 454 | /** |
@@ -458,7 +458,7 @@ discard block |
||
| 458 | 458 | * @return array |
| 459 | 459 | */ |
| 460 | 460 | function wpinv_get_south_africa_states_list() { |
| 461 | - return apply_filters( 'wpinv_south_africa_states', wpinv_get_country_states( 'ZA' ) ); |
|
| 461 | + return apply_filters('wpinv_south_africa_states', wpinv_get_country_states('ZA')); |
|
| 462 | 462 | } |
| 463 | 463 | |
| 464 | 464 | /** |
@@ -468,7 +468,7 @@ discard block |
||
| 468 | 468 | * @return array |
| 469 | 469 | */ |
| 470 | 470 | function wpinv_get_thailand_states_list() { |
| 471 | - return apply_filters( 'wpinv_thailand_states', wpinv_get_country_states( 'TH' ) ); |
|
| 471 | + return apply_filters('wpinv_thailand_states', wpinv_get_country_states('TH')); |
|
| 472 | 472 | } |
| 473 | 473 | |
| 474 | 474 | /** |
@@ -478,7 +478,7 @@ discard block |
||
| 478 | 478 | * @return array |
| 479 | 479 | */ |
| 480 | 480 | function wpinv_get_turkey_states_list() { |
| 481 | - return apply_filters( 'wpinv_turkey_states', wpinv_get_country_states( 'TR' ) ); |
|
| 481 | + return apply_filters('wpinv_turkey_states', wpinv_get_country_states('TR')); |
|
| 482 | 482 | } |
| 483 | 483 | |
| 484 | 484 | /** |
@@ -488,28 +488,28 @@ discard block |
||
| 488 | 488 | * @return array |
| 489 | 489 | */ |
| 490 | 490 | function wpinv_get_spain_states_list() { |
| 491 | - return apply_filters( 'wpinv_spain_states', wpinv_get_country_states( 'ES' ) ); |
|
| 491 | + return apply_filters('wpinv_spain_states', wpinv_get_country_states('ES')); |
|
| 492 | 492 | } |
| 493 | 493 | |
| 494 | 494 | function wpinv_get_states_field() { |
| 495 | - if( empty( $_POST['country'] ) ) { |
|
| 495 | + if (empty($_POST['country'])) { |
|
| 496 | 496 | $_POST['country'] = wpinv_get_default_country(); |
| 497 | 497 | } |
| 498 | - $states = wpinv_get_country_states( sanitize_text_field( $_POST['country'] ) ); |
|
| 498 | + $states = wpinv_get_country_states(sanitize_text_field($_POST['country'])); |
|
| 499 | 499 | |
| 500 | - if( !empty( $states ) ) { |
|
| 501 | - $sanitized_field_name = sanitize_text_field( $_POST['field_name'] ); |
|
| 500 | + if (!empty($states)) { |
|
| 501 | + $sanitized_field_name = sanitize_text_field($_POST['field_name']); |
|
| 502 | 502 | |
| 503 | 503 | $args = array( |
| 504 | 504 | 'name' => $sanitized_field_name, |
| 505 | 505 | 'id' => $sanitized_field_name, |
| 506 | 506 | 'class' => $sanitized_field_name . 'custom-select wpinv-select wpi_select2', |
| 507 | - 'options' => array_merge( array( '' => '' ), $states ), |
|
| 507 | + 'options' => array_merge(array('' => ''), $states), |
|
| 508 | 508 | 'show_option_all' => false, |
| 509 | 509 | 'show_option_none' => false |
| 510 | 510 | ); |
| 511 | 511 | |
| 512 | - $response = wpinv_html_select( $args ); |
|
| 512 | + $response = wpinv_html_select($args); |
|
| 513 | 513 | |
| 514 | 514 | } else { |
| 515 | 515 | $response = 'nostates'; |
@@ -518,10 +518,10 @@ discard block |
||
| 518 | 518 | return $response; |
| 519 | 519 | } |
| 520 | 520 | |
| 521 | -function wpinv_default_billing_country( $country = '', $user_id = 0 ) { |
|
| 522 | - $country = !empty( $country ) ? $country : wpinv_get_default_country(); |
|
| 521 | +function wpinv_default_billing_country($country = '', $user_id = 0) { |
|
| 522 | + $country = !empty($country) ? $country : wpinv_get_default_country(); |
|
| 523 | 523 | |
| 524 | - return apply_filters( 'wpinv_default_billing_country', $country, $user_id ); |
|
| 524 | + return apply_filters('wpinv_default_billing_country', $country, $user_id); |
|
| 525 | 525 | } |
| 526 | 526 | |
| 527 | 527 | /** |
@@ -533,7 +533,7 @@ discard block |
||
| 533 | 533 | */ |
| 534 | 534 | function wpinv_get_address_formats() { |
| 535 | 535 | |
| 536 | - return apply_filters( 'wpinv_localisation_address_formats', |
|
| 536 | + return apply_filters('wpinv_localisation_address_formats', |
|
| 537 | 537 | array( |
| 538 | 538 | 'default' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}\n{{zip}}\n{{country}}", |
| 539 | 539 | 'AU' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}} {{zip}}\n{{country}}", |
@@ -582,9 +582,9 @@ discard block |
||
| 582 | 582 | * @see `wpinv_get_invoice_address_replacements` |
| 583 | 583 | * @return string |
| 584 | 584 | */ |
| 585 | -function wpinv_get_full_address_format( $country = false) { |
|
| 585 | +function wpinv_get_full_address_format($country = false) { |
|
| 586 | 586 | |
| 587 | - if( empty( $country ) ) { |
|
| 587 | + if (empty($country)) { |
|
| 588 | 588 | $country = wpinv_get_default_country(); |
| 589 | 589 | } |
| 590 | 590 | |
@@ -592,7 +592,7 @@ discard block |
||
| 592 | 592 | $formats = wpinv_get_address_formats(); |
| 593 | 593 | |
| 594 | 594 | // Get format for the specified country. |
| 595 | - $format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default']; |
|
| 595 | + $format = ($country && isset($formats[$country])) ? $formats[$country] : $formats['default']; |
|
| 596 | 596 | |
| 597 | 597 | /** |
| 598 | 598 | * Filters the address format to use on Invoices. |
@@ -604,7 +604,7 @@ discard block |
||
| 604 | 604 | * @param string $format The address format to use. |
| 605 | 605 | * @param string $country The country who's address format is being retrieved. |
| 606 | 606 | */ |
| 607 | - return apply_filters( 'wpinv_get_full_address_format', $format, $country ); |
|
| 607 | + return apply_filters('wpinv_get_full_address_format', $format, $country); |
|
| 608 | 608 | } |
| 609 | 609 | |
| 610 | 610 | /** |
@@ -615,7 +615,7 @@ discard block |
||
| 615 | 615 | * @param array $billing_details customer's billing details |
| 616 | 616 | * @return array |
| 617 | 617 | */ |
| 618 | -function wpinv_get_invoice_address_replacements( $billing_details ) { |
|
| 618 | +function wpinv_get_invoice_address_replacements($billing_details) { |
|
| 619 | 619 | |
| 620 | 620 | $default_args = array( |
| 621 | 621 | 'address' => '', |
@@ -628,22 +628,22 @@ discard block |
||
| 628 | 628 | 'company' => '', |
| 629 | 629 | ); |
| 630 | 630 | |
| 631 | - $args = map_deep( wp_parse_args( $billing_details, $default_args ), 'trim' ); |
|
| 631 | + $args = map_deep(wp_parse_args($billing_details, $default_args), 'trim'); |
|
| 632 | 632 | $state = $args['state']; |
| 633 | 633 | $country = $args['country']; |
| 634 | 634 | |
| 635 | 635 | // Handle full country name. |
| 636 | - $full_country = empty( $country ) ? $country : wpinv_country_name( $country ); |
|
| 636 | + $full_country = empty($country) ? $country : wpinv_country_name($country); |
|
| 637 | 637 | |
| 638 | 638 | // Handle full state name. |
| 639 | - $full_state = ( $country && $state ) ? wpinv_state_name( $state, $country ) : $state; |
|
| 639 | + $full_state = ($country && $state) ? wpinv_state_name($state, $country) : $state; |
|
| 640 | 640 | |
| 641 | 641 | $args['postcode'] = $args['zip']; |
| 642 | 642 | $args['name'] = $args['first_name'] . ' ' . $args['last_name']; |
| 643 | 643 | $args['state'] = $full_state; |
| 644 | 644 | $args['state_code'] = $state; |
| 645 | 645 | $args['country'] = $full_country; |
| 646 | - $args['country_code']= $country; |
|
| 646 | + $args['country_code'] = $country; |
|
| 647 | 647 | |
| 648 | 648 | /** |
| 649 | 649 | * Filters the address format replacements to use on Invoices. |
@@ -654,14 +654,14 @@ discard block |
||
| 654 | 654 | * @param array $replacements The address replacements to use. |
| 655 | 655 | * @param array $billing_details The billing details to use. |
| 656 | 656 | */ |
| 657 | - $replacements = apply_filters( 'wpinv_get_invoice_address_replacements', $args, $billing_details ); |
|
| 657 | + $replacements = apply_filters('wpinv_get_invoice_address_replacements', $args, $billing_details); |
|
| 658 | 658 | |
| 659 | 659 | $return = array(); |
| 660 | 660 | |
| 661 | - foreach( $replacements as $key => $value ) { |
|
| 662 | - $value = is_scalar( $value ) ? trim( sanitize_text_field( $value ) ) : ''; |
|
| 661 | + foreach ($replacements as $key => $value) { |
|
| 662 | + $value = is_scalar($value) ? trim(sanitize_text_field($value)) : ''; |
|
| 663 | 663 | $return['{{' . $key . '}}'] = $value; |
| 664 | - $return['{{' . $key . '_upper}}'] = wpinv_utf8_strtoupper( $value ); |
|
| 664 | + $return['{{' . $key . '_upper}}'] = wpinv_utf8_strtoupper($value); |
|
| 665 | 665 | } |
| 666 | 666 | |
| 667 | 667 | return $return; |
@@ -675,6 +675,6 @@ discard block |
||
| 675 | 675 | * @since 1.0.14 |
| 676 | 676 | * @return string |
| 677 | 677 | */ |
| 678 | -function wpinv_trim_formatted_address_line( $line ) { |
|
| 679 | - return trim( $line, ', ' ); |
|
| 678 | +function wpinv_trim_formatted_address_line($line) { |
|
| 679 | + return trim($line, ', '); |
|
| 680 | 680 | } |
| 681 | 681 | \ No newline at end of file |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | - exit; // Exit if accessed directly |
|
| 11 | + exit; // Exit if accessed directly |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | /** |
@@ -17,10 +17,10 @@ discard block |
||
| 17 | 17 | class GetPaid_Meta_Box_Invoice_Items { |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | - * Output the metabox. |
|
| 21 | - * |
|
| 22 | - * @param WP_Post $post |
|
| 23 | - */ |
|
| 20 | + * Output the metabox. |
|
| 21 | + * |
|
| 22 | + * @param WP_Post $post |
|
| 23 | + */ |
|
| 24 | 24 | public static function output( $post ) { |
| 25 | 25 | |
| 26 | 26 | // Prepare the invoice. |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | * |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 10 | +if (!defined('ABSPATH')) { |
|
| 11 | 11 | exit; // Exit if accessed directly |
| 12 | 12 | } |
| 13 | 13 | |
@@ -21,43 +21,43 @@ discard block |
||
| 21 | 21 | * |
| 22 | 22 | * @param WP_Post $post |
| 23 | 23 | */ |
| 24 | - public static function output( $post ) { |
|
| 24 | + public static function output($post) { |
|
| 25 | 25 | |
| 26 | 26 | // Prepare the invoice. |
| 27 | - $invoice = new WPInv_Invoice( $post ); |
|
| 27 | + $invoice = new WPInv_Invoice($post); |
|
| 28 | 28 | |
| 29 | 29 | // Invoice items. |
| 30 | 30 | $items = $invoice->get_items(); |
| 31 | 31 | |
| 32 | 32 | // New item url. |
| 33 | - $new_item = admin_url( 'post-new.php?post_type=wpi_item' ); |
|
| 33 | + $new_item = admin_url('post-new.php?post_type=wpi_item'); |
|
| 34 | 34 | |
| 35 | 35 | // Totals. |
| 36 | 36 | $totals = array( |
| 37 | 37 | |
| 38 | 38 | 'subtotal' => array( |
| 39 | - 'label' => __( 'Items Subtotal', 'invoicing' ), |
|
| 40 | - 'value' => wpinv_price( wpinv_format_amount( $invoice->get_subtotal() ) ), |
|
| 39 | + 'label' => __('Items Subtotal', 'invoicing'), |
|
| 40 | + 'value' => wpinv_price(wpinv_format_amount($invoice->get_subtotal())), |
|
| 41 | 41 | ), |
| 42 | 42 | |
| 43 | 43 | 'discount' => array( |
| 44 | - 'label' => __( 'Total Discount', 'invoicing' ), |
|
| 45 | - 'value' => wpinv_price( wpinv_format_amount( $invoice->get_total_discount() ) ), |
|
| 44 | + 'label' => __('Total Discount', 'invoicing'), |
|
| 45 | + 'value' => wpinv_price(wpinv_format_amount($invoice->get_total_discount())), |
|
| 46 | 46 | ), |
| 47 | 47 | |
| 48 | 48 | 'tax' => array( |
| 49 | - 'label' => __( 'Total Tax', 'invoicing' ), |
|
| 50 | - 'value' => wpinv_price( wpinv_format_amount( $invoice->get_total_tax() ) ), |
|
| 49 | + 'label' => __('Total Tax', 'invoicing'), |
|
| 50 | + 'value' => wpinv_price(wpinv_format_amount($invoice->get_total_tax())), |
|
| 51 | 51 | ), |
| 52 | 52 | |
| 53 | 53 | 'total' => array( |
| 54 | - 'label' => __( 'Invoice Total', 'invoicing' ), |
|
| 55 | - 'value' => wpinv_price( wpinv_format_amount( $invoice->get_total() ) ), |
|
| 54 | + 'label' => __('Invoice Total', 'invoicing'), |
|
| 55 | + 'value' => wpinv_price(wpinv_format_amount($invoice->get_total())), |
|
| 56 | 56 | ) |
| 57 | 57 | ); |
| 58 | 58 | |
| 59 | - if ( ! wpinv_use_taxes() ) { |
|
| 60 | - unset( $totals['tax'] ); |
|
| 59 | + if (!wpinv_use_taxes()) { |
|
| 60 | + unset($totals['tax']); |
|
| 61 | 61 | } |
| 62 | 62 | ?> |
| 63 | 63 | |
@@ -73,10 +73,10 @@ discard block |
||
| 73 | 73 | } |
| 74 | 74 | </style> |
| 75 | 75 | |
| 76 | - <div class="bsui getpaid-invoice-items-inner <?php echo sanitize_html_class( $invoice->get_template( 'edit' ) ); ?> <?php echo empty( $items ) ? 'no-items' : 'has-items'; ?> <?php echo $invoice->is_paid() || $invoice->is_refunded() ? 'not-editable' : 'editable'; ?>" style="margin-top: 1.5rem"> |
|
| 76 | + <div class="bsui getpaid-invoice-items-inner <?php echo sanitize_html_class($invoice->get_template('edit')); ?> <?php echo empty($items) ? 'no-items' : 'has-items'; ?> <?php echo $invoice->is_paid() || $invoice->is_refunded() ? 'not-editable' : 'editable'; ?>" style="margin-top: 1.5rem"> |
|
| 77 | 77 | |
| 78 | - <?php if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) : ?> |
|
| 79 | - <?php do_action( 'wpinv_meta_box_before_invoice_template_row', $invoice->get_id() ); ?> |
|
| 78 | + <?php if (!$invoice->is_paid() && !$invoice->is_refunded()) : ?> |
|
| 79 | + <?php do_action('wpinv_meta_box_before_invoice_template_row', $invoice->get_id()); ?> |
|
| 80 | 80 | |
| 81 | 81 | <div class="row"> |
| 82 | 82 | <div class="col-12 col-sm-6"> |
@@ -85,15 +85,15 @@ discard block |
||
| 85 | 85 | array( |
| 86 | 86 | 'id' => 'wpinv_template', |
| 87 | 87 | 'name' => 'wpinv_template', |
| 88 | - 'label' => __( 'Template', 'invoicing' ), |
|
| 88 | + 'label' => __('Template', 'invoicing'), |
|
| 89 | 89 | 'label_type' => 'vertical', |
| 90 | - 'placeholder' => __( 'Choose a template', 'invoicing' ), |
|
| 90 | + 'placeholder' => __('Choose a template', 'invoicing'), |
|
| 91 | 91 | 'class' => 'form-control-sm', |
| 92 | - 'value' => $invoice->get_template( 'edit' ), |
|
| 92 | + 'value' => $invoice->get_template('edit'), |
|
| 93 | 93 | 'options' => array( |
| 94 | - 'quantity' => __( 'Quantity', 'invoicing' ), |
|
| 95 | - 'hours' => __( 'Hours', 'invoicing' ), |
|
| 96 | - 'amount' => __( 'Amount Only', 'invoicing' ), |
|
| 94 | + 'quantity' => __('Quantity', 'invoicing'), |
|
| 95 | + 'hours' => __('Hours', 'invoicing'), |
|
| 96 | + 'amount' => __('Amount Only', 'invoicing'), |
|
| 97 | 97 | ), |
| 98 | 98 | 'data-allow-clear' => 'false', |
| 99 | 99 | 'select2' => true, |
@@ -109,11 +109,11 @@ discard block |
||
| 109 | 109 | array( |
| 110 | 110 | 'id' => 'wpinv_currency', |
| 111 | 111 | 'name' => 'wpinv_currency', |
| 112 | - 'label' => __( 'Currency', 'invoicing' ), |
|
| 112 | + 'label' => __('Currency', 'invoicing'), |
|
| 113 | 113 | 'label_type' => 'vertical', |
| 114 | - 'placeholder' => __( 'Select Invoice Currency', 'invoicing' ), |
|
| 114 | + 'placeholder' => __('Select Invoice Currency', 'invoicing'), |
|
| 115 | 115 | 'class' => 'form-control-sm', |
| 116 | - 'value' => $invoice->get_currency( 'edit' ), |
|
| 116 | + 'value' => $invoice->get_currency('edit'), |
|
| 117 | 117 | 'required' => false, |
| 118 | 118 | 'data-allow-clear' => 'false', |
| 119 | 119 | 'select2' => true, |
@@ -125,24 +125,24 @@ discard block |
||
| 125 | 125 | </div> |
| 126 | 126 | </div> |
| 127 | 127 | |
| 128 | - <?php do_action( 'wpinv_meta_box_invoice_template_row', $invoice->get_id() ); ?> |
|
| 128 | + <?php do_action('wpinv_meta_box_invoice_template_row', $invoice->get_id()); ?> |
|
| 129 | 129 | <?php endif; ?> |
| 130 | 130 | |
| 131 | 131 | <table cellpadding="0" cellspacing="0" class="getpaid_invoice_items"> |
| 132 | 132 | <thead> |
| 133 | 133 | <tr> |
| 134 | - <th class="getpaid-item" colspan="2"><?php _e( 'Item', 'invoicing' ) ?></th> |
|
| 134 | + <th class="getpaid-item" colspan="2"><?php _e('Item', 'invoicing') ?></th> |
|
| 135 | 135 | <th class="getpaid-quantity hide-if-amount text-right"> |
| 136 | - <span class="getpaid-hide-if-hours"><?php _e( 'Quantity', 'invoicing' ) ?></span> |
|
| 137 | - <span class="getpaid-hide-if-quantity"><?php _e( 'Hours', 'invoicing' ) ?></span> |
|
| 136 | + <span class="getpaid-hide-if-hours"><?php _e('Quantity', 'invoicing') ?></span> |
|
| 137 | + <span class="getpaid-hide-if-quantity"><?php _e('Hours', 'invoicing') ?></span> |
|
| 138 | 138 | </th> |
| 139 | 139 | <th class="getpaid-price hide-if-amount text-right"> |
| 140 | - <span class="getpaid-hide-if-hours"><?php _e( 'Price', 'invoicing' ) ?></span> |
|
| 141 | - <span class="getpaid-hide-if-quantity"><?php _e( 'Rate', 'invoicing' ) ?></span> |
|
| 140 | + <span class="getpaid-hide-if-hours"><?php _e('Price', 'invoicing') ?></span> |
|
| 141 | + <span class="getpaid-hide-if-quantity"><?php _e('Rate', 'invoicing') ?></span> |
|
| 142 | 142 | </th> |
| 143 | 143 | <th class="getpaid-item-subtotal text-right"> |
| 144 | - <span class="getpaid-hide-if-hours getpaid-hide-if-quantity"><?php _e( 'Amount', 'invoicing' ) ?></span> |
|
| 145 | - <span class="hide-if-amount"><?php _e( 'Total', 'invoicing' ) ?></span> |
|
| 144 | + <span class="getpaid-hide-if-hours getpaid-hide-if-quantity"><?php _e('Amount', 'invoicing') ?></span> |
|
| 145 | + <span class="hide-if-amount"><?php _e('Total', 'invoicing') ?></span> |
|
| 146 | 146 | </th> |
| 147 | 147 | <th class="getpaid-item-actions hide-if-not-editable" width="70px"> </th> |
| 148 | 148 | </tr> |
@@ -150,8 +150,8 @@ discard block |
||
| 150 | 150 | <tbody class="getpaid_invoice_line_items"> |
| 151 | 151 | <tr class="hide-if-has-items hide-if-not-editable"> |
| 152 | 152 | <td colspan="2" class="pt-4 pb-4"> |
| 153 | - <button type="button" class="button button-primary add-invoice-item" data-toggle="modal" data-target="#getpaid-add-items-to-invoice"><?php _e( 'Add Existing Items', 'invoicing' ) ?></button> |
|
| 154 | - <a href="<?php echo esc_url( $new_item ); ?>" target="_blank" class="button button-secondary"><?php _e( 'Create New Item', 'invoicing' ) ?></a> |
|
| 153 | + <button type="button" class="button button-primary add-invoice-item" data-toggle="modal" data-target="#getpaid-add-items-to-invoice"><?php _e('Add Existing Items', 'invoicing') ?></button> |
|
| 154 | + <a href="<?php echo esc_url($new_item); ?>" target="_blank" class="button button-secondary"><?php _e('Create New Item', 'invoicing') ?></a> |
|
| 155 | 155 | </td> |
| 156 | 156 | <td class="hide-if-amount"> </th> |
| 157 | 157 | <td class="hide-if-amount"> </th> |
@@ -183,11 +183,11 @@ discard block |
||
| 183 | 183 | <div class="col-12 col-sm-6 offset-sm-6"> |
| 184 | 184 | <table class="getpaid-invoice-totals text-right w-100"> |
| 185 | 185 | <tbody> |
| 186 | - <?php foreach ( apply_filters( 'getpaid_invoice_subtotal_rows', $totals, $invoice ) as $key => $data ) : ?> |
|
| 187 | - <tr class="getpaid-totals-<?php echo sanitize_html_class( $key ); ?>"> |
|
| 188 | - <td class="label"><?php echo sanitize_text_field( $data['label'] ) ?>:</td> |
|
| 186 | + <?php foreach (apply_filters('getpaid_invoice_subtotal_rows', $totals, $invoice) as $key => $data) : ?> |
|
| 187 | + <tr class="getpaid-totals-<?php echo sanitize_html_class($key); ?>"> |
|
| 188 | + <td class="label"><?php echo sanitize_text_field($data['label']) ?>:</td> |
|
| 189 | 189 | <td width="1%"></td> |
| 190 | - <td class="value"><?php echo sanitize_text_field( $data['value'] ) ?></td> |
|
| 190 | + <td class="value"><?php echo sanitize_text_field($data['value']) ?></td> |
|
| 191 | 191 | </tr> |
| 192 | 192 | <?php endforeach; ?> |
| 193 | 193 | </tbody> |
@@ -200,18 +200,18 @@ discard block |
||
| 200 | 200 | <div class="getpaid-invoice-item-actions hide-if-no-items hide-if-not-editable"> |
| 201 | 201 | <div class="row"> |
| 202 | 202 | <div class="text-left col-12 col-sm-8"> |
| 203 | - <button type="button" class="button add-invoice-item" data-toggle="modal" data-target="#getpaid-add-items-to-invoice"><?php _e( 'Add Existing Item', 'invoicing' ) ?></button> |
|
| 204 | - <a href="<?php echo esc_url( $new_item ); ?>" target="_blank" class="button button-secondary"><?php _e( 'Create New Item', 'invoicing' ) ?></a> |
|
| 205 | - <?php do_action( 'getpaid-invoice-items-actions', $invoice ); ?> |
|
| 203 | + <button type="button" class="button add-invoice-item" data-toggle="modal" data-target="#getpaid-add-items-to-invoice"><?php _e('Add Existing Item', 'invoicing') ?></button> |
|
| 204 | + <a href="<?php echo esc_url($new_item); ?>" target="_blank" class="button button-secondary"><?php _e('Create New Item', 'invoicing') ?></a> |
|
| 205 | + <?php do_action('getpaid-invoice-items-actions', $invoice); ?> |
|
| 206 | 206 | </div> |
| 207 | 207 | <div class="text-right col-12 col-sm-4"> |
| 208 | - <button type="button" class="button button-primary recalculate-totals-button"><?php _e( 'Recalculate Totals', 'invoicing' ) ?></button> |
|
| 208 | + <button type="button" class="button button-primary recalculate-totals-button"><?php _e('Recalculate Totals', 'invoicing') ?></button> |
|
| 209 | 209 | </div> |
| 210 | 210 | </div> |
| 211 | 211 | </div> |
| 212 | 212 | |
| 213 | 213 | <div class="getpaid-invoice-item-actions hide-if-editable"> |
| 214 | - <p class="description m-2 text-right text-muted"><?php _e( 'This invoice is no longer editable', 'invoicing' ); ?></p> |
|
| 214 | + <p class="description m-2 text-right text-muted"><?php _e('This invoice is no longer editable', 'invoicing'); ?></p> |
|
| 215 | 215 | </div> |
| 216 | 216 | |
| 217 | 217 | <!-- Add items to an invoice --> |
@@ -219,8 +219,8 @@ discard block |
||
| 219 | 219 | <div class="modal-dialog modal-dialog-centered" role="document"> |
| 220 | 220 | <div class="modal-content"> |
| 221 | 221 | <div class="modal-header"> |
| 222 | - <h5 class="modal-title" id="getpaid-add-item-to-invoice-label"><?php _e( "Add Item(s)", 'invoicing' ); ?></h5> |
|
| 223 | - <button type="button" class="close" data-dismiss="modal" aria-label="<?php _e( "Close", 'invoicing' ); ?>"> |
|
| 222 | + <h5 class="modal-title" id="getpaid-add-item-to-invoice-label"><?php _e("Add Item(s)", 'invoicing'); ?></h5> |
|
| 223 | + <button type="button" class="close" data-dismiss="modal" aria-label="<?php _e("Close", 'invoicing'); ?>"> |
|
| 224 | 224 | <span aria-hidden="true">×</span> |
| 225 | 225 | </button> |
| 226 | 226 | </div> |
@@ -228,17 +228,17 @@ discard block |
||
| 228 | 228 | <table class="widefat"> |
| 229 | 229 | <thead> |
| 230 | 230 | <tr> |
| 231 | - <th class="pl-0 text-left"><?php _e( 'Item', 'invoicing' ) ?></th> |
|
| 231 | + <th class="pl-0 text-left"><?php _e('Item', 'invoicing') ?></th> |
|
| 232 | 232 | <th class="pr-0 text-right hide-if-amount"> |
| 233 | - <span class="getpaid-hide-if-hours"><?php _e( 'Quantity', 'invoicing' ) ?></span> |
|
| 234 | - <span class="getpaid-hide-if-quantity"><?php _e( 'Hours', 'invoicing' ) ?></span> |
|
| 233 | + <span class="getpaid-hide-if-hours"><?php _e('Quantity', 'invoicing') ?></span> |
|
| 234 | + <span class="getpaid-hide-if-quantity"><?php _e('Hours', 'invoicing') ?></span> |
|
| 235 | 235 | </th> |
| 236 | 236 | </tr> |
| 237 | 237 | </thead> |
| 238 | 238 | <tbody> |
| 239 | 239 | <tr> |
| 240 | 240 | <td class="pl-0 text-left"> |
| 241 | - <select class="getpaid-item-search regular-text" data-placeholder="<?php esc_attr_e( 'Search for an item…', 'invoicing' ); ?>"></select> |
|
| 241 | + <select class="getpaid-item-search regular-text" data-placeholder="<?php esc_attr_e('Search for an item…', 'invoicing'); ?>"></select> |
|
| 242 | 242 | </td> |
| 243 | 243 | <td class="pr-0 text-right hide-if-amount"> |
| 244 | 244 | <input type="number" class="w100" step="1" min="1" autocomplete="off" value="1" placeholder="1"> |
@@ -248,8 +248,8 @@ discard block |
||
| 248 | 248 | </table> |
| 249 | 249 | </div> |
| 250 | 250 | <div class="modal-footer"> |
| 251 | - <button type="button" class="btn btn-secondary getpaid-cancel" data-dismiss="modal"><?php _e( 'Cancel', 'invoicing' ); ?></button> |
|
| 252 | - <button type="button" class="btn btn-primary getpaid-add" data-dismiss="modal"><?php _e( 'Add', 'invoicing' ); ?></button> |
|
| 251 | + <button type="button" class="btn btn-secondary getpaid-cancel" data-dismiss="modal"><?php _e('Cancel', 'invoicing'); ?></button> |
|
| 252 | + <button type="button" class="btn btn-primary getpaid-add" data-dismiss="modal"><?php _e('Add', 'invoicing'); ?></button> |
|
| 253 | 253 | </div> |
| 254 | 254 | </div> |
| 255 | 255 | </div> |
@@ -260,8 +260,8 @@ discard block |
||
| 260 | 260 | <div class="modal-dialog modal-dialog-centered" role="document"> |
| 261 | 261 | <div class="modal-content"> |
| 262 | 262 | <div class="modal-header"> |
| 263 | - <h5 class="modal-title" id="getpaid-edit-invoice-item-label"><?php _e( "Edit Item", 'invoicing' ); ?></h5> |
|
| 264 | - <button type="button" class="close" data-dismiss="modal" aria-label="<?php _e( "Close", 'invoicing' ); ?>"> |
|
| 263 | + <h5 class="modal-title" id="getpaid-edit-invoice-item-label"><?php _e("Edit Item", 'invoicing'); ?></h5> |
|
| 264 | + <button type="button" class="close" data-dismiss="modal" aria-label="<?php _e("Close", 'invoicing'); ?>"> |
|
| 265 | 265 | <span aria-hidden="true">×</span> |
| 266 | 266 | </button> |
| 267 | 267 | </div> |
@@ -269,27 +269,27 @@ discard block |
||
| 269 | 269 | <div class="getpaid-edit-item-div"> |
| 270 | 270 | <input type="hidden" name="id" class="form-control form-control-sm item-id"> |
| 271 | 271 | <label class="form-group w-100"> |
| 272 | - <span><?php _e( 'Name', 'invoicing' ); ?></span> |
|
| 273 | - <input type="text" name="name" placeholder="<?php esc_attr_e( 'Item Name', 'invoicing' ); ?>" class="form-control form-control-sm item-name"> |
|
| 272 | + <span><?php _e('Name', 'invoicing'); ?></span> |
|
| 273 | + <input type="text" name="name" placeholder="<?php esc_attr_e('Item Name', 'invoicing'); ?>" class="form-control form-control-sm item-name"> |
|
| 274 | 274 | </label> |
| 275 | 275 | <label class="form-group w-100"> |
| 276 | - <span class="getpaid-hide-if-hours getpaid-hide-if-quantity item-price"><?php _e( 'Amount', 'invoicing' ); ?></span> |
|
| 277 | - <span class="hide-if-amount"><?php _e( 'Price', 'invoicing' ); ?></span> |
|
| 278 | - <input type="text" name="price" placeholder="<?php wpinv_sanitize_amount( 0 ); ?>" class="form-control form-control-sm item-price"> |
|
| 276 | + <span class="getpaid-hide-if-hours getpaid-hide-if-quantity item-price"><?php _e('Amount', 'invoicing'); ?></span> |
|
| 277 | + <span class="hide-if-amount"><?php _e('Price', 'invoicing'); ?></span> |
|
| 278 | + <input type="text" name="price" placeholder="<?php wpinv_sanitize_amount(0); ?>" class="form-control form-control-sm item-price"> |
|
| 279 | 279 | </label> |
| 280 | 280 | <label class="form-group w-100 hide-if-amount"> |
| 281 | - <span><?php _e( 'Quantity', 'invoicing' ); ?></span> |
|
| 281 | + <span><?php _e('Quantity', 'invoicing'); ?></span> |
|
| 282 | 282 | <input type="number" name="quantity" placeholder="1" class="form-control form-control-sm item-quantity"> |
| 283 | 283 | </label> |
| 284 | 284 | <label class="form-group w-100"> |
| 285 | - <span><?php _e( 'Item Description', 'invoicing' ); ?></span> |
|
| 286 | - <textarea name="description" placeholder="<?php esc_attr_e( 'Enter a description for this item', 'invoicing' ); ?>" class="form-control item-description"></textarea> |
|
| 285 | + <span><?php _e('Item Description', 'invoicing'); ?></span> |
|
| 286 | + <textarea name="description" placeholder="<?php esc_attr_e('Enter a description for this item', 'invoicing'); ?>" class="form-control item-description"></textarea> |
|
| 287 | 287 | </label> |
| 288 | 288 | </div> |
| 289 | 289 | </div> |
| 290 | 290 | <div class="modal-footer"> |
| 291 | - <button type="button" class="btn btn-secondary getpaid-cancel" data-dismiss="modal"><?php _e( 'Cancel', 'invoicing' ); ?></button> |
|
| 292 | - <button type="button" class="btn btn-primary getpaid-save" data-dismiss="modal"><?php _e( 'Save', 'invoicing' ); ?></button> |
|
| 291 | + <button type="button" class="btn btn-secondary getpaid-cancel" data-dismiss="modal"><?php _e('Cancel', 'invoicing'); ?></button> |
|
| 292 | + <button type="button" class="btn btn-primary getpaid-save" data-dismiss="modal"><?php _e('Save', 'invoicing'); ?></button> |
|
| 293 | 293 | </div> |
| 294 | 294 | </div> |
| 295 | 295 | </div> |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | - exit; // Exit if accessed directly |
|
| 11 | + exit; // Exit if accessed directly |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | /** |
@@ -17,10 +17,10 @@ discard block |
||
| 17 | 17 | class GetPaid_Meta_Box_Invoice_Payment_Meta { |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | - * Output the metabox. |
|
| 21 | - * |
|
| 22 | - * @param WP_Post $post |
|
| 23 | - */ |
|
| 20 | + * Output the metabox. |
|
| 21 | + * |
|
| 22 | + * @param WP_Post $post |
|
| 23 | + */ |
|
| 24 | 24 | public static function output( $post ) { |
| 25 | 25 | |
| 26 | 26 | // Prepare the invoice. |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | * |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 10 | +if (!defined('ABSPATH')) { |
|
| 11 | 11 | exit; // Exit if accessed directly |
| 12 | 12 | } |
| 13 | 13 | |
@@ -21,10 +21,10 @@ discard block |
||
| 21 | 21 | * |
| 22 | 22 | * @param WP_Post $post |
| 23 | 23 | */ |
| 24 | - public static function output( $post ) { |
|
| 24 | + public static function output($post) { |
|
| 25 | 25 | |
| 26 | 26 | // Prepare the invoice. |
| 27 | - $invoice = new WPInv_Invoice( $post ); |
|
| 27 | + $invoice = new WPInv_Invoice($post); |
|
| 28 | 28 | |
| 29 | 29 | ?> |
| 30 | 30 | |
@@ -46,10 +46,10 @@ discard block |
||
| 46 | 46 | 'type' => 'text', |
| 47 | 47 | 'id' => 'wpinv_key', |
| 48 | 48 | 'name' => 'wpinv_key', |
| 49 | - 'label' => __( 'Invoice Key:', 'invoicing' ), |
|
| 49 | + 'label' => __('Invoice Key:', 'invoicing'), |
|
| 50 | 50 | 'label_type' => 'vertical', |
| 51 | 51 | 'class' => 'form-control-sm', |
| 52 | - 'value' => $invoice->get_key( 'edit' ), |
|
| 52 | + 'value' => $invoice->get_key('edit'), |
|
| 53 | 53 | 'extra_attributes' => array( |
| 54 | 54 | 'onclick' => 'this.select();', |
| 55 | 55 | 'readonly' => 'true', |
@@ -58,13 +58,13 @@ discard block |
||
| 58 | 58 | ); |
| 59 | 59 | |
| 60 | 60 | // View URL. |
| 61 | - if ( ! $invoice->is_draft() ) { |
|
| 61 | + if (!$invoice->is_draft()) { |
|
| 62 | 62 | echo aui()->input( |
| 63 | 63 | array( |
| 64 | 64 | 'type' => 'text', |
| 65 | 65 | 'id' => 'wpinv_view_url', |
| 66 | 66 | 'name' => 'wpinv_view_url', |
| 67 | - 'label' => __( 'Invoice URL:', 'invoicing' ), |
|
| 67 | + 'label' => __('Invoice URL:', 'invoicing'), |
|
| 68 | 68 | 'label_type' => 'vertical', |
| 69 | 69 | 'class' => 'form-control-sm', |
| 70 | 70 | 'value' => $invoice->get_view_url(), |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | // If the invoice is paid... |
| 80 | - if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
| 80 | + if ($invoice->is_paid() || $invoice->is_refunded()) { |
|
| 81 | 81 | |
| 82 | 82 | // Payment date. |
| 83 | 83 | echo aui()->input( |
@@ -85,11 +85,11 @@ discard block |
||
| 85 | 85 | 'type' => 'datepicker', |
| 86 | 86 | 'id' => 'wpinv_date_completed', |
| 87 | 87 | 'name' => 'date_completed', |
| 88 | - 'label' => __( 'Payment Date:', 'invoicing' ), |
|
| 88 | + 'label' => __('Payment Date:', 'invoicing'), |
|
| 89 | 89 | 'label_type' => 'vertical', |
| 90 | 90 | 'placeholder' => 'YYYY-MM-DD 00:00', |
| 91 | 91 | 'class' => 'form-control-sm', |
| 92 | - 'value' => $invoice->get_date_completed( 'edit' ), |
|
| 92 | + 'value' => $invoice->get_date_completed('edit'), |
|
| 93 | 93 | 'extra_attributes' => array( |
| 94 | 94 | 'data-enable-time' => 'true', |
| 95 | 95 | 'data-time_24hr' => 'true', |
@@ -104,10 +104,10 @@ discard block |
||
| 104 | 104 | 'type' => 'text', |
| 105 | 105 | 'id' => 'wpinv_gateway', |
| 106 | 106 | 'name' => 'wpinv_gateway', |
| 107 | - 'label' => __( 'Gateway:', 'invoicing' ), |
|
| 107 | + 'label' => __('Gateway:', 'invoicing'), |
|
| 108 | 108 | 'label_type' => 'vertical', |
| 109 | 109 | 'class' => 'form-control-sm', |
| 110 | - 'value' => wpinv_get_gateway_admin_label( $invoice->get_gateway( 'edit' ) ), |
|
| 110 | + 'value' => wpinv_get_gateway_admin_label($invoice->get_gateway('edit')), |
|
| 111 | 111 | 'extra_attributes' => array( |
| 112 | 112 | 'onclick' => 'this.select();', |
| 113 | 113 | 'readonly' => 'true', |
@@ -121,11 +121,11 @@ discard block |
||
| 121 | 121 | 'type' => 'text', |
| 122 | 122 | 'id' => 'wpinv_transaction_id', |
| 123 | 123 | 'name' => 'wpinv_transaction_id', |
| 124 | - 'label' => __( 'Transaction ID:', 'invoicing' ), |
|
| 124 | + 'label' => __('Transaction ID:', 'invoicing'), |
|
| 125 | 125 | 'label_type' => 'vertical', |
| 126 | 126 | 'class' => 'form-control-sm', |
| 127 | - 'value' => $invoice->get_transaction_id( 'edit' ), |
|
| 128 | - 'help_text' => apply_filters( 'wpinv_invoice_transaction_link_' . $invoice->get_gateway( 'edit' ), '', $invoice->get_transaction_id(), $invoice ), |
|
| 127 | + 'value' => $invoice->get_transaction_id('edit'), |
|
| 128 | + 'help_text' => apply_filters('wpinv_invoice_transaction_link_' . $invoice->get_gateway('edit'), '', $invoice->get_transaction_id(), $invoice), |
|
| 129 | 129 | 'extra_attributes' => array( |
| 130 | 130 | 'onclick' => 'this.select();', |
| 131 | 131 | 'readonly' => 'true', |
@@ -139,10 +139,10 @@ discard block |
||
| 139 | 139 | 'type' => 'text', |
| 140 | 140 | 'id' => 'wpinv_currency', |
| 141 | 141 | 'name' => 'wpinv_currency', |
| 142 | - 'label' => __( 'Currency:', 'invoicing' ), |
|
| 142 | + 'label' => __('Currency:', 'invoicing'), |
|
| 143 | 143 | 'label_type' => 'vertical', |
| 144 | 144 | 'class' => 'form-control-sm', |
| 145 | - 'value' => $invoice->get_currency( 'edit' ), |
|
| 145 | + 'value' => $invoice->get_currency('edit'), |
|
| 146 | 146 | 'extra_attributes' => array( |
| 147 | 147 | 'onclick' => 'this.select();', |
| 148 | 148 | 'readonly' => 'true', |
@@ -153,13 +153,13 @@ discard block |
||
| 153 | 153 | } else { |
| 154 | 154 | |
| 155 | 155 | // Payment URL. |
| 156 | - if ( ! $invoice->is_draft() ) { |
|
| 156 | + if (!$invoice->is_draft()) { |
|
| 157 | 157 | echo aui()->input( |
| 158 | 158 | array( |
| 159 | 159 | 'type' => 'text', |
| 160 | 160 | 'id' => 'wpinv_payment_url', |
| 161 | 161 | 'name' => 'wpinv_payment_url', |
| 162 | - 'label' => __( 'Payment URL:', 'invoicing' ), |
|
| 162 | + 'label' => __('Payment URL:', 'invoicing'), |
|
| 163 | 163 | 'label_type' => 'vertical', |
| 164 | 164 | 'class' => 'form-control-sm', |
| 165 | 165 | 'value' => $invoice->get_checkout_payment_url(), |
@@ -176,13 +176,13 @@ discard block |
||
| 176 | 176 | array( |
| 177 | 177 | 'id' => 'wpinv_gateway', |
| 178 | 178 | 'name' => 'wpinv_gateway', |
| 179 | - 'label' => __( 'Gateway:', 'invoicing' ), |
|
| 179 | + 'label' => __('Gateway:', 'invoicing'), |
|
| 180 | 180 | 'label_type' => 'vertical', |
| 181 | - 'placeholder' => __( 'Select Gateway', 'invoicing' ), |
|
| 182 | - 'value' => $invoice->get_gateway( 'edit' ), |
|
| 181 | + 'placeholder' => __('Select Gateway', 'invoicing'), |
|
| 182 | + 'value' => $invoice->get_gateway('edit'), |
|
| 183 | 183 | 'select2' => true, |
| 184 | 184 | 'data-allow-clear' => 'false', |
| 185 | - 'options' => wp_list_pluck( wpinv_get_enabled_payment_gateways( true ), 'admin_label' ), |
|
| 185 | + 'options' => wp_list_pluck(wpinv_get_enabled_payment_gateways(true), 'admin_label'), |
|
| 186 | 186 | ) |
| 187 | 187 | ); |
| 188 | 188 | |