Field.php
198 lines
| 1 | <?php |
| 2 | namespace EmbedPress\Plugins\Html; |
| 3 | |
| 4 | (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed."); |
| 5 | |
| 6 | /** |
| 7 | * Entity responsible to generating and rendering html fields to the settings page. |
| 8 | * |
| 9 | * @package EmbedPress |
| 10 | * @author PressShack <help@pressshack.com> |
| 11 | * @copyright Copyright (C) 2016 Open Source Training, LLC. All rights reserved. |
| 12 | * @license GPLv2 or later |
| 13 | * @since 1.4.0 |
| 14 | */ |
| 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 = array(); |
| 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 = array('<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 represents the slug of the plugin where the field belongs to. |
| 106 | * @return void |
| 107 | */ |
| 108 | public static function render($params) |
| 109 | { |
| 110 | $field = json_decode(json_encode($params['field'])); |
| 111 | |
| 112 | $pluginSlug = "embedpress:{$params['pluginSlug']}"; |
| 113 | |
| 114 | $options = (array)get_option($pluginSlug); |
| 115 | |
| 116 | $field->type = strtolower($field->type); |
| 117 | |
| 118 | if ($field->slug === "license_key") { |
| 119 | $value = isset($options['license']['key']) ? (string)$options['license']['key'] : ""; |
| 120 | } else { |
| 121 | $value = isset($options[$field->slug]) ? $options[$field->slug] : (isset($field->default) ? $field->default : ''); |
| 122 | } |
| 123 | |
| 124 | if (in_array($field->type, array('bool', 'boolean'))) { |
| 125 | $html = self::radio(array( |
| 126 | 0 => 'No', |
| 127 | 1 => 'Yes' |
| 128 | ), (int)$value); |
| 129 | } else if (isset($field->options)) { |
| 130 | $html = self::select((array)$field->options, (string)$value); |
| 131 | } else if (in_array($field->type, array('textarea'))) { |
| 132 | $html = self::textarea((string)$value); |
| 133 | } else { |
| 134 | $html = self::text((string)$value); |
| 135 | } |
| 136 | |
| 137 | $html = str_replace('{{slug}}', $params['pluginSlug'], $html); |
| 138 | $html = str_replace('{{name}}', $field->slug, $html); |
| 139 | $html = str_replace('{{classes}}', implode(' ', (!empty($field->classes) ? (array)$field->classes : array())), $html); |
| 140 | $html = str_replace('{{placeholder}}', (!empty($field->placeholder) ? (string)$field->placeholder : ""), $html); |
| 141 | |
| 142 | $html .= wp_nonce_field("{$pluginSlug}:nonce", "{$pluginSlug}:nonce"); |
| 143 | |
| 144 | if ($field->slug === "license_key") { |
| 145 | $licenseStatusClass = "ep-label-danger"; |
| 146 | $currentLicenseStatus = isset($options['license']['status']) ? trim(strtoupper($options['license']['status'])) : ""; |
| 147 | switch ($currentLicenseStatus) { |
| 148 | case '': |
| 149 | $licenseStatusMessage = "Missing license"; |
| 150 | break; |
| 151 | case 'EXPIRED': |
| 152 | $licenseStatusMessage = "Your license key is expired"; |
| 153 | break; |
| 154 | case 'REVOKED': |
| 155 | $licenseStatusMessage = "Your license key has been disabled"; |
| 156 | break; |
| 157 | case 'MISSING': |
| 158 | case 'INVALID': |
| 159 | $licenseStatusMessage = "Invalid license"; |
| 160 | break; |
| 161 | case 'SITE_INACTIVE': |
| 162 | $licenseStatusMessage = "Your license is not active for this URL"; |
| 163 | break; |
| 164 | case 'ITEM_NAME_MISMATCH': |
| 165 | $licenseStatusMessage = "This appears to be an invalid license key for this product"; |
| 166 | break; |
| 167 | case 'NO_ACTIVATIONS_LEFT': |
| 168 | $licenseStatusMessage = "Your license key has reached its activation limit"; |
| 169 | break; |
| 170 | case 'VALID': |
| 171 | $licenseStatusClass = "ep-label-success"; |
| 172 | $licenseStatusMessage = "Activated"; |
| 173 | break; |
| 174 | default: |
| 175 | $licenseStatusMessage = "Not validated yet"; |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | $html .= '<br/><br/><strong>Status: <span class="'. $licenseStatusClass .'">'. __($licenseStatusMessage) .'</span>.</strong><br/><br/>'; |
| 180 | |
| 181 | if (!(isset($options['license']['status']) && $options['license']['status'] === 'valid')) { |
| 182 | $html .= '<button type="submit" class="button-secondary">' . __('Activate License') . '</button> '; |
| 183 | $html .= '<a href="'. EMBEDPRESS_LICENSES_MORE_INFO_URL .'" target="_blank" class="ep-small-link ep-small-spacing" rel="noopener noreferrer" style="display: inline-block; margin-left: 20px;" title="'. __('Click here to read more about licenses.') .'">' . __('More information') . '</a>'; |
| 184 | $html .= '<br/><br/>'; |
| 185 | } |
| 186 | |
| 187 | $html .= '<hr>'; |
| 188 | } |
| 189 | |
| 190 | if (!empty($field->description)) { |
| 191 | $html .= '<br/>'; |
| 192 | $html .= '<p class="description">'. $field->description .'</p>'; |
| 193 | } |
| 194 | |
| 195 | echo $html; |
| 196 | } |
| 197 | } |
| 198 |