|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly |
|
4
|
|
|
} |
|
5
|
|
|
/* |
|
6
|
|
|
* People can extend this class to create their own options |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class TitanFrameworkOption { |
|
11
|
|
|
|
|
12
|
|
|
const TYPE_META = 'meta'; |
|
13
|
|
|
const TYPE_ADMIN = 'option'; |
|
14
|
|
|
const TYPE_CUSTOMIZER = 'customizer'; |
|
15
|
|
|
|
|
16
|
|
|
public $settings; |
|
17
|
|
|
public $type; // One of the TYPE_* constants above |
|
18
|
|
|
public $owner; |
|
19
|
|
|
|
|
20
|
|
|
private static $rowIndex = 0; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Default settings across all options |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private static $defaultSettings = array( |
|
28
|
|
|
|
|
29
|
|
|
'type' => 'text', |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The name of the option, for display purposes only. |
|
33
|
|
|
* |
|
34
|
|
|
* @since 1.0 |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
'name' => '', |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The description to display together with this option. |
|
41
|
|
|
* |
|
42
|
|
|
* @since 1.0 |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
'desc' => '', |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* A unique ID for this option. This ID will be used to get the value for this option. |
|
49
|
|
|
* |
|
50
|
|
|
* @since 1.0 |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
'id' => '', |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* (Optional) The default value for this option. |
|
57
|
|
|
* |
|
58
|
|
|
* @since 1.0 |
|
59
|
|
|
* @var mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
'default' => '', |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* (Optional) jQuery code that updates something in your site in the live preview. Only used when the option is placed in a theme customizer section. |
|
65
|
|
|
* |
|
66
|
|
|
* @since 1.0 |
|
67
|
|
|
* @var string |
|
68
|
|
|
* @see http://www.titanframework.net/livepreview-parameter |
|
69
|
|
|
*/ |
|
70
|
|
|
'livepreview' => '', // jQuery script to update something in the site. For theme customizer only |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* (Optional) CSS rules to be used with this option. Only used when the option is placed in an admin page / panel or a theme customizer section. |
|
74
|
|
|
* @since 1.0 |
|
75
|
|
|
* @var string |
|
76
|
|
|
* @see http://www.titanframework.net/generate-css-automatically-for-your-options/ |
|
77
|
|
|
*/ |
|
78
|
|
|
'css' => '', |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* (Optional) If true, the option will not be displayed, but will still be accessible using `getOption`. This is helpful for deprecating old settings, while still making your project backward compatible. |
|
82
|
|
|
* @since 1.8 |
|
83
|
|
|
* @var bool |
|
84
|
|
|
*/ |
|
85
|
|
|
'hidden' => false, |
|
86
|
|
|
|
|
87
|
|
|
'example' => '', // An example value for this field, will be displayed in a <code> |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Default settings specific for this option. This is overridden by each option class |
|
92
|
|
|
* @var array |
|
93
|
|
|
*/ |
|
94
|
|
|
public $defaultSecondarySettings = array(); |
|
95
|
|
|
|
|
96
|
|
|
public static function factory( $settings, $owner ) { |
|
97
|
|
|
$settings = array_merge( self::$defaultSettings, $settings ); |
|
98
|
|
|
|
|
99
|
|
|
$className = 'TitanFrameworkOption' . str_replace( ' ', '', ucwords( str_replace( '-', ' ', $settings['type'] ) ) ); |
|
100
|
|
|
|
|
101
|
|
|
// assume all the classes are already required |
|
102
|
|
|
if ( ! class_exists( $className ) && ! class_exists( $settings['type'] ) ) { |
|
103
|
|
|
TitanFramework::displayFrameworkError( |
|
104
|
|
|
sprintf( __( 'Option type or extended class %s does not exist.', TF_I18NDOMAIN ), '<code>' . $settings['type'] . '</code>', $settings ), |
|
105
|
|
|
$settings ); |
|
106
|
|
|
return null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ( class_exists( $className ) ) { |
|
110
|
|
|
$obj = new $className( $settings, $owner ); |
|
111
|
|
|
return $obj; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$className = $settings['type']; |
|
115
|
|
|
$obj = new $className( $settings, $owner ); |
|
116
|
|
|
return $obj; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
function __construct( $settings, $owner ) { |
|
|
|
|
|
|
120
|
|
|
$this->owner = $owner; |
|
121
|
|
|
|
|
122
|
|
|
$this->settings = array_merge( self::$defaultSettings, $this->defaultSecondarySettings ); |
|
123
|
|
|
$this->settings = array_merge( $this->settings, $settings ); |
|
124
|
|
|
|
|
125
|
|
|
$this->type = is_a( $owner, 'TitanFrameworkMetaBox' ) ? self::TYPE_META : self::TYPE_ADMIN; |
|
126
|
|
|
$this->type = is_a( $owner, 'TitanFrameworkCustomizer' ) ? self::TYPE_CUSTOMIZER : $this->type; |
|
127
|
|
|
|
|
128
|
|
|
// Generate a unique ID depending on the settings for those without IDs |
|
129
|
|
|
if ( empty( $this->settings['id'] ) && $this->settings['type'] != 'save' ) { |
|
130
|
|
|
$this->settings['id'] = substr( md5( serialize( $this->settings ) . serialize( $this->owner->settings ) ), 0, 16 ); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
public function getValue( $postID = null ) { |
|
136
|
|
|
|
|
137
|
|
|
$value = false; |
|
138
|
|
|
|
|
139
|
|
|
if ( empty( $this->settings['id'] ) ) { |
|
140
|
|
|
return $value; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if ( $this->type == self::TYPE_ADMIN ) { |
|
144
|
|
|
|
|
145
|
|
|
$value = $this->getFramework()->getInternalAdminPageOption( $this->settings['id'], $this->settings['default'] ); |
|
146
|
|
|
|
|
147
|
|
|
} else if ( $this->type == self::TYPE_META ) { |
|
148
|
|
|
|
|
149
|
|
|
if ( empty( $postID ) ) { |
|
150
|
|
|
$postID = $this->owner->postID; |
|
151
|
|
|
} |
|
152
|
|
|
// If no $postID is given, try and get it if we are in a loop. |
|
153
|
|
|
if ( empty( $postID ) && ! is_admin() && get_post() != null ) { |
|
154
|
|
|
$postID = get_the_ID(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// for meta options, use the default value for new posts/pages |
|
158
|
|
|
if ( metadata_exists( 'post', $postID, $this->getID() ) ) { |
|
159
|
|
|
$value = get_post_meta( $postID, $this->getID(), true ); |
|
160
|
|
|
} else { |
|
161
|
|
|
$value = $this->settings['default']; |
|
162
|
|
|
} |
|
163
|
|
|
} else if ( $this->type == self::TYPE_CUSTOMIZER ) { |
|
164
|
|
|
$value = get_theme_mod( $this->getID(), $this->settings['default'] ); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Allow others to change the value of the option before it gets cleaned |
|
169
|
|
|
* |
|
170
|
|
|
* @since 1.9.2 |
|
171
|
|
|
*/ |
|
172
|
|
|
$value = apply_filters( 'tf_pre_get_value_' . $this->getOptionNamespace(), $value, $postID, $this ); |
|
173
|
|
|
|
|
174
|
|
|
// Apply cleaning method for the value (for serialized data, slashes, etc). |
|
175
|
|
|
$value = $this->cleanValueForGetting( $value ); |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Allow others to change the value of the option after it gets cleaned |
|
179
|
|
|
* |
|
180
|
|
|
* @since 1.9 |
|
181
|
|
|
*/ |
|
182
|
|
|
return apply_filters( 'tf_get_value_' . $this->settings['type'] . '_' . $this->getOptionNamespace(), $value, $postID, $this ); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* |
|
188
|
|
|
*/ |
|
189
|
|
|
public function setValue( $value, $postID = null ) { |
|
190
|
|
|
|
|
191
|
|
|
// Apply cleaning method for the value (for serialized data, slashes, etc). |
|
192
|
|
|
$value = $this->cleanValueForSaving( $value ); |
|
193
|
|
|
|
|
194
|
|
|
if ( $this->type == self::TYPE_ADMIN ) { |
|
195
|
|
|
|
|
196
|
|
|
$this->getFramework()->setInternalAdminPageOption( $this->settings['id'], $value ); |
|
197
|
|
|
|
|
198
|
|
|
} else if ( $this->type == self::TYPE_META ) { |
|
199
|
|
|
|
|
200
|
|
|
if ( empty( $postID ) ) { |
|
201
|
|
|
$postID = $this->owner->postID; |
|
202
|
|
|
} |
|
203
|
|
|
// If no $postID is given, try and get it if we are in a loop. |
|
204
|
|
|
if ( empty( $postID ) && ! is_admin() && get_post() != null ) { |
|
205
|
|
|
$postID = get_the_ID(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
update_post_meta( $postID, $this->getID(), $value ); |
|
209
|
|
|
|
|
210
|
|
|
} else if ( $this->type == self::TYPE_CUSTOMIZER ) { |
|
211
|
|
|
|
|
212
|
|
|
set_theme_mod( $this->getID(), $value ); |
|
213
|
|
|
|
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
do_action( 'tf_set_value_' . $this->settings['type'] . '_' . $this->getOptionNamespace(), $value, $postID, $this ); |
|
217
|
|
|
|
|
218
|
|
|
return true; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Gets the framework instance currently used |
|
224
|
|
|
* |
|
225
|
|
|
* @return TitanFramework |
|
226
|
|
|
* @since 1.3 |
|
227
|
|
|
*/ |
|
228
|
|
|
protected function getFramework() { |
|
229
|
|
|
if ( is_a( $this->owner, 'TitanFrameworkAdminTab' ) ) { |
|
230
|
|
|
// a tab's parent is an admin panel |
|
231
|
|
|
return $this->owner->owner->owner; |
|
232
|
|
|
} else { |
|
233
|
|
|
// an admin panel's parent is the framework |
|
234
|
|
|
// a meta panel's parent is the framework |
|
235
|
|
|
// a theme customizer's parent is the framework |
|
236
|
|
|
return $this->owner->owner; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Gets the option namespace used in the framework instance currently used |
|
243
|
|
|
* |
|
244
|
|
|
* @return string The option namespace |
|
245
|
|
|
* @since 1.0 |
|
246
|
|
|
*/ |
|
247
|
|
|
public function getOptionNamespace() { |
|
248
|
|
|
return $this->getFramework()->optionNamespace; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
public function getID() { |
|
252
|
|
|
return $this->getOptionNamespace() . '_' . $this->settings['id']; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
public function __call( $name, $args ) { |
|
256
|
|
|
$default = is_array( $args ) && count( $args ) ? $args[0] : ''; |
|
257
|
|
|
if ( stripos( $name, 'get' ) == 0 ) { |
|
258
|
|
|
$setting = strtolower( substr( $name, 3 ) ); |
|
259
|
|
|
return empty( $this->settings[ $setting ] ) ? $default : $this->settings[ $setting ]; |
|
260
|
|
|
} |
|
261
|
|
|
return $default; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
protected function echoOptionHeader( $showDesc = false ) { |
|
265
|
|
|
// Allow overriding for custom styling |
|
266
|
|
|
$useCustom = false; |
|
267
|
|
|
$useCustom = apply_filters( 'tf_use_custom_option_header', $useCustom ); |
|
268
|
|
|
$useCustom = apply_filters( 'tf_use_custom_option_header_' . $this->getOptionNamespace(), $useCustom ); |
|
269
|
|
|
if ( $useCustom ) { |
|
270
|
|
|
do_action( 'tf_custom_option_header', $this ); |
|
271
|
|
|
do_action( 'tf_custom_option_header_' . $this->getOptionNamespace(), $this ); |
|
272
|
|
|
return; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
$id = $this->getID(); |
|
276
|
|
|
$name = $this->getName(); |
|
|
|
|
|
|
277
|
|
|
$evenOdd = self::$rowIndex++ % 2 == 0 ? 'odd' : 'even'; |
|
278
|
|
|
|
|
279
|
|
|
$style = $this->getHidden() == true ? 'style="display: none"' : ''; |
|
|
|
|
|
|
280
|
|
|
|
|
281
|
|
|
?> |
|
282
|
|
|
<tr valign="top" class="row-<?php echo self::$rowIndex ?> <?php echo $evenOdd ?>" <?php echo $style ?>> |
|
283
|
|
|
<th scope="row" class="first"> |
|
284
|
|
|
<label for="<?php echo ! empty( $id ) ? $id : '' ?>"><?php echo ! empty( $name ) ? $name : '' ?></label> |
|
285
|
|
|
</th> |
|
286
|
|
|
<td class="second tf-<?php echo $this->settings['type'] ?>"> |
|
287
|
|
|
<?php |
|
288
|
|
|
|
|
289
|
|
|
$desc = $this->getDesc(); |
|
|
|
|
|
|
290
|
|
|
if ( ! empty( $desc ) && $showDesc ) : |
|
291
|
|
|
?> |
|
292
|
|
|
<p class='description'><?php echo $desc ?></p> |
|
293
|
|
|
<?php |
|
294
|
|
|
endif; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
protected function echoOptionHeaderBare() { |
|
298
|
|
|
// Allow overriding for custom styling |
|
299
|
|
|
$useCustom = false; |
|
300
|
|
|
$useCustom = apply_filters( 'tf_use_custom_option_header', $useCustom ); |
|
301
|
|
|
$useCustom = apply_filters( 'tf_use_custom_option_header_' . $this->getOptionNamespace(), $useCustom ); |
|
302
|
|
|
if ( $useCustom ) { |
|
303
|
|
|
do_action( 'tf_custom_option_header', $this ); |
|
304
|
|
|
do_action( 'tf_custom_option_header_' . $this->getOptionNamespace(), $this ); |
|
305
|
|
|
return; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
$id = $this->getID(); |
|
309
|
|
|
$name = $this->getName(); |
|
|
|
|
|
|
310
|
|
|
$evenOdd = self::$rowIndex++ % 2 == 0 ? 'odd' : 'even'; |
|
311
|
|
|
|
|
312
|
|
|
$style = $this->getHidden() == true ? 'style="display: none"' : ''; |
|
|
|
|
|
|
313
|
|
|
|
|
314
|
|
|
?> |
|
315
|
|
|
<tr valign="top" class="row-<?php echo self::$rowIndex ?> <?php echo $evenOdd ?>" <?php echo $style ?>> |
|
316
|
|
|
<td class="second tf-<?php echo $this->settings['type'] ?>"> |
|
317
|
|
|
<?php |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
protected function echoOptionFooter( $showDesc = true ) { |
|
321
|
|
|
// Allow overriding for custom styling |
|
322
|
|
|
$useCustom = false; |
|
323
|
|
|
$useCustom = apply_filters( 'tf_use_custom_option_footer', $useCustom ); |
|
324
|
|
|
$useCustom = apply_filters( 'tf_use_custom_option_footer_' . $this->getOptionNamespace(), $useCustom ); |
|
325
|
|
|
if ( $useCustom ) { |
|
326
|
|
|
do_action( 'tf_custom_option_footer', $this ); |
|
327
|
|
|
do_action( 'tf_custom_option_footer_' . $this->getOptionNamespace(), $this ); |
|
328
|
|
|
return; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
$desc = $this->getDesc(); |
|
|
|
|
|
|
332
|
|
|
if ( ! empty( $desc ) && $showDesc ) : |
|
333
|
|
|
?> |
|
334
|
|
|
<p class='description'><?php echo $desc ?></p> |
|
335
|
|
|
<?php |
|
336
|
|
|
endif; |
|
337
|
|
|
|
|
338
|
|
|
$example = $this->getExample(); |
|
|
|
|
|
|
339
|
|
|
if ( ! empty( $example ) ) : |
|
340
|
|
|
?> |
|
341
|
|
|
<p class="description"><code><?php echo htmlentities( $example ) ?></code></p> |
|
342
|
|
|
<?php |
|
343
|
|
|
endif; |
|
344
|
|
|
|
|
345
|
|
|
?> |
|
346
|
|
|
</td> |
|
347
|
|
|
</tr> |
|
348
|
|
|
<?php |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
protected function echoOptionFooterBare( $showDesc = true ) { |
|
|
|
|
|
|
352
|
|
|
// Allow overriding for custom styling |
|
353
|
|
|
$useCustom = false; |
|
354
|
|
|
$useCustom = apply_filters( 'tf_use_custom_option_footer', $useCustom ); |
|
355
|
|
|
$useCustom = apply_filters( 'tf_use_custom_option_footer_' . $this->getOptionNamespace(), $useCustom ); |
|
356
|
|
|
if ( $useCustom ) { |
|
357
|
|
|
do_action( 'tf_custom_option_footer', $this ); |
|
358
|
|
|
do_action( 'tf_custom_option_footer_' . $this->getOptionNamespace(), $this ); |
|
359
|
|
|
return; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
?> |
|
363
|
|
|
</td> |
|
364
|
|
|
</tr> |
|
365
|
|
|
<?php |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/* overridden */ |
|
369
|
|
|
public function display() { |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/* overridden */ |
|
373
|
|
|
public function cleanValueForSaving( $value ) { |
|
374
|
|
|
return $value; |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
/* overridden */ |
|
378
|
|
|
public function cleanValueForGetting( $value ) { |
|
379
|
|
|
if ( is_array( $value ) ) { |
|
380
|
|
|
return $value; |
|
381
|
|
|
} |
|
382
|
|
|
return stripslashes( $value ); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/* overridden */ |
|
386
|
|
|
public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) { |
|
387
|
|
|
|
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.