ewww-image-optimizer
Last commit date
bin
9 years ago
binaries
2 years ago
classes
2 months ago
docs
7 years ago
images
3 months ago
includes
2 months ago
tests
2 months ago
vendor
10 months ago
.travis.yml
5 months ago
aux-optimize.php
2 months ago
bulk.php
2 months ago
changelog.txt
2 months ago
common.php
2 months ago
composer.json
10 months ago
composer.lock
3 years ago
ewww-image-optimizer.php
2 months ago
functions.php
6 months ago
license.txt
7 years ago
mwebp.php
2 months ago
phpcs.ruleset.xml
6 months ago
phpunit.xml
6 years ago
readme.txt
2 months ago
uninstall.php
7 years ago
unique.php
3 months ago
functions.php
167 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Wrapper functions for commonly used functions that haven't been fully migrated to oop OR failsafes for backwards compat. |
| 4 | * |
| 5 | * @link https://ewww.io |
| 6 | * @package EWWW_Image_Optimizer |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Check if file exists, and that it is local rather than using a protocol like http:// or phar:// |
| 15 | * |
| 16 | * @param string $file The path of the file to check. |
| 17 | * @return bool True if the file exists and is local, false otherwise. |
| 18 | */ |
| 19 | function ewwwio_is_file( $file ) { |
| 20 | return ewwwio()->is_file( $file ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Check filesize, and prevent errors by ensuring file exists, and that the cache has been cleared. |
| 25 | * |
| 26 | * @param string $file The name of the file. |
| 27 | * @return int The size of the file or zero. |
| 28 | */ |
| 29 | function ewww_image_optimizer_filesize( $file ) { |
| 30 | return ewwwio()->filesize( $file ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Check the mimetype of the given file with magic mime strings/patterns. |
| 35 | * |
| 36 | * @param string $path The absolute path to the file. |
| 37 | * @param string $type The type of file we are checking. Default 'i' for |
| 38 | * images/pdfs or 'b' for binary. |
| 39 | * @return bool|string A valid mime-type or false. |
| 40 | */ |
| 41 | function ewww_image_optimizer_mimetype( $path, $type = 'i' ) { |
| 42 | return ewwwio()->mimetype( $path, $type ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get mimetype based on file extension instead of file contents when speed outweighs accuracy. |
| 47 | * |
| 48 | * @param string $path The name of the file. |
| 49 | * @return string|bool The mime type based on the extension or false. |
| 50 | */ |
| 51 | function ewww_image_optimizer_quick_mimetype( $path ) { |
| 52 | return ewwwio()->quick_mimetype( $path ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Retrieve option: use 'site' setting if plugin is network activated, otherwise use 'blog' setting. |
| 57 | * |
| 58 | * Retrieves multi-site and single-site options as appropriate as well as allowing overrides with |
| 59 | * same-named constant. Overrides are only available for integers, booleans, and specifically supported options. |
| 60 | * |
| 61 | * @param string $option_name The name of the option to retrieve. |
| 62 | * @param mixed $default_value The default to use if not found/set, defaults to false, but not currently used. |
| 63 | * @param bool $single Use single-site setting regardless of multisite activation. Default is off/false. |
| 64 | * @return mixed The value of the option. |
| 65 | */ |
| 66 | function ewww_image_optimizer_get_option( $option_name, $default_value = false, $single = false ) { |
| 67 | return ewwwio()->get_option( $option_name, $default_value, $single ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Set an option: use 'site' setting if plugin is network activated, otherwise use 'blog' setting. |
| 72 | * |
| 73 | * @param string $option_name The name of the option to save. |
| 74 | * @param mixed $option_value The value to save for the option. |
| 75 | * @return bool True if the operation was successful. |
| 76 | */ |
| 77 | function ewww_image_optimizer_set_option( $option_name, $option_value ) { |
| 78 | return ewwwio()->set_option( $option_name, $option_value ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Escape any spaces in the filename. |
| 83 | * |
| 84 | * @param string $path The path to a binary file. |
| 85 | * @return string The path with spaces escaped. |
| 86 | */ |
| 87 | function ewww_image_optimizer_escapeshellcmd( $path ) { |
| 88 | return ewwwio()->escapeshellcmd( $path ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Replacement for escapeshellarg() that won't kill non-ASCII characters. |
| 93 | * |
| 94 | * @param string $arg A value to sanitize/escape for commmand-line usage. |
| 95 | * @return string The value after being escaped. |
| 96 | */ |
| 97 | function ewww_image_optimizer_escapeshellarg( $arg ) { |
| 98 | return ewwwio()->escapeshellarg( $arg ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Sanitize the folders/patterns to exclude from optimization. |
| 103 | * |
| 104 | * @param string $input A list of filesystem paths, from a textarea. |
| 105 | * @return array The sanitized list of paths/patterns to exclude. |
| 106 | */ |
| 107 | function ewww_image_optimizer_exclude_paths_sanitize( $input ) { |
| 108 | return ewwwio()->exclude_paths_sanitize( $input ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Checks if a function is disabled or does not exist. |
| 113 | * |
| 114 | * @param string $function_name The name of a function to test. |
| 115 | * @param bool $debug Whether to output debugging. |
| 116 | * @return bool True if the function is available, False if not. |
| 117 | */ |
| 118 | function ewww_image_optimizer_function_exists( $function_name, $debug = false ) { |
| 119 | return ewwwio()->function_exists( $function_name, $debug ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Make sure an array/object can be parsed by a foreach(). |
| 124 | * |
| 125 | * @param mixed $value A variable to test for iteration ability. |
| 126 | * @return bool True if the variable is iterable. |
| 127 | */ |
| 128 | function ewww_image_optimizer_iterable( $value ) { |
| 129 | return ewwwio()->is_iterable( $value ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Wrapper around size_format to remove the decimal from sizes in bytes. |
| 134 | * |
| 135 | * @param int $size A filesize in bytes. |
| 136 | * @param int $precision Number of places after the decimal separator. |
| 137 | * @return string Human-readable filesize. |
| 138 | */ |
| 139 | function ewww_image_optimizer_size_format( $size, $precision = 1 ) { |
| 140 | return ewwwio()->size_format( $size, $precision ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Checks if the S3 Uploads plugin is installed and active. |
| 145 | * |
| 146 | * @return bool True if it is fully active and rewriting/offloding media, false otherwise. |
| 147 | */ |
| 148 | function ewww_image_optimizer_s3_uploads_enabled() { |
| 149 | return ewwwio()->s3_uploads_enabled(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Adds information to the in-memory debug log. |
| 154 | * |
| 155 | * @param string $message Debug information to add to the log. |
| 156 | */ |
| 157 | function ewwwio_debug_message( $message ) { |
| 158 | ewwwio()->debug_message( $message ); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Saves the in-memory debug log to a logfile in the plugin folder. |
| 163 | */ |
| 164 | function ewww_image_optimizer_debug_log() { |
| 165 | ewwwio()->debug_log(); |
| 166 | } |
| 167 |