PluginProbe
Property Hive / trunk
Property Hive vtrunk
2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 1.4.62 1.4.63 All 259 releases
propertyhive / includes / admin / class-ph-admin-settings.php

class-ph-admin-settings.php in Property Hive trunk, at includes/admin/class-ph-admin-settings.php

901 lines 33.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PropertyHive Admin Settings Class.
4 *
5 * @author PropertyHive
6 * @category Admin
7 * @package PropertyHive/Admin
8 * @version 1.0.0
9 */
10
11 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
12
13 if ( ! class_exists( 'PH_Admin_Settings' ) ) :
14
15 /**
16 * PH_Admin_Settings
17 */
18 class PH_Admin_Settings {
19
20 private static $settings = array();
21 private static $errors = array();
22 private static $messages = array();
23
24 /**
25 * Include the settings page classes
26 */
27 public static function get_settings_pages() {
28 if ( empty( self::$settings ) ) {
29 $settings = array();
30
31 include_once( 'settings/class-ph-settings-page.php' );
32
33 $settings[] = include( 'settings/class-ph-settings-general.php' );
34 $settings[] = include( 'settings/class-ph-settings-offices.php' );
35 $settings[] = include( 'settings/class-ph-settings-custom-fields.php' );
36 $propertyhive_template_assistant_auto_deactivated = get_option('propertyhive_template_assistant_auto_deactivated', '');
37 if ( !empty($propertyhive_template_assistant_auto_deactivated) )
38 {
39 // Only show if they had the TA active and we deactived it. Don't want it showing for new users
40 $settings[] = include( 'settings/class-ph-settings-template-assistant.php' ); // Maybe temporary after migrating TA code into core. Remove in future version
41 }
42 $settings[] = include( 'settings/class-ph-settings-frontend.php' );
43 $settings[] = include( 'settings/class-ph-settings-emails.php' );
44 $settings[] = include( 'settings/class-ph-settings-features.php' );
45 $settings[] = include( 'settings/class-ph-settings-licenses.php' );
46
47 // Only show demo data tab if demo data add on not active, tab not dismissed and if newly installed since 2021-04-13 00:00:00
48 if (
49 !class_exists('PH_Demo_Data') &&
50 get_option( 'propertyhive_install_timestamp', '' ) >= 1618268400 &&
51 get_option( 'propertyhive_hide_demo_data_tab', '' ) != 'yes'
52 )
53 {
54 $settings[] = include( 'settings/class-ph-settings-demo-data.php' );
55 }
56
57 self::$settings = apply_filters( 'propertyhive_get_settings_pages', $settings );
58 }
59 return self::$settings;
60 }
61
62 /**
63 * Save the settings
64 */
65 public static function save() {
66 global $current_section, $current_tab;
67
68 if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'propertyhive-settings' ) )
69 die( esc_html(__( 'Action failed. Please refresh the page and retry.', 'propertyhive' )) );
70
71 // Trigger actions
72 do_action( 'propertyhive_settings_save_' . $current_tab );
73 do_action( 'propertyhive_update_options_' . $current_tab );
74 do_action( 'propertyhive_update_options' );
75
76 self::add_message( __( 'Your settings have been saved.', 'propertyhive' ) );
77
78 update_option( 'propertyhive_queue_flush_rewrite_rules', 'yes' );
79
80 do_action( 'propertyhive_settings_saved' );
81 }
82
83 /**
84 * Add a message
85 * @param string $text
86 */
87 public static function add_message( $text ) {
88 self::$messages[] = $text;
89 }
90
91 /**
92 * Add an error
93 * @param string $text
94 */
95 public static function add_error( $text ) {
96 self::$errors[] = $text;
97 }
98
99 /**
100 * Output messages + errors
101 */
102 public static function show_messages() {
103 if ( sizeof( self::$errors ) > 0 ) {
104 foreach ( self::$errors as $error )
105 {
106 $allowed_tags = array(
107 'a' => array(
108 'href' => array(),
109 ),
110 );
111
112 $error = wp_kses($error, $allowed_tags);
113
114 echo '<div id="message" class="error fade"><p><strong>' . $error . '</strong></p></div>';
115 }
116 } elseif ( sizeof( self::$messages ) > 0 ) {
117 foreach ( self::$messages as $message )
118 {
119 $allowed_tags = array(
120 'a' => array(
121 'href' => array(),
122 ),
123 );
124
125 $message = wp_kses($message, $allowed_tags);
126
127 echo '<div id="message" class="updated fade"><p><strong>' . $message . '</strong></p></div>';
128 }
129 }
130 }
131
132 /**
133 * Settings page.
134 *
135 * Handles the display of the main propertyhive settings page in admin.
136 *
137 * @access public
138 * @return void
139 */
140 public static function output() {
141 global $current_section, $current_tab, $redirect_after_save;
142
143 do_action( 'propertyhive_settings_start' );
144
145 //wp_enqueue_script( 'propertyhive_settings', PH()->plugin_url() . '/assets/js/admin/settings.min.js', array( 'jquery'/*, 'jquery-ui-datepicker', 'jquery-ui-sortable', 'iris', 'chosen'*/ ), PH()->version, true );
146
147 /*wp_localize_script( 'propertyhive_settings', 'propertyhive_settings_params', array(
148 'i18n_nav_warning' => __( 'The changes you made will be lost if you navigate away from this page.', 'propertyhive' )
149 ) );*/
150
151 // Include settings pages
152 self::get_settings_pages();
153
154 // Get current tab/section
155 $current_tab = empty( $_GET['tab'] ) ? 'general' : sanitize_title( $_GET['tab'] );
156 $current_section = empty( $_REQUEST['section'] ) ? '' : sanitize_title( $_REQUEST['section'] );
157
158 // Save settings if data has been posted
159 //if ( ! empty( $_POST ) )
160 // self::save();
161
162 // Add any posted messages
163 if ( ! empty( $_GET['ph_error'] ) )
164 self::add_error( stripslashes( $_GET['ph_error'] ) );
165
166 if ( ! empty( $_GET['ph_message'] ) )
167 self::add_message( stripslashes( $_GET['ph_message'] ) );
168
169 self::show_messages();
170
171 // Get tabs for the settings page
172 $tabs = apply_filters( 'propertyhive_settings_tabs_array', array() );
173
174 include 'views/html-admin-settings.php';
175 }
176
177 /**
178 * Get a setting from the settings API.
179 *
180 * @param mixed $option
181 * @return string
182 */
183 public static function get_option( $option_name, $default = '' ) {
184 // Array value
185 if ( strstr( $option_name, '[' ) ) {
186
187 parse_str( $option_name, $option_array );
188
189 // Option name is first key
190 $option_name = current( array_keys( $option_array ) );
191
192 // Get value
193 $option_values = get_option( $option_name, '' );
194
195 $key = key( $option_array[ $option_name ] );
196
197 if ( isset( $option_values[ $key ] ) )
198 $option_value = $option_values[ $key ];
199 else
200 $option_value = null;
201
202 // Single value
203 } else {
204 $option_value = get_option( $option_name, null );
205 }
206
207 if ( is_array( $option_value ) )
208 $option_value = array_map( 'stripslashes', $option_value );
209 elseif ( ! is_null( $option_value ) )
210 $option_value = stripslashes( $option_value );
211
212 return $option_value === null ? $default : $option_value;
213 }
214
215 /**
216 * Output admin fields.
217 *
218 * Loops though the propertyhive options array and outputs each field.
219 *
220 * @access public
221 * @param array $options Opens array to output
222 */
223 public static function output_fields( $options ) {
224 foreach ( $options as $value ) {
225 if ( ! isset( $value['type'] ) ) continue;
226 if ( ! isset( $value['id'] ) ) $value['id'] = '';
227 if ( ! isset( $value['title'] ) ) $value['title'] = isset( $value['name'] ) ? $value['name'] : '';
228 if ( ! isset( $value['class'] ) ) $value['class'] = '';
229 if ( ! isset( $value['css'] ) ) $value['css'] = '';
230 if ( ! isset( $value['default'] ) ) $value['default'] = '';
231 if ( ! isset( $value['desc'] ) ) $value['desc'] = '';
232 if ( ! isset( $value['desc_tip'] ) ) $value['desc_tip'] = false;
233
234 // Custom attribute handling
235 $custom_attributes = array();
236
237 if ( ! empty( $value['custom_attributes'] ) && is_array( $value['custom_attributes'] ) )
238 foreach ( $value['custom_attributes'] as $attribute => $attribute_value )
239 $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
240
241 // Description handling
242 if ( $value['desc_tip'] === true ) {
243 $description = '';
244 $tip = $value['desc'];
245 } elseif ( ! empty( $value['desc_tip'] ) ) {
246 $description = $value['desc'];
247 $tip = $value['desc_tip'];
248 } elseif ( ! empty( $value['desc'] ) ) {
249 $description = $value['desc'];
250 $tip = '';
251 } else {
252 $description = $tip = '';
253 }
254
255 if ( $description && in_array( $value['type'], array( 'textarea', 'radio' ) ) ) {
256 $description = '<p style="margin-top:0">' . wp_kses_post( $description ) . '</p>';
257 } elseif ( $description && in_array( $value['type'], array( 'checkbox' ) ) ) {
258 $description = wp_kses_post( $description );
259 } elseif ( $description ) {
260 $description = '<span class="description">' . wp_kses_post( $description ) . '</span>';
261 }
262
263 if ( $tip && in_array( $value['type'], array( 'checkbox' ) ) ) {
264
265 $tip = '<p class="description">' . $tip . '</p>';
266
267 } elseif ( $tip ) {
268
269 $tip = '<img class="help_tip" data-tip="' . esc_attr( $tip ) . '" src="' . PH()->plugin_url() . '/assets/images/help.png" height="16" width="16" />';
270
271 }
272
273 // Switch based on type
274 switch( $value['type'] ) {
275
276 // Section Titles
277 case 'title':
278 if ( ! empty( $value['title'] ) ) {
279 echo '<h3>' . esc_html( $value['title'] ) . '</h3>';
280 }
281 if ( ! empty( $value['desc'] ) ) {
282 echo wpautop( wptexturize( wp_kses_post( $value['desc'] ) ) );
283 }
284 echo '<table class="form-table">'. "\n\n";
285 if ( ! empty( $value['id'] ) ) {
286 do_action( 'propertyhive_settings_' . sanitize_title( $value['id'] ) );
287 }
288 break;
289
290 // Section Ends
291 case 'sectionend':
292 if ( ! empty( $value['id'] ) ) {
293 do_action( 'propertyhive_settings_' . sanitize_title( $value['id'] ) . '_end' );
294 }
295 echo '</table>';
296 if ( ! empty( $value['id'] ) ) {
297 do_action( 'propertyhive_settings_' . sanitize_title( $value['id'] ) . '_after' );
298 }
299 break;
300
301 case 'html':
302 $full_width = ( isset($value['full_width']) && is_bool($value['full_width']) ) ? $value['full_width'] : false;
303 ?>
304 <tr valign="top" id="row_<?php echo esc_attr( $value['id'] ); ?>">
305 <?php if ( $full_width !== true ) { ?>
306 <th scope="row" class="titledesc">
307 <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
308 <?php echo $tip; ?>
309 </th>
310 <?php } ?>
311 <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
312 <?php echo $value['html']; ?>
313 </td>
314 </tr>
315 <?php
316 break;
317
318 // Standard text inputs and subtypes like 'number'
319 case 'text':
320 case 'email':
321 case 'number':
322 case 'color' :
323 case 'password' :
324
325 $type = $value['type'];
326 $class = '';
327 $option_value = self::get_option( $value['id'], $value['default'] );
328
329 if ( $value['type'] == 'color' ) {
330 $type = 'text';
331 $value['class'] .= 'colorpick';
332 $description .= '<div id="colorPickerDiv_' . esc_attr( $value['id'] ) . '" class="colorpickdiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;display:none;"></div>';
333 }
334
335 ?><tr valign="top" id="row_<?php echo esc_attr( $value['id'] ); ?>">
336 <th scope="row" class="titledesc">
337 <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
338 <?php echo $tip; ?>
339 </th>
340 <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
341 <input
342 name="<?php echo esc_attr( $value['id'] ); ?>"
343 id="<?php echo esc_attr( $value['id'] ); ?>"
344 type="<?php echo esc_attr( $type ); ?>"
345 style="<?php echo esc_attr( $value['css'] ); ?>"
346 value="<?php echo esc_attr( $option_value ); ?>"
347 class="<?php echo esc_attr( $value['class'] ); ?>"
348 <?php echo implode( ' ', $custom_attributes ); ?>
349 /> <?php echo $description; ?>
350 </td>
351 </tr><?php
352 break;
353
354 // Hidden
355 case 'hidden':
356
357 $option_value = self::get_option( $value['id'], $value['default'] );
358
359 ?><input type="hidden"
360 name="<?php echo esc_attr( $value['id'] ); ?>"
361 value="<?php echo esc_attr( $option_value ); ?>"
362 /><?php
363
364 break;
365
366 // Textarea
367 case 'textarea':
368
369 $option_value = self::get_option( $value['id'], $value['default'] );
370
371 ?><tr valign="top" id="row_<?php echo esc_attr( $value['id'] ); ?>">
372 <th scope="row" class="titledesc">
373 <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
374 <?php echo $tip; ?>
375 </th>
376 <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
377 <?php echo $description; ?>
378
379 <textarea
380 name="<?php echo esc_attr( $value['id'] ); ?>"
381 id="<?php echo esc_attr( $value['id'] ); ?>"
382 style="<?php echo esc_attr( $value['css'] ); ?>"
383 class="<?php echo esc_attr( $value['class'] ); ?>"
384 <?php echo implode( ' ', $custom_attributes ); ?>
385 ><?php echo esc_textarea( $option_value ); ?></textarea>
386 </td>
387 </tr><?php
388 break;
389
390 // WYSIWYG
391 case 'wysiwyg':
392
393 $option_value = self::get_option( $value['id'], $value['default'] );
394
395 ?><tr valign="top" id="row_<?php echo esc_attr( $value['id'] ); ?>">
396 <th scope="row" class="titledesc">
397 <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
398 <?php echo $tip; ?>
399 </th>
400 <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
401
402 <?php wp_editor( $option_value, esc_attr( $value['id'] ), array( 'media_buttons' => false, 'textarea_rows' => 3, 'teeny' => true ) ); ?>
403
404 <?php echo '<br>' . $description; ?>
405
406 <?php /*<textarea
407 name="<?php echo esc_attr( $value['id'] ); ?>"
408 id="<?php echo esc_attr( $value['id'] ); ?>"
409 <?php echo implode( ' ', $custom_attributes ); ?>
410 ><?php echo esc_textarea( $option_value ); ?></textarea>*/ ?>
411 </td>
412 </tr><?php
413 break;
414
415 // Select boxes
416 case 'select' :
417 case 'multiselect' :
418
419 $option_value = self::get_option( $value['id'], $value['default'] );
420
421 ?><tr valign="top" id="row_<?php echo esc_attr( $value['id'] ); ?>">
422 <th scope="row" class="titledesc">
423 <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
424 <?php echo $tip; ?>
425 </th>
426 <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
427 <select
428 name="<?php echo esc_attr( $value['id'] ); ?><?php if ( $value['type'] == 'multiselect' ) echo '[]'; ?>"
429 id="<?php echo esc_attr( $value['id'] ); ?>"
430 style="<?php echo esc_attr( $value['css'] ); ?>"
431 class="<?php echo esc_attr( $value['class'] ); ?>"
432 <?php echo implode( ' ', $custom_attributes ); ?>
433 <?php if ( $value['type'] == 'multiselect' ) echo 'multiple="multiple"'; ?>
434 >
435 <?php
436 foreach ( $value['options'] as $key => $val ) {
437 ?>
438 <option value="<?php echo esc_attr( $key ); ?>" <?php
439
440 if ( is_array( $option_value ) )
441 selected( in_array( $key, $option_value ), true );
442 else
443 selected( $option_value, $key );
444
445 ?>><?php echo $val ?></option>
446 <?php
447 }
448 ?>
449 </select> <?php echo $description; ?>
450 </td>
451 </tr><?php
452 break;
453
454 // Radio inputs
455 case 'radio' :
456
457 $option_value = self::get_option( $value['id'], $value['default'] );
458
459 ?><tr valign="top" id="row_<?php echo esc_attr( $value['id'] ); ?>">
460 <th scope="row" class="titledesc">
461 <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
462 <?php echo $tip; ?>
463 </th>
464 <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
465 <fieldset>
466 <?php echo $description; ?>
467 <ul>
468 <?php
469 foreach ( $value['options'] as $key => $val ) {
470 ?>
471 <li>
472 <label><input
473 name="<?php echo esc_attr( $value['id'] ); ?>"
474 value="<?php echo $key; ?>"
475 type="radio"
476 style="<?php echo esc_attr( $value['css'] ); ?>"
477 class="<?php echo esc_attr( $value['class'] ); ?>"
478 <?php echo implode( ' ', $custom_attributes ); ?>
479 <?php checked( $key, $option_value ); ?>
480 /> <?php echo $val ?></label>
481 </li>
482 <?php
483 }
484 ?>
485 </ul>
486 </fieldset>
487 </td>
488 </tr><?php
489 break;
490
491 // Checkbox input
492 case 'checkbox' :
493
494 $name = isset($value['name']) && $value['name'] != '' ? ph_clean($value['name']) : $value['id'];
495 $option_value = isset($value['value']) ? ph_clean($value['value']) : self::get_option( $value['id'], $value['default'] );
496 $fieldset_css = isset($value['fieldset_css']) ? ph_clean($value['fieldset_css']) : '';
497
498 $visbility_class = array();
499
500 if ( ! isset( $value['hide_if_checked'] ) ) {
501 $value['hide_if_checked'] = false;
502 }
503 if ( ! isset( $value['show_if_checked'] ) ) {
504 $value['show_if_checked'] = false;
505 }
506 if ( $value['hide_if_checked'] == 'yes' || $value['show_if_checked'] == 'yes' ) {
507 $visbility_class[] = 'hidden_option';
508 }
509 if ( $value['hide_if_checked'] == 'option' ) {
510 $visbility_class[] = 'hide_options_if_checked';
511 }
512 if ( $value['show_if_checked'] == 'option' ) {
513 $visbility_class[] = 'show_options_if_checked';
514 }
515
516 if ( ! isset( $value['checkboxgroup'] ) || 'start' == $value['checkboxgroup'] ) {
517 ?>
518 <tr valign="top" class="<?php echo esc_attr( implode( ' ', $visbility_class ) ); ?>" id="row_<?php echo esc_attr( $value['id'] ); ?>">
519 <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?></th>
520 <td class="forminp forminp-checkbox">
521 <fieldset style="<?php echo $fieldset_css; ?>">
522 <?php
523 } else {
524 ?>
525 <fieldset style="<?php echo $fieldset_css; ?>" class="<?php echo esc_attr( implode( ' ', $visbility_class ) ); ?>">
526 <?php
527 }
528
529 if ( ! empty( $value['title'] ) ) {
530 ?>
531 <legend class="screen-reader-text"><span><?php echo esc_html( $value['title'] ) ?></span></legend>
532 <?php
533 }
534
535 ?>
536 <label for="<?php echo $value['id'] ?>">
537 <input
538 name="<?php echo esc_attr( $name ); ?>"
539 id="<?php echo esc_attr( $value['id'] ); ?>"
540 type="checkbox"
541 value="1"
542 <?php checked( $option_value, 'yes'); ?>
543 <?php echo implode( ' ', $custom_attributes ); ?>
544 /> <?php echo $description ?>
545 </label> <?php echo $tip; ?>
546 <?php
547
548 if ( ! isset( $value['checkboxgroup'] ) || 'end' == $value['checkboxgroup'] ) {
549 ?>
550 </fieldset>
551 </td>
552 </tr>
553 <?php
554 } else {
555 ?>
556 </fieldset>
557 <?php
558 }
559 break;
560
561 // Image width settings
562 case 'image_width' :
563
564 $width = self::get_option( $value['id'] . '[width]', $value['default']['width'] );
565 $height = self::get_option( $value['id'] . '[height]', $value['default']['height'] );
566 $crop = checked( 1, self::get_option( $value['id'] . '[crop]', $value['default']['crop'] ), false );
567
568 ?><tr valign="top" id="row_<?php echo esc_attr( $value['id'] ); ?>">
569 <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?> <?php echo $tip; ?></th>
570 <td class="forminp image_width_settings">
571
572 <input name="<?php echo esc_attr( $value['id'] ); ?>[width]" id="<?php echo esc_attr( $value['id'] ); ?>-width" type="text" size="3" value="<?php echo $width; ?>" /> &times; <input name="<?php echo esc_attr( $value['id'] ); ?>[height]" id="<?php echo esc_attr( $value['id'] ); ?>-height" type="text" size="3" value="<?php echo $height; ?>" />px
573
574 <label><input name="<?php echo esc_attr( $value['id'] ); ?>[crop]" id="<?php echo esc_attr( $value['id'] ); ?>-crop" type="checkbox" <?php echo $crop; ?> /> <?php _e( 'Hard Crop?', 'propertyhive' ); ?></label>
575
576 </td>
577 </tr><?php
578 break;
579
580 // Image
581 case 'image' :
582
583 $option_value = self::get_option( $value['id'], $value['default'] );
584
585 ?>
586 <tr valign="top" id="row_<?php echo esc_attr( $value['id'] ); ?>_uploaded" <?php if ( $option_value == '' ) { echo ' style="display:none"'; } ?>>
587 <th scope="row" class="titledesc"><?php echo esc_html( __( 'Uploaded', 'propertyhive' ) . ' ' . $value['title'] ); ?></th>
588 <td class="forminp image_settings">
589 <?php
590 $image = wp_get_attachment_image_src( $option_value, 'thumbnail' );
591 if ($image !== FALSE)
592 {
593 echo '<img src="' . $image[0] . '" width="150" alt="">';
594 }
595 else
596 {
597 echo 'Image doesn\'t exist';
598 }
599 ?>
600 </td>
601 </tr>
602 <tr valign="top" id="row_<?php echo esc_attr( $value['id'] ); ?>">
603 <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?> <?php echo $tip; ?></th>
604 <td class="forminp image_settings">
605
606 <a href="" class="button button-primary ph_upload_photo_button<?php echo esc_attr( $value['id'] ); ?>">Select Image</a>
607 <input name="<?php echo esc_attr( $value['id'] ); ?>" id="<?php echo esc_attr( $value['id'] ); ?>" type="hidden" value="<?php echo $option_value; ?>" />
608
609 </td>
610 </tr><?php
611 echo '<script>
612
613 var file_frame' . $value['id'] . ';
614
615 jQuery(document).ready(function()
616 {
617 jQuery(\'body\').on(\'click\', \'.ph_upload_photo_button' . $value['id'] . '\', function( event ){
618
619 event.preventDefault();
620
621 // If the media frame already exists, reopen it.
622 if ( file_frame' . $value['id'] . ' ) {
623 file_frame' . $value['id'] . '.open();
624 return;
625 }
626
627 // Create the media frame.
628 file_frame' . $value['id'] . ' = wp.media.frames.file_frame' . $value['id'] . ' = wp.media({
629 title: jQuery( this ).data( \'uploader_title\' ),
630 button: {
631 text: jQuery( this ).data( \'uploader_button_text\' ),
632 },
633 multiple: false // Set to true to allow multiple files to be selected
634 });
635
636 // When an image is selected, run a callback.
637 file_frame' . $value['id'] . '.on( \'select\', function() {
638 var selection = file_frame' . $value['id'] . '.state().get(\'selection\');
639
640 selection.map( function( attachment ) {
641
642 attachment = attachment.toJSON();
643
644 // Do something with attachment.id and/or attachment.url here
645 console.log(attachment.url);
646
647 // Add selected image to page
648 //add_photo_attachment_to_grid(attachment);
649
650 jQuery(\'#row_' . esc_attr( $value['id'] ) . '_uploaded\').show();
651 jQuery(\'#row_' . esc_attr( $value['id'] ) . '_uploaded td\').html(\'<img src="\' + attachment.url + \'" width="150" alt="">\');
652 jQuery(\'#' . esc_attr( $value['id'] ) . '\').val(attachment.id);
653 });
654 });
655
656 // Finally, open the modal
657 file_frame' . $value['id'] . '.open();
658 });
659 });
660
661 </script>';
662 break;
663
664 // Single page selects
665 case 'single_select_page' :
666
667 $args = array( 'name' => $value['id'],
668 'id' => $value['id'],
669 'sort_column' => 'menu_order',
670 'sort_order' => 'ASC',
671 'show_option_none' => ' ',
672 'class' => $value['class'],
673 'echo' => false,
674 'selected' => absint( self::get_option( $value['id'] ) )
675 );
676
677 if( isset( $value['args'] ) )
678 $args = wp_parse_args( $value['args'], $args );
679
680 ?><tr valign="top" class="single_select_page" id="row_<?php echo esc_attr( $value['id'] ); ?>">
681 <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?> <?php echo $tip; ?></th>
682 <td class="forminp">
683 <?php echo str_replace(' id=', " data-placeholder='" . __( 'Select a page&hellip;', 'propertyhive' ) . "' style='" . $value['css'] . "' class='" . $value['class'] . "' id=", wp_dropdown_pages( $args ) ); ?> <?php echo $description; ?>
684 </td>
685 </tr><?php
686 break;
687
688 // Single country selects
689 case 'single_select_country' :
690 $country_setting = (string) self::get_option( $value['id'] );
691 $countries = PH()->countries->countries;
692
693 if ( strstr( $country_setting, ':' ) ) {
694 $country_setting = explode( ':', $country_setting );
695 $country = current( $country_setting );
696 } else {
697 $country = $country_setting;
698 }
699 ?><tr valign="top" id="row_<?php echo esc_attr( $value['id'] ); ?>">
700 <th scope="row" class="titledesc">
701 <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
702 <?php echo $tip; ?>
703 </th>
704 <td class="forminp">
705 <select name="<?php echo esc_attr( $value['id'] ); ?>" style="<?php echo esc_attr( $value['css'] ); ?>">
706 <?php PH()->countries->country_dropdown_options( $country ); ?>
707 </select>
708 <?php echo $description; ?>
709 </td>
710 </tr><?php
711 break;
712
713 // Country multiselects
714 case 'multi_select_countries' :
715
716 $selections = (array) self::get_option( $value['id'] );
717
718 if ( ! empty( $value['options'] ) )
719 $countries = $value['options'];
720 else
721 $countries = PH()->countries->countries;
722
723 asort( $countries );
724 ?><tr valign="top" id="row_<?php echo esc_attr( $value['id'] ); ?>">
725 <th scope="row" class="titledesc">
726 <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
727 <?php echo $tip; ?>
728 </th>
729 <td class="forminp">
730 <select multiple="multiple" name="<?php echo esc_attr( $value['id'] ); ?>[]" style="<?php echo esc_attr( $value['css'] ); ?>">
731 <?php
732 if ( $countries )
733 foreach ( $countries as $key => $val )
734 echo '<option value="' . esc_attr( $key ) . '" ' . selected( in_array( $key, $selections ), true, false ).'>' . $val['name'] . '</option>';
735 ?>
736 </select> <?php if ( $description ) echo $description; ?>
737 </td>
738 </tr><?php
739 break;
740
741 // Default: run an action
742 default:
743 do_action( 'propertyhive_admin_field_' . $value['type'], $value );
744 break;
745 }
746 }
747 }
748
749 /**
750 * Save admin fields.
751 *
752 * Loops though the propertyhive options array and outputs each field.
753 *
754 * @access public
755 * @param array $options Opens array to output
756 * @return bool
757 */
758 public static function save_fields( $options ) {
759 if ( empty( $_POST ) )
760 return false;
761
762 // Options to update will be stored here
763 $update_options = array();
764
765 // Loop options and get values to save
766 foreach ( $options as $value ) {
767
768 if ( ! isset( $value['id'] ) )
769 continue;
770
771 $type = isset( $value['type'] ) ? sanitize_title( $value['type'] ) : '';
772
773 // Get the option name
774 $option_value = null;
775
776 switch ( $type ) {
777
778 // Standard types
779 case "checkbox" :
780
781 if ( isset( $_POST[ $value['id'] ] ) ) {
782 $option_value = 'yes';
783 } else {
784 $option_value = 'no';
785 }
786
787 break;
788
789 case "textarea" :
790 case "wysiwyg" :
791
792 if ( isset( $_POST[$value['id']] ) ) {
793 $option_value = wp_kses_post( trim( stripslashes( $_POST[ $value['id'] ] ) ) );
794 } else {
795 $option_value = '';
796 }
797
798 break;
799
800 case "text" :
801 case 'email':
802 case 'number':
803 case "select" :
804 case "color" :
805 case 'password' :
806 case "single_select_page" :
807 case "single_select_country" :
808 case 'radio' :
809
810 if ( isset( $_POST[$value['id']] ) ) {
811 $option_value = sanitize_text_field( stripslashes( $_POST[ $value['id'] ] ) );
812 } else {
813 $option_value = '';
814 }
815
816 break;
817
818 // Special types
819 case "multiselect" :
820 case "multi_select_countries" :
821
822 // Get countries array
823 if ( isset( $_POST[ $value['id'] ] ) )
824 $selected_countries = array_map( 'ph_clean', array_map( 'stripslashes', (array) $_POST[ $value['id'] ] ) );
825 else
826 $selected_countries = array();
827
828 $option_value = $selected_countries;
829
830 break;
831
832 case "image_width" :
833
834 if ( isset( $_POST[$value['id'] ]['width'] ) ) {
835
836 $update_options[ $value['id'] ]['width'] = ph_clean( stripslashes( $_POST[ $value['id'] ]['width'] ) );
837 $update_options[ $value['id'] ]['height'] = ph_clean( stripslashes( $_POST[ $value['id'] ]['height'] ) );
838
839 if ( isset( $_POST[ $value['id'] ]['crop'] ) )
840 $update_options[ $value['id'] ]['crop'] = 1;
841 else
842 $update_options[ $value['id'] ]['crop'] = 0;
843
844 } else {
845 $update_options[ $value['id'] ]['width'] = $value['default']['width'];
846 $update_options[ $value['id'] ]['height'] = $value['default']['height'];
847 $update_options[ $value['id'] ]['crop'] = $value['default']['crop'];
848 }
849
850 break;
851
852 // Custom handling
853 default :
854
855 do_action( 'propertyhive_update_option_' . $type, $value );
856
857 break;
858
859 }
860
861 if ( ! is_null( $option_value ) ) {
862 // Check if option is an array
863 if ( strstr( $value['id'], '[' ) ) {
864
865 parse_str( $value['id'], $option_array );
866
867 // Option name is first key
868 $option_name = current( array_keys( $option_array ) );
869
870 // Get old option value
871 if ( ! isset( $update_options[ $option_name ] ) )
872 $update_options[ $option_name ] = get_option( $option_name, array() );
873
874 if ( ! is_array( $update_options[ $option_name ] ) )
875 $update_options[ $option_name ] = array();
876
877 // Set keys and value
878 $key = key( $option_array[ $option_name ] );
879
880 $update_options[ $option_name ][ $key ] = $option_value;
881
882 // Single value
883 } else {
884 $update_options[ $value['id'] ] = $option_value;
885 }
886 }
887
888 // Custom handling
889 do_action( 'propertyhive_update_option', $value );
890 }
891
892 // Now save the options
893 foreach( $update_options as $name => $value )
894 update_option( $name, $value );
895
896 return true;
897 }
898 }
899
900 endif;
901