Test Failed
Push — feature/meta-tables ( 709186...4c8cbd )
by Ravinder
05:18
created

Give_DB_Meta::__posts_where()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 7
Ratio 33.33 %

Importance

Changes 0
Metric Value
cc 8
eloc 11
nc 8
nop 2
dl 7
loc 21
rs 7.1428
c 0
b 0
f 0
1
<?php
2
/**
3
 * Give DB Meta
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_DB_Meta
7
 * @copyright   Copyright (c) 2017, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       2.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
class Give_DB_Meta extends Give_DB {
18
	/**
19
	 * Post type
20
	 *
21
	 * @since  2.0
22
	 * @access protected
23
	 * @var bool
24
	 */
25
	protected $post_type = '';
26
27
	/**
28
	 * Meta type
29
	 *
30
	 * @since  2.0
31
	 * @access protected
32
	 * @var bool
33
	 */
34
	protected $meta_type = '';
35
36
	/**
37
	 * Flag to handle result type
38
	 *
39
	 * @since  2.0
40
	 * @access protected
41
	 */
42
	protected $raw_result = false;
43
44
	/**
45
	 * Flag for short circuit of meta function
46
	 *
47
	 * @since  2.0
48
	 * @access protected
49
	 */
50
	protected $check = false;
51
52
53
	/**
54
	 * Meta supports.
55
	 *
56
	 * @since  2.0
57
	 * @access protected
58
	 * @var array
59
	 */
60
	protected $supports = array(
61
		'add_post_metadata',
62
		'get_post_metadata',
63
		'update_post_metadata',
64
		'delete_post_metadata',
65
		'posts_where',
66
		'posts_join',
67
	);
68
69
	/**
70
	 * Give_DB_Meta constructor.
71
	 *
72
	 * @since 2.0
73
	 */
74
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
75
		// Bailout.
76
		if ( ! empty( $this->supports ) || ! give_has_upgrade_completed( 'v20_move_metadata_into_new_table' ) ) {
77
			return;
78
		}
