Passed
Pull Request — master (#371)
by Brian
88:26
created

GetPaid_Item_Data_Store_CPT::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 27
rs 9.584
1
<?php
2
/**
3
 * GetPaid_Order_Data_Store_CPT class file.
4
 *
5
 */
6
if ( ! defined( 'ABSPATH' ) ) {
7
	exit;
8
}
9
10
/**
11
 * Abstract Order Data Store: Stored in CPT.
12
 *
13
 * @version  1.0.19
14
 */
15
abstract class GetPaid_Item_Data_Store_CPT extends GetPaid_Data_Store_WP {
16
17
	/**
18
	 * Internal meta type used to store order data.
19
	 *
20
	 * @var string
21
	 */
22
	protected $meta_type = 'post';
23
24
	/**
25
	 * Data stored in meta keys, but not considered "meta" for an item.
26
	 *
27
	 * @since 1.0.19
28
	 * @var array
29
	 */
30
	protected $internal_meta_keys = array(
31
		'_wpinv_price',
32
		'_wpinv_vat_rule',
33
		'_wpinv_vat_class',
34
		'_wpinv_type',
35
		'_wpinv_custom_id',
36
		'_wpinv_custom_name',
37
		'_wpinv_custom_singular_name',
38
		'_wpinv_editable',
39
		'_wpinv_dynamic_pricing',
40
		'_minimum_price',
41
		'_wpinv_is_recurring',
42
		'_wpinv_recurring_period',
43
		'_wpinv_recurring_interval',
44
		'_wpinv_recurring_limit',
45
		'_wpinv_free_trial',
46
		'_wpinv_trial_period',
47
		'_wpinv_signup_fee',
48
		'_wpinv_trial_interval'
49
	);
50
51
	/*
52
	|--------------------------------------------------------------------------
53
	| CRUD Methods
54
	|--------------------------------------------------------------------------
55
	*/
56
57
	/**
58
	 * Method to create a new item in the database.
59
	 *
60
	 * @param WPInv_Item $item Item object.
61
	 */
62
	public function create( &$item ) {
63
		$item->set_version( WPINV_VERSION );
64
		$item->set_date_created( current_time('mysql') );
65
66
		$id = wp_insert_post(
67
			apply_filters(
68
				'getpaid_new_item_data',
69
				array(
70
					'post_date'     => $item->get_date_created( 'edit' ),
71
					'post_type'     => 'wpi_item',
72
					'post_status'   => $this->get_post_status( $item ),
73
					'ping_status'   => 'closed',
74
					'post_author'   => $item->get_author( 'edit' ),
75
					'post_title'    => $item->get_title( 'edit' ),
76
					'post_parent'   => 0,
77
					'post_excerpt'  => $item->get_description( 'edit' ),
78
				)
79
			),
80
			true
81
		);
82
83
		if ( $id && ! is_wp_error( $id ) ) {
84
			$item->set_id( $id );
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type WP_Error; however, parameter $id of GetPaid_Data::set_id() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
			$item->set_id( /** @scrutinizer ignore-type */ $id );
Loading history...
85
			$this->update_post_meta( $item );
86
			$item->save_meta_data();
87
			$item->apply_changes();
88
			$this->clear_caches( $item );
89
		}
90
	}
91
92
	/**
93
	 * Method to read an order from the database.
94
	 *
95
	 * @param WPInv_Item $item Item object.
96
	 *
97
	 * @throws Exception If passed item is invalid.
98
	 */
99
	public function read( &$item ) {
100
		$item->set_defaults();
101
		$item_object = get_post( $item->get_id() );
102
103
		if ( ! $item->get_id() || ! $item_object || ! $item_object->post_type != 'wpi_item' ) {
104
			throw new Exception( __( 'Invalid item.', 'invoicing' ) );
105
		}
106
107
		$item->set_props(
108
			array(
109
				'parent_id'     => $item_object->post_parent,
110
				'date_created'  => 0 < $item_object->post_date_gmt ? $item_object->post_date_gmt : null,
111
				'date_modified' => 0 < $item_object->post_modified_gmt ? $item_object->post_modified_gmt : null,
112
				'status'        => $item_object->post_status,
113
				'name'          => $item_object->post_title,
114
				'description'   => $item_object->post_excerpt,
115
				'author'        => $item_object->post_author,
116
			)
117
		);
118
119
		$this->read_item_data( $item, $item_object );
0 ignored issues
show
Bug introduced by
The method read_item_data() does not exist on GetPaid_Item_Data_Store_CPT. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
		$this->/** @scrutinizer ignore-call */ 
120
         read_item_data( $item, $item_object );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
		$item->read_meta_data();
121
		$item->set_object_read( true );
122
123
	}
124
125
	/**
126
	 * Method to update an item in the database.
127
	 *
128
	 * @param WPInv_Item $item Order object.
129
	 */
130
	public function update( &$item ) {
131
		$item->save_meta_data();
132
		$item->set_version( WPINV_VERSION );
133
134
		if ( null === $item->get_date_created( 'edit' ) ) {
0 ignored issues
show
introduced by
The condition null === $item->get_date_created('edit') is always false.
Loading history...
135
			$item->set_date_created(  current_time('mysql') );
136
		}
137
138
		$changes = $item->get_changes();
139
140
		// Only update the post when the post data changes.
141
		if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'parent_id', 'post_excerpt' ), array_keys( $changes ) ) ) {
142
			$post_data = array(
143
				'post_date'         => $item->get_date_created( 'edit' ),
144
				'post_status'       => $item->get_status( $item ),
145
				'post_parent'       => $item->get_parent_id(),
146
				'post_excerpt'      => $item->get_description(),
147
				'post_modified'     => $item->get_date_modified( 'edit' ),
148
			);
149
150
			/**
151
			 * When updating this object, to prevent infinite loops, use $wpdb
152
			 * to update data, since wp_update_post spawns more calls to the
153
			 * save_post action.
154
			 *
155
			 * This ensures hooks are fired by either WP itself (admin screen save),
156
			 * or an update purely from CRUD.
157
			 */
158
			if ( doing_action( 'save_post' ) ) {
159
				$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $item->get_id() ) );
