Completed
Push — master ( b71328...08c185 )
by Brian
20s queued 15s
created
includes/data-stores/class-getpaid-invoice-data-store.php 2 patches
Indentation   +439 added lines, -439 removed lines patch added patch discarded remove patch
@@ -5,7 +5,7 @@  discard block
 block discarded – undo
5 5
  *
6 6
  */
7 7
 if ( ! defined( 'ABSPATH' ) ) {
8
-	exit;
8
+    exit;
9 9
 }
10 10
 
11 11
 /**
@@ -15,508 +15,508 @@  discard block
 block discarded – undo
15 15
  */
16 16
 class GetPaid_Invoice_Data_Store extends GetPaid_Data_Store_WP {
17 17
 
18
-	/**
19
-	 * Data stored in meta keys, but not considered "meta" for a discount.
20
-	 *
21
-	 * @since 1.0.19
22
-	 * @var array
23
-	 */
24
-	protected $internal_meta_keys = array(
25
-		'_wpinv_subscr_profile_id',
26
-		'_wpinv_taxes',
27
-		'_wpinv_fees',
28
-		'_wpinv_discounts',
29
-		'_wpinv_submission_id',
30
-		'_wpinv_payment_form',
31
-		'_wpinv_is_viewed',
32
-		'wpinv_email_cc',
33
-		'wpinv_template'
34
-	);
35
-
36
-	/**
37
-	 * A map of meta keys to data props.
38
-	 *
39
-	 * @since 1.0.19
40
-	 *
41
-	 * @var array
42
-	 */
43
-	protected $meta_key_to_props = array(
44
-		'_wpinv_subscr_profile_id' => 'subscription_id',
45
-		'_wpinv_taxes'             => 'taxes',
46
-		'_wpinv_fees'              => 'fees',
47
-		'_wpinv_discounts'         => 'discounts',
48
-		'_wpinv_submission_id'     => 'submission_id',
49
-		'_wpinv_payment_form'      => 'payment_form',
50
-		'_wpinv_is_viewed'         => 'is_viewed',
51
-		'wpinv_email_cc'           => 'email_cc',
52
-		'wpinv_template'           => 'template',
53
-	);
54
-
55
-	/**
56
-	 * A map of database fields to data props.
57
-	 *
58
-	 * @since 1.0.19
59
-	 *
60
-	 * @var array
61
-	 */
62
-	protected $database_fields_to_props = array(
63
-		'post_id'            => 'id',
64
-		'number'             => 'number',
65
-		'currency'           => 'currency',
66
-		'key'                => 'key',
67
-		'type'               => 'type',
68
-		'mode'               => 'mode',
69
-		'user_ip'            => 'user_ip',
70
-		'first_name'         => 'first_name',
71
-		'last_name'          => 'last_name',
72
-		'address'            => 'address',
73
-		'city'               => 'city',
74
-		'state'              => 'state',
75
-		'country'            => 'country',
76
-		'zip'                => 'zip',
77
-		'zip'                => 'zip',
78
-		'adddress_confirmed' => 'address_confirmed',
79
-		'gateway'            => 'gateway',
80
-		'transaction_id'     => 'transaction_id',
81
-		'currency'           => 'currency',
82
-		'subtotal'           => 'subtotal',
83
-		'tax'                => 'total_tax',
84
-		'fees_total'         => 'total_fees',
85
-		'discount'           => 'total_discount',
86
-		'total'              => 'total',
87
-		'discount_code'      => 'discount_code',
88
-		'disable_taxes'      => 'disable_taxes',
89
-		'due_date'           => 'due_date',
90
-		'completed_date'     => 'completed_date',
91
-		'company'            => 'company',
92
-		'vat_number'         => 'vat_number',
93
-		'vat_rate'           => 'vat_rate',
94
-	);
95
-
96
-	/*
18
+    /**
19
+     * Data stored in meta keys, but not considered "meta" for a discount.
20
+     *
21
+     * @since 1.0.19
22
+     * @var array
23
+     */
24
+    protected $internal_meta_keys = array(
25
+        '_wpinv_subscr_profile_id',
26
+        '_wpinv_taxes',
27
+        '_wpinv_fees',
28
+        '_wpinv_discounts',
29
+        '_wpinv_submission_id',
30
+        '_wpinv_payment_form',
31
+        '_wpinv_is_viewed',
32
+        'wpinv_email_cc',
33
+        'wpinv_template'
34
+    );
35
+
36
+    /**
37
+     * A map of meta keys to data props.
38
+     *
39
+     * @since 1.0.19
40
+     *
41
+     * @var array
42
+     */
43
+    protected $meta_key_to_props = array(
44
+        '_wpinv_subscr_profile_id' => 'subscription_id',
45
+        '_wpinv_taxes'             => 'taxes',
46
+        '_wpinv_fees'              => 'fees',
47
+        '_wpinv_discounts'         => 'discounts',
48
+        '_wpinv_submission_id'     => 'submission_id',
49
+        '_wpinv_payment_form'      => 'payment_form',
50
+        '_wpinv_is_viewed'         => 'is_viewed',
51
+        'wpinv_email_cc'           => 'email_cc',
52
+        'wpinv_template'           => 'template',
53
+    );
54
+
55
+    /**
56
+     * A map of database fields to data props.
57
+     *
58
+     * @since 1.0.19
59
+     *
60
+     * @var array
61
+     */
62
+    protected $database_fields_to_props = array(
63
+        'post_id'            => 'id',
64
+        'number'             => 'number',
65
+        'currency'           => 'currency',
66
+        'key'                => 'key',
67
+        'type'               => 'type',
68
+        'mode'               => 'mode',
69
+        'user_ip'            => 'user_ip',
70
+        'first_name'         => 'first_name',
71
+        'last_name'          => 'last_name',
72
+        'address'            => 'address',
73
+        'city'               => 'city',
74
+        'state'              => 'state',
75
+        'country'            => 'country',
76
+        'zip'                => 'zip',
77
+        'zip'                => 'zip',
78
+        'adddress_confirmed' => 'address_confirmed',
79
+        'gateway'            => 'gateway',
80
+        'transaction_id'     => 'transaction_id',
81
+        'currency'           => 'currency',
82
+        'subtotal'           => 'subtotal',
83
+        'tax'                => 'total_tax',
84
+        'fees_total'         => 'total_fees',
85
+        'discount'           => 'total_discount',
86
+        'total'              => 'total',
87
+        'discount_code'      => 'discount_code',
88
+        'disable_taxes'      => 'disable_taxes',
89
+        'due_date'           => 'due_date',
90
+        'completed_date'     => 'completed_date',
91
+        'company'            => 'company',
92
+        'vat_number'         => 'vat_number',
93
+        'vat_rate'           => 'vat_rate',
94
+    );
95
+
96
+    /*
97 97
 	|--------------------------------------------------------------------------
98 98
 	| CRUD Methods
99 99
 	|--------------------------------------------------------------------------
100 100
 	*/
101
-	/**
102
-	 * Method to create a new invoice in the database.
103
-	 *
104
-	 * @param WPInv_Invoice $invoice Invoice object.
105
-	 */
106
-	public function create( &$invoice ) {
107
-		$invoice->set_version( WPINV_VERSION );
108
-		$invoice->set_date_created( current_time('mysql') );
109
-
110
-		// Create a new post.
111
-		$id = wp_insert_post(
112
-			apply_filters(
113
-				'getpaid_new_invoice_data',
114
-				array(
115
-					'post_date'     => $invoice->get_date_created( 'edit' ),
116
-					'post_type'     => $invoice->get_post_type( 'edit' ),
117
-					'post_status'   => $this->get_post_status( $invoice ),
118
-					'ping_status'   => 'closed',
119
-					'post_author'   => $invoice->get_user_id( 'edit' ),
120
-					'post_title'    => $invoice->get_title( 'edit' ),
121
-					'post_excerpt'  => $invoice->get_description( 'edit' ),
122
-					'post_parent'   => $invoice->get_parent_id( 'edit' ),
123
-					'post_name'     => $invoice->get_path( 'edit' ),
124
-				)
125
-			),
126
-			true
127
-		);
128
-
129
-		if ( $id && ! is_wp_error( $id ) ) {
130
-
131
-			// Update the new id and regenerate a title.
132
-			$invoice->set_id( $id );
133
-			wp_update_post( array( 'ID' => $invoice->get_id(), 'post_title' => $invoice->get_number( 'edit' ) ) );
134
-
135
-			// Ensure both the key and number are set.
136
-			$invoice->get_key();
137
-			$invoice->get_number();
138
-
139
-			// Save special fields and items.
140
-			$this->save_special_fields( $invoice );
141
-			$this->save_items( $invoice );
142
-
143
-			// Update meta data.
144
-			$this->update_post_meta( $invoice );
145
-			$invoice->save_meta_data();
146
-
147
-			// Apply changes.
148
-			$invoice->apply_changes();
149
-			$this->clear_caches( $invoice );
150
-
151
-			// Fires after a new invoice is created.
152
-			do_action( 'getpaid_new_' . $invoice->get_type(), $invoice->get_id(), $invoice );
153
-			return true;
154
-		}
155
-
156
-		if ( is_wp_error( $id ) ) {
157
-			$invoice->last_error = $id->get_error_message();
158
-		}
159
-
160
-		return false;
161
-	}
162
-
163
-	/**
164
-	 * Method to read an invoice from the database.
165
-	 *
166
-	 * @param WPInv_Invoice $invoice Invoice object.
167
-	 *
168
-	 */
169
-	public function read( &$invoice ) {
170
-
171
-		$invoice->set_defaults();
172
-		$invoice_object = get_post( $invoice->get_id() );
173
-
174
-		if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) {
175
-			$invoice->last_error = __( 'Invalid invoice.', 'invoicing' );
176
-			$invoice->set_id( 0 );
177
-			return false;
178
-		}
179
-
180
-		$invoice->set_props(
181
-			array(
182
-				'date_created'  => 0 < $invoice_object->post_date ? $invoice_object->post_date : null,
183
-				'date_modified' => 0 < $invoice_object->post_modified ? $invoice_object->post_modified : null,
184
-				'status'        => $invoice_object->post_status,
185
-				'author'        => $invoice_object->post_author,
186
-				'description'   => $invoice_object->post_excerpt,
187
-				'parent_id'     => $invoice_object->post_parent,
188
-				'name'          => $invoice_object->post_title,
189
-				'path'          => $invoice_object->post_name,
190
-				'post_type'     => $invoice_object->post_type,
191
-			)
192
-		);
193
-
194
-		$invoice->set_type( $invoice_object->post_type );
195
-
196
-		$this->read_object_data( $invoice, $invoice_object );
197
-		$this->add_special_fields( $invoice );
198
-		$this->add_items( $invoice );
199
-		$invoice->read_meta_data();
200
-		$invoice->set_object_read( true );
201
-		do_action( 'getpaid_read_' . $invoice->get_type(), $invoice->get_id(), $invoice );
202
-
203
-	}
204
-
205
-	/**
206
-	 * Method to update an invoice in the database.
207
-	 *
208
-	 * @param WPInv_Invoice $invoice Invoice object.
209
-	 */
210
-	public function update( &$invoice ) {
211
-		$invoice->save_meta_data();
212
-		$invoice->set_version( WPINV_VERSION );
213
-
214
-		if ( null === $invoice->get_date_created( 'edit' ) ) {
215
-			$invoice->set_date_created(  current_time('mysql') );
216
-		}
217
-
218
-		// Ensure both the key and number are set.
219
-		$invoice->get_key();
220
-		$invoice->get_number();
221
-
222
-		// Grab the current status so we can compare.
223
-		$previous_status = get_post_status( $invoice->get_id() );
224
-
225
-		$changes = $invoice->get_changes();
226
-
227
-		// Only update the post when the post data changes.
228
-		if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) {
229
-			$post_data = array(
230
-				'post_date'         => $invoice->get_date_created( 'edit' ),
231
-				'post_date_gmt'     => $invoice->get_date_created_gmt( 'edit' ),
232
-				'post_status'       => $invoice->get_status( 'edit' ),
233
-				'post_title'        => $invoice->get_name( 'edit' ),
234
-				'post_author'       => $invoice->get_user_id( 'edit' ),
235
-				'post_modified'     => $invoice->get_date_modified( 'edit' ),
236
-				'post_excerpt'      => $invoice->get_description( 'edit' ),
237
-				'post_parent'       => $invoice->get_parent_id( 'edit' ),
238
-				'post_name'         => $invoice->get_path( 'edit' ),
239
-				'post_type'         => $invoice->get_post_type( 'edit' ),
240
-			);
241
-
242
-			/**
243
-			 * When updating this object, to prevent infinite loops, use $wpdb
244
-			 * to update data, since wp_update_post spawns more calls to the
245
-			 * save_post action.
246
-			 *
247
-			 * This ensures hooks are fired by either WP itself (admin screen save),
248
-			 * or an update purely from CRUD.
249
-			 */
250
-			if ( doing_action( 'save_post' ) ) {
251
-				$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) );
252
-				clean_post_cache( $invoice->get_id() );
253
-			} else {
254
-				wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) );
255
-			}
256
-			$invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
257
-		}
258
-
259
-		// Update meta data.
260
-		$this->update_post_meta( $invoice );
261
-
262
-		// Save special fields and items.
263
-		$this->save_special_fields( $invoice );
264
-		$this->save_items( $invoice );
265
-
266
-		// Apply the changes.
267
-		$invoice->apply_changes();
268
-
269
-		// Clear caches.
270
-		$this->clear_caches( $invoice );
271
-
272
-		// Fire a hook depending on the status - this should be considered a creation if it was previously draft status.
273
-		$new_status = $invoice->get_status( 'edit' );
274
-
275
-		if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) {
276
-			do_action( 'getpaid_new_' . $invoice->get_type(), $invoice->get_id(), $invoice );
277
-		} else {
278
-			do_action( 'getpaid_update_' . $invoice->get_type(), $invoice->get_id(), $invoice );
279
-		}
280
-
281
-	}
282
-
283
-	/*
101
+    /**
102
+     * Method to create a new invoice in the database.
103
+     *
104
+     * @param WPInv_Invoice $invoice Invoice object.
105
+     */
106
+    public function create( &$invoice ) {
107
+        $invoice->set_version( WPINV_VERSION );
108
+        $invoice->set_date_created( current_time('mysql') );
109
+
110
+        // Create a new post.
111
+        $id = wp_insert_post(
112
+            apply_filters(
113
+                'getpaid_new_invoice_data',
114
+                array(
115
+                    'post_date'     => $invoice->get_date_created( 'edit' ),
116
+                    'post_type'     => $invoice->get_post_type( 'edit' ),
117
+                    'post_status'   => $this->get_post_status( $invoice ),
118
+                    'ping_status'   => 'closed',
119
+                    'post_author'   => $invoice->get_user_id( 'edit' ),
120
+                    'post_title'    => $invoice->get_title( 'edit' ),
121
+                    'post_excerpt'  => $invoice->get_description( 'edit' ),
122
+                    'post_parent'   => $invoice->get_parent_id( 'edit' ),
123
+                    'post_name'     => $invoice->get_path( 'edit' ),
124
+                )
125
+            ),
126
+            true
127
+        );
128
+
129
+        if ( $id && ! is_wp_error( $id ) ) {
130
+
131
+            // Update the new id and regenerate a title.
132
+            $invoice->set_id( $id );
133
+            wp_update_post( array( 'ID' => $invoice->get_id(), 'post_title' => $invoice->get_number( 'edit' ) ) );
134
+
135
+            // Ensure both the key and number are set.
136
+            $invoice->get_key();
137
+            $invoice->get_number();
138
+
139
+            // Save special fields and items.
140
+            $this->save_special_fields( $invoice );
141
+            $this->save_items( $invoice );
142
+
143
+            // Update meta data.
144
+            $this->update_post_meta( $invoice );
145
+            $invoice->save_meta_data();
146
+
147
+            // Apply changes.
148
+            $invoice->apply_changes();
149
+            $this->clear_caches( $invoice );
150
+
151
+            // Fires after a new invoice is created.
152
+            do_action( 'getpaid_new_' . $invoice->get_type(), $invoice->get_id(), $invoice );
153
+            return true;
154
+        }
155
+
156
+        if ( is_wp_error( $id ) ) {
157
+            $invoice->last_error = $id->get_error_message();
158
+        }
159
+
160
+        return false;
161
+    }
162
+
163
+    /**
164
+     * Method to read an invoice from the database.
165
+     *
166
+     * @param WPInv_Invoice $invoice Invoice object.
167
+     *
168
+     */
169
+    public function read( &$invoice ) {
170
+
171
+        $invoice->set_defaults();
172
+        $invoice_object = get_post( $invoice->get_id() );
173
+
174
+        if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) {
175
+            $invoice->last_error = __( 'Invalid invoice.', 'invoicing' );
176
+            $invoice->set_id( 0 );
177
+            return false;
178
+        }
179
+
180
+        $invoice->set_props(
181
+            array(
182
+                'date_created'  => 0 < $invoice_object->post_date ? $invoice_object->post_date : null,
183
+                'date_modified' => 0 < $invoice_object->post_modified ? $invoice_object->post_modified : null,
184
+                'status'        => $invoice_object->post_status,
185
+                'author'        => $invoice_object->post_author,
186
+                'description'   => $invoice_object->post_excerpt,
187
+                'parent_id'     => $invoice_object->post_parent,
188
+                'name'          => $invoice_object->post_title,
189
+                'path'          => $invoice_object->post_name,
190
+                'post_type'     => $invoice_object->post_type,
191
+            )
192
+        );
193
+
194
+        $invoice->set_type( $invoice_object->post_type );
195
+
196
+        $this->read_object_data( $invoice, $invoice_object );
197
+        $this->add_special_fields( $invoice );
198
+        $this->add_items( $invoice );
199
+        $invoice->read_meta_data();
200
+        $invoice->set_object_read( true );
201
+        do_action( 'getpaid_read_' . $invoice->get_type(), $invoice->get_id(), $invoice );
202
+
203
+    }
204
+
205
+    /**
206
+     * Method to update an invoice in the database.
207
+     *
208
+     * @param WPInv_Invoice $invoice Invoice object.
209
+     */
210
+    public function update( &$invoice ) {
211
+        $invoice->save_meta_data();
212
+        $invoice->set_version( WPINV_VERSION );
213
+
214
+        if ( null === $invoice->get_date_created( 'edit' ) ) {
215
+            $invoice->set_date_created(  current_time('mysql') );
216
+        }
217
+
218
+        // Ensure both the key and number are set.
219
+        $invoice->get_key();
220
+        $invoice->get_number();
221
+
222
+        // Grab the current status so we can compare.
223
+        $previous_status = get_post_status( $invoice->get_id() );
224
+
225
+        $changes = $invoice->get_changes();
226
+
227
+        // Only update the post when the post data changes.
228
+        if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) {
229
+            $post_data = array(
230
+                'post_date'         => $invoice->get_date_created( 'edit' ),
231
+                'post_date_gmt'     => $invoice->get_date_created_gmt( 'edit' ),
232
+                'post_status'       => $invoice->get_status( 'edit' ),
233
+                'post_title'        => $invoice->get_name( 'edit' ),
234
+                'post_author'       => $invoice->get_user_id( 'edit' ),
235
+                'post_modified'     => $invoice->get_date_modified( 'edit' ),
236
+                'post_excerpt'      => $invoice->get_description( 'edit' ),
237
+                'post_parent'       => $invoice->get_parent_id( 'edit' ),
238
+                'post_name'         => $invoice->get_path( 'edit' ),
239
+                'post_type'         => $invoice->get_post_type( 'edit' ),
240
+            );
241
+
242
+            /**
243
+             * When updating this object, to prevent infinite loops, use $wpdb
244
+             * to update data, since wp_update_post spawns more calls to the
245
+             * save_post action.
246
+             *
247
+             * This ensures hooks are fired by either WP itself (admin screen save),
248
+             * or an update purely from CRUD.
249
+             */
250
+            if ( doing_action( 'save_post' ) ) {
251
+                $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) );
252
+                clean_post_cache( $invoice->get_id() );
253
+            } else {
254
+                wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) );
255
+            }
256
+            $invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
257
+        }
258
+
259
+        // Update meta data.
260
+        $this->update_post_meta( $invoice );
261
+
262
+        // Save special fields and items.
263
+        $this->save_special_fields( $invoice );
264
+        $this->save_items( $invoice );
265
+
266
+        // Apply the changes.
267
+        $invoice->apply_changes();
268
+
269
+        // Clear caches.
270
+        $this->clear_caches( $invoice );
271
+
272
+        // Fire a hook depending on the status - this should be considered a creation if it was previously draft status.
273
+        $new_status = $invoice->get_status( 'edit' );
274
+
275
+        if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) {
276
+            do_action( 'getpaid_new_' . $invoice->get_type(), $invoice->get_id(), $invoice );
277
+        } else {
278
+            do_action( 'getpaid_update_' . $invoice->get_type(), $invoice->get_id(), $invoice );
279
+        }
280
+
281
+    }
282
+
283
+    /*
284 284
 	|--------------------------------------------------------------------------
285 285
 	| Additional Methods
286 286
 	|--------------------------------------------------------------------------
287 287
 	*/
