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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
<?php namespace Imagify\WriteFile;
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
/** * Abstract class used to add and remove contents to a directory conf file (.htaccess, etc). * * @since 1.9 * @author Grégory Viguier */ abstract class AbstractWriteDirConfFile implements WriteFileInterface {
/** * Name of the tag used as block delemiter. * * @var string * @since 1.9 * @author Grégory Viguier */ const TAG_NAME = 'Imagify ###';
/** * Filesystem object. * * @var \Imagify_Filesystem * @since 1.9 * @access protected * @author Grégory Viguier */ protected $filesystem;
/** * Constructor. * * @since 1.9 * @access public * @author Grégory Viguier */ public function __construct() { $this->filesystem = \Imagify_Filesystem::get_instance(); }
/** * Add new contents to the file. * * @since 1.9 * @access public * @author Grégory Viguier * * @return bool|\WP_Error True on success. A \WP_Error object on error. */ public function add() { $result = $this->insert_contents( $this->get_new_contents() );
if ( ! is_wp_error( $result ) ) { return true; } $file_path = $this->get_file_path(); $file_name = $this->filesystem->make_path_relative( $file_path );
if ( 'edition_disabled' === $result->get_error_code() ) { return new \WP_Error( 'edition_disabled', sprintf( /* translators: %s is a file name. */ __( 'Imagify did not add contents to the %s file, as its edition is disabled.', 'imagify' ), $file_name ) ); }
return new \WP_Error( 'add_contents_failure', sprintf( /* translators: 1 is a file name, 2 is an error message. */ __( 'Imagify could not insert contents into the %1$s file: %2$s', 'imagify' ), $file_name, $result->get_error_message() ), [ 'code' => $result->get_error_code() ] ); }
/** * Remove the related contents from the file. * * @since 1.9 * @access public * @author Grégory Viguier * * @return bool|\WP_Error True on success. A \WP_Error object on error. */ public function remove() { $result = $this->insert_contents( '' );
if ( ! is_wp_error( $result ) ) { return true; }
$file_name = $this->filesystem->make_path_relative( $file_path );
if ( 'edition_disabled' === $result->get_error_code() ) { return new \WP_Error( 'edition_disabled', sprintf( /* translators: %s is a file name. */ __( 'Imagify did not remove the contents from the %s file, as its edition is disabled.', 'imagify' ), $file_name ) ); }
return new \WP_Error( 'add_contents_failure', sprintf( /* translators: 1 is a file name, 2 is an error message. */ __( 'Imagify could not remove contents from the %1$s file: %2$s', 'imagify' ), $file_name, $result->get_error_message() ), [ 'code' => $result->get_error_code() ] ); }
/** * Get the path to the file. * * @since 1.9 * @access public * @author Grégory Viguier * * @return string */ public function get_file_path() { $file_path = $this->get_raw_file_path();
/** * Filter the path to the directory conf file. * * @since 1.9 * @author Grégory Viguier * * @param string $file_path Path to the file. */ $new_file_path = apply_filters( 'imagify_dir_conf_path', $file_path );
if ( $new_file_path && is_string( $new_file_path ) ) { return $new_file_path; }
return $file_path; }
/** * Tell if the file is writable. * * @since 1.9 * @access public * @author Grégory Viguier * * @return bool|\WP_Error True if writable. A \WP_Error object if not. */ public function is_file_writable() { $file_path = $this->get_file_path(); $file_name = $this->filesystem->make_path_relative( $file_path );
if ( $this->is_conf_edition_disabled() ) { return new \WP_Error( 'edition_disabled', sprintf( /* translators: %s is a file name. */ __( 'Edition of the %s file is disabled.', 'imagify' ), '<code>' . esc_html( $file_name ) . '</code>' ) ); }
if ( ! $this->filesystem->exists( $file_path ) ) { $dir_path = $this->filesystem->dir_path( $file_path );
$this->filesystem->make_dir( $dir_path );
if ( ! $this->filesystem->is_writable( $dir_path ) ) { return new \WP_Error( 'parent_not_writable', sprintf( /* translators: %s is a file name. */ __( '%s’s parent folder is not writable.', 'imagify' ), '<code>' . esc_html( $file_name ) . '</code>' ) ); } if ( ! $this->filesystem->touch( $file_path ) ) { return new \WP_Error( 'not_created', sprintf( /* translators: %s is a file name. */ __( 'The %s file could not be created.', 'imagify' ), '<code>' . esc_html( $file_name ) . '</code>' ) ); } } elseif ( ! $this->filesystem->is_writable( $file_path ) ) { return new \WP_Error( 'not_writable', sprintf( /* translators: %s is a file name. */ __( 'The %s file is not writable.', 'imagify' ), '<code>' . esc_html( $file_name ) . '</code>' ) ); }
return true; }
/** * Get new contents to write into the file. * * @since 1.9 * @access public * @author Grégory Viguier * * @return string */ public function get_new_contents() { $contents = $this->get_raw_new_contents();
/** * Filter the contents to add to the directory conf file. * * @since 1.9 * @author Grégory Viguier * * @param string $contents The contents. */ $new_contents = apply_filters( 'imagify_dir_conf_contents', $contents );
if ( $new_contents && is_string( $new_contents ) ) { return $new_contents; }
return $contents; }
/** ----------------------------------------------------------------------------------------- */ /** ABSTRACT METHODS ======================================================================== */ /** ----------------------------------------------------------------------------------------- */
/** * Insert new contents into the directory conf file. * Replaces existing marked info. Creates file if none exists. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param string $new_contents Contents to insert. * @return bool|\WP_Error True on write success, a \WP_Error object on failure. */ abstract protected function insert_contents( $new_contents );
/** * Get the unfiltered path to the file. * * @since 1.9 * @access protected * @author Grégory Viguier * * @return string */ abstract protected function get_raw_file_path();
/** * Get unfiltered new contents to write into the file. * * @since 1.9 * @access protected * @author Grégory Viguier * * @return string */ abstract protected function get_raw_new_contents();
/** ----------------------------------------------------------------------------------------- */ /** OTHER TOOLS ============================================================================= */ /** ----------------------------------------------------------------------------------------- */
/** * Get the file contents. * * @since 1.9 * @access protected * @author Grégory Viguier * * @return mixed|\WP_Error The file contents on success, a \WP_Error object on failure. */ protected function get_file_contents() { $writable = $this->is_file_writable();
if ( is_wp_error( $writable ) ) { return $writable; }
$file_path = $this->get_file_path();
if ( ! $this->filesystem->exists( $file_path ) ) { // This should not happen. return ''; }
$contents = $this->filesystem->get_contents( $file_path );
if ( false === $contents ) { return new \WP_Error( 'not_read', sprintf( /* translators: %s is a file name. */ __( 'The %s file could not be read.', 'imagify' ), '<code>' . esc_html( $file_name ) . '</code>' ) ); }
return $contents; }
/** * Put new contents into the file. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param string $contents New contents to add to the file. * @return bool|\WP_Error True on success, a \WP_Error object on failure. */ protected function put_file_contents( $contents ) { $file_path = $this->get_file_path(); $result = $this->filesystem->put_contents( $file_path, $contents );
if ( $result ) { return true; }
$file_name = $this->filesystem->make_path_relative( $file_path );
return new \WP_Error( 'edition_failed', sprintf( /* translators: %s is a file name. */ __( 'Could not write into the %s file.', 'imagify' ), '<code>' . esc_html( $file_name ) . '</code>' ) ); }
/** * Tell if edition of the directory conf file is disabled. * * @since 1.9 * @access public * @author Grégory Viguier * * @return bool True to disable, false otherwise. */ protected function is_conf_edition_disabled() { /** * Disable directory conf edition. * * @since 1.9 * @author Grégory Viguier * * @param bool $disable True to disable, false otherwise. */ return (bool) apply_filters( 'imagify_disable_dir_conf_edition', false ); }
/** * Get a regex pattern to be used to match the supported file extensions. * * @since 1.9 * @access protected * @author Grégory Viguier * * @return string */ protected function get_extensions_pattern() { $extensions = imagify_get_mime_types( 'image' ); $extensions = array_keys( $extensions );
return implode( '|', $extensions ); } }
|