| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') or die('Jog on!'); |
| 4 |
|
| 5 |
function sh_cd_get_all_shortcodes() |
| 6 |
{ |
| 7 |
global $wpdb; |
| 8 |
|
| 9 |
$sql = 'SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE . ' order by slug asc'; |
| 10 |
|
| 11 |
$rows = $wpdb->get_results( $sql ); |
| 12 |
|
| 13 |
if (!is_null($rows)) |
| 14 |
return $rows; |
| 15 |
|
| 16 |
return false; |
| 17 |
|
| 18 |
} |
| 19 |
|
| 20 |
function sh_cd_get_shortcode($id) |
| 21 |
{ |
| 22 |
if (!is_admin()) |
| 23 |
return false; |
| 24 |
|
| 25 |
global $wpdb; |
| 26 |
|
| 27 |
$sql = $wpdb->prepare('SELECT slug, data FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id); |
| 28 |
|
| 29 |
$row = $wpdb->get_row( $sql ); |
| 30 |
|
| 31 |
if (!is_null($row)) |
| 32 |
return $row; |
| 33 |
|
| 34 |
return false; |
| 35 |
|
| 36 |
} |
| 37 |
function sh_cd_get_shortcode_by_slug($slug) |
| 38 |
{ |
| 39 |
global $wpdb; |
| 40 |
|
| 41 |
$sql = $wpdb->prepare('SELECT data FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s', $slug); |
| 42 |
|
| 43 |
$row = $wpdb->get_var( $sql ); |
| 44 |
|
| 45 |
if (!is_null($row)) |
| 46 |
return stripslashes($row); |
| 47 |
|
| 48 |
return false; |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
function sh_cd_save_shortcode($slug, $data, $id = false) |
| 53 |
{ |
| 54 |
if (!is_admin()) |
| 55 |
return false; |
| 56 |
|
| 57 |
if (false == $id && empty($slug)) |
| 58 |
return false; |
| 59 |
|
| 60 |
global $wpdb; |
| 61 |
|
| 62 |
$slug = sh_cd_get_slug($slug); |
| 63 |
|
| 64 |
$result = false; |
| 65 |
|
| 66 |
if ($id) |
| 67 |
{ |
| 68 |
//Update data (Slug can never be updated!) |
| 69 |
$result = $wpdb->update( |
| 70 |
$wpdb->prefix . SH_CD_TABLE, |
| 71 |
array( |
| 72 |
'data' => $data |
| 73 |
), |
| 74 |
array( 'id' => $id), |
| 75 |
array( |
| 76 |
'%s' |
| 77 |
), |
| 78 |
array( '%d' ) |
| 79 |
); |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
$result = $wpdb->insert( |
| 84 |
$wpdb->prefix . SH_CD_TABLE, |
| 85 |
array( |
| 86 |
'slug' => $slug, |
| 87 |
'data' => $data |
| 88 |
), |
| 89 |
array( |
| 90 |
'%s', |
| 91 |
'%s' |
| 92 |
) |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
if (false === $result) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
function sh_cd_delete_shortcode($id) |
| 107 |
{ |
| 108 |
if (!is_admin() || !is_numeric($id)) |
| 109 |
return false; |
| 110 |
|
| 111 |
global $wpdb; |
| 112 |
|
| 113 |
if (false === $wpdb->delete( $wpdb->prefix . SH_CD_TABLE, array( 'id' => $id ) )) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
else { |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
function sh_cd_get_slug($text) |
| 123 |
{ |
| 124 |
if(!empty($text)) |
| 125 |
{ |
| 126 |
$text = sanitize_title($text); |
| 127 |
|
| 128 |
$original_slug = $text; |
| 129 |
|
| 130 |
$try = 1; |
| 131 |
|
| 132 |
// If slug exists, then fetch a unique one! |
| 133 |
while (!sh_cd_is_slug_unique($text)) |
| 134 |
{ |
| 135 |
$text = $original_slug . '_' . $try; |
| 136 |
|
| 137 |
$try++; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return $text; |
| 142 |
} |
| 143 |
|
| 144 |
function sh_cd_is_slug_unique($slug) |
| 145 |
{ |
| 146 |
if (!is_admin() || empty($slug)) |
| 147 |
return false; |
| 148 |
|
| 149 |
global $wpdb; |
| 150 |
|
| 151 |
$sql = $wpdb->prepare('SELECT count(slug) FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s', $slug); |
| 152 |
|
| 153 |
$row = $wpdb->get_var( $sql ); |
| 154 |
|
| 155 |
if(0 == $row) |
| 156 |
{ |
| 157 |
return true; |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
function sh_cd_create_database_table() |
| 168 |
{ |
| 169 |
global $wpdb; |
| 170 |
|
| 171 |
$table_name = $wpdb->prefix . SH_CD_TABLE; |
| 172 |
|
| 173 |
$charset_collate = $wpdb->get_charset_collate(); |
| 174 |
|
| 175 |
$sql = "CREATE TABLE $table_name ( |
| 176 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 177 |
slug varchar(100) NOT NULL, |
| 178 |
data text, |
| 179 |
UNIQUE KEY id (id) |
| 180 |
) $charset_collate;"; |
| 181 |
|
| 182 |
$wpdb->query($sql); |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
function sh_cd_display_message($text, $error = false) |
| 187 |
{ |
| 188 |
if (!empty($text)) |
| 189 |
{ |
| 190 |
$class = (($error) ? 'error' : 'updated'); |
| 191 |
|
| 192 |
echo '<div class="' . $class . '"> |
| 193 |
<p>' . $text . '</p> |
| 194 |
</div>'; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
function sh_cd_create_dialog_jquery_code($title, $message, $class_used_to_prompt_confirmation, $js_call = false) |
| 199 |
{ |
| 200 |
global $wp_scripts; |
| 201 |
$queryui = $wp_scripts->query('jquery-ui-core'); |
| 202 |
$url = "//ajax.googleapis.com/ajax/libs/jqueryui/".$queryui->ver."/themes/smoothness/jquery-ui.css"; |
| 203 |
wp_enqueue_script( 'jquery-ui-dialog' ); |
| 204 |
wp_enqueue_style('jquery-ui-smoothness', $url, false, null); |
| 205 |
|
| 206 |
$id_hash = md5($title . $message . $class_used_to_prompt_confirmation); |
| 207 |
|
| 208 |
?> |
| 209 |
<div id='<?php echo $id_hash; ?>' title='<?php echo $title; ?>'> |
| 210 |
<p><?php echo $message; ?></p> |
| 211 |
</div> |
| 212 |
<script> |
| 213 |
jQuery(function($) { |
| 214 |
|
| 215 |
var $info = $('#<?php echo $id_hash; ?>'); |
| 216 |
|
| 217 |
$info.dialog({ |
| 218 |
'dialogClass' : 'wp-dialog', |
| 219 |
'modal' : true, |
| 220 |
'autoOpen' : false |
| 221 |
}); |
| 222 |
|
| 223 |
$('.<?php echo $class_used_to_prompt_confirmation; ?>').click(function(event) { |
| 224 |
|
| 225 |
event.preventDefault(); |
| 226 |
|
| 227 |
target_url = $(this).attr('href'); |
| 228 |
|
| 229 |
var $info = $('#<?php echo $id_hash; ?>'); |
| 230 |
|
| 231 |
$info.dialog({ |
| 232 |
'dialogClass' : 'wp-dialog', |
| 233 |
'modal' : true, |
| 234 |
'autoOpen' : false, |
| 235 |
'closeOnEscape' : true, |
| 236 |
'buttons' : { |
| 237 |
'Yes': function() { |
| 238 |
|
| 239 |
<?php if ($js_call != false): ?> |
| 240 |
<?php echo $js_call; ?> |
| 241 |
|
| 242 |
$(this).dialog('close'); |
| 243 |
<?php else: ?> |
| 244 |
window.location.href = target_url; |
| 245 |
<?php endif; ?> |
| 246 |
}, |
| 247 |
'No': function() { |
| 248 |
$(this).dialog('close'); |
| 249 |
} |
| 250 |
} |
| 251 |
}); |
| 252 |
|
| 253 |
$info.dialog('open'); |
| 254 |
}); |
| 255 |
|
| 256 |
}); |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
</script> |
| 262 |
|
| 263 |
<?php |
| 264 |
} |