288 288
 
289
-	/**
289
+    /**
290 290
      * Retrieves special fields and adds to the invoice.
291
-	 *
292
-	 * @param WPInv_Invoice $invoice Invoice object.
291
+     *
292
+     * @param WPInv_Invoice $invoice Invoice object.
293 293
      */
294 294
     public function add_special_fields( &$invoice ) {
295
-		global $wpdb;
295
+        global $wpdb;
296 296
 
297
-		// Maybe retrieve from the cache.
298
-		$data   = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' );
297
+        // Maybe retrieve from the cache.
298
+        $data   = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' );
299 299
 
300
-		// If not found, retrieve from the db.
301
-		if ( false === $data ) {
302
-			$table =  $wpdb->prefix . 'getpaid_invoices';
300
+        // If not found, retrieve from the db.
301
+        if ( false === $data ) {
302
+            $table =  $wpdb->prefix . 'getpaid_invoices';
303 303
 
304
-			$data  = $wpdb->get_row(
305
-				$wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ),
306
-				ARRAY_A
307
-			);
304
+            $data  = $wpdb->get_row(
305
+                $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ),
306
+                ARRAY_A
307
+            );
308 308
 
309
-			// Update the cache with our data
310
-			wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' );
309
+            // Update the cache with our data
310
+            wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' );
311 311
 
312
-		}
312
+        }
313 313
 
