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