Completed
Push — issues/1132 ( f8ce82...f564f5 )
by Ravinder
53:40 queued 33:35
created

Give_DB_Form_Meta::update_meta()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 4
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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
 * Form Meta DB class
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/DB Form Meta
7
 * @copyright   Copyright (c) 2016, 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
/**
18
 * Class Give_DB_Form_Meta
19
 *
20
 * This class is for interacting with the form meta database table.
21
 *
22
 * @since 2.0
23
 */
24
class Give_DB_Form_Meta extends Give_DB {
25
	/**
26
	 * Give_DB_Form_Meta constructor.
27
	 *
28
	 * @access  public
29
	 * @since   2.0
30
	 */
31
	public function __construct() {
32
		/* @var WPDB $wpdb */
33
		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...
34
35
		$wpdb->formmeta    = $this->table_name = $wpdb->prefix . 'give_formmeta';
36
		$this->primary_key = 'meta_id';
37
		$this->version     = '1.0';
38
39
		$this->register_table();
40
41
		add_filter( 'add_post_metadata', array( $this, '__add_meta' ), 0, 4 );
42
		add_filter( 'get_post_metadata', array( $this, '__get_meta' ), 0, 4 );
43
		add_filter( 'update_post_metadata', array( $this, '__update_meta' ), 0, 4 );
44
		add_filter( 'delete_post_metadata', array( $this, '__delete_meta' ), 0, 4 );
45
	}
46
47
	/**
48
	 * Get table columns and data types.
49
	 *
50
	 * @access  public
51
	 * @since   2.0
52
	 *
53
	 * @return  array  Columns and formats.
54
	 */
55
	public function get_columns() {
56
		return array(
57
			'meta_id'    => '%d',
58
			'form_id'    => '%d',
59
			'meta_key'   => '%s',
60
			'meta_value' => '%s',
61
		);
62
	}
63
64
	/**
65
	 * Retrieve form meta field for a form.
66
	 *
67
	 * For internal use only. Use Give_Form->get_meta() for public usage.
68
	 *
69
	 * @access  public
70
	 * @since   2.0
71
	 *
72
	 * @param   int    $form_id  Form ID.
73
	 * @param   string $meta_key The meta key to retrieve.
74
	 * @param   bool   $single   Whether to return a single value.
75
	 *
76
	 * @return  mixed                 Will be an array if $single is false. Will be value of meta data field if $single is true.
77
	 */
78
	public function get_meta( $form_id = 0, $meta_key = '', $single = false ) {
79
		$form_id = $this->sanitize_id( $form_id );
80
81
		// Bailout.
82
		if ( ! $form_id || ! Give()->logs->log_db->is_log( $form_id ) ) {
83
			return null;
84
		}
85
86
		return get_metadata( 'form', $form_id, $meta_key, $single );
87
	}
88
89
	/**
90
	 * Add meta data field to a form.
91
	 *
92
	 * For internal use only. Use Give_Form->add_meta() for public usage.
93
	 *
94
	 * @access  private
95
	 * @since   2.0
96
	 *
97
	 * @param   int    $form_id    Form ID.
98
	 * @param   string $meta_key   Metadata name.
99
	 * @param   mixed  $meta_value Metadata value.
100
	 * @param   bool   $unique     Optional, default is false. Whether the same key should not be added.
101
	 *
102
	 * @return  bool                  False for failure. True for success.
103
	 */
104
	public function add_meta( $form_id = 0, $meta_key = '', $meta_value, $unique = false ) {
105
		$form_id = $this->sanitize_id( $form_id );
106
107
		// Bailout.
108
		if ( ! $form_id || ! Give()->logs->log_db->is_log( $form_id ) ) {
109
			return null;
110
		}
111
112
		return add_metadata( 'form', $form_id, $meta_key, $meta_value, $unique );
113
	}
114
115
	/**
116
	 * Update form meta field based on Form ID.
117
	 *
118
	 * For internal use only. Use Give_Form->update_meta() for public usage.
119
	 *
120
	 * Use the $prev_value parameter to differentiate between meta fields with the
121
	 * same key and Form ID.
122
	 *
123
	 * If the meta field for the form does not exist, it will be added.
124
	 *
125
	 * @access  public
126
	 * @since   2.0
127
	 *
128
	 * @param   int    $form_id    Form ID.
129
	 * @param   string $meta_key   Metadata key.
130
	 * @param   mixed  $meta_value Metadata value.
131
	 * @param   mixed  $prev_value Optional. Previous value to check before removing.
132
	 *
133
	 * @return  bool                  False on failure, true if success.
134
	 */
135
	public function update_meta( $form_id = 0, $meta_key = '', $meta_value, $prev_value = '' ) {
136
		$form_id = $this->sanitize_id( $form_id );
137
138
		// Bailout.
139
		if ( ! $form_id || ! Give()->logs->log_db->is_log( $form_id ) ) {
140
			return null;
141
		}
142
143
		return update_metadata( 'form', $form_id, $meta_key, $meta_value, $prev_value );
144
	}
145
146
	/**
147
	 * Remove metadata matching criteria from a form.
148
	 *
149
	 * You can match based on the key, or key and value. Removing based on key and
150
	 * value, will keep from removing duplicate metadata with the same key. It also
151
	 * allows removing all metadata matching key, if needed.
152
	 *
153
	 * @access  public
154
	 * @since   2.0
155
	 *
156
	 * @param   int    $form_id    Form ID.
157
	 * @param   string $meta_key   Metadata name.
158
	 * @param   mixed  $meta_value Optional. Metadata value.
159
	 *
160
	 * @return  bool                  False for failure. True for success.
161
	 */
162
	public function delete_meta( $form_id = 0, $meta_key = '', $meta_value = '' ) {
163
		$form_id = $this->sanitize_id( $form_id );
164
165
		// Bailout.
166
		if ( ! $form_id || ! Give()->logs->log_db->is_log( $form_id ) ) {
167
			return null;
168
		}
169
170
		return delete_metadata( 'form', $form_id, $meta_key, $meta_value );
171
	}
172
173
	/**
174
	 * Create the table
175
	 *
176
	 * @access public
177
	 * @since  2.0
178
	 *
179
	 * @return void
180
	 */
181
	public function create_table() {
182
		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...
183
184
		$charset_collate = $wpdb->get_charset_collate();
185
186
		$sql = "CREATE TABLE {$this->table_name} (
187
			meta_id bigint(20) NOT NULL AUTO_INCREMENT,
188
			form_id bigint(20) NOT NULL,
189
			meta_key varchar(255) DEFAULT NULL,
190
			meta_value longtext,
191
			PRIMARY KEY  (meta_id),
192
			KEY form_id (form_id),
193
			KEY meta_key (meta_key)
194
			) {$charset_collate};";
195
196
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
197
		dbDelta( $sql );
198
199
		update_option( $this->table_name . '_db_version', $this->version );
200
	}
