PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.0.1
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.0.1
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 2.3.2 2.3.3 All 194 releases
convertkit / includes / blocks / class-convertkit-block.php

class-convertkit-block.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.0.1, at includes/blocks/class-convertkit-block.php

374 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Block class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * ConvertKit Block definition for Gutenberg and Shortcode.
11 *
12 * @package ConvertKit
13 * @author ConvertKit
14 */
15 class ConvertKit_Block {
16
17 /**
18 * Registers this block with the ConvertKit Plugin.
19 *
20 * @since 1.9.6
21 *
22 * @param array $blocks Blocks to Register.
23 * @return array Blocks to Register
24 */
25 public function register( $blocks ) {
26
27 $blocks[ $this->get_name() ] = array_merge(
28 $this->get_overview(),
29 array(
30 'name' => $this->get_name(),
31 'fields' => $this->get_fields(),
32 'attributes' => $this->get_attributes(),
33 'supports' => $this->get_supports(),
34 'panels' => $this->get_panels(),
35 'default_values' => $this->get_default_values(),
36 )
37 );
38
39 return $blocks;
40
41 }
42
43 /**
44 * Returns this block's programmatic name, excluding the convertkit- prefix.
45 *
46 * @since 1.9.6
47 */
48 public function get_name() {
49
50 /**
51 * This will register as:
52 * - a shortcode, with the name [convertkit_form].
53 * - a shortcode, with the name [convertkit], for backward compat.
54 * - a Gutenberg block, with the name convertkit/form.
55 */
56 return '';
57
58 }
59
60 /**
61 * Returns this block's Title, Icon, Categories, Keywords and properties.
62 *
63 * @since 1.9.6
64 *
65 * @return array
66 */
67 public function get_overview() {
68
69 return array();
70
71 }
72
73 /**
74 * Returns this block's Attributes
75 *
76 * @since 1.9.6.5
77 *
78 * @return array
79 */
80 public function get_attributes() {
81
82 return array();
83
84 }
85
86 /**
87 * Gutenberg: Returns supported built in attributes, such as
88 * className, color etc.
89 *
90 * @since 1.9.7.4
91 *
92 * @return array Supports
93 */
94 public function get_supports() {
95
96 return array(
97 'className' => true,
98 );
99
100 }
101
102 /**
103 * Returns this block's Fields
104 *
105 * @since 1.9.6
106 *
107 * @return array
108 */
109 public function get_fields() {
110
111 return array();
112
113 }
114
115 /**
116 * Returns this block's UI panels / sections.
117 *
118 * @since 1.9.6
119 *
120 * @return array
121 */
122 public function get_panels() {
123
124 return array();
125
126 }
127
128 /**
129 * Returns this block's Default Values
130 *
131 * @since 1.9.6
132 *
133 * @return array
134 */
135 public function get_default_values() {
136
137 return array();
138
139 }
140
141 /**
142 * Returns the given block's field's Default Value
143 *
144 * @since 1.9.6
145 *
146 * @param string $field Field Name.
147 * @return string
148 */
149 public function get_default_value( $field ) {
150
151 $defaults = $this->get_default_values();
152 if ( isset( $defaults[ $field ] ) ) {
153 return $defaults[ $field ];
154 }
155
156 return '';
157
158 }
159
160 /**
161 * Performs several transformation on a block's attributes, including:
162 * - sanitization
163 * - adding attributes with default values are missing but registered by the block
164 * - cast attribute values based on their defined type
165 *
166 * These steps are performed because the attributes may be defined by a shortcode,
167 * block or third party widget/page builder's block, each of which handle attributes
168 * slightly differently.
169 *
170 * Returns a standardised attributes array.
171 *
172 * @since 1.9.7.4
173 *
174 * @param array $atts Declared attributes.
175 * @return array All attributes, standardised.
176 */
177 public function sanitize_and_declare_atts( $atts ) {
178
179 // Sanitize attributes, merging with default values so that the array
180 // of attributes contains all expected keys for this block.
181 $atts = shortcode_atts(
182 $this->get_default_values(),
183 $this->sanitize_atts( $atts ),
184 $this->get_name()
185 );
186
187 // Fetch attribute definitions.
188 $atts_definitions = $this->get_attributes();
189
190 // Iterate through attributes, casting them based on their attribute definition.
191 foreach ( $atts as $att => $value ) {
192 // Skip if no definition exists for this attribute.
193 if ( ! array_key_exists( $att, $atts_definitions ) ) {
194 continue;
195 }
196
197 // Skip if no type exists for this attribute.
198 if ( ! array_key_exists( 'type', $atts_definitions[ $att ] ) ) {
199 continue;
200 }
201
202 // Cast, depending on the attribute type.
203 switch ( $atts_definitions[ $att ]['type'] ) {
204 case 'number':
205 $atts[ $att ] = (int) $value;
206 break;
207
208 case 'boolean':
209 $atts[ $att ] = (bool) $value;
210 break;
211 }
212 }
213
214 // Build CSS class(es) that might need to be added to the top level element for this block.
215 $atts['_css_classes'] = array( 'convertkit-' . $this->get_name() );
216 $atts['_css_styles'] = array();
217
218 // If the block supports a text color, and a preset color was selected, add it to the
219 // array of CSS classes.
220 if ( $atts['textColor'] ) {
221 $atts['_css_classes'][] = 'has-text-color';
222 $atts['_css_classes'][] = 'has-' . $atts['textColor'] . '-color';
223 }
224
225 // If the block supports a text color, and a custom hex color was selected, add it to the
226 // array of CSS inline styles.
227 if ( isset( $atts['style']['color'] ) && isset( $atts['style']['color']['text'] ) ) {
228 $atts['_css_classes'][] = 'has-text-color';
229 $atts['_css_styles']['color'] = 'color:' . $atts['style']['color']['text'];
230 }
231
232 // If the shortcode supports a text color, and a custom hex color was selected, add it to the
233 // array of CSS inline styles.
234 if ( isset( $atts['text_color'] ) && ! empty( $atts['text_color'] ) ) {
235 $atts['_css_classes'][] = 'has-text-color';
236 $atts['_css_styles']['color'] = 'color:' . $atts['text_color'];
237 }
238
239 // If the block supports a background color, and a preset color was selected, add it to the
240 // array of CSS classes.
241 if ( $atts['backgroundColor'] ) {
242 $atts['_css_classes'][] = 'has-background';
243 $atts['_css_classes'][] = 'has-' . $atts['backgroundColor'] . '-background-color';
244 }
245
246 // If the block supports a background color, and a custom hex color was selected, add it to the
247 // array of CSS inline styles.
248 if ( isset( $atts['style']['color'] ) && isset( $atts['style']['color']['background'] ) ) {
249 $atts['_css_classes'][] = 'has-background';
250 $atts['_css_styles']['background'] = 'background-color:' . $atts['style']['color']['background'];
251 }
252
253 // If the block supports a font size, and a preset font size was selected, add it to the
254 // array of CSS classes.
255 if ( isset( $atts['fontSize'] ) && ! empty( $atts['fontSize'] ) ) {
256 $atts['_css_classes'][] = 'has-custom-font-size';
257 $atts['_css_classes'][] = 'has-' . $atts['fontSize'] . '-font-size';
258 }
259
260 // If the block supports padding, and padding is set, add it to the
261 // array of CSS inline styles.
262 if ( isset( $atts['style']['spacing'] ) && isset( $atts['style']['spacing']['padding'] ) ) {
263 foreach ( $atts['style']['spacing']['padding'] as $position => $value ) {
264 $atts['_css_styles'][ 'padding-' . $position ] = 'padding-' . $position . ':' . $value;
265 }
266 }
267
268 // If the shortcode supports a background color, and a custom hex color was selected, add it to the
269 // array of CSS inline styles.
270 if ( isset( $atts['background_color'] ) && ! empty( $atts['background_color'] ) ) {
271 $atts['_css_classes'][] = 'has-background';
272 $atts['_css_styles']['background'] = 'background-color:' . $atts['background_color'];
273 }
274
275 // Remove some unused attributes, now they're declared above.
276 unset( $atts['style'] );
277
278 return $atts;
279
280 }
281
282 /**
283 * Removes any HTML that might be wrongly included in the shorcode attribute's values
284 * due to e.g. copy and pasting from Documentation or other examples.
285 *
286 * @since 1.9.6
287 *
288 * @param array $atts Block or shortcode attributes.
289 * @return array Block or shortcode attributes
290 */
291 public function sanitize_atts( $atts ) {
292
293 if ( ! is_array( $atts ) ) {
294 return $atts;
295 }
296
297 foreach ( $atts as $key => $value ) {
298 if ( is_array( $value ) ) {
299 continue;
300 }
301
302 $atts[ $key ] = wp_strip_all_tags( $value );
303 }
304
305 return $atts;
306
307 }
308
309 /**
310 * Returns the given block / shortcode attributes array as HTML data-* attributes, which can be output
311 * in a block's container.
312 *
313 * @since 1.9.7.6
314 *
315 * @param array $atts Block or shortcode attributes.
316 * @return string Block or shortcode attributes
317 */
318 public function get_atts_as_html_data_attributes( $atts ) {
319
320 // Define attributes provided by Gutenberg, which will be skipped, such as
321 // styling.
322 $skip_keys = array(
323 'backgroundColor',
324 'textColor',
325 '_css_classes',
326 '_css_styles',
327 );
328
329 // Define a blank string to build the data-* attributes in.
330 $data = '';
331
332 foreach ( $atts as $key => $value ) {
333 // Skip built in attributes provided by Gutenberg.
334 if ( in_array( $key, $skip_keys, true ) ) {
335 continue;
336 }
337
338 // Append to data string, replacing underscores with hyphens in the key name.
339 $data .= ' data-' . strtolower( str_replace( '_', '-', $key ) ) . '="' . esc_attr( $value ) . '"';
340 }
341
342 return trim( $data );
343
344 }
345
346 /**
347 * Determines if the request for the block is from the block editor or the frontend site.
348 *
349 * @since 1.9.8.5
350 *
351 * @return bool
352 */
353 public function is_block_editor_request() {
354
355 // Return false if not a WordPress REST API request, which Gutenberg uses.
356 if ( ! defined( 'REST_REQUEST' ) ) {
357 return false;
358 }
359 if ( REST_REQUEST !== true ) {
360 return false;
361 }
362
363 // Return false if the context parameter isn't edit.
364 if ( filter_input( INPUT_GET, 'context', FILTER_SANITIZE_STRING ) !== 'edit' ) {
365 return false;
366 }
367
368 // Request is for the block editor.
369 return true;
370
371 }
372
373 }
374