314
-		// Abort if the data does not exist.
315
-		if ( empty( $data ) ) {
316
-			return;
317
-		}
314
+        // Abort if the data does not exist.
315
+        if ( empty( $data ) ) {
316
+            return;
317
+        }
318 318
 
319
-		$props = array();
319
+        $props = array();
320 320
 
321
-		foreach ( $this->database_fields_to_props as $db_field => $prop ) {
321
+        foreach ( $this->database_fields_to_props as $db_field => $prop ) {
322 322
 			
323
-			if ( $db_field == 'post_id' ) {
324
-				continue;
325
-			}
326
-
327
-			$props[ $prop ] = $data[ $db_field ];
328
-		}
329
-
330
-		$invoice->set_props( $props );
331
-
332
-	}
333
-
334
-	/**
335
-	 * Gets a list of special fields that need updated based on change state
336
-	 * or if they are present in the database or not.
337
-	 *
338
-	 * @param  WPInv_Invoice $invoice       The Invoice object.
339
-	 * @return array                        A mapping of field keys => prop names, filtered by ones that should be updated.
340
-	 */
341
-	protected function get_special_fields_to_update( $invoice ) {
342
-		$fields_to_update = array();
343
-		$changed_props   = $invoice->get_changes();
344
-
345
-		// Props should be updated if they are a part of the $changed array or don't exist yet.
346
-		foreach ( $this->database_fields_to_props as $database_field => $prop ) {
347
-			if ( array_key_exists( $prop, $changed_props ) ) {
348
-				$fields_to_update[ $database_field ] = $prop;
349
-			}
350
-		}
351
-
352
-		return $fields_to_update;
353
-	}
354
-
355
-	/**
356
-	 * Helper method that updates all the database fields for an invoice based on it's settings in the WPInv_Invoice class.
357
-	 *
358
-	 * @param WPInv_Invoice $invoice WPInv_Invoice object.
359
-	 * @since 1.0.19
360
-	 */
361
-	protected function update_special_fields( &$invoice ) {
362
-		global $wpdb;
363
-
364
-		$updated_props    = array();
365
-		$fields_to_update = $this->get_special_fields_to_update( $invoice );
366
-
367
-		foreach ( $fields_to_update as $database_field => $prop ) {
368
-			$value = $invoice->{"get_$prop"}( 'edit' );
369
-			$value = is_string( $value ) ? wp_slash( $value ) : $value;
370
-			$value = is_bool( $value ) ? ( int ) $value : $value;
371
-			$updated_props[ $database_field ] = maybe_serialize( $value );
372
-		}
373
-
374
-		if ( ! empty( $updated_props ) ) {
375
-
376
-			$table = $wpdb->prefix . 'getpaid_invoices';
377
-			$wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) );
378
-			wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' );
379
-			do_action( "getpaid_invoice_update_database_fields", $invoice, $updated_props );
380
-
381
-		}
382
-
383
-	}
384
-
385
-	/**
386
-	 * Helper method that inserts special fields to the database.
387
-	 *
388
-	 * @param WPInv_Invoice $invoice WPInv_Invoice object.
389
-	 * @since 1.0.19
390
-	 */
391
-	protected function insert_special_fields( &$invoice ) {
392
-		global $wpdb;
393
-
394
-		$updated_props   = array();
395
-
396
-		foreach ( $this->database_fields_to_props as $database_field => $prop ) {
397
-			$value = $invoice->{"get_$prop"}( 'edit' );
398
-			$value = is_string( $value ) ? wp_slash( $value ) : $value;
399
-			$value = is_bool( $value ) ? ( int ) $value : $value;
400
-			$updated_props[ $database_field ] = maybe_serialize( $value );
401
-		}
402
-
403
-		$table = $wpdb->prefix . 'getpaid_invoices';
404
-		$wpdb->insert( $table, $updated_props );
405
-		wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' );
406
-		do_action( "getpaid_invoice_insert_database_fields", $invoice, $updated_props );
407
-
408
-	}
409
-
410
-	/**
323
+            if ( $db_field == 'post_id' ) {
324
+                continue;
325
+            }
326
+
327
+            $props[ $prop ] = $data[ $db_field ];
328
+        }
329
+
330
+        $invoice->set_props( $props );
331
+
332
+    }
333
+
334
+    /**
335
+     * Gets a list of special fields that need updated based on change state
336
+     * or if they are present in the database or not.
337
+     *
338
+     * @param  WPInv_Invoice $invoice       The Invoice object.
339
+     * @return array                        A mapping of field keys => prop names, filtered by ones that should be updated.
340
+     */
341
+    protected function get_special_fields_to_update( $invoice ) {
342
+        $fields_to_update = array();
343
+        $changed_props   = $invoice->get_changes();
344
+
345
+        // Props should be updated if they are a part of the $changed array or don't exist yet.
346
+        foreach ( $this->database_fields_to_props as $database_field => $prop ) {
347
+            if ( array_key_exists( $prop, $changed_props ) ) {
348
+                $fields_to_update[ $database_field ] = $prop;
349
+            }
350
+        }
351
+
352
+        return $fields_to_update;
353
+    }
354
+
355
+    /**
356
+     * Helper method that updates all the database fields for an invoice based on it's settings in the WPInv_Invoice class.
357
+     *
358
+     * @param WPInv_Invoice $invoice WPInv_Invoice object.
359
+     * @since 1.0.19
360
+     */
361
+    protected function update_special_fields( &$invoice ) {
362
+        global $wpdb;
363
+
364
+        $updated_props    = array();
365
+        $fields_to_update = $this->get_special_fields_to_update( $invoice );
366
+
367
+        foreach ( $fields_to_update as $database_field => $prop ) {
368
+            $value = $invoice->{"get_$prop"}( 'edit' );
369
+            $value = is_string( $value ) ? wp_slash( $value ) : $value;
370
+            $value = is_bool( $value ) ? ( int ) $value : $value;
371
+            $updated_props[ $database_field ] = maybe_serialize( $value );
372
+        }
373
+
374
+        if ( ! empty( $updated_props ) ) {
375
+
376
+            $table = $wpdb->prefix . 'getpaid_invoices';
377
+            $wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) );
378
+            wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' );
379
+            do_action( "getpaid_invoice_update_database_fields", $invoice, $updated_props );
380
+
381
+        }
382
+
383
+    }
384
+
385
+    /**
386
+     * Helper method that inserts special fields to the database.
387
+     *
388
+     * @param WPInv_Invoice $invoice WPInv_Invoice object.
389
+     * @since 1.0.19
390
+     */
391
+    protected function insert_special_fields( &$invoice ) {
392
+        global $wpdb;
393
+
394
+        $updated_props   = array();
395
+
396
+        foreach ( $this->database_fields_to_props as $database_field => $prop ) {
397
+            $value = $invoice->{"get_$prop"}( 'edit' );
398
+            $value = is_string( $value ) ? wp_slash( $value ) : $value;
399
+            $value = is_bool( $value ) ? ( int ) $value : $value;
400
+            $updated_props[ $database_field ] = maybe_serialize( $value );
401
+        }
402
+
403
+        $table = $wpdb->prefix . 'getpaid_invoices';
404
+        $wpdb->insert( $table, $updated_props );
405
+        wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' );
406
+        do_action( "getpaid_invoice_insert_database_fields", $invoice, $updated_props );
407
+
408
+    }
409
+
410
+    /**
411 411
      * Saves all special fields.
412
-	 *
413
-	 * @param WPInv_Invoice $invoice Invoice object.
412
+     *
413
+     * @param WPInv_Invoice $invoice Invoice object.
414 414
      */
