controls.php (7239B)
1 <?php 2 namespace Elementor; 3 4 if ( ! defined( 'ABSPATH' ) ) { 5 exit; // Exit if accessed directly. 6 } 7 8 /** 9 * Elementor settings controls. 10 * 11 * Elementor settings controls handler class responsible for creating the final 12 * HTML for various input field types used in Elementor settings pages. 13 * 14 * @since 1.0.0 15 */ 16 class Settings_Controls { 17 18 /** 19 * Render settings control. 20 * 21 * Generates the final HTML on the frontend for any given field based on 22 * the field type (text, select, checkbox, raw HTML, etc.). 23 * 24 * @since 1.0.0 25 * @access public 26 * @static 27 * 28 * @param array $field Optional. Field data. Default is an empty array. 29 */ 30 public static function render( $field = [] ) { 31 if ( empty( $field ) || empty( $field['id'] ) ) { 32 return; 33 } 34 35 $defaults = [ 36 'type' => '', 37 'attributes' => [], 38 'std' => '', 39 'desc' => '', 40 ]; 41 42 $field = array_merge( $defaults, $field ); 43 44 $method_name = $field['type']; 45 46 if ( ! method_exists( __CLASS__, $method_name ) ) { 47 $method_name = 'text'; 48 } 49 50 self::$method_name( $field ); 51 } 52 53 /** 54 * Render text control. 55 * 56 * Generates the final HTML for text controls. 57 * 58 * @since 2.0.0 59 * @access private 60 * @static 61 * 62 * @param array $field Field data. 63 */ 64 private static function text( array $field ) { 65 if ( empty( $field['attributes']['class'] ) ) { 66 $field['attributes']['class'] = 'regular-text'; 67 } 68 69 ?> 70 <input type="<?php echo esc_attr( $field['type'] ); ?>" id="<?php echo esc_attr( $field['id'] ); ?>" name="<?php echo esc_attr( $field['id'] ); ?>" value="<?php echo esc_attr( get_option( $field['id'], $field['std'] ) ); ?>" <?php Utils::print_html_attributes( $field['attributes'] ); ?>/> 71 <?php 72 if ( ! empty( $field['sub_desc'] ) ) : 73 echo wp_kses_post( $field['sub_desc'] ); 74 endif; 75 ?> 76 <?php if ( ! empty( $field['desc'] ) ) : ?> 77 <p class="description"><?php echo wp_kses_post( $field['desc'] ); ?></p> 78 <?php 79 endif; 80 } 81 82 /** 83 * Render checkbox control. 84 * 85 * Generates the final HTML for checkbox controls. 86 * 87 * @since 2.0.0 88 * @access private 89 * @static 90 * 91 * @param array $field Field data. 92 */ 93 private static function checkbox( array $field ) { 94 ?> 95 <label> 96 <input type="<?php echo esc_attr( $field['type'] ); ?>" id="<?php echo esc_attr( $field['id'] ); ?>" name="<?php echo esc_attr( $field['id'] ); ?>" value="<?php echo esc_attr( $field['value'] ); ?>"<?php checked( $field['value'], get_option( $field['id'], $field['std'] ) ); ?> /> 97 <?php 98 if ( ! empty( $field['sub_desc'] ) ) : 99 echo wp_kses_post( $field['sub_desc'] ); 100 endif; 101 ?> 102 </label> 103 <?php if ( ! empty( $field['desc'] ) ) : ?> 104 <p class="description"><?php echo wp_kses_post( $field['desc'] ); ?></p> 105 <?php 106 endif; 107 } 108 109 /** 110 * Render checkbox list control. 111 * 112 * Generates the final HTML for checkbox list controls. 113 * 114 * @since 2.0.0 115 * @access private 116 * @static 117 * 118 * @param array $field Field data. 119 */ 120 private static function checkbox_list( array $field ) { 121 $old_value = get_option( $field['id'], $field['std'] ); 122 if ( ! is_array( $old_value ) ) { 123 $old_value = []; 124 } 125 126 foreach ( $field['options'] as $option_key => $option_value ) : 127 ?> 128 <label> 129 <input type="checkbox" name="<?php echo esc_attr( $field['id'] ); ?>[]" value="<?php echo esc_attr( $option_key ); ?>"<?php checked( in_array( $option_key, $old_value ), true ); ?> /> 130 <?php echo wp_kses_post( $option_value ); ?> 131 </label><br /> 132 <?php endforeach; ?> 133 <?php if ( ! empty( $field['desc'] ) ) : ?> 134 <p class="description"><?php echo wp_kses_post( $field['desc'] ); ?></p> 135 <?php 136 endif; 137 } 138 139 /** 140 * Render select control. 141 * 142 * Generates the final HTML for select controls. 143 * 144 * @since 2.0.0 145 * @access private 146 * @static 147 * 148 * @param array $field Field data. 149 */ 150 private static function select( array $field ) { 151 $old_value = get_option( $field['id'], $field['std'] ); 152 ?> 153 <select name="<?php echo esc_attr( $field['id'] ); ?>"> 154 <?php if ( ! empty( $field['show_select'] ) ) : ?> 155 <option value="">— <?php echo esc_html__( 'Select', 'elementor' ); ?> —</option> 156 <?php endif; ?> 157 158 <?php foreach ( $field['options'] as $value => $label ) : ?> 159 <option value="<?php echo esc_attr( $value ); ?>"<?php esc_attr( selected( $value, $old_value ) ); ?>><?php echo esc_html( $label ); ?></option> 160 <?php endforeach; ?> 161 </select> 162 163 <?php if ( ! empty( $field['desc'] ) ) : ?> 164 <p class="description"><?php echo wp_kses_post( $field['desc'] ); ?></p> 165 <?php 166 endif; 167 } 168 169 /** 170 * Render checkbox list control for CPT. 171 * 172 * Generates the final HTML for checkbox list controls populated with Custom Post Types. 173 * 174 * @since 2.0.0 175 * @access private 176 * @static 177 * 178 * @param array $field Field data. 179 */ 180 private static function checkbox_list_cpt( array $field ) { 181 $defaults = [ 182 'exclude' => [], 183 ]; 184 $field = array_merge( $defaults, $field ); 185 186 $post_types_objects = get_post_types( 187 [ 188 'public' => true, 189 ], 'objects' 190 ); 191 192 /** 193 * Filters the list of post type objects used by Elementor. 194 * 195 * @since 2.8.0 196 * 197 * @param array $post_types_objects List of post type objects used by Elementor. 198 */ 199 $post_types_objects = apply_filters( 'elementor/settings/controls/checkbox_list_cpt/post_type_objects', $post_types_objects ); 200 201 $field['options'] = []; 202 foreach ( $post_types_objects as $cpt_slug => $post_type ) { 203 if ( in_array( $cpt_slug, $field['exclude'], true ) ) { 204 continue; 205 } 206 207 $field['options'][ $cpt_slug ] = $post_type->labels->name; 208 } 209 210 self::checkbox_list( $field ); 211 } 212 213 /** 214 * Render checkbox list control for user roles. 215 * 216 * Generates the final HTML for checkbox list controls populated with user roles. 217 * 218 * @since 2.0.0 219 * @access private 220 * @static 221 * 222 * @param array $field Field data. 223 */ 224 private static function checkbox_list_roles( array $field ) { 225 $defaults = [ 226 'exclude' => [], 227 ]; 228 $field = array_merge( $defaults, $field ); 229 230 $field['options'] = []; 231 $roles = get_editable_roles(); 232 233 if ( is_multisite() ) { 234 $roles = [ 235 'super_admin' => [ 236 'name' => esc_html__( 'Super Admin', 'elementor' ), 237 ], 238 ] + $roles; 239 } 240 241 foreach ( $roles as $role_slug => $role_data ) { 242 if ( in_array( $role_slug, $field['exclude'] ) ) { 243 continue; 244 } 245 246 $field['options'][ $role_slug ] = $role_data['name']; 247 } 248 249 self::checkbox_list( $field ); 250 } 251 252 /** 253 * Render raw HTML control. 254 * 255 * Generates the final HTML for raw HTML controls. 256 * 257 * @since 2.0.0 258 * @access private 259 * @static 260 * 261 * @param array $field Field data. 262 */ 263 private static function raw_html( array $field ) { 264 if ( empty( $field['html'] ) ) { 265 return; 266 } 267 ?> 268 <div id="<?php echo esc_attr( $field['id'] ); ?>"> 269 270 <?php // PHPCS - This is a Raw HTML control, it is not escaped on purpose. ?> 271 <div><?php echo $field['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div> 272 <?php 273 if ( ! empty( $field['sub_desc'] ) ) : 274 echo wp_kses_post( $field['sub_desc'] ); 275 endif; 276 ?> 277 <?php if ( ! empty( $field['desc'] ) ) : ?> 278 <p class="description"><?php echo wp_kses_post( $field['desc'] ); ?></p> 279 <?php endif; ?> 280 </div> 281 <?php 282 } 283 }