button.php
268 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Button Block. |
| 4 | * |
| 5 | * @since 8.5.0 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\Button; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Jetpack_Gutenberg; |
| 14 | |
| 15 | const FEATURE_NAME = 'button'; |
| 16 | const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; |
| 17 | |
| 18 | /** |
| 19 | * Registers the block for use in Gutenberg |
| 20 | * This is done via an action so that we can disable |
| 21 | * registration if we need to. |
| 22 | */ |
| 23 | function register_block() { |
| 24 | Blocks::jetpack_register_block( |
| 25 | BLOCK_NAME, |
| 26 | array( |
| 27 | 'render_callback' => __NAMESPACE__ . '\render_block', |
| 28 | 'uses_context' => array( 'jetpack/parentBlockWidth' ), |
| 29 | ) |
| 30 | ); |
| 31 | } |
| 32 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 33 | |
| 34 | /** |
| 35 | * Button block render callback. |
| 36 | * |
| 37 | * @param array $attributes Array containing the Button block attributes. |
| 38 | * @param string $content The Button block content. |
| 39 | * |
| 40 | * @return string |
| 41 | */ |
| 42 | function render_block( $attributes, $content ) { |
| 43 | $save_in_post_content = get_attribute( $attributes, 'saveInPostContent' ); |
| 44 | |
| 45 | // The Jetpack Button block depends on the core button block styles. |
| 46 | // The following ensures that those styles are enqueued when rendering this block. |
| 47 | enqueue_existing_button_style_dependency( 'core/button' ); |
| 48 | enqueue_existing_button_style_dependency( 'core/buttons' ); |
| 49 | |
| 50 | Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME ); |
| 51 | |
| 52 | if ( $save_in_post_content || ! class_exists( 'DOMDocument' ) ) { |
| 53 | return $content; |
| 54 | } |
| 55 | |
| 56 | $element = get_attribute( $attributes, 'element' ); |
| 57 | $text = wp_kses_post( get_attribute( $attributes, 'text' ) ); |
| 58 | $unique_id = get_attribute( $attributes, 'uniqueId' ); |
| 59 | $url = get_attribute( $attributes, 'url' ); |
| 60 | $classes = Blocks::classes( FEATURE_NAME, $attributes, array( 'wp-block-button' ) ); |
| 61 | |
| 62 | $button_classes = get_button_classes( $attributes ); |
| 63 | $button_styles = get_button_styles( $attributes ); |
| 64 | $wrapper_styles = get_button_wrapper_styles( $attributes ); |
| 65 | |
| 66 | $wrapper_attributes = sprintf( ' class="%s" style="%s"', esc_attr( $classes ), esc_attr( $wrapper_styles ) ); |
| 67 | $button_attributes = sprintf( ' class="%s" style="%s"', esc_attr( $button_classes ), esc_attr( $button_styles ) ); |
| 68 | |
| 69 | if ( empty( $unique_id ) ) { |
| 70 | $button_attributes .= ' data-id-attr="placeholder"'; |
| 71 | } else { |
| 72 | $button_attributes .= sprintf( ' data-id-attr="%1$s" id="%1$s"', esc_attr( $unique_id ) ); |
| 73 | } |
| 74 | |
| 75 | if ( ! in_array( $element, array( 'a', 'button', 'input' ), true ) ) { |
| 76 | $element = 'a'; |
| 77 | } |
| 78 | |
| 79 | if ( 'a' === $element ) { |
| 80 | $button_attributes .= sprintf( ' href="%s" target="_blank" role="button" rel="noopener noreferrer"', esc_url( $url ) ); |
| 81 | } elseif ( 'button' === $element ) { |
| 82 | $button_attributes .= ' type="submit"'; |
| 83 | } elseif ( 'input' === $element ) { |
| 84 | $button_attributes .= sprintf( ' type="submit" value="%s"', esc_attr( wp_strip_all_tags( $text, true ) ) ); |
| 85 | } |
| 86 | |
| 87 | $button = 'input' === $element |
| 88 | ? '<' . $element . $button_attributes . ' />' |
| 89 | : '<' . $element . $button_attributes . '>' . $text . '</' . $element . '>'; |
| 90 | |
| 91 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 92 | return '<div' . $wrapper_attributes . '>' . $button . '</div>'; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the Button block classes. |
| 97 | * |
| 98 | * @param array $attributes Array containing the block attributes. |
| 99 | * |
| 100 | * @return string |
| 101 | */ |
| 102 | function get_button_classes( $attributes ) { |
| 103 | $classes = array( 'wp-block-button__link' ); |
| 104 | $has_class_name = array_key_exists( 'className', $attributes ); |
| 105 | $has_named_text_color = array_key_exists( 'textColor', $attributes ); |
| 106 | $has_custom_text_color = array_key_exists( 'customTextColor', $attributes ); |
| 107 | $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); |
| 108 | $has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes ); |
| 109 | $has_named_gradient = array_key_exists( 'gradient', $attributes ); |
| 110 | $has_custom_gradient = array_key_exists( 'customGradient', $attributes ); |
| 111 | $has_border_radius = array_key_exists( 'borderRadius', $attributes ); |
| 112 | $has_font_size = array_key_exists( 'fontSize', $attributes ); |
| 113 | |
| 114 | if ( $has_font_size ) { |
| 115 | $classes[] = 'has-' . $attributes['fontSize'] . '-font-size'; |
| 116 | $classes[] = 'has-custom-font-size'; |
| 117 | } |
| 118 | |
| 119 | if ( $has_class_name ) { |
| 120 | $classes[] = $attributes['className']; |
| 121 | } |
| 122 | |
| 123 | if ( $has_named_text_color || $has_custom_text_color ) { |
| 124 | $classes[] = 'has-text-color'; |
| 125 | } |
| 126 | if ( $has_named_text_color ) { |
| 127 | $classes[] = sprintf( 'has-%s-color', $attributes['textColor'] ); |
| 128 | } |
| 129 | |
| 130 | if ( |
| 131 | $has_named_background_color || |
| 132 | $has_custom_background_color || |
| 133 | $has_named_gradient || |
| 134 | $has_custom_gradient |
| 135 | ) { |
| 136 | $classes[] = 'has-background'; |
| 137 | } |
| 138 | if ( $has_named_background_color && ! $has_custom_gradient ) { |
| 139 | $classes[] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); |
| 140 | } |
| 141 | if ( $has_named_gradient ) { |
| 142 | $classes[] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] ); |
| 143 | } |
| 144 | |
| 145 | // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual |
| 146 | if ( $has_border_radius && 0 == $attributes['borderRadius'] ) { |
| 147 | $classes[] = 'no-border-radius'; |
| 148 | } |
| 149 | |
| 150 | return implode( ' ', $classes ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Get the Button block styles. |
| 155 | * |
| 156 | * @param array $attributes Array containing the block attributes. |
| 157 | * |
| 158 | * @return string |
| 159 | */ |
| 160 | function get_button_styles( $attributes ) { |
| 161 | $styles = array(); |
| 162 | $has_named_text_color = array_key_exists( 'textColor', $attributes ); |
| 163 | $has_custom_text_color = array_key_exists( 'customTextColor', $attributes ); |
| 164 | $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); |
| 165 | $has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes ); |
| 166 | $has_named_gradient = array_key_exists( 'gradient', $attributes ); |
| 167 | $has_custom_gradient = array_key_exists( 'customGradient', $attributes ); |
| 168 | $has_border_radius = array_key_exists( 'borderRadius', $attributes ); |
| 169 | $has_font_family = array_key_exists( 'fontFamily', $attributes ); |
| 170 | $has_typography_styles = array_key_exists( 'style', $attributes ) && array_key_exists( 'typography', $attributes['style'] ); |
| 171 | $has_custom_font_size = $has_typography_styles && array_key_exists( 'fontSize', $attributes['style']['typography'] ); |
| 172 | $has_custom_text_transform = $has_typography_styles && array_key_exists( 'textTransform', $attributes['style']['typography'] ); |
| 173 | |
| 174 | if ( $has_font_family ) { |
| 175 | $styles[] = sprintf( 'font-family: %s;', $attributes['fontFamily'] ); |
| 176 | } |
| 177 | |
| 178 | if ( $has_custom_font_size ) { |
| 179 | $styles[] = sprintf( 'font-size: %s;', $attributes['style']['typography']['fontSize'] ); |
| 180 | } |
| 181 | |
| 182 | if ( $has_custom_text_transform ) { |
| 183 | $styles[] = sprintf( 'text-transform: %s;', $attributes['style']['typography']['textTransform'] ); |
| 184 | } |
| 185 | |
| 186 | if ( ! $has_named_text_color && $has_custom_text_color ) { |
| 187 | $styles[] = sprintf( 'color: %s;', $attributes['customTextColor'] ); |
| 188 | } |
| 189 | |
| 190 | if ( ! $has_named_background_color && ! $has_named_gradient && $has_custom_gradient ) { |
| 191 | $styles[] = sprintf( 'background: %s;', $attributes['customGradient'] ); |
| 192 | } |
| 193 | |
| 194 | if ( |
| 195 | $has_custom_background_color && |
| 196 | ! $has_named_background_color && |
| 197 | ! $has_named_gradient && |
| 198 | ! $has_custom_gradient |
| 199 | ) { |
| 200 | $styles[] = sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] ); |
| 201 | } |
| 202 | |
| 203 | // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual |
| 204 | if ( $has_border_radius && 0 != $attributes['borderRadius'] ) { |
| 205 | $styles[] = sprintf( 'border-radius: %spx;', $attributes['borderRadius'] ); |
| 206 | } |
| 207 | |
| 208 | return implode( ' ', $styles ); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Get the Button wrapper block styles. |
| 213 | * |
| 214 | * @param array $attributes Array containing the block attributes. |
| 215 | * |
| 216 | * @return string |
| 217 | */ |
| 218 | function get_button_wrapper_styles( $attributes ) { |
| 219 | $styles = array(); |
| 220 | $has_width = array_key_exists( 'width', $attributes ); |
| 221 | |
| 222 | if ( $has_width ) { |
| 223 | $styles[] = sprintf( 'width: %s;', $attributes['width'] ); |
| 224 | } |
| 225 | |
| 226 | return implode( ' ', $styles ); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Get filtered attributes. |
| 231 | * |
| 232 | * @param array $attributes Array containing the Button block attributes. |
| 233 | * @param string $attribute_name String containing the attribute name to get. |
| 234 | * |
| 235 | * @return string |
| 236 | */ |
| 237 | function get_attribute( $attributes, $attribute_name ) { |
| 238 | if ( isset( $attributes[ $attribute_name ] ) ) { |
| 239 | return $attributes[ $attribute_name ]; |
| 240 | } |
| 241 | |
| 242 | $default_attributes = array( |
| 243 | 'url' => '#', |
| 244 | 'element' => 'a', |
| 245 | 'saveInPostContent' => false, |
| 246 | ); |
| 247 | |
| 248 | if ( isset( $default_attributes[ $attribute_name ] ) ) { |
| 249 | return $default_attributes[ $attribute_name ]; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Enqueue style for an existing block. |
| 255 | * |
| 256 | * The Jetpack Button block depends on styles from the core button block. |
| 257 | * In case that block is not already within the post content, we can use |
| 258 | * this function to ensure the block's style assets are enqueued. |
| 259 | * |
| 260 | * @param string $block_name Block type name including namespace. |
| 261 | */ |
| 262 | function enqueue_existing_button_style_dependency( $block_name ) { |
| 263 | $existing_block = \WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); |
| 264 | if ( isset( $existing_block ) && ! empty( $existing_block->style ) ) { |
| 265 | wp_enqueue_style( $existing_block->style ); |
| 266 | } |
| 267 | } |
| 268 |