160
				clean_post_cache( $item->get_id() );
161
			} else {
162
				wp_update_post( array_merge( array( 'ID' => $item->get_id() ), $post_data ) );
163
			}
164
			$item->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
165
		}
166
		$this->update_post_meta( $item );
167
		$item->apply_changes();
168
		$this->clear_caches( $item );
169
	}
170
171
	/**
172
	 * Method to delete an item from the database.
173
	 *
174
	 * @param WPInv_Item $item Item object.
175
	 * @param array    $args Array of args to pass to the delete method.
176
	 *
177
	 * @return void
178
	 */
179
	public function delete( &$item, $args = array() ) {
180
		$id   = $item->get_id();
181
		$args = wp_parse_args(
182
			$args,
183
			array(
184
				'force_delete' => false,
185
			)
186
		);
187
188
		if ( ! $id ) {
189
			return;
190
		}
191
192
		if ( $args['force_delete'] ) {
193
			wp_delete_post( $id );
194
			$item->set_id( 0 );
195
			do_action( 'getpaid_delete_item', $id );
196
		} else {
197
			wp_trash_post( $id );
198
			$item->set_status( 'trash' );
199
			do_action( 'getpaid_trash_item', $id );
200
		}
201
	}
202
203
	/*
204
	|--------------------------------------------------------------------------
205
	| Additional Methods
206
	|--------------------------------------------------------------------------
207
	*/
208
209
	/**
210
	 * Get the status to save to the post object.
211
	 *
212
	 *
213
	 * @since 1.0.19
214
	 * @param  WPInv_item $item Item object.
215
	 * @return string
216
	 */
217
	protected function get_post_status( $item ) {
218
		$item_status = $item->get_status( 'edit' );
219
220
		if ( ! $item_status ) {
221
			$item_status = apply_filters( 'getpaid_default_item_status', 'draft' );
222
		}
223
224
		return $item_status;
225
	}
226
227
	/**
228
	 * Read item data.
229
	 *
230
	 * @param WPInv_Item $item Item object.
231
	 * @param object   $post_object Post object.
232
	 * @since 1.0.19
233
	 */
