| @@ -15,8 +15,9 @@ | ||
| 15 | 15 | |
| 16 | 16 | $sql = "CREATE TABLE $table_name ( |
| 17 | 17 | id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 18 | 18 | slug varchar(100) NOT NULL, |
| 19 | + previous_slug varchar(100) NOT NULL, | |
| 19 | 20 | data text, |
| 20 | 21 | disabled bit default 0, |
| 21 | 22 | UNIQUE KEY id (id) |
| 22 | 23 | ) $charset_collate;"; |
| @@ -48,9 +49,9 @@ | ||
| 48 | 49 | function sh_cd_db_shortcodes_by_id( $id ) { |
| 49 | 50 | |
| 50 | 51 | global $wpdb; |
| 51 | 52 | |
| 52 | - $sql = $wpdb->prepare('SELECT id, slug, data, disabled FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id); | |
| 53 | + $sql = $wpdb->prepare('SELECT id, slug, previous_slug, data, disabled FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id); | |
| 53 | 54 | |
| 54 | 55 | return $wpdb->get_row( $sql, ARRAY_A ); |
| 55 | 56 | } |
| 56 | 57 | |
| @@ -103,8 +104,9 @@ | ||
| 103 | 104 | |
| 104 | 105 | $shortcode = wp_parse_args( $shortcode, [ |
| 105 | 106 | 'id' => NULL, |
| 106 | 107 | 'slug' => NULL, |
| 108 | + 'previous_slug' => NULL, | |
| 107 | 109 | 'data' => NULL, |
| 108 | 110 | 'disabled' => 0 |
| 109 | 111 | ]); |
| 110 | 112 | |
| @@ -121,10 +123,9 @@ | ||
| 121 | 123 | |
| 122 | 124 | // Updating an existing shortcode? |
| 123 | 125 | if ( false === empty( $shortcode['id'] ) && true === is_numeric( $shortcode['id'] ) ){ |
| 124 | 126 | |
| 125 | - // Once set, a slug cannot be updated. | |
| 126 | - unset( $shortcode['slug'] ); | |
| 127 | + $shortcode['slug'] = sh_cd_slug_generate( $shortcode['slug'], $shortcode['id'] ); | |
| 127 | 128 | |
| 128 | 129 | $formats = sh_cd_db_get_formats( $shortcode ); |
| 129 | 130 | |
| 130 | 131 | $result = $wpdb->update( |
| @@ -135,8 +136,9 @@ | ||
| 135 | 136 | [ '%d' ] |
| 136 | 137 | ); |
| 137 | 138 | |
| 138 | 139 | sh_cd_cache_delete_by_slug_or_key( $shortcode['id'] ); |
| 140 | + sh_cd_cache_delete_by_slug_or_key( $shortcode['previous_slug'] ); | |
| 139 | 141 | |
| 140 | 142 | // Adding a new shortcode |
| 141 | 143 | } else { |
| 142 | 144 | |
| @@ -252,8 +254,9 @@ | ||
| 252 | 254 | |
| 253 | 255 | $lookup = [ |
| 254 | 256 | 'id' => '%d', |
| 255 | 257 | 'slug' => '%s', |
| 258 | + 'previous_slug' => '%s', | |
| 256 | 259 | 'data' => '%s', |
| 257 | 260 | 'disabled' => '%d' |
| 258 | 261 | ]; |
| 259 | 262 | |
| @@ -272,12 +275,13 @@ | ||
| 272 | 275 | /** |
| 273 | 276 | * Check if the slug already exists |
| 274 | 277 | * |
| 275 | 278 | * @param $slug |
| 279 | + * @param $existing_id | |
| 276 | 280 | * |
| 277 | 281 | * @return bool |
| 278 | 282 | */ |
| 279 | -function sh_cd_slug_is_unique( $slug ) { | |
| 283 | +function sh_cd_slug_is_unique( $slug, $existing_id = NULL ) { | |
| 280 | 284 | |
| 281 | 285 | if ( true === empty( $slug ) ) { |
| 282 | 286 | return false; |
| 283 | 287 | } |
| @@ -284,8 +288,12 @@ | ||
| 284 | 288 | |
| 285 | 289 | global $wpdb; |
| 286 | 290 | |
| 287 | 291 | $sql = $wpdb->prepare( 'SELECT count(slug) FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s', $slug ); |
| 292 | + | |
| 293 | + if ( false === empty( $existing_id ) ) { | |
| 294 | + $sql .= $wpdb->prepare( ' and id <> %d', $existing_id ); | |
| 295 | + } | |
| 288 | 296 | |
| 289 | 297 | $row = $wpdb->get_var( $sql ); |
| 290 | 298 | |
| 291 | 299 | return ( empty( $row ) ); |