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() ) { |
||
44 | $this->set_excludes( $excludes ); |
||
45 | } |
||
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 ) { |
||
57 | |||
58 | if ( is_string( $excludes ) ) { |
||
59 | $excludes = explode( ',', $excludes ); |
||
|
|||
60 | } |
||
61 | |||
62 | $this->excludes = $excludes; |
||
63 | |||
64 | } |
||
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() { |
||
74 | return array_merge( $this->get_default_excludes(), $this->get_user_excludes() ); |
||
75 | } |
||
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() { |
||
109 | |||
110 | /** |
||
111 | * Get the user defined excludes. |
||
112 | * |
||
113 | * @return array The array of excludes. |
||
114 | */ |
||
115 | public function get_user_excludes() { |
||
116 | |||
117 | $excludes = $this->excludes; |
||
118 | |||
119 | // If path() is inside root(), exclude it |
||
120 | if ( strpos( Path::get_path(), Path::get_root() ) !== false && Path::get_root() !== Path::get_path() ) { |
||
121 | array_unshift( $excludes, trailingslashit( Path::get_path() ) ); |
||
122 | } |
||
123 | |||
124 | return $this->normalize( $excludes ); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get the array of default excludes. |
||
129 | * |
||
130 | * @return array The array of excludes. |
||
131 | */ |
||
132 | public function get_default_excludes() { |
||
133 | |||
134 | $excludes = array(); |
||
135 | |||
136 | // Back compat with the old Constant |
||
137 | if ( defined( 'HMBKP_EXCLUDE' ) && HMBKP_EXCLUDE ) { |
||
138 | $excludes = explode( ',', implode( ',', (array) HMBKP_EXCLUDE ) ); |
||
139 | } |
||
140 | |||
141 | $excludes = array_merge( $this->default_excludes, $excludes ); |
||
142 | |||
143 | /** |
||
144 | * Allow the default excludes list to be modified. |
||
145 | * |
||
146 | * @param $excludes The array of exclude rules. |
||
147 | */ |
||
148 | $excludes = apply_filters( 'hmbkp_default_excludes', $excludes ); |
||
149 | |||
150 | return $this->normalize( $excludes ); |
||
151 | |||
152 | } |
||
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 ) { |
||
211 | } |
||
212 |