| 1 |
<?php |
| 2 |
/** |
| 3 |
* PropertyHive Formatting |
| 4 |
* |
| 5 |
* Functions for formatting data. |
| 6 |
* |
| 7 |
* @author PropertyHive |
| 8 |
* @category Core |
| 9 |
* @package PropertyHive/Functions |
| 10 |
* @version 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Clean variables using sanitize_text_field. |
| 19 |
* @param string|array $var |
| 20 |
* @return string|array |
| 21 |
*/ |
| 22 |
function ph_clean( $var ) { |
| 23 |
|
| 24 |
if ( is_array( $var ) ) { |
| 25 |
return array_map( 'ph_clean', $var ); |
| 26 |
} else { |
| 27 |
return is_scalar( $var ) ? sanitize_text_field( $var ) : $var; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Strip none numeric or comma chars from phone numbers |
| 33 |
* @param string |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
|
| 37 |
function ph_clean_telephone_number( $var ) { |
| 38 |
|
| 39 |
return preg_replace( "/[^0-9,]/", "", $var ); |
| 40 |
} |
| 41 |
|
| 42 |
if ( ! function_exists( 'ph_rgb_from_hex' ) ) { |
| 43 |
|
| 44 |
/** |
| 45 |
* Hex darker/lighter/contrast functions for colours. |
| 46 |
* |
| 47 |
* @param mixed $color |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
function ph_rgb_from_hex( $color ) { |
| 51 |
$color = str_replace( '#', '', $color ); |
| 52 |
// Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF" |
| 53 |
$color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color ); |
| 54 |
|
| 55 |
$rgb = array(); |
| 56 |
$rgb['R'] = hexdec( $color{0} . $color{1} ); |
| 57 |
$rgb['G'] = hexdec( $color{2} . $color{3} ); |
| 58 |
$rgb['B'] = hexdec( $color{4} . $color{5} ); |
| 59 |
|
| 60 |
return $rgb; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
if ( ! function_exists( 'ph_hex_darker' ) ) { |
| 65 |
|
| 66 |
/** |
| 67 |
* Hex darker/lighter/contrast functions for colours. |
| 68 |
* |
| 69 |
* @param mixed $color |
| 70 |
* @param int $factor (default: 30) |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
function ph_hex_darker( $color, $factor = 30 ) { |
| 74 |
$base = ph_rgb_from_hex( $color ); |
| 75 |
$color = '#'; |
| 76 |
|
| 77 |
foreach ( $base as $k => $v ) { |
| 78 |
$amount = $v / 100; |
| 79 |
$amount = round( $amount * $factor ); |
| 80 |
$new_decimal = $v - $amount; |
| 81 |
|
| 82 |
$new_hex_component = dechex( $new_decimal ); |
| 83 |
if ( strlen( $new_hex_component ) < 2 ) { |
| 84 |
$new_hex_component = "0" . $new_hex_component; |
| 85 |
} |
| 86 |
$color .= $new_hex_component; |
| 87 |
} |
| 88 |
|
| 89 |
return $color; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! function_exists( 'ph_hex_lighter' ) ) { |
| 94 |
|
| 95 |
/** |
| 96 |
* Hex darker/lighter/contrast functions for colours. |
| 97 |
* |
| 98 |
* @param mixed $color |
| 99 |
* @param int $factor (default: 30) |
| 100 |
* @return string |
| 101 |
*/ |
| 102 |
function ph_hex_lighter( $color, $factor = 30 ) { |
| 103 |
$base = ph_rgb_from_hex( $color ); |
| 104 |
$color = '#'; |
| 105 |
|
| 106 |
foreach ( $base as $k => $v ) { |
| 107 |
$amount = 255 - $v; |
| 108 |
$amount = $amount / 100; |
| 109 |
$amount = round( $amount * $factor ); |
| 110 |
$new_decimal = $v + $amount; |
| 111 |
|
| 112 |
$new_hex_component = dechex( $new_decimal ); |
| 113 |
if ( strlen( $new_hex_component ) < 2 ) { |
| 114 |
$new_hex_component = "0" . $new_hex_component; |
| 115 |
} |
| 116 |
$color .= $new_hex_component; |
| 117 |
} |
| 118 |
|
| 119 |
return $color; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if ( ! function_exists( 'ph_light_or_dark' ) ) { |
| 124 |
|
| 125 |
/** |
| 126 |
* Detect if we should use a light or dark colour on a background colour. |
| 127 |
* |
| 128 |
* @param mixed $color |
| 129 |
* @param string $dark (default: '#000000') |
| 130 |
* @param string $light (default: '#FFFFFF') |
| 131 |
* @return string |
| 132 |
*/ |
| 133 |
function ph_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) { |
| 134 |
|
| 135 |
$hex = str_replace( '#', '', $color ); |
| 136 |
|
| 137 |
$c_r = hexdec( substr( $hex, 0, 2 ) ); |
| 138 |
$c_g = hexdec( substr( $hex, 2, 2 ) ); |
| 139 |
$c_b = hexdec( substr( $hex, 4, 2 ) ); |
| 140 |
|
| 141 |
$brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000; |
| 142 |
|
| 143 |
return $brightness > 155 ? $dark : $light; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if ( ! function_exists( 'ph_format_hex' ) ) { |
| 148 |
|
| 149 |
/** |
| 150 |
* Format string as hex. |
| 151 |
* |
| 152 |
* @param string $hex |
| 153 |
* @return string |
| 154 |
*/ |
| 155 |
function ph_format_hex( $hex ) { |
| 156 |
|
| 157 |
$hex = trim( str_replace( '#', '', $hex ) ); |
| 158 |
|
| 159 |
if ( strlen( $hex ) == 3 ) { |
| 160 |
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; |
| 161 |
} |
| 162 |
|
| 163 |
return $hex ? '#' . $hex : null; |
| 164 |
} |
| 165 |
} |