| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Normalization traits. |
| 5 |
* |
| 6 |
* @package \Optml\Inc\Traits |
| 7 |
* @author Optimole <friends@optimole.com> |
| 8 |
*/ |
| 9 |
trait Optml_Normalizer { |
| 10 |
|
| 11 |
/** |
| 12 |
* Normalize value to boolean. |
| 13 |
* |
| 14 |
* @param mixed $value Value to process. |
| 15 |
* |
| 16 |
* @return bool |
| 17 |
*/ |
| 18 |
public function to_boolean( $value ) { |
| 19 |
if ( in_array( $value, [ 'yes', 'enabled', 'true', '1' ], true ) ) { |
| 20 |
return true; |
| 21 |
} |
| 22 |
|
| 23 |
if ( in_array( $value, [ 'no', 'disabled', 'false', '0' ], true ) ) { |
| 24 |
return false; |
| 25 |
} |
| 26 |
|
| 27 |
return boolval( $value ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Return domain hash. |
| 32 |
* |
| 33 |
* @param string $domain Full url. |
| 34 |
* |
| 35 |
* @return string Domain hash. |
| 36 |
*/ |
| 37 |
public function to_domain_hash( $domain ) { |
| 38 |
$domain_parts = parse_url( $domain ); |
| 39 |
$domain = isset( $domain_parts['host'] ) ? $domain_parts['host'] : ''; |
| 40 |
$prefix = substr( $domain, 0, 4 ); |
| 41 |
if ( $prefix === 'www.' ) { |
| 42 |
$domain = substr( $domain, 4 ); |
| 43 |
} |
| 44 |
|
| 45 |
return base64_encode( $domain ); |
| 46 |
} |
| 47 |
/** |
| 48 |
* Strip slashes on unicode encoded strings. |
| 49 |
* |
| 50 |
* @param string $string Input string. |
| 51 |
* |
| 52 |
* @return string Decoded string. |
| 53 |
*/ |
| 54 |
public function strip_slashes( $string ) { |
| 55 |
return html_entity_decode( stripslashes( preg_replace( '/\\\u([\da-fA-F]{4})/', '&#x\1;', $string ) ) ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Normalize value to positive integer. |
| 60 |
* |
| 61 |
* @param mixed $value Value to process. |
| 62 |
* |
| 63 |
* @return integer |
| 64 |
*/ |
| 65 |
public function to_positive_integer( $value ) { |
| 66 |
$integer = (int) $value; |
| 67 |
|
| 68 |
return ( $integer > 0 ) ? $integer : 0; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Normalize value to map. |
| 73 |
* |
| 74 |
* @param mixed $value Value to process. |
| 75 |
* @param array $map Associative list from witch to return. |
| 76 |
* @param mixed $default Default. |
| 77 |
* |
| 78 |
* @return mixed |
| 79 |
*/ |
| 80 |
public function to_map_values( $value, $map, $default ) { |
| 81 |
if ( in_array( $value, $map, true ) ) { |
| 82 |
return $value; |
| 83 |
} |
| 84 |
|
| 85 |
return $default; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Normalize value to an accepted quality. |
| 90 |
* |
| 91 |
* @param mixed $value Value to process. |
| 92 |
* |
| 93 |
* @return mixed |
| 94 |
*/ |
| 95 |
public function to_accepted_quality( $value ) { |
| 96 |
if ( is_numeric( $value ) ) { |
| 97 |
return intval( $value ); |
| 98 |
} |
| 99 |
$value = trim( $value ); |
| 100 |
|
| 101 |
$accepted_qualities = [ |
| 102 |
'eco' => 'eco', |
| 103 |
'auto' => 'auto', |
| 104 |
'mauto' => 'mauto', |
| 105 |
'high_c' => 55, |
| 106 |
'medium_c' => 75, |
| 107 |
'low_c' => 90, |
| 108 |
]; |
| 109 |
|
| 110 |
if ( array_key_exists( $value, $accepted_qualities ) ) { |
| 111 |
return $accepted_qualities[ $value ]; |
| 112 |
} |
| 113 |
|
| 114 |
// Legacy values. |
| 115 |
return 60; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Normalize value to an accepted minify. |
| 120 |
* |
| 121 |
* @param mixed $value Value to process. |
| 122 |
* |
| 123 |
* @return mixed |
| 124 |
*/ |
| 125 |
public function to_accepted_minify( $value ) { |
| 126 |
if ( is_numeric( $value ) ) { |
| 127 |
return $this->to_bound_integer( $value, 0, 1 ); |
| 128 |
} |
| 129 |
|
| 130 |
return 'auto'; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Normalize value to an integer within bounds. |
| 135 |
* |
| 136 |
* @param mixed $value Value to process. |
| 137 |
* @param integer $min Lower bound. |
| 138 |
* @param integer $max Upper bound. |
| 139 |
* |
| 140 |
* @return integer |
| 141 |
*/ |
| 142 |
public function to_bound_integer( $value, $min, $max ) { |
| 143 |
$integer = absint( $value ); |
| 144 |
if ( $integer < $min ) { |
| 145 |
$integer = $min; |
| 146 |
} |
| 147 |
if ( $integer > $max ) { |
| 148 |
$integer = $max; |
| 149 |
} |
| 150 |
|
| 151 |
return $integer; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Normalize arguments for crop. |
| 156 |
* |
| 157 |
* @param array|bool $crop_args Crop arguments. |
| 158 |
* |
| 159 |
* @return array |
| 160 |
*/ |
| 161 |
public function to_optml_crop( $crop_args = [] ) { |
| 162 |
|
| 163 |
$enlarge = false; |
| 164 |
if ( isset( $crop_args['enlarge'] ) ) { |
| 165 |
$enlarge = $crop_args['enlarge']; |
| 166 |
$crop_args = $crop_args['crop']; |
| 167 |
} |
| 168 |
if ( $crop_args === true ) { |
| 169 |
return [ |
| 170 |
'type' => Optml_Resize::RESIZE_FILL, |
| 171 |
'enlarge' => $enlarge, |
| 172 |
'gravity' => Optml_Resize::GRAVITY_CENTER, |
| 173 |
]; |
| 174 |
} |
| 175 |
if ( $crop_args === false || ! is_array( $crop_args ) || count( $crop_args ) !== 2 ) { |
| 176 |
return []; |
| 177 |
} |
| 178 |
|
| 179 |
$allowed_x = [ |
| 180 |
'left' => true, |
| 181 |
'center' => true, |
| 182 |
'right' => true, |
| 183 |
]; |
| 184 |
$allowed_y = [ |
| 185 |
'top' => true, |
| 186 |
'center' => true, |
| 187 |
'bottom' => true, |
| 188 |
]; |
| 189 |
$allowed_gravities = [ |
| 190 |
'left' => Optml_Resize::GRAVITY_WEST, |
| 191 |
'right' => Optml_Resize::GRAVITY_EAST, |
| 192 |
'top' => Optml_Resize::GRAVITY_NORTH, |
| 193 |
'bottom' => Optml_Resize::GRAVITY_SOUTH, |
| 194 |
'lefttop' => Optml_Resize::GRAVITY_NORTH_WEST, |
| 195 |
'leftbottom' => Optml_Resize::GRAVITY_SOUTH_WEST, |
| 196 |
'righttop' => Optml_Resize::GRAVITY_NORTH_EAST, |
| 197 |
'rightbottom' => Optml_Resize::GRAVITY_SOUTH_EAST, |
| 198 |
'centertop' => [ 0.5, 0 ], |
| 199 |
'centerbottom' => [ 0.5, 1 ], |
| 200 |
'leftcenter' => [ 0, 0.5 ], |
| 201 |
'rightcenter' => [ 1, 0.5 ], |
| 202 |
]; |
| 203 |
|
| 204 |
$gravity = Optml_Resize::GRAVITY_CENTER; |
| 205 |
$key_search = ( $crop_args[0] === true ? '' : |
| 206 |
( isset( $allowed_x[ $crop_args[0] ] ) ? $crop_args[0] : '' ) ) . |
| 207 |
( $crop_args[1] === true ? '' : |
| 208 |
( isset( $allowed_y[ $crop_args[1] ] ) ? $crop_args[1] : '' ) ); |
| 209 |
|
| 210 |
if ( array_key_exists( $key_search, $allowed_gravities ) ) { |
| 211 |
$gravity = $allowed_gravities[ $key_search ]; |
| 212 |
} |
| 213 |
|
| 214 |
return [ |
| 215 |
'type' => Optml_Resize::RESIZE_FILL, |
| 216 |
'enlarge' => $enlarge, |
| 217 |
'gravity' => $gravity, |
| 218 |
]; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Normalize arguments for watermark. |
| 223 |
* |
| 224 |
* @param array $watermark_args Watermark arguments. |
| 225 |
* |
| 226 |
* @return array |
| 227 |
*/ |
| 228 |
public function to_optml_watermark( $watermark_args = [] ) { |
| 229 |
$allowed_gravities = [ |
| 230 |
'left' => Optml_Resize::GRAVITY_WEST, |
| 231 |
'right' => Optml_Resize::GRAVITY_EAST, |
| 232 |
'top' => Optml_Resize::GRAVITY_NORTH, |
| 233 |
'bottom' => Optml_Resize::GRAVITY_SOUTH, |
| 234 |
'left_top' => Optml_Resize::GRAVITY_NORTH_WEST, |
| 235 |
'left_bottom' => Optml_Resize::GRAVITY_SOUTH_WEST, |
| 236 |
'right_top' => Optml_Resize::GRAVITY_NORTH_EAST, |
| 237 |
'right_bottom' => Optml_Resize::GRAVITY_SOUTH_EAST, |
| 238 |
]; |
| 239 |
$gravity = Optml_Resize::GRAVITY_CENTER; |
| 240 |
if ( isset( $watermark_args['position'] ) && array_key_exists( $watermark_args['position'], $allowed_gravities ) ) { |
| 241 |
$gravity = $allowed_gravities[ $watermark_args['position'] ]; |
| 242 |
} |
| 243 |
|
| 244 |
return [ |
| 245 |
'opacity' => 1, |
| 246 |
'position' => $gravity, |
| 247 |
]; |
| 248 |
} |
| 249 |
/** |
| 250 |
* If missing, add schema to urls. |
| 251 |
* |
| 252 |
* @param string $url Url to check. |
| 253 |
* |
| 254 |
* @return string |
| 255 |
*/ |
| 256 |
public function add_schema( $url ) { |
| 257 |
$schema_url = $url; |
| 258 |
$should_add_schema = false; |
| 259 |
if ( function_exists( 'str_starts_with' ) ) { |
| 260 |
$should_add_schema = str_starts_with( $schema_url, '//' ); |
| 261 |
} else { |
| 262 |
$should_add_schema = substr( $schema_url, 0, strlen( '//' ) ) === '//'; |
| 263 |
} |
| 264 |
if ( $should_add_schema ) { |
| 265 |
$schema_url = is_ssl() ? 'https:' : 'http:' . $schema_url; |
| 266 |
} |
| 267 |
return $schema_url; |
| 268 |
} |
| 269 |
} |
| 270 |
|