| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
/** |
| 5 |
* Class allowing to filter DirectoryIterator, to return only files that Imagify can optimize and folders. |
| 6 |
* It also allows to remove forbidden folders. |
| 7 |
* |
| 8 |
* @since 1.7 |
| 9 |
* @author Grégory Viguier |
| 10 |
*/ |
| 11 |
class Imagify_Files_Iterator extends FilterIterator { |
| 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 |
* Tell if the iterator will return both folders and images, or only images. |
| 24 |
* |
| 25 |
* @var bool |
| 26 |
* @since 1.7 |
| 27 |
*/ |
| 28 |
protected $include_folders; |
| 29 |
|
| 30 |
/** |
| 31 |
* Check whether the current element of the iterator is acceptable. |
| 32 |
* |
| 33 |
* @since 1.7 |
| 34 |
* @access public |
| 35 |
* @author Grégory Viguier |
| 36 |
* |
| 37 |
* @param object $iterator The iterator that is being filtered. |
| 38 |
* @param bool $include_folders True to return both folders and images. False to return only images. |
| 39 |
*/ |
| 40 |
public function __construct( $iterator, $include_folders = true ) { |
| 41 |
parent::__construct( $iterator ); |
| 42 |
$this->include_folders = (bool) $include_folders; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Check whether the current element of the iterator is acceptable. |
| 47 |
* |
| 48 |
* @since 1.7 |
| 49 |
* @access public |
| 50 |
* @author Grégory Viguier |
| 51 |
* |
| 52 |
* @return bool Returns whether the current element of the iterator is acceptable through this filter. |
| 53 |
*/ |
| 54 |
public function accept() { |
| 55 |
static $extensions, $has_extension_method; |
| 56 |
|
| 57 |
// Forbidden file/folder paths and names. |
| 58 |
$file_path = $this->current()->getPathname(); |
| 59 |
|
| 60 |
if ( Imagify_Files_Scan::is_path_forbidden( $file_path ) ) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
// OK for folders. |
| 65 |
if ( $this->include_folders && $this->isDir() ) { |
| 66 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
// Only files. |
| 70 |
if ( ! $this->current()->isFile() ) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
// Only files with the required extension. |
| 75 |
if ( ! isset( $extensions ) ) { |
| 76 |
$extensions = array_keys( imagify_get_mime_types() ); |
| 77 |
$extensions = implode( '|', $extensions ); |
| 78 |
} |
| 79 |
|
| 80 |
if ( ! isset( $has_extension_method ) ) { |
| 81 |
// This method was introduced in php 5.3.6. |
| 82 |
$has_extension_method = method_exists( $this->current(), 'getExtension' ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( $has_extension_method ) { |
| 86 |
$file_extension = strtolower( $this->current()->getExtension() ); |
| 87 |
} else { |
| 88 |
$file_extension = strtolower( pathinfo( $file_path, PATHINFO_EXTENSION ) ); |
| 89 |
} |
| 90 |
|
| 91 |
return preg_match( '@^' . $extensions . '$@', $file_extension ); |
| 92 |
} |
| 93 |
} |
| 94 |
|