PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.9.13
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.9.13
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / classes / class-imagify-files-recursive-iterator.php

class-imagify-files-recursive-iterator.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.9.13, at inc/classes/class-imagify-files-recursive-iterator.php

105 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.2';
21
22 /**
23 * Filesystem object.
24 *
25 * @var object Imagify_Filesystem
26 * @since 1.7.1
27 * @access protected
28 * @author Grégory Viguier
29 */
30 protected $filesystem;
31
32 /**
33 * Check whether the current element of the iterator is acceptable.
34 *
35 * @since 1.7.1
36 * @access public
37 * @author Grégory Viguier
38 *
39 * @param object $iterator The iterator that is being filtered.
40 */
41 public function __construct( $iterator ) {
42 parent::__construct( $iterator );
43 $this->filesystem = Imagify_Filesystem::get_instance();
44 }
45
46 /**
47 * Check whether the current element of the iterator is acceptable.
48 *
49 * @since 1.7
50 * @access public
51 * @author Grégory Viguier
52 *
53 * @return bool Returns whether the current element of the iterator is acceptable through this filter.
54 */
55 public function accept() {
56 static $extensions, $has_extension_method;
57
58 $file_path = $this->current()->getPathname();
59
60 // Prevent triggering an open_basedir restriction error.
61 $file_name = $this->filesystem->file_name( $file_path );
62
63 if ( '.' === $file_name || '..' === $file_name ) {
64 return false;
65 }
66
67 if ( $this->current()->isDir() ) {
68 $file_path = trailingslashit( $file_path );
69 }
70
71 if ( Imagify_Files_Scan::is_path_forbidden( $file_path ) ) {
72 return false;
73 }
74
75 // OK for folders.
76 if ( $this->hasChildren() ) {
77 return true;
78 }
79
80 // Only files.
81 if ( ! $this->current()->isFile() ) {
82 return false;
83 }
84
85 // Only files with the required extension.
86 if ( ! isset( $extensions ) ) {
87 $extensions = array_keys( imagify_get_mime_types() );
88 $extensions = implode( '|', $extensions );
89 }
90
91 if ( ! isset( $has_extension_method ) ) {
92 // This method was introduced in php 5.3.6.
93 $has_extension_method = method_exists( $this->current(), 'getExtension' );
94 }
95
96 if ( $has_extension_method ) {
97 $file_extension = strtolower( $this->current()->getExtension() );
98 } else {
99 $file_extension = strtolower( $this->filesystem->path_info( $file_path, 'extension' ) );
100 }
101
102 return preg_match( '@^' . $extensions . '$@', $file_extension );
103 }
104 }
105