class-redux-field.php (8048B)
1 <?php 2 /** 3 * Redux Field Class 4 * 5 * @class Redux_Field 6 * @version 4.0.0 7 * @package Redux Framework/Classes 8 */ 9 10 defined( 'ABSPATH' ) || exit; 11 12 use Redux_Descriptor_Types as RDT; // TODO Require instead! 13 14 if ( ! class_exists( 'Redux_Field', false ) ) { 15 16 /** 17 * Class Redux_Field 18 */ 19 abstract class Redux_Field { 20 21 /** 22 * Array of descriptors. 23 * 24 * @var Redux_Descriptor[] 25 */ 26 public static $descriptors = array(); 27 28 /** 29 * Make base descriptor. 30 * 31 * @return Redux_Descriptor 32 */ 33 public static function make_base_descriptor(): Redux_Descriptor { 34 $d = new Redux_Descriptor( get_called_class() ); 35 self::$descriptors[ get_called_class() ] = $d; 36 37 $d->add_field( 'id', __( 'Field ID', 'redux-framework' ), RDT::TEXT )->set_order( 0 )->set_required(); 38 $d->add_field( 'title', __( 'Title', 'redux-framework' ), RDT::TEXT )->set_order( 1 ); 39 $d->add_field( 'subtitle', __( 'Subtitle', 'redux-framework' ), RDT::TEXT )->set_order( 2 ); 40 $d->add_field( 'desc', __( 'Description', 'redux-framework' ), RDT::TEXT )->set_order( 3 ); 41 $d->add_field( 'class', __( 'Class', 'redux-framework' ), RDT::TEXT )->set_order( 3 ); 42 $d->add_field( 'compiler', __( 'Compiler', 'redux-framework' ), RDT::BOOL, '', false )->set_order( 60 ); 43 $d->add_field( 'default', __( 'Default', 'redux-framework' ), RDT::OPTIONS, '', false )->set_order( 60 ); 44 $d->add_field( 'disabled', __( 'Disabled', 'redux-framework' ), RDT::BOOL, '', false )->set_order( 60 ); 45 $d->add_field( 'hint', __( 'Hint', 'redux-framework' ), RDT::OPTIONS, '', false )->set_order( 60 ); 46 $d->add_field( 'hint', __( 'Permissions', 'redux-framework' ), RDT::OPTIONS, '', false )->set_order( 60 ); 47 $d->add_field( 'required', __( 'Required', 'redux-framework' ), RDT::BOOL, '', false )->set_order( 60 ); 48 49 return $d; 50 } 51 52 /** 53 * Renders an attribute array into a html attributes string. 54 * 55 * @param array $attributes HTML attributes. 56 * 57 * @return string 58 */ 59 public static function render_attributes( array $attributes = array() ): string { 60 $output = ''; 61 62 if ( empty( $attributes ) ) { 63 return $output; 64 } 65 66 foreach ( $attributes as $key => $value ) { 67 if ( false === $value || '' === $value ) { 68 continue; 69 } 70 71 if ( is_array( $value ) ) { 72 $value = wp_json_encode( $value ); 73 } 74 75 $output .= sprintf( true === $value ? ' %s' : ' %s="%s"', $key, esc_attr( $value ) ); 76 } 77 78 return $output; 79 } 80 81 /** 82 * Get descriptor. 83 * 84 * @return Redux_Descriptor 85 */ 86 public static function get_descriptor(): Redux_Descriptor { 87 if ( ! isset( static::$descriptors[ get_called_class() ] ) ) { 88 static::make_descriptor(); 89 } 90 91 $d = self::$descriptors[ get_called_class() ]; 92 93 static::make_descriptor(); 94 95 // This part is out of opt name because it's non vendor dependant! 96 return apply_filters( 'redux/field/' . $d->get_field_type() . '/get_descriptor', $d ); // phpcs:ignore WordPress.NamingConventions.ValidHookName 97 } 98 99 /** 100 * Build the field descriptor in this function. 101 */ 102 public static function make_descriptor() { 103 static::make_base_descriptor(); 104 } 105 106 107 /** 108 * CSS styling per field output/compiler. 109 * 110 * @var string 111 */ 112 public $style = null; 113 114 /** 115 * Class dir. 116 * 117 * @var string 118 */ 119 public $dir = null; 120 121 /** 122 * Class URL. 123 * 124 * @var string 125 */ 126 public $url = null; 127 128 /** 129 * Timestamp for ver append in dev_mode 130 * 131 * @var string 132 */ 133 public $timestamp = null; 134 135 /** 136 * ReduxFramework object pointer. 137 * 138 * @var ReduxFramework 139 */ 140 public $parent; 141 142 /** 143 * Field values. 144 * 145 * @var string|array 146 */ 147 public $value; 148 149 /** 150 * Redux_Field constructor. 151 * 152 * @param array|string|null $field Field array. 153 * @param string|array|null $value Field values. 154 * @param null $parent ReduxFramework object pointer. 155 * 156 * @throws ReflectionException Comment. 157 */ 158 public function __construct( $field = array(), $value = null, $parent = null ) { 159 $this->parent = $parent; 160 $this->field = $field; 161 $this->value = $value; 162 163 $this->select2_config = array( 164 'width' => 'resolve', 165 'allowClear' => false, 166 'theme' => 'default', 167 ); 168 169 $this->set_defaults(); 170 171 $class_name = get_class( $this ); 172 $reflector = new ReflectionClass( $class_name ); 173 $path = $reflector->getFilename(); 174 $path_info = Redux_Helpers::path_info( $path ); 175 $this->dir = trailingslashit( dirname( $path_info['real_path'] ) ); 176 $this->url = trailingslashit( dirname( $path_info['url'] ) ); 177 178 $this->timestamp = Redux_Core::$version; 179 if ( $parent->args['dev_mode'] ) { 180 $this->timestamp .= '.' . time(); 181 } 182 } 183 184 /** 185 * Retrive dirname. 186 * 187 * @return string 188 */ 189 protected function get_dir(): ?string { 190 return $this->dir; 191 } 192 193 /** 194 * Media query compiler for Redux Pro, 195 * 196 * @param string $style_data CSS string. 197 */ 198 public function media_query( string $style_data = '' ) { 199 $query_arr = $this->field['media_query']; 200 $css = ''; 201 202 if ( isset( $query_arr['queries'] ) ) { 203 foreach ( $query_arr['queries'] as $idx => $query ) { 204 $rule = $query['rule'] ?? ''; 205 $selectors = $query['selectors'] ?? array(); 206 207 if ( ! is_array( $selectors ) && '' !== $selectors ) { 208 $selectors = array( $selectors ); 209 } 210 211 if ( '' !== $rule && ! empty( $selectors ) ) { 212 $selectors = implode( ',', $selectors ); 213 214 $css .= '@media ' . $rule . '{'; 215 $css .= $selectors . '{' . $style_data . '}'; 216 $css .= '}'; 217 } 218 } 219 } else { 220 return; 221 } 222 223 if ( isset( $query_arr['output'] ) && $query_arr['output'] ) { 224 $this->parent->outputCSS .= $css; 225 } 226 227 if ( isset( $query_arr['compiler'] ) && $query_arr['compiler'] ) { 228 $this->parent->compilerCSS .= $css; 229 } 230 } 231 232 /** 233 * CSS for field output, if set (Remove the noinpection line and fix this function when we drop support for PHP 7.1). 234 * 235 * @param string $style CSS string. 236 * 237 * @noinspection PhpMissingParamTypeInspection 238 */ 239 public function output( $style = '' ) { 240 if ( '' !== $style ) { 241 242 // Force output value into an array. 243 if ( isset( $this->field['output'] ) && ! is_array( $this->field['output'] ) ) { 244 $this->field['output'] = array( $this->field['output'] ); 245 } 246 247 if ( ! empty( $this->field['output'] ) && is_array( $this->field['output'] ) ) { 248 if ( isset( $this->field['output']['important'] ) ) { 249 if ( $this->field['output']['important'] ) { 250 $style = str_replace( ';', ' !important;', $style ); 251 } 252 unset( $this->field['output']['important'] ); 253 } 254 255 $keys = implode( ',', $this->field['output'] ); 256 $this->parent->outputCSS .= $keys . '{' . $style . '}'; 257 } 258 259 // Force compiler value into an array. 260 if ( isset( $this->field['compiler'] ) && ! is_array( $this->field['compiler'] ) ) { 261 $this->field['compiler'] = array( $this->field['compiler'] ); 262 } 263 264 if ( isset( $this->field['compiler']['important'] ) ) { 265 if ( $this->field['compiler']['important'] ) { 266 $style = str_replace( ';', ' !important;', $style ); 267 } 268 unset( $this->field['compiler']['important'] ); 269 } 270 271 if ( ! empty( $this->field['compiler'] ) && is_array( $this->field['compiler'] ) ) { 272 $keys = implode( ',', $this->field['compiler'] ); 273 $this->parent->compilerCSS .= $keys . '{' . $style . '}'; 274 } 275 } 276 } 277 278 /** 279 * Unused for now. 280 * 281 * @param mixed $data CSS data. 282 */ 283 public function css_style( $data ) { 284 285 } 286 287 /** 288 * Unused for now. 289 */ 290 public function set_defaults() { 291 292 } 293 294 /** 295 * Unused for now. 296 */ 297 public function render() { 298 299 } 300 301 /** 302 * Unused for now. 303 */ 304 public function enqueue() { 305 306 } 307 308 /** 309 * Unused for now. 310 * 311 * @param array $field Field array. 312 * @param string $value Value array. 313 */ 314 public function localize( array $field, string $value = '' ) { 315 316 } 317 } 318 }