Test Failed
Push — feature/meta-tables ( 00f89f...b5a008 )
by Ravinder
05:05
created

Give_DB_Log_Meta::get_meta()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
252
				$log_id     = $arguments[1];
253
				$meta_key   = $arguments[2];
254
				$meta_value = $arguments[3];
255
				$unique     = $arguments[4];
256
257
				return $this->add_meta( $log_id, $meta_key, $meta_value, $unique );
258
259
			case '__get_meta':
260
				$check    = $arguments[0];
0 ignored issues
show
Unused Code introduced by
$check is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
261
				$log_id   = $arguments[1];
262
				$meta_key = $arguments[2];
263
				$single   = $arguments[3];
264
265
				$this->raw_result = true;
266
				return $this->get_meta( $log_id, $meta_key, $single );
267
268
			case '__update_meta':
269
				$check      = $arguments[0];
0 ignored issues
show
Unused Code introduced by
$check is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
270
				$log_id     = $arguments[1];
271
				$meta_key   = $arguments[2];
272
				$meta_value = $arguments[3];
273
274
				return $this->update_meta( $log_id, $meta_key, $meta_value );
275
276
			case '__delete_meta':
277
				$check      = $arguments[0];
0 ignored issues
show
Unused Code introduced by
$check is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
278
				$log_id     = $arguments[1];
279
				$meta_key   = $arguments[2];
280
				$meta_value = $arguments[3];
281
282
				return $this->delete_meta( $log_id, $meta_key, $meta_value );
283
		}
284
	}
285
}
286