1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Admin Page Framework Loader |
4
|
|
|
* |
5
|
|
|
* http://admin-page-framework.michaeluno.jp/ |
6
|
|
|
* Copyright (c) 2013-2022, Michael Uno; Licensed GPLv2 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Adds the 'Compiler' section to the 'Compiler' tab. |
11
|
|
|
* |
12
|
|
|
* @since 3.5.4 |
13
|
|
|
*/ |
14
|
|
|
class AdminPageFrameworkLoader_AdminPage_Tool_Compiler_Compiler extends AdminPageFrameworkLoader_AdminPage_Section_Base { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Stores the admin page factory object. |
18
|
|
|
* @var AdminPageFramework |
19
|
|
|
*/ |
20
|
|
|
public $oFactory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A user constructor. |
24
|
|
|
* |
25
|
|
|
* @param AdminPageFramework $oFactory |
26
|
|
|
* @since 3.5.4 |
27
|
|
|
*/ |
28
|
|
|
protected function construct( $oFactory ) { |
29
|
|
|
|
30
|
|
|
// Store the factory object in a property. |
31
|
|
|
$this->oFactory = $oFactory; |
32
|
|
|
|
33
|
|
|
add_action( |
34
|
|
|
'export_name_' . $this->sPageSlug . '_' . $this->sTabSlug, |
35
|
|
|
array( $this, 'replyToFilterFileName' ), |
36
|
|
|
10, |
37
|
|
|
5 |
38
|
|
|
); |
39
|
|
|
add_action( |
40
|
|
|
// export_{instantiated class name}_{section id}_{field id} |
41
|
|
|
"export_{$oFactory->oProp->sClassName}_{$this->sSectionID}_download", |
42
|
|
|
array( $this, 'replyToDownloadFramework' ), |
43
|
|
|
10, |
44
|
|
|
4 |
45
|
|
|
); |
46
|
|
|
add_action( |
47
|
|
|
"export_header_{$oFactory->oProp->sClassName}_{$this->sSectionID}", |
48
|
|
|
array( $this, 'replyToGetExportHTTPHeaderModified' ), |
49
|
|
|
10, |
50
|
|
|
6 |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Adds form fields. |
57
|
|
|
* @since 3.5.4 |
58
|
|
|
*/ |
59
|
|
|
public function addFields( $oFactory, $sSectionID ) { |
60
|
|
|
|
61
|
|
|
$oFactory->addSettingFields( |
62
|
|
|
$sSectionID, // the target section id |
63
|
|
|
array( |
64
|
|
|
'field_id' => 'version', |
65
|
|
|
'title' => __( 'Version', 'admin-page-framework-loader' ), |
66
|
|
|
'type' => 'text', |
67
|
|
|
'save' => false, |
68
|
|
|
'value' => AdminPageFramework_Registry::VERSION, |
69
|
|
|
'attributes' => array( |
70
|
|
|
'size' => 20, |
71
|
|
|
'readonly' => 'readonly', |
72
|
|
|
), |
73
|
|
|
), |
74
|
|
|
array( |
75
|
|
|
'field_id' => 'class_prefix', |
76
|
|
|
'title' => __( 'Class Prefix', 'admin-page-framework-loader' ), |
77
|
|
|
'type' => 'text', |
78
|
|
|
'tip' => array( |
79
|
|
|
__( 'Set alphanumeric characters for the class names.', 'admin-page-framework-loader' ), |
80
|
|
|
__( 'For example, if you set here <code>MyPluginName_</code>, you will need to extend the class named <code>MyClassName_AdminPageFramework</code> instead of <code>AdminPageFramework</code>.', 'admin-page-framework-loader' ), |
81
|
|
|
), |
82
|
|
|
'description' => 'e.g.<code>MyPluginName_</code>', |
83
|
|
|
'attributes' => array( |
84
|
|
|
'size' => 30, |
85
|
|
|
// 'required' => 'required', |
86
|
|
|
'placeholder' => __( 'Type a prefix.', 'admin-page-framework-loader' ), |
87
|
|
|
), |
88
|
|
|
), |
89
|
|
|
array( |
90
|
|
|
'field_id' => 'text_domain', |
91
|
|
|
'title' => __( 'Text Domain', 'admin-page-framework-loader' ), |
92
|
|
|
'type' => 'text', |
93
|
|
|
'tip' => __( 'The default text domain of your project.', 'admin-page-framework-loader' ), |
94
|
|
|
'description' => 'e.g.<code>my-plugin</code>', |
95
|
|
|
'attributes' => array( |
96
|
|
|
'size' => 40, |
97
|
|
|
// 'required' => 'required', |
98
|
|
|
'placeholder' => __( 'Type your text domain.', 'admin-page-framework-loader' ), |
99
|
|
|
), |
100
|
|
|
), |
101
|
|
|
array( |
102
|
|
|
'field_id' => 'components', |
103
|
|
|
'title' => __( 'Components', 'admin-page-framework-loader' ), |
104
|
|
|
'type' => 'checkbox', |
105
|
|
|
'description' => array( |
106
|
|
|
__( 'Select the components you would like to include in your framework files.', 'admin-page-framework-loader' ), |
107
|
|
|
__( 'If you are not sure what to select, check them all.', 'admin-page-framework-loader' ), |
108
|
|
|
), |
109
|
|
|
'label' => $this->___getComponentLabels(), |
110
|
|
|
'default' => array_fill_keys( |
111
|
|
|
array_keys( $this->___getComponentLabels() ), |
112
|
|
|
true // all true |
113
|
|
|
), |
114
|
|
|
'select_all_button' => true, |
115
|
|
|
'select_none_button' => true, |
116
|
|
|
'label_min_width' => '100%', |
117
|
|
|
'attributes' => array( |
118
|
|
|
'core' => array( |
119
|
|
|
'disabled' => 'disabled', |
120
|
|
|
), |
121
|
|
|
), |
122
|
|
|
), |
123
|
|
|
array( |
124
|
|
|
'field_id' => 'download', |
125
|
|
|
'title' => __( 'Compile', 'admin-page-framework-loader' ), |
126
|
|
|
'type' => 'export', |
127
|
|
|
'label_min_width' => 0, |
128
|
|
|
'order' => 100, |
129
|
|
|
'value' => __( 'Download', 'admin-page-framework-demo' ), |
130
|
|
|
'file_name' => 'admin-page-framework.zip', // the default file name. This will be modified by the filter. |
131
|
|
|
'format' => 'text', // 'json', 'text', 'array' |
132
|
|
|
'description' => $oFactory->oUtil->getAOrB( |
133
|
|
|
class_exists( 'ZipArchive' ), |
134
|
|
|
__( 'Download the compiled framework files as a zip file.', 'admin-page-framework-loader' ), |
135
|
|
|
__( 'The zip extension needs to be enabled to use this feature.', 'admin-page-framework-loader' ) |
136
|
|
|
), |
137
|
|
|
'attributes' => array( |
138
|
|
|
'disabled' => $oFactory->oUtil->getAOrB( |
139
|
|
|
class_exists( 'ZipArchive' ), |
140
|
|
|
null, |
141
|
|
|
'disabled' |
142
|
|
|
), |
143
|
|
|
), |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
new AdminPageFrameworkLoader_AdminPage_Tool_Compiler_CustomFieldTypes( $oFactory, $sSectionID ); |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
/** |
151
|
|
|
* Returns component labels as an array. |
152
|
|
|
* @since 3.5.4 |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
private function ___getComponentLabels() { |
156
|
|
|
return array( |
157
|
|
|
'admin_pages' => __( 'Admin Pages', 'admin-page-framework-loader' ), |
158
|
|
|
'network_admin_pages' => __( 'Network Admin Pages', 'admin-page-framework-loader' ), |
159
|
|
|
'post_types' => __( 'Custom Post Types', 'admin-page-framework-loader' ), |
160
|
|
|
'taxonomies' => __( 'Taxonomy Fields', 'admin-page-framework-loader' ), |
161
|
|
|
'term_meta' => __( 'Term Meta', 'admin-page-framework-loader' ), |
162
|
|
|
'meta_boxes' => __( 'Post Meta Boxes', 'admin-page-framework-loader' ), |
163
|
|
|
'page_meta_boxes' => __( 'Page Meta Boxes', 'admin-page-framework-loader' ), |
164
|
|
|
'widgets' => __( 'Widgets', 'admin-page-framework-loader' ), |
165
|
|
|
'user_meta' => __( 'User Meta', 'admin-page-framework-loader' ), |
166
|
|
|
'utilities' => __( 'Utilities', 'admin-page-framework-loader' ), |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Validates the submitted form data. |
172
|
|
|
* |
173
|
|
|
* @since 3.5.4 |
174
|
|
|
* @param array $aInputs |
175
|
|
|
* @param array $aOldInputs |
176
|
|
|
* @param AdminPageFramework $oAdminPage |
177
|
|
|
* @param array $aSubmitInfo |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
public function validate( $aInputs, $aOldInputs, $oAdminPage, $aSubmitInfo ) { |
181
|
|
|
|
182
|
|
|
$_bVerified = true; |
183
|
|
|
$_aErrors = array(); |
184
|
|
|
$aInputs = $this->___getFieldValuesSanitized( $aInputs, $oAdminPage ); |
185
|
|
|
|
186
|
|
|
// the class prefix must not contain white spaces and some other characters not supported in PHP class names. |
187
|
|
|
preg_match( |
188
|
|
|
'/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', // pattern - allowed characters for variables in PHP. |
189
|
|
|
$aInputs[ 'class_prefix' ], // subject |
190
|
|
|
$_aMatches |
191
|
|
|
); |
192
|
|
|
if ( $aInputs[ 'class_prefix' ] && empty( $_aMatches ) ) { |
193
|
|
|
$_aErrors[ $this->sSectionID ][ 'class_prefix' ] = __( 'The prefix must consist of alphanumeric with underscores.', 'admin-page-framework-loader' ); |
194
|
|
|
$_bVerified = false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if ( ! $aInputs[ 'text_domain' ] ) { |
198
|
|
|
$_aErrors[ $this->sSectionID ][ 'text_domain' ] = __( 'The text domain cannot be empty.', 'admin-page-framework-loader' ); |
199
|
|
|
$_bVerified = false; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
// An invalid value is found. Set a field error array and an admin notice and return the old values. |
203
|
|
|
if ( ! $_bVerified ) { |
204
|
|
|
$oAdminPage->setFieldErrors( $_aErrors ); |
205
|
|
|
$oAdminPage->setSettingNotice( __( 'There was something wrong with your input.', 'admin-page-framework-loader' ) ); |
206
|
|
|
return $aOldInputs; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $aInputs; |
210
|
|
|
|
211
|
|
|
} |
212
|
|
|
/** |
213
|
|
|
* Sanitizes user-submitted form field values. |
214
|
|
|
* @since 3.5.4 |
215
|
|
|
* @param array $aInputs |
216
|
|
|
* @param AdminPageFramework $oAdminPage |
217
|
|
|
* @return array The modified input array. |
218
|
|
|
*/ |
219
|
|
|
private function ___getFieldValuesSanitized( array $aInputs, $oAdminPage ) { |
220
|
|
|
$aInputs[ 'class_prefix' ] = trim( |
221
|
|
|
$oAdminPage->oUtil->getElement( |
222
|
|
|
$aInputs, |
223
|
|
|
'class_prefix', |
224
|
|
|
'' |
225
|
|
|
) |
226
|
|
|
); |
227
|
|
|
$aInputs[ 'text_domain' ] = trim( |
228
|
|
|
$oAdminPage->oUtil->getElement( |
229
|
|
|
$aInputs, |
230
|
|
|
'text_domain', |
231
|
|
|
'' |
232
|
|
|
) |
233
|
|
|
); |
234
|
|
|
return $aInputs; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Lets the user download their own version of Admin Page Framework. |
239
|
|
|
* |
240
|
|
|
* @since 3.5.4 |
241
|
|
|
* @param array $aSavedData |
242
|
|
|
* @param string $sSubmittedFieldID |
243
|
|
|
* @param string $sSubmittedInputID |
244
|
|
|
* @param AdminPageFramework $oAdminPage |
245
|
|
|
* @callback add_filter() export_{instantiated class name}_{section id}_{field id} |
246
|
|
|
*/ |
247
|
|
|
public function replyToDownloadFramework( $aSavedData, $sSubmittedFieldID, $sSubmittedInputID, $oAdminPage ) { |
248
|
|
|
|
249
|
|
|
$_sFrameworkDirPath = AdminPageFrameworkLoader_Registry::$sDirPath . '/library/apf'; |
250
|
|
|
if ( ! file_exists( $_sFrameworkDirPath ) ) { |
251
|
|
|
return $aSavedData; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$_sTempFile = $oAdminPage->oUtil->setTempPath( 'admin-page-framework.zip' ); |
255
|
|
|
$_sData = $this->___getDownloadFrameworkZipFile( |
256
|
|
|
$_sFrameworkDirPath, |
257
|
|
|
$_sTempFile |
258
|
|
|
); |
259
|
|
|
header( "Content-Length: " . strlen( $_sData ) ); |
260
|
|
|
unlink( $_sTempFile ); |
261
|
|
|
return $_sData; |
262
|
|
|
|
263
|
|
|
} |
264
|
|
|
/** |
265
|
|
|
* Generates the framework zip data. |
266
|
|
|
* |
267
|
|
|
* @since 3.5.4 |
268
|
|
|
* @return string The binary zip data. |
269
|
|
|
*/ |
270
|
|
|
private function ___getDownloadFrameworkZipFile( $sFrameworkDirPath, $sDestinationPath ) { |
271
|
|
|
|
272
|
|
|
$_oZip = new AdminPageFramework_Zip( |
273
|
|
|
$sFrameworkDirPath, |
274
|
|
|
$sDestinationPath, |
275
|
|
|
array( |
276
|
|
|
'include_directory' => false, // wrap contents in a sub-directory |
277
|
|
|
'additional_source_directories' => apply_filters( |
278
|
|
|
AdminPageFrameworkLoader_Registry::HOOK_SLUG . '_filter_generator_additional_source_directories', |
279
|
|
|
array() // directory paths |
280
|
|
|
), |
281
|
|
|
), |
282
|
|
|
array( // callbacks |
283
|
|
|
'file_name' => array( $this, 'replyToGetPathInArchiveModified' ), |
284
|
|
|
'directory_name' => array( $this, 'replyToGetPathInArchiveModified' ), |
285
|
|
|
'file_contents' => array( $this, 'replyToGetFileContentModified' ), |
286
|
|
|
) |
287
|
|
|
); |
288
|
|
|
$_bSucceed = $_oZip->compress(); |
289
|
|
|
if ( ! $_bSucceed ) { |
290
|
|
|
return ''; |
291
|
|
|
} |
292
|
|
|
return file_get_contents( $sDestinationPath ); |
293
|
|
|
|
294
|
|
|
} |
295
|
|
|
/** |
296
|
|
|
* Modifies the path in the archive which include the file name. |
297
|
|
|
* |
298
|
|
|
* Return an empty string to drop the item. |
299
|
|
|
* |
300
|
|
|
* @remark Gets called earlier than the callback for file contents. |
301
|
|
|
* @param string $sPathInArchive The internal path of the archive including the parsing file name. |
302
|
|
|
* @since 3.5.4 |
303
|
|
|
* @return string |
304
|
|
|
*/ |
305
|
|
|
public function replyToGetPathInArchiveModified( $sPathInArchive ) { |
306
|
|
|
// Check if it belongs to selected components. |
307
|
|
|
if ( false === $this->___isAllowedArchivePath( $sPathInArchive ) ) { |
308
|
|
|
return ''; // empty value will drop the entry |
309
|
|
|
} |
310
|
|
|
return $sPathInArchive; |
311
|
|
|
} |
312
|
|
|
/** |
313
|
|
|
* Checks whether the passed archive path is allowed. |
314
|
|
|
* |
315
|
|
|
* @since 3.5.4 |
316
|
|
|
* @param string $sPath The path to check. It can be a directory or a file. |
317
|
|
|
* @return boolean |
318
|
|
|
*/ |
319
|
|
|
private function ___isAllowedArchivePath( $sPath ) { |
320
|
|
|
foreach( $this->___getDisallowedArchiveDirectoryPaths() as $_sDisallowedPath ) { |
321
|
|
|
$_bHasPrefix = $this->oFactory->oUtil->hasPrefix( |
322
|
|
|
ltrim( $_sDisallowedPath, '/' ), // needle |
323
|
|
|
ltrim( $sPath, '/' ) // haystack |
324
|
|
|
); |
325
|
|
|
if ( $_bHasPrefix ) { |
326
|
|
|
return false; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
return true; |
330
|
|
|
} |
331
|
|
|
/** |
332
|
|
|
* Defines the archive paths of components. |
333
|
|
|
* |
334
|
|
|
* @remark Make sure to have a trailing slash. |
335
|
|
|
* Otherwise, 'factory/AdminPageFramework' will match items that belong to other components. |
336
|
|
|
* @since 3.5.4 |
337
|
|
|
*/ |
338
|
|
|
private $_aComponentPaths = array( |
339
|
|
|
'admin_pages' => array( |
340
|
|
|
'factory/admin_page/', |
341
|
|
|
), |
342
|
|
|
'network_admin_pages' => array( |
343
|
|
|
'factory/admin_page/', |
344
|
|
|
'factory/network_admin_page/', |
345
|
|
|
), |
346
|
|
|
'post_types' => array( |
347
|
|
|
'factory/post_type/', |
348
|
|
|
), |
349
|
|
|
'taxonomies' => array( |
350
|
|
|
'factory/taxonomy_field/', |
351
|
|
|
), |
352
|
|
|
'term_meta' => array( |
353
|
|
|
'factory/taxonomy_field/', |
354
|
|
|
'factory/term_meta/', |
355
|
|
|
), |
356
|
|
|
'meta_boxes' => array( |
357
|
|
|
'factory/meta_box/', |
358
|
|
|
), |
359
|
|
|
'page_meta_boxes' => array( |
360
|
|
|
'factory/meta_box/', |
361
|
|
|
'factory/page_meta_box/', |
362
|
|
|
), |
363
|
|
|
'widgets' => array( |
364
|
|
|
'factory/widget/', |
365
|
|
|
), |
366
|
|
|
'user_meta' => array( |
367
|
|
|
'factory/user_meta/', |
368
|
|
|
), |
369
|
|
|
'utilities' => array( |
370
|
|
|
'utility/', |
371
|
|
|
), |
372
|
|
|
); |
373
|
|
|
/** |
374
|
|
|
* Returns an array holding allowed paths set to the archive. |
375
|
|
|
* @since 3.5.4 |
376
|
|
|
* @return array |
377
|
|
|
*/ |
378
|
|
|
private function ___getDisallowedArchiveDirectoryPaths() { |
379
|
|
|
|
380
|
|
|
// Cache |
381
|
|
|
static $_aDisallowedPaths; |
382
|
|
|
if ( isset( $_aDisallowedPaths ) ) { |
383
|
|
|
return $_aDisallowedPaths; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
// User selected items |
387
|
|
|
$_aSelectedComponents = $this->___getCheckedComponents(); |
388
|
|
|
|
389
|
|
|
// List paths. |
390
|
|
|
$_aAllComponentsPaths = array(); |
391
|
|
|
$_aSelectedComponentsPaths = array(); |
392
|
|
|
foreach( $this->_aComponentPaths as $_sKey => $_aPaths ) { |
393
|
|
|
|
394
|
|
|
// Extract all component paths. |
395
|
|
|
$_aAllComponentsPaths = array_merge( |
396
|
|
|
$_aAllComponentsPaths, |
397
|
|
|
$_aPaths |
398
|
|
|
); |
399
|
|
|
|
400
|
|
|
// Extract selected components paths. |
401
|
|
|
if ( in_array( $_sKey, $_aSelectedComponents, true ) ) { |
402
|
|
|
$_aSelectedComponentsPaths = array_merge( |
403
|
|
|
$_aSelectedComponentsPaths, |
404
|
|
|
$_aPaths |
405
|
|
|
); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
return array_diff( |
411
|
|
|
array_unique( $_aAllComponentsPaths ), |
412
|
|
|
array_unique( $_aSelectedComponentsPaths ) |
413
|
|
|
); |
414
|
|
|
|
415
|
|
|
} |
416
|
|
|
/** |
417
|
|
|
* Returns an array holding elements that the user has selected in the form. |
418
|
|
|
* @since 3.5.4 |
419
|
|
|
* @return array |
420
|
|
|
*/ |
421
|
|
|
private function ___getCheckedComponents() { |
422
|
|
|
$_aChecked = $this->oFactory->oUtil->getElementAsArray( |
423
|
|
|
$_POST, |
424
|
|
|
array( |
425
|
|
|
$this->oFactory->oProp->sOptionKey, |
426
|
|
|
$this->sSectionID, |
427
|
|
|
'components' // field id |
428
|
|
|
), |
429
|
|
|
array() |
430
|
|
|
); |
431
|
|
|
$_aChecked = array_filter( $_aChecked ); |
432
|
|
|
return array_keys( $_aChecked ); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Modifies the file contents of the archive. |
437
|
|
|
* |
438
|
|
|
* @since 3.5.4 |
439
|
|
|
* @return string The modified file contents. |
440
|
|
|
*/ |
441
|
|
|
public function replyToGetFileContentModified( $sFileContent, $sPathInArchive, $sSourceItemPath ) { |
442
|
|
|
|
443
|
|
|
if ( ! $this->___isAllowedToModifyContent( $sPathInArchive, $sSourceItemPath ) ) { |
444
|
|
|
return $sFileContent; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
// Modify the file contents. |
448
|
|
|
$sFileContent = apply_filters( |
449
|
|
|
AdminPageFrameworkLoader_Registry::HOOK_SLUG . '_filter_generator_file_contents', |
450
|
|
|
$sFileContent, |
451
|
|
|
$sPathInArchive, |
452
|
|
|
$this->oFactory->oUtil->getElement( |
453
|
|
|
$_POST, |
454
|
|
|
array( |
455
|
|
|
$this->oFactory->oProp->sOptionKey, |
456
|
|
|
), |
457
|
|
|
array() |
458
|
|
|
), |
459
|
|
|
$this->oFactory |
460
|
|
|
); |
461
|
|
|
|
462
|
|
|
// At this point, it is a php file. |
463
|
|
|
return $this->___getClassNameModifiedByPath( $sFileContent, $sPathInArchive ); |
464
|
|
|
|
465
|
|
|
} |
466
|
|
|
/** |
467
|
|
|
* @return boolean |
468
|
|
|
* @since 3.9.0 |
469
|
|
|
*/ |
470
|
|
|
private function ___isAllowedToModifyContent( $sPathInArchive, $sSourceItemPath ) { |
471
|
|
|
|
472
|
|
|
// Store ignore directory paths |
473
|
|
|
static $_aIgnoreDirPaths = array(); |
474
|
|
|
$_sSourceItemDirPath = dirname( $sSourceItemPath ); |
475
|
|
|
if ( basename( $sSourceItemPath ) === 'ignore-apf-build.txt' ) { |
476
|
|
|
$_aIgnoreDirPaths[] = $_sSourceItemDirPath; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
// Check the file extension. |
480
|
|
|
$_aAllowedExtensions = apply_filters( |
481
|
|
|
AdminPageFrameworkLoader_Registry::HOOK_SLUG . '_filter_generator_allowed_file_extensions', |
482
|
|
|
array( 'php', 'css', 'js' ) |
483
|
|
|
); |
484
|
|
|
if ( ! in_array( pathinfo( $sPathInArchive, PATHINFO_EXTENSION ), $_aAllowedExtensions, true ) ) { |
485
|
|
|
return false; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
// Check if it is inside an ignored directory. |
489
|
|
|
foreach( $_aIgnoreDirPaths as $_sIgnoreDirPath ) { |
490
|
|
|
if ( false !== strpos( $sSourceItemPath, $_sIgnoreDirPath ) ) { |
491
|
|
|
return false; |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
return true; |
495
|
|
|
|
496
|
|
|
} |
497
|
|
|
/** |
498
|
|
|
* Modifies the given file contents. |
499
|
|
|
* |
500
|
|
|
* @since 3.5.4 |
501
|
|
|
* @return string |
502
|
|
|
*/ |
503
|
|
|
private function ___getClassNameModifiedByPath( $sFileContent, $sPathInArchive ) { |
504
|
|
|
|
505
|
|
|
// The inclusion class list file needs to be handled differently. |
506
|
|
|
if ( $this->oFactory->oUtil->hasSuffix( 'admin-page-framework-class-map.php', $sPathInArchive ) ) { |
507
|
|
|
return $this->___getClassNameOfIncludeListModified( $sFileContent ); |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
// Insert notes in the header comment. |
511
|
|
|
if ( $this->oFactory->oUtil->hasSuffix( 'admin-page-framework.php', $sPathInArchive ) ) { |
512
|
|
|
$sFileContent = $this->___getFileDocblockModified( $sFileContent ); |
513
|
|
|
return $this->___getClassNameModified( $sFileContent ); |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
$sFileContent = $this->___getClassNameModified( $sFileContent ); |
517
|
|
|
|
518
|
|
|
// If it is the message class, modify the text domain. |
519
|
|
|
// @deprecated 3.6.0+ |
520
|
|
|
// if ( ! $this->oFactory->oUtil->hasSuffix( 'AdminPageFramework_Message.php', $sPathInArchive ) ) { |
521
|
|
|
// return $sFileContent; |
522
|
|
|
// } |
523
|
|
|
return $this->___getTextDomainModified( $sFileContent ); |
524
|
|
|
|
525
|
|
|
} |
526
|
|
|
/** |
527
|
|
|
* Inserts additional information such as an included component list and a date to the file doc-block (the header comment part). |
528
|
|
|
* @since 3.5.4 |
529
|
|
|
* @return string |
530
|
|
|
*/ |
531
|
|
|
private function ___getFileDocblockModified( $sFileContent ) { |
532
|
|
|
|
533
|
|
|
$_aCheckedComponents = $this->oFactory->oUtil->getArrayElementsByKeys( |
534
|
|
|
$this->___getComponentLabels(), |
535
|
|
|
$this->___getCheckedComponents() |
536
|
|
|
); |
537
|
|
|
$_aInsert = array( |
538
|
|
|
'Compiled on ' . date( 'Y-m-d' ), // today's date |
539
|
|
|
'Included Components: ' . implode( ', ', $_aCheckedComponents ), |
540
|
|
|
); |
541
|
|
|
$_sInsertComment = apply_filters( AdminPageFrameworkLoader_Registry::HOOK_SLUG . '_filter_generator_header_comment', implode( PHP_EOL . ' * ', $_aInsert ) ); |
542
|
|
|
return preg_replace( |
543
|
|
|
'#[^\S\r\n]+?\*/#', // needle - matches '*/' or ' */' |
544
|
|
|
' * ' . trim( $_sInsertComment ) . PHP_EOL . '\0', // replacement \0 is a back-reference to '*/' |
545
|
|
|
$sFileContent, // subject |
546
|
|
|
1 // replace only the first occurrence |
547
|
|
|
); |
548
|
|
|
|
549
|
|
|
} |
550
|
|
|
/** |
551
|
|
|
* Modifies the class inclusion list. |
552
|
|
|
* @since 3.5.4 |
553
|
|
|
* @return string |
554
|
|
|
*/ |
555
|
|
|
private function ___getClassNameOfIncludeListModified( $sFileContents ) { |
556
|
|
|
// Replace the array key names. |
557
|
|
|
$sFileContents = preg_replace_callback( |
558
|
|
|
'/(["\'])(.+)\1(?=\s?+=>)/', // pattern ' |
559
|
|
|
array( $this, '_replyToModifyPathName' ), // callable |
560
|
|
|
$sFileContents // subject |
561
|
|
|
); |
562
|
|
|
// Replace the registry class names. |
563
|
|
|
return preg_replace_callback( |
564
|
|
|
'/(=>\s?+)(.+)(?=::)/', // pattern ' |
565
|
|
|
array( $this, '_replyToModifyPathName' ), // callable |
566
|
|
|
$sFileContents // subject |
567
|
|
|
); |
568
|
|
|
} |
569
|
|
|
/** |
570
|
|
|
* Modifies the regex-matched string. |
571
|
|
|
* @callback function preg_replace_callback() |
572
|
|
|
* @since 3.5.4 |
573
|
|
|
*/ |
574
|
|
|
public function _replyToModifyPathName( $aMatches ) { |
575
|
|
|
return $this->___getClassNameModified( $aMatches[ 0 ] ); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Modifies the given class name. |
580
|
|
|
* |
581
|
|
|
* @since 3.5.4 |
582
|
|
|
* @return string |
583
|
|
|
*/ |
584
|
|
|
private function ___getClassNameModified( $sSubject ) { |
585
|
|
|
$_sPrefix = $this->___getFormSubmitValueByFieldIDAsString( 'class_prefix' ); |
586
|
|
|
return strlen( $_sPrefix ) |
587
|
|
|
? str_replace( |
588
|
|
|
'AdminPageFramework', // search |
589
|
|
|
$_sPrefix . 'AdminPageFramework', // replace |
590
|
|
|
$sSubject // subject |
591
|
|
|
) |
592
|
|
|
: $sSubject; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Modifies the text domain in the given file contents. |
597
|
|
|
* |
598
|
|
|
* @since 3.5.4 |
599
|
|
|
* @return string |
600
|
|
|
*/ |
601
|
|
|
private function ___getTextDomainModified( $sFileContents ) { |
602
|
|
|
$_sTextDomain = $this->___getFormSubmitValueByFieldIDAsString( 'text_domain' ); |
603
|
|
|
return strlen( $_sTextDomain ) |
604
|
|
|
? str_replace( |
605
|
|
|
'admin-page-framework', // search |
606
|
|
|
$_sTextDomain, // replace |
607
|
|
|
$sFileContents // subject |
608
|
|
|
) |
609
|
|
|
: $sFileContents; |
610
|
|
|
} |
611
|
|
|
/** |
612
|
|
|
* Retrieves the value from the $_POST array by the given field ID. |
613
|
|
|
* |
614
|
|
|
* @since 3.5.4 |
615
|
|
|
* @return string |
616
|
|
|
*/ |
617
|
|
|
private function ___getFormSubmitValueByFieldIDAsString( $sFieldID ) { |
618
|
|
|
static $_aCaches=array(); |
619
|
|
|
$_aCaches[ $sFieldID ] = isset( $_aCaches[ $sFieldID ] ) |
620
|
|
|
? $_aCaches[ $sFieldID ] |
621
|
|
|
: $this->oFactory->oUtil->getElement( |
622
|
|
|
$_POST, |
623
|
|
|
array( |
624
|
|
|
$this->oFactory->oProp->sOptionKey, |
625
|
|
|
$this->sSectionID, |
626
|
|
|
$sFieldID |
627
|
|
|
), |
628
|
|
|
'' |
629
|
|
|
); |
630
|
|
|
return trim( ( string ) $_aCaches[ $sFieldID ] ); |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
/** |
634
|
|
|
* Modifies the HTTP header of the export field. |
635
|
|
|
* |
636
|
|
|
* @callback add_filter() export_header_{...} |
637
|
|
|
* @since 3.5.4 |
638
|
|
|
* @return array |
639
|
|
|
*/ |
640
|
|
|
public function replyToGetExportHTTPHeaderModified( $aHeader, $sFieldID, $sInputID, $mData, $sFileName, $oFactory ) { |
|
|
|
|
641
|
|
|
|
642
|
|
|
$sFileName = $this->___getDownloadFileName(); |
643
|
|
|
return array( |
644
|
|
|
'Pragma' => 'public', |
645
|
|
|
'Expires' => 0, |
646
|
|
|
'Cache-Control' => array( |
647
|
|
|
'must-revalidate, post-check=0, pre-check=0', |
648
|
|
|
'public', |
649
|
|
|
), |
650
|
|
|
'Content-Description' => 'File Transfer', |
651
|
|
|
'Content-type' => 'application/octet-stream', // 'application/zip' may work as well |
652
|
|
|
'Content-Transfer-Encoding' => 'binary', |
653
|
|
|
'Content-Disposition' => 'attachment; filename="' . $sFileName .'";', |
654
|
|
|
// 'Content-Length' => strlen( $mData ), |
655
|
|
|
) + $aHeader; |
656
|
|
|
|
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
/** |
660
|
|
|
* Filters the exporting file name. |
661
|
|
|
* |
662
|
|
|
* @callback add_filter() "export_name_{page slug}_{tab slug}" filter. |
663
|
|
|
* @return string |
664
|
|
|
*/ |
665
|
|
|
public function replyToFilterFileName( $sFileName, $sFieldID, $sInputID, $vExportingData, $oAdminPage ) { |
|
|
|
|
666
|
|
|
return $this->___getDownloadFileName(); |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* Returns the user-set file name. |
671
|
|
|
* |
672
|
|
|
* The user set text domain will be added as a prefix to `admin-page-framework.zip`. |
673
|
|
|
* |
674
|
|
|
* @since 3.5.4 |
675
|
|
|
* @return string |
676
|
|
|
*/ |
677
|
|
|
private function ___getDownloadFileName() { |
678
|
|
|
$_sFileNameWOExtension = $this->oFactory->oUtil->getElement( |
679
|
|
|
$_POST, |
680
|
|
|
array( |
681
|
|
|
$this->oFactory->oProp->sOptionKey, |
682
|
|
|
$this->sSectionID, |
683
|
|
|
'text_domain' // field id |
684
|
|
|
) |
685
|
|
|
); |
686
|
|
|
$_sFileNameWOExtension = trim( sanitize_text_field( $_sFileNameWOExtension ) ); |
687
|
|
|
return $this->oFactory->oUtil->getAOrB( |
688
|
|
|
$_sFileNameWOExtension, |
689
|
|
|
$_sFileNameWOExtension . '-admin-page-framework', |
690
|
|
|
'admin-page-framework' |
691
|
|
|
) |
692
|
|
|
. '.' . AdminPageFramework_Registry::VERSION |
693
|
|
|
. '.zip'; |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.