415 415
     public function save_special_fields( $invoice ) {
416
-		global $wpdb;
416
+        global $wpdb;
417 417
 
418
-		// The invoices table.
419
-		$table = $wpdb->prefix . 'getpaid_invoices';
420
-		$id    = (int) $invoice->get_id();
418
+        // The invoices table.
419
+        $table = $wpdb->prefix . 'getpaid_invoices';
420
+        $id    = (int) $invoice->get_id();
421 421
 
422
-		if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) {
422
+        if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) {
423 423
 
424
-			$this->update_special_fields( $invoice );
424
+            $this->update_special_fields( $invoice );
425 425
 
426
-		} else {
426
+        } else {
427 427
 
428
-			$this->insert_special_fields( $invoice );
428
+            $this->insert_special_fields( $invoice );
429 429
 
430
-		}
430
+        }
431 431
 
432
-	}
432
+    }
433 433
 
434
-	/**
434
+    /**
435 435
      * Set's up cart details.
436
-	 *
437
-	 * @param WPInv_Invoice $invoice Invoice object.
436
+     *
437
+     * @param WPInv_Invoice $invoice Invoice object.
438 438
      */
439 439
     public function add_items( &$invoice ) {
440
-		global $wpdb;
440
+        global $wpdb;
441 441
 
442
-		// Maybe retrieve from the cache.
443
-		$items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' );
442
+        // Maybe retrieve from the cache.
443
+        $items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' );
444 444
 
445
-		// If not found, retrieve from the db.
446
-		if ( false === $items ) {
447
-			$table =  $wpdb->prefix . 'getpaid_invoice_items';
445
+        // If not found, retrieve from the db.
446
+        if ( false === $items ) {
447
+            $table =  $wpdb->prefix . 'getpaid_invoice_items';
448 448
 
449
-			$items = $wpdb->get_results(
450
-				$wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() )
451
-			);
449
+            $items = $wpdb->get_results(
450
+                $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() )
451
+            );
452 452
 
453
-			// Update the cache with our data
454
-			wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' );
453
+            // Update the cache with our data
454
+            wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' );
455 455
 
456
-		}
456
+        }
457 457
 
