PluginProbe
Snippet Shortcodes / 4.0
Snippet Shortcodes v4.0
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 +39 -94 5.1.54.0 View file →
@@ -16,15 +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 23 UNIQUE KEY id (id)
28 24 ) $charset_collate;";
29 25
30 26 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
@@ -47,11 +43,8 @@
47 43 id mediumint(9) NOT NULL AUTO_INCREMENT,
48 44 slug varchar(100) NOT NULL,
49 45 data text,
50 46 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 47 UNIQUE KEY id (id)
55 48 ) $charset_collate;";
56 49
57 50 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
@@ -105,16 +98,11 @@
105 98 function sh_cd_db_shortcodes_by_id( $id ) {
106 99
107 100 global $wpdb;
108 101
109 - $sql = $wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id);
110 - $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);
111 103
112 - if ( false === empty( $shortcode ) ) {
113 - $shortcode = sh_cd_db_filter_loaded_shortcode( $shortcode );
114 - }
115 -
116 - return $shortcode;
104 + return $wpdb->get_row( $sql, ARRAY_A );
117 105 }
118 106
119 107 /**
120 108 * Fetch the content of the shortcode by slug
@@ -130,41 +118,28 @@
130 118 $sqls = [];
131 119
132 120 // If multi site functionality is enabled, look there first for the shortcode!
133 121 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 122
137 - $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 + }
138 125
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 - }
126 + $sqls[] = $wpdb->prepare('SELECT data FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s and disabled <> 1', $slug);
145 127
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 );
128 + foreach ( $sqls as $sql ) {
149 129
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 );
130 + $row = $wpdb->get_var( $sql );
131 +
132 + if ( false === empty( $row ) ) {
133 +
134 + return stripslashes( $row );
135 + }
154 136 }
155 -
137 +
156 138 return NULL;
157 139 }
158 140
159 141 /**
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 142 * Get a Shortcode's slug from the slug ID
168 143 *
169 144 * @param $id
170 145 *
@@ -193,25 +168,22 @@
193 168 }
194 169
195 170 $multi_site_enabled = sh_cd_is_multisite_enabled();
196 171
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
172 + $shortcode = wp_parse_args( $shortcode, [ 'id' => NULL,
173 + 'slug' => NULL,
174 + 'previous_slug' => NULL,
175 + 'data' => NULL,
176 + 'disabled' => 0,
177 + 'multisite' => 0
204 178 ]);
205 179
206 - $shortcode = wp_parse_args( $shortcode, $default_values );
207 -
208 180 // We need either a slug or an ID
209 181 if ( true === empty( $shortcode['slug'] ) && true === empty( $shortcode['id'] ) ) {
210 182 return false;
211 183 }
212 184
213 - $shortcode['disabled'] = (int) $shortcode['disabled'];
185 + $shortcode['disabled'] = (int) $shortcode['disabled'];
214 186 $shortcode['multisite'] = ( true === $multi_site_enabled ) ? (int) $shortcode['multisite'] : 0;
215 187
216 188 global $wpdb;
217 189
@@ -221,10 +193,8 @@
221 193 if ( false === empty( $shortcode['id'] ) && true === is_numeric( $shortcode['id'] ) ){
222 194
223 195 $shortcode['slug'] = sh_cd_slug_generate( $shortcode['slug'], $shortcode['id'] );
224 196
225 - $shortcode = apply_filters( 'sh-cd-db-default-shortcode-before-save', $shortcode );
226 -
227 197 $formats = sh_cd_db_get_formats( $shortcode );
228 198
229 199 $result = $wpdb->update(
230 200 $wpdb->prefix . SH_CD_TABLE,
@@ -233,12 +203,8 @@
233 203 $formats,
234 204 [ '%d' ]
235 205 );
236 206
237 - if ( false !== $result ) {
238 - do_action( 'sh-cd-shortcode-updated', $shortcode );
239 - }
240 -
241 207 sh_cd_cache_delete_by_slug_or_key( $shortcode['id'] );
242 208 sh_cd_cache_delete_by_slug_or_key( $shortcode['previous_slug'] );
243 209
244 210 // Adding a new shortcode
@@ -248,10 +214,8 @@
248 214
249 215 // Ensure slug is santised and unique
250 216 $shortcode['slug'] = sh_cd_slug_generate( $shortcode['slug'] );
251 217
252 - $shortcode = apply_filters( 'sh-cd-db-default-shortcode-before-save', $shortcode );
253 -
254 218 $formats = sh_cd_db_get_formats( $shortcode );
255 219
256 220 $result = $wpdb->insert(
257 221 $wpdb->prefix . SH_CD_TABLE,
@@ -258,12 +222,8 @@
258 222 $shortcode,
259 223 $formats
260 224 );
261 225
262 - if ( false !== $result ) {
263 - do_action( 'sh-cd-shortcode-added', $shortcode );
264 - }
265 -
266 226 // It's an insert, so there should be no cache... however, just a wee sanity check in case
267 227 // a shortcode with the same slug previously exists.
268 228 sh_cd_cache_delete_by_slug_or_key( $shortcode['slug'] );
269 229 }
@@ -270,9 +230,9 @@
270 230
271 231 if ( false !== $result ) {
272 232
273 233 if ( 1 === $shortcode['multisite'] ) {
274 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
234 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
275 235 } else {
276 236 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
277 237 }
278 238
@@ -297,37 +257,35 @@
297 257 * @param $shortcode
298 258 *
299 259 * @return bool
300 260 */
301 -function sh_cd_db_shortcodes_multisite_insert( $shortcode ) {
261 +function sh_cd_db_shortcodes_multisite_insert( $slug, $data ) {
302 262
303 263 if ( false === is_admin() ) {
304 264 return false;
305 265 }
306 266
307 - sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
267 + sh_cd_db_shortcodes_multisite_delete( $slug );
308 268
309 269 global $wpdb;
310 270
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()
271 + $shortcode = [
272 + 'slug' => $slug,
273 + 'data' => $data,
274 + 'site_id' => get_current_blog_id()
317 275 ];
318 276
319 - $formats = sh_cd_db_get_formats( $shortcode_multisite );
277 + $formats = sh_cd_db_get_formats( $shortcode );
320 278
321 279 $result = $wpdb->insert(
322 280 $wpdb->base_prefix . SH_CD_TABLE_MULTISITE,
323 - $shortcode_multisite,
281 + $shortcode,
324 282 $formats
325 283 );
326 284
327 - sh_cd_cache_delete( $shortcode['slug'] );
285 + sh_cd_cache_delete( $slug );
328 286
329 - do_action( 'sh_cd_multisite_changed', $shortcode );
287 + do_action( 'sh_cd_multisite_changed', $slug );
330 288
331 289 return ( false !== $result );
332 290 }
333 291
@@ -419,9 +377,9 @@
419 377
420 378 if ( false === empty( $shortcode ) ) {
421 379
422 380 if ( 1 === (int) $multisite ) {
423 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
381 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
424 382 } else {
425 383 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
426 384 }
427 385
@@ -459,9 +417,9 @@
459 417
460 418 if ( false === empty( $shortcode ) ) {
461 419
462 420 if ( 1 === (int) $shortcode[ 'multisite' ] ) {
463 - sh_cd_db_shortcodes_multisite_insert( $shortcode );
421 + sh_cd_db_shortcodes_multisite_insert( $shortcode['slug'], $shortcode['data'] );
464 422 } else {
465 423 sh_cd_db_shortcodes_multisite_delete( $shortcode['slug'] );
466 424 }
467 425
@@ -486,22 +444,13 @@
486 444 }
487 445
488 446 global $wpdb;
489 447
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 448 // Clear cached version
495 449 sh_cd_cache_delete_by_slug_or_key( $id );
496 450
497 - $slug = sh_cd_db_shortcodes_get_slug_by_id( $id );
498 451 $result = $wpdb->delete( $wpdb->prefix . SH_CD_TABLE, [ 'id' => $id ], [ '%d' ] );
499 452
500 - if ( false !== $result ) {
501 - do_action( 'sh-cd-shortcode-updated', $id, $slug );
502 - }
503 -
504 453 return ( false !== $result );
505 454 }
506 455
507 456 /**
@@ -513,19 +462,15 @@
513 462 */
514 463 function sh_cd_db_get_formats( $data ) {
515 464
516 465 $lookup = [
517 - 'id' => '%d',
518 - 'slug' => '%s',
466 + 'id' => '%d',
467 + 'slug' => '%s',
519 468 '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'
469 + 'data' => '%s',
470 + 'disabled' => '%d',
471 + 'multisite' => '%d',
472 + 'site_id' => '%d'
528 473 ];
529 474
530 475 $formats = [];
531 476