PluginProbe
Snippet Shortcodes / 4.1.4
Snippet Shortcodes v4.1.4
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
← All changes | includes/db.php +43 -261 5.24.1.4 View file →
@@ -16,20 +16,11 @@
16 16 $sql = "CREATE TABLE $table_name (
17 17 id mediumint(9) NOT NULL AUTO_INCREMENT,
18 18 slug varchar(100) NOT NULL,
19 19 previous_slug varchar(100) NOT NULL,
20 - editor varchar(10) NULL,
21 20 data text,
22 21 disabled bit default 0,
23 22 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 - roles varchar(255) NULL DEFAULT NULL,
28 - visibility_start datetime NULL DEFAULT NULL,
29 - visibility_end datetime NULL DEFAULT NULL,
30 - render_count bigint(20) unsigned NOT NULL DEFAULT 0,
31 - target_pages text NULL DEFAULT NULL,
32 23 UNIQUE KEY id (id)
33 24 ) $charset_collate;";
34 25
35 26 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
@@ -52,16 +43,8 @@
52 43 id mediumint(9) NOT NULL AUTO_INCREMENT,
53 44 slug varchar(100) NOT NULL,
54 45 data text,
55 46 site_id int default 0,
56 - header bit default 0,
57 - footer bit default 0,
58 - device_type varchar(50) NULL DEFAULT '[\"desktop\",\"mobile\",\"tablet\"]',
59 - roles varchar(255) NULL DEFAULT NULL,
60 - visibility_start datetime NULL DEFAULT NULL,
61 - visibility_end datetime NULL DEFAULT NULL,
62 - render_count bigint(20) unsigned NOT NULL DEFAULT 0,
63 - target_pages text NULL DEFAULT NULL,
64 47 UNIQUE KEY id (id)
65 48 ) $charset_collate;";
66 49
67 50 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
@@ -69,34 +52,8 @@
69 52 dbDelta( $sql );
70 53 }
71 54
72 55 /**
73 - * Build database table for shortcode content revisions
74 - */
75 -function sh_cd_create_database_table_revisions() {
76 -
77 - global $wpdb;
78 -
79 - $table_name = $wpdb->prefix . SH_CD_TABLE_REVISIONS;
80 -
81 - $charset_collate = $wpdb->get_charset_collate();
82 -
83 - $sql = "CREATE TABLE $table_name (
84 - id mediumint(9) NOT NULL AUTO_INCREMENT,
85 - shortcode_id mediumint(9) NOT NULL,
86 - data text,
87 - created_by bigint(20) unsigned NOT NULL DEFAULT 0,
88 - created_at datetime NOT NULL,
89 - UNIQUE KEY id (id),
90 - KEY shortcode_id (shortcode_id)
91 - ) $charset_collate;";
92 -
93 - require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
94 -
95 - dbDelta( $sql );
96 -}
97 -
98 -/**
99 56 * Fetch all Shortcodes
100 57 *
101 58 * @return bool
102 59 */
@@ -141,16 +98,11 @@
141 98 function sh_cd_db_shortcodes_by_id( $id ) {
142 99
143 100 global $wpdb;
144 101
145 - $sql = $wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id);
146 - $shortcode = $wpdb->get_row( $sql, ARRAY_A );
102 + $sql = $wpdb->prepare('SELECT id, slug, previous_slug, data, disabled, multisite FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id);
147 103
148 - if ( false === empty( $shortcode ) ) {
149 - $shortcode = sh_cd_db_filter_loaded_shortcode( $shortcode );
150 - }
151 -
152 - return $shortcode;
104 + return $wpdb->get_row( $sql, ARRAY_A );
153 105 }
154 106
155 107 /**
156 108 * Fetch the content of the shortcode by slug
@@ -166,44 +118,28 @@
166 118 $sqls = [];
167 119
168 120 // If multi site functionality is enabled, look there first for the shortcode!
169 121 if ( true === sh_cd_is_multisite_enabled() ) {
170 -
171 - $sql = $wpdb->prepare('SELECT * FROM ' . $wpdb->base_prefix . SH_CD_TABLE_MULTISITE . ' where slug = %s', $slug);
172 122
173 - $shortcode = $wpdb->get_row( $sql, ARRAY_A );
123 + $sqls[] = $wpdb->prepare('SELECT data FROM ' . $wpdb->base_prefix . SH_CD_TABLE_MULTISITE . ' where slug = %s', $slug);
124 + }
174 125
175 - if ( false === empty( $shortcode[ 'data' ] ) ) {
176 - $shortcode[ 'data' ] = stripslashes( $shortcode[ 'data' ] );
177 - $shortcode[ 'source' ] = 'multisite';
178 - return sh_cd_db_filter_loaded_shortcode( $shortcode );
179 - }
180 - }
126 + $sqls[] = $wpdb->prepare('SELECT data FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s and disabled <> 1', $slug);
181 127
182 - // No multisite slug by this name, so check the main table
183 - $sql = $wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s and disabled <> 1', $slug);
184 - $shortcode = $wpdb->get_row( $sql, ARRAY_A );
128 + foreach ( $sqls as $sql ) {
185 129
186 - if ( false === empty( $shortcode[ 'data' ] ) ) {
187 - $shortcode[ 'data' ] = stripslashes( $shortcode[ 'data' ] );
188 - $shortcode[ 'source' ] = 'local';
189 - return sh_cd_db_filter_loaded_shortcode( $shortcode );
130 + $row = $wpdb->get_var( $sql );
131 +
132 + if ( false === empty( $row ) ) {
133 +
134 + return stripslashes( $row );
135 + }
190 136 }
191 -
137 +
192 138 return NULL;
193 139 }
194 140
195 141 /**
196 - * Allow processing of loaded shortcode data. Called both for single by-id/by-slug fetches and,
197 - * from sh_cd_export_csv(), in a bulk per-row loop over the entire collection - registered
198 - * callbacks must be pure/side-effect-free (no queries, no global state) so they stay safe to
199 - * run hundreds of times in one request.
200 - */
201 -function sh_cd_db_filter_loaded_shortcode( $shortcode ) {
202 - return apply_filters( 'sh-cd-db-loaded-shortcode', $shortcode );
203 -}
204 -
205 -/**
206 142 * Get a Shortcode's slug from the slug ID
207 143 *
208 144 * @param $id
209 145 *
@@ -232,25 +168,22 @@
232 168 }
233 169
234 170 $multi_site_enabled = sh_cd_is_multisite_enabled();
235 171
236 - $default_values = apply_filters( 'sh-cd-db-default-values', [ 'id' => NULL,
237 - 'slug' => NULL,
238 - 'previous_slug' => NULL,
239 - 'editor' => NULL,
240 - 'data' => NULL,
241 - 'disabled' => 0,
242 - 'multisite' => 0
172 + $shortcode = wp_parse_args( $shortcode, [ 'id' => NULL,
173 + 'slug' => NULL,
174 + 'previous_slug' => NULL,
175 + 'data' => NULL,
176 + 'disabled' => 0,
177 + 'multisite' => 0
243 178 ]);
244 179
245 - $shortcode = wp_parse_args( $shortcode, $default_values );
246 -
247 180 // We need either a slug or an ID
248 181 if ( true === empty( $shortcode['slug'] ) && true === empty( $shortcode['id'] ) ) {
249 182 return false;
250 183 }
251 184
252 - $shortcode['disabled'] = (int) $shortcode['disabled'];
185 + $shortcode['disabled'] = (int) $shortcode['disabled'];
253 186 $shortcode['multisite'] = ( true === $multi_site_enabled ) ? (int) $shortcode['multisite'] : 0;
254 187
255 188 global $wpdb;
256 189
@@ -258,14 +191,10 @@
258 191
259 192 // Updating an existing shortcode?
260 193 if ( false === empty( $shortcode['id'] ) && true === is_numeric( $shortcode['id'] ) ){
261 194
262 - $shortcode_before_update = sh_cd_db_shortcodes_by_id( $shortcode['id'] );
263 -
264 195 $shortcode['slug'] = sh_cd_slug_generate( $shortcode['slug'], $shortcode['id'] );
265 196
266 - $shortcode = apply_filters( 'sh-cd-db-default-shortcode-before-save', $shortcode );
267 -
268 197 $formats = sh_cd_db_get_formats( $shortcode );
269 198
270 199 $result = $wpdb->update(
271 200 $wpdb->prefix . SH_CD_TABLE,
@@ -274,17 +203,8 @@
274 203 $formats,
275 204 [ '%d' ]
276 205 );
277 206
278 - if ( false !== $result ) {
279 -
280 - do_action( 'sh-cd-shortcode-updated', $shortcode );
281 -
282 - if ( false === empty( $shortcode_before_update ) ) {
283 - do_action( 'sh-cd-shortcode-before-update', $shortcode_before_update, $shortcode );
284 - }
285 - }
286 -
287 207 sh_cd_cache_delete_by_slug_or_key( $shortcode['id'] );
288 208 sh_cd_cache_delete_by_slug_or_key( $shortcode['previous_slug'] );
289 209
290 210 // Adding a new shortcode
@@ -294,10 +214,8 @@
294 214
295 215 // Ensure slug is santised and unique
296 216 $shortcode['slug'] = sh_cd_slug_generate( $shortcode['slug'] );
297 217
298 - $shortcode = apply_filters( 'sh-cd-db-default-shortcode-before-save', $shortcode );
299 -
300 218 $formats = sh_cd_db_get_formats( $shortcode );
301 219
302 220 $result = $wpdb->insert(
303 221 $wpdb->prefix . SH_CD_TABLE,
@@ -304,12 +222,8 @@
304 222 $shortcode,
305 223 $formats
306 224 );
307 225
308 - if ( false !== $result ) {
309 - do_action( 'sh-cd-shortcode-added', $shortcode );
310 - }
311 -
312 226 // It's an insert, so there should be no cache... however, just a wee sanity check in case
313 227 // a shortcode with the same slug previously exists.
314 228 sh_cd_cache_delete_by_slug_or_key( $shortcode['slug'] );
315 229 }
@@ -316,9 +230,9 @@
316 230
317 231 if ( false !== $result ) {
318 232
319 233 if ( 1 === $shortcode['multisite'] ) {
320 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
234 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
321 235 } else {
322 236 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
323 237 }
324 238
@@ -343,37 +257,35 @@
343 257 * @param $shortcode
344 258 *
345 259 * @return bool
346 260 */
347 -function sh_cd_db_shortcodes_multisite_insert( $shortcode ) {
261 +function sh_cd_db_shortcodes_multisite_insert( $slug, $data ) {
348 262
349 263 if ( false === is_admin() ) {
350 264 return false;
351 265 }
352 266
353 - sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
267 + sh_cd_db_shortcodes_multisite_delete( $slug );
354 268
355 269 global $wpdb;
356 270
357 - $shortcode_multisite = [ 'slug' => $shortcode['slug'],
358 - 'data' => $shortcode['data'],
359 - 'header' => $shortcode['header'],
360 - 'footer' => $shortcode['footer'],
361 - 'device_type' => $shortcode['device_type'],
362 - 'site_id' => get_current_blog_id()
271 + $shortcode = [
272 + 'slug' => $slug,
273 + 'data' => $data,
274 + 'site_id' => get_current_blog_id()
363 275 ];
364 276
365 - $formats = sh_cd_db_get_formats( $shortcode_multisite );
277 + $formats = sh_cd_db_get_formats( $shortcode );
366 278
367 279 $result = $wpdb->insert(
368 280 $wpdb->base_prefix . SH_CD_TABLE_MULTISITE,
369 - $shortcode_multisite,
281 + $shortcode,
370 282 $formats
371 283 );
372 284
373 - sh_cd_cache_delete( $shortcode['slug'] );
285 + sh_cd_cache_delete( $slug );
374 286
375 - do_action( 'sh_cd_multisite_changed', $shortcode );
287 + do_action( 'sh_cd_multisite_changed', $slug );
376 288
377 289 return ( false !== $result );
378 290 }
379 291
@@ -465,9 +377,9 @@
465 377
466 378 if ( false === empty( $shortcode ) ) {
467 379
468 380 if ( 1 === (int) $multisite ) {
469 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
381 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
470 382 } else {
471 383 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
472 384 }
473 385
@@ -490,10 +402,8 @@
490 402 if ( false === is_admin() ) {
491 403 return false;
492 404 }
493 405
494 - $shortcode_before_update = sh_cd_db_shortcodes_by_id( $id );
495 -
496 406 global $wpdb;
497 407
498 408 $result = $wpdb->update(
499 409 $wpdb->prefix . SH_CD_TABLE,
@@ -507,19 +417,14 @@
507 417
508 418 if ( false === empty( $shortcode ) ) {
509 419
510 420 if ( 1 === (int) $shortcode[ 'multisite' ] ) {
511 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
421 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
512 422 } else {
513 423 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
514 424 }
515 425
516 426 }
517 -
518 - if ( false !== $result && false === empty( $shortcode_before_update ) ) {
519 - do_action( 'sh-cd-shortcode-before-update', $shortcode_before_update, $shortcode );
520 - }
521 -
522 427 sh_cd_cache_delete_by_slug_or_key( $id );
523 428
524 429 return ( false !== $result );
525 430 }
@@ -539,25 +444,13 @@
539 444 }
540 445
541 446 global $wpdb;
542 447
543 - // Clear multi site
544 - $shortcode_before_delete = sh_cd_db_shortcodes_by_id( $id );
545 - sh_cd_db_shortcodes_multisite_delete( $shortcode_before_delete['slug'] );
546 -
547 - // Clear revisions
548 - sh_cd_db_shortcode_revisions_delete_by_shortcode_id( $id );
549 -
550 448 // Clear cached version
551 449 sh_cd_cache_delete_by_slug_or_key( $id );
552 450
553 - $slug = sh_cd_db_shortcodes_get_slug_by_id( $id );
554 451 $result = $wpdb->delete( $wpdb->prefix . SH_CD_TABLE, [ 'id' => $id ], [ '%d' ] );
555 452
556 - if ( false !== $result ) {
557 - do_action( 'sh-cd-shortcode-updated', $id, $slug );
558 - }
559 -
560 453 return ( false !== $result );
561 454 }
562 455
563 456 /**
@@ -569,24 +462,15 @@
569 462 */
570 463 function sh_cd_db_get_formats( $data ) {
571 464
572 465 $lookup = [
573 - 'id' => '%d',
574 - 'slug' => '%s',
575 - 'previous_slug' => '%s',
576 - 'data' => '%s',
577 - 'disabled' => '%d',
578 - 'multisite' => '%d',
579 - 'site_id' => '%d',
580 - 'editor' => '%s',
581 - 'header' => '%d',
582 - 'footer' => '%d',
583 - 'device_type' => '%s',
584 - 'roles' => '%s',
585 - 'visibility_start' => '%s',
586 - 'visibility_end' => '%s',
587 - 'render_count' => '%d',
588 - 'target_pages' => '%s',
466 + 'id' => '%d',
467 + 'slug' => '%s',
468 + 'previous_slug' => '%s',
469 + 'data' => '%s',
470 + 'disabled' => '%d',
471 + 'multisite' => '%d',
472 + 'site_id' => '%d'
589 473 ];
590 474
591 475 $formats = [];
592 476
@@ -591,12 +475,11 @@
591 475 $formats = [];
592 476
593 477 foreach ( $data as $key => $value ) {
594 478
595 - // $wpdb->update()/insert() match $formats to $data positionally, not by key - so every
596 - // key must produce an entry here (even an unrecognised one, defaulted to '%s') or every
597 - // column from that point on silently shifts onto the wrong format specifier.
598 - $formats[] = $lookup[ $key ] ?? '%s';
479 + if ( false === empty( $lookup[ $key ] ) ) {
480 + $formats[] = $lookup[ $key ];
481 + }
599 482 }
600 483
601 484 return $formats;
602 485 }
@@ -625,106 +508,5 @@
625 508
626 509 $row = $wpdb->get_var( $sql );
627 510
628 511 return ( empty( $row ) );
629 -}
630 -
631 -/**
632 - * Save a shortcode content revision, pruning older revisions beyond the retention limit
633 - *
634 - * @param $shortcode_id
635 - * @param $data
636 - * @param $user_id
637 - *
638 - * @return bool
639 - */
640 -function sh_cd_db_shortcode_revisions_save( $shortcode_id, $data, $user_id = NULL ) {
641 -
642 - global $wpdb;
643 -
644 - $user_id = ( true === empty( $user_id ) ) ? get_current_user_id() : $user_id;
645 -
646 - $result = $wpdb->insert(
647 - $wpdb->prefix . SH_CD_TABLE_REVISIONS,
648 - [
649 - 'shortcode_id' => $shortcode_id,
650 - 'data' => $data,
651 - 'created_by' => $user_id,
652 - 'created_at' => current_time( 'mysql' ),
653 - ],
654 - [ '%d', '%s', '%d', '%s' ]
655 - );
656 -
657 - sh_cd_db_shortcode_revisions_prune( $shortcode_id );
658 -
659 - return ( false !== $result );
660 -}
661 -
662 -/**
663 - * Keep only the most recent $keep revisions for a given shortcode
664 - *
665 - * @param $shortcode_id
666 - * @param $keep
667 - */
668 -function sh_cd_db_shortcode_revisions_prune( $shortcode_id, $keep = 20 ) {
669 -
670 - global $wpdb;
671 -
672 - $table = $wpdb->prefix . SH_CD_TABLE_REVISIONS;
673 -
674 - $sql = $wpdb->prepare(
675 - "DELETE FROM {$table} WHERE shortcode_id = %d AND id NOT IN (
676 - SELECT id FROM ( SELECT id FROM {$table} WHERE shortcode_id = %d ORDER BY created_at DESC, id DESC LIMIT %d ) AS keep_revisions
677 - )",
678 - $shortcode_id, $shortcode_id, $keep
679 - );
680 -
681 - $wpdb->query( $sql );
682 -}
683 -
684 -/**
685 - * Fetch all revisions for a shortcode, most recent first
686 - *
687 - * @param $shortcode_id
688 - *
689 - * @return array
690 - */
691 -function sh_cd_db_shortcode_revisions_get( $shortcode_id ) {
692 -
693 - global $wpdb;
694 -
695 - $sql = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE_REVISIONS . ' WHERE shortcode_id = %d ORDER BY created_at DESC, id DESC', $shortcode_id );
696 -
697 - return $wpdb->get_results( $sql, ARRAY_A );
698 -}
699 -
700 -/**
701 - * Fetch a single revision by its ID
702 - *
703 - * @param $revision_id
704 - *
705 - * @return array|null
706 - */
707 -function sh_cd_db_shortcode_revisions_get_by_id( $revision_id ) {
708 -
709 - global $wpdb;
710 -
711 - $sql = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE_REVISIONS . ' WHERE id = %d', $revision_id );
712 -
713 - return $wpdb->get_row( $sql, ARRAY_A );
714 -}
715 -
716 -/**
717 - * Delete all revisions for a given shortcode (e.g. when the shortcode itself is deleted)
718 - *
719 - * @param $shortcode_id
720 - *
721 - * @return bool
722 - */
723 -function sh_cd_db_shortcode_revisions_delete_by_shortcode_id( $shortcode_id ) {
724 -
725 - global $wpdb;
726 -
727 - $result = $wpdb->delete( $wpdb->prefix . SH_CD_TABLE_REVISIONS, [ 'shortcode_id' => $shortcode_id ], [ '%d' ] );
728 -
729 - return ( false !== $result );
730 512 }