Completed
Push — issues/1132 ( b0ddd9...34d612 )
by Ravinder
22:33 queued 02:37
created

Give_DB_Log_Meta::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 15
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
 * 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
	 * Give_DB_Log_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->logmeta     = $this->table_name = $wpdb->prefix . 'give_logmeta';
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
			'log_id'     => '%d',
59
			'meta_key'   => '%s',
60
			'meta_value' => '%s',
61
		);
62
	}
63
64
	/**
65
	 * Retrieve log meta field for a log.
66
	 *
67
	 * @access  private
68
	 * @since   2.0
69
	 *
70
	 * @param   int    $log_id   Log ID.
71
	 * @param   string $meta_key The meta key to retrieve.
72
	 * @param   bool   $single   Whether to return a single value.
73
	 *
74
	 * @return  mixed                 Will be an array if $single is false. Will be value of meta data field if $single is true.
75
	 */
76
	public function get_meta( $log_id, $meta_key, $single ) {
77
		$log_id = $this->sanitize_id( $log_id );
78
79
		// Bailout.
80
		if ( ! $log_id || ! Give()->logs->log_db->is_log( $log_id ) ) {
81
			return null;
82
		}
83
84
		return get_metadata( 'log', $log_id, $meta_key, $single );
85
	}
86
87
	/**
88
	 * Add meta data field to a log.
89
	 *
90
	 * @access  private
91
	 * @since   2.0
92
	 *
93
	 * @param   int    $log_id     Log ID.
94
	 * @param   string $meta_key   Metadata name.
95
	 * @param   mixed  $meta_value Metadata value.
96
	 * @param   bool   $unique     Optional, default is false. Whether the same key should not be added.
97
	 *
98
	 * @return  bool                  False for failure. True for success.
99
	 */
100
	public function add_meta( $log_id = 0, $meta_key = '', $meta_value, $unique = false ) {
101
		$log_id = $this->sanitize_id( $log_id );
102
		if ( ! $log_id || ! Give()->logs->log_db->is_log( $log_id ) ) {
103
			return null;
104
		}
105
106
		return add_metadata( 'log', $log_id, $meta_key, $meta_value, $unique );
107
	}
108
109
	/**
110
	 * Update log meta field based on Log ID.
111
	 *
112
	 * @access  private
113
	 * @since   2.0
114
	 *
115
	 * @param   int    $log_id     Log ID.
116
	 * @param   string $meta_key   Metadata key.
117
	 * @param   mixed  $meta_value Metadata value.
118
	 * @param   mixed  $prev_value Optional. Previous value to check before removing.
119
	 *
120
	 * @return  bool                  False on failure, true if success.
121
	 */
122
	public function update_meta( $log_id = 0, $meta_key = '', $meta_value, $prev_value = '' ) {
123
		$log_id = $this->sanitize_id( $log_id );
124
		if ( ! $log_id || ! Give()->logs->log_db->is_log( $log_id ) ) {
125
			return null;
126
		}
127
128
		return update_metadata( 'log', $log_id, $meta_key, $meta_value, $prev_value );
129
	}
130
131
	/**
132
	 * Remove metadata matching criteria from a log.
133
	 *
134
	 * For internal use only. Use Give_Log->delete_meta() for public usage.
135
	 *
136
	 * You can match based on the key, or key and value. Removing based on key and
137
	 * value, will keep from removing duplicate metadata with the same key. It also
138
	 * allows removing all metadata matching key, if needed.
139
	 *
140
	 * @access  private
141
	 * @since   2.0
142
	 *
143
	 * @param   int    $log_id     Log ID.
144
	 * @param   string $meta_key   Metadata name.
145
	 * @param   mixed  $meta_value Optional. Metadata value.
146
	 *
147
	 * @return  bool                  False for failure. True for success.
148
	 */
149
	public function delete_meta( $log_id = 0, $meta_key = '', $meta_value = '' ) {
150
		$log_id = $this->sanitize_id( $log_id );
151
152
		if ( ! $log_id || ! Give()->logs->log_db->is_log( $log_id ) ) {
153
			return null;
154
		}
155
156
		return delete_metadata( 'log', $log_id, $meta_key, $meta_value );
157
	}
158
159
160
	/**
161
	 * Delete all log meta
162
	 *
163
	 * @since  2.0
164
	 * @access public
165
	 *
166
	 * @param int $log_id
167
	 *
168
	 * @return bool
169
	 */
170
	public function delete_row( $log_id = 0 ) {
171
		/* @var WPDB $wpdb */
172
		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...
173
174
		// Row ID must be positive integer
175
		$log_id = absint( $log_id );
176
177
		if ( empty( $log_id ) ) {
178
			return false;
179
		}
180
181
		if ( false === $wpdb->query( $wpdb->prepare( "DELETE FROM $this->table_name WHERE log_id = %d", $log_id ) ) ) {
182
			return false;
183
		}
184
185
		return true;
186
	}
187
188
	/**
189
	 * Create the table
190
	 *
191
	 * @access public
192
	 * @since  2.0
193
	 *
194
	 * @return void
195
	 */
196
	public function create_table() {
197
		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...
198
		$charset_collate = $wpdb->get_charset_collate();
199
200
		$sql = "CREATE TABLE {$wpdb->logmeta} (
201
			meta_id bigint(20) NOT NULL AUTO_INCREMENT,
202
			log_id bigint(20) NOT NULL,
203
			meta_key varchar(255) DEFAULT NULL,
204
			meta_value longtext,
205
			PRIMARY KEY  (meta_id),
206
			KEY log_id (log_id),
207
			KEY meta_key (meta_key)
208
			) {$charset_collate};";
209
210
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
211
		dbDelta( $sql );
212
213
		update_option( $this->table_name . '_db_version', $this->version );
214
	}
215
216
217
	/**
218
	 * Add support for hidden functions.
219
	 *
220
	 * @since  2.0
221
	 * @access public
222
	 *
223
	 * @param $name
224
	 * @param $arguments
225
	 *
226
	 * @return mixed
227
	 */
228
	public function __call( $name, $arguments ) {
229
		switch ( $name ) {
230
			case '__add_meta':
231
				$check    = $arguments[0];
232
				$log_id   = $arguments[1];
233
				$meta_key = $arguments[2];
234
				$single   = $arguments[3];
235
236
				return $this->add_meta( $log_id, $meta_key, $single );
237
238
			case '__get_meta':
239
				$check    = $arguments[0];
240
				$log_id   = $arguments[1];
241
				$meta_key = $arguments[2];
242
				$single   = $arguments[3];
243
244
				return $this->get_meta( $log_id, $meta_key, $single );
245
246
			case '__update_meta':
247
				$check    = $arguments[0];
248
				$log_id   = $arguments[1];
249
				$meta_key = $arguments[2];
250
				$single   = $arguments[3];
251
252
				return $this->update_meta( $log_id, $meta_key, $single );
253
254
			case '__delete_meta':
255
				$check    = $arguments[0];
256
				$log_id   = $arguments[1];
257
				$meta_key = $arguments[2];
258
				$single   = $arguments[3];
259
260
				return $this->delete_meta( $log_id, $meta_key, $single );
261
		}
262
	}
263
}
264