234
	protected function read_order_data( &$item, $post_object ) {
235
		$id = $item->get_id();
236
237
		// Set item properties.
238
		$item->set_props(
239
			array(
240
				'price'                => get_post_meta( $id, '_wpinv_price', true ),
241
				'vat_rule'             => get_post_meta( $id, '_wpinv_vat_rule', true ),
242
				'vat_class'            => get_post_meta( $id, '_wpinv_vat_class', true ),
243
				'type'                 => get_post_meta( $id, '_wpinv_type', true ),
244
				'custom_id'            => get_post_meta( $id, '_wpinv_custom_id', true ),
245
				'custom_name'          => get_post_meta( $id, '_wpinv_custom_name', true ),
246
				'custom_singular_name' => get_post_meta( $id, '_wpinv_custom_singular_name', true ),
247
				'is_editable'          => get_post_meta( $id, '_wpinv_editable', true ),
248
				'is_dynamic_pricing'   => get_post_meta( $id, '_wpinv_dynamic_pricing', true ),
249
				'minimum_price'        => get_post_meta( $id, '_minimum_price', true ),
250
				'is_recurring'         => get_post_meta( $id, '_wpinv_is_recurring', true ),
251
				'recurring_period'     => get_post_meta( $id, '_wpinv_recurring_period', true ),
252
				'recurring_interval'   => get_post_meta( $id, '_wpinv_recurring_interval', true ),
253
				'recurring_limit'      => get_post_meta( $id, '_wpinv_recurring_limit', true ),
254
				'is_free_trial'        => get_post_meta( $id, '_wpinv_free_trial', true ),
255
				'trial_period'         => get_post_meta( $id, '_wpinv_trial_period', true ),
256
				'signup_fee'           => get_post_meta( $id, '_wpinv_signup_fee', true ),
257
				'trial_interval'       => get_post_meta( $id, '_wpinv_trial_interval', true ),
258
			)
259
		);
260
261
		// Gets extra data associated with the item if needed.
262
		foreach ( $item->get_extra_data_keys() as $key ) {
263
			$function = 'set_' . $key;
264
			if ( is_callable( array( $item, $function ) ) ) {
265
				$item->{$function}( get_post_meta( $item->get_id(), '_' . $key, true ) );
266
			}
267
		}
268
	}
269
270
	/**
271
	 * Helper method that updates all the post meta for an item based on it's settings in the WPInv_Item class.
272
	 *
273
	 * @param WPInv_Item $item Item object.
274
	 * @since 1.0.19
275
	 */
276
	protected function update_post_meta( &$item ) {
277
		$updated_props     = array();
278
279
		$meta_key_to_props = array(
280
			'_wpinv_price'                => 'price',
281
			'_wpinv_vat_rule'             => 'vat_rule',
282
			'_wpinv_vat_class'            => 'vat_class',
283
			'_wpinv_type'                 => 'type',
284
			'_wpinv_custom_id'            => 'custom_id',
285
			'_wpinv_custom_name'          => 'custom_name',
286
			'_wpinv_custom_singular_name' => 'custom_singular_name',
287
			'_wpinv_editable'             => 'is_editable',
288
			'_wpinv_dynamic_pricing'      => 'is_dynamic_pricing',
289
			'_minimum_price'              => 'minimum_price',
290
			'_wpinv_custom_name'          => 'custom_name',
291
			'_wpinv_is_recurring'         => 'is_recurring',
292
			'_wpinv_recurring_period'     => 'recurring_period',
293
			'_wpinv_recurring_interval'   => 'recurring_interval',
294
			'_wpinv_recurring_limit'      => 'recurring_limit',
295
			'_wpinv_free_trial'           => 'is_free_trial',
296
			'_wpinv_trial_period'         => 'trial_period',
297
			'_wpinv_signup_fee'           => 'signup_fee',
298
			'_wpinv_trial_interval'       => 'trial_interval',
299
		);
300
301
		$props_to_update = $this->get_props_to_update( $item, $meta_key_to_props );
302
303
		foreach ( $props_to_update as $meta_key => $prop ) {
304
			$value = $item->{"get_$prop"}( 'edit' );
305
			$value = is_string( $value ) ? wp_slash( $value ) : $value;
306
307
			$updated = $this->update_or_delete_post_meta( $item, $meta_key, $value );
308
309
			if ( $updated ) {
310
				$updated_props[] = $prop;
311
			}
312
		}
313
314
		do_action( 'getpaid_item_object_updated_props', $item, $updated_props );
315
	}
316
317
	/**
318
	 * Clear any caches.
319
	 *
320
	 * @param WPInv_Item $item Item object.
321
	 * @since 1.0.19
322
	 */
323
	protected function clear_caches( &$item ) {
324
		clean_post_cache( $item->get_id() );
325
	}
326
327
}
328