|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Admin Page Framework |
|
4
|
|
|
* |
|
5
|
|
|
* http://en.michaeluno.jp/admin-page-framework/ |
|
6
|
|
|
* Copyright (c) 2013-2015 Michael Uno; Licensed MIT |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Provides methods for models. |
|
12
|
|
|
* |
|
13
|
|
|
* @abstract |
|
14
|
|
|
* @since 3.0.4 |
|
15
|
|
|
* @package AdminPageFramework |
|
16
|
|
|
* @subpackage Factory |
|
17
|
|
|
* @transient apf_field_erros_{user id} stores the user-set fields error array. |
|
18
|
|
|
* @transient apf_notices_{user id} stores the user-set admin notification messages. |
|
19
|
|
|
* @internal |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class AdminPageFramework_Factory_Model extends AdminPageFramework_Factory_Router { |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Sets up hooks and properties. |
|
25
|
|
|
* |
|
26
|
|
|
* @since DEVVER |
|
27
|
|
|
* @internal |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct( $oProp ) { |
|
30
|
|
|
|
|
31
|
|
|
parent::__construct( $oProp ); |
|
32
|
|
|
|
|
33
|
|
|
add_filter( |
|
34
|
|
|
'field_types_admin_page_framework', |
|
35
|
|
|
array( $this, '_replyToFilterFieldTypeDefinitions' ) |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Calls the setUp() method. |
|
42
|
|
|
* |
|
43
|
|
|
* @since 3.1.0 |
|
44
|
|
|
* @todo Deprecate this method. This method was intended to be used in a user defined abstract class |
|
45
|
|
|
* but it requires to call the setUp() method in the overridden method so it's not that useful. |
|
46
|
|
|
* @internal |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function _setUp() { |
|
49
|
|
|
$this->setUp(); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Called upon fieldset resource registration. |
|
54
|
|
|
* |
|
55
|
|
|
* A contextual help pane item associated with this fieldset will be added. |
|
56
|
|
|
* |
|
57
|
|
|
* @since DEVVER |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
public function _replyToFieldsetReourceRegistration( $aFieldset ) { |
|
61
|
|
|
|
|
62
|
|
|
$aFieldset = $aFieldset + array( |
|
63
|
|
|
'help' => null, |
|
64
|
|
|
'title' => null, |
|
65
|
|
|
'help_aside' => null, |
|
66
|
|
|
); |
|
67
|
|
|
if ( ! $aFieldset[ 'help' ] ) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
$this->oHelpPane->_addHelpTextForFormFields( |
|
71
|
|
|
$aFieldset[ 'title' ], |
|
72
|
|
|
$aFieldset[ 'help' ], |
|
73
|
|
|
$aFieldset[ 'help_aside' ] |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Filters field type definitions array. |
|
80
|
|
|
* @callback filter field_types_admin_page_framework |
|
81
|
|
|
* @since DEVVER |
|
82
|
|
|
*/ |
|
83
|
|
|
public function _replyToFilterFieldTypeDefinitions( $aFieldTypeDefinitions ) { |
|
84
|
|
|
return $this->oUtil->addAndApplyFilters( |
|
85
|
|
|
$this, |
|
86
|
|
|
"field_types_{$this->oProp->sClassName}", |
|
87
|
|
|
$aFieldTypeDefinitions |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Modifies registered sectionsets definition array. |
|
93
|
|
|
* |
|
94
|
|
|
* This lets third party scripts to set their own sections |
|
95
|
|
|
* before the framework registered field resource (assets) files. |
|
96
|
|
|
* |
|
97
|
|
|
* @remark Called prior to field resource registrations. |
|
98
|
|
|
* @since DEVVER |
|
99
|
|
|
* @return array The modified sectionsets definition array. |
|
100
|
|
|
*/ |
|
101
|
|
|
public function _replyToModifySectionsets( $aSectionsets ) { |
|
102
|
|
|
|
|
103
|
|
|
return $this->oUtil->addAndApplyFilter( |
|
104
|
|
|
$this, // caller factory object |
|
105
|
|
|
"sections_{$this->oProp->sClassName}", |
|
106
|
|
|
$aSectionsets |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Modifies registered fieldsets definition array. |
|
113
|
|
|
* |
|
114
|
|
|
* This lets third party scripts to set their own sections |
|
115
|
|
|
* before the framework registered field resource (assets) files. |
|
116
|
|
|
* |
|
117
|
|
|
* @remark Called prior to field resource registrations. |
|
118
|
|
|
* @since DEVVER |
|
119
|
|
|
* @return array The modified fieldsets definition array. |
|
120
|
|
|
*/ |
|
121
|
|
|
public function _replyToModifyFieldsets( $aFieldsets, $aSectionsets ) { |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
// Apply filters to added fieldsets |
|
124
|
|
|
foreach( $aFieldsets as $_sSectionPath => $_aFields ) { |
|
125
|
|
|
$_aSectionPath = explode( '|', $_sSectionPath ); |
|
126
|
|
|
$_sFilterSuffix = implode( '_', $_aSectionPath ); |
|
127
|
|
|
$aFieldsets[ $_sSectionPath ] = $this->oUtil->addAndApplyFilter( |
|
128
|
|
|
$this, |
|
129
|
|
|
"fields_{$this->oProp->sClassName}_{$_sFilterSuffix}", |
|
130
|
|
|
$_aFields |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
$aFieldsets = $this->oUtil->addAndApplyFilter( |
|
134
|
|
|
$this, |
|
135
|
|
|
"fields_{$this->oProp->sClassName}", |
|
136
|
|
|
$aFieldsets |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
// If at lease one filed is added, set the flag to enable the form. |
|
140
|
|
|
// Do not set `false` when there is no field because page meta boxes may add form fields. |
|
141
|
|
|
if ( count( $aFieldsets ) ) { |
|
142
|
|
|
$this->oProp->bEnableForm = true; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $aFieldsets; |
|
146
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Applies filters to all the conditioned field definitions array. |
|
151
|
|
|
* @since DEVVER |
|
152
|
|
|
* @return array |
|
153
|
|
|
*/ |
|
154
|
|
|
public function _replyToModifyFieldsetsDefinitions( $aFieldsets /*, $aSectionsets */ ) { |
|
155
|
|
|
return $this->oUtil->addAndApplyFilter( |
|
156
|
|
|
$this, |
|
157
|
|
|
"field_definition_{$this->oProp->sClassName}", |
|
158
|
|
|
$aFieldsets |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Applies filters to each conditioned (formatted) field definition array. |
|
164
|
|
|
* |
|
165
|
|
|
* @since 3.0.2 |
|
166
|
|
|
* @since 3.1.1 Made it reformat the fields after applying filters. |
|
167
|
|
|
* @since DEVVER Changed the name from `applyFiltersToFieldsets()`. |
|
168
|
|
|
* Moved from `AdminPageFramework_FormDefinition_Base`. |
|
169
|
|
|
* @return array |
|
170
|
|
|
*/ |
|
171
|
|
|
public function _replyToModifyFieldsetDefinition( $aFieldset /*, $aSectionsets */ ) { |
|
172
|
|
|
|
|
173
|
|
|
$_sFieldPart = '_' . implode( '_', $aFieldset[ '_field_path_array' ] ); |
|
174
|
|
|
$_sSectionPart = implode( '_', $aFieldset[ '_section_path_array' ] ); |
|
175
|
|
|
$_sSectionPart = $this->oUtil->getAOrB( |
|
176
|
|
|
'_default' === $_sSectionPart, |
|
177
|
|
|
'', |
|
178
|
|
|
'_' . $_sSectionPart |
|
179
|
|
|
); |
|
180
|
|
|
return $this->oUtil->addAndApplyFilter( |
|
181
|
|
|
$this, |
|
182
|
|
|
"field_definition_{$this->oProp->sClassName}{$_sSectionPart}{$_sFieldPart}", |
|
183
|
|
|
$aFieldset, |
|
184
|
|
|
$aFieldset[ '_subsection_index' ] |
|
185
|
|
|
); |
|
186
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Gets called after the form element registration is done. |
|
191
|
|
|
* |
|
192
|
|
|
* @since DEVVER |
|
193
|
|
|
*/ |
|
194
|
|
|
public function _replyToHandleSubmittedFormData( $aSavedData, $aArguments, $aSectionsets, $aFieldsets ) { |
|
195
|
|
|
// Do validation and saving data |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @since DEVVER |
|
200
|
|
|
* @return array |
|
201
|
|
|
*/ |
|
202
|
|
|
public function _replyToFormatFieldsetDefinition( $aFieldset, $aSectionsets ) { |
|
203
|
|
|
|
|
204
|
|
|
if ( empty( $aFieldset ) ) { |
|
205
|
|
|
return $aFieldset; |
|
206
|
|
|
} |
|
207
|
|
|
return $aFieldset; |
|
208
|
|
|
|
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @since DEVVER |
|
213
|
|
|
* @return array |
|
214
|
|
|
*/ |
|
215
|
|
|
public function _replyToFormatSectionsetDefinition( $aSectionset ) { |
|
216
|
|
|
|
|
217
|
|
|
if ( empty( $aSectionset ) ) { |
|
218
|
|
|
return $aSectionset; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$aSectionset = $aSectionset |
|
222
|
|
|
+ array( |
|
223
|
|
|
'_fields_type' => $this->oProp->_sPropertyType, // backward compatibility |
|
224
|
|
|
'_structure_type' => $this->oProp->_sPropertyType, |
|
225
|
|
|
); |
|
226
|
|
|
|
|
227
|
|
|
return $aSectionset; |
|
228
|
|
|
|
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @since DEVVER |
|
233
|
|
|
* @return boolean Whether or not the form registration should be allowed in the current screen. |
|
234
|
|
|
*/ |
|
235
|
|
|
public function _replyToDetermineWhetherToProcessFormRegistration( $bAllowed ) { |
|
|
|
|
|
|
236
|
|
|
return $this->_isInThePage(); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Returns the inherited capability value from the page and in-page tab for form elements. |
|
241
|
|
|
* |
|
242
|
|
|
* @since DEVVER Moved from `AdminPageFramework_FormDefinition_Page`. |
|
243
|
|
|
* @return string |
|
244
|
|
|
*/ |
|
245
|
|
|
public function _replyToGetCapabilityForForm( $sCapability ) { |
|
|
|
|
|
|
246
|
|
|
return $this->oProp->sCapability; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Called when the form object tries to set the form data from the database. |
|
251
|
|
|
* |
|
252
|
|
|
* @callback form `saved_data` |
|
253
|
|
|
* @remark The `oOptions` property will be automatically set with the overload method. |
|
254
|
|
|
* @return array The saved form data. |
|
255
|
|
|
* @since DEVVER |
|
256
|
|
|
*/ |
|
257
|
|
|
public function _replyToGetSavedFormData() { |
|
258
|
|
|
return $this->oUtil->addAndApplyFilter( |
|
259
|
|
|
$this, // the caller factory object |
|
260
|
|
|
'options_' . $this->oProp->sClassName, |
|
261
|
|
|
$this->oProp->aOptions // subject value to be filtered |
|
262
|
|
|
); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Returns the saved options array. |
|
267
|
|
|
* |
|
268
|
|
|
* The scope public it is accessed from the outside. This is mainly for field callback methods to create inner nested or different type of fields |
|
269
|
|
|
* as instantiating a field object requires this value. |
|
270
|
|
|
* |
|
271
|
|
|
* This method is used from inside field classes especially for the 'revealer' custom field type that needs to create a field object |
|
272
|
|
|
* while processing the revealer field output. For that, the saved option array needs to be passed and accessing the property object was somewhat indirect |
|
273
|
|
|
* so there needs to be a direct method to retrieve the options. |
|
274
|
|
|
* |
|
275
|
|
|
* As of DEVVER, the form object will store the saved options by itself. And the revealer field type shuold use the form object method. |
|
276
|
|
|
* |
|
277
|
|
|
* @remark When the confirmation URL query key is set, it will merger the saved options with the last form input array, used for contact forms. |
|
278
|
|
|
* @since 3.3.0 |
|
279
|
|
|
* @since 3.3.1 Moved from `AdminPageFramework_Setting_Base`. Changed the visibility scope to `protected` as the caller method has moved to the view class. |
|
280
|
|
|
* @since 3.4.0 Changed the visibility scope to public. |
|
281
|
|
|
* @since 3.4.1 Changed the name from '_getSavedOptions()'. |
|
282
|
|
|
* @since DEVVER Moved from `AdminPageFramework_Model_Form`. |
|
283
|
|
|
* @remark assumes the `aSavedData` property is already set. |
|
284
|
|
|
* This is set when the form fields are registered. |
|
285
|
|
|
* @deprecated DEVVER Kept for backward compatibility. |
|
286
|
|
|
*/ |
|
287
|
|
|
public function getSavedOptions() { |
|
288
|
|
|
return $this->oForm->aSavedData; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* Returns the settings error array set by the user in the validation callback. |
|
293
|
|
|
* |
|
294
|
|
|
* The scope is public because it is accessed from outside ofo the class. This is mainly for field callback methods to create inner nested or different type of fields |
|
295
|
|
|
* as instantiating a field object requires this value. |
|
296
|
|
|
* |
|
297
|
|
|
* @since 3.4.0 |
|
298
|
|
|
*/ |
|
299
|
|
|
public function getFieldErrors() { |
|
300
|
|
|
return $this->oForm->getFieldErrors(); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Retrieves the settings error array set by the user in the validation callback. |
|
305
|
|
|
* |
|
306
|
|
|
* @since 3.0.4 |
|
307
|
|
|
* @since 3.6.3 Changed the visibility scope to public as a delegation class needs to access this method. |
|
308
|
|
|
* @since DEVVER Changed back the visibility scope to protected as there is the `getFieldErrors()` public method. |
|
309
|
|
|
* @access protected |
|
310
|
|
|
* @internal |
|
311
|
|
|
* @param string $sID deprecated |
|
|
|
|
|
|
312
|
|
|
* @param boolean $bDelete whether or not the transient should be deleted after retrieving it. |
|
|
|
|
|
|
313
|
|
|
* @return array |
|
314
|
|
|
* @deprecated DEVVER Use `getFieldErrors()` instead. Kept for backward compatibility. |
|
315
|
|
|
*/ |
|
316
|
|
|
protected function _getFieldErrors( /* $sID='deprecated', $bDelete=true */ ) { |
|
|
|
|
|
|
317
|
|
|
return $this->oForm->getFieldErrors(); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* Saves user last input in the database as a transient. |
|
322
|
|
|
* |
|
323
|
|
|
* To get the set input, call `$this->oProp->aLastInput`. |
|
324
|
|
|
* |
|
325
|
|
|
* @since 3.4.1 |
|
326
|
|
|
* @since DEVVER Changed the name from `_setLastInput()`. |
|
327
|
|
|
* @return boolean True if set; otherwise, false. |
|
328
|
|
|
* @internal |
|
329
|
|
|
*/ |
|
330
|
|
|
public function setLastInputs( array $aLastInputs ) { |
|
331
|
|
|
return $this->oForm->setLastInputs( $aLastInputs ); |
|
332
|
|
|
} |
|
333
|
|
|
/** |
|
334
|
|
|
* An alias of `_setLastInputs()`. |
|
335
|
|
|
* @deprecated DEVVER |
|
336
|
|
|
*/ |
|
337
|
|
|
public function _setLastInput( $aLastInputs ) { |
|
338
|
|
|
return $this->setLastInputs( $aLastInputs ); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
|
|
342
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.