Passed
Pull Request — master (#375)
by Brian
87:27
created

GetPaid_Item_Data_Store::read_item_data()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 25
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 32
rs 9.52
1
<?php
2
/**
3
 * GetPaid_Item_Data_Store class file.
4
 *
5
 */
6
if ( ! defined( 'ABSPATH' ) ) {
7
	exit;
8
}
9
10
/**
11
 * Item Data Store: Stored in CPT.
12
 *
13
 * @version  1.0.19
14
 */
15
class GetPaid_Item_Data_Store extends GetPaid_Data_Store_WP {
16
17
	/**
18
	 * Data stored in meta keys, but not considered "meta" for an item.
19
	 *
20
	 * @since 1.0.19
21
	 * @var array
22
	 */
23
	protected $internal_meta_keys = array(
24
		'_wpinv_price',
25
		'_wpinv_vat_rule',
26
		'_wpinv_vat_class',
27
		'_wpinv_type',
28
		'_wpinv_custom_id',
29
		'_wpinv_custom_name',
30
		'_wpinv_custom_singular_name',
31
		'_wpinv_editable',
32
		'_wpinv_dynamic_pricing',
33
		'_minimum_price',
34
		'_wpinv_is_recurring',
35
		'_wpinv_recurring_period',
36
		'_wpinv_recurring_interval',
37
		'_wpinv_recurring_limit',
38
		'_wpinv_free_trial',
39
		'_wpinv_trial_period',
40
		'_wpinv_trial_interval'
41
	);
42
43
	/**
44
	 * A map of meta keys to data props.
45
	 *
46
	 * @since 1.0.19
47
	 *
48
	 * @var array
49
	 */
50
	protected $meta_key_to_props = array(
51
		'_wpinv_price'                => 'price',
52
		'_wpinv_vat_rule'             => 'vat_rule',
53
		'_wpinv_vat_class'            => 'vat_class',
54
		'_wpinv_type'                 => 'type',
55
		'_wpinv_custom_id'            => 'custom_id',
56
		'_wpinv_custom_name'          => 'custom_name',
57
		'_wpinv_custom_singular_name' => 'custom_singular_name',
58
		'_wpinv_editable'             => 'is_editable',
59
		'_wpinv_dynamic_pricing'      => 'is_dynamic_pricing',
60
		'_minimum_price'              => 'minimum_price',
61
		'_wpinv_custom_name'          => 'custom_name',
62
		'_wpinv_is_recurring'         => 'is_recurring',
63
		'_wpinv_recurring_period'     => 'recurring_period',
64
		'_wpinv_recurring_interval'   => 'recurring_interval',
65
		'_wpinv_recurring_limit'      => 'recurring_limit',
66
		'_wpinv_free_trial'           => 'is_free_trial',
67
		'_wpinv_trial_period'         => 'trial_period',
68
		'_wpinv_trial_interval'       => 'trial_interval',
69
		'_wpinv_version'              => 'version',
70
	);
71
72
	/*
73
	|--------------------------------------------------------------------------
74
	| CRUD Methods
75
	|--------------------------------------------------------------------------
76
	*/
77
78
	/**
79
	 * Method to create a new item in the database.
80
	 *
81
	 * @param WPInv_Item $item Item object.
82
	 */
83
	public function create( &$item ) {
84
		$item->set_version( WPINV_VERSION );
85
		$item->set_date_created( current_time('mysql') );
86
87
		// Create a new post.
88
		$id = wp_insert_post(
89
			apply_filters(
90
				'getpaid_new_item_data',
91
				array(
92
					'post_date'     => $item->get_date_created( 'edit' ),
93
					'post_type'     => 'wpi_item',
94
					'post_status'   => $this->get_post_status( $item ),
95
					'ping_status'   => 'closed',
96
					'post_author'   => $item->get_author( 'edit' ),
97
					'post_title'    => $item->get_name( 'edit' ),
98
					'post_parent'   => 0,
99
					'post_excerpt'  => $item->get_description( 'edit' ),
100
				)
101
			),
102
			true
103
		);
104
105
		if ( $id && ! is_wp_error( $id ) ) {
106
			$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

106
			$item->set_id( /** @scrutinizer ignore-type */ $id );
Loading history...
107
			$this->update_post_meta( $item );
108
			$item->save_meta_data();
109
			$item->apply_changes();
110
			$this->clear_caches( $item );
111
			return true;
112
		}
113
114
		if ( is_wp_error( $id ) ) {
115
			$item->last_error = $id->get_error_message();
116
		}
117
		
118
		return false;
119
	}
120
121
	/**
122
	 * Method to read an item from the database.
123
	 *
124
	 * @param WPInv_Item $item Item object.
125
	 *
126
	 */
127
	public function read( &$item ) {
128
129
		$item->set_defaults();
130
		$item_object = get_post( $item->get_id() );
131
132
		if ( ! $item->get_id() || ! $item_object || $item_object->post_type != 'wpi_item' ) {
133
			$item->last_error = __( 'Invalid item.', 'invoicing' );
134
			return false;
135
		}
136
137
		$item->set_props(
138
			array(
139
				'parent_id'     => $item_object->post_parent,
140
				'date_created'  => 0 < $item_object->post_date_gmt ? $item_object->post_date_gmt : null,
141
				'date_modified' => 0 < $item_object->post_modified_gmt ? $item_object->post_modified_gmt : null,
142
				'status'        => $item_object->post_status,
143
				'name'          => $item_object->post_title,
144
				'description'   => $item_object->post_excerpt,
145
				'author'        => $item_object->post_author,
146
			)
147
		);
148
149
		$this->read_object_data( $item, $item_object );
150
		$item->read_meta_data();
151
		$item->set_object_read( true );
152
153
	}
154
155
	/**
156
	 * Method to update an item in the database.
157
	 *
158
	 * @param WPInv_Item $item Item object.
159
	 */
160
	public function update( &$item ) {
161
		$item->save_meta_data();
162
		$item->set_version( WPINV_VERSION );
163
164
		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...
165
			$item->set_date_created(  current_time('mysql') );
166
		}
167
168
		$changes = $item->get_changes();
169
170
		// Only update the post when the post data changes.
171
		if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'parent_id', 'post_excerpt', 'name', 'author' ), array_keys( $changes ) ) ) {
172
			$post_data = array(
173
				'post_date'         => $item->get_date_created( 'edit' ),
174
				'post_status'       => $item->get_status( 'edit' ),
175
				'post_parent'       => $item->get_parent_id( 'edit' ),
176
				'post_excerpt'      => $item->get_description( 'edit' ),
177
				'post_modified'     => $item->get_date_modified( 'edit' ),
178
				'post_title'        => $item->get_name( 'edit' ),
179
				'post_author'       => $item->get_author( 'edit' ),
180
			);
181
182
			/**
183
			 * When updating this object, to prevent infinite loops, use $wpdb
184
			 * to update data, since wp_update_post spawns more calls to the
185
			 * save_post action.
186
			 *
187
			 * This ensures hooks are fired by either WP itself (admin screen save),
188
			 * or an update purely from CRUD.
189
			 */
190
			if ( doing_action( 'save_post' ) ) {
191
				$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $item->get_id() ) );
192
				clean_post_cache( $item->get_id() );
193
			} else {
194
				wp_update_post( array_merge( array( 'ID' => $item->get_id() ), $post_data ) );
195
			}
196
			$item->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
197
		}
198
		$this->update_post_meta( $item );
199
		$item->apply_changes();
200
		$this->clear_caches( $item );
201
	}
202
203
	/*
204
	|--------------------------------------------------------------------------
205
	| Additional Methods
206
	|--------------------------------------------------------------------------
207
	*/
208
209
	/**
210
	 * Helper method that updates all the post meta for an item based on it's settings in the WPInv_Item class.
211
	 *
212
	 * @param WPInv_Item $item WPInv_Item object.
213
	 * @since 1.0.19
214
	 */
215
	protected function update_post_meta( &$item ) {
216
217
		// Ensure that we have a custom id.
218
        if ( ! $item->get_custom_id() ) {
219
            $item->set_custom_id( $item->get_id() );
220
		}
221
222
		parent::update_post_meta( $item );
223
	}
224
225
}
226