Completed
Push — issues/1796 ( b53179...52862a )
by Ravinder
41:58 queued 22:00
created

Give_DB_Log_Meta::delete_row()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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