PluginProbe
Snippet Shortcodes / 3.4.1
Snippet Shortcodes v3.4.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
← All changes | includes/db.php +45 -268 trunk3.4.1 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 *
@@ -224,9 +160,9 @@
224 160 * @param $shortcode
225 161 *
226 162 * @return bool
227 163 */
228 -function sh_cd_db_shortcodes_save( $shortcode, $return_shortcode = false ) {
164 +function sh_cd_db_shortcodes_save( $shortcode ) {
229 165
230 166 if ( false === is_admin() ) {
231 167 return false;
232 168 }
@@ -232,25 +168,23 @@
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, [
173 + 'id' => NULL,
174 + 'slug' => NULL,
175 + 'previous_slug' => NULL,
176 + 'data' => NULL,
177 + 'disabled' => 0,
178 + 'multisite' => 0
243 179 ]);
244 180
245 - $shortcode = wp_parse_args( $shortcode, $default_values );
246 -
247 181 // We need either a slug or an ID
248 182 if ( true === empty( $shortcode['slug'] ) && true === empty( $shortcode['id'] ) ) {
249 183 return false;
250 184 }
251 185
252 - $shortcode['disabled'] = (int) $shortcode['disabled'];
186 + $shortcode['disabled'] = (int) $shortcode['disabled'];
253 187 $shortcode['multisite'] = ( true === $multi_site_enabled ) ? (int) $shortcode['multisite'] : 0;
254 188
255 189 global $wpdb;
256 190
@@ -258,14 +192,10 @@
258 192
259 193 // Updating an existing shortcode?
260 194 if ( false === empty( $shortcode['id'] ) && true === is_numeric( $shortcode['id'] ) ){
261 195
262 - $shortcode_before_update = sh_cd_db_shortcodes_by_id( $shortcode['id'] );
263 -
264 196 $shortcode['slug'] = sh_cd_slug_generate( $shortcode['slug'], $shortcode['id'] );
265 197
266 - $shortcode = apply_filters( 'sh-cd-db-default-shortcode-before-save', $shortcode );
267 -
268 198 $formats = sh_cd_db_get_formats( $shortcode );
269 199
270 200 $result = $wpdb->update(
271 201 $wpdb->prefix . SH_CD_TABLE,
@@ -274,17 +204,8 @@
274 204 $formats,
275 205 [ '%d' ]
276 206 );
277 207
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 208 sh_cd_cache_delete_by_slug_or_key( $shortcode['id'] );
288 209 sh_cd_cache_delete_by_slug_or_key( $shortcode['previous_slug'] );
289 210
290 211 // Adding a new shortcode
@@ -294,10 +215,8 @@
294 215
295 216 // Ensure slug is santised and unique
296 217 $shortcode['slug'] = sh_cd_slug_generate( $shortcode['slug'] );
297 218
298 - $shortcode = apply_filters( 'sh-cd-db-default-shortcode-before-save', $shortcode );
299 -
300 219 $formats = sh_cd_db_get_formats( $shortcode );
301 220
302 221 $result = $wpdb->insert(
303 222 $wpdb->prefix . SH_CD_TABLE,
@@ -304,12 +223,8 @@
304 223 $shortcode,
305 224 $formats
306 225 );
307 226
308 - if ( false !== $result ) {
309 - do_action( 'sh-cd-shortcode-added', $shortcode );
310 - }
311 -
312 227 // It's an insert, so there should be no cache... however, just a wee sanity check in case
313 228 // a shortcode with the same slug previously exists.
314 229 sh_cd_cache_delete_by_slug_or_key( $shortcode['slug'] );
315 230 }
@@ -316,9 +231,9 @@
316 231
317 232 if ( false !== $result ) {
318 233
319 234 if ( 1 === $shortcode['multisite'] ) {
320 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
235 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
321 236 } else {
322 237 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
323 238 }
324 239
@@ -323,14 +238,8 @@
323 238 }
324 239
325 240 sh_cd_cache_delete( $shortcode['slug'] );
326 241
327 - if ( true === $return_shortcode ) {
328 -
329 - $shortcode['id'] = $result;
330 - return $shortcode;
331 - }
332 -
333 242 return true;
334 243 }
335 244
336 245 return false;
@@ -343,37 +252,35 @@
343 252 * @param $shortcode
344 253 *
345 254 * @return bool
346 255 */
347 -function sh_cd_db_shortcodes_multisite_insert( $shortcode ) {
256 +function sh_cd_db_shortcodes_multisite_insert( $slug, $data ) {
348 257
349 258 if ( false === is_admin() ) {
350 259 return false;
351 260 }
352 261
353 - sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
262 + sh_cd_db_shortcodes_multisite_delete( $slug );
354 263
355 264 global $wpdb;
356 265
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()
266 + $shortcode = [
267 + 'slug' => $slug,
268 + 'data' => $data,
269 + 'site_id' => get_current_blog_id()
363 270 ];
364 271
365 - $formats = sh_cd_db_get_formats( $shortcode_multisite );
272 + $formats = sh_cd_db_get_formats( $shortcode );
366 273
367 274 $result = $wpdb->insert(
368 275 $wpdb->base_prefix . SH_CD_TABLE_MULTISITE,
369 - $shortcode_multisite,
276 + $shortcode,
370 277 $formats
371 278 );
372 279
373 - sh_cd_cache_delete( $shortcode['slug'] );
280 + sh_cd_cache_delete( $slug );
374 281
375 - do_action( 'sh_cd_multisite_changed', $shortcode );
282 + do_action( 'sh_cd_multisite_changed', $slug );
376 283
377 284 return ( false !== $result );
378 285 }
379 286
@@ -465,9 +372,9 @@
465 372
466 373 if ( false === empty( $shortcode ) ) {
467 374
468 375 if ( 1 === (int) $multisite ) {
469 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
376 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
470 377 } else {
471 378 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
472 379 }
473 380
@@ -490,10 +397,8 @@
490 397 if ( false === is_admin() ) {
491 398 return false;
492 399 }
493 400
494 - $shortcode_before_update = sh_cd_db_shortcodes_by_id( $id );
495 -
496 401 global $wpdb;
497 402
498 403 $result = $wpdb->update(
499 404 $wpdb->prefix . SH_CD_TABLE,
@@ -507,19 +412,14 @@
507 412
508 413 if ( false === empty( $shortcode ) ) {
509 414
510 415 if ( 1 === (int) $shortcode[ 'multisite' ] ) {
511 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
416 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
512 417 } else {
513 418 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
514 419 }
515 420
516 421 }
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 422 sh_cd_cache_delete_by_slug_or_key( $id );
523 423
524 424 return ( false !== $result );
525 425 }
@@ -539,25 +439,13 @@
539 439 }
540 440
541 441 global $wpdb;
542 442
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 443 // Clear cached version
551 444 sh_cd_cache_delete_by_slug_or_key( $id );
552 445
553 - $slug = sh_cd_db_shortcodes_get_slug_by_id( $id );
554 446 $result = $wpdb->delete( $wpdb->prefix . SH_CD_TABLE, [ 'id' => $id ], [ '%d' ] );
555 447
556 - if ( false !== $result ) {
557 - do_action( 'sh-cd-shortcode-updated', $id, $slug );
558 - }
559 -
560 448 return ( false !== $result );
561 449 }
562 450
563 451 /**
@@ -569,24 +457,15 @@
569 457 */
570 458 function sh_cd_db_get_formats( $data ) {
571 459
572 460 $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',
461 + 'id' => '%d',
462 + 'slug' => '%s',
463 + 'previous_slug' => '%s',
464 + 'data' => '%s',
465 + 'disabled' => '%d',
466 + 'multisite' => '%d',
467 + 'site_id' => '%d'
589 468 ];
590 469
591 470 $formats = [];
592 471
@@ -591,12 +470,11 @@
591 470 $formats = [];
592 471
593 472 foreach ( $data as $key => $value ) {
594 473
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';
474 + if ( false === empty( $lookup[ $key ] ) ) {
475 + $formats[] = $lookup[ $key ];
476 + }
599 477 }
600 478
601 479 return $formats;
602 480 }
@@ -625,106 +503,5 @@
625 503
626 504 $row = $wpdb->get_var( $sql );
627 505
628 506 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 507 }