1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
<?php
namespace WPForms\Tasks;
/** * Class Meta helps to manage the tasks meta information * between Action Scheduler and WPForms hooks arguments. * We can't pass arguments longer than >191 chars in JSON to AS, * so we need to store them somewhere (and clean from time to time). * * @since 1.5.9 */ class Meta extends \WPForms_DB {
/** * Primary key (unique field) for the database table. * * @since 1.5.9 * * @var string */ public $primary_key = 'id';
/** * Database type identifier. * * @since 1.5.9 * * @var string */ public $type = 'tasks_meta';
/** * Primary class constructor. * * @since 1.5.9 */ public function __construct() {
$this->table_name = self::get_table_name(); }
/** * Get the DB table name. * * @since 1.5.9 * * @return string */ public static function get_table_name() {
global $wpdb;
return $wpdb->prefix . 'wpforms_tasks_meta'; }
/** * Get table columns. * * @since 1.5.9 */ public function get_columns() {
return array( 'id' => '%d', 'action' => '%s', 'data' => '%s', 'date' => '%s', ); }
/** * Default column values. * * @since 1.5.9 * * @return array */ public function get_column_defaults() {
return array( 'action' => '', 'data' => '', 'date' => gmdate( 'Y-m-d H:i:s' ), ); }
/** * Create custom entry meta database table. * Used in migration and on plugin activation. * * @since 1.5.9 */ public function create_table() {
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$charset_collate = '';
if ( ! empty( $wpdb->charset ) ) { $charset_collate .= "DEFAULT CHARACTER SET {$wpdb->charset}"; } if ( ! empty( $wpdb->collate ) ) { $charset_collate .= " COLLATE {$wpdb->collate}"; }
$sql = "CREATE TABLE {$this->table_name} ( id bigint(20) NOT NULL AUTO_INCREMENT, action varchar(255) NOT NULL, data longtext NOT NULL, date datetime NOT NULL, PRIMARY KEY (id) ) {$charset_collate};";
dbDelta( $sql ); }
/** * Remove queue records for a defined period of time in the past. * Calling this method will remove queue records that are older than $period seconds. * * @since 1.5.9 * * @param string $action Action that should be cleaned up. * @param int $interval Number of seconds from now. * * @return int Number of removed tasks meta records. */ public function clean_by( $action, $interval ) {
global $wpdb;
if ( empty( $action ) || empty( $interval ) ) { return 0; }
$table = self::get_table_name(); $action = sanitize_key( $action ); $date = gmdate( 'Y-m-d H:i:s', time() - (int) $interval );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching return (int) $wpdb->query( $wpdb->prepare( "DELETE FROM `$table` WHERE action = %s AND date < %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $action, $date ) ); }
/** * Inserts a new record into the database. * * @since 1.5.9 * * @param array $data Column data. * @param string $type Optional. Data type context. * * @return int ID for the newly inserted record. 0 otherwise. */ public function add( $data, $type = '' ) {
if ( empty( $data['action'] ) || ! is_string( $data['action'] ) ) { return 0; }
$data['action'] = sanitize_key( $data['action'] );
if ( isset( $data['data'] ) ) { $string = wp_json_encode( $data['data'] );
if ( $string === false ) { $string = ''; }
/* * We are encoding the string representation of all the data * to make sure that nothing can harm the database. * This is not an encryption, and we need this data later as is, * so we are using one of the fastest way to do that. * This data is removed from DB on a daily basis. */ // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode $data['data'] = base64_encode( $string ); }
if ( empty( $type ) ) { $type = $this->type; }
return parent::add( $data, $type ); }
/** * Retrieve a row from the database based on a given row ID. * * @since 1.5.9} * * @param int $meta_id Meta ID. * * @return null|object */ public function get( $meta_id ) {
$meta = parent::get( $meta_id );
if ( empty( $meta ) || empty( $meta->data ) ) { return $meta; }
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode $decoded = base64_decode( $meta->data );
if ( $decoded === false || ! is_string( $decoded ) ) { $meta->data = ''; } else { $meta->data = json_decode( $decoded, true ); }
return $meta; } }
|