Skip to content

Commit 09547a9

Browse files
committed
Version 2.6.0
= Version 2.6.0 – May 10, 2024 =
1 parent f6f6b62 commit 09547a9

25 files changed

+1748
-306
lines changed

Extras/Extensions/myAwesomeExtension/Extensions/class.myAwesomeExtension.extension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @category WordPress Plugin
66
* @package {eac}Doojigger
77
* @author Kevin Burkholder <KBurkholder@EarthAsylum.com>
8-
* @copyright Copyright (c) 2022 EarthAsylum Consulting <www.earthasylum.com>
8+
* @copyright Copyright (c) 2024 EarthAsylum Consulting <www.earthasylum.com>
99
* @version 1.x
1010
* @uses EarthAsylumConsulting\eacDoojigger
1111
*/
@@ -17,7 +17,7 @@ class myAwesomeExtension extends \EarthAsylumConsulting\abstract_extension
1717
/**
1818
* @var string extension version
1919
*/
20-
const VERSION = '22.1108.1';
20+
const VERSION = '24.0510.1';
2121

2222

2323
/**
@@ -53,7 +53,7 @@ public function admin_options_settings()
5353
$this->registerExtensionOptions( $this->className,
5454
[
5555
'my_option_name' => array(
56-
'type' => 'type: {input type}',
56+
'type' => 'text',
5757
'label' => 'label: {field label}',
5858
'title' => 'title: information text/html to be displayed',
5959
// 'options' => array({option,...}),

Extras/Extensions/myAwesomeExtension/myAwesomeExtension.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
* @category WordPress Plugin
66
* @package myAwesomePlugin
77
* @author Kevin Burkholder <KBurkholder@EarthAsylum.com>
8-
* @copyright Copyright (c) 2022 EarthAsylum Consulting <www.earthasylum.com>
8+
* @copyright Copyright (c) 2024 EarthAsylum Consulting <www.earthasylum.com>
99
* @version 1.x
1010
*
1111
* @wordpress-plugin
1212
* Plugin Name: My Awesome Extension
1313
* Description: My Awesome Extension
14-
* Version: 1.1.0
15-
* Requires at least: 5.5
16-
* Requires PHP: 7.2
14+
* Version: 1.1.1
15+
* Requires at least: 5.8
16+
* Requires PHP: 7.4
1717
* Author: Kevin Burkholder @ EarthAsylum Consulting
1818
* Author URI: http://www.earthasylum.com
1919
*/

Extras/OptionMetaData/myOptionsTest/EarthAsylumConsulting/Plugin/myOptionsTest.plugin.php

Lines changed: 128 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
/**
33
* EarthAsylum Consulting {eac}Doojigger derivative
44
*
5+
* Provides examples of input types, parameters, and processing callbacks & filters.
6+
*
57
* @category WordPress Plugin
68
* @package myOptionsTest, {eac}Doojigger derivative
79
* @author Kevin Burkholder <KBurkholder@EarthAsylum.com>
8-
* @copyright Copyright (c) 2023 EarthAsylum Consulting <www.earthasylum.com>
9-
* @version 1.x
10+
* @copyright Copyright (c) 2024 EarthAsylum Consulting <www.earthasylum.com>
11+
* @version 24.0426.1
1012
*/
1113

