Completed
Push — issues/1796 ( 616c54 )
by Ravinder
20:12
created

Give_DB_Log_Meta::register_table()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
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
	 * Register the table with $wpdb so the metadata api can find it.
62
	 *
63
	 * @access  public
64
	 * @since   2.0
65
	 *
66
	 * @return  void
67
	 */
68
	public function register_table() {
69
		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...
70
		$wpdb->logmeta = $this->table_name;
71
	}
72
73
	/**
74
	 * Retrieve log meta field for a log.
75
	 *
76
	 * @access  private
77
	 * @since   2.0
78
	 *
79
	 * @param   int    $log_id   Log ID.
80
	 * @param   string $meta_key The meta key to retrieve.
81
	 * @param   bool   $single   Whether to return a single value.
82
	 *
83
	 * @return  mixed                 Will be an array if $single is false. Will be value of meta data field if $single is true.
84
	 */
85
	public function get_meta( $log_id = 0, $meta_key = '', $single = false ) {
86
		$log_id = $this->sanitize_log_id( $log_id );
87
		if ( false === $log_id ) {
88
			return false;
89
		}
90
91
		return get_metadata( 'log', $log_id, $meta_key, $single );
92
	}
93
94
	/**
95
	 * Add meta data field to a log.
96
	 *
97
	 * @access  private
98
	 * @since   2.0
99
	 *
100
	 * @param   int    $log_id     Log ID.
101
	 * @param   string $meta_key   Metadata name.
102
	 * @param   mixed  $meta_value Metadata value.
103
	 * @param   bool   $unique     Optional, default is false. Whether the same key should not be added.
104
	 *
105
	 * @return  bool                  False for failure. True for success.
106
	 */
107
	public function add_meta( $log_id = 0, $meta_key = '', $meta_value, $unique = false ) {
108
		$log_id = $this->sanitize_log_id( $log_id );
109
		if ( false === $log_id ) {
110
			return false;
111
		}
112
113
		return add_metadata( 'log', $log_id, $meta_key, $meta_value, $unique );
114
	}
115
116
	/**
117
	 * Update log meta field based on Log ID.
118
	 *
119
	 * @access  private
120
	 * @since   2.0
121
	 *
122
	 * @param   int    $log_id     Log ID.
123
	 * @param   string $meta_key   Metadata key.
124
	 * @param   mixed  $meta_value Metadata value.
125
	 * @param   mixed  $prev_value Optional. Previous value to check before removing.
126
	 *
127
	 * @return  bool                  False on failure, true if success.
128
	 */
129
	public function update_meta( $log_id = 0, $meta_key = '', $meta_value, $prev_value = '' ) {
130
		$log_id = $this->sanitize_log_id( $log_id );
131
		if ( false === $log_id ) {
132
			return false;
133
		}
134
135
		return update_metadata( 'log', $log_id, $meta_key, $meta_value, $prev_value );
136
	}
137
138
	/**
139
	 * Remove metadata matching criteria from a log.
140
	 *
141
	 * For internal use only. Use Give_Log->delete_meta() for public usage.
142
	 *
143
	 * You can match based on the key, or key and value. Removing based on key and
144
	 * value, will keep from removing duplicate metadata with the same key. It also
145
	 * allows removing all metadata matching key, if needed.
146
	 *
147
	 * @access  private
148
	 * @since   2.0
149
	 *
150
	 * @param   int    $log_id     Log ID.
151
	 * @param   string $meta_key   Metadata name.
152
	 * @param   mixed  $meta_value Optional. Metadata value.
153
	 *
154
	 * @return  bool                  False for failure. True for success.
155
	 */
156
	public function delete_meta( $log_id = 0, $meta_key = '', $meta_value = '' ) {
157
		return delete_metadata( 'log', $log_id, $meta_key, $meta_value );
158
	}
159
160
	/**
161
	 * Create the table
162
	 *
163
	 * @access public
164
	 * @since  2.0
165
	 *
166
	 * @return void
167
	 */
168
	public function create_table() {
169
170
		$sql = "CREATE TABLE {$this->table_name} (
171
			meta_id bigint(20) NOT NULL AUTO_INCREMENT,
172
			log_id bigint(20) NOT NULL,
173
			meta_key varchar(255) DEFAULT NULL,
174
			meta_value longtext,
175
			PRIMARY KEY  (meta_id),
176
			KEY log_id (log_id),
177
			KEY meta_key (meta_key)
178
			) CHARACTER SET utf8 COLLATE utf8_general_ci;";
179
180
		dbDelta( $sql );
181
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
182
183
		update_option( $this->table_name . '_db_version', $this->version );
184
	}
185
186
	/**
187
	 * Given a log ID, make sure it's a positive number, greater than zero before inserting or adding.
188
	 *
189
	 * @access private
190
	 * @since  2.0
191
	 *
192
	 * @param  int|stripe $log_id A passed log ID.
193
	 *
194
	 * @return int|bool                The normalized log ID or false if it's found to not be valid.
195
	 */
196
	private function sanitize_log_id( $log_id ) {
197
		if ( ! is_numeric( $log_id ) ) {
198
			return false;
199
		}
200
201
		$log_id = (int) $log_id;
202
203
		// We were given a non positive number.
204
		if ( absint( $log_id ) !== $log_id ) {
205
			return false;
206
		}
207
208
		if ( empty( $log_id ) ) {
209
			return false;
210
		}
211
212
		return absint( $log_id );
213
214
	}
215
216
}
217