everest-forms
/
addons
/
StyleCustomizer
/
includes
/
customize
/
class-evf-customize-select2-control.php
class-evf-customize-background-image-control.php
1 year ago
class-evf-customize-color-control.php
1 year ago
class-evf-customize-color-palette-control.php
1 year ago
class-evf-customize-dimension-control.php
1 year ago
class-evf-customize-image-checkbox-control.php
1 year ago
class-evf-customize-image-radio-control.php
1 year ago
class-evf-customize-select2-control.php
1 year ago
class-evf-customize-slider-control.php
1 year ago
class-evf-customize-templates-section.php
1 year ago
class-evf-customize-toggle-control.php
1 year ago
class-evf-customize-select2-control.php
133 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Customize API: EVF_Customize_Select2_Control class |
| 4 | * |
| 5 | * @package EverestForms_Style_Customizer\Customize |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Customize Select2 Control class. |
| 13 | * |
| 14 | * @see WP_Customize_Control |
| 15 | */ |
| 16 | class EVF_Customize_Select2_Control extends WP_Customize_Control { |
| 17 | |
| 18 | /** |
| 19 | * Type. |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | public $type = 'evf-select2'; |
| 24 | |
| 25 | /** |
| 26 | * Use Google Fonts. |
| 27 | * |
| 28 | * @var bool |
| 29 | */ |
| 30 | public $google_font = false; |
| 31 | |
| 32 | /** |
| 33 | * Google Font List. |
| 34 | * |
| 35 | * @var array |
| 36 | */ |
| 37 | public $google_font_list = array(); |
| 38 | |
| 39 | /** |
| 40 | * Item List. |
| 41 | * |
| 42 | * @var array |
| 43 | */ |
| 44 | public $item_list = array(); |
| 45 | |
| 46 | /** |
| 47 | * Enqueue controls scripts. |
| 48 | */ |
| 49 | public function enqueue() { |
| 50 | wp_enqueue_style( 'selectWoo' ); |
| 51 | wp_enqueue_script( 'selectWoo' ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Refresh the parameters passed to the JavaScript via JSON. |
| 56 | * |
| 57 | * @uses WP_Customize_Control::to_json() |
| 58 | */ |
| 59 | public function to_json() { |
| 60 | parent::to_json(); |
| 61 | $this->json['id'] = $this->id; |
| 62 | $this->json['value'] = $this->value(); |
| 63 | $this->json['link'] = $this->get_link(); |
| 64 | $this->json['choices'] = $this->choices; |
| 65 | $this->json['default'] = $this->setting->default; |
| 66 | $this->json['google_font'] = $this->google_font; |
| 67 | $this->json['google_font_list'] = ( $this->google_font ) ? $this->get_google_fonts() : array(); |
| 68 | $this->json['item_list'] = $this->item_list; |
| 69 | |
| 70 | $this->json['inputAttrs'] = ''; |
| 71 | foreach ( $this->input_attrs as $attr => $value ) { |
| 72 | $this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" '; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Don't render the control content from PHP, as it's rendered via JS on load. |
| 78 | */ |
| 79 | public function render_content() {} |
| 80 | |
| 81 | /** |
| 82 | * Render a JS template for control display. |
| 83 | * |
| 84 | * @see WP_Customize_Control::print_template() |
| 85 | */ |
| 86 | public function content_template() { |
| 87 | ?> |
| 88 | <# if ( data.label ) { #> |
| 89 | <span class="customize-control-title">{{ data.label }}</span> |
| 90 | <# } #> |
| 91 | <# if ( data.description ) { #> |
| 92 | <span class="description customize-control-description">{{{ data.description }}}</span> |
| 93 | <# } #> |
| 94 | |
| 95 | <select class="evf-select2" {{{data.link}}} {{{data.inputAttrs}}} > |
| 96 | <# if ( data.google_font === true ) { #> |
| 97 | <# Object.keys( data.google_font_list ).forEach( function( key ) { #> |
| 98 | <option value='{{{ data.google_font_list[key].family }}}' {{{ (data.value === data.google_font_list[key].family )?'selected':'' }}} >{{{ data.google_font_list[key].family }}}</option> |
| 99 | <# } ); #> |
| 100 | <#} else {#> |
| 101 | <# Object.keys( data.item_list ).forEach( function( key ) { #> |
| 102 | <option value='{{{ key }}}' {{{ (data.value === key )?'selected':'' }}} >{{{ data.item_list[key] }}}</option> |
| 103 | <# } ); #> |
| 104 | <# } #> |
| 105 | </select> |
| 106 | <?php |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns Google Fonts. |
| 111 | * |
| 112 | * @return object Google fonts object. |
| 113 | */ |
| 114 | public function get_google_fonts() { |
| 115 | $google_fonts = get_transient( 'evf_google_fonts' ); |
| 116 | |
| 117 | if ( false === $google_fonts ) { |
| 118 | $raw_google_fonts = wp_safe_remote_get( 'https://raw.githubusercontent.com/wpeverest/google-fonts/master/google-fonts.json' ); |
| 119 | |
| 120 | if ( ! is_wp_error( $raw_google_fonts ) ) { |
| 121 | $google_fonts = json_decode( wp_remote_retrieve_body( $raw_google_fonts ) ); |
| 122 | |
| 123 | if ( isset( $google_fonts->items ) ) { |
| 124 | $google_fonts = $google_fonts->items; |
| 125 | set_transient( 'evf_google_fonts', $google_fonts, WEEK_IN_SECONDS ); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return apply_filters( 'everest_forms_extensions_sections', $google_fonts ); |
| 131 | } |
| 132 | } |
| 133 |