79
80
		if( in_array( 'add_post_metadata', $this->supports ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
81
			add_filter( 'add_post_metadata', array( $this, '__add_meta' ), 0, 5 );
82
		}
83
84
		if( in_array( 'get_post_metadata', $this->supports ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
85
			add_filter( 'get_post_metadata', array( $this, '__get_meta' ), 0, 4 );
86
		}
87
88
		if( in_array( 'update_post_metadata', $this->supports ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
89
			add_filter( 'update_post_metadata', array( $this, '__update_meta' ), 0, 5 );
90
		}
91
92
		if( in_array( 'delete_post_metadata', $this->supports ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
93
			add_filter( 'delete_post_metadata', array( $this, '__delete_meta' ), 0, 5 );
94
		}
95
96
		if( in_array( 'posts_where', $this->supports ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
97
			add_filter( 'posts_where', array( $this, '__posts_where' ), 10, 2 );
98
		}
99
100
		if( in_array( 'posts_join', $this->supports ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
101
			add_filter( 'posts_join', array( $this, '__posts_join' ), 10, 2 );
102
		}
103
	}
104
105
106
	/**
107
	 * Retrieve payment meta field for a payment.
108
	 *
109
	 * @access  public
110
	 * @since   2.0
111
	 *
112
	 * @param   int    $id       Pst Type  ID.
113
	 * @param   string $meta_key The meta key to retrieve.
114
	 * @param   bool   $single   Whether to return a single value.
115
	 *
116
	 * @return  mixed                 Will be an array if $single is false. Will be value of meta data field if $single is true.
117
	 */
118
	public function get_meta( $id = 0, $meta_key = '', $single = false ) {
119
		$id = $this->sanitize_id( $id );
120
121
		// Bailout.
122
		if ( ! $this->is_valid_post_type( $id ) ) {
123
			return $this->check;
124
		}
125
126
		if ( $this->raw_result ) {
127
			if ( ! ( $value = get_metadata( $this->meta_type, $id, $meta_key, false ) ) ) {
128
				$value = '';
129
			}
130
131
			// Reset flag.
132
			$this->raw_result = false;
133
134
		} else {
135
			$value = get_metadata( $this->meta_type, $id, $meta_key, $single );
136
		}
137
138
		return $value;
139
	}
140
141
142
	/**
143
	 * Filter where clause of every query for new payment meta table
144
	 *
145
	 * @since  2.0
146
	 * @access public
147
	 *
148
	 * @param string   $where
149
	 * @param WP_Query $wp_query
150
	 *
151
	 * @return string
152
	 */
153
	public function __posts_where( $where, $wp_query ) {
0 ignored issues
show
Coding Style introduced by
Method name "Give_DB_Meta::__posts_where" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
154
		global $wpdb;
155
156
		$is_payment_post_type = false;
157
158
		// Check if it is payment query.
159 View Code Duplication
		if ( ! empty( $wp_query->query['post_type'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
			if ( is_string( $wp_query->query['post_type'] ) && $this->post_type === $wp_query->query['post_type'] ) {
161
				$is_payment_post_type = true;
162
			} elseif ( is_array( $wp_query->query['post_type'] ) && in_array( $this->post_type, $wp_query->query['post_type'] ) ) {
163
				$is_payment_post_type = true;
164
			}
165
		}
166
167
		// Add new table to sql query.
168
		if ( $is_payment_post_type && ! empty( $wp_query->meta_query->queries ) ) {
169
			$where = str_replace( $wpdb->postmeta, $this->table_name, $where );
170
		}
171
172
		return $where;
173
	}
174
175
	/**
176
	 * Add meta data field to a payment.
177
	 *
178
	 * For internal use only. Use Give_Payment->add_meta() for public usage.
179
	 *
180
	 * @access  private
181
	 * @since   2.0
182
	 *
183
	 * @param   int    $id         Post Type ID.
184
	 * @param   string $meta_key   Metadata name.
185
	 * @param   mixed  $meta_value Metadata value.
186
	 * @param   bool   $unique     Optional, default is false. Whether the same key should not be added.
187
	 *
188
	 * @return  bool                  False for failure. True for success.
189
	 */
190 View Code Duplication
	public function add_meta( $id = 0, $meta_key = '', $meta_value, $unique = false ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
		$id = $this->sanitize_id( $id );
192
193
		// Bailout.
194
		if ( ! $this->is_valid_post_type( $id ) ) {
195
			return $this->check;
196
		}
197
198
		return add_metadata( $this->meta_type, $id, $meta_key, $meta_value, $unique );
199
	}
200
201
	/**
202
	 * Update payment meta field based on Post Type ID.
203
	 *
204
	 * For internal use only. Use Give_Payment->update_meta() for public usage.
205
	 *
206
	 * Use the $prev_value parameter to differentiate between meta fields with the
207
	 * same key and Post Type ID.
208
	 *
209
	 * If the meta field for the payment does not exist, it will be added.
210
	 *
211
	 * @access  public
212
	 * @since   2.0
213
	 *
214
	 * @param   int    $id         Post Type ID.
215
	 * @param   string $meta_key   Metadata key.
216
	 * @param   mixed  $meta_value Metadata value.
217
	 * @param   mixed  $prev_value Optional. Previous value to check before removing.
218
	 *
219
	 * @return  bool                  False on failure, true if success.
220
	 */
221 View Code Duplication
	public function update_meta( $id = 0, $meta_key = '', $meta_value, $prev_value = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
222
		$id = $this->sanitize_id( $id );
223
224
		// Bailout.
225
		if ( ! $this->is_valid_post_type( $id ) ) {
226
			return $this->check;
227
		}
228
229
		return update_metadata( $this->meta_type, $id, $meta_key, $meta_value, $prev_value );
230
	}
231
232
	/**
233
	 * Remove metadata matching criteria from a payment.
234
	 *
235
	 * You can match based on the key, or key and value. Removing based on key and
236
	 * value, will keep from removing duplicate metadata with the same key. It also
237
	 * allows removing all metadata matching key, if needed.
238
	 *
239
	 * @access  public
240
	 * @since   2.0
241
	 *
242
	 * @param   int    $id         Post Type ID.
243
	 * @param   string $meta_key   Metadata name.
244
	 * @param   mixed  $meta_value Optional. Metadata value.
245
	 *
246
	 * @return  bool                  False for failure. True for success.
247
	 */
248 View Code Duplication
	public function delete_meta( $id = 0, $meta_key = '', $meta_value = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
249
		$id = $this->sanitize_id( $id );
250
251
		// Bailout.
252
		if ( ! $this->is_valid_post_type( $id ) ) {
253
			return $this->check;
254
		}
255
256
		return delete_metadata( $this->meta_type, $id, $meta_key, $meta_value );
257
	}
258
259
260
	/**
261
	 * Filter join clause of every query for new payment meta table
262
	 *
263
	 * @since  2.0
264
	 * @access public
265
	 *
266
	 * @param string   $join
267
	 * @param WP_Query $wp_query
268
	 *
269
	 * @return string
270
	 */
271
	public function __posts_join( $join, $wp_query ) {
0 ignored issues
show
Coding Style introduced by
Method name "Give_DB_Meta::__posts_join" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
272
		global $wpdb;
273
274
		$is_payment_post_type = false;
275
276
		// Check if it is payment query.
277 View Code Duplication
		if ( ! empty( $wp_query->query['post_type'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
278
			if ( is_string( $wp_query->query['post_type'] ) && $this->post_type === $wp_query->query['post_type'] ) {
279
				$is_payment_post_type = true;
280
			} elseif ( is_array( $wp_query->query['post_type'] ) && in_array( $this->post_type, $wp_query->query['post_type'] ) ) {
281
				$is_payment_post_type = true;
282
			}
283
		}
284
285
		// Add new table to sql query.
286
		if ( $is_payment_post_type && ! empty( $wp_query->meta_query->queries ) ) {
287
			$join = str_replace( "{$wpdb->postmeta}.post_id", "{$this->table_name}.payment_id", $join );
288
			$join = str_replace( $wpdb->postmeta, $this->table_name, $join );
289
		}
290
291
		return $join;
292
	}
293
294
	/**
295
	 * Add support for hidden functions.
296
	 *
297
	 * @since  2.0
298
	 * @access public
299
	 *
300
	 * @param $name
301
	 * @param $arguments
302
	 *
303
	 * @return mixed
304
	 */
305 View Code Duplication
	public function __call( $name, $arguments ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
306
		switch ( $name ) {
307
			case '__add_meta':
308
				$this->check = $arguments[0];
309
				$id          = $arguments[1];
310
				$meta_key    = $arguments[2];
311
				$meta_value  = $arguments[3];
312
				$unique      = $arguments[4];
313
314
				return $this->add_meta( $id, $meta_key, $meta_value, $unique );
315
316
			case '__get_meta':
317
				$this->check = $arguments[0];
318
				$id          = $arguments[1];
319
				$meta_key    = $arguments[2];
320
				$single      = $arguments[3];
321
322
				$this->raw_result = true;
323
324
				return $this->get_meta( $id, $meta_key, $single );
325
326
			case '__update_meta':
327
				$this->check = $arguments[0];
328
				$id          = $arguments[1];
329
				$meta_key    = $arguments[2];
330
				$meta_value  = $arguments[3];
331
332
				return $this->update_meta( $id, $meta_key, $meta_value );
333
334
			case '__delete_meta':
335
				$this->check = $arguments[0];
336
				$id          = $arguments[1];
337
				$meta_key    = $arguments[2];
338
				$meta_value  = $arguments[3];
339
				$delete_all  = $arguments[3];
340
341
				return $this->delete_meta( $id, $meta_key, $meta_value, $delete_all );
0 ignored issues
show
Unused Code introduced by
The call to Give_DB_Meta::delete_meta() has too many arguments starting with $delete_all.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
342
		}
343
	}
344
345
	/**
346
	 * Check if current id of payment type or not
347
	 *
348
	 * @since  2.0
349
	 * @access private
350
	 *
351
	 * @param $ID
352
	 *
353
	 * @return bool
354
	 */
355
	private function is_valid_post_type( $ID ) {
356
		return $ID && ( $this->post_type === get_post_type( $ID ) );
357
	}
358
}