| 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 |
case 'string': |
| 213 |
// If the attribute's value is empty, check if the default attribute has a value. |
| 214 |
// If so, apply it now. |
| 215 |
// shortcode_atts() will only do this if the attribute key isn't specified. |
| 216 |
if ( empty( $value ) && ! empty( $this->get_default_value( $att ) ) ) { |
| 217 |
$atts[ $att ] = $this->get_default_value( $att ); |
| 218 |
} |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
// Build CSS class(es) that might need to be added to the top level element for this block. |
| 224 |
$atts['_css_classes'] = array( 'convertkit-' . $this->get_name() ); |
| 225 |
$atts['_css_styles'] = array(); |
| 226 |
|
| 227 |
// If the block supports a text color, and a preset color was selected, add it to the |
| 228 |
// array of CSS classes. |
| 229 |
if ( $atts['textColor'] ) { |
| 230 |
$atts['_css_classes'][] = 'has-text-color'; |
| 231 |
$atts['_css_classes'][] = 'has-' . $atts['textColor'] . '-color'; |
| 232 |
} |
| 233 |
|
| 234 |
// If the block supports a text color, and a custom hex color was selected, add it to the |
| 235 |
// array of CSS inline styles. |
| 236 |
if ( isset( $atts['style']['color'] ) && isset( $atts['style']['color']['text'] ) ) { |
| 237 |
$atts['_css_classes'][] = 'has-text-color'; |
| 238 |
$atts['_css_styles']['color'] = 'color:' . $atts['style']['color']['text']; |
| 239 |
} |
| 240 |
|
| 241 |
// If the shortcode supports a text color, and a custom hex color was selected, add it to the |
| 242 |
// array of CSS inline styles. |
| 243 |
if ( isset( $atts['text_color'] ) && ! empty( $atts['text_color'] ) ) { |
| 244 |
$atts['_css_classes'][] = 'has-text-color'; |
| 245 |
$atts['_css_styles']['color'] = 'color:' . $atts['text_color']; |
| 246 |
} |
| 247 |
|
| 248 |
// If the block supports a background color, and a preset color was selected, add it to the |
| 249 |
// array of CSS classes. |
| 250 |
if ( $atts['backgroundColor'] ) { |
| 251 |
$atts['_css_classes'][] = 'has-background'; |
| 252 |
$atts['_css_classes'][] = 'has-' . $atts['backgroundColor'] . '-background-color'; |
| 253 |
} |
| 254 |
|
| 255 |
// If the block supports a background color, and a custom hex color was selected, add it to the |
| 256 |
// array of CSS inline styles. |
| 257 |
if ( isset( $atts['style']['color'] ) && isset( $atts['style']['color']['background'] ) ) { |
| 258 |
$atts['_css_classes'][] = 'has-background'; |
| 259 |
$atts['_css_styles']['background'] = 'background-color:' . $atts['style']['color']['background']; |
| 260 |
} |
| 261 |
|
| 262 |
// If the block supports a font size, and a preset font size was selected, add it to the |
| 263 |
// array of CSS classes. |
| 264 |
if ( isset( $atts['fontSize'] ) && ! empty( $atts['fontSize'] ) ) { |
| 265 |
$atts['_css_classes'][] = 'has-custom-font-size'; |
| 266 |
$atts['_css_classes'][] = 'has-' . $atts['fontSize'] . '-font-size'; |
| 267 |
} |
| 268 |
|
| 269 |
// If the block supports padding, and padding is set, add it to the |
| 270 |
// array of CSS inline styles. |
| 271 |
if ( isset( $atts['style']['spacing'] ) && isset( $atts['style']['spacing']['padding'] ) ) { |
| 272 |
foreach ( $atts['style']['spacing']['padding'] as $position => $value ) { |
| 273 |
$atts['_css_styles'][ 'padding-' . $position ] = 'padding-' . $position . ':' . $value; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
// If the shortcode supports a background color, and a custom hex color was selected, add it to the |
| 278 |
// array of CSS inline styles. |
| 279 |
if ( isset( $atts['background_color'] ) && ! empty( $atts['background_color'] ) ) { |
| 280 |
$atts['_css_classes'][] = 'has-background'; |
| 281 |
$atts['_css_styles']['background'] = 'background-color:' . $atts['background_color']; |
| 282 |
} |
| 283 |
|
| 284 |
// Remove some unused attributes, now they're declared above. |
| 285 |
unset( $atts['style'] ); |
| 286 |
|
| 287 |
return $atts; |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Removes any HTML that might be wrongly included in the shorcode attribute's values |
| 293 |
* due to e.g. copy and pasting from Documentation or other examples. |
| 294 |
* |
| 295 |
* @since 1.9.6 |
| 296 |
* |
| 297 |
* @param array $atts Block or shortcode attributes. |
| 298 |
* @return array Block or shortcode attributes |
| 299 |
*/ |
| 300 |
public function sanitize_atts( $atts ) { |
| 301 |
|
| 302 |
if ( ! is_array( $atts ) ) { |
| 303 |
return $atts; |
| 304 |
} |
| 305 |
|
| 306 |
foreach ( $atts as $key => $value ) { |
| 307 |
if ( is_array( $value ) ) { |
| 308 |
continue; |
| 309 |
} |
| 310 |
|
| 311 |
$atts[ $key ] = wp_strip_all_tags( $value ); |
| 312 |
} |
| 313 |
|
| 314 |
return $atts; |
| 315 |
|
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Returns the given block / shortcode attributes array as HTML data-* attributes, which can be output |
| 320 |
* in a block's container. |
| 321 |
* |
| 322 |
* @since 1.9.7.6 |
| 323 |
* |
| 324 |
* @param array $atts Block or shortcode attributes. |
| 325 |
* @return string Block or shortcode attributes |
| 326 |
*/ |
| 327 |
public function get_atts_as_html_data_attributes( $atts ) { |
| 328 |
|
| 329 |
// Define attributes provided by Gutenberg, which will be skipped, such as |
| 330 |
// styling. |
| 331 |
$skip_keys = array( |
| 332 |
'backgroundColor', |
| 333 |
'textColor', |
| 334 |
'_css_classes', |
| 335 |
'_css_styles', |
| 336 |
); |
| 337 |
|
| 338 |
// Define a blank string to build the data-* attributes in. |
| 339 |
$data = ''; |
| 340 |
|
| 341 |
foreach ( $atts as $key => $value ) { |
| 342 |
// Skip built in attributes provided by Gutenberg. |
| 343 |
if ( in_array( $key, $skip_keys, true ) ) { |
| 344 |
continue; |
| 345 |
} |
| 346 |
|
| 347 |
// Append to data string, replacing underscores with hyphens in the key name. |
| 348 |
$data .= ' data-' . strtolower( str_replace( '_', '-', $key ) ) . '="' . esc_attr( $value ) . '"'; |
| 349 |
} |
| 350 |
|
| 351 |
return trim( $data ); |
| 352 |
|
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Determines if the request for the block is from the block editor or the frontend site. |
| 357 |
* |
| 358 |
* @since 1.9.8.5 |
| 359 |
* |
| 360 |
* @return bool |
| 361 |
*/ |
| 362 |
public function is_block_editor_request() { |
| 363 |
|
| 364 |
// Return false if not a WordPress REST API request, which Gutenberg uses. |
| 365 |
if ( ! defined( 'REST_REQUEST' ) ) { |
| 366 |
return false; |
| 367 |
} |
| 368 |
if ( REST_REQUEST !== true ) { |
| 369 |
return false; |
| 370 |
} |
| 371 |
|
| 372 |
// Return false if the context parameter isn't edit. |
| 373 |
if ( filter_input( INPUT_GET, 'context', FILTER_SANITIZE_STRING ) !== 'edit' ) { |
| 374 |
return false; |
| 375 |
} |
| 376 |
|
| 377 |
// Request is for the block editor. |
| 378 |
return true; |
| 379 |
|
| 380 |
} |
| 381 |
|
| 382 |
} |
| 383 |
|