$control The description. * @param callable|null $options callable( array $control ): ?array, a select2's list. * @param array $strings The factory's words (`ids`). * @return array */ public static function args( array $control, ?callable $options = null, array $strings = array() ): array { $args = array( 'label' => (string) $control['label'], 'default' => $control['default'] ?? '', ); if ( isset( $control['help'] ) ) { $args['description'] = (string) $control['help']; } switch ( $control['type'] ) { case 'switcher': $args['type'] = \Elementor\Controls_Manager::SWITCHER; $args['return_value'] = 'yes'; break; case 'number': $args['type'] = \Elementor\Controls_Manager::NUMBER; if ( isset( $control['min'] ) ) { $args['min'] = (int) $control['min']; } if ( isset( $control['max'] ) ) { $args['max'] = (int) $control['max']; } break; case 'select': $args['type'] = \Elementor\Controls_Manager::SELECT; $args['options'] = (array) $control['options']; break; case 'color': $args['type'] = \Elementor\Controls_Manager::COLOR; $args['alpha'] = false; break; case 'select2': $list = isset( $control['options'] ) ? (array) $control['options'] : ( null !== $options ? call_user_func( $options, $control ) : array() ); if ( null === $list ) { if ( ! empty( $control['multiple'] ) ) { $args['type'] = \Elementor\Controls_Manager::TEXT; $args['default'] = ''; $args['label_block'] = true; $args['description'] = (string) ( $strings['ids'] ?? '' ); } else { $args['type'] = \Elementor\Controls_Manager::NUMBER; $args['min'] = 0; } break; } $args['type'] = \Elementor\Controls_Manager::SELECT2; $args['options'] = (array) $list; $args['multiple'] = ! empty( $control['multiple'] ); $args['label_block'] = true; break; case 'repeater': // One row per object; its fields are described like controls (Elementor's repeater takes their arguments // with a name each). The row title shows the first field's value. $fields = array(); foreach ( (array) ( $control['fields'] ?? array() ) as $field ) { $fields[] = array( 'name' => (string) $field['key'] ) + self::args( $field, $options, $strings ); } $args['type'] = \Elementor\Controls_Manager::REPEATER; $args['fields'] = $fields; $args['label_block'] = true; if ( array() !== $fields ) { $args['title_field'] = '{{{ ' . $fields[0]['name'] . ' }}}'; } break; case 'textarea': $args['type'] = \Elementor\Controls_Manager::TEXTAREA; break; default: $args['type'] = \Elementor\Controls_Manager::TEXT; $args['label_block'] = true; } if ( isset( $control['condition'] ) && is_array( $control['condition'] ) ) { $args['condition'] = $control['condition']; } return $args; } /** * Adds controls in their sections (ControlMap::sections()) to a widget's Content tab: the first section always (with * a note when it holds nothing), the others when they hold a control. Section ids are "_
", the note's * "_note". * * @param \Elementor\Widget_Base $widget The widget. * @param array>}> $sections The sections. * @param string $id The ids' prefix. * @param callable|null $options A select2's list. * @param array $strings The factory's words (`note`, `ids`). */ public static function add( \Elementor\Widget_Base $widget, array $sections, string $id, ?callable $options = null, array $strings = array() ): void { $first = true; foreach ( $sections as $key => $section ) { if ( ! $first && array() === $section['controls'] ) { continue; } $widget->start_controls_section( $id . '_' . $key, array( 'label' => $section['label'], 'tab' => \Elementor\Controls_Manager::TAB_CONTENT, ) ); if ( $first && array() === $section['controls'] ) { $widget->add_control( $id . '_note', array( 'type' => \Elementor\Controls_Manager::RAW_HTML, 'raw' => esc_html( (string) ( $strings['note'] ?? '' ) ), ) ); } foreach ( $section['controls'] as $control ) { $widget->add_control( (string) $control['key'], self::args( $control, $options, $strings ) ); } $widget->end_controls_section(); $first = false; } } /** * A Style-tab section of described style controls. * * @param \Elementor\Widget_Base $widget The widget. * @param string $id The section's id. * @param string $label Its label. * @param list> $controls The descriptions (style()). */ public static function style_section( \Elementor\Widget_Base $widget, string $id, string $label, array $controls ): void { $widget->start_controls_section( $id, array( 'label' => $label, 'tab' => \Elementor\Controls_Manager::TAB_STYLE, ) ); foreach ( $controls as $control ) { self::style( $widget, $control ); } $widget->end_controls_section(); } /** * One described style control: a colour, a size (slider) or four sides (dimensions) write a CSS custom property on * the widget (the block's CSS reads it), a typography or box-shadow group styles a selector inside it. Elementor * writes the CSS, its globals and breakpoints included. * * @param \Elementor\Widget_Base $widget The widget. * @param array $control key, type (color|slider|dimensions|typography|box-shadow), label, and * property, selector, global, min, max, percent, responsive. */ public static function style( \Elementor\Widget_Base $widget, array $control ): void { $key = (string) $control['key']; switch ( $control['type'] ) { case 'color': $args = array( 'label' => (string) $control['label'], 'type' => \Elementor\Controls_Manager::COLOR, 'selectors' => array( '{{WRAPPER}}' => $control['property'] . ': {{VALUE}};' ), ); if ( ! empty( $control['global'] ) ) { $args['global'] = array( 'default' => (string) $control['global'] ); } $widget->add_control( $key, $args ); return; case 'slider': $args = array( 'label' => (string) $control['label'], 'type' => \Elementor\Controls_Manager::SLIDER, 'size_units' => isset( $control['percent'] ) ? array( 'px', '%' ) : array( 'px' ), 'range' => array( 'px' => array( 'min' => (int) ( $control['min'] ?? 0 ), 'max' => (int) ( $control['max'] ?? 100 ), ), ), 'selectors' => array( '{{WRAPPER}}' => $control['property'] . ': {{SIZE}}{{UNIT}};' ), ); if ( ! empty( $control['responsive'] ) ) { $widget->add_responsive_control( $key, $args ); } else { $widget->add_control( $key, $args ); } return; case 'dimensions': $widget->add_responsive_control( $key, array( 'label' => (string) $control['label'], 'type' => \Elementor\Controls_Manager::DIMENSIONS, 'size_units' => array( 'px' ), 'selectors' => array( '{{WRAPPER}}' => $control['property'] . ': {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ), ) ); return; case 'box-shadow': if ( class_exists( '\Elementor\Group_Control_Box_Shadow' ) ) { $widget->add_group_control( \Elementor\Group_Control_Box_Shadow::get_type(), array( 'name' => $key, 'label' => (string) $control['label'], 'selector' => '{{WRAPPER}} ' . $control['selector'], ) ); } return; case 'typography': if ( class_exists( '\Elementor\Group_Control_Typography' ) ) { $widget->add_group_control( \Elementor\Group_Control_Typography::get_type(), array( 'name' => $key, 'label' => (string) $control['label'], 'selector' => '{{WRAPPER}} ' . $control['selector'], ) ); } return; } } }