Passed
Pull Request — master (#377)
by Brian
05:01
created

GetPaid_Item_Data_Store::update()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 52
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
c 0
b 0
f 0
dl 0
loc 52
rs 8.8337
cc 6
nc 12
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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