|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Admin Page Framework |
|
4
|
|
|
* |
|
5
|
|
|
* http://en.michaeluno.jp/admin-page-framework/ |
|
6
|
|
|
* Copyright (c) 2013-2016 Michael Uno; Licensed MIT |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Provides methods which mainly deal with the stored data for creating meta boxes in admin pages added by the framework. |
|
11
|
|
|
* |
|
12
|
|
|
* @abstract |
|
13
|
|
|
* @since 3.0.4 |
|
14
|
|
|
* @package AdminPageFramework |
|
15
|
|
|
* @subpackage PageMetaBox |
|
16
|
|
|
* @internal |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AdminPageFramework_PageMetaBox_Model extends AdminPageFramework_PageMetaBox_Router { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A validation callback method. |
|
22
|
|
|
* |
|
23
|
|
|
* The user may just override this method instead of defining a `validation_{...}` callback method. |
|
24
|
|
|
* |
|
25
|
|
|
* @since 3.4.1 |
|
26
|
|
|
* @since 3.5.3 Moved from `AdminPageFramework_Factory_Model`. |
|
27
|
|
|
* @todo Examine if the forth parameter of submit info can be added or not. |
|
28
|
|
|
* @param array $aInput The submit form data. |
|
29
|
|
|
* @param array $aOldInput The previously submit form data stored in the database. |
|
30
|
|
|
* @param object $oFactory The page meta box factory object. |
|
31
|
|
|
* @param array $aSubmitInfo [3.5.3+] An array holding submit information such as which submit field is pressed. |
|
32
|
|
|
* @remark Not defining this method for backward compatibility to avoid strict standard warnings of PHP. |
|
33
|
|
|
*/ |
|
34
|
|
|
// public function validate( $aInput, $aOldInput, $oFactory, $aSubmitInfo ) { |
|
35
|
|
|
// return $aInput; |
|
36
|
|
|
// } |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Sets up validation hooks. |
|
40
|
|
|
* |
|
41
|
|
|
* @since 3.3.0 |
|
42
|
|
|
* @since 3.7.9 Renamed from `_setUpValidationHooks`. Changed the scope to public from protected. |
|
43
|
|
|
* @callback action set_up_{instantiated class name} |
|
44
|
|
|
* @internal |
|
45
|
|
|
*/ |
|
46
|
|
|
public function _replyToSetUpValidationHooks( $oScreen ) { |
|
47
|
|
|
|
|
48
|
|
|
// Validation hooks |
|
49
|
|
|
foreach( $this->oProp->aPageSlugs as $_sIndexOrPageSlug => $_asTabArrayOrPageSlug ) { |
|
50
|
|
|
|
|
51
|
|
|
if ( is_scalar( $_asTabArrayOrPageSlug ) ) { |
|
52
|
|
|
$_sPageSlug = $_asTabArrayOrPageSlug; |
|
53
|
|
|
add_filter( "validation_saved_options_without_dynamic_elements_{$_sPageSlug}", array( $this, '_replyToFilterPageOptionsWODynamicElements' ), 10, 2 ); // 3.4.1+ |
|
54
|
|
|
add_filter( "validation_{$_sPageSlug}", array( $this, '_replyToValidateOptions' ), 10, 4 ); |
|
55
|
|
|
add_filter( "options_update_status_{$_sPageSlug}", array( $this, '_replyToModifyOptionsUpdateStatus' ) ); |
|
56
|
|
|
continue; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// At this point, the array key is the page slug. It means the user specified the tab(s). |
|
60
|
|
|
$_sPageSlug = $_sIndexOrPageSlug; |
|
61
|
|
|
$_aTabs = $_asTabArrayOrPageSlug; |
|
62
|
|
|
foreach( $_aTabs as $_sTabSlug ) { |
|
63
|
|
|
add_filter( "validation_{$_sPageSlug}_{$_sTabSlug}", array( $this, '_replyToValidateOptions' ), 10, 4 ); |
|
64
|
|
|
add_filter( "validation_saved_options_without_dynamic_elements_{$_sPageSlug}_{$_sTabSlug}", array( $this, '_replyToFilterPageOptionsWODynamicElements' ), 10, 2 ); // 3.4.1+ |
|
65
|
|
|
add_filter( "options_update_status_{$_sPageSlug}_{$_sTabSlug}", array( $this, '_replyToModifyOptionsUpdateStatus' ) ); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Adds the defined meta box. |
|
74
|
|
|
* |
|
75
|
|
|
* @internal |
|
76
|
|
|
* @since 3.0.0 |
|
77
|
|
|
* @since 3.7.10 Changed the name from `_replyToAddMetaBox()`. |
|
78
|
|
|
* @remark Before this method is called, the pages and in-page tabs need to be registered already. |
|
79
|
|
|
* @return void |
|
80
|
|
|
* @callback action add_meta_boxes |
|
81
|
|
|
*/ |
|
82
|
|
|
public function _replyToRegisterMetaBoxes( $sPageHook='' ) { |
|
|
|
|
|
|
83
|
|
|
foreach( $this->oProp->aPageSlugs as $_sKey => $_asPage ) { |
|
84
|
|
|
if ( is_string( $_asPage ) ) { |
|
85
|
|
|
$this->_registerMetaBox( $_asPage ); |
|
86
|
|
|
continue; |
|
87
|
|
|
} |
|
88
|
|
|
$this->_registerMetaBoxes( $_sKey, $_asPage ); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
/** |
|
92
|
|
|
* Adds meta boxes. |
|
93
|
|
|
* |
|
94
|
|
|
* @since 3.7.0 |
|
95
|
|
|
* @since 3.7.10 Changed the name from `_addMetaBoxes()`. |
|
96
|
|
|
* @internal |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
private function _registerMetaBoxes( $sPageSlug, $asPage ) { |
|
100
|
|
|
foreach( $this->oUtil->getAsArray( $asPage ) as $_sTabSlug ) { |
|
101
|
|
|
if ( ! $this->oProp->isCurrentTab( $_sTabSlug ) ) { |
|
102
|
|
|
continue; |
|
103
|
|
|
} |
|
104
|
|
|
$this->_registerMetaBox( $sPageSlug ); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
/** |
|
108
|
|
|
* Adds meta box with the given page slug. |
|
109
|
|
|
* |
|
110
|
|
|
* @since 3.0.0 |
|
111
|
|
|
* @since 3.7.10 Changed the name from `_addMetaBox()`. |
|
112
|
|
|
* @internal |
|
113
|
|
|
* @uses add_meta_box() |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
|
|
private function _registerMetaBox( $sPageSlug ) { |
|
117
|
|
|
add_meta_box( |
|
118
|
|
|
$this->oProp->sMetaBoxID, // id |
|
119
|
|
|
$this->oProp->sTitle, // title |
|
120
|
|
|
array( $this, '_replyToPrintMetaBoxContents' ), // callback |
|
121
|
|
|
$this->oProp->_getScreenIDOfPage( $sPageSlug ), // screen ID |
|
122
|
|
|
$this->oProp->sContext, // context |
|
123
|
|
|
$this->oProp->sPriority, // priority |
|
124
|
|
|
null // argument (deprecated) |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Filters the page option array. |
|
130
|
|
|
* |
|
131
|
|
|
* This is triggered from the system validation method of the main Admin Page Framework factory class with the `validation_saved_options_{page slug}` filter hook. |
|
132
|
|
|
* |
|
133
|
|
|
* @since 3.0.0 |
|
134
|
|
|
* @param array $aPageOptions |
|
135
|
|
|
* @deprecated 3.4.1 |
|
136
|
|
|
* @internal |
|
137
|
|
|
*/ |
|
138
|
|
|
public function _replyToFilterPageOptions( $aPageOptions ) { |
|
139
|
|
|
return $aPageOptions; |
|
140
|
|
|
} |
|
141
|
|
|
/** |
|
142
|
|
|
* Filters the array of the options without dynamic elements. |
|
143
|
|
|
* |
|
144
|
|
|
* @since 3.4.1 Deprecated `_replyToFilterPageOptions()`. |
|
145
|
|
|
* @callback filter validation_saved_options_without_dynamic_elements_{$_sPageSlug} |
|
146
|
|
|
* @callback filter validation_saved_options_without_dynamic_elements_{page slug}_{tab slug} |
|
147
|
|
|
* @internal |
|
148
|
|
|
*/ |
|
149
|
|
|
public function _replyToFilterPageOptionsWODynamicElements( $aOptionsWODynamicElements, $oFactory ) { |
|
|
|
|
|
|
150
|
|
|
return $this->oForm->dropRepeatableElements( $aOptionsWODynamicElements ); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Validates the submitted option values. |
|
155
|
|
|
* |
|
156
|
|
|
* This method is triggered with the `validation_{page slug}` or `validation_{page slug}_{tab slug}` method of the main Admin Page Framework factory class. |
|
157
|
|
|
* |
|
158
|
|
|
* @internal |
|
159
|
|
|
* @callback filter validation_{page slug}, validation_{page slug}_{tab slug} |
|
160
|
|
|
* @sicne 3.0.0 |
|
161
|
|
|
* @param array $aNewPageOptions The array holing the field values of the page sent from the framework page class (the main class). |
|
162
|
|
|
* @param array $aOldPageOptions The array holing the saved options of the page. Note that this will be empty if non of generic page fields are created. |
|
163
|
|
|
* @param object $oAdminPage The admin page factory class object. |
|
164
|
|
|
* @param array $aSubmitInfo An array containing submit information such as a pressed submit field ID. |
|
165
|
|
|
* @return array The validated form input data. |
|
166
|
|
|
*/ |
|
167
|
|
|
public function _replyToValidateOptions( $aNewPageOptions, $aOldPageOptions, $oAdminPage, $aSubmitInfo ) { |
|
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
$_aNewMetaBoxInputs = $this->oForm->getSubmittedData( $_POST ); |
|
170
|
|
|
$_aOldMetaBoxInputs = $this->oUtil->castArrayContents( |
|
171
|
|
|
$this->oForm->getDataStructureFromAddedFieldsets(), // model |
|
172
|
|
|
$aOldPageOptions // data source |
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
|
|
// Apply filters - third party scripts will have access to the input. |
|
176
|
|
|
$_aNewMetaBoxInputsRaw = $_aNewMetaBoxInputs; // copy one for validation errors. |
|
177
|
|
|
$_aNewMetaBoxInputs = call_user_func_array( |
|
178
|
|
|
array( $this, 'validate' ), // triggers __call() |
|
179
|
|
|
array( $_aNewMetaBoxInputs, $_aOldMetaBoxInputs, $this, $aSubmitInfo ) |
|
180
|
|
|
); // 3.5.3+ |
|
181
|
|
|
$_aNewMetaBoxInputs = $this->oUtil->addAndApplyFilters( |
|
182
|
|
|
$this, |
|
183
|
|
|
"validation_{$this->oProp->sClassName}", |
|
184
|
|
|
$_aNewMetaBoxInputs, |
|
185
|
|
|
$_aOldMetaBoxInputs, |
|
186
|
|
|
$this, |
|
187
|
|
|
$aSubmitInfo |
|
188
|
|
|
); |
|
189
|
|
|
|
|
190
|
|
|
// If there are validation errors. set the last input. |
|
191
|
|
|
if ( $this->hasFieldError() ) { |
|
192
|
|
|
$this->setLastInputs( $_aNewMetaBoxInputsRaw ); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
// Now merge the input values with the passed page options, and plus the old data to cover different in-page tab field options. |
|
196
|
|
|
return $this->oUtil->uniteArrays( |
|
197
|
|
|
$_aNewMetaBoxInputs, |
|
198
|
|
|
$aNewPageOptions |
|
199
|
|
|
); |
|
200
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Modifies the options update status array. |
|
205
|
|
|
* |
|
206
|
|
|
* This is to insert the 'field_errors' key into the options update status array when there is a field error. |
|
207
|
|
|
* |
|
208
|
|
|
* @internal |
|
209
|
|
|
* @since 3.4.1 |
|
210
|
|
|
* @callback filter options_update_status_{page slug} |
|
211
|
|
|
* @callback filter options_update_status_{page slug}_{tab slug} |
|
212
|
|
|
* @return array |
|
213
|
|
|
*/ |
|
214
|
|
|
public function _replyToModifyOptionsUpdateStatus( $aStatus ) { |
|
215
|
|
|
|
|
216
|
|
|
if ( ! $this->hasFieldError() ) { |
|
217
|
|
|
return $aStatus; |
|
218
|
|
|
} |
|
219
|
|
|
return array( |
|
220
|
|
|
'field_errors' => true |
|
221
|
|
|
) |
|
222
|
|
|
+ $this->oUtil->getAsArray( $aStatus ); |
|
223
|
|
|
|
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Called when the form object tries to set the form data from the database. |
|
228
|
|
|
* |
|
229
|
|
|
* @callback form `saved_data` |
|
230
|
|
|
* @remark The `oOptions` property will be automatically set with the overload method. |
|
231
|
|
|
* @remark Redefine (override) this method completely because the parent meta box factory class |
|
232
|
|
|
* has this method and its own way to retrieve form data. |
|
233
|
|
|
* @return array The saved form data. |
|
234
|
|
|
* @since 3.7.0 |
|
235
|
|
|
*/ |
|
236
|
|
|
public function _replyToGetSavedFormData() { |
|
237
|
|
|
$_aPageOptions = $this->oUtil->addAndApplyFilter( |
|
238
|
|
|
$this, // the caller factory object |
|
239
|
|
|
'options_' . $this->oProp->sClassName, |
|
240
|
|
|
$this->oProp->oAdminPage->oProp->aOptions |
|
241
|
|
|
); |
|
242
|
|
|
return $this->oUtil->castArrayContents( |
|
243
|
|
|
$this->oForm->getDataStructureFromAddedFieldsets(), // model |
|
244
|
|
|
$_aPageOptions // data source |
|
245
|
|
|
); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Extracts meta box form fields options array from the given options array of an admin page. |
|
250
|
|
|
* |
|
251
|
|
|
* @since 3.5.6 |
|
252
|
|
|
* @return array The extracted options array. |
|
253
|
|
|
* @internal |
|
254
|
|
|
* @deprecated 3.7.0 |
|
255
|
|
|
*/ |
|
256
|
|
|
private function _getPageMetaBoxOptionsFromPageOptions( array $aPageOptions, array $aFieldsets ) { |
|
257
|
|
|
|
|
258
|
|
|
$_aOptions = array(); |
|
259
|
|
|
foreach( $aFieldsets as $_sSectionID => $_aFieldsets ) { |
|
260
|
|
|
if ( '_default' === $_sSectionID ) { |
|
261
|
|
|
foreach( $_aFieldsets as $_aField ) { |
|
262
|
|
|
if ( array_key_exists( $_aField[ 'field_id' ], $aPageOptions ) ) { |
|
263
|
|
|
$_aOptions[ $_aField[ 'field_id' ] ] = $aPageOptions[ $_aField[ 'field_id' ] ]; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
if ( array_key_exists( $_sSectionID, $aPageOptions ) ) { |
|
268
|
|
|
$_aOptions[ $_sSectionID ] = $aPageOptions[ $_sSectionID ]; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
return $_aOptions; |
|
272
|
|
|
|
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
} |
|
276
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.