1214
namespace EarthAsylumConsulting\Plugin;
@@ -94,78 +96,108 @@ public function admin_options_settings()
9496
foreach (self::INPUT_TYPES as $type)
9597
{
9698
$typeId = str_replace('-','_',$type);
97-
/* define the input meta-data array */
98-
$options[ "input_{$typeId}" ] = array(
99-
'type' => $type,
100-
'title' => "HTML input type: {$type}",
101-
'label' => ucwords(str_replace('-',' ',$type)),
102-
'before' => '<span class="dashicons dashicons-arrow-left-alt2"></span>',
103-
'options' => [$type=>$type, 'option2'=>'2', 'option3'=>'3'],
104-
'default' => $type,
105-
'after' => '<span class="dashicons dashicons-arrow-right-alt2"></span>',
106-
'info' => "Saved option name: '".$this->prefixOptionName("input_{$typeId}")."'",
107-
// 'class' => "{$type}_class",
108-
// 'style' => "max-width: 50em;",
109-
/* attributes as a string */
110-
'attributes' => "placeholder='{$type}' alt='{$type} input' title='input type: {$type}'",
111-
/* attributes as array of strings */
112-
// 'attributes' => [ "placeholder='{$type}'", "alt='{$type} input'", "title='input type: {$type}'" ],
113-
/* attributes as associative array */
114-
// 'attributes' => [ 'placeholder'=>$type, 'alt'=>"{$type} input", 'title'=>"input type: {$type}" ],
115-
/* attributes as array of [strings] */
116-
// 'attributes' => [ ["data-1"], ["data-2"], ["data-3"=>"data-3"] ],
117-
'sanitize' => [ $this,'sanitize_callback' ],
118-
'filter' => [ FILTER_CALLBACK, ['options'=>[$this,'filter_callback']] ],
119-
'validate' => [ $this,'validate_callback' ],
120-
// contextual help using meta ([title],[type],[info]) macros
121-
'help' => "<details><summary>[title]</summary>This field is an HTML input type '[type]'<br>[info]</details>",
99+
/*
100+
define the input meta-data array.
101+
normally fields are defineed individually with appropriate parameters for each.
102+
here we use a foreach loop for all input types where some parameters may not be applicable.
103+
*/
104+
$options[ "input_{$typeId}" ] = array
105+
(
106+
'type' => $type,
107+
'title' => "HTML input type: {$type}",
108+
'label' => ucwords(str_replace('-',' ',$type)),
109+
'before' => '<span class="dashicons dashicons-arrow-left-alt2"></span>',
110+
'options' => [$type=>$type, 'option2'=>'2', 'option3'=>'3'],
111+
'default' => $type,
112+
'after' => '<span class="dashicons dashicons-arrow-right-alt2"></span>',
113+
'info' => "Saved option name: '".$this->prefixOptionName("input_{$typeId}")."'",
114+
// info is converted to tooltip if tooltip isn't set, here we do both.
115+
'tooltip' => "This field is an HTML input type '[type]'",
116+
// 'class' => "{$type}_class",
117+
// 'style' => "max-width: 50em;",
118+
/* attributes as a string */
119+
'attributes' => "placeholder='{$type}' alt='{$type} input' title='input type: {$type}'",
120+
/* attributes as array of strings */
121+
// 'attributes' => [ "placeholder='{$type}'", "alt='{$type} input'", "title='input type: {$type}'" ],
122+
/* attributes as associative array */
123+
// 'attributes' => [ 'placeholder'=>$type, 'alt'=>"{$type} input", 'title'=>"input type: {$type}" ],
124+
/* attributes as array of [strings] */
125+
// 'attributes' => [ ["data-1"], ["data-2"], ["data-3"=>"data-3"] ],
126+
'sanitize' => [ $this,'sanitize_callback' ],
127+
'filter' => [ FILTER_CALLBACK, ['options'=>[$this,'filter_callback']] ],
128+
'validate' => [ $this,'validate_callback' ],
129+
// contextual help using meta ([title],[tooltip],[info]) macros
130+
'help' => "<details><summary>[title]</summary>[tooltip]<br>[info]</details>",
122131
);
123132

124-
/* display values when changed */
133+
/* display values when changed for these fields */
125134
if (in_array($type,['color','date','datetime-local','month','time','week']))
126135
{
127-
$options["input_{$typeId}"]['attributes'] = ['oninput'=>"input_{$typeId}_show.value = this.value" ];
128-
$options["input_{$typeId}"]['after'] .= "<output name='input_{$typeId}_show' for='input_{$typeId}' style='padding:2em;color:blue;'>...</output>";
136+
$options["input_{$typeId}"]
137+
['attributes'] = ['oninput'=>"input_{$typeId}_show.value = this.value"];
138+
$options["input_{$typeId}"]
139+
['after'] .= "<output name='input_{$typeId}_show' for='input_{$typeId}' style='padding:2em;color:blue;'>...</output>";
140+
}
141+
142+
/* don't use generic callbacks for file upload, see my_form_post_file() */
143+
if ($type == 'file')
144+
{
145+
unset( $options["input_{$typeId}"]['sanitize'],
146+
$options["input_{$typeId}"]['filter'],
147+
$options["input_{$typeId}"]['validate']
148+
);
129149
}
130150

131151
/* add output display and formatted data-points to our range input */
132152
if ($type == 'range')
133153
{
134-
$options["input_{$typeId}"]['attributes'] = ['min="0"', 'max="10"','step="1"', "list='input_{$typeId}_ticks'",
135-
'oninput'=>"input_{$typeId}_show.value = this.value" ];
136-
$options["input_{$typeId}"]['default'] = 5;
137-
$options["input_{$typeId}"]['after'] .= "<output name='input_{$typeId}_show' for='input_{$typeId}' style='padding:2em;color:blue;'></output>".
138-
"<datalist id='input_{$typeId}_ticks'>".
139-
'<option value="0" label="0"></option>'.
140-
'<option value="1"></option>'.
141-
'<option value="2" label="2"></option>'.
142-
'<option value="3"></option>'.
143-
'<option value="4" label="4"></option>'.
144-
'<option value="5"></option>'.
145-
'<option value="6" label="6"></option>'.
146-
'<option value="7"></option>'.
147-
'<option value="8" label="8"></option>'.
148-
'<option value="9"></option>'.
149-
'<option value="10" label="10"></option>'.
150-
'</datalist>';
154+
unset($options["input_{$typeId}"]['before']);
155+
$options["input_{$typeId}"]
156+
['attributes'] = ['min="0"', 'max="10"','step=".5"', "list='input_{$typeId}_ticks'",
157+
'oninput'=>"input_{$typeId}_show.value = this.value"];
158+
$options["input_{$typeId}"]
159+
['default'] = 5;
160+
$options["input_{$typeId}"]
161+
['after'] =
162+
"<datalist id='input_{$typeId}_ticks'>".
163+
'<option value="0" label="0"></option>'.
164+
'<option value="1"></option>'.
165+
'<option value="2" label="2"></option>'.
166+
'<option value="3"></option>'.
167+
'<option value="4" label="4"></option>'.
168+
'<option value="5"></option>'.
169+
'<option value="6" label="6"></option>'.
170+
'<option value="7"></option>'.
171+
'<option value="8" label="8"></option>'.
172+
'<option value="9"></option>'.
173+
'<option value="10" label="10"></option>'.
174+
'</datalist>'.
175+
"Range input: <code>".
176+
"<output name='input_{$typeId}_show' for='input_{$typeId}' ".
177+
"style='padding:1em;color:blue;'>[value]</output>".
178+
"</code>";
151179
}
152180
}
153181

154182
/* register this plugin with options */
155183
$this->registerPluginOptions('plugin_settings',$options);
156184

157-
/* filters to handle 'custom' input field ('input_custom is field name)*/
158-
$this->add_filter( 'options_form_input_input_custom', array($this, 'options_form_input_custom'), 10, 4 );
159-
$this->add_filter( 'options_form_post_input_custom', array($this, 'options_form_post_custom'), 10, 4 );
160185

161-
/* filter to handle 'file' input field ('input_file is field name)*/
162-
$this->add_filter( 'options_form_post_input_file', array($this, 'options_form_post_file'), 10, 4 );
186+
/* filters to handle 'custom' input field ('input_custom' is field name)*/
187+
$this->add_filter( 'options_form_input_input_custom', array($this, 'my_form_input_custom'), 10, 4 );
188+
$this->add_filter( 'options_form_post_input_custom', array($this, 'my_form_post_custom'), 10, 4 );
189+
190+
191+
/* filter to handle 'file' input field ('input_file' is field name)*/
192+
$this->add_filter( 'options_form_post_input_file', array($this, 'my_form_post_file'), 10, 4 );
193+
194+
195+
/* filter to sanitize all fields individually (redundant when using above 'sanitize' callback) */
196+
$this->add_filter( 'sanitize_option', array($this, 'my_option_sanitize'), 10, 4 );
163197

164-
/* filter to sanitize all fields */
165-
$this->add_filter( 'sanitize_option', array($this, 'sanitize_options'), 10, 4 );
166198

167199
/* action after form posted and fields updated */
168-
// $this->add_action( 'options_form_post', array($this, 'my_options_form_post') );
200+
$this->add_action( 'options_form_post', array($this, 'my_options_form_post') );
169201
}
170202

