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

GetPaid_Payment_Form_Data_Store   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 74
c 1
b 0
f 0
dl 0
loc 156
rs 10
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 23 6
A create() 0 34 4
A update() 0 39 4
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
			return true;
86
		}
87
88
		if ( is_wp_error( $id ) ) {
89
			$form->last_error = $id->get_error_message();
90
		}
91
92
		return false;
93
	}
94
95
	/**
96
	 * Method to read a form from the database.
97
	 *
98
	 * @param GetPaid_Payment_Form $form Form object.
99
	 *
100
	 */
101
	public function read( &$form ) {
102
103
		$form->set_defaults();
104
		$form_object = get_post( $form->get_id() );
105
106
		if ( ! $form->get_id() || ! $form_object || $form_object->post_type != 'wpi_payment_form' ) {
107
			$form->last_error = __( 'Invalid form.', 'invoicing' );
108
			return false;
109
		}
110
111
		$form->set_props(
112
			array(
113
				'date_created'  => 0 < $form_object->post_date_gmt ? $form_object->post_date_gmt : null,
114
				'date_modified' => 0 < $form_object->post_modified_gmt ? $form_object->post_modified_gmt : null,
115
				'status'        => $form_object->post_status,
116
				'name'          => $form_object->post_title,
117
				'author'        => $form_object->post_author,
118
			)
119
		);
120
121
		$this->read_object_data( $form, $form_object );
122
		$form->read_meta_data();
123
		$form->set_object_read( true );
124
125
	}
126
127
	/**
128
	 * Method to update a form in the database.
129
	 *
130
	 * @param GetPaid_Payment_Form $form Form object.
131
	 */
132
	public function update( &$form ) {
133
		$form->save_meta_data();
134
		$form->set_version( WPINV_VERSION );
135
136
		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...
137
			$form->set_date_created(  current_time('mysql') );
138
		}
139
140
		$changes = $form->get_changes();
141
142
		// Only update the post when the post data changes.
143
		if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author' ), array_keys( $changes ) ) ) {
144
			$post_data = array(
145
				'post_date'         => $form->get_date_created( 'edit' ),
146
				'post_status'       => $form->get_status( 'edit' ),
147
				'post_title'        => $form->get_name( 'edit' ),
148
				'post_author'       => $form->get_author( 'edit' ),
149
				'post_modified'     => $form->get_date_modified( 'edit' ),
150
			);
151
152
			/**
153
			 * When updating this object, to prevent infinite loops, use $wpdb
154
			 * to update data, since wp_update_post spawns more calls to the
155
			 * save_post action.
156
			 *
157
			 * This ensures hooks are fired by either WP itself (admin screen save),
158
			 * or an update purely from CRUD.
159
			 */
160
			if ( doing_action( 'save_post' ) ) {
161
				$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $form->get_id() ) );
162
				clean_post_cache( $form->get_id() );
163
			} else {
164
				wp_update_post( array_merge( array( 'ID' => $form->get_id() ), $post_data ) );
165
			}
166
			$form->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
167
		}
168
		$this->update_post_meta( $form );
169
		$form->apply_changes();
170
		$this->clear_caches( $form );
171
	}
172
173
	/*
174
	|--------------------------------------------------------------------------
175
	| Additional Methods
176
	|--------------------------------------------------------------------------
177
	*/
178
179
}
180