Completed
Pull Request — master (#1832)
by Devin
04:50
created

Give_DB_Donor_Meta   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 192
rs 10
c 0
b 0
f 0
wmc 14
lcom 2
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A get_columns() 0 8 1
A get_meta() 0 8 2
A add_meta() 0 8 2
A update_meta() 0 8 2
A delete_meta() 0 3 1
A create_table() 0 18 1
A sanitize_donor_id() 0 19 4
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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',
56
			'meta_value'  => '%s',
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
	public function get_meta( $donor_id = 0, $meta_key = '', $single = false ) {
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
	public function add_meta( $donor_id = 0, $meta_key = '', $meta_value, $unique = false ) {
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
	public function update_meta( $donor_id = 0, $meta_key = '', $meta_value, $prev_value = '' ) {
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
	public function create_table() {
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