Field.php
183 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 number type input. |
| 20 | * |
| 21 | * @param $value |
| 22 | * |
| 23 | * @return string |
| 24 | * @since 2.7.5 |
| 25 | * @access protected |
| 26 | * @static |
| 27 | */ |
| 28 | protected static function number($value) |
| 29 | { |
| 30 | return '<input type="number" name="embedpress:{{slug}}[{{name}}]" class="{{classes}}" placeholder="{{placeholder}}" value="' . (int)$value . '">'; |
| 31 | |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Generates a url type input. |
| 36 | * |
| 37 | * @param $value |
| 38 | * |
| 39 | * @return string |
| 40 | * @since 2.7.5 |
| 41 | * @access protected |
| 42 | * @static |
| 43 | */ |
| 44 | protected static function url($value) |
| 45 | { |
| 46 | return '<input type="url" name="embedpress:{{slug}}[{{name}}]" class="{{classes}}" placeholder="{{placeholder}}" value="' . (string)$value . '">'; |
| 47 | |
| 48 | } |
| 49 | /** |
| 50 | * Generates a text type input. |
| 51 | * |
| 52 | * @since 1.4.0 |
| 53 | * @access protected |
| 54 | * @static |
| 55 | * |
| 56 | * @return string |
| 57 | */ |
| 58 | protected static function text($value) |
| 59 | { |
| 60 | return '<input type="text" name="embedpress:{{slug}}[{{name}}]" class="{{classes}}" placeholder="{{placeholder}}" value="' . (string)$value . '">'; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Generates a textarea input. |
| 65 | * |
| 66 | * @since 1.4.0 |
| 67 | * @access protected |
| 68 | * @static |
| 69 | * |
| 70 | * @return string |
| 71 | */ |
| 72 | protected static function textarea($value) |
| 73 | { |
| 74 | return '<textarea name="embedpress:{{slug}}[{{name}}]" class="{{classes}}" placeholder="{{placeholder}}">' . (string)$value . '</textarea>'; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Generates a radio type input. |
| 79 | * |
| 80 | * @since 1.4.0 |
| 81 | * @access protected |
| 82 | * @static |
| 83 | * |
| 84 | * @return string |
| 85 | */ |
| 86 | protected static function radio($options, $value = null) |
| 87 | { |
| 88 | $html = []; |
| 89 | |
| 90 | foreach ((array)$options as $optionValue => $optionLabel) { |
| 91 | $html[] = '<label>'; |
| 92 | $html[] = '<input type="radio" name="embedpress:{{slug}}[{{name}}]" class="{{classes}}" value="' . $optionValue . '"' . ($value === $optionValue ? ' checked' : '') . '>'; |
| 93 | $html[] = ' ' . $optionLabel; |
| 94 | $html[] = '</label> '; |
| 95 | } |
| 96 | |
| 97 | return implode('', $html); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Generates a select input. |
| 102 | * |
| 103 | * @since 1.4.0 |
| 104 | * @access protected |
| 105 | * @static |
| 106 | * |
| 107 | * @return string |
| 108 | */ |
| 109 | protected static function select($options, $value = null) |
| 110 | { |
| 111 | $html = ['<select name="embedpress:{{slug}}[{{name}}]" class="{{classes}}">']; |
| 112 | |
| 113 | foreach ((array)$options as $optionValue => $optionLabel) { |
| 114 | $html[] = '<option value="' . $optionValue . '"' . ($value === (string)$optionValue ? ' selected' : '') . '>' . $optionLabel . '</option>'; |
| 115 | } |
| 116 | |
| 117 | $html[] = '</select>'; |
| 118 | |
| 119 | return implode('', $html); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Render a field based on a field schema. |
| 124 | * |
| 125 | * @since 1.4.0 |
| 126 | * @static |
| 127 | * |
| 128 | * @param array $params There's two available keys: 'field' which holds the field schema; and 'pluginSlug' which |
| 129 | * represents the slug of the plugin where the field belongs to. |
| 130 | * |
| 131 | * @return void |
| 132 | */ |
| 133 | public static function render($params) |
| 134 | { |
| 135 | $field = json_decode(json_encode($params['field'])); |
| 136 | |
| 137 | $pluginSlug = "embedpress:{$params['pluginSlug']}"; |
| 138 | |
| 139 | $options = (array)get_option($pluginSlug); |
| 140 | |
| 141 | $field->type = strtolower($field->type); |
| 142 | |
| 143 | if ($field->slug === "license_key") { |
| 144 | $value = isset($options['license']['key']) ? (string)$options['license']['key'] : ""; |
| 145 | } else { |
| 146 | $value = isset($options[$field->slug]) ? $options[$field->slug] : (isset($field->default) ? $field->default : ''); |
| 147 | } |
| 148 | |
| 149 | if (in_array($field->type, ['bool', 'boolean'])) { |
| 150 | $html = self::radio([ |
| 151 | 0 => 'No', |
| 152 | 1 => 'Yes', |
| 153 | ], (int)$value); |
| 154 | } elseif (isset($field->options)) { |
| 155 | $html = self::select((array)$field->options, (string)$value); |
| 156 | } elseif (in_array($field->type, ['textarea'])) { |
| 157 | $html = self::textarea((string)$value); |
| 158 | } elseif (in_array($field->type, ['number', 'NUMBER'])) { |
| 159 | $html = self::number((int)$value); |
| 160 | } elseif (in_array($field->type, ['url', 'link'])) { |
| 161 | $html = self::url($value); |
| 162 | } else { |
| 163 | $html = self::text((string)$value); |
| 164 | } |
| 165 | |
| 166 | $html = str_replace('{{slug}}', $params['pluginSlug'], $html); |
| 167 | $html = str_replace('{{name}}', $field->slug, $html); |
| 168 | $html = str_replace('{{classes}}', implode(' ', (! empty($field->classes) ? (array)$field->classes : [])), |
| 169 | $html); |
| 170 | $html = str_replace('{{placeholder}}', (! empty($field->placeholder) ? (string)$field->placeholder : ""), |
| 171 | $html); |
| 172 | |
| 173 | $html .= wp_nonce_field("{$pluginSlug}:nonce", "{$pluginSlug}:nonce"); |
| 174 | |
| 175 | if ( ! empty($field->description)) { |
| 176 | $html .= '<br/>'; |
| 177 | $html .= '<p class="description">' . $field->description . '</p>'; |
| 178 | } |
| 179 | |
| 180 | echo $html; |
| 181 | } |
| 182 | } |
| 183 |