| 1 |
<?php |
| 2 |
defined( 'WPINC' ) OR exit; |
| 3 |
|
| 4 |
/** |
| 5 |
* General utility function for Document Gallery. |
| 6 |
* |
| 7 |
* @author drossiter |
| 8 |
*/ |
| 9 |
class DG_Util { |
| 10 |
|
| 11 |
/** |
| 12 |
* @param callable $callable The callable. |
| 13 |
* @return string The string representation of the callable. |
| 14 |
*/ |
| 15 |
public static function callableToString($callable) { |
| 16 |
$ret = $callable; |
| 17 |
if ( is_array( $callable ) ) { |
| 18 |
$sep = '::'; |
| 19 |
if ( !is_string( $callable[0] ) ) { |
| 20 |
$callable[0] = get_class( $callable[0] ); |
| 21 |
$sep = '->'; |
| 22 |
} |
| 23 |
|
| 24 |
$ret = "{$callable[0]}$sep{$callable[1]}"; |
| 25 |
} |
| 26 |
|
| 27 |
return $ret; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* @return array All blog IDs. |
| 32 |
*/ |
| 33 |
public static function getBlogIds() { |
| 34 |
global $wpdb; |
| 35 |
return $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Whether the given attachment has a thumbanil graphic. |
| 40 |
* |
| 41 |
* @param $ID int The id of the attachment to be checked. |
| 42 |
* @return bool Whether the given attachment has a thumbnail image. |
| 43 |
*/ |
| 44 |
public static function hasThumb($ID) { |
| 45 |
$options = DocumentGallery::getOptions(); |
| 46 |
$thumbs = $options['thumber']['thumbs']; |
| 47 |
return array_key_exists($ID, $thumbs) && ! empty( $thumbs[ $ID ]['thumber'] ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @param mixed $maybeint Data you wish to have converted to a positive integer. |
| 52 |
* |
| 53 |
* @return int A positive integer. |
| 54 |
*/ |
| 55 |
public static function posint($maybeint) { |
| 56 |
return max(absint($maybeint), 1); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Converts provided value to bool. |
| 61 |
* |
| 62 |
* @param mixed $val To be converted. |
| 63 |
* @param bool|NULL $default The value to return if unable to parse $val. |
| 64 |
* |
| 65 |
* @return bool|NULL Bool value if can be parsed, else NULL. |
| 66 |
*/ |
| 67 |
public static function toBool( $val, $default = null ) { |
| 68 |
if ( is_null( $val ) ) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
if ( is_bool( $val ) ) { |
| 73 |
return $val; |
| 74 |
} |
| 75 |
|
| 76 |
if ( is_int( $val ) ) { |
| 77 |
if ( 1 === $val ) { |
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
if ( 0 === $val ) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if ( is_string( $val ) ) { |
| 87 |
$val = strtolower( $val ); |
| 88 |
if ( 'true' === $val || '1' === $val ) { |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
if ( 'false' === $val || '0' === $val ) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return $default; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Wrapper method which handles deciding whether to include minified assets. Minified files are not used |
| 102 |
* in WP_DEBUG mode to make troubleshooting easier. |
| 103 |
* |
| 104 |
* @param $handle string Unique identifier for the script/style. |
| 105 |
* @param $src string Relative path to asset from DG_URL. |
| 106 |
* @param array $deps Any assets depended on by asset to be enqueued. |
| 107 |
* @param bool $in_footer For scripts, dictates whether to put in footer. |
| 108 |
*/ |
| 109 |
public static function enqueueAsset( $handle, $src, $deps = array(), $in_footer = true ) { |
| 110 |
if ( !defined('WP_DEBUG') || !WP_DEBUG ) { |
| 111 |
$src = preg_replace('/^(.*)\.(css|js)$/', '$1.min.$2', $src, 1); |
| 112 |
} |
| 113 |
|
| 114 |
if ( preg_match( '/.js/', $src ) ) { |
| 115 |
wp_enqueue_script( $handle, DG_URL . $src, $deps, DG_VERSION, $in_footer ); |
| 116 |
} else { |
| 117 |
wp_enqueue_style( $handle, DG_URL . $src, $deps, DG_VERSION ); |
| 118 |
} |
| 119 |
} |
| 120 |
} |