Test Failed
Push — feature/meta-tables ( 709186...4c8cbd )
by Ravinder
05:18
created

Give_DB_Log_Meta::get_meta()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 3
dl 0
loc 22
rs 8.9197
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_Meta {
25
	/**
26
	 * Meta supports.
27
	 *
28
	 * @since  2.0
29
	 * @access protected
30
	 * @var array
31
	 */
32
	protected $supports = array(
33
		'add_post_metadata',
34
		'get_post_metadata',
35
		'update_post_metadata',
36
		'delete_post_metadata',
37
	);
38
39
	/**
40
	 * Post type
41
	 *
42
	 * @since  2.0
43
	 * @access protected
44
	 * @var bool
45
	 */
46
	protected $post_type = 'give_log';
47
48
	/**
49
	 * Meta type
50
	 *
51
	 * @since  2.0
52
	 * @access protected
53
	 * @var bool
54
	 */
55
	protected $meta_type = 'log';
56
57
	/**
58
	 * Give_DB_Log_Meta constructor.
59
	 *
60
	 * @access  public
61
	 * @since   2.0
62
	 */
63 View Code Duplication
	public function __construct() {
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...
64
		/* @var WPDB $wpdb */
65
		global $wpdb;
66
67
		$wpdb->logmeta     = $this->table_name = $wpdb->prefix . 'give_logmeta';
68
		$this->primary_key = 'meta_id';
69
		$this->version     = '1.0';
70
71
		$this->register_table();
72
73
		parent::__construct();
74
	}
75
76
	/**
77
	 * Get table columns and data types.
78
	 *
79
	 * @access  public
80
	 * @since   2.0
81
	 *
82
	 * @return  array  Columns and formats.
83
	 */
84
	public function get_columns() {
85
		return array(
86
			'meta_id'    => '%d',
87
			'log_id'     => '%d',
88
			'meta_key'   => '%s',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
89
			'meta_value' => '%s',
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
90
		);
91
	}
92
93
	/**
94
	 * Delete all log meta
95
	 *
96
	 * @since  2.0
97
	 * @access public
98
	 *
99
	 * @param int $log_id
100
	 *
101
	 * @return bool
102
	 */
103 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...
104
		/* @var WPDB $wpdb */
105
		global $wpdb;
106
107
		// Row ID must be positive integer
108
		$log_id = absint( $log_id );
109
110
		if ( empty( $log_id ) ) {
111
			return false;
112
		}
113
114
		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...
115
			return false;
116
		}
117
118
		return true;
119
	}
120
121
	/**
122
	 * Create the table
123
	 *
124
	 * @access public
125
	 * @since  2.0
126
	 *
127
	 * @return void
128
	 */
129
	public function create_table() {
130
		global $wpdb;
131
		$charset_collate = $wpdb->get_charset_collate();
132
133
		$sql = "CREATE TABLE {$wpdb->logmeta} (
134
			meta_id bigint(20) NOT NULL AUTO_INCREMENT,
135
			log_id bigint(20) NOT NULL,
136
			meta_key varchar(255) DEFAULT NULL,
137
			meta_value longtext,
138
			PRIMARY KEY  (meta_id),
139
			KEY log_id (log_id),
140
			KEY meta_key (meta_key)
141
			) {$charset_collate};";
142
143
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
144
		dbDelta( $sql );
145
146
		update_option( $this->table_name . '_db_version', $this->version );
147
	}
148
149
150
	/**
151
	 * Add support for hidden functions.
152
	 *
153
	 * @since  2.0
154
	 * @access public
155
	 *
156
	 * @param $name
157
	 * @param $arguments
158
	 *
159
	 * @return mixed
160
	 */
161 View Code Duplication
	public function __call( $name, $arguments ) {
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...
162
		if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
163
			return;
164
		}
165
166
		switch ( $name ) {
167
			case '__add_meta':
168
				$this->check = $arguments[0];
169
				$log_id      = $arguments[1];
170
				$meta_key    = $arguments[2];
171
				$meta_value  = $arguments[3];
172
				$unique      = $arguments[4];
173
174
				return $this->add_meta( $log_id, $meta_key, $meta_value, $unique );
175
176
			case '__get_meta':
177
				$this->check = $arguments[0];
178
				$log_id      = $arguments[1];
179
				$meta_key    = $arguments[2];
180
				$single      = $arguments[3];
181
182
				$this->raw_result = true;
183
184
				return $this->get_meta( $log_id, $meta_key, $single );
185
186
			case '__update_meta':
187
				$this->check = $arguments[0];
188
				$log_id      = $arguments[1];
189
				$meta_key    = $arguments[2];
190
				$meta_value  = $arguments[3];
191
192
				return $this->update_meta( $log_id, $meta_key, $meta_value );
193
194
			case '__delete_meta':
195
				$this->check = $arguments[0];
196
				$log_id      = $arguments[1];
197
				$meta_key    = $arguments[2];
198
				$meta_value  = $arguments[3];
199
				$delete_all  = $arguments[4];
200
201
				return $this->delete_meta( $log_id, $meta_key, $meta_value, $delete_all );
0 ignored issues
show
Unused Code introduced by
The call to Give_DB_Log_Meta::delete_meta() has too many arguments starting with $delete_all.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
202
		}
203
	}
204
}
205