| 1 |
<?php |
| 2 |
|
| 3 |
namespace LWS\WOOREWARDS\Ui; |
| 4 |
|
| 5 |
// don't call the file directly |
| 6 |
if (!defined('ABSPATH')) exit(); |
| 7 |
|
| 8 |
/** Provide a widget to let display rewards. |
| 9 |
* Can be used as a Widget, a Shortcode [lws_rewards] or a Guttenberg block (soon). |
| 10 |
* Rewards can be filtered by pool |
| 11 |
* For a looged in user, we can filter only the unlockable ones. */ |
| 12 |
class Widget extends \WP_Widget |
| 13 |
{ |
| 14 |
static protected function register($className) |
| 15 |
{ |
| 16 |
\add_action('widgets_init', function () use ($className) { |
| 17 |
\register_widget($className); |
| 18 |
}); |
| 19 |
} |
| 20 |
|
| 21 |
/** echo a form select line @param $options (array) value=>text */ |
| 22 |
protected function eFormFieldSelect($id, $label, $name, $options, $value) |
| 23 |
{ |
| 24 |
$input = "<select class='widefat' id='$id' name='$name'>"; |
| 25 |
foreach ($options as $v => $txt) { |
| 26 |
$selected = $v == $value ? ' selected' : ''; |
| 27 |
$input .= "<option value='$v'$selected>$txt</option>"; |
| 28 |
} |
| 29 |
$input .= "</select>"; |
| 30 |
$this->eFormField($id, $label, $input); |
| 31 |
} |
| 32 |
|
| 33 |
/** echo a form text line */ |
| 34 |
protected function eFormFieldText($id, $label, $name, $value, $placeholder = '', $type = 'text') |
| 35 |
{ |
| 36 |
$input = "<input class='widefat' id='$id' name='$name' type='$type' value='$value' placeholder='$placeholder'/>"; |
| 37 |
$this->eFormField($id, $label, $input); |
| 38 |
} |
| 39 |
|
| 40 |
/** echo a form radio line */ |
| 41 |
protected function eFormFieldRadio($id, $label, $name, $options, $value) |
| 42 |
{ |
| 43 |
$input = ''; |
| 44 |
foreach ($options as $v => $txt) { |
| 45 |
$selected = $v == $value ? ' checked' : ''; |
| 46 |
$input .= "<input type='radio' style='margin:0 5px 0 15px;' name='$name' value='$v'$selected>$txt"; |
| 47 |
} |
| 48 |
$this->eFormField($id, $label, $input); |
| 49 |
} |
| 50 |
|
| 51 |
/** echo a checkbox */ |
| 52 |
protected function eFormFieldCheckbox($id, $label, $name, $value) |
| 53 |
{ |
| 54 |
echo sprintf( |
| 55 |
'<p><input class="checkbox" id="%s" name="%s" type="checkbox" %s/>', |
| 56 |
\esc_attr($id), |
| 57 |
\esc_attr($name), |
| 58 |
$value ? ' checked' : '' |
| 59 |
); |
| 60 |
echo "<label for='" . esc_attr($id) . "'>" . esc_html($label) . "</label></p>"; |
| 61 |
} |
| 62 |
|
| 63 |
/** echo a form entry line */ |
| 64 |
protected function eFormField($id, $label, $input) |
| 65 |
{ |
| 66 |
echo "<p><label for='" . esc_attr($id) . "'>" . esc_html($label) . "</label>"; |
| 67 |
echo $input; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 68 |
echo "</p>"; |
| 69 |
} |
| 70 |
} |