|
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
|
|
|
* A back-end factory class that redirects callback methods to the main widget class. |
|
12
|
|
|
* |
|
13
|
|
|
* @abstract |
|
14
|
|
|
* @since 3.2.0 |
|
15
|
|
|
* @package AdminPageFramework |
|
16
|
|
|
* @subpackage Widget |
|
17
|
|
|
* @extends WP_Widget |
|
18
|
|
|
* @internal |
|
19
|
|
|
*/ |
|
20
|
|
|
class AdminPageFramework_Widget_Factory extends WP_Widget { |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Sets up internal properties. |
|
24
|
|
|
* |
|
25
|
|
|
* @since 3.2.0 |
|
26
|
|
|
* @return void |
|
|
|
|
|
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct( $oCaller, $sWidgetTitle, array $aArguments=array() ) { |
|
29
|
|
|
|
|
30
|
|
|
$aArguments = $aArguments |
|
31
|
|
|
+ array( |
|
32
|
|
|
'classname' => 'admin_page_framework_widget', |
|
33
|
|
|
'description' => '', |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
parent::__construct( |
|
37
|
|
|
$oCaller->oProp->sClassName, // base id |
|
38
|
|
|
$sWidgetTitle, // widget title |
|
39
|
|
|
$aArguments // widget arguments |
|
40
|
|
|
); |
|
41
|
|
|
$this->oCaller = $oCaller; |
|
42
|
|
|
|
|
43
|
|
|
// Set up callbacks for field element outputs such as for name and it attributes. |
|
44
|
|
|
$this->oCaller->oProp->aFormCallbacks = array( |
|
45
|
|
|
'hfID' => array( $this, 'get_field_id' ), // defined in the WP_Widget class. |
|
46
|
|
|
'hfTagID' => array( $this, 'get_field_id' ), // defined in the WP_Widget class. |
|
47
|
|
|
'hfName' => array( $this, '_replyToGetFieldName' ), // defined in the WP_Widget class. |
|
48
|
|
|
'hfInputName' => array( $this, '_replyToGetFieldInputName' ), |
|
49
|
|
|
// 'hfClass' => array( $this, '_replyToAddClassSelector' ), |
|
|
|
|
|
|
50
|
|
|
// 'hfNameFlat' => array( $this, '_replyToGetFlatFieldName' ), // @deprecated 3.6.0+ the same as the framework factory method. |
|
|
|
|
|
|
51
|
|
|
) + $this->oCaller->oProp->aFormCallbacks; |
|
52
|
|
|
$this->oCaller->oForm->aCallbacks = $this->oCaller->oProp->aFormCallbacks + $this->oCaller->oForm->aCallbacks; |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Displays the widget contents in the front end. |
|
59
|
|
|
* |
|
60
|
|
|
* @since 3.2.0 |
|
61
|
|
|
* @since 3.5.9 Changed the timing of the hooks (do_{...} and content_{...} ) to allow the user to decide |
|
62
|
|
|
* whether the title should be visible or not depending on the content. |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
public function widget( $aArguments, $aFormData ) { |
|
66
|
|
|
|
|
67
|
|
|
echo $aArguments[ 'before_widget' ]; |
|
68
|
|
|
|
|
69
|
|
|
$this->oCaller->oUtil->addAndDoActions( |
|
70
|
|
|
$this->oCaller, |
|
71
|
|
|
'do_' . $this->oCaller->oProp->sClassName, |
|
72
|
|
|
$this->oCaller |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
$_sContent = $this->oCaller->oUtil->addAndApplyFilters( |
|
76
|
|
|
$this->oCaller, |
|
77
|
|
|
"content_{$this->oCaller->oProp->sClassName}", |
|
78
|
|
|
$this->oCaller->content( '', $aArguments, $aFormData ), |
|
79
|
|
|
$aArguments, |
|
80
|
|
|
$aFormData |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
// 3.5.9+ Moved this after the content_{...} filter hook so that the user can decide whether the title shoudl be visible or not. |
|
84
|
|
|
echo $this->_getTitle( $aArguments, $aFormData ); |
|
85
|
|
|
|
|
86
|
|
|
echo $_sContent; |
|
87
|
|
|
|
|
88
|
|
|
echo $aArguments[ 'after_widget' ]; |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
/** |
|
92
|
|
|
* Returns the widget title. |
|
93
|
|
|
* |
|
94
|
|
|
* @since 3.5.7 |
|
95
|
|
|
* @remark The user needs to add a field with the id, `title` to display a title. |
|
96
|
|
|
* @remark In order to disable the title, add a field with the id `show_title` and if the value yields `false`, |
|
97
|
|
|
* the title will not be displayed. |
|
98
|
|
|
* @return string The widget title |
|
99
|
|
|
*/ |
|
100
|
|
|
private function _getTitle( array $aArguments, array $aFormData ) { |
|
101
|
|
|
|
|
102
|
|
|
if ( ! $this->oCaller->oProp->bShowWidgetTitle ) { |
|
103
|
|
|
return ''; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$_sTitle = apply_filters( |
|
107
|
|
|
'widget_title', |
|
108
|
|
|
$this->oCaller->oUtil->getElement( |
|
109
|
|
|
$aFormData, |
|
110
|
|
|
'title', |
|
111
|
|
|
'' |
|
112
|
|
|
), |
|
113
|
|
|
$aFormData, |
|
114
|
|
|
$this->id_base |
|
115
|
|
|
); |
|
116
|
|
|
if ( ! $_sTitle ) { |
|
117
|
|
|
return ''; |
|
118
|
|
|
} |
|
119
|
|
|
return $aArguments['before_title'] |
|
120
|
|
|
. $_sTitle |
|
121
|
|
|
. $aArguments['after_title']; |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Validates the submitted form data. |
|
127
|
|
|
* |
|
128
|
|
|
* @since 3.2.0 |
|
129
|
|
|
* @return mixed The validated form data. The type should be an array but it is dealt by the framework user it will be unknown. |
|
130
|
|
|
*/ |
|
131
|
|
|
public function update( $aSubmittedFormData, $aSavedFormData ) { |
|
132
|
|
|
|
|
133
|
|
|
return $this->oCaller->oUtil->addAndApplyFilters( |
|
134
|
|
|
$this->oCaller, |
|
135
|
|
|
"validation_{$this->oCaller->oProp->sClassName}", |
|
136
|
|
|
call_user_func_array( |
|
137
|
|
|
array( $this->oCaller, 'validate' ), // triggers __call() |
|
138
|
|
|
array( $aSubmittedFormData, $aSavedFormData, $this->oCaller ) // parameters |
|
139
|
|
|
), // 3.5.3+ |
|
140
|
|
|
$aSavedFormData, |
|
141
|
|
|
$this->oCaller |
|
142
|
|
|
); |
|
143
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Constructs the widget form with the given saved form data. |
|
148
|
|
|
* |
|
149
|
|
|
* In widgets.php, this method is called multiple times per instance of the class defining the widget (widget model). |
|
150
|
|
|
* It is called for the number of added widget instances via drag-and-drop in the UI |
|
151
|
|
|
* of the widget model that the caller class defines. |
|
152
|
|
|
* |
|
153
|
|
|
* This means, the framework factory class has to renew the saved data every time this method is called. |
|
154
|
|
|
* |
|
155
|
|
|
* @return void |
|
156
|
|
|
*/ |
|
157
|
|
|
public function form( $aFormData ) { |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Set the form data - the form object will trigger a callback to construct the saved form data. |
|
161
|
|
|
* And the factory abstract class has a defined method (_replyToGetSavedFormData()) for it |
|
162
|
|
|
* and it applies a filter (options_{...}) to the form data (options) array. |
|
163
|
|
|
*/ |
|
164
|
|
|
$this->oCaller->oProp->aOptions = $aFormData; |
|
165
|
|
|
|
|
166
|
|
|
// The hook (load_{...}) in the method triggers the form registration method. |
|
167
|
|
|
$this->_loadFrameworkFactory(); |
|
168
|
|
|
|
|
169
|
|
|
// Render the form |
|
170
|
|
|
$this->oCaller->_printWidgetForm(); |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Initialize the form object that stores registered sections and fields |
|
174
|
|
|
* because this class gets called multiple times to render the form including added widgets |
|
175
|
|
|
* and the initial widget that gets listed on the left hand side of the page. |
|
176
|
|
|
* |
|
177
|
|
|
* @since 3.5.2 |
|
178
|
|
|
*/ |
|
179
|
|
|
$this->oCaller->oForm = new AdminPageFramework_Form_widget( |
|
180
|
|
|
array( |
|
181
|
|
|
'register_if_action_already_done' => false, // do not register fields right away |
|
182
|
|
|
) + $this->oCaller->oProp->aFormArguments, // form arguments |
|
183
|
|
|
$this->oCaller->oProp->aFormCallbacks, // callbacks |
|
184
|
|
|
$this->oCaller->oMsg |
|
185
|
|
|
); |
|
186
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
/** |
|
189
|
|
|
* Calls the load() method of the caller factory object. |
|
190
|
|
|
* |
|
191
|
|
|
* Ensures it is called once per a page load. |
|
192
|
|
|
* @since DEVVER |
|
193
|
|
|
*/ |
|
194
|
|
|
private function _loadFrameworkFactory() { |
|
195
|
|
|
|
|
196
|
|
|
// Trigger the load() method and load_{...} actions. The user sets up the form. |
|
197
|
|
|
$this->oCaller->load( $this->oCaller ); |
|
198
|
|
|
$this->oCaller->oUtil->addAndDoActions( |
|
199
|
|
|
$this->oCaller, |
|
200
|
|
|
array( |
|
201
|
|
|
'load_' . $this->oCaller->oProp->sClassName, |
|
202
|
|
|
), |
|
203
|
|
|
$this->oCaller |
|
204
|
|
|
); |
|
205
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* |
|
210
|
|
|
* @remark This one is tricky as the core widget factory method enclose this value in []. So when the framework field has a section, it must NOT end with ]. |
|
211
|
|
|
* @since 3.5.7 Moved from `AdminPageFramework_FormField`. |
|
212
|
|
|
* @since 3.6.0 Changed the name from `_replyToGetInputName`. |
|
213
|
|
|
* @return string |
|
214
|
|
|
*/ |
|
215
|
|
|
public function _replyToGetFieldName( /* $sNameAttribute, array $aField */ ) { |
|
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
$_aParams = func_get_args() + array( null, null, null ); |
|
218
|
|
|
$aField = $_aParams[ 1 ]; |
|
219
|
|
|
|
|
220
|
|
|
$_sSectionIndex = isset( $aField['section_id'], $aField['_section_index'] ) |
|
221
|
|
|
? "[{$aField['_section_index']}]" |
|
222
|
|
|
: ""; |
|
223
|
|
|
$_sID = $this->oCaller->isSectionSet( $aField ) |
|
224
|
|
|
? $aField['section_id'] . "]" . $_sSectionIndex . "[" . $aField[ 'field_id' ] |
|
225
|
|
|
: $aField['field_id']; |
|
226
|
|
|
return $this->get_field_name( $_sID ); |
|
227
|
|
|
|
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* |
|
232
|
|
|
* @since 3.6.0 |
|
233
|
|
|
* @return string |
|
234
|
|
|
*/ |
|
235
|
|
|
public function _replyToGetFieldInputName( /* $sNameAttribute, array $aField, $sIndex */ ) { |
|
|
|
|
|
|
236
|
|
|
|
|
237
|
|
|
$_aParams = func_get_args() + array( null, null, null ); |
|
238
|
|
|
$aField = $_aParams[ 1 ]; |
|
239
|
|
|
$sIndex = $_aParams[ 2 ]; |
|
240
|
|
|
|
|
241
|
|
|
$_sIndex = $this->oCaller->oUtil->getAOrB( |
|
242
|
|
|
'0' !== $sIndex && empty( $sIndex ), |
|
243
|
|
|
'', |
|
244
|
|
|
"[" . $sIndex . "]" |
|
245
|
|
|
); |
|
246
|
|
|
$_sSectionIndex = isset( $aField['section_id'], $aField['_section_index'] ) |
|
247
|
|
|
? "[{$aField['_section_index']}]" |
|
248
|
|
|
: ""; |
|
249
|
|
|
$_sID = $this->oCaller->isSectionSet( $aField ) |
|
250
|
|
|
? $aField['section_id'] . "]" . $_sSectionIndex . "[" . $aField[ 'field_id' ] |
|
251
|
|
|
: $aField[ 'field_id' ]; |
|
252
|
|
|
return $this->get_field_name( $_sID ) . $_sIndex; |
|
253
|
|
|
|
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Returns the flat input name. |
|
258
|
|
|
* |
|
259
|
|
|
* A flat input name is a 'name' attribute value whose dimensional elements are delimited by the pile character. |
|
260
|
|
|
* |
|
261
|
|
|
* Instead of [] enclosing array elements, it uses the pipe(|) to represent the multi dimensional array key. |
|
262
|
|
|
* This is used to create a reference to the submit field name to determine which button is pressed. |
|
263
|
|
|
* |
|
264
|
|
|
* @since 3.5.7 Moved from `AdminPageFramework_FormField`. |
|
265
|
|
|
* @since 3.6.0 Changed the name from `_replyToGetFlatInputName()`. |
|
266
|
|
|
* @return string |
|
267
|
|
|
* @deprecated |
|
268
|
|
|
*/ |
|
269
|
|
|
// protected function _replyToGetFlatFieldName( /* $sFlatInputName, array $aField */ ) { |
|
|
|
|
|
|
270
|
|
|
// $_aParams = func_get_args() + array( null, null ); |
|
|
|
|
|
|
271
|
|
|
// $aField = $_aParams[ 1 ]; |
|
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
// $_sSectionIndex = isset( $aField['section_id'], $aField['_section_index'] ) |
|
|
|
|
|
|
275
|
|
|
// ? "|{$aField['_section_index']}" |
|
276
|
|
|
// : ''; |
|
277
|
|
|
// $sFlatInputName = $this->oCaller->isSectionSet( $aField ) |
|
|
|
|
|
|
278
|
|
|
// ? "{$aField['section_id']}{$_sSectionIndex}|{$aField['field_id']}" |
|
279
|
|
|
// : "{$aField['field_id']}"; |
|
280
|
|
|
// return $sFlatInputName; |
|
281
|
|
|
// } |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Modifies the class Selector. |
|
285
|
|
|
* |
|
286
|
|
|
* @since 3.2.0 |
|
287
|
|
|
* @remark currently not used |
|
288
|
|
|
* @deprecated |
|
289
|
|
|
*/ |
|
290
|
|
|
// public function _replyToAddClassSelector( $sClassSelectors ) { |
|
|
|
|
|
|
291
|
|
|
|
|
292
|
|
|
// $sClassSelectors .= ' widefat'; |
|
|
|
|
|
|
293
|
|
|
// return trim( $sClassSelectors ); |
|
|
|
|
|
|
294
|
|
|
|
|
295
|
|
|
// } |
|
296
|
|
|
|
|
297
|
|
|
} |
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.