458
-		// Abort if no items found.
458
+        // Abort if no items found.
459 459
         if ( empty( $items ) ) {
460 460
             return;
461
-		}
461
+        }
462 462
 
463
-		foreach ( $items as $item_data ) {
464
-			$item = new GetPaid_Form_Item( $item_data->item_id );
463
+        foreach ( $items as $item_data ) {
464
+            $item = new GetPaid_Form_Item( $item_data->item_id );
465 465
 
466
-			// Set item data.
467
-			$item->item_tax      = wpinv_sanitize_amount( $item_data->tax );
468
-			$item->item_discount = wpinv_sanitize_amount( $item_data->discount );
469
-			$item->set_name( $item_data->item_name );
470
-			$item->set_description( $item_data->item_description );
471
-			$item->set_price( $item_data->item_price );
472
-			$item->set_quantity( $item_data->quantity );
473
-			$item->set_item_meta( $item_data->meta );
466
+            // Set item data.
467
+            $item->item_tax      = wpinv_sanitize_amount( $item_data->tax );
468
+            $item->item_discount = wpinv_sanitize_amount( $item_data->discount );
469
+            $item->set_name( $item_data->item_name );
470
+            $item->set_description( $item_data->item_description );
471
+            $item->set_price( $item_data->item_price );
472
+            $item->set_quantity( $item_data->quantity );
473
+            $item->set_item_meta( $item_data->meta );
474 474
 
475
-			$invoice->add_item( $item );
476
-		}
475
+            $invoice->add_item( $item );
476
+        }
477 477
 
478
-	}
478
+    }
479 479
 
480
-	/**
480
+    /**
481 481
      * Saves cart details.
482
-	 *
483
-	 * @param WPInv_Invoice $invoice Invoice object.
482
+     *
483
+     * @param WPInv_Invoice $invoice Invoice object.
484 484
      */
485 485
     public function save_items( $invoice ) {
486 486
 
487
-		// Delete previously existing items.
488
-		$this->delete_items( $invoice );
487
+        // Delete previously existing items.
488
+        $this->delete_items( $invoice );
489 489
 
490
-		$table   =  $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items';
490
+        $table   =  $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items';
491 491
 
492
-		foreach ( $invoice->get_cart_details() as $item_data ) {
493
-			$item_data = array_map( 'maybe_serialize', $item_data );
494
-			$GLOBALS['wpdb']->insert( $table, $item_data );
495
-		}
492
+        foreach ( $invoice->get_cart_details() as $item_data ) {
493
+            $item_data = array_map( 'maybe_serialize', $item_data );
494
+            $GLOBALS['wpdb']->insert( $table, $item_data );
495
+        }
496 496
 
497
-		wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' );
498
-		do_action( "getpaid_invoice_save_items", $invoice );
497
+        wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' );
498
+        do_action( "getpaid_invoice_save_items", $invoice );
499 499
 
500
-	}
500
+    }
501 501
 
502
-	/**
502
+    /**
503 503
      * Deletes an invoice's cart details from the database.
504
-	 *
505
-	 * @param WPInv_Invoice $invoice Invoice object.
504
+     *
505
+     * @param WPInv_Invoice $invoice Invoice object.
506 506
      */
507 507
     public function delete_items( $invoice ) {
508
-		$table =  $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items';
509
-		return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) );
510
-	}
508
+        $table =  $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items';
509
+        return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) );
510
+    }
511 511
 
512
-	/**
512
+    /**
513 513
      * Deletes an invoice's special fields from the database.
514
-	 *
515
-	 * @param WPInv_Invoice $invoice Invoice object.
514
+     *
515
+     * @param WPInv_Invoice $invoice Invoice object.
516 516
      */
517 517
     public function delete_special_fields( $invoice ) {
518
-		$table =  $GLOBALS['wpdb']->prefix . 'getpaid_invoices';
519
-		return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) );
518
+        $table =  $GLOBALS['wpdb']->prefix . 'getpaid_invoices';
519
+        return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) );
520 520
     }
521 521
 
522 522
 }
Please login to merge, or discard this patch.
Spacing   +134 added lines, -134 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
  * GetPaid_Invoice_Data_Store class file.
5 5
  *
6 6
  */
