| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') or die('Jog on!'); |
| 4 |
|
| 5 |
function sh_cd_get_user_defined_arguments($default, $specified) { |
| 6 |
|
| 7 |
$user_defined = array(); |
| 8 |
|
| 9 |
if(is_array($default) && !empty($default) && is_array($specified) && !empty($specified)) { |
| 10 |
|
| 11 |
foreach ($specified as $key => $value) { |
| 12 |
if(!isset($default[$key])) { |
| 13 |
$user_defined[$key] = $value; |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
} |
| 18 |
|
| 19 |
return (is_array($user_defined) && !empty($user_defined)) ? $user_defined : false; |
| 20 |
} |
| 21 |
|
| 22 |
function sh_cd_apply_user_defined_parameters($shortcode, $user_defined_parameters){ |
| 23 |
|
| 24 |
if(!empty($shortcode) && is_array($user_defined_parameters) && !empty($user_defined_parameters)) { |
| 25 |
|
| 26 |
foreach ($user_defined_parameters as $key => $value) { |
| 27 |
$shortcode = str_replace('%%' . $key . '%%', $value, $shortcode); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
return $shortcode; |
| 32 |
} |
| 33 |
|
| 34 |
function sh_cd_get_all_shortcodes() |
| 35 |
{ |
| 36 |
global $wpdb; |
| 37 |
|
| 38 |
$sql = 'SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE . ' order by slug asc'; |
| 39 |
|
| 40 |
$rows = $wpdb->get_results( $sql ); |
| 41 |
|
| 42 |
if (!is_null($rows)) |
| 43 |
return $rows; |
| 44 |
|
| 45 |
return false; |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
function sh_cd_get_shortcode($id) |
| 50 |
{ |
| 51 |
if (!is_admin()) |
| 52 |
return false; |
| 53 |
|
| 54 |
global $wpdb; |
| 55 |
|
| 56 |
$sql = $wpdb->prepare('SELECT slug, data, disabled FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id); |
| 57 |
|
| 58 |
$row = $wpdb->get_row( $sql ); |
| 59 |
|
| 60 |
if (!is_null($row)) |
| 61 |
return $row; |
| 62 |
|
| 63 |
return false; |
| 64 |
|
| 65 |
} |
| 66 |
function sh_cd_get_shortcode_by_slug($slug) |
| 67 |
{ |
| 68 |
global $wpdb; |
| 69 |
|
| 70 |
$sql = $wpdb->prepare('SELECT data FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s and disabled <> 1', $slug); |
| 71 |
|
| 72 |
$row = $wpdb->get_var( $sql ); |
| 73 |
|
| 74 |
if (!is_null($row)) |
| 75 |
return stripslashes($row); |
| 76 |
|
| 77 |
return false; |
| 78 |
|
| 79 |
} |
| 80 |
function sh_cd_get_slug_by_id($id) |
| 81 |
{ |
| 82 |
global $wpdb; |
| 83 |
|
| 84 |
$sql = $wpdb->prepare('SELECT slug FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id); |
| 85 |
|
| 86 |
$row = $wpdb->get_var( $sql ); |
| 87 |
|
| 88 |
if (!is_null($row)) |
| 89 |
return $row; |
| 90 |
|
| 91 |
return false; |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
function sh_cd_save_shortcode($slug, $data, $disabled = false, $id = false) |
| 96 |
{ |
| 97 |
if (!is_admin()) |
| 98 |
return false; |
| 99 |
|
| 100 |
if (false == $id && empty($slug)) |
| 101 |
return false; |
| 102 |
|
| 103 |
global $wpdb; |
| 104 |
|
| 105 |
$slug = sh_cd_get_slug($slug); |
| 106 |
|
| 107 |
$result = false; |
| 108 |
|
| 109 |
$disabled = (true === $disabled) ? 1: 0; |
| 110 |
|
| 111 |
if ($id) |
| 112 |
{ |
| 113 |
//Update data (Slug can never be updated!) |
| 114 |
$result = $wpdb->update( |
| 115 |
$wpdb->prefix . SH_CD_TABLE, |
| 116 |
array( |
| 117 |
'data' => $data, |
| 118 |
'disabled' => $disabled |
| 119 |
), |
| 120 |
array( 'id' => $id), |
| 121 |
array( |
| 122 |
'%s', |
| 123 |
'%d' |
| 124 |
), |
| 125 |
array( '%d' ) |
| 126 |
); |
| 127 |
|
| 128 |
sh_cd_delete_cache(sh_cd_get_slug_by_id($id)); |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
$result = $wpdb->insert( |
| 133 |
$wpdb->prefix . SH_CD_TABLE, |
| 134 |
array( |
| 135 |
'slug' => $slug, |
| 136 |
'data' => $data, |
| 137 |
'disabled' => $disabled |
| 138 |
), |
| 139 |
array( |
| 140 |
'%s', |
| 141 |
'%s', |
| 142 |
'%d' |
| 143 |
) |
| 144 |
); |
| 145 |
|
| 146 |
sh_cd_delete_cache($slug); |
| 147 |
} |
| 148 |
|
| 149 |
if (false === $result) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
else |
| 153 |
{ |
| 154 |
return true; |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
function sh_cd_delete_shortcode($id) |
| 160 |
{ |
| 161 |
if (!is_admin() || !is_numeric($id)) |
| 162 |
return false; |
| 163 |
|
| 164 |
global $wpdb; |
| 165 |
|
| 166 |
// Clear cached version |
| 167 |
sh_cd_delete_cache(sh_cd_get_slug_by_id($id)); |
| 168 |
|
| 169 |
if (false === $wpdb->delete( $wpdb->prefix . SH_CD_TABLE, array( 'id' => $id ) )) { |
| 170 |
return false; |
| 171 |
} |
| 172 |
else { |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
function sh_cd_get_slug($text) |
| 179 |
{ |
| 180 |
if(!empty($text)) |
| 181 |
{ |
| 182 |
$text = sanitize_title($text); |
| 183 |
|
| 184 |
$original_slug = $text; |
| 185 |
|
| 186 |
$try = 1; |
| 187 |
|
| 188 |
// If slug exists, then fetch a unique one! |
| 189 |
// v1.1: and ensure slug isn't a preset |
| 190 |
while (!sh_cd_is_slug_unique($text)) |
| 191 |
{ |
| 192 |
$text = $original_slug . '_' . $try; |
| 193 |
|
| 194 |
$try++; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
return $text; |
| 199 |
} |
| 200 |
|
| 201 |
function sh_cd_is_slug_unique($slug) |
| 202 |
{ |
| 203 |
if (!is_admin() || empty($slug)) |
| 204 |
return false; |
| 205 |
|
| 206 |
// 1.1 Ensure slug is not a prefix |
| 207 |
if (sh_cd_is_shortcode_preset($slug)) |
| 208 |
return false; |
| 209 |
|
| 210 |
global $wpdb; |
| 211 |
|
| 212 |
$sql = $wpdb->prepare('SELECT count(slug) FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s', $slug); |
| 213 |
|
| 214 |
$row = $wpdb->get_var( $sql ); |
| 215 |
|
| 216 |
if(0 == $row) |
| 217 |
{ |
| 218 |
return true; |
| 219 |
} |
| 220 |
else |
| 221 |
{ |
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
function sh_cd_create_database_table() |
| 229 |
{ |
| 230 |
global $wpdb; |
| 231 |
|
| 232 |
$table_name = $wpdb->prefix . SH_CD_TABLE; |
| 233 |
|
| 234 |
$charset_collate = $wpdb->get_charset_collate(); |
| 235 |
|
| 236 |
$sql = "CREATE TABLE $table_name ( |
| 237 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 238 |
slug varchar(100) NOT NULL, |
| 239 |
data text, |
| 240 |
disabled bit default 0, |
| 241 |
UNIQUE KEY id (id) |
| 242 |
) $charset_collate;"; |
| 243 |
|
| 244 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 245 |
dbDelta( $sql ); |
| 246 |
|
| 247 |
update_option('sh-cd-version', SH_CD_PLUGIN_VERSION); |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
function sh_cd_display_message($text, $error = false) |
| 252 |
{ |
| 253 |
if (!empty($text)) |
| 254 |
{ |
| 255 |
$class = (($error) ? 'error' : 'updated'); |
| 256 |
|
| 257 |
echo '<div class="' . $class . '"> |
| 258 |
<p>' . $text . '</p> |
| 259 |
</div>'; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
function sh_cd_create_dialog_jquery_code($title, $message, $class_used_to_prompt_confirmation, $js_call = false) |
| 264 |
{ |
| 265 |
global $wp_scripts; |
| 266 |
$queryui = $wp_scripts->query('jquery-ui-core'); |
| 267 |
$url = "//ajax.googleapis.com/ajax/libs/jqueryui/".$queryui->ver."/themes/smoothness/jquery-ui.css"; |
| 268 |
wp_enqueue_script( 'jquery-ui-dialog' ); |
| 269 |
wp_enqueue_style('jquery-ui-smoothness', $url, false, null); |
| 270 |
|
| 271 |
$id_hash = md5($title . $message . $class_used_to_prompt_confirmation); |
| 272 |
|
| 273 |
?> |
| 274 |
<div id='<?php echo $id_hash; ?>' title='<?php echo $title; ?>'> |
| 275 |
<p><?php echo $message; ?></p> |
| 276 |
</div> |
| 277 |
<script> |
| 278 |
jQuery(function($) { |
| 279 |
|
| 280 |
var $info = $('#<?php echo $id_hash; ?>'); |
| 281 |
|
| 282 |
$info.dialog({ |
| 283 |
'dialogClass' : 'wp-dialog', |
| 284 |
'modal' : true, |
| 285 |
'autoOpen' : false |
| 286 |
}); |
| 287 |
|
| 288 |
$('.<?php echo $class_used_to_prompt_confirmation; ?>').click(function(event) { |
| 289 |
|
| 290 |
event.preventDefault(); |
| 291 |
|
| 292 |
target_url = $(this).attr('href'); |
| 293 |
|
| 294 |
var $info = $('#<?php echo $id_hash; ?>'); |
| 295 |
|
| 296 |
$info.dialog({ |
| 297 |
'dialogClass' : 'wp-dialog', |
| 298 |
'modal' : true, |
| 299 |
'autoOpen' : false, |
| 300 |
'closeOnEscape' : true, |
| 301 |
'buttons' : { |
| 302 |
'Yes': function() { |
| 303 |
|
| 304 |
<?php if ($js_call != false): ?> |
| 305 |
<?php echo $js_call; ?> |
| 306 |
|
| 307 |
$(this).dialog('close'); |
| 308 |
<?php else: ?> |
| 309 |
window.location.href = target_url; |
| 310 |
<?php endif; ?> |
| 311 |
}, |
| 312 |
'No': function() { |
| 313 |
$(this).dialog('close'); |
| 314 |
} |
| 315 |
} |
| 316 |
}); |
| 317 |
|
| 318 |
$info.dialog('open'); |
| 319 |
}); |
| 320 |
|
| 321 |
}); |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
</script> |
| 327 |
|
| 328 |
<?php |
| 329 |
} |
| 330 |
|
| 331 |
function sh_cd_get_cache($key) |
| 332 |
{ |
| 333 |
$key = sh_cd_generate_cache_key($key); |
| 334 |
|
| 335 |
return get_transient($key); |
| 336 |
} |
| 337 |
function sh_cd_set_cache($key, $data) |
| 338 |
{ |
| 339 |
$key = sh_cd_generate_cache_key($key); |
| 340 |
|
| 341 |
set_transient($key, $data, SH_CD_CACHING_TIME); |
| 342 |
} |
| 343 |
function sh_cd_delete_cache($key) |
| 344 |
{ |
| 345 |
$key = sh_cd_generate_cache_key($key); |
| 346 |
|
| 347 |
return delete_transient($key); |
| 348 |
} |
| 349 |
function sh_cd_generate_cache_key($key) |
| 350 |
{ |
| 351 |
return SH_CD_SHORTCODE . $key; |
| 352 |
} |
| 353 |
|