PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.0.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.0.3
3.4.3 3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 All 196 releases
convertkit / includes / integrations / divi / class-convertkit-divi-module.php

class-convertkit-divi-module.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.0.3, at includes/integrations/divi/class-convertkit-divi-module.php

217 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Divi Module
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Registers blocks as Divi Modules.
11 *
12 * @package ConvertKit
13 * @author ConvertKit
14 */
15 class ConvertKit_Divi_Module extends ET_Builder_Module {
16
17 /**
18 * How modules are supported in the Visual Builder (off|partial|on)
19 *
20 * @since 2.5.6
21 *
22 * @var string
23 */
24 public $vb_support = 'on';
25
26 /**
27 * The ConvertKit block name.
28 *
29 * @since 2.5.6
30 *
31 * @var string
32 */
33 public $block_name = '';
34
35 /**
36 * The ConvertKit Divi module name.
37 *
38 * @since 2.5.6
39 *
40 * @var string
41 */
42 public $slug = '';
43
44 /**
45 * The full path to the SVG icon for this Divi module.
46 *
47 * @since 2.5.7
48 *
49 * @var string
50 */
51 public $icon_path = '';
52
53 /**
54 * Holds the block definition, properties and fields.
55 *
56 * @since 2.5.6
57 *
58 * @var bool|WP_Error|array
59 */
60 public $block = false;
61
62 /**
63 * Defines the Module name
64 *
65 * @since 2.5.6
66 */
67 public function init() {
68
69 // Get block.
70 $blocks = convertkit_get_blocks();
71
72 // Bail if no blocks are available.
73 if ( ! count( $blocks ) ) {
74 return;
75 }
76
77 // Bail if the block doesn't exist.
78 if ( ! array_key_exists( $this->block_name, $blocks ) ) {
79 return;
80 }
81
82 // Define the block, name and icon.
83 $this->block = $blocks[ $this->block_name ];
84 $this->name = esc_html( $this->block['title'] );
85 $this->icon_path = CONVERTKIT_PLUGIN_PATH . '/' . $this->block['icon'];
86
87 }
88
89 /**
90 * Defines the fields that can be configured for this Module
91 *
92 * @since 2.5.6
93 */
94 public function get_fields() {
95
96 // Bail if no block.
97 if ( is_wp_error( $this->block ) ) {
98 return array();
99 }
100
101 // Bail if no fields.
102 if ( ! is_array( $this->block['fields'] ) ) {
103 return array();
104 }
105
106 // Build fields.
107 $fields = array();
108 foreach ( $this->block['fields'] as $field_name => $field ) {
109 // Start building field definition.
110 $fields[ $field_name ] = array(
111 'type' => $field['type'],
112 'default' => $this->get_default_value( $field ),
113 'description' => ( isset( $field['description'] ) ? $field['description'] : '' ),
114 'label' => $field['label'],
115 'toggle_slug' => 'main_content',
116 );
117
118 // Add/change field parameters depending on the field's type.
119 switch ( $field['type'] ) {
120 /**
121 * Number
122 */
123 case 'number':
124 $fields[ $field_name ] = array_merge(
125 $fields[ $field_name ],
126 array(
127 'type' => 'range',
128 'range_settings' => array(
129 'min' => $field['min'],
130 'max' => $field['max'],
131 'step' => $field['step'],
132 ),
133 'unitless' => true,
134 )
135 );
136 break;
137
138 /**
139 * Select
140 */
141 case 'resource':
142 case 'select':
143 // For select dropdowns, Divi treats the first <option> as the default. If it's selected,
144 // Divi won't pass the underlying value, resulting in no output.
145 // Forcing a 'None' option as the first ensures the user must select an <option>, therefore
146 // ensuring output is correct.
147 $fields[ $field_name ]['type'] = 'select';
148 $fields[ $field_name ]['options'] = array( 0 => __( '(None)', 'convertkit' ) ) + $field['values'];
149 break;
150
151 /**
152 * Toggle
153 */
154 case 'toggle':
155 $fields[ $field_name ] = array_merge(
156 $fields[ $field_name ],
157 array(
158 'type' => 'yes_no_button',
159 'default' => ( $fields[ $field_name ]['default'] ? 'on' : 'off' ),
160 'options' => array(
161 'off' => __( 'No', 'convertkit' ),
162 'on' => __( 'Yes', 'convertkit' ),
163 ),
164 )
165 );
166 break;
167
168 }
169 }
170
171 // Return.
172 return $fields;
173
174 }
175
176 /**
177 * Render the module.
178 *
179 * @since 2.5.6
180 *
181 * @param array|string $unprocessed_props Unprocessed properties.
182 * @param array|string $content Content.
183 * @param string $render_slug Slug.
184 * @return string Block's output.
185 */
186 public function render( $unprocessed_props, $content, $render_slug ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
187
188 // Render using Block class' render() function.
189 // Output is already escaped in render() function.
190 return WP_ConvertKit()->get_class( 'blocks_convertkit_' . $this->block_name )->render( $unprocessed_props );
191
192 }
193
194 /**
195 * Returns the default value for the given field configuration.
196 *
197 * If the field's default value is an array, it's converted to a string,
198 * to prevent Divi builder timeout errors on the frontend.
199 *
200 * @since 2.5.6
201 *
202 * @param array $field Field.
203 * @return string|int|object Default Value
204 */
205 private function get_default_value( $field ) {
206
207 // Return a blank string if the field doesn't specify a default value.
208 if ( ! array_key_exists( 'default_value', $field ) ) {
209 return '';
210 }
211
212 return $field['default_value'];
213
214 }
215
216 }
217