| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
if ( ! class_exists( 'ReduxFrameworkLegacy_select' ) ) { |
| 9 |
class ReduxFrameworkLegacy_select { |
| 10 |
|
| 11 |
public $parent; |
| 12 |
public $field; |
| 13 |
public $value; |
| 14 |
/** |
| 15 |
* Field Constructor. |
| 16 |
* Required - must call the parent constructor, then assign field and value to vars, and obviously call the render field function |
| 17 |
* |
| 18 |
* @since ReduxFrameworkLegacy 1.0.0 |
| 19 |
*/ |
| 20 |
public function __construct( $field = array(), $value = '', $parent = null ) { |
| 21 |
$this->parent = $parent; |
| 22 |
$this->field = $field; |
| 23 |
$this->value = $value; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Field Render Function. |
| 28 |
* Takes the vars and outputs the HTML for the field in the settings |
| 29 |
* |
| 30 |
* @since ReduxFrameworkLegacy 1.0.0 |
| 31 |
*/ |
| 32 |
public function render() { |
| 33 |
$sortable = ( isset( $this->field['sortable'] ) && $this->field['sortable'] ) ? ' select2-sortable"' : ''; |
| 34 |
|
| 35 |
if ( ! empty( $sortable ) ) { // Dummy proofing :P |
| 36 |
$this->field['multi'] = true; |
| 37 |
} |
| 38 |
|
| 39 |
if ( ! empty( $this->field['data'] ) && empty( $this->field['options'] ) ) { |
| 40 |
if ( empty( $this->field['args'] ) ) { |
| 41 |
$this->field['args'] = array(); |
| 42 |
} |
| 43 |
|
| 44 |
if ( $this->field['data'] == 'elusive-icons' || $this->field['data'] == 'elusive-icon' || $this->field['data'] == 'elusive' ) { |
| 45 |
$icons_file = ReduxFrameworkLegacy::$_dir . 'inc/fields/select/elusive-icons.php'; |
| 46 |
/** |
| 47 |
* filter 'redux-font-icons-file}' |
| 48 |
* |
| 49 |
* @param array $icon_file File for the icons |
| 50 |
*/ |
| 51 |
$icons_file = apply_filters( 'redux-font-icons-file', $icons_file ); |
| 52 |
|
| 53 |
/** |
| 54 |
* filter 'redux/{opt_name}/field/font/icons/file' |
| 55 |
* |
| 56 |
* @param array $icon_file File for the icons |
| 57 |
*/ |
| 58 |
$icons_file = apply_filters( "redux/{$this->parent->args['opt_name']}/field/font/icons/file", $icons_file ); |
| 59 |
if ( file_exists( $icons_file ) ) { |
| 60 |
require_once $icons_file; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
$this->field['options'] = $this->parent->get_wordpress_data( $this->field['data'], $this->field['args'] ); |
| 65 |
} |
| 66 |
|
| 67 |
if ( ! empty( $this->field['data'] ) && ( $this->field['data'] == 'elusive-icons' || $this->field['data'] == 'elusive-icon' || $this->field['data'] == 'elusive' ) ) { |
| 68 |
$this->field['class'] .= ' font-icons'; |
| 69 |
} |
| 70 |
// if |
| 71 |
|
| 72 |
if ( ! empty( $this->field['options'] ) ) { |
| 73 |
$multi = ( isset( $this->field['multi'] ) && $this->field['multi'] ) ? ' multiple="multiple"' : ''; |
| 74 |
|
| 75 |
if ( ! empty( $this->field['width'] ) ) { |
| 76 |
$width = ' style="' . $this->field['width'] . '"'; |
| 77 |
} else { |
| 78 |
$width = ' style="width: 40%;"'; |
| 79 |
} |
| 80 |
|
| 81 |
$nameBrackets = ''; |
| 82 |
if ( ! empty( $multi ) ) { |
| 83 |
$nameBrackets = '[]'; |
| 84 |
} |
| 85 |
|
| 86 |
$placeholder = ( isset( $this->field['placeholder'] ) ) ? esc_attr( $this->field['placeholder'] ) : __( 'Select an item', 'redux-framework' ); |
| 87 |
|
| 88 |
if ( isset( $this->field['select2'] ) ) { // if there are any let's pass them to js |
| 89 |
$select2_params = json_encode( $this->field['select2'] ); |
| 90 |
$select2_params = htmlspecialchars( $select2_params, ENT_QUOTES ); |
| 91 |
|
| 92 |
echo '<input type="hidden" class="select2_params" value="' . $select2_params . '">'; |
| 93 |
} |
| 94 |
|
| 95 |
if ( isset( $this->field['multi'] ) && $this->field['multi'] && isset( $this->field['sortable'] ) && $this->field['sortable'] && ! empty( $this->value ) && is_array( $this->value ) ) { |
| 96 |
$origOption = $this->field['options']; |
| 97 |
$this->field['options'] = array(); |
| 98 |
|
| 99 |
foreach ( $this->value as $value ) { |
| 100 |
$this->field['options'][ $value ] = $origOption[ $value ]; |
| 101 |
} |
| 102 |
|
| 103 |
if ( count( $this->field['options'] ) < count( $origOption ) ) { |
| 104 |
foreach ( $origOption as $key => $value ) { |
| 105 |
if ( ! in_array( $key, $this->field['options'] ) ) { |
| 106 |
$this->field['options'][ $key ] = $value; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
$sortable = ( isset( $this->field['sortable'] ) && $this->field['sortable'] ) ? ' select2-sortable"' : ''; |
| 113 |
|
| 114 |
echo '<select ' . $multi . ' id="' . $this->field['id'] . '-select" data-placeholder="' . $placeholder . '" name="' . $this->field['name'] . $this->field['name_suffix'] . $nameBrackets . '" class="redux-select-item ' . $this->field['class'] . $sortable . '"' . $width . ' rows="6">'; |
| 115 |
echo '<option></option>'; |
| 116 |
|
| 117 |
foreach ( $this->field['options'] as $k => $v ) { |
| 118 |
|
| 119 |
if ( is_array( $v ) ) { |
| 120 |
echo '<optgroup label="' . $k . '">'; |
| 121 |
|
| 122 |
foreach ( $v as $opt => $val ) { |
| 123 |
$this->make_option( $opt, $val, $k ); |
| 124 |
} |
| 125 |
|
| 126 |
echo '</optgroup>'; |
| 127 |
|
| 128 |
continue; |
| 129 |
} |
| 130 |
|
| 131 |
$this->make_option( $k, $v ); |
| 132 |
} |
| 133 |
// foreach |
| 134 |
|
| 135 |
echo '</select>'; |
| 136 |
} else { |
| 137 |
echo '<strong>' . __( 'No items of this type were found.', 'redux-framework' ) . '</strong>'; |
| 138 |
} |
| 139 |
} //function |
| 140 |
|
| 141 |
private function make_option( $id, $value, $group_name = '' ) { |
| 142 |
if ( is_array( $this->value ) ) { |
| 143 |
$selected = ( is_array( $this->value ) && in_array( $id, $this->value ) ) ? ' selected="selected"' : ''; |
| 144 |
} else { |
| 145 |
$selected = selected( $this->value, $id, false ); |
| 146 |
} |
| 147 |
|
| 148 |
echo '<option value="' . $id . '"' . $selected . '>' . $value . '</option>'; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Enqueue Function. |
| 153 |
* If this field requires any scripts, or css define this function and register/enqueue the scripts/css |
| 154 |
* |
| 155 |
* @since ReduxFrameworkLegacy 1.0.0 |
| 156 |
*/ |
| 157 |
public function enqueue() { |
| 158 |
wp_enqueue_style( 'select2-css' ); |
| 159 |
|
| 160 |
if ( isset( $this->field['sortable'] ) && $this->field['sortable'] ) { |
| 161 |
wp_enqueue_script( 'jquery-ui-sortable' ); |
| 162 |
} |
| 163 |
|
| 164 |
wp_enqueue_script( |
| 165 |
'redux-field-select-js', |
| 166 |
ReduxFrameworkLegacy::$_url . 'inc/fields/select/field_select' . ReduxLegacy_Functions::isMin() . '.js', |
| 167 |
array( 'jquery', 'select2-js', 'redux-js' ), |
| 168 |
time(), |
| 169 |
true |
| 170 |
); |
| 171 |
|
| 172 |
if ( $this->parent->args['dev_mode'] ) { |
| 173 |
wp_enqueue_style( |
| 174 |
'redux-field-select-css', |
| 175 |
ReduxFrameworkLegacy::$_url . 'inc/fields/select/field_select.css', |
| 176 |
array(), |
| 177 |
time(), |
| 178 |
'all' |
| 179 |
); |
| 180 |
} |
| 181 |
} //function |
| 182 |
} //class |
| 183 |
} |
| 184 |
|