class-excludes.php
3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace HM\BackUpWordPress;
/**
* Manages exclude rules
*/
class Excludes {
/**
* The array of exclude rules.
*
* @var array
*/
private $excludes;
/**
* The array of default exclude rules
*
* @var array
*/
private $default_excludes = array(
'.svn',
'_svn',
'CVS',
'_darcs',
'.arch-params',
'.monotone',
'.bzr',
'.git',
'.hg',
'backwpup-*',
'updraft',
'wp-snapshots',
'backupbuddy_backups',
'pb_backupbuddy',
'backup-db',
'Envato-backups',
'managewp',
'backupwordpress-*-backups',
);
public function __construct( $excludes = array() ) {
$this->set_excludes( $excludes );
}
/**
* Set the exclude rules.
*
* Excludes rules should be a complete or partial directory or file path.
* Wildcards can be specified with the * character.
*
* @param string|array $excludes The list of exclude rules, accepts either
* a comma separated list or an array.
*/
public function set_excludes( $excludes ) {
if ( is_string( $excludes ) ) {
$excludes = explode( ',', $excludes );
}
$this->excludes = $excludes;
}
/**
* Get the excludes
*
* Returns any user set excludes as well as the default list.
*
* @return array The array of exclude rules.
*/
public function get_excludes() {
return array_merge( $this->get_default_excludes(), $this->get_user_excludes() );
}
/**
* Get the excludes prepared for use with regex.
*
* The primary difference being that any wildcard (*) rules are converted to the regex
* fragment `[\s\S]*?`.
*
* @return array The array of exclude rules
*/
public function get_excludes_for_regex() {
$excludes = $this->get_excludes();
// Prepare the exclude rules
foreach ( $excludes as &$exclude ) {
if ( strpos( $exclude, '*' ) !== false ) {
// Escape slashes
$exclude = str_replace( '/', '\/', $exclude );
// Convert WildCards to regex
$exclude = str_replace( '*', '[\s\S]*?', $exclude );
// Wrap in slashes
$exclude = '/' . $exclude . '/';
}
}
return $excludes;
}
/**
* Get the user defined excludes.
*
* @return array The array of excludes.
*/
public function get_user_excludes() {
$excludes = $this->excludes;
// If path() is inside root(), exclude it
if ( strpos( Path::get_path(), Path::get_root() ) !== false && Path::get_root() !== Path::get_path() ) {
array_unshift( $excludes, trailingslashit( Path::get_path() ) );
}
return $this->normalize( $excludes );
}
/**
* Get the array of default excludes.
*
* @return array The array of excludes.
*/
public function get_default_excludes() {
$excludes = array();
// Back compat with the old Constant
if ( defined( 'HMBKP_EXCLUDE' ) && HMBKP_EXCLUDE ) {
$excludes = explode( ',', implode( ',', (array) HMBKP_EXCLUDE ) );
}
$excludes = array_merge( $this->default_excludes, $excludes );
/**
* Allow the default excludes list to be modified.
*
* @param $excludes The array of exclude rules.
*/
$excludes = apply_filters( 'hmbkp_default_excludes', $excludes );
return $this->normalize( $excludes );
}
/**
* normalise the exclude rules so they are ready to work with.
*
* @param array $excludes The array of exclude rules to normalise.
*
* @return array The array of normalised rules.
*/
public function normalize( $excludes ) {
$excludes = array_map(
function( $exclude ) {
// Convert absolute paths to relative
$exclude = str_replace( PATH::get_root(), '', wp_normalize_path( $exclude ) );
// Trim the slashes
$exclude = trim( $exclude );
$exclude = ltrim( $exclude, '/' );
$exclude = untrailingslashit( $exclude );
return $exclude;
},
$excludes );
// Remove duplicate or empty rules
$excludes = array_unique( $excludes );
$excludes = array_filter( $excludes );
return $excludes;
}
}