PluginProbe
Snippet Shortcodes / 5.0a
Snippet Shortcodes v5.0a
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 +45 -247 5.2.15.0a View file →
@@ -20,16 +20,8 @@
20 20 editor varchar(10) NULL,
21 21 data text,
22 22 disabled bit default 0,
23 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 - 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 24 UNIQUE KEY id (id)
33 25 ) $charset_collate;";
34 26
35 27 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
@@ -52,16 +44,8 @@
52 44 id mediumint(9) NOT NULL AUTO_INCREMENT,
53 45 slug varchar(100) NOT NULL,
54 46 data text,
55 47 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 48 UNIQUE KEY id (id)
65 49 ) $charset_collate;";
66 50
67 51 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
@@ -69,34 +53,8 @@
69 53 dbDelta( $sql );
70 54 }
71 55
72 56 /**
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 57 * Fetch all Shortcodes
100 58 *
101 59 * @return bool
102 60 */
@@ -141,16 +99,11 @@
141 99 function sh_cd_db_shortcodes_by_id( $id ) {
142 100
143 101 global $wpdb;
144 102
145 - $sql = $wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id);
146 - $shortcode = $wpdb->get_row( $sql, ARRAY_A );
103 + $sql = $wpdb->prepare('SELECT id, slug, previous_slug, data, disabled, multisite, editor FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id);
147 104
148 - if ( false === empty( $shortcode ) ) {
149 - $shortcode = sh_cd_db_filter_loaded_shortcode( $shortcode );
150 - }
151 -
152 - return $shortcode;
105 + return $wpdb->get_row( $sql, ARRAY_A );
153 106 }
154 107
155 108 /**
156 109 * Fetch the content of the shortcode by slug
@@ -166,44 +119,28 @@
166 119 $sqls = [];
167 120
168 121 // If multi site functionality is enabled, look there first for the shortcode!
169 122 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 123
173 - $shortcode = $wpdb->get_row( $sql, ARRAY_A );
124 + $sqls[] = $wpdb->prepare('SELECT data FROM ' . $wpdb->base_prefix . SH_CD_TABLE_MULTISITE . ' where slug = %s', $slug);
125 + }
174 126
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 - }
127 + $sqls[] = $wpdb->prepare('SELECT data FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s and disabled <> 1', $slug);
181 128
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 );
129 + foreach ( $sqls as $sql ) {
185 130
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 );
131 + $row = $wpdb->get_var( $sql );
132 +
133 + if ( false === empty( $row ) ) {
134 +
135 + return stripslashes( $row );
136 + }
190 137 }
191 -
138 +
192 139 return NULL;
193 140 }
194 141
195 142 /**
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 143 * Get a Shortcode's slug from the slug ID
207 144 *
208 145 * @param $id
209 146 *
@@ -232,25 +169,23 @@
232 169 }
233 170
234 171 $multi_site_enabled = sh_cd_is_multisite_enabled();
235 172
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
173 + $shortcode = wp_parse_args( $shortcode, [ 'id' => NULL,
174 + 'slug' => NULL,
175 + 'previous_slug' => NULL,
176 + 'editor' => NULL,
177 + 'data' => NULL,
178 + 'disabled' => 0,
179 + 'multisite' => 0
243 180 ]);
244 181
245 - $shortcode = wp_parse_args( $shortcode, $default_values );
246 -
247 182 // We need either a slug or an ID
248 183 if ( true === empty( $shortcode['slug'] ) && true === empty( $shortcode['id'] ) ) {
249 184 return false;
250 185 }
251 186
252 - $shortcode['disabled'] = (int) $shortcode['disabled'];
187 + $shortcode['disabled'] = (int) $shortcode['disabled'];
253 188 $shortcode['multisite'] = ( true === $multi_site_enabled ) ? (int) $shortcode['multisite'] : 0;
254 189
255 190 global $wpdb;
256 191
@@ -258,14 +193,10 @@
258 193
259 194 // Updating an existing shortcode?
260 195 if ( false === empty( $shortcode['id'] ) && true === is_numeric( $shortcode['id'] ) ){
261 196
262 - $shortcode_before_update = sh_cd_db_shortcodes_by_id( $shortcode['id'] );
263 -
264 197 $shortcode['slug'] = sh_cd_slug_generate( $shortcode['slug'], $shortcode['id'] );
265 198
266 - $shortcode = apply_filters( 'sh-cd-db-default-shortcode-before-save', $shortcode );
267 -
268 199 $formats = sh_cd_db_get_formats( $shortcode );
269 200
270 201 $result = $wpdb->update(
271 202 $wpdb->prefix . SH_CD_TABLE,
@@ -275,14 +206,9 @@
275 206 [ '%d' ]
276 207 );
277 208
278 209 if ( false !== $result ) {
279 -
280 210 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 211 }
286 212
287 213 sh_cd_cache_delete_by_slug_or_key( $shortcode['id'] );
288 214 sh_cd_cache_delete_by_slug_or_key( $shortcode['previous_slug'] );
@@ -294,10 +220,8 @@
294 220
295 221 // Ensure slug is santised and unique
296 222 $shortcode['slug'] = sh_cd_slug_generate( $shortcode['slug'] );
297 223
298 - $shortcode = apply_filters( 'sh-cd-db-default-shortcode-before-save', $shortcode );
299 -
300 224 $formats = sh_cd_db_get_formats( $shortcode );
301 225
302 226 $result = $wpdb->insert(
303 227 $wpdb->prefix . SH_CD_TABLE,
@@ -316,9 +240,9 @@
316 240
317 241 if ( false !== $result ) {
318 242
319 243 if ( 1 === $shortcode['multisite'] ) {
320 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
244 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
321 245 } else {
322 246 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
323 247 }
324 248
@@ -343,37 +267,35 @@
343 267 * @param $shortcode
344 268 *
345 269 * @return bool
346 270 */
347 -function sh_cd_db_shortcodes_multisite_insert( $shortcode ) {
271 +function sh_cd_db_shortcodes_multisite_insert( $slug, $data ) {
348 272
349 273 if ( false === is_admin() ) {
350 274 return false;
351 275 }
352 276
353 - sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
277 + sh_cd_db_shortcodes_multisite_delete( $slug );
354 278
355 279 global $wpdb;
356 280
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()
281 + $shortcode = [
282 + 'slug' => $slug,
283 + 'data' => $data,
284 + 'site_id' => get_current_blog_id()
363 285 ];
364 286
365 - $formats = sh_cd_db_get_formats( $shortcode_multisite );
287 + $formats = sh_cd_db_get_formats( $shortcode );
366 288
367 289 $result = $wpdb->insert(
368 290 $wpdb->base_prefix . SH_CD_TABLE_MULTISITE,
369 - $shortcode_multisite,
291 + $shortcode,
370 292 $formats
371 293 );
372 294
373 - sh_cd_cache_delete( $shortcode['slug'] );
295 + sh_cd_cache_delete( $slug );
374 296
375 - do_action( 'sh_cd_multisite_changed', $shortcode );
297 + do_action( 'sh_cd_multisite_changed', $slug );
376 298
377 299 return ( false !== $result );
378 300 }
379 301
@@ -465,9 +387,9 @@
465 387
466 388 if ( false === empty( $shortcode ) ) {
467 389
468 390 if ( 1 === (int) $multisite ) {
469 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
391 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
470 392 } else {
471 393 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
472 394 }
473 395
@@ -490,10 +412,8 @@
490 412 if ( false === is_admin() ) {
491 413 return false;
492 414 }
493 415
494 - $shortcode_before_update = sh_cd_db_shortcodes_by_id( $id );
495 -
496 416 global $wpdb;
497 417
498 418 $result = $wpdb->update(
499 419 $wpdb->prefix . SH_CD_TABLE,
@@ -507,19 +427,14 @@
507 427
508 428 if ( false === empty( $shortcode ) ) {
509 429
510 430 if ( 1 === (int) $shortcode[ 'multisite' ] ) {
511 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
431 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
512 432 } else {
513 433 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
514 434 }
515 435
516 436 }
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 437 sh_cd_cache_delete_by_slug_or_key( $id );
523 438
524 439 return ( false !== $result );
525 440 }
@@ -539,15 +454,8 @@
539 454 }
540 455
541 456 global $wpdb;
542 457
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 458 // Clear cached version
551 459 sh_cd_cache_delete_by_slug_or_key( $id );
552 460
553 461 $slug = sh_cd_db_shortcodes_get_slug_by_id( $id );
@@ -569,24 +477,16 @@
569 477 */
570 478 function sh_cd_db_get_formats( $data ) {
571 479
572 480 $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',
481 + 'id' => '%d',
482 + 'slug' => '%s',
483 + 'previous_slug' => '%s',
484 + 'data' => '%s',
485 + 'disabled' => '%d',
486 + 'multisite' => '%d',
487 + 'site_id' => '%d',
488 + 'editor' => '%s'
589 489 ];
590 490
591 491 $formats = [];
592 492
@@ -591,12 +491,11 @@
591 491 $formats = [];
592 492
593 493 foreach ( $data as $key => $value ) {
594 494
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';
495 + if ( false === empty( $lookup[ $key ] ) ) {
496 + $formats[] = $lookup[ $key ];
497 + }
599 498 }
600 499
601 500 return $formats;
602 501 }
@@ -625,106 +524,5 @@
625 524
626 525 $row = $wpdb->get_var( $sql );
627 526
628 527 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 528 }