7
-if ( ! defined( 'ABSPATH' ) ) {
7
+if (!defined('ABSPATH')) {
8 8
 	exit;
9 9
 }
10 10
 
@@ -103,57 +103,57 @@  discard block
 block discarded – undo
103 103
 	 *
104 104
 	 * @param WPInv_Invoice $invoice Invoice object.
105 105
 	 */
106
-	public function create( &$invoice ) {
107
-		$invoice->set_version( WPINV_VERSION );
108
-		$invoice->set_date_created( current_time('mysql') );
106
+	public function create(&$invoice) {
107
+		$invoice->set_version(WPINV_VERSION);
108
+		$invoice->set_date_created(current_time('mysql'));
109 109
 
110 110
 		// Create a new post.
111 111
 		$id = wp_insert_post(
112 112
 			apply_filters(
113 113
 				'getpaid_new_invoice_data',
114 114
 				array(
115
-					'post_date'     => $invoice->get_date_created( 'edit' ),
116
-					'post_type'     => $invoice->get_post_type( 'edit' ),
117
-					'post_status'   => $this->get_post_status( $invoice ),
115
+					'post_date'     => $invoice->get_date_created('edit'),
116
+					'post_type'     => $invoice->get_post_type('edit'),
117
+					'post_status'   => $this->get_post_status($invoice),
118 118
 					'ping_status'   => 'closed',
119
-					'post_author'   => $invoice->get_user_id( 'edit' ),
120
-					'post_title'    => $invoice->get_title( 'edit' ),
121
-					'post_excerpt'  => $invoice->get_description( 'edit' ),
122
-					'post_parent'   => $invoice->get_parent_id( 'edit' ),
123
-					'post_name'     => $invoice->get_path( 'edit' ),
119
+					'post_author'   => $invoice->get_user_id('edit'),
120
+					'post_title'    => $invoice->get_title('edit'),
121
+					'post_excerpt'  => $invoice->get_description('edit'),
122
+					'post_parent'   => $invoice->get_parent_id('edit'),
123
+					'post_name'     => $invoice->get_path('edit'),
124 124
 				)
125 125
 			),
126 126
 			true
127 127
 		);
128 128
 
129
-		if ( $id && ! is_wp_error( $id ) ) {
129
+		if ($id && !is_wp_error($id)) {
130 130
 
131 131
 			// Update the new id and regenerate a title.
132
-			$invoice->set_id( $id );
133
-			wp_update_post( array( 'ID' => $invoice->get_id(), 'post_title' => $invoice->get_number( 'edit' ) ) );
132
+			$invoice->set_id($id);
133
+			wp_update_post(array('ID' => $invoice->get_id(), 'post_title' => $invoice->get_number('edit')));
134 134
 
135 135
 			// Ensure both the key and number are set.
136 136
 			$invoice->get_key();
137 137
 			$invoice->get_number();
138 138
 
139 139
 			// Save special fields and items.
140
-			$this->save_special_fields( $invoice );
141
-			$this->save_items( $invoice );
140
+			$this->save_special_fields($invoice);
141
+			$this->save_items($invoice);
142 142
 
143 143
 			// Update meta data.
144
-			$this->update_post_meta( $invoice );
144
+			$this->update_post_meta($invoice);
145 145
 			$invoice->save_meta_data();
146 146
 
147 147
 			// Apply changes.
148 148
 			$invoice->apply_changes();
149
-			$this->clear_caches( $invoice );
149
+			$this->clear_caches($invoice);
150 150
 
151 151
 			// Fires after a new invoice is created.
152
-			do_action( 'getpaid_new_' . $invoice->get_type(), $invoice->get_id(), $invoice );
152
+			do_action('getpaid_new_' . $invoice->get_type(), $invoice->get_id(), $invoice);
153 153
 			return true;
154 154
 		}
155 155
 
156
-		if ( is_wp_error( $id ) ) {
156
+		if (is_wp_error($id)) {
157 157
 			$invoice->last_error = $id->get_error_message();
158 158
 		}
159 159
 
@@ -166,14 +166,14 @@  discard block
 block discarded – undo
166 166
 	 * @param WPInv_Invoice $invoice Invoice object.
167 167
 	 *
168 168
 	 */
169
-	public function read( &$invoice ) {
169
+	public function read(&$invoice) {
170 170
 
171 171
 		$invoice->set_defaults();
172
-		$invoice_object = get_post( $invoice->get_id() );
172
+		$invoice_object = get_post($invoice->get_id());
173 173
 
174
-		if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) {
175
-			$invoice->last_error = __( 'Invalid invoice.', 'invoicing' );
176
-			$invoice->set_id( 0 );
174
+		if (!$invoice->get_id() || !$invoice_object || !getpaid_is_invoice_post_type($invoice_object->post_type)) {
175
+			$invoice->last_error = __('Invalid invoice.', 'invoicing');
176
+			$invoice->set_id(0);
177 177
 			return false;
178 178
 		}
179 179
 
@@ -191,14 +191,14 @@  discard block
 block discarded – undo
191 191
 			)
192 192
 		);
193 193
 
194
-		$invoice->set_type( $invoice_object->post_type );
194
+		$invoice->set_type($invoice_object->post_type);
195 195
 
196
-		$this->read_object_data( $invoice, $invoice_object );
197
-		$this->add_special_fields( $invoice );
198
-		$this->add_items( $invoice );
196
+		$this->read_object_data($invoice, $invoice_object);
197
+		$this->add_special_fields($invoice);
198
+		$this->add_items($invoice);
199 199
 		$invoice->read_meta_data();
200
-		$invoice->set_object_read( true );
201
-		do_action( 'getpaid_read_' . $invoice->get_type(), $invoice->get_id(), $invoice );
200
+		$invoice->set_object_read(true);
201
+		do_action('getpaid_read_' . $invoice->get_type(), $invoice->get_id(), $invoice);
202 202
 
203 203
 	}
204 204
 
@@ -207,12 +207,12 @@  discard block
 block discarded – undo
207 207
 	 *
208 208
 	 * @param WPInv_Invoice $invoice Invoice object.
209 209
 	 */
210
-	public function update( &$invoice ) {
210
+	public function update(&$invoice) {
211 211
 		$invoice->save_meta_data();
212
-		$invoice->set_version( WPINV_VERSION );
212
+		$invoice->set_version(WPINV_VERSION);
213 213
 
214
-		if ( null === $invoice->get_date_created( 'edit' ) ) {
215
-			$invoice->set_date_created(  current_time('mysql') );
214
+		if (null === $invoice->get_date_created('edit')) {
215
+			$invoice->set_date_created(current_time('mysql'));
216 216
 		}
217 217
 
218 218
 		// Ensure both the key and number are set.
@@ -220,23 +220,23 @@  discard block
 block discarded – undo
220 220
 		$invoice->get_number();
221 221
 
222 222
 		// Grab the current status so we can compare.
223
-		$previous_status = get_post_status( $invoice->get_id() );
223
+		$previous_status = get_post_status($invoice->get_id());
224 224
 
225 225
 		$changes = $invoice->get_changes();
226 226
 
227 227
 		// Only update the post when the post data changes.
228
-		if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) {
228
+		if (array_intersect(array('date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path'), array_keys($changes))) {
229 229
 			$post_data = array(
230
-				'post_date'         => $invoice->get_date_created( 'edit' ),
231
-				'post_date_gmt'     => $invoice->get_date_created_gmt( 'edit' ),
232
-				'post_status'       => $invoice->get_status( 'edit' ),
233
-				'post_title'        => $invoice->get_name( 'edit' ),
234
-				'post_author'       => $invoice->get_user_id( 'edit' ),
235
-				'post_modified'     => $invoice->get_date_modified( 'edit' ),
236
-				'post_excerpt'      => $invoice->get_description( 'edit' ),
237
-				'post_parent'       => $invoice->get_parent_id( 'edit' ),
238
-				'post_name'         => $invoice->get_path( 'edit' ),
239
-				'post_type'         => $invoice->get_post_type( 'edit' ),
230
+				'post_date'         => $invoice->get_date_created('edit'),
231
+				'post_date_gmt'     => $invoice->get_date_created_gmt('edit'),
232
+				'post_status'       => $invoice->get_status('edit'),
233
+				'post_title'        => $invoice->get_name('edit'),
234
+				'post_author'       => $invoice->get_user_id('edit'),
235
+				'post_modified'     => $invoice->get_date_modified('edit'),
236
+				'post_excerpt'      => $invoice->get_description('edit'),
237
+				'post_parent'       => $invoice->get_parent_id('edit'),
238
+				'post_name'         => $invoice->get_path('edit'),
239
+				'post_type'         => $invoice->get_post_type('edit'),
240 240
 			);
241 241
 
242 242
 			/**
@@ -247,35 +247,35 @@  discard block
 block discarded – undo
247 247
 			 * This ensures hooks are fired by either WP itself (admin screen save),
248 248
 			 * or an update purely from CRUD.
249 249
 			 */
250
-			if ( doing_action( 'save_post' ) ) {
251
-				$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) );
252
-				clean_post_cache( $invoice->get_id() );
250
+			if (doing_action('save_post')) {
251
+				$GLOBALS['wpdb']->update($GLOBALS['wpdb']->posts, $post_data, array('ID' => $invoice->get_id()));
252
+				clean_post_cache($invoice->get_id());
253 253
 			} else {
254
-				wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) );
254
+				wp_update_post(array_merge(array('ID' => $invoice->get_id()), $post_data));
255 255
 			}
256
-			$invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
256
+			$invoice->read_meta_data(true); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
257 257
 		}
258 258
 
259 259
 		// Update meta data.
260
-		$this->update_post_meta( $invoice );
260
+		$this->update_post_meta($invoice);
261 261
 
262 262
 		// Save special fields and items.
263
-		$this->save_special_fields( $invoice );
264
-		$this->save_items( $invoice );
263
+		$this->save_special_fields($invoice);
264
+		$this->save_items($invoice);
265 265
 
266 266
 		// Apply the changes.
267 267
 		$invoice->apply_changes();
268 268
 
269 269
 		// Clear caches.
270
-		$this->clear_caches( $invoice );
270
+		$this->clear_caches($invoice);
271 271
 
272 272
 		// Fire a hook depending on the status - this should be considered a creation if it was previously draft status.
273
-		$new_status = $invoice->get_status( 'edit' );
273
+		$new_status = $invoice->get_status('edit');
274 274
 
275
-		if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) {
276
-			do_action( 'getpaid_new_' . $invoice->get_type(), $invoice->get_id(), $invoice );
275
+		if ($new_status !== $previous_status && in_array($previous_status, array('new', 'auto-draft', 'draft'), true)) {
276
+			do_action('getpaid_new_' . $invoice->get_type(), $invoice->get_id(), $invoice);
277 277
 		} else {
278
-			do_action( 'getpaid_update_' . $invoice->get_type(), $invoice->get_id(), $invoice );
278
+			do_action('getpaid_update_' . $invoice->get_type(), $invoice->get_id(), $invoice);
279 279
 		}
280 280
 
281 281
 	}
@@ -291,43 +291,43 @@  discard block
 block discarded – undo
291 291
 	 *
292 292
 	 * @param WPInv_Invoice $invoice Invoice object.
293 293
      */
294
-    public function add_special_fields( &$invoice ) {
294
+    public function add_special_fields(&$invoice) {
295 295
 		global $wpdb;
296 296
 
297 297
 		// Maybe retrieve from the cache.
298
-		$data   = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' );
298
+		$data = wp_cache_get($invoice->get_id(), 'getpaid_invoice_special_fields');
299 299
 
300 300
 		// If not found, retrieve from the db.
301
-		if ( false === $data ) {
302
-			$table =  $wpdb->prefix . 'getpaid_invoices';
301
+		if (false === $data) {
302
+			$table = $wpdb->prefix . 'getpaid_invoices';
303 303
 
304 304
 			$data  = $wpdb->get_row(
305
-				$wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ),
305
+				$wpdb->prepare("SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id()),
306 306
 				ARRAY_A
307 307
 			);
308 308
 
309 309
 			// Update the cache with our data
310
-			wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' );
310
+			wp_cache_set($invoice->get_id(), $data, 'getpaid_invoice_special_fields');
311 311
 
312 312
 		}
313 313
 
314 314
 		// Abort if the data does not exist.
315
-		if ( empty( $data ) ) {
315
+		if (empty($data)) {
316 316
 			return;
317 317
 		}
318 318
 
319 319
 		$props = array();
320 320
 
321
-		foreach ( $this->database_fields_to_props as $db_field => $prop ) {
321
+		foreach ($this->database_fields_to_props as $db_field => $prop) {
322 322
 			
323
-			if ( $db_field == 'post_id' ) {
323
+			if ($db_field == 'post_id') {
324 324
 				continue;
325 325
 			}
326 326
 
327
-			$props[ $prop ] = $data[ $db_field ];
327
+			$props[$prop] = $data[$db_field];
328 328
 		}
329 329
 
330
-		$invoice->set_props( $props );
330
+		$invoice->set_props($props);
331 331
 
332 332
 	}
333 333
 
@@ -338,14 +338,14 @@  discard block
 block discarded – undo
338 338
 	 * @param  WPInv_Invoice $invoice       The Invoice object.
339 339
 	 * @return array                        A mapping of field keys => prop names, filtered by ones that should be updated.
340 340
 	 */
341
-	protected function get_special_fields_to_update( $invoice ) {
341
+	protected function get_special_fields_to_update($invoice) {
342 342
 		$fields_to_update = array();
343
-		$changed_props   = $invoice->get_changes();
343
+		$changed_props = $invoice->get_changes();
344 344
 
345 345
 		// Props should be updated if they are a part of the $changed array or don't exist yet.
346
-		foreach ( $this->database_fields_to_props as $database_field => $prop ) {
347
-			if ( array_key_exists( $prop, $changed_props ) ) {
348
-				$fields_to_update[ $database_field ] = $prop;
346
+		foreach ($this->database_fields_to_props as $database_field => $prop) {
347
+			if (array_key_exists($prop, $changed_props)) {
348
+				$fields_to_update[$database_field] = $prop;
349 349
 			}
350 350
 		}
351 351
 
@@ -358,25 +358,25 @@  discard block
 block discarded – undo
358 358
 	 * @param WPInv_Invoice $invoice WPInv_Invoice object.
359 359
 	 * @since 1.0.19
360 360
 	 */
361
-	protected function update_special_fields( &$invoice ) {
361
+	protected function update_special_fields(&$invoice) {
362 362
 		global $wpdb;
363 363
 
364 364
 		$updated_props    = array();
365
-		$fields_to_update = $this->get_special_fields_to_update( $invoice );
365
+		$fields_to_update = $this->get_special_fields_to_update($invoice);
366 366
 
367
-		foreach ( $fields_to_update as $database_field => $prop ) {
368
-			$value = $invoice->{"get_$prop"}( 'edit' );
369
-			$value = is_string( $value ) ? wp_slash( $value ) : $value;
370
-			$value = is_bool( $value ) ? ( int ) $value : $value;
371
-			$updated_props[ $database_field ] = maybe_serialize( $value );
367
+		foreach ($fields_to_update as $database_field => $prop) {
368
+			$value = $invoice->{"get_$prop"}('edit');
369
+			$value = is_string($value) ? wp_slash($value) : $value;
370
+			$value = is_bool($value) ? (int) $value : $value;
371
+			$updated_props[$database_field] = maybe_serialize($value);
372 372
 		}
373 373
 
374
-		if ( ! empty( $updated_props ) ) {
374
+		if (!empty($updated_props)) {
375 375
 
376 376
 			$table = $wpdb->prefix . 'getpaid_invoices';
377
-			$wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) );
378
-			wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' );
379
-			do_action( "getpaid_invoice_update_database_fields", $invoice, $updated_props );
377
+			$wpdb->update($table, $updated_props, array('post_id' => $invoice->get_id()));
378
+			wp_cache_delete($invoice->get_id(), 'getpaid_invoice_special_fields');
379
+			do_action("getpaid_invoice_update_database_fields", $invoice, $updated_props);
380 380
 
381 381
 		}
382 382
 
@@ -388,22 +388,22 @@  discard block
 block discarded – undo
388 388
 	 * @param WPInv_Invoice $invoice WPInv_Invoice object.
389 389
 	 * @since 1.0.19
390 390
 	 */
391
-	protected function insert_special_fields( &$invoice ) {
391
+	protected function insert_special_fields(&$invoice) {
392 392
 		global $wpdb;
393 393
 
394
-		$updated_props   = array();
394
+		$updated_props = array();
395 395
 
396
-		foreach ( $this->database_fields_to_props as $database_field => $prop ) {
397
-			$value = $invoice->{"get_$prop"}( 'edit' );
398
-			$value = is_string( $value ) ? wp_slash( $value ) : $value;
399
-			$value = is_bool( $value ) ? ( int ) $value : $value;
400
-			$updated_props[ $database_field ] = maybe_serialize( $value );
396
+		foreach ($this->database_fields_to_props as $database_field => $prop) {
397
+			$value = $invoice->{"get_$prop"}('edit');
398
+			$value = is_string($value) ? wp_slash($value) : $value;
399
+			$value = is_bool($value) ? (int) $value : $value;
400
+			$updated_props[$database_field] = maybe_serialize($value);
401 401
 		}
402 402
 
403 403
 		$table = $wpdb->prefix . 'getpaid_invoices';
404
-		$wpdb->insert( $table, $updated_props );
405
-		wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' );
406
-		do_action( "getpaid_invoice_insert_database_fields", $invoice, $updated_props );
404
+		$wpdb->insert($table, $updated_props);
405
+		wp_cache_delete($invoice->get_id(), 'getpaid_invoice_special_fields');
406
+		do_action("getpaid_invoice_insert_database_fields", $invoice, $updated_props);
407 407
 
408 408
 	}
409 409
 
@@ -412,20 +412,20 @@  discard block
 block discarded – undo
412 412
 	 *
413 413
 	 * @param WPInv_Invoice $invoice Invoice object.
414 414
      */
415
-    public function save_special_fields( $invoice ) {
415
+    public function save_special_fields($invoice) {
416 416
 		global $wpdb;
417 417
 
418 418
 		// The invoices table.
419 419
 		$table = $wpdb->prefix . 'getpaid_invoices';
420 420
 		$id    = (int) $invoice->get_id();
421 421
 
422
-		if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) {
422
+		if ($wpdb->get_var("SELECT `post_id` FROM $table WHERE `post_id`= $id")) {
423 423
 
424
-			$this->update_special_fields( $invoice );
424
+			$this->update_special_fields($invoice);
425 425
 
426 426
 		} else {
427 427
 
428
-			$this->insert_special_fields( $invoice );
428
+			$this->insert_special_fields($invoice);
429 429
 
430 430
 		}
431 431
 
@@ -436,43 +436,43 @@  discard block
 block discarded – undo
436 436
 	 *
437 437
 	 * @param WPInv_Invoice $invoice Invoice object.
438 438
      */
439
-    public function add_items( &$invoice ) {
439
+    public function add_items(&$invoice) {
440 440
 		global $wpdb;
441 441
 
442 442
 		// Maybe retrieve from the cache.
443
-		$items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' );
443
+		$items = wp_cache_get($invoice->get_id(), 'getpaid_invoice_cart_details');
444 444
 
445 445
 		// If not found, retrieve from the db.
446
-		if ( false === $items ) {
447
-			$table =  $wpdb->prefix . 'getpaid_invoice_items';
446
+		if (false === $items) {
447
+			$table = $wpdb->prefix . 'getpaid_invoice_items';
448 448
 
449 449
 			$items = $wpdb->get_results(
450
-				$wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() )
450
+				$wpdb->prepare("SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id())
451 451
 			);
452 452
 
453 453
 			// Update the cache with our data
454
-			wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' );
454
+			wp_cache_set($invoice->get_id(), $items, 'getpaid_invoice_cart_details');
455 455
 
456 456
 		}
457 457
 
458 458
 		// Abort if no items found.
459
-        if ( empty( $items ) ) {
459
+        if (empty($items)) {
460 460
             return;
461 461
 		}
462 462
 
463
-		foreach ( $items as $item_data ) {
464
-			$item = new GetPaid_Form_Item( $item_data->item_id );
463
+		foreach ($items as $item_data) {
464
+			$item = new GetPaid_Form_Item($item_data->item_id);
465 465
 
466 466
 			// Set item data.
467
-			$item->item_tax      = wpinv_sanitize_amount( $item_data->tax );
468
-			$item->item_discount = wpinv_sanitize_amount( $item_data->discount );
469
-			$item->set_name( $item_data->item_name );
470
-			$item->set_description( $item_data->item_description );
471
-			$item->set_price( $item_data->item_price );
472
-			$item->set_quantity( $item_data->quantity );
473
-			$item->set_item_meta( $item_data->meta );
474
-
475
-			$invoice->add_item( $item );
467
+			$item->item_tax      = wpinv_sanitize_amount($item_data->tax);
468
+			$item->item_discount = wpinv_sanitize_amount($item_data->discount);
469
+			$item->set_name($item_data->item_name);
470
+			$item->set_description($item_data->item_description);
471
+			$item->set_price($item_data->item_price);
472
+			$item->set_quantity($item_data->quantity);
473
+			$item->set_item_meta($item_data->meta);
474
+
475
+			$invoice->add_item($item);
476 476
 		}
477 477
 
478 478
 	}
@@ -482,20 +482,20 @@  discard block
 block discarded – undo
482 482
 	 *
483 483
 	 * @param WPInv_Invoice $invoice Invoice object.
484 484
      */
485
-    public function save_items( $invoice ) {
485
+    public function save_items($invoice) {
486 486
 
487 487
 		// Delete previously existing items.
488
-		$this->delete_items( $invoice );
488
+		$this->delete_items($invoice);
489 489
 
490
-		$table   =  $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items';
490
+		$table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items';
491 491
 
492
-		foreach ( $invoice->get_cart_details() as $item_data ) {
493
-			$item_data = array_map( 'maybe_serialize', $item_data );
494
-			$GLOBALS['wpdb']->insert( $table, $item_data );
492
+		foreach ($invoice->get_cart_details() as $item_data) {
493
+			$item_data = array_map('maybe_serialize', $item_data);
494
+			$GLOBALS['wpdb']->insert($table, $item_data);
495 495
 		}
496 496
 
497
-		wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' );
498
-		do_action( "getpaid_invoice_save_items", $invoice );
497
+		wp_cache_delete($invoice->get_id(), 'getpaid_invoice_cart_details');
498
+		do_action("getpaid_invoice_save_items", $invoice);
499 499
 
500 500
 	}
501 501
 
@@ -504,9 +504,9 @@  discard block
 block discarded – undo
504 504
 	 *
505 505
 	 * @param WPInv_Invoice $invoice Invoice object.
506 506
      */
507
-    public function delete_items( $invoice ) {
508
-		$table =  $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items';
509
-		return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) );
507
+    public function delete_items($invoice) {
508
+		$table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items';
509
+		return $GLOBALS['wpdb']->delete($table, array('post_id' => $invoice->get_id()));
510 510
 	}
511 511
 
512 512
 	/**
@@ -514,9 +514,9 @@  discard block
 block discarded – undo
514 514
 	 *
515 515
 	 * @param WPInv_Invoice $invoice Invoice object.
516 516
      */
517
-    public function delete_special_fields( $invoice ) {
518
-		$table =  $GLOBALS['wpdb']->prefix . 'getpaid_invoices';
519
-		return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) );
517
+    public function delete_special_fields($invoice) {
518
+		$table = $GLOBALS['wpdb']->prefix . 'getpaid_invoices';
519
+		return $GLOBALS['wpdb']->delete($table, array('post_id' => $invoice->get_id()));
520 520
     }
521 521
 
522 522
 }
Please login to merge, or discard this patch.