Test Failed
Push — feature/meta-tables ( be55f7...728b69 )
by Ravinder
04:38
created

Give_DB_Meta::__call()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 29
nc 5
nop 2
dl 0
loc 39
rs 8.439
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 ) || ! $this->is_custom_meta_table_active() ) {
77
			return;
78
		}
79
80
		if ( in_array( 'add_post_metadata', $this->supports ) ) {
81
			add_filter( 'add_post_metadata', array( $this, '__add_meta' ), 0, 5 );
82
		}
83
84
		if ( in_array( 'get_post_metadata', $this->supports ) ) {
85
			add_filter( 'get_post_metadata', array( $this, '__get_meta' ), 0, 4 );
86
		}
87
88
		if ( in_array( 'update_post_metadata', $this->supports ) ) {
89
			add_filter( 'update_post_metadata', array( $this, '__update_meta' ), 0, 5 );
90
		}
91
92
		if ( in_array( 'delete_post_metadata', $this->supports ) ) {
93
			add_filter( 'delete_post_metadata', array( $this, '__delete_meta' ), 0, 5 );
94
		}
95
96
		if ( in_array( 'posts_where', $this->supports ) ) {
97
			add_filter( 'posts_where', array( $this, '__posts_where' ), 10, 2 );
98
		}
99
100
		if ( in_array( 'posts_join', $this->supports ) ) {
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
	 * @param   mixed  $delete_all Optional.
246
	 *
247
	 * @return  bool                  False for failure. True for success.
248
	 */
249 View Code Duplication
	public function delete_meta( $id = 0, $meta_key = '', $meta_value = '', $delete_all = '' ) {
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...
250
		$id = $this->sanitize_id( $id );
251
252
		// Bailout.
253
		if ( ! $this->is_valid_post_type( $id ) ) {
254
			return $this->check;
255
		}
256
257
		return delete_metadata( $this->meta_type, $id, $meta_key, $meta_value, $delete_all );
258
	}
259
260
261
	/**
262
	 * Filter join clause of every query for new payment meta table
263
	 *
264
	 * @since  2.0
265
	 * @access public
266
	 *
267
	 * @param string   $join
268
	 * @param WP_Query $wp_query
269
	 *
270
	 * @return string
271
	 */
272
	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...
273
		global $wpdb;
274
275
		$is_payment_post_type = false;
276
277
		// Check if it is payment query.
278 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...
279
			if ( is_string( $wp_query->query['post_type'] ) && $this->post_type === $wp_query->query['post_type'] ) {
280
				$is_payment_post_type = true;
281
			} elseif ( is_array( $wp_query->query['post_type'] ) && in_array( $this->post_type, $wp_query->query['post_type'] ) ) {
282
				$is_payment_post_type = true;
283
			}
284
		}
285
286
		// Add new table to sql query.
287
		if ( $is_payment_post_type && ! empty( $wp_query->meta_query->queries ) ) {
288
			$join = str_replace( "{$wpdb->postmeta}.post_id", "{$this->table_name}.payment_id", $join );
289
			$join = str_replace( $wpdb->postmeta, $this->table_name, $join );
290
		}
291
292
		return $join;
293
	}
294
295
	/**
296
	 * Add support for hidden functions.
297
	 *
298
	 * @since  2.0
299
	 * @access public
300
	 *
301
	 * @param $name
302
	 * @param $arguments
303
	 *
304
	 * @return mixed
305
	 */
306
	public function __call( $name, $arguments ) {
307
		switch ( $name ) {
308
			case '__add_meta':
309
				$this->check = $arguments[0];
310
				$id          = $arguments[1];
311
				$meta_key    = $arguments[2];
312
				$meta_value  = $arguments[3];
313
				$unique      = $arguments[4];
314
315
				return $this->add_meta( $id, $meta_key, $meta_value, $unique );
316
317
			case '__get_meta':
318
				$this->check = $arguments[0];
319
				$id          = $arguments[1];
320
				$meta_key    = $arguments[2];
321
				$single      = $arguments[3];
322
323
				$this->raw_result = true;
324
325
				return $this->get_meta( $id, $meta_key, $single );
326
327
			case '__update_meta':
328
				$this->check = $arguments[0];
329
				$id          = $arguments[1];
330
				$meta_key    = $arguments[2];
331
				$meta_value  = $arguments[3];
332
333
				return $this->update_meta( $id, $meta_key, $meta_value );
334
335
			case '__delete_meta':
336
				$this->check = $arguments[0];
337
				$id          = $arguments[1];
338
				$meta_key    = $arguments[2];
339
				$meta_value  = $arguments[3];
340
				$delete_all  = $arguments[3];
341
342
				return $this->delete_meta( $id, $meta_key, $meta_value, $delete_all );
343
		}
344
	}
345
346
	/**
347
	 * Check if current id of payment type or not
348
	 *
349
	 * @since  2.0
350
	 * @access protected
351
	 *
352
	 * @param $ID
353
	 *
354
	 * @return bool
355
	 */
356
	protected function is_valid_post_type( $ID ) {
357
		return $ID && ( $this->post_type === get_post_type( $ID ) );
358
	}
359
360
	/**
361
	 * check if custom meta table enabled or not.
362
	 *
363
	 * @since  2.0
364
	 * @access protected
365
	 * @return bool
366
	 */
367
	protected function is_custom_meta_table_active() {
368
		return false;
369
	}
370
}