PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.3.3
Block Animations, Motion & Scroll Effects – Ghost Kit v3.3.3
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / gutenberg / blocks / form / fields / select / block.php

block.php in Block Animations, Motion & Scroll Effects – Ghost Kit 3.3.3, at gutenberg/blocks/form/fields/select/block.php

124 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Form Field Select block.
4 *
5 * @package ghostkit
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Class GhostKit_Form_Field_Select_Block
14 */
15 class GhostKit_Form_Field_Select_Block {
16 /**
17 * GhostKit_Form_Field_Select_Block constructor.
18 */
19 public function __construct() {
20 add_action( 'init', array( $this, 'init' ) );
21 }
22
23 /**
24 * Init.
25 */
26 public function init() {
27 register_block_type_from_metadata(
28 dirname( __FILE__ ),
29 array(
30 'render_callback' => array( $this, 'block_render' ),
31 'attributes' => GhostKit_Form_Field_Attributes::get_block_attributes(
32 array(
33 'label' => array(
34 'default' => esc_html__( 'Select', 'ghostkit' ),
35 ),
36 'options' => array(
37 'type' => 'array',
38 'items' => array(
39 'type' => 'object',
40 ),
41 ),
42 'multiple' => array(
43 'type' => 'boolean',
44 ),
45 )
46 ),
47 )
48 );
49 }
50
51 /**
52 * Register gutenberg block output
53 *
54 * @param array $attributes - block attributes.
55 *
56 * @return string
57 */
58 public function block_render( $attributes ) {
59 $attributes = array_merge(
60 array(
61 'options' => array(),
62 'multiple' => false,
63 ),
64 $attributes
65 );
66
67 $options = $attributes['options'];
68 if ( ! is_array( $options ) ) {
69 $options = array();
70 }
71
72 // Add null option.
73 if ( ! $attributes['multiple'] ) {
74 $add_null_option = true;
75
76 foreach ( $options as $data ) {
77 if ( $data['selected'] ) {
78 $add_null_option = false;
79 }
80 }
81
82 if ( $add_null_option ) {
83 array_unshift(
84 $options,
85 array(
86 'label' => esc_attr__( '--- Select ---', 'ghostkit' ),
87 'value' => '',
88 'selected' => true,
89 )
90 );
91 }
92 }
93
94 ob_start();
95
96 $class = 'ghostkit-form-field ghostkit-form-field-select';
97
98 if ( isset( $attributes['className'] ) ) {
99 $class .= ' ' . $attributes['className'];
100 }
101
102 ?>
103
104 <div class="<?php echo esc_attr( $class ); ?>">
105 <?php GhostKit_Form_Field_Label::get( $attributes ); ?>
106
107 <select <?php GhostKit_Form_Field_Attributes::get( $attributes ); ?>>
108 <?php foreach ( $options as $option ) : ?>
109 <option <?php selected( $option['selected'] ); ?> value="<?php echo esc_attr( $option['value'] ); ?>">
110 <?php echo esc_html( $option['label'] ); ?>
111 </option>
112 <?php endforeach; ?>
113 </select>
114
115 <?php GhostKit_Form_Field_Description::get( $attributes ); ?>
116 </div>
117
118 <?php
119
120 return ob_get_clean();
121 }
122 }
123 new GhostKit_Form_Field_Select_Block();
124