PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / system / class-file.php

class-file.php in DecaLog 4.4.0, at includes/system/class-file.php

92 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Files handling
4 *
5 * Handles all files operations and detection.
6 *
7 * @package System
8 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
9 * @since 1.0.0
10 */
11
12 namespace Decalog\System;
13
14 /**
15 * Define the files functionality.
16 *
17 * Handles all files operations and detection.
18 *
19 * @package System
20 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
21 * @since 1.0.0
22 */
23 class File {
24
25 /**
26 * Initializes the class and set its properties.
27 *
28 * @since 1.0.0
29 */
30 public function __construct() {
31 }
32
33 /**
34 * Lists files.
35 *
36 * @param string $folder Optional. The starting folder.
37 * @param integer $levels Optional. Level of recursion (1 = no recursion).
38 * @param array $file_include Optional. Include list (regex) for file whitelist mode.
39 * @param array $file_exclude Optional. Exclude list (regex) for file blacklist mode.
40 * @param boolean $hidden Optional. List hidden files too.
41 * @return array The list of files.
42 * @since 1.0.0
43 */
44 public static function list_files( $folder = ABSPATH, $levels = 100, $file_include = [], $file_exclude = [], $hidden = false ) {
45 if ( empty( $folder ) || ! $levels ) {
46 return [];
47 }
48 $folder = trailingslashit( $folder );
49 $files = [];
50 // phpcs:ignore
51 $dir = @opendir( $folder );
52 if ( $dir ) {
53 while ( false !== ( $file = readdir( $dir ) ) ) {
54 if ( in_array( $file, [ '.', '..' ], true ) ) {
55 continue;
56 }
57 if ( ! $hidden && '.' === $file[0] ) {
58 continue;
59 }
60 if ( is_dir( $folder . $file ) ) {
61 $files = array_merge( $files, self::list_files( $folder . $file, $levels - 1, $file_include, $file_exclude, $hidden ) );
62 } else {
63 if ( 0 < count( $file_include ) ) {
64 $continue = true;
65 foreach ( $file_include as $rule ) {
66 if ( preg_match( $rule, $folder . $file ) ) {
67 $continue = false;
68 break;
69 }
70 }
71 if ( $continue ) {
72 continue;
73 }
74 }
75 if ( 0 < count( $file_exclude ) ) {
76 foreach ( $file_exclude as $rule ) {
77 if ( preg_match( $rule, $folder . $file ) ) {
78 continue;
79 }
80 }
81 }
82 $files[] = $folder . $file;
83 }
84 }
85 }
86 // phpcs:ignore
87 @closedir( $dir );
88 return $files;
89 }
90
91 }
92