PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.0
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.0
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
automatorwp / libraries / automatorwp-select-filter.php

automatorwp-select-filter.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.0, at libraries/automatorwp-select-filter.php

109 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CMB2 Select Multiple Field Type
4 */
5
6 if( ! function_exists( 'cmb2_render_automatorwp_select_filter_field_type' ) ) {
7
8 /**
9 * Adds a custom field type for select multiples.
10 * @param object $field The CMB2_Field type object.
11 * @param string $escaped_value The saved (and escaped) value.
12 * @param int $object_id The current post ID.
13 * @param string $object_type The current object type.
14 * @param CMB2_Types $field_type_object The CMB2_Types object.
15 * @return void
16 */
17 function cmb2_render_automatorwp_select_filter_field_type( $field, $escaped_value, $object_id, $object_type, $field_type_object ) {
18
19 // Parse args
20 $attrs = $field_type_object->parse_args( 'select', array(
21 'class' => 'cmb2_select',
22 'name' => $field_type_object->_name(),
23 'id' => $field_type_object->_id(),
24 ) );
25
26 // Support for multiple
27 if( isset( $field->args['attributes']['multiple'] ) && $field->args['attributes']['multiple'] ) {
28 $attrs['name'] = $attrs['name'] . '[]';
29 $attrs['multiple'] = 'true';
30 }
31
32 // Parse options
33 $options = '';
34
35 foreach ( $field->options() as $value => $option ) {
36
37 if( is_array( $option ) ) {
38
39 // Options group
40
41 $integration = automatorwp_get_integration( $value );
42
43 if( $integration ) {
44 $options .= '<optgroup label="'. $integration['label'] .'" data-icon="'. $integration['icon'] .'">';
45 } else {
46 $options .= '<optgroup label="'. $value .'">';
47 }
48
49 foreach ( $option as $key => $label ) {
50 $selected = ( is_array( $escaped_value ) && in_array( $key, $escaped_value ) ) || $escaped_value === $key;
51
52 $options .= sprintf( '<option value="%s"%s>%s</option>',
53 $key,
54 $selected ? 'selected="selected"' : '',
55 $label
56 );
57 }
58
59 $options .= '</optgroup>';
60
61 } else {
62
63 $selected = ( is_array( $escaped_value ) && in_array( $value, $escaped_value ) ) || $escaped_value === $value;
64
65 // Single option
66 $options .= sprintf( '<option value="%s"%s>%s</option>',
67 $value,
68 $selected ? 'selected="selected"' : '',
69 $option
70 );
71
72 }
73 }
74
75 echo sprintf( '<select%s>%s</select>%s',
76 $field_type_object->concat_attrs( $attrs ),
77 $options,
78 $field_type_object->_desc( true )
79 );
80
81 }
82 add_action( 'cmb2_render_automatorwp_select_filter', 'cmb2_render_automatorwp_select_filter_field_type', 10, 5 );
83
84
85 /**
86 * Sanitize the selected value.
87 */
88 function cmb2_sanitize_automatorwp_select_filter_callback( $override_value, $value ) {
89
90 // Sanitize multiple value
91 if ( is_array( $value ) ) {
92
93 foreach ( $value as $key => $saved_value ) {
94
95 $value[$key] = sanitize_text_field( $saved_value );
96
97 }
98
99 return $value;
100
101 }
102
103 return sanitize_text_field( $value );
104
105 }
106 add_filter( 'cmb2_sanitize_automatorwp_select_filter', 'cmb2_sanitize_automatorwp_select_filter_callback', 10, 2 );
107
108 }
109