171203

@@ -210,7 +242,10 @@ public function addActionsAndFilters(): void
210242
{
211243
parent::addActionsAndFilters();
212244

213-
/* custom stylesheet action - add formatting for our input_range data-points */
245+
/*
246+
custom stylesheet action - called when stylesheet is enqueued
247+
add formatting for our input_range data-points
248+
*/
214249
$this->add_action('admin_enqueue_styles', function($styleId)
215250
{
216251
$style =
@@ -219,24 +254,24 @@ public function addActionsAndFilters(): void
219254
"display: flex; width: 80%; max-width: 38em;".
220255
"justify-content: space-between;".
221256
"font-size: 0.85em; color:blue;".
222-
"padding-left: 2em;".
223-
"}\n";
257+
"padding-left: .2em;".
258+
"}";
224259

225260
wp_add_inline_style( $styleId, $style );
226261
});
227262
}
228263

229264

230265
/**
231-
* options_form_input_{$fieldName} filter
266+
* options_form_input_{$fieldName} filter - input_custom field
232267
*
233268
* @param string $html current html for field
234269
* @param string $fieldName option name
235270
* @param array $metaData option meta data
236271
* @param mixed $value current option value
237272
* @return string new html for field
238273
*/
239-
public function options_form_input_custom($html, $fieldName, $metaData, $value)
274+
public function my_form_input_custom($html, $fieldName, $metaData, $value)
240275
{
241276
// The default for custom is:
242277
// <blockquote>[title]</blockquote>
@@ -248,7 +283,7 @@ public function options_form_input_custom($html, $fieldName, $metaData, $value)
248283
"<mark>Custom input field</mark>".
249284
"<input type='{$metaData['type']}' name='{$fieldName}' id='{$fieldName}' ".
250285
"value='{$value}' size='{$metaData['width']}'{$metaData['attributes']} />",
251-
$html);
286+
$html);
252287
// return the updated input html wrapped in a styled <div>
253288
return "<div class='custom-example' style='border: solid 1px yellow; padding: .5em; background: #ddd;'>" .
254289
$html .
@@ -257,66 +292,77 @@ public function options_form_input_custom($html, $fieldName, $metaData, $value)
257292

