PluginProbe
Snippet Shortcodes / 5.1
Snippet Shortcodes v5.1
5.2.1 5.2 5.1.8 5.1.6 5.1.7 5.1.5 trunk 1.0 1.1 1.2 1.3 1.3.1 1.4 1.5 1.5.1 1.6 1.6.1 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.8 2.0 2.0.1 All 74 releases
shortcode-variables / includes / db.php

db.php in Snippet Shortcodes 5.1, at includes/db.php

568 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined('ABSPATH') or die('Jog on!');
4
5 /**
6 * Build database table
7 */
8 function sh_cd_create_database_table() {
9
10 global $wpdb;
11
12 $table_name = $wpdb->prefix . SH_CD_TABLE;
13
14 $charset_collate = $wpdb->get_charset_collate();
15
16 $sql = "CREATE TABLE $table_name (
17 id mediumint(9) NOT NULL AUTO_INCREMENT,
18 slug varchar(100) NOT NULL,
19 previous_slug varchar(100) NOT NULL,
20 editor varchar(10) NULL,
21 data text,
22 disabled bit default 0,
23 multisite bit default 0,
24 header bit default 0,
25 footer bit default 0,
26 device_type varchar(50) NULL DEFAULT '[\"desktop\",\"mobile\",\"tablet\"]',
27 UNIQUE KEY id (id)
28 ) $charset_collate;";
29
30 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
31
32 dbDelta( $sql );
33 }
34
35 /**
36 * Build database table for multisite
37 */
38 function sh_cd_create_database_table_multisite() {
39
40 global $wpdb;
41
42 $table_name = $wpdb->base_prefix . SH_CD_TABLE_MULTISITE;
43
44 $charset_collate = $wpdb->get_charset_collate();
45
46 $sql = "CREATE TABLE $table_name (
47 id mediumint(9) NOT NULL AUTO_INCREMENT,
48 slug varchar(100) NOT NULL,
49 data text,
50 site_id int default 0,
51 header bit default 0,
52 footer bit default 0,
53 device_type varchar(50) NULL DEFAULT '[\"desktop\",\"mobile\",\"tablet\"]',
54 UNIQUE KEY id (id)
55 ) $charset_collate;";
56
57 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
58
59 dbDelta( $sql );
60 }
61
62 /**
63 * Fetch all Shortcodes
64 *
65 * @return bool
66 */
67 function sh_cd_db_shortcodes_all() {
68
69 global $wpdb;
70
71 return $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE . ' order by slug asc', ARRAY_A );
72 }
73
74 /**
75 * Fetch all enabled Shortcodes
76 *
77 * @return bool
78 */
79 function sh_cd_db_shortcodes_all_enabled() {
80
81 global $wpdb;
82
83 return $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where disabled = 0 order by slug asc', ARRAY_A );
84 }
85
86 /**
87 * Fetch a count of all shortcodes
88 *
89 * @return bool
90 */
91 function sh_cd_db_shortcodes_count() {
92
93 global $wpdb;
94
95 return $wpdb->get_var( 'SELECT count(id) FROM ' . $wpdb->prefix . SH_CD_TABLE );
96 }
97
98 /**
99 * Fetch a shortcode by ID (mainly used for quick lookups in admin)
100 *
101 * @param $id
102 *
103 * @return mixed
104 */
105 function sh_cd_db_shortcodes_by_id( $id ) {
106
107 global $wpdb;
108
109 $sql = $wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id);
110 $shortcode = $wpdb->get_row( $sql, ARRAY_A );
111
112 if ( false === empty( $shortcode ) ) {
113 $shortcode = sh_cd_db_filter_loaded_shortcode( $shortcode );
114 }
115
116 return $shortcode;
117 }
118
119 /**
120 * Fetch the content of the shortcode by slug
121 *
122 * @param $slug
123 *
124 * @return null|string
125 */
126 function sh_cd_db_shortcodes_by_slug( $slug ) {
127
128 global $wpdb;
129
130 $sqls = [];
131
132 // If multi site functionality is enabled, look there first for the shortcode!
133 if ( true === sh_cd_is_multisite_enabled() ) {
134
135 $sql = $wpdb->prepare('SELECT * FROM ' . $wpdb->base_prefix . SH_CD_TABLE_MULTISITE . ' where slug = %s', $slug);
136
137 $shortcode = $wpdb->get_row( $sql, ARRAY_A );
138
139 if ( false === empty( $shortcode[ 'data' ] ) ) {
140 $shortcode[ 'data' ] = stripslashes( $shortcode[ 'data' ] );
141 $shortcode[ 'source' ] = 'multisite';
142 return sh_cd_db_filter_loaded_shortcode( $shortcode );
143 }
144 }
145
146 // No multisite slug by this name, so check the main table
147 $sql = $wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s and disabled <> 1', $slug);
148 $shortcode = $wpdb->get_row( $sql, ARRAY_A );
149
150 if ( false === empty( $shortcode[ 'data' ] ) ) {
151 $shortcode[ 'data' ] = stripslashes( $shortcode[ 'data' ] );
152 $shortcode[ 'source' ] = 'local';
153 return sh_cd_db_filter_loaded_shortcode( $shortcode );
154 }
155
156 return NULL;
157 }
158
159 /**
160 * Allow processing of loaded shortcode data
161 */
162 function sh_cd_db_filter_loaded_shortcode( $shortcode ) {
163 return apply_filters( 'sh-cd-db-loaded-shortcode', $shortcode );
164 }
165
166 /**
167 * Get a Shortcode's slug from the slug ID
168 *
169 * @param $id
170 *
171 * @return bool
172 */
173 function sh_cd_db_shortcodes_get_slug_by_id( $id ) {
174
175 global $wpdb;
176
177 $sql = $wpdb->prepare('SELECT slug FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id);
178
179 return $wpdb->get_var( $sql );
180 }
181
182 /**
183 * Save / Insert a shortcode
184 *
185 * @param $shortcode
186 *
187 * @return bool
188 */
189 function sh_cd_db_shortcodes_save( $shortcode, $return_shortcode = false ) {
190
191 if ( false === is_admin() ) {
192 return false;
193 }
194
195 $multi_site_enabled = sh_cd_is_multisite_enabled();
196
197 $default_values = apply_filters( 'sh-cd-db-default-values', [ 'id' => NULL,
198 'slug' => NULL,
199 'previous_slug' => NULL,
200 'editor' => NULL,
201 'data' => NULL,
202 'disabled' => 0,
203 'multisite' => 0
204 ]);
205
206 $shortcode = wp_parse_args( $shortcode, $default_values );
207
208 // We need either a slug or an ID
209 if ( true === empty( $shortcode['slug'] ) && true === empty( $shortcode['id'] ) ) {
210 return false;
211 }
212
213 $shortcode['disabled'] = (int) $shortcode['disabled'];
214 $shortcode['multisite'] = ( true === $multi_site_enabled ) ? (int) $shortcode['multisite'] : 0;
215
216 global $wpdb;
217
218 $result = false;
219
220 // Updating an existing shortcode?
221 if ( false === empty( $shortcode['id'] ) && true === is_numeric( $shortcode['id'] ) ){
222
223 $shortcode['slug'] = sh_cd_slug_generate( $shortcode['slug'], $shortcode['id'] );
224
225 $shortcode = apply_filters( 'sh-cd-db-default-shortcode-before-save', $shortcode );
226
227 $formats = sh_cd_db_get_formats( $shortcode );
228
229 $result = $wpdb->update(
230 $wpdb->prefix . SH_CD_TABLE,
231 $shortcode,
232 [ 'id' => $shortcode['id'] ],
233 $formats,
234 [ '%d' ]
235 );
236
237 if ( false !== $result ) {
238 do_action( 'sh-cd-shortcode-updated', $shortcode );
239 }
240
241 sh_cd_cache_delete_by_slug_or_key( $shortcode['id'] );
242 sh_cd_cache_delete_by_slug_or_key( $shortcode['previous_slug'] );
243
244 // Adding a new shortcode
245 } else {
246
247 unset( $shortcode['id'] );
248
249 // Ensure slug is santised and unique
250 $shortcode['slug'] = sh_cd_slug_generate( $shortcode['slug'] );
251
252 $shortcode = apply_filters( 'sh-cd-db-default-shortcode-before-save', $shortcode );
253
254 $formats = sh_cd_db_get_formats( $shortcode );
255
256 $result = $wpdb->insert(
257 $wpdb->prefix . SH_CD_TABLE,
258 $shortcode,
259 $formats
260 );
261
262 if ( false !== $result ) {
263 do_action( 'sh-cd-shortcode-added', $shortcode );
264 }
265
266 // It's an insert, so there should be no cache... however, just a wee sanity check in case
267 // a shortcode with the same slug previously exists.
268 sh_cd_cache_delete_by_slug_or_key( $shortcode['slug'] );
269 }
270
271 if ( false !== $result ) {
272
273 if ( 1 === $shortcode['multisite'] ) {
274 sh_cd_db_shortcodes_multisite_insert( $shortcode );
275 } else {
276 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
277 }
278
279 sh_cd_cache_delete( $shortcode['slug'] );
280
281 if ( true === $return_shortcode ) {
282
283 $shortcode['id'] = $result;
284 return $shortcode;
285 }
286
287 return true;
288 }
289
290 return false;
291 }
292
293
294 /**
295 * Insert a multisite shortcode
296 *
297 * @param $shortcode
298 *
299 * @return bool
300 */
301 function sh_cd_db_shortcodes_multisite_insert( $shortcode ) {
302
303 if ( false === is_admin() ) {
304 return false;
305 }
306
307 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
308
309 global $wpdb;
310
311 $shortcode_multisite = [ 'slug' => $shortcode['slug'],
312 'data' => $shortcode['data'],
313 'header' => $shortcode['header'],
314 'footer' => $shortcode['footer'],
315 'device_type' => $shortcode['device_type'],
316 'site_id' => get_current_blog_id()
317 ];
318
319 $formats = sh_cd_db_get_formats( $shortcode_multisite );
320
321 $result = $wpdb->insert(
322 $wpdb->base_prefix . SH_CD_TABLE_MULTISITE,
323 $shortcode_multisite,
324 $formats
325 );
326
327 sh_cd_cache_delete( $shortcode['slug'] );
328
329 do_action( 'sh_cd_multisite_changed', $shortcode );
330
331 return ( false !== $result );
332 }
333
334 /**
335 * Delete a multisite shortcode
336 *
337 * @param $slug
338 *
339 * @return bool
340 */
341 function sh_cd_db_shortcodes_multisite_delete( $slug ) {
342
343 global $wpdb;
344
345 $result = $wpdb->delete( $wpdb->base_prefix . SH_CD_TABLE_MULTISITE, [ 'slug' => $slug ], [ '%s' ] );
346
347 sh_cd_cache_delete( $slug );
348
349 do_action( 'sh_cd_multisite_changed', $slug );
350
351 return ( false !== $result );
352 }
353
354 /**
355 * Fetch all multisite slugs
356
357 * @return null|array
358 */
359 function sh_cd_db_shortcodes_multisite_slugs() {
360
361 global $wpdb;
362
363 return $wpdb->get_results( 'SELECT slug FROM ' . $wpdb->base_prefix . SH_CD_TABLE_MULTISITE, ARRAY_A );
364 }
365
366
367 /**
368 * Update a shortcode's status
369 *
370 * @param $shortcode
371 *
372 * @return bool
373 */
374 function sh_cd_db_shortcodes_update_status( $id, $status ) {
375
376 if ( false === is_admin() ) {
377 return false;
378 }
379
380 global $wpdb;
381
382 $result = $wpdb->update(
383 $wpdb->prefix . SH_CD_TABLE,
384 [ 'disabled' => $status ],
385 [ 'id' => $id ],
386 [ '%d' ],
387 [ '%d' ]
388 );
389
390 sh_cd_cache_delete_by_slug_or_key( $id );
391
392 return ( false !== $result );
393 }
394
395 /**
396 * Update a shortcode's multisite
397 *
398 * @param $shortcode
399 *
400 * @return bool
401 */
402 function sh_cd_db_shortcodes_update_multisite( $id, $multisite ) {
403
404 if ( false === is_admin() ) {
405 return false;
406 }
407
408 global $wpdb;
409
410 $result = $wpdb->update(
411 $wpdb->prefix . SH_CD_TABLE,
412 [ 'multisite' => $multisite ],
413 [ 'id' => $id ],
414 [ '%d' ],
415 [ '%d' ]
416 );
417
418 $shortcode = sh_cd_db_shortcodes_by_id( $id );
419
420 if ( false === empty( $shortcode ) ) {
421
422 if ( 1 === (int) $multisite ) {
423 sh_cd_db_shortcodes_multisite_insert( $shortcode );
424 } else {
425 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
426 }
427
428 }
429
430 sh_cd_cache_delete_by_slug_or_key( $id );
431
432 return ( false !== $result );
433 }
434
435 /**
436 * Update a shortcode's content
437 *
438 * @param $shortcode
439 *
440 * @return bool
441 */
442 function sh_cd_db_shortcodes_update_content( $id, $data ) {
443
444 if ( false === is_admin() ) {
445 return false;
446 }
447
448 global $wpdb;
449
450 $result = $wpdb->update(
451 $wpdb->prefix . SH_CD_TABLE,
452 [ 'data' => $data ],
453 [ 'id' => $id ],
454 [ '%s' ],
455 [ '%d' ]
456 );
457
458 $shortcode = sh_cd_db_shortcodes_by_id( $id );
459
460 if ( false === empty( $shortcode ) ) {
461
462 if ( 1 === (int) $shortcode[ 'multisite' ] ) {
463 sh_cd_db_shortcodes_multisite_insert( $shortcode );
464 } else {
465 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
466 }
467
468 }
469 sh_cd_cache_delete_by_slug_or_key( $id );
470
471 return ( false !== $result );
472 }
473
474
475 /**
476 * Delete a shortcode
477 *
478 * @param $id
479 *
480 * @return bool
481 */
482 function sh_cd_db_shortcodes_delete( $id ) {
483
484 if ( false === is_admin() || false === is_numeric( $id ) ) {
485 return false;
486 }
487
488 global $wpdb;
489
490 // Clear multi site
491 $shortcode_before_delete = sh_cd_db_shortcodes_by_id( $id );
492 sh_cd_db_shortcodes_multisite_delete( $shortcode_before_delete['slug'] );
493
494 // Clear cached version
495 sh_cd_cache_delete_by_slug_or_key( $id );
496
497 $slug = sh_cd_db_shortcodes_get_slug_by_id( $id );
498 $result = $wpdb->delete( $wpdb->prefix . SH_CD_TABLE, [ 'id' => $id ], [ '%d' ] );
499
500 if ( false !== $result ) {
501 do_action( 'sh-cd-shortcode-updated', $id, $slug );
502 }
503
504 return ( false !== $result );
505 }
506
507 /**
508 * For a given key value array, look up and return the expected MySQL data formats.
509 *
510 * @param $data
511 *
512 * @return array
513 */
514 function sh_cd_db_get_formats( $data ) {
515
516 $lookup = [
517 'id' => '%d',
518 'slug' => '%s',
519 'previous_slug' => '%s',
520 'data' => '%s',
521 'disabled' => '%d',
522 'multisite' => '%d',
523 'site_id' => '%d',
524 'editor' => '%s',
525 'header' => '%d',
526 'footer' => '%d',
527 'device_type' => '%s'
528 ];
529
530 $formats = [];
531
532 foreach ( $data as $key => $value ) {
533
534 if ( false === empty( $lookup[ $key ] ) ) {
535 $formats[] = $lookup[ $key ];
536 }
537 }
538
539 return $formats;
540 }
541
542 /**
543 * Check if the slug already exists
544 *
545 * @param $slug
546 * @param $existing_id
547 *
548 * @return bool
549 */
550 function sh_cd_slug_is_unique( $slug, $existing_id = NULL ) {
551
552 if ( true === empty( $slug ) ) {
553 return false;
554 }
555
556 global $wpdb;
557
558 $sql = $wpdb->prepare( 'SELECT count(slug) FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s', $slug );
559
560 if ( false === empty( $existing_id ) ) {
561 $sql .= $wpdb->prepare( ' and id <> %d', $existing_id );
562 }
563
564 $row = $wpdb->get_var( $sql );
565
566 return ( empty( $row ) );
567 }
568