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

GetPaid_Payment_Form_Data_Store   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
eloc 82
c 2
b 0
f 0
dl 0
loc 169
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 24 6
A create() 0 35 4
B update() 0 50 6
1
<?php
2
/**
3
 * GetPaid_Payment_Form_Data_Store class file.
4
 *
5
 */
6
if ( ! defined( 'ABSPATH' ) ) {
7
	exit;
8
}
9
10
/**
11
 * Payment Form Data Store: Stored in CPT.
12
 *
13
 * @version  1.0.19
14
 */
15
class GetPaid_Payment_Form_Data_Store extends GetPaid_Data_Store_WP {
16
17
	/**
18
	 * Data stored in meta keys, but not considered "meta" for a form.
19
	 *
20
	 * @since 1.0.19
21
	 * @var array
22
	 */
23
	protected $internal_meta_keys = array(
24
		'wpinv_form_elements',
25
		'wpinv_form_items',
26
		'wpinv_form_earned',
27
		'wpinv_form_refunded',
28
		'wpinv_form_cancelled',
29
		'wpinv_form_failed'
30
	);
31
32
	/**
33
	 * A map of meta keys to data props.
34
	 *
35
	 * @since 1.0.19
36
	 *
37
	 * @var array
38
	 */
39
	protected $meta_key_to_props = array(
40
		'wpinv_form_elements'  => 'elements',
41
		'wpinv_form_items'     => 'items',
42
		'wpinv_form_earned'    => 'earned',
43
		'wpinv_form_refunded'  => 'refunded',
44
		'wpinv_form_cancelled' => 'cancelled',
45
		'wpinv_form_failed'    => 'failed',
46
	);
47
48
	/*
49
	|--------------------------------------------------------------------------
50
	| CRUD Methods
51
	|--------------------------------------------------------------------------
52
	*/
53
54
	/**
55
	 * Method to create a new form in the database.
56
	 *
57
	 * @param GetPaid_Payment_Form $form Form object.
58
	 */
59
	public function create( &$form ) {
60
		$form->set_version( WPINV_VERSION );
61
		$form->set_date_created( current_time('mysql') );
62
63
		// Create a new post.
64
		$id = wp_insert_post(
65
			apply_filters(
66
				'getpaid_new_payment_form_data',
67
				array(
68
					'post_date'     => $form->get_date_created( 'edit' ),
69
					'post_type'     => 'wpi_payment_form',
70
					'post_status'   => $this->get_post_status( $form ),
71
					'ping_status'   => 'closed',
72
					'post_author'   => $form->get_author( 'edit' ),
73
					'post_title'    => $form->get_name( 'edit' ),
74
				)
75
			),
76
			true
77
		);
78
79
		if ( $id && ! is_wp_error( $id ) ) {
80
			$form->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

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