201
202
	/**
203
	 * Add support for hidden functions.
204
	 *
205
	 * @since  2.0
206
	 * @access public
207
	 *
208
	 * @param $name
209
	 * @param $arguments
210
	 *
211
	 * @return mixed
212
	 */
213
	public function __call( $name, $arguments ) {
214
		switch ( $name ) {
215
			case '__add_meta':
216
				$check    = $arguments[0];
217
				$log_id   = $arguments[1];
218
				$meta_key = $arguments[2];
219
				$single   = $arguments[3];
220
221
				return $this->add_meta( $log_id, $meta_key, $single );
222
223
			case '__get_meta':
224
				$check    = $arguments[0];
225
				$log_id   = $arguments[1];
226
				$meta_key = $arguments[2];
227
				$single   = $arguments[3];
228
229
				return $this->get_meta( $log_id, $meta_key, $single );
230
231
			case '__update_meta':
232
				$check    = $arguments[0];
233
				$log_id   = $arguments[1];
234
				$meta_key = $arguments[2];
235
				$single   = $arguments[3];
236
237
				return $this->update_meta( $log_id, $meta_key, $single );
238
239
			case '__delete_meta':
240
				$check    = $arguments[0];
241
				$log_id   = $arguments[1];
242
				$meta_key = $arguments[2];
243
				$single   = $arguments[3];
244
245
				return $this->delete_meta( $log_id, $meta_key, $single );
246
		}
247
	}
248
}
249