Controls
1 year ago
Fields
1 month ago
DragDropBuilder.php
1 year ago
FieldBase.php
3 months ago
FieldInterface.php
5 years ago
Metabox.php
1 year ago
google-web-fonts.txt
5 years ago
Metabox.php
331 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), esc_attr($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), esc_attr($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), esc_attr($this->saved_values_bucket($name)), esc_attr(ppress_var($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), esc_attr($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), esc_textarea($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 ( |
| 131 | isset($options['options']) && |
| 132 | ($options['options'] instanceof \Generator || (is_array($options['options']) && ! empty($options['options']))) |
| 133 | ) { |
| 134 | foreach ($options['options'] as $id => $val) { |
| 135 | if (is_array($val)) { |
| 136 | echo "<optgroup label='$id'>"; |
| 137 | foreach ($val as $id2 => $val2) { |
| 138 | printf('<option value="%1$s" %3$s>%2$s</option>', $id2, $val2, selected($id2, $this->saved_values_bucket($name), false)); |
| 139 | } |
| 140 | echo "</optgroup>"; |
| 141 | } else { |
| 142 | printf('<option value="%1$s" %3$s>%2$s</option>', $id, $val, selected($id, $this->saved_values_bucket($name), false)); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | echo '</select>'; |
| 148 | } |
| 149 | |
| 150 | public function custom($name, $options) |
| 151 | { |
| 152 | echo isset($options['content']) ? wp_kses_post($options['content']) : ''; |
| 153 | } |
| 154 | |
| 155 | private function select2_selected($id, $name) |
| 156 | { |
| 157 | $bucket = $this->saved_values_bucket($name); |
| 158 | |
| 159 | return in_array($id, is_array($bucket) ? $bucket : []) ? 'selected="selected"' : ''; |
| 160 | } |
| 161 | |
| 162 | public function select2($name, $options) |
| 163 | { |
| 164 | printf('<input name="%1$s[]" type="hidden" value="">', esc_attr($name)); |
| 165 | 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')); |
| 166 | |
| 167 | if ( |
| 168 | isset($options['options']) && |
| 169 | ($options['options'] instanceof \Generator || (is_array($options['options']) && ! empty($options['options']))) |
| 170 | ) { |
| 171 | foreach ($options['options'] as $id => $val) { |
| 172 | if (is_array($val)) { |
| 173 | echo "<optgroup label='$id'>"; |
| 174 | foreach ($val as $id2 => $val2) { |
| 175 | printf('<option value="%1$s" %3$s>%2$s</option>', $id2, $val2, $this->select2_selected($id2, $name)); |
| 176 | } |
| 177 | echo "</optgroup>"; |
| 178 | } else { |
| 179 | printf('<option value="%1$s" %3$s>%2$s</option>', $id, $val, $this->select2_selected($id, $name)); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | echo '</select>'; |
| 185 | } |
| 186 | |
| 187 | protected function get_google_fonts($amount = 300) |
| 188 | { |
| 189 | $cache_folder = dirname(__FILE__); |
| 190 | |
| 191 | $fontFile = $cache_folder . '/google-web-fonts.txt'; |
| 192 | //Total time the file will be cached in seconds, set to a month. |
| 193 | $cachetime = MONTH_IN_SECONDS; |
| 194 | if (file_exists($fontFile) && $cachetime < filemtime($fontFile)) { |
| 195 | $content = json_decode(file_get_contents($fontFile)); |
| 196 | } else { |
| 197 | $googleApi = 'https://www.googleapis.com/webfonts/v1/webfonts?sort=popularity&key=AIzaSyA-iyjXck3LdOsAcGBDBfmlMtXM6jUp1Fk'; |
| 198 | $response = wp_remote_retrieve_body(wp_remote_get($googleApi, array('sslverify' => false))); |
| 199 | $fp = fopen($fontFile, 'w'); |
| 200 | if ($fp) { |
| 201 | fwrite($fp, $response); |
| 202 | fclose($fp); |
| 203 | } |
| 204 | |
| 205 | $content = json_decode($response); |
| 206 | } |
| 207 | |
| 208 | if ($amount == 'all') { |
| 209 | $google_fonts = $content->items; |
| 210 | } else { |
| 211 | $google_fonts = array_slice($content->items, 0, $amount); |
| 212 | } |
| 213 | |
| 214 | // reduce google font collection to indexed array |
| 215 | $fonts = array_reduce($google_fonts, function ($carry, $item) { |
| 216 | $option_value = str_replace(' ', '+', $item->family); |
| 217 | $carry[$option_value] = $item->family; |
| 218 | |
| 219 | return $carry; |
| 220 | }); |
| 221 | |
| 222 | // sort in alphabetic order. |
| 223 | natsort($fonts); |
| 224 | |
| 225 | // combine fonts. |
| 226 | return $fonts; |
| 227 | } |
| 228 | |
| 229 | public function font_family($name, $setting) |
| 230 | { |
| 231 | $setting['options'] = [ |
| 232 | '' => esc_html__('Select...', 'wp-user-avatar'), |
| 233 | esc_html__('Standard Fonts', 'wp-user-avatar') => [ |
| 234 | 'Arial' => esc_html__('Arial', 'wp-user-avatar'), |
| 235 | 'Lucida' => esc_html__('Lucida', 'wp-user-avatar'), |
| 236 | ], |
| 237 | esc_html__('Google Fonts', 'wp-user-avatar') => [ |
| 238 | 'Comic Sans MS' => esc_html__('Comic Sans MS', 'wp-user-avatar'), |
| 239 | 'Courier New' => esc_html__('Courier New', 'wp-user-avatar'), |
| 240 | 'Georgia' => esc_html__('Georgia', 'wp-user-avatar'), |
| 241 | 'Helvetica' => esc_html__('Helvetica', 'wp-user-avatar'), |
| 242 | 'Tahoma' => esc_html__('Tahoma', 'wp-user-avatar'), |
| 243 | 'Times New Roman' => esc_html__('Times New Roman', 'wp-user-avatar'), |
| 244 | 'Trebuchet MS' => esc_html__('Trebuchet MS', 'wp-user-avatar'), |
| 245 | 'Verdana' => esc_html__('Verdana', 'wp-user-avatar') |
| 246 | ] + $this->get_google_fonts() |
| 247 | ]; |
| 248 | |
| 249 | $this->select($name, $setting); |
| 250 | } |
| 251 | |
| 252 | public function tab_radio($name, $options) |
| 253 | { |
| 254 | echo '<div class="ppmb-tab-radios">'; |
| 255 | if (is_array($options['options']) && ! empty($options['options'])) { |
| 256 | foreach ($options['options'] as $id => $val) { |
| 257 | 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)); |
| 258 | printf('<label for="%1$s">%2$s</label>', $name . '-' . $id, $val); |
| 259 | } |
| 260 | } |
| 261 | echo isset($options['tab_description']) ? $options['tab_description'] : ''; |
| 262 | echo '</div>'; |
| 263 | } |
| 264 | |
| 265 | public function build() |
| 266 | { |
| 267 | $tabs = []; |
| 268 | $tab_settings = []; |
| 269 | foreach ($this->args as $key => $value) { |
| 270 | $tabs[$key] = $value['tab_title']; |
| 271 | unset($value['tab_title']); |
| 272 | $tab_settings[$key] = $value; |
| 273 | } |
| 274 | |
| 275 | $metabox_title = esc_html__('Form Settings', 'wp-user-avatar'); |
| 276 | |
| 277 | if ($this->DragDropBuilderInstance->form_type == FR::USER_PROFILE_TYPE) { |
| 278 | $metabox_title = esc_html__('User Profile Settings', 'wp-user-avatar'); |
| 279 | } |
| 280 | |
| 281 | if ($this->DragDropBuilderInstance->form_type == FR::MEMBERS_DIRECTORY_TYPE) { |
| 282 | $metabox_title = esc_html__('Directory Settings', 'wp-user-avatar'); |
| 283 | } |
| 284 | ?> |
| 285 | <div class="postbox-container" id="settings"> |
| 286 | <div id="pp-form-builder-metabox" class="postbox"> |
| 287 | <div class="postbox-header"><h2 class="hndle is-non-sortable"> |
| 288 | <span><?= esc_html($metabox_title) ?></span></h2> |
| 289 | <div class="handle-actions hide-if-no-js"> |
| 290 | <button type="button" class="handlediv" aria-expanded="true"> |
| 291 | <span class="toggle-indicator" aria-hidden="true"></span> |
| 292 | </button> |
| 293 | </div> |
| 294 | </div> |
| 295 | <div class="inside"> |
| 296 | <div class="panel-wrap pp-form-builder-mb-data"> |
| 297 | <ul class="pp-form-builder-mb-data_tabs pp-tabs"> |
| 298 | <?php foreach ($tabs as $key => $value) : $key = esc_attr($key); ?> |
| 299 | <?php if (empty($tab_settings[$key])) continue; ?> |
| 300 | <li class="<?= $key ?>_options <?= $key ?>_tab"> |
| 301 | <a href="#<?= $key ?>_data"><span><?= $value ?></span></a> |
| 302 | </li> |
| 303 | <?php endforeach; ?> |
| 304 | </ul> |
| 305 | |
| 306 | <?php foreach ($tab_settings as $key => $fields) { |
| 307 | echo '<div id="' . esc_attr($key) . '_data" class="panel pp-form-builder_options_panel hidden">'; |
| 308 | |
| 309 | foreach ($fields as $options) { |
| 310 | $field_id = $options['id']; |
| 311 | |
| 312 | echo sprintf('<div class="form-field %s_wrap">', $field_id); |
| 313 | echo "<label for=\"$field_id\">" . wp_kses_post($options['label']) . '</label>'; |
| 314 | if ( ! empty($options['description'])) { |
| 315 | printf('<span class="pp-form-builder-help-tip" title="%s"></span>', esc_attr($options['description'])); |
| 316 | } |
| 317 | echo '<div class="pp-field-row-content">'; |
| 318 | $this->{$options['type']}($field_id, $options); |
| 319 | echo '</div>'; |
| 320 | echo '</div>'; |
| 321 | } |
| 322 | echo '</div>'; |
| 323 | } |
| 324 | ?> |
| 325 | </div> |
| 326 | </div> |
| 327 | </div> |
| 328 | </div> |
| 329 | <?php |
| 330 | } |
| 331 | } |