| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') or die("Jog on!"); |
| 4 |
|
| 5 |
/** |
| 6 |
* Return a list of slugs / titles for free presets |
| 7 |
* @return array |
| 8 |
*/ |
| 9 |
function sh_cd_shortcode_presets_premium_list() { |
| 10 |
|
| 11 |
return [ |
| 12 |
'sc-db-value-by-id' => [ 'class' => 'SC_DB_VALUE_BY_ID', 'description' => __( 'Return a column value from a MySQL database for the given ID of search criteria e.g. <a href="https://snippet-shortcodes.yeken.uk/shortcodes/sc-db-value-by-id.html" rel="noopener" target="_blank">[sv slug="sc-db-value-by-id" table="users" column="user_login" column-to-search="id" key="3" key-format="%d"]</a>', SH_CD_SLUG ), 'premium' => true, 'args' => [ '_sh_cd_func' => 'by-id' ] ], |
| 13 |
'sc-date' => [ 'class' => 'SC_DATE', 'description' => __( 'A shortcode that displays today\'s date with the ability to add or subtract days, months and years. To specify an interval to add or subtract onto the date use the parameter "interval" e.g. [sv slug="sc-date" interval="-1 year"], [sv slug="sc-date" interval="+5 days"], [sv slug="sc-date" interval="+3 months"]. Intervals are based upon PHP intervals and are outlined here <a href="https://www.php.net/manual/en/dateinterval.createfromdatestring.php" target="_blank">https://www.php.net/manual/en/dateinterval.createfromdatestring.php</a>. Default is UK format (DD/MM/YYYY). Format can be changed by adding the parameter format="m/d/Y" onto the shortcode. Format syntax is based upon PHP date: <a href="http://php.net/manual/en/function.date.php" target="_blank">http://php.net/manual/en/function.date.php</a>', SH_CD_SLUG ), 'premium' => true ], |
| 14 |
'sc-site-language' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'Language code for the current site', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'language' ], 'premium' => true ], |
| 15 |
'sc-site-description' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'Site tagline (set in Settings > General)', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'description' ], 'premium' => true ], |
| 16 |
'sc-site-wp-url' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'The WordPress address (URL) (set in Settings > General)', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'wpurl' ], 'premium' => true ], |
| 17 |
'sc-site-charset' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'The "Encoding for pages and feeds" (set in Settings > Reading)', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'charset' ], 'premium' => true ], |
| 18 |
'sc-site-wp-version' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'The current WordPress version', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'version' ], 'premium' => true ], |
| 19 |
'sc-site-html-type' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'The content-type (default: "text/html"). Themes and plugins', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'html_type' ], 'premium' => true ], |
| 20 |
'sc-site-stylesheet-url' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'URL to the stylesheet for the active theme.', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'stylesheet_url' ], 'premium' => true ], |
| 21 |
'sc-site-stylesheet_directory' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'Directory path for the active theme.', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'stylesheet_directory' ], 'premium' => true ], |
| 22 |
'sc-site-current-url' => [ 'class' => 'SC_CURRENT_URL', 'description' => __( 'Get the current URL.', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'current_url' ], 'premium' => true], |
| 23 |
'sc-site-register-url' => [ 'class' => 'SC_REGISTER_URL', 'description' => __( 'Get the URL to the WordPress registration page.', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'registration_url' ], 'premium' => true], |
| 24 |
'sc-site-template-url' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'The URL of the active theme\'s directory.', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'template_url' ], 'premium' => true], |
| 25 |
'sc-site-pingback-url' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'The pingback XML-RPC file URL (xmlrpc.php)', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'pingback_url' ], 'premium' => true ], |
| 26 |
'sc-site-atom-feed' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'The Atom feed URL (/feed/atom)', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'atom_url' ], 'premium' => true ], |
| 27 |
'sc-site-rdf-url' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'The RDF/RSS 1.0 feed URL (/feed/rfd)', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'rdf_url' ], 'premium' => true ], |
| 28 |
'sc-site-rss-url' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'The RSS 0.92 feed URL (/feed/rss)', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'rss_url' ], 'premium' => true ], |
| 29 |
'sc-site-rss2-url' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'The RSS 2.0 feed URL (/feed)', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'rss2_url' ], 'premium' => true ], |
| 30 |
'sc-site-comments-atom-url' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'The comments Atom feed URL (/comments/feed)', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'comments_atom_url' ], 'premium' => true ], |
| 31 |
'sc-site-comments-rss2-url' => [ 'class' => 'SC_BLOG_INFO', 'description' => __( 'The comments RSS 2.0 feed URL (/comments/feed)', SH_CD_SLUG ), 'args' => [ '_sh_cd_func' => 'comments_rss2_url' ], 'premium' => true ], |
| 32 |
'sc-php-server-info' => [ 'class' => 'SC_SERVER_INFO', 'description' => __( 'Display data from the PHP $_SERVER global e.g. [sv slug="sc-server-info" field="SERVER_SOFTWARE"]. <a href="http://php.net/manual/en/reserved.variables.server.php" rel="noopener" target="_blank">Allowed values for field attribute</a>.', SH_CD_SLUG ), 'premium' => true ], |
| 33 |
'sc-php-unique-id' => [ 'class' => 'SC_UNIQUE_ID', 'description' => __( 'Generate a unique ID. Based upon <a href="http://php.net/manual/en/function.uniqid.php" rel="noopener" target="_blank">uniqid()</a>. If you wish the unique ID to be prefixed, add a the prefix attribute e.g. [sv slug="sc-php-unique-id" prefix="yeken"]', SH_CD_SLUG ), 'premium' => true ], |
| 34 |
'sc-php-timestamp' => [ 'class' => 'SC_TIMESTAMP', 'description' => __( 'Display the current unix timestamp. Based upon <a href="http://php.net/manual/en/function.time.php" rel="noopener" target="_blank">time()</a>.', SH_CD_SLUG ), 'premium' => true ], |
| 35 |
'sc-php-random-number' => [ 'class' => 'SC_RAND_NUMBER', 'description' => __( 'Display a random number. Based upon <a href="http://php.net/manual/en/function.rand.php" rel="noopener" target="_blank">rand()</a>. It also supports the optional arguments of min and max e.g. [sv slug="sc-php-random-number" min="5" max="20" ]', SH_CD_SLUG ), 'premium' => true ], |
| 36 |
'sc-php-random-string' => [ 'class' => 'SC_RAND_STRING', 'description' => __( 'Display a random string of characters. It also supports the optional argument of "length". This specifies the number of characters you wish to display (default is 10) [sv slug="sc-php-random-string" length="15"]', SH_CD_SLUG ), 'premium' => true ], |
| 37 |
'sc-php-post-value' => [ 'class' => 'SC_POST_VALUE', 'description' => __( 'Display a value from the $_POST array. The "key" arguments specifies which array value to render. It also supports the optional arguments of "default". If there is no value in the array for the given "key" then the "default" will be displayed. [sv slug="sc-php-post-value" key="username" default="Not Found"]', SH_CD_SLUG ), 'premium' => true ], |
| 38 |
'sc-php-get-value' => [ 'class' => 'SC_GET_VALUE', 'description' => __( 'Display a value from the $_GET array. The "key" arguments specifies which array value to render. It also supports the optional arguments of "default". If there is no value in the array for the given "key" then the "default" will be displayed. [sv slug="sc-php-get-value" key="username" default="Not Found"]', SH_CD_SLUG ), 'premium' => true ], |
| 39 |
'sc-php-info' => [ 'class' => 'SC_PHP_INFO', 'description' => __( 'Display PHP Info', SH_CD_SLUG ), 'premium' => true ], |
| 40 |
'sc-post-id' => [ 'class' => 'SC_POST_ID', 'description' => __( 'Display ID for the current post.', SH_CD_SLUG ), 'premium' => true ], |
| 41 |
'sc-post-author' => [ 'class' => 'SC_POST_AUTHOR', 'description' => __( 'Display the author\'s display name or ID. The optional argument "field" allows you to specify whether you wish to display the author\'s "display-name" or "id". [sv slug="sc-post-author" field="id" ]', SH_CD_SLUG ), 'premium' => true ], |
| 42 |
'sc-post-counts' => [ 'class' => 'SC_POST_COUNTS', 'description' => __( 'Display a count of posts for certain statuses. Using the argument status, specify whether to return a count for all posts that have a status of "publish" (default), "future", "draft", "pending" or "private". [sv slug="sc-post-counts" status="draft"]', SH_CD_SLUG ), 'premium' => true ], |
| 43 |
'sc-user-counts' => [ 'class' => 'SC_USER_COUNTS', 'description' => __( 'Display a count of all WordPress users or the number of WordPress users for a given role e.g. [sv slug="sc-user-counts" role="subscriber"] or [sv slug="sc-user-counts"]', SH_CD_SLUG ), 'premium' => true ], |
| 44 |
'sc-user-profile-photo' => [ 'class' => 'SC_AVATAR', 'description' => __( 'Display the WordPress profile photo for the logged in user e.g. [sv slug="sc-user-profile-photo" width="150"] or [sv slug="sc-user-profile-photo"]. Please note, width defaults to 96px.', SH_CD_SLUG ), 'premium' => true ] |
| 45 |
|
| 46 |
// '' => [ 'class' => '', 'description' => '', 'premium' => true ] |
| 47 |
]; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Get data from get_bloginfo() |
| 53 |
* |
| 54 |
* Class SV_SC_SITE_TITLE |
| 55 |
*/ |
| 56 |
class SV_SC_BLOG_INFO extends SV_Preset { |
| 57 |
|
| 58 |
protected function unsanitised() { |
| 59 |
|
| 60 |
$args = $this->get_arguments(); |
| 61 |
|
| 62 |
$key = ( false === empty( $args['_sh_cd_func'] ) ) ? $args['_sh_cd_func'] : 'name'; |
| 63 |
|
| 64 |
return get_bloginfo( $key ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get data from $_SERVER |
| 70 |
* |
| 71 |
* Class SV_SC_SERVER_INFO |
| 72 |
*/ |
| 73 |
class SV_SC_SERVER_INFO extends SV_Preset { |
| 74 |
|
| 75 |
protected function unsanitised() { |
| 76 |
|
| 77 |
$args = $this->get_arguments(); |
| 78 |
|
| 79 |
return ( false === empty( $_SERVER[ $args['field'] ] ) ) ? $_SERVER[ $args['field'] ] : ''; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get a unique ID |
| 85 |
* |
| 86 |
* Class SV_SC_UNIQUE_ID |
| 87 |
*/ |
| 88 |
class SV_SC_UNIQUE_ID extends SV_Preset { |
| 89 |
|
| 90 |
protected function unsanitised() { |
| 91 |
|
| 92 |
$args = $this->get_arguments(); |
| 93 |
|
| 94 |
$prefix = ( false === empty( $args['prefix'] ) ) ? $args['prefix'] : ''; |
| 95 |
|
| 96 |
return uniqid ( $prefix, true ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get timestamp |
| 102 |
* |
| 103 |
* Class SV_SC_TIMESTAMP |
| 104 |
*/ |
| 105 |
class SV_SC_TIMESTAMP extends SV_Preset { |
| 106 |
|
| 107 |
protected function unsanitised() { |
| 108 |
return time(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Display PHP INFO |
| 114 |
* |
| 115 |
* Class SV_SC_PHP_INFO |
| 116 |
*/ |
| 117 |
class SV_SC_PHP_INFO extends SV_Preset { |
| 118 |
|
| 119 |
protected function unsanitised() { |
| 120 |
return phpinfo(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Current URL |
| 126 |
* |
| 127 |
* Class SV_SC_CURRENT_URL |
| 128 |
*/ |
| 129 |
class SV_SC_CURRENT_URL extends SV_Preset { |
| 130 |
|
| 131 |
protected function unsanitised() { |
| 132 |
|
| 133 |
$protocol = ( |
| 134 |
( isset($_SERVER['HTTPS'] ) && 'on' == $_SERVER['HTTPS'] ) || |
| 135 |
( isset($_SERVER['SERVER_PORT'] ) && 443 == $_SERVER['SERVER_PORT'] ) |
| 136 |
) ? 'https://' : 'http://'; |
| 137 |
|
| 138 |
return $protocol . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Register URL |
| 144 |
* |
| 145 |
* Class SV_SC_REGISTER_URL |
| 146 |
*/ |
| 147 |
class SV_SC_REGISTER_URL extends SV_Preset { |
| 148 |
|
| 149 |
protected function unsanitised() { |
| 150 |
return wp_registration_url(); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Random number |
| 156 |
* |
| 157 |
* Class SV_SC_RAND_NUMBER |
| 158 |
*/ |
| 159 |
class SV_SC_RAND_NUMBER extends SV_Preset { |
| 160 |
|
| 161 |
protected function unsanitised() { |
| 162 |
|
| 163 |
$args = $this->get_arguments(); |
| 164 |
|
| 165 |
$min = ( false === empty( $args['min'] ) ) ? (int) $args['min'] : 0; |
| 166 |
|
| 167 |
$max = ( false === empty( $args['max'] ) ) ? (int) $args['max'] : getrandmax(); |
| 168 |
|
| 169 |
return rand( $min, $max ); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Avatar URL |
| 175 |
* |
| 176 |
* Class SV_SC_AVATAR |
| 177 |
*/ |
| 178 |
class SV_SC_AVATAR extends SV_Preset { |
| 179 |
|
| 180 |
public function init() { |
| 181 |
|
| 182 |
$this->escape_method = false; |
| 183 |
} |
| 184 |
|
| 185 |
protected function unsanitised() { |
| 186 |
|
| 187 |
$args = $this->get_arguments(); |
| 188 |
$user_id = get_current_user_id(); |
| 189 |
|
| 190 |
// if ( true === empty( $user_id ) ) { |
| 191 |
// return ''; |
| 192 |
// } |
| 193 |
|
| 194 |
$width = ( false === empty( $args['width'] ) ) ? (int) $args['width'] : 96; |
| 195 |
$profile_url = get_avatar_url( $user_id, [ 'size' => $width ] ); |
| 196 |
|
| 197 |
if ( true === empty( $profile_url ) ){ |
| 198 |
return ''; |
| 199 |
} |
| 200 |
|
| 201 |
return sprintf( '<img src="%1$s" />', esc_url( $profile_url ) ); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Random string |
| 207 |
* |
| 208 |
* Based upon: https://stackoverflow.com/questions/4356289/php-random-string-generator |
| 209 |
* |
| 210 |
* Class SV_SC_RAND_STRING |
| 211 |
*/ |
| 212 |
class SV_SC_RAND_STRING extends SV_Preset { |
| 213 |
|
| 214 |
protected function unsanitised() { |
| 215 |
|
| 216 |
$args = $this->get_arguments(); |
| 217 |
|
| 218 |
$length = ( false === empty( $args['length'] ) ) ? (int) $args['length'] : 10; |
| 219 |
|
| 220 |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 221 |
$no_characters = strlen( $characters ); |
| 222 |
$random_string = ''; |
| 223 |
|
| 224 |
for ($i = 0; $i < $length; $i++) { |
| 225 |
$random_string .= $characters[ rand( 0, $no_characters - 1 ) ]; |
| 226 |
} |
| 227 |
|
| 228 |
return $random_string; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Fetch an item from the $_POST array |
| 234 |
* |
| 235 |
* Class SV_SC_POST_VALUE |
| 236 |
*/ |
| 237 |
class SV_SC_POST_VALUE extends SV_Preset { |
| 238 |
|
| 239 |
protected function unsanitised() { |
| 240 |
|
| 241 |
$args = $this->get_arguments(); |
| 242 |
|
| 243 |
$post_value = ( false === empty( $_POST[ $args['key'] ] ) ) ? $_POST[ $args['key'] ] : NULL; |
| 244 |
|
| 245 |
if ( null !== $post_value ) { |
| 246 |
return $post_value; |
| 247 |
} |
| 248 |
|
| 249 |
// Do we have a fall back default? |
| 250 |
return ( false === empty( $args['default'] ) ) ? $args['default'] : ''; |
| 251 |
|
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Fetch an item from the $_GET array |
| 257 |
* |
| 258 |
* Class SV_SC_GET_VALUE |
| 259 |
*/ |
| 260 |
class SV_SC_GET_VALUE extends SV_Preset { |
| 261 |
|
| 262 |
protected function unsanitised() { |
| 263 |
|
| 264 |
$args = $this->get_arguments(); |
| 265 |
|
| 266 |
$post_value = ( false === empty( $_GET[ $args['key'] ] ) ) ? $_GET[ $args['key'] ] : NULL; |
| 267 |
|
| 268 |
if ( null !== $post_value ) { |
| 269 |
return $post_value; |
| 270 |
} |
| 271 |
|
| 272 |
// Do we have a fall back default? |
| 273 |
return ( false === empty( $args['default'] ) ) ? $args['default'] : ''; |
| 274 |
|
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Display current Post ID |
| 280 |
* |
| 281 |
* Class SV_SC_POST_ID |
| 282 |
*/ |
| 283 |
class SV_SC_POST_ID extends SV_Preset { |
| 284 |
|
| 285 |
protected function unsanitised() { |
| 286 |
return get_the_ID(); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Display author of current post |
| 292 |
* |
| 293 |
* Class SV_SC_POST_AUTHOR |
| 294 |
*/ |
| 295 |
class SV_SC_POST_AUTHOR extends SV_Preset { |
| 296 |
|
| 297 |
protected function unsanitised() { |
| 298 |
|
| 299 |
$args = $this->get_arguments(); |
| 300 |
|
| 301 |
switch ( $args['field'] ) { |
| 302 |
case 'id': |
| 303 |
return get_the_author_meta( 'ID' ); |
| 304 |
break; |
| 305 |
default: |
| 306 |
return get_the_author(); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Display post counts |
| 313 |
* |
| 314 |
* Class SV_SC_POST_COUNTS |
| 315 |
*/ |
| 316 |
class SV_SC_POST_COUNTS extends SV_Preset { |
| 317 |
|
| 318 |
protected function unsanitised() { |
| 319 |
|
| 320 |
$args = $this->get_arguments(); |
| 321 |
|
| 322 |
$counts = wp_count_posts(); |
| 323 |
|
| 324 |
switch ( $args['status'] ) { |
| 325 |
case 'future': |
| 326 |
return $counts->future; |
| 327 |
break; |
| 328 |
case 'draft': |
| 329 |
return $counts->draft; |
| 330 |
break; |
| 331 |
case 'pending': |
| 332 |
return $counts->pending; |
| 333 |
break; |
| 334 |
case 'private': |
| 335 |
return $counts->private; |
| 336 |
break; |
| 337 |
default: |
| 338 |
return $counts->publish; |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Get a User counts |
| 345 |
* |
| 346 |
* Class SV_SC_USER_COUNTS |
| 347 |
*/ |
| 348 |
class SV_SC_USER_COUNTS extends SV_Preset { |
| 349 |
|
| 350 |
protected function unsanitised() { |
| 351 |
|
| 352 |
$args = $this->get_arguments(); |
| 353 |
|
| 354 |
$role = ( false === empty( $args['role'] ) ) ? $args['role'] : NULL; |
| 355 |
|
| 356 |
$users = count_users(); |
| 357 |
|
| 358 |
if ( false === empty( $role ) && true === isset( $users['avail_roles'][ $role ] ) ) { |
| 359 |
return $users['avail_roles'][ $role ]; |
| 360 |
} if ( true === isset( $users['total_users'] ) ) { |
| 361 |
return $users['total_users']; |
| 362 |
} |
| 363 |
|
| 364 |
return ''; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Display a date and also support date arithmetic |
| 370 |
* |
| 371 |
* Class SV_SC_DATE |
| 372 |
*/ |
| 373 |
class SV_SC_DATE extends SV_Preset { |
| 374 |
|
| 375 |
protected function unsanitised() { |
| 376 |
|
| 377 |
$args = $this->get_arguments(); |
| 378 |
|
| 379 |
$todays_date = date_create(); |
| 380 |
|
| 381 |
// Do we have an interval? |
| 382 |
if ( false === empty( $args['interval'] ) ) { |
| 383 |
|
| 384 |
$interval = date_interval_create_from_date_string( $args['interval'] ); |
| 385 |
|
| 386 |
if ( $interval !== false ) { |
| 387 |
$todays_date = date_add( $todays_date, $interval ); |
| 388 |
} |
| 389 |
|
| 390 |
} |
| 391 |
|
| 392 |
$date_format = ( false === empty( $args['format'] ) ) ? $args['format'] : 'd/m/Y'; |
| 393 |
|
| 394 |
return date_format( $todays_date, $date_format ); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* Fetch a column from a MySQL database by ID |
| 400 |
* |
| 401 |
* Min example: |
| 402 |
* |
| 403 |
* [sv slug="sc-db-value-by-id" table="users" column="user_login" column-to-search="id" key="3"] |
| 404 |
* |
| 405 |
* Full example: |
| 406 |
* |
| 407 |
* [sv slug="sc-db-value-by-id" table="users" column="user_login" column-to-search="id" key="3" key-format="%d" message-not-found="User not found" cache=false cache-duration=3600 ] |
| 408 |
* |
| 409 |
* SC_DB_VALUE_BY_ID |
| 410 |
*/ |
| 411 |
class SV_SC_DB_VALUE_BY_ID extends SV_Preset { |
| 412 |
|
| 413 |
protected function unsanitised() { |
| 414 |
|
| 415 |
$args = wp_parse_args( $this->get_arguments(), [ 'key-format' => '%d', 'message-not-found' => '', 'cache' => true, 'cache-duration' => 1 * HOUR_IN_SECONDS ] ); |
| 416 |
|
| 417 |
if ( $validation_error = $this->validate_arguments( $args ) ) { |
| 418 |
return $validation_error; |
| 419 |
} |
| 420 |
|
| 421 |
$cache_key = md5( print_r( $args, true ) ); |
| 422 |
|
| 423 |
if( true === sh_cd_to_bool( $args[ 'cache'] ) |
| 424 |
&& $cache = sh_cd_cache_get( $cache_key ) ) { |
| 425 |
return $cache; |
| 426 |
} |
| 427 |
|
| 428 |
global $wpdb; |
| 429 |
|
| 430 |
$sql = sprintf( 'Select %s from %s where %s = %s', |
| 431 |
$args['column'], |
| 432 |
$args['table'], |
| 433 |
$args['column-to-search'], |
| 434 |
$args['key-format'] |
| 435 |
); |
| 436 |
|
| 437 |
$sql = $wpdb->prepare( $sql, $args['key'] ); |
| 438 |
$value = $wpdb->get_var( $sql ); |
| 439 |
|
| 440 |
if ( true === empty( $value )) { |
| 441 |
$value = $args['message-not-found'] ; |
| 442 |
} |
| 443 |
|
| 444 |
sh_cd_cache_set( $cache_key, $value, (int) $args[ 'cache-duration'] ); |
| 445 |
|
| 446 |
return $value; |
| 447 |
} |
| 448 |
|
| 449 |
protected function validate_arguments( $args = [] ) { |
| 450 |
|
| 451 |
if ( false === sh_cd_is_shortcode_db_value_by_id_enabled() ) { |
| 452 |
return 'This shortcode has been disabled by admin. See "WP Admin" > "Snippet Shortcodes" > "Settings" to enable.'; |
| 453 |
} |
| 454 |
|
| 455 |
if ( true === empty( $args['table'] ) ) { |
| 456 |
return 'No MySQL table specified (i.e. "table" argument is missing).'; |
| 457 |
} |
| 458 |
|
| 459 |
if ( true === empty( $args['column'] ) ) { |
| 460 |
return 'No MySQL column specified (i.e. "column" argument is missing).'; |
| 461 |
} |
| 462 |
|
| 463 |
if ( true === empty( $args['column-to-search'] ) ) { |
| 464 |
return 'The MySQL column to search for given key has not been specified (i.e. "column-to-search" argument is missing).'; |
| 465 |
} |
| 466 |
|
| 467 |
if ( true === empty( $args['key'] ) ) { |
| 468 |
return 'No key has been specified (i.e. "key" argument is missing).'; |
| 469 |
} |
| 470 |
|
| 471 |
if ( true === empty( $args['key-format'] ) ) { |
| 472 |
return 'No key format has been specified (i.e. "key-format" argument is missing). If the "key" field is numeric, then use %d otherwise use %s.'; |
| 473 |
} |
| 474 |
|
| 475 |
if ( false === in_array( $args['key-format'], [ '%s', '%d' ] ) ) { |
| 476 |
return 'The key format is invalid. If the "key" field is numeric, then use %d otherwise use %s.'; |
| 477 |
} |
| 478 |
|
| 479 |
} |
| 480 |
} |