| @@ -23,23 +23,8 @@ | ||
| 23 | 23 | * @return array Blocks to Register |
| 24 | 24 | */ |
| 25 | 25 | public function register( $blocks ) { |
| 26 | 26 | |
| 27 | - // If the request is for the frontend, return the minimum block definition required | |
| 28 | - // to register and render the block on the frontend site using register_block_type(). | |
| 29 | - if ( ! $this->is_admin_frontend_editor_or_admin_rest_request() ) { | |
| 30 | - $blocks[ $this->get_name() ] = array( | |
| 31 | - 'title' => $this->get_title(), | |
| 32 | - 'icon' => $this->get_icon(), | |
| 33 | - 'attributes' => $this->get_attributes(), | |
| 34 | - 'render_callback' => array( $this, 'render' ), | |
| 35 | - ); | |
| 36 | - | |
| 37 | - return $blocks; | |
| 38 | - } | |
| 39 | - | |
| 40 | - // Request is for the WordPress Administration, frontend editor or REST API request. | |
| 41 | - // Register the full block definition, including fields, panels, default values and supports. | |
| 42 | 27 | $blocks[ $this->get_name() ] = array_merge( |
| 43 | 28 | $this->get_overview(), |
| 44 | 29 | array( |
| 45 | 30 | 'name' => $this->get_name(), |
| @@ -72,30 +57,8 @@ | ||
| 72 | 57 | |
| 73 | 58 | } |
| 74 | 59 | |
| 75 | 60 | /** |
| 76 | - * Returns this block's title. | |
| 77 | - * | |
| 78 | - * @since 3.1.1 | |
| 79 | - */ | |
| 80 | - public function get_title() { | |
| 81 | - | |
| 82 | - return ''; | |
| 83 | - | |
| 84 | - } | |
| 85 | - | |
| 86 | - /** | |
| 87 | - * Returns this block's icon. | |
| 88 | - * | |
| 89 | - * @since 3.1.1 | |
| 90 | - */ | |
| 91 | - public function get_icon() { | |
| 92 | - | |
| 93 | - return ''; | |
| 94 | - | |
| 95 | - } | |
| 96 | - | |
| 97 | - /** | |
| 98 | 61 | * Returns this block's Title, Icon, Categories, Keywords and properties. |
| 99 | 62 | * |
| 100 | 63 | * @since 1.9.6 |
| 101 | 64 | * |
| @@ -256,140 +219,101 @@ | ||
| 256 | 219 | break; |
| 257 | 220 | } |
| 258 | 221 | } |
| 259 | 222 | |
| 260 | - // Remove some unused attributes, now they're declared above. | |
| 261 | - unset( $atts['style'], $atts['backgroundColor'], $atts['textColor'], $atts['className'] ); | |
| 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(); | |
| 262 | 226 | |
| 263 | - return $atts; | |
| 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 | + } | |
| 264 | 233 | |
| 265 | - } | |
| 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 | + } | |
| 266 | 240 | |
| 267 | - /** | |
| 268 | - * Removes any HTML that might be wrongly included in the shorcode attribute's values | |
| 269 | - * due to e.g. copy and pasting from Documentation or other examples. | |
| 270 | - * | |
| 271 | - * @since 1.9.6 | |
| 272 | - * | |
| 273 | - * @param array $atts Block or shortcode attributes. | |
| 274 | - * @return array | |
| 275 | - */ | |
| 276 | - public function sanitize_atts( $atts ) { | |
| 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 | + } | |
| 277 | 247 | |
| 278 | - foreach ( $atts as $key => $value ) { | |
| 279 | - if ( is_array( $value ) ) { | |
| 280 | - continue; | |
| 281 | - } | |
| 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 | + } | |
| 282 | 254 | |
| 283 | - $atts[ $key ] = wp_strip_all_tags( $value ); | |
| 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']; | |
| 284 | 260 | } |
| 285 | 261 | |
| 286 | - return $atts; | |
| 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 | + } | |
| 287 | 268 | |
| 288 | - } | |
| 289 | - | |
| 290 | - /** | |
| 291 | - * Builds CSS class(es) that might need to be added to the top level element's `class` attribute | |
| 292 | - * when using Gutenberg, to honor the block's styles and layout settings. | |
| 293 | - * | |
| 294 | - * @since 2.8.3 | |
| 295 | - * | |
| 296 | - * @param array $additional_classes Additional classes to add to the block. | |
| 297 | - * @return array | |
| 298 | - */ | |
| 299 | - public function get_css_classes( $additional_classes = array() ) { | |
| 300 | - | |
| 301 | - // To avoid errors in get_block_wrapper_attributes() in non-block themes using the shortcode, | |
| 302 | - // tell WordPress that a block is being rendered. | |
| 303 | - // The attributes don't matter, as we send them to the render() function. | |
| 304 | - if ( class_exists( 'WP_Block_Supports' ) && is_null( WP_Block_Supports::$block_to_render ) ) { // @phpstan-ignore-line | |
| 305 | - WP_Block_Supports::$block_to_render = array( | |
| 306 | - 'blockName' => 'convertkit/' . $this->get_name(), | |
| 307 | - 'attrs' => array(), | |
| 308 | - 'innerBlocks' => array(), | |
| 309 | - 'innerHTML' => '', | |
| 310 | - 'innerContent' => array(), | |
| 311 | - ); | |
| 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 | + } | |
| 312 | 275 | } |
| 313 | 276 | |
| 314 | - // Get the block wrapper attributes string. | |
| 315 | - $wrapper_attributes = get_block_wrapper_attributes( | |
| 316 | - array( | |
| 317 | - 'class' => implode( | |
| 318 | - ' ', | |
| 319 | - array_merge( | |
| 320 | - array( | |
| 321 | - 'convertkit-' . $this->get_name(), | |
| 322 | - ), | |
| 323 | - $additional_classes | |
| 324 | - ) | |
| 325 | - ), | |
| 326 | - ) | |
| 327 | - ); | |
| 328 | - | |
| 329 | - // Extract the class attribute from the wrapper attributes string, returning as an array. | |
| 330 | - // Extract just the class attribute value from the wrapper attributes string. | |
| 331 | - $classes = array(); | |
| 332 | - if ( preg_match( '/class="([^"]*)"/', $wrapper_attributes, $matches ) ) { | |
| 333 | - $classes = explode( ' ', $matches[1] ); | |
| 334 | - } else { | |
| 335 | - $classes = array( | |
| 336 | - 'convertkit-' . $this->get_name(), | |
| 337 | - ); | |
| 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']; | |
| 338 | 282 | } |
| 339 | 283 | |
| 340 | - // Remove some classes WordPress adds that we don't want, as they break the layout. | |
| 341 | - $classes = array_diff( $classes, array( 'alignfull', 'wp-block-post-content' ) ); | |
| 284 | + // Remove some unused attributes, now they're declared above. | |
| 285 | + unset( $atts['style'] ); | |
| 342 | 286 | |
| 343 | - return $classes; | |
| 287 | + return $atts; | |
| 344 | 288 | |
| 345 | 289 | } |
| 346 | 290 | |
| 347 | 291 | /** |
| 348 | - * Builds inline CSS style(s) that might need to be added to the top level element's `style` attribute | |
| 349 | - * when using Gutenberg, a shortcode or third party page builder module / widget. | |
| 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. | |
| 350 | 294 | * |
| 351 | - * @since 2.8.3 | |
| 295 | + * @since 1.9.6 | |
| 352 | 296 | * |
| 353 | 297 | * @param array $atts Block or shortcode attributes. |
| 354 | - * @return array | |
| 298 | + * @return array Block or shortcode attributes | |
| 355 | 299 | */ |
| 356 | - public function get_css_styles( $atts ) { | |
| 300 | + public function sanitize_atts( $atts ) { | |
| 357 | 301 | |
| 358 | - // To avoid errors in get_block_wrapper_attributes() in non-block themes using the shortcode, | |
| 359 | - // tell WordPress that a block is being rendered. | |
| 360 | - // The attributes don't matter, as we send them to the render() function. | |
| 361 | - if ( class_exists( 'WP_Block_Supports' ) && is_null( WP_Block_Supports::$block_to_render ) ) { // @phpstan-ignore-line | |
| 362 | - WP_Block_Supports::$block_to_render = array( | |
| 363 | - 'blockName' => 'convertkit/' . $this->get_name(), | |
| 364 | - 'attrs' => array(), | |
| 365 | - 'innerBlocks' => array(), | |
| 366 | - 'innerHTML' => '', | |
| 367 | - 'innerContent' => array(), | |
| 368 | - ); | |
| 302 | + if ( ! is_array( $atts ) ) { | |
| 303 | + return $atts; | |
| 369 | 304 | } |
| 370 | 305 | |
| 371 | - $styles = array(); | |
| 306 | + foreach ( $atts as $key => $value ) { | |
| 307 | + if ( is_array( $value ) ) { | |
| 308 | + continue; | |
| 309 | + } | |
| 372 | 310 | |
| 373 | - // Get the block wrapper attributes string, extracting any styles that the block has set, | |
| 374 | - // such as margin, padding or block spacing. | |
| 375 | - $wrapper_attributes = get_block_wrapper_attributes(); | |
| 376 | - if ( preg_match( '/style="([^"]*)"/', $wrapper_attributes, $matches ) ) { | |
| 377 | - return array_filter( explode( ';', $matches[1] ) ); | |
| 311 | + $atts[ $key ] = wp_strip_all_tags( $value ); | |
| 378 | 312 | } |
| 379 | 313 | |
| 380 | - // If here, no block styles were found. | |
| 381 | - // This might be a shortcode or third party page builder module / widget that has | |
| 382 | - // specific attributes set. | |
| 383 | - if ( isset( $atts['text_color'] ) && ! empty( $atts['text_color'] ) ) { | |
| 384 | - $styles[] = 'color:' . $atts['text_color']; | |
| 385 | - } | |
| 386 | - if ( isset( $atts['background_color'] ) && ! empty( $atts['background_color'] ) ) { | |
| 387 | - $styles[] = 'background-color:' . $atts['background_color']; | |
| 388 | - } | |
| 314 | + return $atts; | |
| 389 | 315 | |
| 390 | - return $styles; | |
| 391 | - | |
| 392 | 316 | } |
| 393 | 317 | |
| 394 | 318 | /** |
| 395 | 319 | * Returns the given block / shortcode attributes array as HTML data-* attributes, which can be output |
| @@ -406,8 +330,9 @@ | ||
| 406 | 330 | // styling. |
| 407 | 331 | $skip_keys = array( |
| 408 | 332 | 'backgroundColor', |
| 409 | 333 | 'textColor', |
| 334 | + '_css_classes', | |
| 410 | 335 | '_css_styles', |
| 411 | 336 | ); |
| 412 | 337 | |
| 413 | 338 | // Define a blank string to build the data-* attributes in. |
| @@ -418,13 +343,8 @@ | ||
| 418 | 343 | if ( in_array( $key, $skip_keys, true ) ) { |
| 419 | 344 | continue; |
| 420 | 345 | } |
| 421 | 346 | |
| 422 | - // Skip empty values. | |
| 423 | - if ( empty( $value ) ) { | |
| 424 | - continue; | |
| 425 | - } | |
| 426 | - | |
| 427 | 347 | // Append to data string, replacing underscores with hyphens in the key name. |
| 428 | 348 | $data .= ' data-' . strtolower( str_replace( '_', '-', $key ) ) . '="' . esc_attr( $value ) . '"'; |
| 429 | 349 | } |
| 430 | 350 | |
| @@ -432,35 +352,8 @@ | ||
| 432 | 352 | |
| 433 | 353 | } |
| 434 | 354 | |
| 435 | 355 | /** |
| 436 | - * Determines if the request is a WordPress REST API request | |
| 437 | - * made by a logged in WordPress user who has the capability to edit posts. | |
| 438 | - * | |
| 439 | - * @since 3.1.0 | |
| 440 | - * | |
| 441 | - * @return bool | |
| 442 | - */ | |
| 443 | - public function is_admin_rest_request() { | |
| 444 | - | |
| 445 | - return defined( 'REST_REQUEST' ) && REST_REQUEST && current_user_can( 'edit_posts' ); | |
| 446 | - | |
| 447 | - } | |
| 448 | - | |
| 449 | - /** | |
| 450 | - * Determines if the request is for the WordPress Administration, frontend editor or REST API request. | |
| 451 | - * | |
| 452 | - * @since 3.1.0 | |
| 453 | - * | |
| 454 | - * @return bool | |
| 455 | - */ | |
| 456 | - public function is_admin_frontend_editor_or_admin_rest_request() { | |
| 457 | - | |
| 458 | - return WP_ConvertKit()->is_admin_or_frontend_editor() || $this->is_admin_rest_request(); | |
| 459 | - | |
| 460 | - } | |
| 461 | - | |
| 462 | - /** | |
| 463 | 356 | * Determines if the request for the block is from the block editor or the frontend site. |
| 464 | 357 | * |
| 465 | 358 | * @since 1.9.8.5 |
| 466 | 359 | * |
| @@ -468,66 +361,24 @@ | ||
| 468 | 361 | */ |
| 469 | 362 | public function is_block_editor_request() { |
| 470 | 363 | |
| 471 | 364 | // Return false if not a WordPress REST API request, which Gutenberg uses. |
| 472 | - if ( ! $this->is_admin_rest_request() ) { | |
| 365 | + if ( ! defined( 'REST_REQUEST' ) ) { | |
| 473 | 366 | return false; |
| 474 | 367 | } |
| 368 | + if ( REST_REQUEST !== true ) { | |
| 369 | + return false; | |
| 370 | + } | |
| 475 | 371 | |
| 476 | 372 | // Return false if the context parameter isn't edit. |
| 477 | - if ( ! filter_has_var( INPUT_GET, 'context' ) ) { | |
| 373 | + if ( ! array_key_exists( 'context', $_GET ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 478 | 374 | return false; |
| 479 | 375 | } |
| 480 | - if ( filter_input( INPUT_GET, 'context', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) !== 'edit' ) { | |
| 376 | + if ( sanitize_text_field( $_GET['context'] ) !== 'edit' ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 481 | 377 | return false; |
| 482 | 378 | } |
| 483 | 379 | |
| 484 | 380 | // Request is for the block editor. |
| 485 | - return true; | |
| 486 | - | |
| 487 | - } | |
| 488 | - | |
| 489 | - /** | |
| 490 | - * If the Block Visiblity Plugin is active, run the block through its conditions now. | |
| 491 | - * We don't wait for Block Visibility to do this, as it performs this on the | |
| 492 | - * `render_block` filter, by which time the code in this method has fully executed, | |
| 493 | - * meaning any non-inline Forms will have had their scripts added to the | |
| 494 | - * `convertkit_output_scripts_footer` hook. | |
| 495 | - * As a result, the non-inline Form will always display, regardless of whether | |
| 496 | - * Block Visibility's conditions are met. | |
| 497 | - * We deliberately don't output non-inline Forms in their block, instead deferring | |
| 498 | - * to the `convertkit_output_scripts_footer` hook, to ensure the non-inline Forms | |
| 499 | - * styling are not constrained by the Theme's width, layout or other properties. | |
| 500 | - * | |
| 501 | - * @since 2.6.6 | |
| 502 | - * | |
| 503 | - * @param array $atts Block Attributes. | |
| 504 | - * @return bool Display Block | |
| 505 | - */ | |
| 506 | - public function is_block_visible( $atts ) { | |
| 507 | - | |
| 508 | - // Display the block if the Block Visibility Plugin isn't active. | |
| 509 | - if ( ! function_exists( '\BlockVisibility\Frontend\render_with_visibility' ) ) { | |
| 510 | - return true; | |
| 511 | - } | |
| 512 | - | |
| 513 | - // Determine whether the block should display. | |
| 514 | - $display_block = \BlockVisibility\Frontend\render_with_visibility( | |
| 515 | - 'block', | |
| 516 | - array( | |
| 517 | - 'blockName' => 'convertkit-' . $this->get_name(), | |
| 518 | - 'attrs' => $atts, | |
| 519 | - ) | |
| 520 | - ); | |
| 521 | - | |
| 522 | - // If the content returned is a blank string, conditions on this block set | |
| 523 | - // by the user in the Block Visibility Plugin resulted in the block not displaying. | |
| 524 | - // Don't display it. | |
| 525 | - if ( empty( $display_block ) ) { | |
| 526 | - return false; | |
| 527 | - } | |
| 528 | - | |
| 529 | - // If here, the block can be displayed. | |
| 530 | 381 | return true; |
| 531 | 382 | |
| 532 | 383 | } |
| 533 | 384 | |