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