C:\xampp\htdocs\landing\wp-content\plugins\wpforms-lite\includes\class-db.php


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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php

/**
 * DB class.
 *
 * This handy class originated from Pippin's Easy Digital Downloads.
 * https://github.com/easydigitaldownloads/easy-digital-downloads/blob/master/includes/class-edd-db.php
 *
 * Sub-classes should define $table_name, $version, and $primary_key in __construct() method.
 *
 * @since 1.1.6
 */
abstract class WPForms_DB {

    
/**
     * Database table name.
     *
     * @since 1.1.6
     *
     * @var string
     */
    
public $table_name;

    
/**
     * Database version.
     *
     * @since 1.1.6
     *
     * @var string
     */
    
public $version;

    
/**
     * Primary key (unique field) for the database table.
     *
     * @since 1.1.6
     *
     * @var string
     */
    
public $primary_key;

    
/**
     * Database type identifier.
     *
     * @since 1.5.1
     *
     * @var string
     */
    
public $type;

    
/**
     * Retrieve the list of columns for the database table.
     * Sub-classes should define an array of columns here.
     *
     * @since 1.1.6
     *
     * @return array List of columns.
     */
    
public function get_columns() {

        return array();
    }

    
/**
     * Retrieve column defaults.
     * Sub-classes can define default for any/all of columns defined in the get_columns() method.
     *
     * @since 1.1.6
     *
     * @return array All defined column defaults.
     */
    
public function get_column_defaults() {

        return array();
    }

    
/**
     * Retrieve a row from the database based on a given row ID.
     *
     * @since 1.1.6
     *
     * @param int $row_id Row ID.
     *
     * @return null|object
     */
    
public function get$row_id ) {

        global 
$wpdb;

        
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
        
return $wpdb->get_row(
            
$wpdb->prepare(
                
"SELECT * FROM {$this->table_name} WHERE {$this->primary_key} = %s LIMIT 1;",
                
$row_id
            
)
        );
    }

    
/**
     * Retrieve a row based on column and row ID.
     *
     * @since 1.1.6
     *
     * @param string     $column Column name.
     * @param int|string $row_id Row ID.
     *
     * @return object|null|bool Database query result, object or null on failure.
     */
    
public function get_by$column$row_id ) {

        global 
$wpdb;

        if ( empty( 
$row_id ) || ! array_key_exists$column$this->get_columns() ) ) {
            return 
false;
        }

        
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
        
return $wpdb->get_row(
            
$wpdb->prepare(
                
"SELECT * FROM $this->table_name WHERE $column = '%s' LIMIT 1;",
                
$row_id
            
)
        );
    }

    
/**
     * Retrieve a value based on column name and row ID.
     *
     * @since 1.1.6
     *
     * @param string     $column Column name.
     * @param int|string $row_id Row ID.
     *
     * @return string|null Database query result (as string), or null on failure.
     */
    
public function get_column$column$row_id ) {

        global 
$wpdb;

        if ( empty( 
$row_id ) || ! array_key_exists$column$this->get_columns() ) ) {
            return 
false;
        }

        
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
        
return $wpdb->get_var(
            
$wpdb->prepare(
                
"SELECT $column FROM $this->table_name WHERE $this->primary_key = '%s' LIMIT 1;",
                
$row_id
            
)
        );
    }

    
/**
     * Retrieve one column value based on another given column and matching value.
     *
     * @since 1.1.6
     *
     * @param string $column       Column name.
     * @param string $column_where Column to match against in the WHERE clause.
     * @param string $column_value Value to match to the column in the WHERE clause.
     *
     * @return string|null Database query result (as string), or null on failure.
     */
    
public function get_column_by$column$column_where$column_value ) {

        global 
$wpdb;

        if ( empty( 
$column ) || empty( $column_where ) || empty( $column_value ) || ! array_key_exists$column$this->get_columns() ) ) {
            return 
false;
        }

        
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
        
return $wpdb->get_var(
            
$wpdb->prepare(
                
"SELECT $column FROM $this->table_name WHERE $column_where = %s LIMIT 1;",
                
$column_value
            
)
        );
    }

    
/**
     * Insert a new record into the database.
     *
     * @since 1.1.6
     *
     * @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 '' ) {

        global 
$wpdb;

        
// Set default values.
        
$data wp_parse_args$data$this->get_column_defaults() );

        
do_action'wpforms_pre_insert_' $type$data );

        
// Initialise column format array.
        
$column_formats $this->get_columns();

        
// Force fields to lower case.
        
$data array_change_key_case$data );

        
// White list columns.
        
$data array_intersect_key$data$column_formats );

        
// Reorder $column_formats to match the order of columns given in $data.
        
$data_keys      array_keys$data );
        
$column_formats array_mergearray_flip$data_keys ), $column_formats );

        
$wpdb->insert$this->table_name$data$column_formats );

        
do_action'wpforms_post_insert_' $type$wpdb->insert_id$data );

        return 
$wpdb->insert_id;
    }

    
/**
     * Insert a new record into the database. This runs the add method.
     *
     * @since 1.1.6
     *
     * @param array $data Column data.
     *
     * @return int ID for the newly inserted record.
     */
    
public function insert$data ) {

        return 
$this->add$data );
    }

    
