| 1 |
<?php |
| 2 |
|
| 3 |
use Imagify\User\User; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 6 |
|
| 7 |
/** |
| 8 |
* Class used to check that Imagify has everything it needs. |
| 9 |
* |
| 10 |
* @since 1.7.1 |
| 11 |
* @author Grégory Viguier |
| 12 |
*/ |
| 13 |
class Imagify_Requirements { |
| 14 |
/** |
| 15 |
* Cache the test results. |
| 16 |
* |
| 17 |
* @var object |
| 18 |
* @access protected |
| 19 |
* @since 1.7.1 |
| 20 |
* @author Grégory Viguier |
| 21 |
*/ |
| 22 |
protected static $supports = []; |
| 23 |
|
| 24 |
|
| 25 |
/** ----------------------------------------------------------------------------------------- */ |
| 26 |
/** SERVER ================================================================================== */ |
| 27 |
/** ----------------------------------------------------------------------------------------- */ |
| 28 |
|
| 29 |
/** |
| 30 |
* Test for cURL. |
| 31 |
* |
| 32 |
* @since 1.7.1 |
| 33 |
* @access public |
| 34 |
* @author Grégory Viguier |
| 35 |
* |
| 36 |
* @param bool $reset_cache True to get a fresh value. |
| 37 |
* @return bool |
| 38 |
*/ |
| 39 |
public static function supports_curl( $reset_cache = false ) { |
| 40 |
if ( $reset_cache || ! isset( self::$supports['curl'] ) ) { |
| 41 |
self::$supports['curl'] = function_exists( 'curl_init' ) && function_exists( 'curl_exec' ); |
| 42 |
} |
| 43 |
|
| 44 |
return self::$supports['curl']; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Test for imageMagick and GD. |
| 49 |
* Similar to _wp_image_editor_choose(), but allows to test for multiple mime types at once. |
| 50 |
* |
| 51 |
* @since 1.7.1 |
| 52 |
* @access public |
| 53 |
* @see _wp_image_editor_choose() |
| 54 |
* @author Grégory Viguier |
| 55 |
* |
| 56 |
* @param bool $reset_cache True to get a fresh value. |
| 57 |
* @return bool |
| 58 |
*/ |
| 59 |
public static function supports_image_editor( $reset_cache = false ) { |
| 60 |
if ( ! $reset_cache && isset( self::$supports['image_editor'] ) ) { |
| 61 |
return self::$supports['image_editor']; |
| 62 |
} |
| 63 |
|
| 64 |
require_once ABSPATH . WPINC . '/class-wp-image-editor.php'; |
| 65 |
require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php'; |
| 66 |
require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php'; |
| 67 |
|
| 68 |
self::$supports['image_editor'] = false; |
| 69 |
|
| 70 |
$args = [ |
| 71 |
'path' => IMAGIFY_PATH . 'assets/images/imagify-logo.png', |
| 72 |
'mime_types' => imagify_get_mime_types( 'image' ), |
| 73 |
'methods' => Imagify_Attachment::get_editor_methods(), |
| 74 |
]; |
| 75 |
|
| 76 |
/** This filter is documented in /wp-includes/media.php. */ |
| 77 |
$implementations = apply_filters( 'wp_image_editors', [ 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ] ); |
| 78 |
|
| 79 |
foreach ( $implementations as $implementation ) { |
| 80 |
if ( ! call_user_func( [ $implementation, 'test' ], $args ) ) { |
| 81 |
continue; |
| 82 |
} |
| 83 |
|
| 84 |
foreach ( $args['mime_types'] as $mime_type ) { |
| 85 |
if ( ! call_user_func( [ $implementation, 'supports_mime_type' ], $mime_type ) ) { |
| 86 |
continue 2; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if ( array_diff( $args['methods'], get_class_methods( $implementation ) ) ) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
self::$supports['image_editor'] = true; |
| 95 |
break; |
| 96 |
} |
| 97 |
|
| 98 |
return self::$supports['image_editor']; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Test for the uploads directory. |
| 103 |
* |
| 104 |
* @since 1.7.1 |
| 105 |
* @access public |
| 106 |
* @author Grégory Viguier |
| 107 |
* |
| 108 |
* @param bool $reset_cache True to get a fresh value. |
| 109 |
* @return bool |
| 110 |
*/ |
| 111 |
public static function supports_uploads( $reset_cache = false ) { |
| 112 |
if ( ! $reset_cache && isset( self::$supports['uploads'] ) ) { |
| 113 |
return self::$supports['uploads']; |
| 114 |
} |
| 115 |
|
| 116 |
self::$supports['uploads'] = Imagify_Filesystem::get_instance()->get_upload_basedir(); |
| 117 |
|
| 118 |
if ( self::$supports['uploads'] ) { |
| 119 |
self::$supports['uploads'] = Imagify_Filesystem::get_instance()->is_writable( self::$supports['uploads'] ); |
| 120 |
} |
| 121 |
|
| 122 |
return self::$supports['uploads']; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Test if external requests are blocked for Imagify. |
| 127 |
* |
| 128 |
* @since 1.7.1 |
| 129 |
* @access public |
| 130 |
* @author Grégory Viguier |
| 131 |
* |
| 132 |
* @param bool $reset_cache True to get a fresh value. |
| 133 |
* @return bool |
| 134 |
*/ |
| 135 |
public static function is_imagify_blocked( $reset_cache = false ) { |
| 136 |
if ( ! $reset_cache && isset( self::$supports['imagify_blocked'] ) ) { |
| 137 |
return self::$supports['imagify_blocked']; |
| 138 |
} |
| 139 |
|
| 140 |
if ( ! defined( 'WP_HTTP_BLOCK_EXTERNAL' ) || ! WP_HTTP_BLOCK_EXTERNAL ) { |
| 141 |
self::$supports['imagify_blocked'] = false; |
| 142 |
return self::$supports['imagify_blocked']; |
| 143 |
} |
| 144 |
|
| 145 |
if ( ! defined( 'WP_ACCESSIBLE_HOSTS' ) ) { |
| 146 |
self::$supports['imagify_blocked'] = true; |
| 147 |
return self::$supports['imagify_blocked']; |
| 148 |
} |
| 149 |
|
| 150 |
$accessible_hosts = explode( ',', WP_ACCESSIBLE_HOSTS ); |
| 151 |
$accessible_hosts = array_map( 'trim', $accessible_hosts ); |
| 152 |
$accessible_hosts = array_flip( $accessible_hosts ); |
| 153 |
|
| 154 |
if ( isset( $accessible_hosts['*.imagify.io'] ) ) { |
| 155 |
self::$supports['imagify_blocked'] = false; |
| 156 |
return self::$supports['imagify_blocked']; |
| 157 |
} |
| 158 |
|
| 159 |
if ( isset( $accessible_hosts['imagify.io'], $accessible_hosts['app.imagify.io'], $accessible_hosts['storage.imagify.io'] ) ) { |
| 160 |
self::$supports['imagify_blocked'] = false; |
| 161 |
return self::$supports['imagify_blocked']; |
| 162 |
} |
| 163 |
|
| 164 |
self::$supports['imagify_blocked'] = true; |
| 165 |
return self::$supports['imagify_blocked']; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
/** ----------------------------------------------------------------------------------------- */ |
| 170 |
/** IMAGIFY BACKUP DIRECTORIES ============================================================== */ |
| 171 |
/** ----------------------------------------------------------------------------------------- */ |
| 172 |
|
| 173 |
/** |
| 174 |
* Test for the attachments backup directory. |
| 175 |
* |
| 176 |
* @since 1.7.1 |
| 177 |
* @access public |
| 178 |
* @author Grégory Viguier |
| 179 |
* |
| 180 |
* @param bool $reset_cache True to get a fresh value. |
| 181 |
* @return bool |
| 182 |
*/ |
| 183 |
public static function attachments_backup_dir_is_writable( $reset_cache = false ) { |
| 184 |
if ( $reset_cache || ! isset( self::$supports['attachment_backups'] ) ) { |
| 185 |
self::$supports['attachment_backups'] = imagify_backup_dir_is_writable(); |
| 186 |
} |
| 187 |
|
| 188 |
return self::$supports['attachment_backups']; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Test for the custom folders backup directory. |
| 193 |
* |
| 194 |
* @since 1.7.1 |
| 195 |
* @access public |
| 196 |
* @author Grégory Viguier |
| 197 |
* |
| 198 |
* @param bool $reset_cache True to get a fresh value. |
| 199 |
* @return bool |
| 200 |
*/ |
| 201 |
public static function custom_folders_backup_dir_is_writable( $reset_cache = false ) { |
| 202 |
if ( $reset_cache || ! isset( self::$supports['custom_folder_backups'] ) ) { |
| 203 |
self::$supports['custom_folder_backups'] = Imagify_Custom_Folders::backup_dir_is_writable(); |
| 204 |
} |
| 205 |
|
| 206 |
return self::$supports['custom_folder_backups']; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
/** ----------------------------------------------------------------------------------------- */ |
| 211 |
/** IMAGIFY API ============================================================================= */ |
| 212 |
/** ----------------------------------------------------------------------------------------- */ |
| 213 |
|
| 214 |
/** |
| 215 |
* Determine if the Imagify API is available by checking the API version. |
| 216 |
* The result is cached for 3 minutes. |
| 217 |
* |
| 218 |
* @since 1.7.1 |
| 219 |
* @access public |
| 220 |
* @author Grégory Viguier |
| 221 |
* |
| 222 |
* @param bool $reset_cache True to get a fresh value. |
| 223 |
* @return bool |
| 224 |
*/ |
| 225 |
public static function is_api_up( $reset_cache = false ) { |
| 226 |
if ( ! $reset_cache && isset( self::$supports['api_up'] ) ) { |
| 227 |
return self::$supports['api_up']; |
| 228 |
} |
| 229 |
|
| 230 |
$transient_name = 'imagify_check_api_version'; |
| 231 |
$transient_expiration = 3 * MINUTE_IN_SECONDS; |
| 232 |
$transient_value = $reset_cache ? false : get_site_transient( $transient_name ); |
| 233 |
|
| 234 |
if ( false !== $transient_value ) { |
| 235 |
self::$supports['api_up'] = (bool) $transient_value; |
| 236 |
return self::$supports['api_up']; |
| 237 |
} |
| 238 |
|
| 239 |
self::$supports['api_up'] = ! is_wp_error( get_imagify_api_version() ); |
| 240 |
$transient_value = (int) self::$supports['api_up']; |
| 241 |
|
| 242 |
set_site_transient( $transient_name, $transient_value, $transient_expiration ); |
| 243 |
|
| 244 |
return self::$supports['api_up']; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Test for the Imagify API key validity. |
| 249 |
* A positive result is cached for 1 year. |
| 250 |
* |
| 251 |
* @since 1.7.1 |
| 252 |
* @access public |
| 253 |
* @author Grégory Viguier |
| 254 |
* |
| 255 |
* @param bool $reset_cache True to get a fresh value. |
| 256 |
* @return bool |
| 257 |
*/ |
| 258 |
public static function is_api_key_valid( $reset_cache = false ) { |
| 259 |
if ( $reset_cache ) { |
| 260 |
self::reset_cache( 'api_key_valid' ); |
| 261 |
} |
| 262 |
|
| 263 |
if ( isset( self::$supports['api_key_valid'] ) ) { |
| 264 |
return self::$supports['api_key_valid']; |
| 265 |
} |
| 266 |
|
| 267 |
if ( ! Imagify_Options::get_instance()->get( 'api_key' ) ) { |
| 268 |
self::$supports['api_key_valid'] = false; |
| 269 |
return self::$supports['api_key_valid']; |
| 270 |
} |
| 271 |
|
| 272 |
if ( get_site_transient( 'imagify_check_licence_1' ) ) { |
| 273 |
self::$supports['api_key_valid'] = true; |
| 274 |
return self::$supports['api_key_valid']; |
| 275 |
} |
| 276 |
|
| 277 |
if ( is_wp_error( get_imagify_user() ) ) { |
| 278 |
self::$supports['api_key_valid'] = false; |
| 279 |
return self::$supports['api_key_valid']; |
| 280 |
} |
| 281 |
|
| 282 |
self::$supports['api_key_valid'] = true; |
| 283 |
set_site_transient( 'imagify_check_licence_1', 1, YEAR_IN_SECONDS ); |
| 284 |
|
| 285 |
return self::$supports['api_key_valid']; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Test for the Imagify account quota. |
| 290 |
* |
| 291 |
* @since 1.7.1 |
| 292 |
* @since 1.9.9 Return false when the API cannot be reached. |
| 293 |
* @access public |
| 294 |
* @author Grégory Viguier |
| 295 |
* |
| 296 |
* @param bool $reset_cache True to get a fresh value. |
| 297 |
* @return bool True when over quota. False otherwise, even when the API cannot be reached. |
| 298 |
*/ |
| 299 |
public static function is_over_quota( $reset_cache = false ) { |
| 300 |
if ( ! $reset_cache && isset( self::$supports['over_quota'] ) ) { |
| 301 |
return self::$supports['over_quota']; |
| 302 |
} |
| 303 |
|
| 304 |
$user = new User(); |
| 305 |
|
| 306 |
self::$supports['over_quota'] = $user->get_error() ? false : $user->is_over_quota(); |
| 307 |
|
| 308 |
return self::$supports['over_quota']; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
/** ----------------------------------------------------------------------------------------- */ |
| 313 |
/** CLASS CACHE ============================================================================= */ |
| 314 |
/** ----------------------------------------------------------------------------------------- */ |
| 315 |
|
| 316 |
/** |
| 317 |
* Reset a test cache. |
| 318 |
* |
| 319 |
* @since 1.7.1 |
| 320 |
* @access public |
| 321 |
* @author Grégory Viguier |
| 322 |
* |
| 323 |
* @param string $cache_key Cache key. |
| 324 |
*/ |
| 325 |
public static function reset_cache( $cache_key ) { |
| 326 |
unset( self::$supports[ $cache_key ] ); |
| 327 |
|
| 328 |
$transients = [ |
| 329 |
'api_up' => 'imagify_check_api_version', |
| 330 |
'api_key_valid' => 'imagify_check_licence_1', |
| 331 |
]; |
| 332 |
|
| 333 |
if ( isset( $transients[ $cache_key ] ) && get_site_transient( $transients[ $cache_key ] ) ) { |
| 334 |
delete_site_transient( $transients[ $cache_key ] ); |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|