Test Failed
Push — feature/background-processing ( ddf6d7 )
by Ravinder
06:04
created

Give_DB_Donor_Meta::update_meta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 4
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Donor Meta DB class
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/DB Donor Meta
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.6
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Class Give_DB_Donor_Meta
19
 *
20
 * This class is for interacting with the donor meta database table.
21
 *
22
 * @since 1.6
23
 */
24
class Give_DB_Donor_Meta extends Give_DB {
25
26
	/**
27
	 * Give_DB_Donor_Meta constructor.
28
	 *
29
	 * @access  public
30
	 * @since   1.6
31
	 */
32
	public function __construct() {
33
		/* @var WPDB $wpdb */
34
		global $wpdb;
35
36
		$wpdb->customermeta = $this->table_name  = $wpdb->prefix . 'give_customermeta';
37
		$this->primary_key = 'meta_id';
38
		$this->version     = '1.0';
39
40
		$this->register_table();
41
	}
42
43
	/**
44
	 * Get table columns and data types.
45
	 *
46
	 * @access  public
47
	 * @since   1.6
48
	 *
49
	 * @return  array  Columns and formats.
50
	 */
51
	public function get_columns() {
52
		return array(
53
			'meta_id'     => '%d',
54
			'customer_id' => '%d',
55
			'meta_key'    => '%s',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
56
			'meta_value'  => '%s',
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
57
		);
58
	}
59
60
	/**
61
	 * Retrieve donor meta field for a donor.
62
	 *
63
	 * For internal use only. Use Give_Donor->get_meta() for public usage.
64
	 *
65
	 * @access  private
66
	 * @since   1.6
67
	 *
68
	 * @param   int    $donor_id Donor ID.
69
	 * @param   string $meta_key The meta key to retrieve.
70
	 * @param   bool   $single Whether to return a single value.
71
	 *
72
	 * @return  mixed                 Will be an array if $single is false. Will be value of meta data field if $single is true.
73
	 */
74 View Code Duplication
	public function get_meta( $donor_id = 0, $meta_key = '', $single = 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...
75
		$donor_id = $this->sanitize_donor_id( $donor_id );
76
		if ( false === $donor_id ) {
77
			return false;
78
		}
79
80
		return get_metadata( 'customer', $donor_id, $meta_key, $single );
81
	}
82
83
	/**
84
	 * Add meta data field to a donor.
85
	 *
86
	 * For internal use only. Use Give_Donor->add_meta() for public usage.
87
	 *
88
	 * @access  private
89
	 * @since   1.6
90
	 *
91
	 * @param   int    $donor_id Donor ID.
92
	 * @param   string $meta_key Metadata name.
93
	 * @param   mixed  $meta_value Metadata value.
94
	 * @param   bool   $unique Optional, default is false. Whether the same key should not be added.
95
	 *
96
	 * @return  bool                  False for failure. True for success.
97
	 */
98 View Code Duplication
	public function add_meta( $donor_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...
99
		$donor_id = $this->sanitize_donor_id( $donor_id );
100
		if ( false === $donor_id ) {
101
			return false;
102
		}
103
104
		return add_metadata( 'customer', $donor_id, $meta_key, $meta_value, $unique );
105
	}
106
107
	/**
108
	 * Update donor meta field based on Donor ID.
109
	 *
110
	 * For internal use only. Use Give_Donor->update_meta() for public usage.
111
	 *
112
	 * Use the $prev_value parameter to differentiate between meta fields with the
113
	 * same key and Donor ID.
114
	 *
115
	 * If the meta field for the donor does not exist, it will be added.
116
	 *
117
	 * @access  private
118
	 * @since   1.6
119
	 *
120
	 * @param   int    $donor_id Donor ID.
121
	 * @param   string $meta_key Metadata key.
122
	 * @param   mixed  $meta_value Metadata value.
123
	 * @param   mixed  $prev_value Optional. Previous value to check before removing.
124
	 *
125
	 * @return  bool                  False on failure, true if success.
126
	 */
127 View Code Duplication
	public function update_meta( $donor_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...
128
		$donor_id = $this->sanitize_donor_id( $donor_id );
129
		if ( false === $donor_id ) {
130
			return false;
131
		}
132
133
		return update_metadata( 'customer', $donor_id, $meta_key, $meta_value, $prev_value );
134
	}
135
136
	/**
137
	 * Remove metadata matching criteria from a donor.
138
	 *
139
	 * For internal use only. Use Give_Donor->delete_meta() for public usage.
140
	 *
141
	 * You can match based on the key, or key and value. Removing based on key and
142
	 * value, will keep from removing duplicate metadata with the same key. It also
143
	 * allows removing all metadata matching key, if needed.
144
	 *
145
	 * @access  private
146
	 * @since   1.6
147
	 *
148
	 * @param   int    $donor_id Donor ID.
149
	 * @param   string $meta_key Metadata name.
150
	 * @param   mixed  $meta_value Optional. Metadata value.
151
	 *
152
	 * @return  bool                  False for failure. True for success.
153
	 */
154
	public function delete_meta( $donor_id = 0, $meta_key = '', $meta_value = '' ) {
155
		return delete_metadata( 'customer', $donor_id, $meta_key, $meta_value );
156
	}
157
158
	/**
159
	 * Create the table
160
	 *
161
	 * @access public
162
	 * @since  1.6
163
	 *
164
	 * @return void
165
	 */
166 View Code Duplication
	public function create_table() {
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...
167
168
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
169
170
		$sql = "CREATE TABLE {$this->table_name} (
171
			meta_id bigint(20) NOT NULL AUTO_INCREMENT,
172
			customer_id bigint(20) NOT NULL,
173
			meta_key varchar(255) DEFAULT NULL,
174
			meta_value longtext,
175
			PRIMARY KEY  (meta_id),
176
			KEY customer_id (customer_id),
177
			KEY meta_key (meta_key)
178
			) CHARACTER SET utf8 COLLATE utf8_general_ci;";
179
180
		dbDelta( $sql );
181
182
		update_option( $this->table_name . '_db_version', $this->version );
183
	}
184
185
	/**
186
	 * Given a donor ID, make sure it's a positive number, greater than zero before inserting or adding.
187
	 *
188
	 * @access private
189
	 * @since  1.6
190
	 *
191
	 * @param  int|stripe $donor_id A passed donor ID.
192
	 *
193
	 * @return int|bool                The normalized donor ID or false if it's found to not be valid.
194
	 */
195
	private function sanitize_donor_id( $donor_id ) {
196
		if ( ! is_numeric( $donor_id ) ) {
197
			return false;
198
		}
199
200
		$donor_id = (int) $donor_id;
201
202
		// We were given a non positive number.
203
		if ( absint( $donor_id ) !== $donor_id ) {
204
			return false;
205
		}
206
207
		if ( empty( $donor_id ) ) {
208
			return false;
209
		}
210
211
		return absint( $donor_id );
212
213
	}
214
215
}
216