Controls
5 years ago
Fields
5 years ago
DragDropBuilder.php
5 years ago
FieldBase.php
5 years ago
FieldInterface.php
5 years ago
Metabox.php
5 years ago
google-web-fonts.txt
5 years ago
Metabox.php
315 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages\DragDropBuilder; |
| 4 | |
| 5 | |
| 6 | use ProfilePress\Core\Classes\FormRepository as FR; |
| 7 | use ProfilePress\Core\Themes\DragDrop\AbstractTheme; |
| 8 | |
| 9 | class Metabox |
| 10 | { |
| 11 | protected $args; |
| 12 | protected $saved_values; |
| 13 | protected $theme_class_instance; |
| 14 | /** @var DragDropBuilder */ |
| 15 | protected $DragDropBuilderInstance; |
| 16 | |
| 17 | /** |
| 18 | * @param array $args |
| 19 | * @param AbstractTheme $theme_class_instance |
| 20 | * @param DragDropBuilder $DragDropBuilderInstance |
| 21 | */ |
| 22 | public function __construct($args, $theme_class_instance, $DragDropBuilderInstance) |
| 23 | { |
| 24 | $saved_db_values = FR::get_form_meta($DragDropBuilderInstance->form_id, $DragDropBuilderInstance->form_type, FR::METABOX_FORM_BUILDER_SETTINGS); |
| 25 | |
| 26 | $this->args = $args; |
| 27 | $this->saved_values = $saved_db_values; |
| 28 | $this->theme_class_instance = $theme_class_instance; |
| 29 | $this->DragDropBuilderInstance = $DragDropBuilderInstance; |
| 30 | } |
| 31 | |
| 32 | private function default_values() |
| 33 | { |
| 34 | return call_user_func([$this->theme_class_instance, 'default_metabox_settings']); |
| 35 | } |
| 36 | |
| 37 | private function saved_values_bucket($key) |
| 38 | { |
| 39 | $defaults = $this->default_values(); |
| 40 | |
| 41 | $constants = apply_filters( |
| 42 | 'ppress_form_builder_metabox_field_as_form_meta', |
| 43 | array_values((new \ReflectionClass('ProfilePress\Core\Classes\FormRepository'))->getConstants()) |
| 44 | ); |
| 45 | |
| 46 | if (in_array($key, $constants)) { |
| 47 | $val = FR::get_form_meta( |
| 48 | $this->DragDropBuilderInstance->form_id, |
| 49 | $this->DragDropBuilderInstance->form_type, |
| 50 | $key |
| 51 | ); |
| 52 | |
| 53 | if ( ! $val || empty($val)) { |
| 54 | $val = isset($defaults[$key]) ? $defaults[$key] : ''; |
| 55 | } |
| 56 | |
| 57 | return $val; |
| 58 | } |
| 59 | |
| 60 | return isset($this->saved_values[$key]) ? $this->saved_values[$key] : ppress_var($defaults, $key); |
| 61 | } |
| 62 | |
| 63 | public function text($name, $options) |
| 64 | { |
| 65 | $placeholder = isset($options['placeholder']) ? $options['placeholder'] : ''; |
| 66 | printf( |
| 67 | '<input type="text" class="short" name="%1$s" id="%1$s" value="%3$s" placeholder="%2$s">', |
| 68 | esc_attr($name), esc_attr($placeholder), $this->saved_values_bucket($name) |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | public function number($name, $options) |
| 73 | { |
| 74 | $placeholder = isset($options['placeholder']) ? $options['placeholder'] : ''; |
| 75 | printf( |
| 76 | '<input type="number" class="short" name="%1$s" id="%1$s" value="%3$s" placeholder="%2$s">', |
| 77 | esc_attr($name), esc_attr($placeholder), $this->saved_values_bucket($name) |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | public function color($name, $options) |
| 82 | { |
| 83 | $placeholder = isset($options['placeholder']) ? $options['placeholder'] : ''; |
| 84 | |
| 85 | printf( |
| 86 | '<input type="text" class="short pp-color-field" name="%1$s" id="%1$s" value="%3$s" placeholder="%2$s" data-default-color="%4$s">', |
| 87 | esc_attr($name), esc_attr($placeholder), $this->saved_values_bucket($name), $this->default_values()[$name] |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | public function upload($name, $options) |
| 92 | { |
| 93 | $placeholder = isset($options['placeholder']) ? $options['placeholder'] : ''; |
| 94 | echo '<div class="pp_upload_field_container">'; |
| 95 | printf( |
| 96 | '<input type="text" class="pp_upload_field short large-text" name="%1$s" id="%1$s" value="%3$s" placeholder="%2$s">', |
| 97 | esc_attr($name), esc_attr($placeholder), $this->saved_values_bucket($name) |
| 98 | ); |
| 99 | printf('<span class="pp_upload_file"><a href="#" class="pp_upload_button">%s</a></span>', esc_html__('Upload Image', 'wp-user-avatar')); |
| 100 | echo '</div>'; |
| 101 | } |
| 102 | |
| 103 | public function textarea($name, $options) |
| 104 | { |
| 105 | $placeholder = isset($options['placeholder']) ? $options['placeholder'] : ''; |
| 106 | printf( |
| 107 | '<textarea class="short" name="%1$s" id="%1$s" placeholder="%2$s">%3$s</textarea>', |
| 108 | esc_attr($name), esc_attr($placeholder), $this->saved_values_bucket($name) |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | public function checkbox($name, $options) |
| 113 | { |
| 114 | $checkbox_label = isset($options['checkbox_label']) ? $options['checkbox_label'] : ''; |
| 115 | |
| 116 | printf('<input type="hidden" style="display: none" name="%1$s" value="false">', esc_attr($name)); |
| 117 | |
| 118 | printf( |
| 119 | '<input type="checkbox" class="checkbox" name="%1$s" id="%1$s" value="true" %2$s>', |
| 120 | esc_attr($name), checked('true', $this->saved_values_bucket($name), false) |
| 121 | ); |
| 122 | |
| 123 | printf('<span class="description">%s</span>', $checkbox_label); |
| 124 | } |
| 125 | |
| 126 | public function select($name, $options) |
| 127 | { |
| 128 | printf('<select id="%1$s" name="%1$s" class="select short">', esc_attr($name)); |
| 129 | |
| 130 | if (is_array($options['options']) && ! empty($options['options'])) { |
| 131 | foreach ($options['options'] as $id => $val) { |
| 132 | if (is_array($val)) { |
| 133 | echo "<optgroup label='$id'>"; |
| 134 | foreach ($val as $id2 => $val2) { |
| 135 | printf('<option value="%1$s" %3$s>%2$s</option>', $id2, $val2, selected($id2, $this->saved_values_bucket($name), false)); |
| 136 | } |
| 137 | echo "</optgroup>"; |
| 138 | } else { |
| 139 | printf('<option value="%1$s" %3$s>%2$s</option>', $id, $val, selected($id, $this->saved_values_bucket($name), false)); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | echo '</select>'; |
| 145 | } |
| 146 | |
| 147 | public function custom($name, $options) |
| 148 | { |
| 149 | echo isset($options['content']) ? $options['content'] : ''; |
| 150 | } |
| 151 | |
| 152 | private function select2_selected($id, $name) |
| 153 | { |
| 154 | $bucket = $this->saved_values_bucket($name); |
| 155 | |
| 156 | return in_array($id, is_array($bucket) ? $bucket : []) ? 'selected="selected"' : ''; |
| 157 | } |
| 158 | |
| 159 | public function select2($name, $options) |
| 160 | { |
| 161 | printf('<input name="%1$s[]" type="hidden" value="">', esc_attr($name)); |
| 162 | printf('<select data-placeholder="%2$s" id="%1$s" name="%1$s[]" class="select ppselect2 short" multiple>', esc_attr($name), esc_html__('Select...', 'wp-user-avatar')); |
| 163 | |
| 164 | if (is_array($options['options']) && ! empty($options['options'])) { |
| 165 | foreach ($options['options'] as $id => $val) { |
| 166 | if (is_array($val)) { |
| 167 | echo "<optgroup label='$id'>"; |
| 168 | foreach ($val as $id2 => $val2) { |
| 169 | printf('<option value="%1$s" %3$s>%2$s</option>', $id2, $val2, $this->select2_selected($id2, $name)); |
| 170 | } |
| 171 | echo "</optgroup>"; |
| 172 | } else { |
| 173 | printf('<option value="%1$s" %3$s>%2$s</option>', $id, $val, $this->select2_selected($id, $name)); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | echo '</select>'; |
| 179 | } |
| 180 | |
| 181 | protected function get_google_fonts($amount = 300) |
| 182 | { |
| 183 | $cache_folder = dirname(__FILE__); |
| 184 | |
| 185 | $fontFile = $cache_folder . '/google-web-fonts.txt'; |
| 186 | //Total time the file will be cached in seconds, set to a month. |
| 187 | $cachetime = MONTH_IN_SECONDS; |
| 188 | if (file_exists($fontFile) && $cachetime < filemtime($fontFile)) { |
| 189 | $content = json_decode(file_get_contents($fontFile)); |
| 190 | } else { |
| 191 | $googleApi = 'https://www.googleapis.com/webfonts/v1/webfonts?sort=popularity&key=AIzaSyA-iyjXck3LdOsAcGBDBfmlMtXM6jUp1Fk'; |
| 192 | $response = wp_remote_retrieve_body(wp_remote_get($googleApi, array('sslverify' => false))); |
| 193 | $fp = fopen($fontFile, 'w'); |
| 194 | fwrite($fp, $response); |
| 195 | fclose($fp); |
| 196 | $content = json_decode($response); |
| 197 | } |
| 198 | |
| 199 | if ($amount == 'all') { |
| 200 | $google_fonts = $content->items; |
| 201 | } else { |
| 202 | $google_fonts = array_slice($content->items, 0, $amount); |
| 203 | } |
| 204 | |
| 205 | // reduce google font collection to indexed array |
| 206 | $fonts = array_reduce($google_fonts, function ($carry, $item) { |
| 207 | $option_value = str_replace(' ', '+', $item->family); |
| 208 | $carry[$option_value] = $item->family; |
| 209 | |
| 210 | return $carry; |
| 211 | }); |
| 212 | |
| 213 | // sort in alphabetic order. |
| 214 | natsort($fonts); |
| 215 | |
| 216 | // combine fonts. |
| 217 | return $fonts; |
| 218 | } |
| 219 | |
| 220 | public function font_family($name, $setting) |
| 221 | { |
| 222 | $setting['options'] = [ |
| 223 | '' => esc_html__('Select...', 'wp-user-avatar'), |
| 224 | esc_html__('Standard Fonts', 'wp-user-avatar') => [ |
| 225 | 'Arial' => esc_html__('Arial', 'wp-user-avatar'), |
| 226 | 'Comic Sans MS' => esc_html__('Comic Sans MS', 'wp-user-avatar'), |
| 227 | 'Courier New' => esc_html__('Courier New', 'wp-user-avatar'), |
| 228 | 'Georgia' => esc_html__('Georgia', 'wp-user-avatar'), |
| 229 | 'Helvetica' => esc_html__('Helvetica', 'wp-user-avatar'), |
| 230 | 'Lucida' => esc_html__('Lucida', 'wp-user-avatar'), |
| 231 | 'Tahoma' => esc_html__('Tahoma', 'wp-user-avatar'), |
| 232 | 'Times New Roman' => esc_html__('Times New Roman', 'wp-user-avatar'), |
| 233 | 'Trebuchet MS' => esc_html__('Trebuchet MS', 'wp-user-avatar'), |
| 234 | 'Verdana' => esc_html__('Verdana', 'wp-user-avatar') |
| 235 | ], |
| 236 | esc_html__('Google Fonts', 'wp-user-avatar') => $this->get_google_fonts() |
| 237 | ]; |
| 238 | |
| 239 | $this->select($name, $setting); |
| 240 | } |
| 241 | |
| 242 | public function tab_radio($name, $options) |
| 243 | { |
| 244 | echo '<div class="ppmb-tab-radios">'; |
| 245 | if (is_array($options['options']) && ! empty($options['options'])) { |
| 246 | foreach ($options['options'] as $id => $val) { |
| 247 | printf('<input type="radio" name="%1$s" id="%3$s" value="%2$s" %4$s>', $name, $id, $name . '-' . $id, checked($id, $this->saved_values_bucket($name), false)); |
| 248 | printf('<label for="%1$s">%2$s</label>', $name . '-' . $id, $val); |
| 249 | } |
| 250 | } |
| 251 | echo isset($options['tab_description']) ? $options['tab_description'] : ''; |
| 252 | echo '</div>'; |
| 253 | } |
| 254 | |
| 255 | public function build() |
| 256 | { |
| 257 | $tabs = []; |
| 258 | $tab_settings = []; |
| 259 | foreach ($this->args as $key => $value) { |
| 260 | $tabs[$key] = $value['tab_title']; |
| 261 | unset($value['tab_title']); |
| 262 | $tab_settings[$key] = $value; |
| 263 | } |
| 264 | |
| 265 | $metabox_title = esc_html__('Form Settings', 'wp-user-avatar'); |
| 266 | |
| 267 | if ($this->DragDropBuilderInstance->form_type == FR::USER_PROFILE_TYPE) { |
| 268 | $metabox_title = esc_html__('User Profile Settings', 'wp-user-avatar'); |
| 269 | } |
| 270 | |
| 271 | if ($this->DragDropBuilderInstance->form_type == FR::MEMBERS_DIRECTORY_TYPE) { |
| 272 | $metabox_title = esc_html__('Directory Settings', 'wp-user-avatar'); |
| 273 | } |
| 274 | ?> |
| 275 | <div class="postbox-container" id="settings"> |
| 276 | <div id="pp-form-builder-metabox" class="postbox"> |
| 277 | <div class="postbox-header"><h2 class="hndle is-non-sortable"><span><?= $metabox_title ?></span></h2> |
| 278 | </div> |
| 279 | <div class="inside"> |
| 280 | <div class="panel-wrap pp-form-builder-mb-data"> |
| 281 | <ul class="pp-form-builder-mb-data_tabs pp-tabs"> |
| 282 | <?php foreach ($tabs as $key => $value) : ?> |
| 283 | <?php if (empty($tab_settings[$key])) continue; ?> |
| 284 | <li class="<?= $key ?>_options <?= $key ?>_tab"> |
| 285 | <a href="#<?= $key ?>_data"><span><?= $value ?></span></a> |
| 286 | </li> |
| 287 | <?php endforeach; ?> |
| 288 | </ul> |
| 289 | |
| 290 | <?php foreach ($tab_settings as $key => $fields) { |
| 291 | echo '<div id="' . $key . '_data" class="panel pp-form-builder_options_panel hidden">'; |
| 292 | |
| 293 | foreach ($fields as $options) { |
| 294 | $field_id = $options['id']; |
| 295 | |
| 296 | echo sprintf('<div class="form-field %s_wrap">', $field_id); |
| 297 | echo "<label for=\"$field_id\">" . $options['label'] . '</label>'; |
| 298 | if ( ! empty($options['description'])) { |
| 299 | printf('<span class="pp-form-builder-help-tip" title="%s"></span>', $options['description']); |
| 300 | } |
| 301 | echo '<div class="pp-field-row-content">'; |
| 302 | $this->{$options['type']}($field_id, $options); |
| 303 | echo '</div>'; |
| 304 | echo '</div>'; |
| 305 | } |
| 306 | echo '</div>'; |
| 307 | } |
| 308 | ?> |
| 309 | </div> |
| 310 | </div> |
| 311 | </div> |
| 312 | </div> |
| 313 | <?php |
| 314 | } |
| 315 | } |