| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
/** |
| 5 |
* Round UP to nearest half integer. |
| 6 |
* |
| 7 |
* @since 1.0 |
| 8 |
* @source http://stackoverflow.com/a/13526408 |
| 9 |
* |
| 10 |
* @param int|float|string $number The number to round up. |
| 11 |
* @return float The formatted number. |
| 12 |
*/ |
| 13 |
function imagify_round_half_five( $number ) { |
| 14 |
$number = strval( $number ); |
| 15 |
$number = explode( '.', $number ); |
| 16 |
|
| 17 |
if ( ! isset( $number[1] ) ) { |
| 18 |
return $number[0]; |
| 19 |
} |
| 20 |
|
| 21 |
$decimal = floatval( '0.' . substr( $number[1], 0 , 2 ) ); // Cut only 2 numbers. |
| 22 |
|
| 23 |
if ( $decimal > 0 ) { |
| 24 |
if ( $decimal <= 0.5 ) { |
| 25 |
return floatval( $number[0] ) + 0.5; |
| 26 |
} |
| 27 |
if ( $decimal <= 0.99 ) { |
| 28 |
return floatval( $number[0] ) + 1; |
| 29 |
} |
| 30 |
return 1; |
| 31 |
} |
| 32 |
|
| 33 |
return floatval( $number ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Convert number of bytes largest unit bytes will fit into. |
| 38 |
* This is a clone of size_format(), but with a non-breaking space. |
| 39 |
* |
| 40 |
* @since 1.7 |
| 41 |
* @author Grégory Viguier |
| 42 |
* |
| 43 |
* @param int|string $bytes Number of bytes. Note max integer size for integers. |
| 44 |
* @param int $decimals Optional. Precision of number of decimal places. Default 0. |
| 45 |
* @return string|false False on failure. Number string on success. |
| 46 |
*/ |
| 47 |
function imagify_size_format( $bytes, $decimals = 0 ) { |
| 48 |
$bytes = @size_format( $bytes, $decimals ); |
| 49 |
return str_replace( ' ', ' ', $bytes ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get the Imagify attachment class name depending to a context. |
| 54 |
* |
| 55 |
* @since 1.5 |
| 56 |
* @since 1.6.6 $attachment_id and $identifier have been added. |
| 57 |
* @author Jonathan Buttigieg |
| 58 |
* |
| 59 |
* @param string $context The context to determine the class name. |
| 60 |
* @param int $attachment_id The attachment ID. |
| 61 |
* @param string $identifier An identifier. |
| 62 |
* @return string The Imagify attachment class name. |
| 63 |
*/ |
| 64 |
function get_imagify_attachment_class_name( $context, $attachment_id, $identifier ) { |
| 65 |
$context = $context ? $context : 'wp'; |
| 66 |
|
| 67 |
if ( 'wp' !== $context && 'wp' === strtolower( $context ) ) { |
| 68 |
$context = 'wp'; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Filter the context used for the optimization. |
| 73 |
* |
| 74 |
* @since 1.6.6 |
| 75 |
* @author Grégory Viguier |
| 76 |
* |
| 77 |
* @param string $context The context. |
| 78 |
* @param int $attachment_id The attachment ID. |
| 79 |
* @param string $identifier An identifier. |
| 80 |
*/ |
| 81 |
$context = apply_filters( 'imagify_optimize_attachment_context', $context, $attachment_id, $identifier ); |
| 82 |
|
| 83 |
return 'Imagify_' . ( 'wp' !== $context ? $context . '_' : '' ) . 'Attachment'; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get the Imagify attachment instance depending to a context. |
| 88 |
* |
| 89 |
* @since 1.6.13 |
| 90 |
* @author Grégory Viguier |
| 91 |
* |
| 92 |
* @param string $context The context to determine the class name. |
| 93 |
* @param int $attachment_id The attachment ID. |
| 94 |
* @param string $identifier An identifier. |
| 95 |
* @return object The Imagify attachment instance. |
| 96 |
*/ |
| 97 |
function get_imagify_attachment( $context, $attachment_id, $identifier ) { |
| 98 |
$class_name = get_imagify_attachment_class_name( $context, $attachment_id, $identifier ); |
| 99 |
return new $class_name( $attachment_id ); |
| 100 |
} |
| 101 |
|