Passed
Pull Request — master (#376)
by Brian
160:47 queued 45:56
created

GetPaid_Discount_Data_Store::read()   A

Complexity

Conditions 6
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 24
rs 9.1111
cc 6
nc 2
nop 1
1
<?php
2
3
/**
4
 * GetPaid_Discount_Data_Store class file.
5
 *
6
 */
7
if ( ! defined( 'ABSPATH' ) ) {
8
	exit;
9
}
10
11
/**
12
 * Discount Data Store: Stored in CPT.
13
 *
14
 * @version  1.0.19
15
 */
16
class GetPaid_Discount_Data_Store extends GetPaid_Data_Store_WP {
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
		'_wpi_discount_code',
26
		'_wpi_discount_amount',
27
		'_wpi_discount_start',
28
		'_wpi_discount_expiration',
29
		'_wpi_discount_type',
30
		'_wpi_discount_uses',
31
		'_wpi_discount_is_single_use',
32
		'_wpi_discount_items',
33
		'_wpi_discount_excluded_items',
34
		'_wpi_discount_max_uses',
35
		'_wpi_discount_is_recurring',
36
		'_wpi_discount_min_total',
37
		'_wpi_discount_max_total',
38
	);
39
40
	/**
41
	 * A map of meta keys to data props.
42
	 *
43
	 * @since 1.0.19
44
	 *
45
	 * @var array
46
	 */
47
	protected $meta_key_to_props = array(
48
		'_wpi_discount_code'           => 'code',
49
		'_wpi_discount_amount'         => 'amount',
50
		'_wpi_discount_start'          => 'start',
51
		'_wpi_discount_expiration'     => 'expiration',
52
		'_wpi_discount_type'           => 'type',
53
		'_wpi_discount_uses'           => 'uses',
54
		'_wpi_discount_is_single_use'  => 'is_single_use',
55
		'_wpi_discount_items'          => 'items',
56
		'_wpi_discount_excluded_items' => 'excluded_items',
57
		'_wpi_discount_max_uses'       => 'max_uses',
58
		'_wpi_discount_is_recurring'   => 'is_recurring',
59
		'_wpi_discount_min_total'      => 'min_total',
60
		'_wpi_discount_max_total'      => 'max_total',
61
	);
62
63
	/*
64
	|--------------------------------------------------------------------------
65
	| CRUD Methods
66
	|--------------------------------------------------------------------------
67
	*/
68
69
	/**
70
	 * Method to create a new discount in the database.
71
	 *
72
	 * @param WPInv_Discount $discount Discount object.
73
	 */
74
	public function create( &$discount ) {
75
		$discount->set_version( WPINV_VERSION );
76
		$discount->set_date_created( current_time('mysql') );
77
78
		// Create a new post.
79
		$id = wp_insert_post(
80
			apply_filters(
81
				'getpaid_new_discount_data',
82
				array(
83
					'post_date'     => $discount->get_date_created( 'edit' ),
84
					'post_type'     => 'wpi_discount',
85
					'post_status'   => $this->get_post_status( $discount ),
86
					'ping_status'   => 'closed',
87
					'post_author'   => $discount->get_author( 'edit' ),
88
					'post_title'    => $discount->get_name( 'edit' ),
89
					'post_excerpt'  => $discount->get_description( 'edit' ),
90
				)
91
			),
92
			true
93
		);
94
95
		if ( $id && ! is_wp_error( $id ) ) {
96
			$discount->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

96
			$discount->set_id( /** @scrutinizer ignore-type */ $id );
Loading history...
97
			$this->update_post_meta( $discount );
98
			$discount->save_meta_data();
99
			$discount->apply_changes();
100
			$this->clear_caches( $discount );
101
			return true;
102
		}
103
104
		if ( is_wp_error( $id ) ) {
105
			$discount->last_error = $id->get_error_message();
106
		}
107
108
		return false;
109
	}
110
111
	/**
112
	 * Method to read a discount from the database.
113
	 *
114
	 * @param WPInv_Discount $discount Discount object.
115
	 *
116
	 */
117
	public function read( &$discount ) {
118
119
		$discount->set_defaults();
120
		$discount_object = get_post( $discount->get_id() );
121
122
		if ( ! $discount->get_id() || ! $discount_object || $discount_object->post_type != 'wpi_discount' ) {
123
			$discount->last_error = __( 'Invalid discount.', 'invoicing' );
124
			return false;
125
		}
126
127
		$discount->set_props(
128
			array(
129
				'date_created'  => 0 < $discount_object->post_date_gmt ? $discount_object->post_date_gmt : null,
130
				'date_modified' => 0 < $discount_object->post_modified_gmt ? $discount_object->post_modified_gmt : null,
131
				'status'        => $discount_object->post_status,
132
				'name'          => $discount_object->post_title,
133
				'author'        => $discount_object->post_author,
134
				'description'   => $discount_object->post_excerpt,
135
			)
136
		);
137
138
		$this->read_object_data( $discount, $discount_object );
139
		$discount->read_meta_data();
140
		$discount->set_object_read( true );
141
142
	}
143
144
	/**
145
	 * Method to update a discount in the database.
146
	 *
147
	 * @param WPInv_Discount $discount Discount object.
148
	 */
149
	public function update( &$discount ) {
150
		$discount->save_meta_data();
151
		$discount->set_version( WPINV_VERSION );
152
153
		if ( null === $discount->get_date_created( 'edit' ) ) {
0 ignored issues
show
introduced by
The condition null === $discount->get_date_created('edit') is always false.
Loading history...
154
			$discount->set_date_created(  current_time('mysql') );
155
		}
156
157
		$changes = $discount->get_changes();
158
159
		// Only update the post when the post data changes.
160
		if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'post_excerpt' ), array_keys( $changes ) ) ) {
161
			$post_data = array(
162
				'post_date'         => $discount->get_date_created( 'edit' ),
163
				'post_status'       => $discount->get_status( 'edit' ),
164
				'post_title'        => $discount->get_name( 'edit' ),
165
				'post_author'       => $discount->get_author( 'edit' ),
166
				'post_modified'     => $discount->get_date_modified( 'edit' ),
167
				'post_excerpt'      => $discount->get_description( 'edit' ),
168
			);
169
170
			/**
171
			 * When updating this object, to prevent infinite loops, use $wpdb
172
			 * to update data, since wp_update_post spawns more calls to the
173
			 * save_post action.
174
			 *
175
			 * This ensures hooks are fired by either WP itself (admin screen save),
176
			 * or an update purely from CRUD.
177
			 */
178
			if ( doing_action( 'save_post' ) ) {
179
				$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $discount->get_id() ) );
180
				clean_post_cache( $discount->get_id() );
181
			} else {
182
				wp_update_post( array_merge( array( 'ID' => $discount->get_id() ), $post_data ) );
183
			}
184
			$discount->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
185
		}
186
		$this->update_post_meta( $discount );
187
		$discount->apply_changes();
188
		$this->clear_caches( $discount );
189
	}
190
191
	/*
192
	|--------------------------------------------------------------------------
193
	| Additional Methods
194
	|--------------------------------------------------------------------------
195
	*/
196
197
}
198