PluginProbe
Snippet Shortcodes / 3.3.3
Snippet Shortcodes v3.3.3
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 -280 trunk3.3.3 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 */
@@ -119,20 +76,8 @@
119 76 return $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where disabled = 0 order by slug asc', ARRAY_A );
120 77 }
121 78
122 79 /**
123 - * Fetch a count of all shortcodes
124 - *
125 - * @return bool
126 - */
127 -function sh_cd_db_shortcodes_count() {
128 -
129 - global $wpdb;
130 -
131 - return $wpdb->get_var( 'SELECT count(id) FROM ' . $wpdb->prefix . SH_CD_TABLE );
132 -}
133 -
134 -/**
135 80 * Fetch a shortcode by ID (mainly used for quick lookups in admin)
136 81 *
137 82 * @param $id
138 83 *
@@ -141,16 +86,11 @@
141 86 function sh_cd_db_shortcodes_by_id( $id ) {
142 87
143 88 global $wpdb;
144 89
145 - $sql = $wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id);
146 - $shortcode = $wpdb->get_row( $sql, ARRAY_A );
90 + $sql = $wpdb->prepare('SELECT id, slug, previous_slug, data, disabled, multisite FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id);
147 91
148 - if ( false === empty( $shortcode ) ) {
149 - $shortcode = sh_cd_db_filter_loaded_shortcode( $shortcode );
150 - }
151 -
152 - return $shortcode;
92 + return $wpdb->get_row( $sql, ARRAY_A );
153 93 }
154 94
155 95 /**
156 96 * Fetch the content of the shortcode by slug
@@ -166,44 +106,28 @@
166 106 $sqls = [];
167 107
168 108 // If multi site functionality is enabled, look there first for the shortcode!
169 109 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 110
173 - $shortcode = $wpdb->get_row( $sql, ARRAY_A );
111 + $sqls[] = $wpdb->prepare('SELECT data FROM ' . $wpdb->base_prefix . SH_CD_TABLE_MULTISITE . ' where slug = %s', $slug);
112 + }
174 113
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 - }
114 + $sqls[] = $wpdb->prepare('SELECT data FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s and disabled <> 1', $slug);
181 115
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 );
116 + foreach ( $sqls as $sql ) {
185 117
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 );
118 + $row = $wpdb->get_var( $sql );
119 +
120 + if ( false === empty( $row ) ) {
121 +
122 + return stripslashes( $row );
123 + }
190 124 }
191 -
125 +
192 126 return NULL;
193 127 }
194 128
195 129 /**
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 130 * Get a Shortcode's slug from the slug ID
207 131 *
208 132 * @param $id
209 133 *
@@ -224,9 +148,9 @@
224 148 * @param $shortcode
225 149 *
226 150 * @return bool
227 151 */
228 -function sh_cd_db_shortcodes_save( $shortcode, $return_shortcode = false ) {
152 +function sh_cd_db_shortcodes_save( $shortcode ) {
229 153
230 154 if ( false === is_admin() ) {
231 155 return false;
232 156 }
@@ -232,25 +156,23 @@
232 156 }
233 157
234 158 $multi_site_enabled = sh_cd_is_multisite_enabled();
235 159
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
160 + $shortcode = wp_parse_args( $shortcode, [
161 + 'id' => NULL,
162 + 'slug' => NULL,
163 + 'previous_slug' => NULL,
164 + 'data' => NULL,
165 + 'disabled' => 0,
166 + 'multisite' => 0
243 167 ]);
244 168
245 - $shortcode = wp_parse_args( $shortcode, $default_values );
246 -
247 169 // We need either a slug or an ID
248 170 if ( true === empty( $shortcode['slug'] ) && true === empty( $shortcode['id'] ) ) {
249 171 return false;
250 172 }
251 173
252 - $shortcode['disabled'] = (int) $shortcode['disabled'];
174 + $shortcode['disabled'] = (int) $shortcode['disabled'];
253 175 $shortcode['multisite'] = ( true === $multi_site_enabled ) ? (int) $shortcode['multisite'] : 0;
254 176
255 177 global $wpdb;
256 178
@@ -258,14 +180,10 @@
258 180
259 181 // Updating an existing shortcode?
260 182 if ( false === empty( $shortcode['id'] ) && true === is_numeric( $shortcode['id'] ) ){
261 183
262 - $shortcode_before_update = sh_cd_db_shortcodes_by_id( $shortcode['id'] );
263 -
264 184 $shortcode['slug'] = sh_cd_slug_generate( $shortcode['slug'], $shortcode['id'] );
265 185
266 - $shortcode = apply_filters( 'sh-cd-db-default-shortcode-before-save', $shortcode );
267 -
268 186 $formats = sh_cd_db_get_formats( $shortcode );
269 187
270 188 $result = $wpdb->update(
271 189 $wpdb->prefix . SH_CD_TABLE,
@@ -274,17 +192,8 @@
274 192 $formats,
275 193 [ '%d' ]
276 194 );
277 195
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 196 sh_cd_cache_delete_by_slug_or_key( $shortcode['id'] );
288 197 sh_cd_cache_delete_by_slug_or_key( $shortcode['previous_slug'] );
289 198
290 199 // Adding a new shortcode
@@ -294,10 +203,8 @@
294 203
295 204 // Ensure slug is santised and unique
296 205 $shortcode['slug'] = sh_cd_slug_generate( $shortcode['slug'] );
297 206
298 - $shortcode = apply_filters( 'sh-cd-db-default-shortcode-before-save', $shortcode );
299 -
300 207 $formats = sh_cd_db_get_formats( $shortcode );
301 208
302 209 $result = $wpdb->insert(
303 210 $wpdb->prefix . SH_CD_TABLE,
@@ -304,12 +211,8 @@
304 211 $shortcode,
305 212 $formats
306 213 );
307 214
308 - if ( false !== $result ) {
309 - do_action( 'sh-cd-shortcode-added', $shortcode );
310 - }
311 -
312 215 // It's an insert, so there should be no cache... however, just a wee sanity check in case
313 216 // a shortcode with the same slug previously exists.
314 217 sh_cd_cache_delete_by_slug_or_key( $shortcode['slug'] );
315 218 }
@@ -316,9 +219,9 @@
316 219
317 220 if ( false !== $result ) {
318 221
319 222 if ( 1 === $shortcode['multisite'] ) {
320 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
223 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
321 224 } else {
322 225 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
323 226 }
324 227
@@ -323,14 +226,8 @@
323 226 }
324 227
325 228 sh_cd_cache_delete( $shortcode['slug'] );
326 229
327 - if ( true === $return_shortcode ) {
328 -
329 - $shortcode['id'] = $result;
330 - return $shortcode;
331 - }
332 -
333 230 return true;
334 231 }
335 232
336 233 return false;
@@ -343,37 +240,35 @@
343 240 * @param $shortcode
344 241 *
345 242 * @return bool
346 243 */
347 -function sh_cd_db_shortcodes_multisite_insert( $shortcode ) {
244 +function sh_cd_db_shortcodes_multisite_insert( $slug, $data ) {
348 245
349 246 if ( false === is_admin() ) {
350 247 return false;
351 248 }
352 249
353 - sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
250 + sh_cd_db_shortcodes_multisite_delete( $slug );
354 251
355 252 global $wpdb;
356 253
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()
254 + $shortcode = [
255 + 'slug' => $slug,
256 + 'data' => $data,
257 + 'site_id' => get_current_blog_id()
363 258 ];
364 259
365 - $formats = sh_cd_db_get_formats( $shortcode_multisite );
260 + $formats = sh_cd_db_get_formats( $shortcode );
366 261
367 262 $result = $wpdb->insert(
368 263 $wpdb->base_prefix . SH_CD_TABLE_MULTISITE,
369 - $shortcode_multisite,
264 + $shortcode,
370 265 $formats
371 266 );
372 267
373 - sh_cd_cache_delete( $shortcode['slug'] );
268 + sh_cd_cache_delete( $slug );
374 269
375 - do_action( 'sh_cd_multisite_changed', $shortcode );
270 + do_action( 'sh_cd_multisite_changed', $slug );
376 271
377 272 return ( false !== $result );
378 273 }
379 274
@@ -465,9 +360,9 @@
465 360
466 361 if ( false === empty( $shortcode ) ) {
467 362
468 363 if ( 1 === (int) $multisite ) {
469 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
364 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
470 365 } else {
471 366 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
472 367 }
473 368
@@ -490,10 +385,8 @@
490 385 if ( false === is_admin() ) {
491 386 return false;
492 387 }
493 388
494 - $shortcode_before_update = sh_cd_db_shortcodes_by_id( $id );
495 -
496 389 global $wpdb;
497 390
498 391 $result = $wpdb->update(
499 392 $wpdb->prefix . SH_CD_TABLE,
@@ -507,19 +400,14 @@
507 400
508 401 if ( false === empty( $shortcode ) ) {
509 402
510 403 if ( 1 === (int) $shortcode[ 'multisite' ] ) {
511 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
404 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
512 405 } else {
513 406 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
514 407 }
515 408
516 409 }
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 410 sh_cd_cache_delete_by_slug_or_key( $id );
523 411
524 412 return ( false !== $result );
525 413 }
@@ -539,25 +427,13 @@
539 427 }
540 428
541 429 global $wpdb;
542 430
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 431 // Clear cached version
551 432 sh_cd_cache_delete_by_slug_or_key( $id );
552 433
553 - $slug = sh_cd_db_shortcodes_get_slug_by_id( $id );
554 434 $result = $wpdb->delete( $wpdb->prefix . SH_CD_TABLE, [ 'id' => $id ], [ '%d' ] );
555 435
556 - if ( false !== $result ) {
557 - do_action( 'sh-cd-shortcode-updated', $id, $slug );
558 - }
559 -
560 436 return ( false !== $result );
561 437 }
562 438
563 439 /**
@@ -569,24 +445,15 @@
569 445 */
570 446 function sh_cd_db_get_formats( $data ) {
571 447
572 448 $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',
449 + 'id' => '%d',
450 + 'slug' => '%s',
451 + 'previous_slug' => '%s',
452 + 'data' => '%s',
453 + 'disabled' => '%d',
454 + 'multisite' => '%d',
455 + 'site_id' => '%d'
589 456 ];
590 457
591 458 $formats = [];
592 459
@@ -591,12 +458,11 @@
591 458 $formats = [];
592 459
593 460 foreach ( $data as $key => $value ) {
594 461
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';
462 + if ( false === empty( $lookup[ $key ] ) ) {
463 + $formats[] = $lookup[ $key ];
464 + }
599 465 }
600 466
601 467 return $formats;
602 468 }
@@ -625,106 +491,5 @@
625 491
626 492 $row = $wpdb->get_var( $sql );
627 493
628 494 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 495 }