/**
     * Update an existing record in the database.
     *
     * @since 1.1.6
     *
     * @param int|string $row_id Row ID for the record being updated.
     * @param array      $data   Optional. Array of columns and associated data to update. Default empty array.
     * @param string     $where  Optional. Column to match against in the WHERE clause. If empty, $primary_key
     *                           will be used. Default empty.
     * @param string     $type   Optional. Data type context, e.g. 'affiliate', 'creative', etc. Default empty.
     *
     * @return bool False if the record could not be updated, true otherwise.
     */
    
public function update$row_id$data = array(), $where ''$type '' ) {

        global 
$wpdb;

        
// Row ID must be a positive integer.
        
$row_id absint$row_id );

        if ( empty( 
$row_id ) ) {
            return 
false;
        }

        if ( empty( 
$where ) ) {
            
$where $this->primary_key;
        }

        
do_action'wpforms_pre_update_' $type$data );

        
// Initialise column format array.
        
$column_formats $this->get_columns();

        
// Force fields to lower case.
        
$data array_change_key_case$data );

        
// White list columns.
        
$data array_intersect_key$data$column_formats );

        
// Reorder $column_formats to match the order of columns given in $data.
        
$data_keys      array_keys$data );
        
$column_formats array_mergearray_flip$data_keys ), $column_formats );

        
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
        
if ( false === $wpdb->update$this->table_name$data, array( $where => $row_id ), $column_formats ) ) {
            return 
false;
        }

        
do_action'wpforms_post_update_' $type$data );

        return 
true;
    }

    
/**
     * Delete a record from the database.
     *
     * @since 1.1.6
     *
     * @param int|string $row_id Row ID.
     *
     * @return bool False if the record could not be deleted, true otherwise.
     */
    
public function delete$row_id ) {

        global 
$wpdb;

        
// Row ID must be positive integer.
        
$row_id absint$row_id );

        if ( empty( 
$row_id ) ) {
            return 
false;
        }

        
do_action'wpforms_pre_delete'$row_id );
        
do_action'wpforms_pre_delete_' $this->type$row_id );

        
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
        
if ( false === $wpdb->query$wpdb->prepare"DELETE FROM {$this->table_name} WHERE {$this->primary_key} = %d"$row_id ) ) ) {
            return 
false;
        }

        
do_action'wpforms_post_delete'$row_id );
        
do_action'wpforms_post_delete_' $this->type$row_id );

        return 
true;
    }

    
/**
     * Delete a record from the database by column.
     *
     * @since 1.1.6
     *
     * @param string     $column       Column name.
     * @param int|string $column_value Column value.
     *
     * @return bool False if the record could not be deleted, true otherwise.
     */
    
public function delete_by$column$column_value ) {

        global 
$wpdb;

        if ( empty( 
$column ) || empty( $column_value ) || ! array_key_exists$column$this->get_columns() ) ) {
            return 
false;
        }

        
do_action'wpforms_pre_delete'$column_value );
        
do_action'wpforms_pre_delete_' $this->type$column_value );

        
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
        
if ( false === $wpdb->query$wpdb->prepare"DELETE FROM {$this->table_name} WHERE $column = %s"$column_value ) ) ) {
            return 
false;
        }

        
do_action'wpforms_post_delete'$column_value );
        
do_action'wpforms_post_delete_' $this->type$column_value );

        return 
true;
    }

    
/**
     * Delete record(s) from the database using WHERE IN syntax.
     *
     * @since 1.6.4
     *
     * @param string $column        Column name.
     * @param mixed  $column_values Column values.
     *
     * @return int|bool Number of deleted records, false otherwise.
     */
    
public function delete_where_in$column$column_values ) {

        global 
$wpdb;

        if ( empty( 
$column ) || empty( $column_values ) ) {
            return 
false;
        }

        if ( ! 
array_key_exists$column$this->get_columns() ) ) {
            return 
false;
        }

        
$values is_array$column_values ) ? $column_values : [ $column_values ];

        foreach ( 
$values as $key => $value ) {
            
// Check if a string contains an integer and sanitize accordingly.
            
if ( (string) (int) $value === $value ) {
                
$values$key ]       = (int) $value;
                
$placeholders$key ] = '%d';
            } else {
                
$values$key ]       = sanitize_text_field$value );
                
$placeholders$key ] = '%s';
            }
        }

        
$placeholders = isset( $placeholders ) ? implode','$placeholders ) : '';
        
$sql          "DELETE FROM {$this->table_name} WHERE {$column} IN ( {$placeholders} )";

        
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared
        
return $wpdb->query$wpdb->prepare$sql$values ) );
    }

    
/**
     * Check if the given table exists.
     *
     * @since 1.1.6
     * @since 1.5.9 Default value is now the current child class table name.
     *
     * @param string $table The table name. Defaults to the child class table name.
     *
     * @return string|null If the table name exists.
     */
    
public function table_exists$table '' ) {

        global 
$wpdb;

        if ( ! empty( 
$table ) ) {
            
$table sanitize_text_field$table );
        } else {
            
$table $this->table_name;
        }

        
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
        
return $wpdb->get_var$wpdb->prepare'SHOW TABLES LIKE %s'$table ) ) === $table;
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586