Field.php
156 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress\Plugins\Html; |
| 4 | |
| 5 | (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed."); |
| 6 | |
| 7 | /** |
| 8 | * Entity responsible to generating and rendering html fields to the settings page. |
| 9 | * |
| 10 | * @package EmbedPress |
| 11 | * @author EmbedPress <help@embedpress.com> |
| 12 | * @copyright Copyright (C) 2020 WPDeveloper. All rights reserved. |
| 13 | * @license GPLv3 or later |
| 14 | * @since 1.4.0 |
| 15 | */ |
| 16 | class Field |
| 17 | { |
| 18 | /** |
| 19 | * Generates a text type input. |
| 20 | * |
| 21 | * @since 1.4.0 |
| 22 | * @access protected |
| 23 | * @static |
| 24 | * |
| 25 | * @return string |
| 26 | */ |
| 27 | protected static function text($value) |
| 28 | { |
| 29 | $html = '<input type="text" name="embedpress:{{slug}}[{{name}}]" class="{{classes}}" placeholder="{{placeholder}}" value="' . (string)$value . '">'; |
| 30 | |
| 31 | return $html; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Generates a textarea input. |
| 36 | * |
| 37 | * @since 1.4.0 |
| 38 | * @access protected |
| 39 | * @static |
| 40 | * |
| 41 | * @return string |
| 42 | */ |
| 43 | protected static function textarea($value) |
| 44 | { |
| 45 | $html = '<textarea name="embedpress:{{slug}}[{{name}}]" class="{{classes}}" placeholder="{{placeholder}}">' . (string)$value . '</textarea>'; |
| 46 | |
| 47 | return $html; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Generates a radio type input. |
| 52 | * |
| 53 | * @since 1.4.0 |
| 54 | * @access protected |
| 55 | * @static |
| 56 | * |
| 57 | * @return string |
| 58 | */ |
| 59 | protected static function radio($options, $value = null) |
| 60 | { |
| 61 | $html = []; |
| 62 | |
| 63 | foreach ((array)$options as $optionValue => $optionLabel) { |
| 64 | $html[] = '<label>'; |
| 65 | $html[] = '<input type="radio" name="embedpress:{{slug}}[{{name}}]" class="{{classes}}" value="' . $optionValue . '"' . ($value === $optionValue ? ' checked' : '') . '>'; |
| 66 | $html[] = ' ' . $optionLabel; |
| 67 | $html[] = '</label> '; |
| 68 | } |
| 69 | |
| 70 | $html = implode('', $html); |
| 71 | |
| 72 | return $html; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Generates a select input. |
| 77 | * |
| 78 | * @since 1.4.0 |
| 79 | * @access protected |
| 80 | * @static |
| 81 | * |
| 82 | * @return string |
| 83 | */ |
| 84 | protected static function select($options, $value = null) |
| 85 | { |
| 86 | $html = ['<select name="embedpress:{{slug}}[{{name}}]" class="{{classes}}">']; |
| 87 | |
| 88 | foreach ((array)$options as $optionValue => $optionLabel) { |
| 89 | $html[] = '<option value="' . $optionValue . '"' . ($value === (string)$optionValue ? ' selected' : '') . '>' . $optionLabel . '</option>'; |
| 90 | } |
| 91 | |
| 92 | $html[] = '</select>'; |
| 93 | |
| 94 | $html = implode('', $html); |
| 95 | |
| 96 | return $html; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Render a field based on a field schema. |
| 101 | * |
| 102 | * @since 1.4.0 |
| 103 | * @static |
| 104 | * |
| 105 | * @param array $params There's two available keys: 'field' which holds the field schema; and 'pluginSlug' which |
| 106 | * represents the slug of the plugin where the field belongs to. |
| 107 | * |
| 108 | * @return void |
| 109 | */ |
| 110 | public static function render($params) |
| 111 | { |
| 112 | $field = json_decode(json_encode($params['field'])); |
| 113 | |
| 114 | $pluginSlug = "embedpress:{$params['pluginSlug']}"; |
| 115 | |
| 116 | $options = (array)get_option($pluginSlug); |
| 117 | |
| 118 | $field->type = strtolower($field->type); |
| 119 | |
| 120 | if ($field->slug === "license_key") { |
| 121 | $value = isset($options['license']['key']) ? (string)$options['license']['key'] : ""; |
| 122 | } else { |
| 123 | $value = isset($options[$field->slug]) ? $options[$field->slug] : (isset($field->default) ? $field->default : ''); |
| 124 | } |
| 125 | |
| 126 | if (in_array($field->type, ['bool', 'boolean'])) { |
| 127 | $html = self::radio([ |
| 128 | 0 => 'No', |
| 129 | 1 => 'Yes', |
| 130 | ], (int)$value); |
| 131 | } elseif (isset($field->options)) { |
| 132 | $html = self::select((array)$field->options, (string)$value); |
| 133 | } elseif (in_array($field->type, ['textarea'])) { |
| 134 | $html = self::textarea((string)$value); |
| 135 | } else { |
| 136 | $html = self::text((string)$value); |
| 137 | } |
| 138 | |
| 139 | $html = str_replace('{{slug}}', $params['pluginSlug'], $html); |
| 140 | $html = str_replace('{{name}}', $field->slug, $html); |
| 141 | $html = str_replace('{{classes}}', implode(' ', (! empty($field->classes) ? (array)$field->classes : [])), |
| 142 | $html); |
| 143 | $html = str_replace('{{placeholder}}', (! empty($field->placeholder) ? (string)$field->placeholder : ""), |
| 144 | $html); |
| 145 | |
| 146 | $html .= wp_nonce_field("{$pluginSlug}:nonce", "{$pluginSlug}:nonce"); |
| 147 | |
| 148 | if ( ! empty($field->description)) { |
| 149 | $html .= '<br/>'; |
| 150 | $html .= '<p class="description">' . $field->description . '</p>'; |
| 151 | } |
| 152 | |
| 153 | echo $html; |
| 154 | } |
| 155 | } |
| 156 |