258293

259294
/**
260-
* options_form_post_{$fieldName} filter
295+
* options_form_post_{$fieldName} filter - input_custom field
261296
*
262297
* @param mixed $value posted option value(s)
263298
* @param string $fieldName option name
264299
* @param array $metaData option meta data
265300
* @param mixed $priorValue prior option value
266301
* @return mixed new option value(s)
267302
*/
268-
public function options_form_post_custom($value, $fieldName, $metaData, $priorValue)
303+
public function my_form_post_custom($value, $fieldName, $metaData, $priorValue)
269304
{
270305
// if no change, just return the value
271306
if ($value == $priorValue) return $value;
272307

273308
// sanitize/validate (or otherwise process) the value before it is saved to the database
309+
274310
return $value;
275311
}
276312

277313

278314
/**
279-
* options_form_post_{$fieldName} filter
315+
* options_form_post_{$fieldName} filter - input_file field
280316
*
281317
* @param mixed $value posted option value(s)
282318
* @param string $fieldName option name
283319
* @param array $metaData option meta data
284320
* @param mixed $priorValue prior option value
285321
* @return mixed new option value(s)
286322
*/
287-
public function options_form_post_file($value, $fieldName, $metaData, $priorValue)
323+
public function my_form_post_file($value, $fieldName, $metaData, $priorValue)
288324
{
289-
if (! isset($value['error']) && $value['type'] == 'application/json')
325+
// for 'file' type, $value is the array returned by wp_handle_upload()
326+
if (! isset($value['error']))
290327
{
291-
// valid upload with expected file type, $values['file'] is pathname
292-
if ($data = wp_json_file_decode($values['file'],['associative'=>true]))
328+
if ($value['type'] == 'application/json')
293329
{
294-
// do something here...
330+
// valid upload with expected file type, $values['file'] is pathname
331+
if ($data = wp_json_file_decode($values['file'],['associative'=>true]))
332+
{
333+
// do something here...
334+
}
335+
unlink($value['file']);
336+
return $value;
337+
}
338+
else
339+
{
340+
$this->add_option_error(
341+
$fieldName,
342+
sprintf("%s : Input file of type '%s' could not be processed.",$metaData['label'],$value['type'])
343+
);
295344
}
296-
unlink($value['file']);
297-
return $value;
298345
}
299346

300-
$this->add_option_error(
301-
$aOptionKey,
302-
sprintf('%s : Input file could not be processed.',$aOptionMeta['label'])
303-
);
304347
unlink($values['file']);
305348
return $value;
306349
}
307350

308351

309352
/**
310-
* sanitize_option filter
353+
* sanitize_option filter called for each option POSTed
311354
*
312355
* @param mixed $value posted option value(s)
313356
* @param string $fieldName option name
314357
* @param array $metaData option meta data
315358
* @param mixed $priorValue prior option value
316359
* @return mixed new option value(s)
317360
*/
318-
public function sanitize_options($value, $fieldName, $metaData, $priorValue)
361+
public function my_option_sanitize($value, $fieldName, $metaData, $priorValue)
319362
{
363+
// if no change, just return the value
364+
if ($value == $priorValue) return $value;
365+
320366
switch ($fieldName)
321367
{
322368
case 'input_number':
@@ -355,6 +401,7 @@ public function sanitize_options($value, $fieldName, $metaData, $priorValue)
355401
*/
356402
public function sanitize_callback($value, $fieldName, $metaData, $priorValue)
357403
{
404+
// sanitize value
358405
return $value;
359406
}
360407

@@ -370,10 +417,13 @@ public function sanitize_callback($value, $fieldName, $metaData, $priorValue)
370417
*/
371418
public function filter_callback($value, $fieldName, $metaData, $priorValue)
372419
{
420+
// if no change, just return the value
421+
if ($value == $priorValue) return $value;
422+
373423
// validate the value and display a notification
374424
if (is_numeric($value))
375425
{
376-
// add admin notice using helper method (does not use transient)
426+
// add admin notice using helper method
377427
$this->add_admin_notice(
378428
$fieldName.": {$value} May be the Answer to the Ultimate Question of Life, the Universe, and Everything.",
379429
'notice',

0 commit comments

Comments
 (0)