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

GetPaid_Discount_Data_Store   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 99
c 1
b 0
f 0
dl 0
loc 186
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 36 4
A read() 0 25 6
B update() 0 51 6
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
			do_action( 'getpaid_new_discount', $discount->get_id(), $discount );
102
			return true;
103
		}
104
105
		if ( is_wp_error( $id ) ) {
106
			$discount->last_error = $id->get_error_message();
107
		}
108
109
		return false;
110
	}
111
112
	/**
113
	 * Method to read a discount from the database.
114
	 *
115
	 * @param WPInv_Discount $discount Discount object.
116
	 *
117
	 */
118
	public function read( &$discount ) {
119
120
		$discount->set_defaults();
121
		$discount_object = get_post( $discount->get_id() );
122
123
		if ( ! $discount->get_id() || ! $discount_object || $discount_object->post_type != 'wpi_discount' ) {
124
			$discount->last_error = __( 'Invalid discount.', 'invoicing' );
125
			return false;
126
		}
127
128
		$discount->set_props(
129
			array(
130
				'date_created'  => 0 < $discount_object->post_date ? $discount_object->post_date : null,
131
				'date_modified' => 0 < $discount_object->post_modified ? $discount_object->post_modified : null,
132
				'status'        => $discount_object->post_status,
133
				'name'          => $discount_object->post_title,
134
				'author'        => $discount_object->post_author,
135
				'description'   => $discount_object->post_excerpt,
136
			)
137
		);
138
139
		$this->read_object_data( $discount, $discount_object );
140
		$discount->read_meta_data();
141
		$discount->set_object_read( true );
142
		do_action( 'getpaid_read_discount', $discount->get_id(), $discount );
143
144
	}
145
146
	/**
147
	 * Method to update a discount in the database.
148
	 *
149
	 * @param WPInv_Discount $discount Discount object.
150
	 */
151
	public function update( &$discount ) {
152
		$discount->save_meta_data();
153
		$discount->set_version( WPINV_VERSION );
154
155
		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...
156
			$discount->set_date_created(  current_time('mysql') );
157
		}
158
159
		// Grab the current status so we can compare.
160
		$previous_status = get_post_status( $discount->get_id() );
161
162
		$changes = $discount->get_changes();
163
164
		// Only update the post when the post data changes.
165
		if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'post_excerpt' ), array_keys( $changes ) ) ) {
166
			$post_data = array(
167
				'post_date'         => $discount->get_date_created( 'edit' ),
168
				'post_status'       => $discount->get_status( 'edit' ),
169
				'post_title'        => $discount->get_name( 'edit' ),
170
				'post_author'       => $discount->get_author( 'edit' ),
171
				'post_modified'     => $discount->get_date_modified( 'edit' ),
172
				'post_excerpt'      => $discount->get_description( 'edit' ),
173
			);
174
175
			/**
176
			 * When updating this object, to prevent infinite loops, use $wpdb
177
			 * to update data, since wp_update_post spawns more calls to the
178
			 * save_post action.
179
			 *
180
			 * This ensures hooks are fired by either WP itself (admin screen save),
181
			 * or an update purely from CRUD.
182
			 */
183
			if ( doing_action( 'save_post' ) ) {
184
				$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $discount->get_id() ) );
185
				clean_post_cache( $discount->get_id() );
186
			} else {
187
				wp_update_post( array_merge( array( 'ID' => $discount->get_id() ), $post_data ) );
188
			}
189
			$discount->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
190
		}
191
		$this->update_post_meta( $discount );
192
		$discount->apply_changes();
193
		$this->clear_caches( $discount );
194
195
		// Fire a hook depending on the status - this should be considered a creation if it was previously draft status.
196
		$new_status = $discount->get_status( 'edit' );
197
198
		if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) {
199
			do_action( 'getpaid_new_discount', $discount->get_id(), $discount );
200
		} else {
201
			do_action( 'getpaid_update_discount', $discount->get_id(), $discount );
202
		}
203
204
	}
205
206
	/*
207
	|--------------------------------------------------------------------------
208
	| Additional Methods
209
	|--------------------------------------------------------------------------
210
	*/
211
212
}
213