Completed
Push — release/2.0 ( 4d845f...d9fa8a )
by Ravinder
18:24
created

Give_DB_Donor_Meta   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A get_columns() 0 8 1
A register_table() 0 4 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
		$this->table_name  = $wpdb->prefix . 'give_customermeta';
37
		$this->primary_key = 'meta_id';
38
		$this->version     = '1.0';
39
40
		add_action( 'plugins_loaded', array( $this, 'register_table' ), 11 );
41
42
	}
43
44
	/**
45
	 * Get table columns and data types.
46
	 *
47
	 * @access  public
48
	 * @since   1.6
49
	 *
50
	 * @return  array  Columns and formats.
51
	 */
52
	public function get_columns() {
53
		return array(
54
			'meta_id'     => '%d',
55
			'customer_id' => '%d',
56
			'meta_key'    => '%s',
57
			'meta_value'  => '%s',
58
		);
59
	}
60
61
	/**
62
	 * Register the table with $wpdb so the metadata api can find it.
63
	 *
64
	 * @access  public
65
	 * @since   1.6
66
	 *
67
	 * @return  void
68
	 */
69
	public function register_table() {
70
		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...
71
		$wpdb->customermeta = $this->table_name;
72
	}
73
74
	/**
75
	 * Retrieve donor meta field for a donor.
76
	 *
77
	 * For internal use only. Use Give_Donor->get_meta() for public usage.
78
	 *
79
	 * @access  private
80
	 * @since   1.6
81
	 *
82
	 * @param   int    $donor_id Donor ID.
83
	 * @param   string $meta_key The meta key to retrieve.
84
	 * @param   bool   $single Whether to return a single value.
85
	 *
86
	 * @return  mixed                 Will be an array if $single is false. Will be value of meta data field if $single is true.
87
	 */
88
	public function get_meta( $donor_id = 0, $meta_key = '', $single = false ) {
89
		$donor_id = $this->sanitize_donor_id( $donor_id );
90
		if ( false === $donor_id ) {
91
			return false;
92
		}
93
94
		return get_metadata( 'customer', $donor_id, $meta_key, $single );
95
	}
96
97
	/**
98
	 * Add meta data field to a donor.
99
	 *
100
	 * For internal use only. Use Give_Donor->add_meta() for public usage.
101
	 *
102
	 * @access  private
103
	 * @since   1.6
104
	 *
105
	 * @param   int    $donor_id Donor ID.
106
	 * @param   string $meta_key Metadata name.
107
	 * @param   mixed  $meta_value Metadata value.
108
	 * @param   bool   $unique Optional, default is false. Whether the same key should not be added.
109
	 *
110
	 * @return  bool                  False for failure. True for success.
111
	 */
112
	public function add_meta( $donor_id = 0, $meta_key = '', $meta_value, $unique = false ) {
113
		$donor_id = $this->sanitize_donor_id( $donor_id );
114
		if ( false === $donor_id ) {
115
			return false;
116
		}
117
118
		return add_metadata( 'customer', $donor_id, $meta_key, $meta_value, $unique );
119
	}
120
121
	/**
122
	 * Update donor meta field based on Donor ID.
123
	 *
124
	 * For internal use only. Use Give_Donor->update_meta() for public usage.
125
	 *
126
	 * Use the $prev_value parameter to differentiate between meta fields with the
127
	 * same key and Donor ID.
128
	 *
129
	 * If the meta field for the donor does not exist, it will be added.
130
	 *
131
	 * @access  private
132
	 * @since   1.6
133
	 *
134
	 * @param   int    $donor_id Donor ID.
135
	 * @param   string $meta_key Metadata key.
136
	 * @param   mixed  $meta_value Metadata value.
137
	 * @param   mixed  $prev_value Optional. Previous value to check before removing.
138
	 *
139
	 * @return  bool                  False on failure, true if success.
140
	 */
141
	public function update_meta( $donor_id = 0, $meta_key = '', $meta_value, $prev_value = '' ) {
142
		$donor_id = $this->sanitize_donor_id( $donor_id );
143
		if ( false === $donor_id ) {
144
			return false;
145
		}
146
147
		return update_metadata( 'customer', $donor_id, $meta_key, $meta_value, $prev_value );
148
	}
149
150
	/**
151
	 * Remove metadata matching criteria from a donor.
152
	 *
153
	 * For internal use only. Use Give_Donor->delete_meta() for public usage.
154
	 *
155
	 * You can match based on the key, or key and value. Removing based on key and
156
	 * value, will keep from removing duplicate metadata with the same key. It also
157
	 * allows removing all metadata matching key, if needed.
158
	 *
159
	 * @access  private
160
	 * @since   1.6
161
	 *
162
	 * @param   int    $donor_id Donor ID.
163
	 * @param   string $meta_key Metadata name.
164
	 * @param   mixed  $meta_value Optional. Metadata value.
165
	 *
166
	 * @return  bool                  False for failure. True for success.
167
	 */
168
	public function delete_meta( $donor_id = 0, $meta_key = '', $meta_value = '' ) {
169
		return delete_metadata( 'customer', $donor_id, $meta_key, $meta_value );
170
	}
171
172
	/**
173
	 * Create the table
174
	 *
175
	 * @access public
176
	 * @since  1.6
177
	 *
178
	 * @return void
179
	 */
180
	public function create_table() {
181
182
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
183
184
		$sql = "CREATE TABLE {$this->table_name} (
185
			meta_id bigint(20) NOT NULL AUTO_INCREMENT,
186
			customer_id bigint(20) NOT NULL,
187
			meta_key varchar(255) DEFAULT NULL,
188
			meta_value longtext,
189
			PRIMARY KEY  (meta_id),
190
			KEY customer_id (customer_id),
191
			KEY meta_key (meta_key)
192
			) CHARACTER SET utf8 COLLATE utf8_general_ci;";
193
194
		dbDelta( $sql );
195
196
		update_option( $this->table_name . '_db_version', $this->version );
197
	}
198
199
	/**
200
	 * Given a donor ID, make sure it's a positive number, greater than zero before inserting or adding.
201
	 *
202
	 * @access private
203
	 * @since  1.6
204
	 *
205
	 * @param  int|stripe $donor_id A passed donor ID.
206
	 *
207
	 * @return int|bool                The normalized donor ID or false if it's found to not be valid.
208
	 */
209
	private function sanitize_donor_id( $donor_id ) {
210
		if ( ! is_numeric( $donor_id ) ) {
211
			return false;
212
		}
213
214
		$donor_id = (int) $donor_id;
215
216
		// We were given a non positive number.
217
		if ( absint( $donor_id ) !== $donor_id ) {
218
			return false;
219
		}
220
221
		if ( empty( $donor_id ) ) {
222
			return false;
223
		}
224
225
		return absint( $donor_id );
226
227
	}
228
229
}
230