1 | <?php |
||
8 | class Excludes { |
||
9 | |||
10 | /** |
||
11 | * The array of exclude rules. |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | private $excludes; |
||
16 | |||
17 | /** |
||
18 | * The array of default exclude rules |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | private $default_excludes = array( |
||
23 | '.svn', |
||
24 | '_svn', |
||
25 | 'CVS', |
||
26 | '_darcs', |
||
27 | '.arch-params', |
||
28 | '.monotone', |
||
29 | '.bzr', |
||
30 | '.git', |
||
31 | '.hg', |
||
32 | 'backwpup-*', |
||
33 | 'updraft', |
||
34 | 'wp-snapshots', |
||
35 | 'backupbuddy_backups', |
||
36 | 'pb_backupbuddy', |
||
37 | 'backup-db', |
||
38 | 'Envato-backups', |
||
39 | 'managewp', |
||
40 | 'backupwordpress-*-backups', |
||
41 | ); |
||
42 | |||
43 | public function __construct( $excludes = array() ) { |
||
46 | |||
47 | /** |
||
48 | * Set the exclude rules. |
||
49 | * |
||
50 | * Excludes rules should be a complete or partial directory or file path. |
||
51 | * Wildcards can be specified with the * character. |
||
52 | * |
||
53 | * @param string|array $excludes The list of exclude rules, accepts either |
||
54 | * a comma separated list or an array. |
||
55 | */ |
||
56 | public function set_excludes( $excludes ) { |
||
65 | |||
66 | /** |
||
67 | * Get the excludes |
||
68 | * |
||
69 | * Returns any user set excludes as well as the default list. |
||
70 | * |
||
71 | * @return array The array of exclude rules. |
||
72 | */ |
||
73 | public function get_excludes() { |
||
76 | |||
77 | /** |
||
78 | * Get the excludes prepared for use with regex. |
||
79 | * |
||
80 | * The primary difference being that any wildcard (*) rules are converted to the regex |
||
81 | * fragment `[\s\S]*?`. |
||
82 | * |
||
83 | * @return array The array of exclude rules |
||
84 | */ |
||
85 | public function get_excludes_for_regex() { |
||
86 | |||
87 | $excludes = $this->get_excludes(); |
||
88 | |||
89 | // Prepare the exclude rules |
||
90 | foreach ( $excludes as &$exclude ) { |
||
91 | |||
92 | if ( strpos( $exclude, '*' ) !== false ) { |
||
93 | |||
94 | // Escape slashes |
||
95 | $exclude = str_replace( '/', '\/', $exclude ); |
||
96 | |||
97 | // Convert WildCards to regex |
||
98 | $exclude = str_replace( '*', '[\s\S]*?', $exclude ); |
||
99 | |||
100 | // Wrap in slashes |
||
101 | $exclude = '/' . $exclude . '/'; |
||
102 | |||
103 | } |
||
104 | } |
||
105 | |||
106 | return $excludes; |
||
107 | |||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Get the user defined excludes. |
||
112 | * |
||
113 | * @return array The array of excludes. |
||
114 | */ |
||
115 | public function get_user_excludes() { |
||
126 | |||
127 | /** |
||
128 | * Get the array of default excludes. |
||
129 | * |
||
130 | * @return array The array of excludes. |
||
131 | */ |
||
132 | public function get_default_excludes() { |
||
153 | |||
154 | /** |
||
155 | * normalise the exclude rules so they are ready to work with. |
||
156 | * |
||
157 | * @param array $excludes The array of exclude rules to normalise. |
||
158 | * |
||
159 | * @return array The array of normalised rules. |
||
160 | */ |
||
161 | public function normalize( $excludes ) { |
||
186 | |||
187 | /** |
||
188 | * Check if a file is excluded. |
||
189 | * i.e. excluded directly or is in an excluded folder. |
||
190 | * |
||
191 | * @param \SplFileInfo $file File to check if it's excluded. |
||
192 | * |
||
193 | * @return bool|null True if file is excluded, false otherwise. |
||
194 | * Null - if it's not a file. |
||
195 | */ |
||
196 | public function is_file_excluded( \SplFileInfo $file ) { |
||
207 | } |
||
208 |