class-redux-text.php (6264B)
1 <?php 2 /** 3 * Text Field 4 * 5 * @package Redux Framework/Fields 6 * @author Dovy Paukstys & Kevin Provance (kprovance) 7 * @version 4.0.0 8 */ 9 10 defined( 'ABSPATH' ) || exit; 11 12 use Redux_Descriptor_Types as RDT; 13 14 if ( ! class_exists( 'Redux_Text', false ) ) { 15 16 /** 17 * Class Redux_Text 18 */ 19 class Redux_Text extends Redux_Field { 20 21 /** 22 * Set the field's custom descriptors. 23 */ 24 public static function make_descriptor() { 25 $d = static::make_base_descriptor(); 26 27 $d->set_info( 'Text', __( 'The Text field accepts any form of text and optionally validates the text before saving the value.', 'redux-framework' ) ); 28 29 $d->add_field( 'text_hint', __( 'Display a qTip div when active on the text field.', 'redux-framework' ), RDT::TEXT )->set_order( 100 )->set_required(); 30 $d->add_field( 'placeholder', __( 'Placeholder text.', 'redux-framework' ), RDT::TEXT )->set_order( 100 )->set_required(); 31 } 32 33 /** 34 * Field Render Function. 35 * Takes the vars and outputs the HTML for the field in the settings 36 * 37 * @since ReduxFramework 1.0.0 38 */ 39 public function render() { 40 41 $this->field['attributes'] = wp_parse_args( 42 $this->field['attributes'] ?? array(), 43 array( 44 'qtip_title' => '', 45 'qtip_text' => '', 46 'class' => isset( $this->field['class'] ) && ! empty( $this->field['class'] ) ? array( trim( $this->field['class'] ) ) : array(), 47 'readonly' => isset( $this->field['readonly'] ) && $this->field['readonly'] ? 'readonly' : '', 48 'autocomplete' => isset( $this->field['autocomplete'] ) && false === $this->field['autocomplete'] ? 'off' : '', 49 'type' => ! isset( $this->field['type'] ) ? 'text' : $this->field['type'], 50 ) 51 ); 52 $this->field['attributes']['class'][] = 'regular-text'; 53 54 // Deprecated from the docs. Left as not to break user's code! 55 if ( isset( $this->field['text_hint'] ) && ! empty( $this->field['text_hint'] ) ) { 56 if ( ! is_array( $this->field['text_hint'] ) && is_string( $this->field['text_hint'] ) ) { 57 $this->field['text_hint'] = array( 58 'content' => $this->field['text_hint'], 59 ); 60 } else { 61 if ( isset( $this->field['text_hint']['title'] ) && ! empty( $this->field['text_hint']['title'] ) ) { 62 if ( ! isset( $this->field['text_hint']['content'] ) || ( isset( $this->field['text_hint']['content'] ) && empty( $this->field['text_hint']['content'] ) ) ) { 63 $this->field['text_hint']['content'] = $this->field['text_hint']['title']; 64 unset( $this->field['text_hint']['title'] ); 65 } 66 } 67 } 68 $this->field['attributes']['qtip_title'] = isset( $this->field['text_hint']['title'] ) ? 'qtip-title="' . $this->field['text_hint']['title'] . '" ' : ''; 69 $this->field['attributes']['qtip_text'] = isset( $this->field['text_hint']['content'] ) ? 'qtip-content="' . $this->field['text_hint']['content'] . '" ' : ''; 70 } 71 72 if ( ! empty( $this->field['data'] ) && is_array( $this->field['data'] ) ) { 73 $this->field['options'] = $this->field['data']; 74 unset( $this->field['data'] ); 75 } 76 77 if ( ! empty( $this->field['data'] ) && empty( $this->field['options'] ) ) { 78 if ( empty( $this->field['args'] ) ) { 79 $this->field['args'] = array(); 80 } 81 if ( isset( $this->field['options'] ) && is_array( $this->field['options'] ) && ! is_array( min( $this->field['options'] ) ) ) { 82 $this->field['options'] = $this->parent->wordpress_data->get( $this->field['data'], $this->field['args'], $this->value ); 83 $this->field['attributes']['class'][] = 'hasOptions'; 84 } 85 } 86 87 if ( empty( $this->value ) && ! empty( $this->field['options'] ) ) { 88 $this->value = $this->field['options']; 89 } 90 91 $this->field['attributes']['class'] = implode( ' ', $this->field['attributes']['class'] ); 92 93 if ( isset( $this->field['options'] ) && ! empty( $this->field['options'] ) ) { 94 if ( ! isset( $this->value ) || ( isset( $this->value ) && ! is_array( $this->value ) ) ) { 95 $this->value = array(); 96 } 97 foreach ( $this->field['options'] as $k => $v ) { 98 $attributes = $this->field['attributes']; 99 if ( ! isset( $this->value[ $k ] ) ) { 100 $this->value[ $k ] = $v; 101 } 102 $attributes['value'] = $this->value[ $k ]; 103 if ( ! empty( $placeholder ) ) { 104 $attributes['placeholder'] = ( is_array( $this->field['placeholder'] ) && isset( $this->field['placeholder'][ $k ] ) ) ? esc_attr( $this->field['placeholder'][ $k ] ) : ''; 105 } 106 $attributes['name'] = esc_attr( $this->field['name'] . $this->field['name_suffix'] . '[' . esc_attr( $k ) ) . ']'; 107 $attributes['id'] = esc_attr( $this->field['id'] . $k ); 108 109 $attributes_string = $this->render_attributes( $attributes ); 110 echo '<div class="input_wrapper"><label for="' . $attributes['name'] . '">' . $v . '</label><input placeholder="' . $v . '" ' . $attributes_string . '></div>'; // phpcs:ignore WordPress.Security.EscapeOutput 111 112 } 113 } else { 114 $this->field['attributes']['id'] = $this->field['id']; 115 $this->field['attributes']['name'] = esc_attr( $this->field['name'] . $this->field['name_suffix'] ); 116 $this->field['attributes']['value'] = $this->value; 117 $this->field['attributes']['placeholder'] = ( isset( $this->field['placeholder'] ) && ! is_array( $this->field['placeholder'] ) ) ? esc_attr( $this->field['placeholder'] ) : ''; 118 $attributes_string = $this->render_attributes( $this->field['attributes'] ); 119 echo '<input ' . $attributes_string . '>'; // phpcs:ignore WordPress.Security.EscapeOutput 120 } 121 } 122 123 /** 124 * Enqueue Function. 125 * If this field requires any scripts, or css define this function and register/enqueue the scripts/css 126 * 127 * @since ReduxFramework 3.0.0 128 */ 129 public function enqueue() { 130 if ( $this->parent->args['dev_mode'] ) { 131 wp_enqueue_style( 132 'redux-field-text-css', 133 Redux_Core::$url . 'inc/fields/text/redux-text.css', 134 array(), 135 $this->timestamp 136 ); 137 } 138 } 139 140 /** 141 * Enable output_variables to be generated. 142 * 143 * @since 4.0.3 144 * @return void 145 */ 146 public function output_variables() { 147 // No code needed, just defining the method is enough. 148 } 149 } 150 } 151 152 class_alias( 'Redux_Text', 'ReduxFramework_Text' );