| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
/** |
| 5 |
* Class allowing to filter RecursiveDirectoryIterator, to return only files that Imagify can optimize. |
| 6 |
* It also allows to remove forbidden folders. |
| 7 |
* |
| 8 |
* @since 1.7 |
| 9 |
* @author Grégory Viguier |
| 10 |
*/ |
| 11 |
class Imagify_Files_Recursive_Iterator extends RecursiveFilterIterator { |
| 12 |
|
| 13 |
/** |
| 14 |
* Class version. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
* @since 1.7 |
| 18 |
* @author Grégory Viguier |
| 19 |
*/ |
| 20 |
const VERSION = '1.0'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Check whether the current element of the iterator is acceptable. |
| 24 |
* |
| 25 |
* @since 1.7 |
| 26 |
* @access public |
| 27 |
* @author Grégory Viguier |
| 28 |
* |
| 29 |
* @return bool Returns whether the current element of the iterator is acceptable through this filter. |
| 30 |
*/ |
| 31 |
public function accept() { |
| 32 |
static $extensions, $has_extension_method; |
| 33 |
|
| 34 |
// Forbidden file/folder paths and names. |
| 35 |
$file_path = $this->current()->getPathname(); |
| 36 |
|
| 37 |
if ( Imagify_Files_Scan::is_path_forbidden( $file_path ) ) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
// OK for folders. |
| 42 |
if ( $this->hasChildren() ) { |
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
// Only files. |
| 47 |
if ( ! $this->current()->isFile() ) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
// Only files with the required extension. |
| 52 |
if ( ! isset( $extensions ) ) { |
| 53 |
$extensions = array_keys( imagify_get_mime_types() ); |
| 54 |
$extensions = implode( '|', $extensions ); |
| 55 |
} |
| 56 |
|
| 57 |
if ( ! isset( $has_extension_method ) ) { |
| 58 |
// This method was introduced in php 5.3.6. |
| 59 |
$has_extension_method = method_exists( $this->current(), 'getExtension' ); |
| 60 |
} |
| 61 |
|
| 62 |
if ( $has_extension_method ) { |
| 63 |
$file_extension = strtolower( $this->current()->getExtension() ); |
| 64 |
} else { |
| 65 |
$file_extension = strtolower( pathinfo( $file_path, PATHINFO_EXTENSION ) ); |
| 66 |
} |
| 67 |
|
| 68 |
return preg_match( '@^' . $extensions . '$@', $file_extension ); |
| 69 |
} |
| 70 |
} |
| 71 |
|