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