Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/extensio.../blocks/button
File: button.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Button Block.
[2] Fix | Delete
*
[3] Fix | Delete
* @since 8.5.0
[4] Fix | Delete
*
[5] Fix | Delete
* @package automattic/jetpack
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
namespace Automattic\Jetpack\Extensions\Button;
[9] Fix | Delete
[10] Fix | Delete
use Automattic\Jetpack\Blocks;
[11] Fix | Delete
use Jetpack_Gutenberg;
[12] Fix | Delete
[13] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[14] Fix | Delete
exit( 0 );
[15] Fix | Delete
}
[16] Fix | Delete
[17] Fix | Delete
const FEATURE_NAME = 'button';
[18] Fix | Delete
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Registers the block for use in Gutenberg
[22] Fix | Delete
* This is done via an action so that we can disable
[23] Fix | Delete
* registration if we need to.
[24] Fix | Delete
*/
[25] Fix | Delete
function register_block() {
[26] Fix | Delete
Blocks::jetpack_register_block(
[27] Fix | Delete
BLOCK_NAME,
[28] Fix | Delete
array(
[29] Fix | Delete
'render_callback' => __NAMESPACE__ . '\render_block',
[30] Fix | Delete
'uses_context' => array( 'jetpack/parentBlockWidth' ),
[31] Fix | Delete
'selectors' => array(
[32] Fix | Delete
'border' => '.wp-block-jetpack-button .wp-block-button__link',
[33] Fix | Delete
),
[34] Fix | Delete
)
[35] Fix | Delete
);
[36] Fix | Delete
}
[37] Fix | Delete
add_action( 'init', __NAMESPACE__ . '\register_block' );
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Button block render callback.
[41] Fix | Delete
*
[42] Fix | Delete
* @param array $attributes Array containing the Button block attributes.
[43] Fix | Delete
* @param string $content The Button block content.
[44] Fix | Delete
*
[45] Fix | Delete
* @return string
[46] Fix | Delete
*/
[47] Fix | Delete
function render_block( $attributes, $content ) {
[48] Fix | Delete
$save_in_post_content = get_attribute( $attributes, 'saveInPostContent' );
[49] Fix | Delete
[50] Fix | Delete
// The Jetpack Button block depends on the core button block styles.
[51] Fix | Delete
// The following ensures that those styles are enqueued when rendering this block.
[52] Fix | Delete
enqueue_existing_button_style_dependency( 'core/button' );
[53] Fix | Delete
enqueue_existing_button_style_dependency( 'core/buttons' );
[54] Fix | Delete
[55] Fix | Delete
Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME );
[56] Fix | Delete
[57] Fix | Delete
if ( $save_in_post_content || ! class_exists( 'DOMDocument' ) ) {
[58] Fix | Delete
return $content;
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
$element = get_attribute( $attributes, 'element' );
[62] Fix | Delete
$text = wp_kses_post( get_attribute( $attributes, 'text' ) );
[63] Fix | Delete
$unique_id = get_attribute( $attributes, 'uniqueId' );
[64] Fix | Delete
$url = get_attribute( $attributes, 'url' );
[65] Fix | Delete
$classes = Blocks::classes( FEATURE_NAME, $attributes, array( 'wp-block-button' ) );
[66] Fix | Delete
[67] Fix | Delete
$button_classes = get_button_classes( $attributes );
[68] Fix | Delete
$button_styles = get_button_styles( $attributes );
[69] Fix | Delete
$wrapper_styles = get_button_wrapper_styles( $attributes );
[70] Fix | Delete
[71] Fix | Delete
$wrapper_attributes = sprintf( ' class="%s" style="%s"', esc_attr( $classes ), esc_attr( $wrapper_styles ) );
[72] Fix | Delete
$button_attributes = sprintf( ' class="%s" style="%s"', esc_attr( $button_classes ), esc_attr( $button_styles ) );
[73] Fix | Delete
[74] Fix | Delete
if ( empty( $unique_id ) ) {
[75] Fix | Delete
$button_attributes .= ' data-id-attr="placeholder"';
[76] Fix | Delete
} else {
[77] Fix | Delete
$button_attributes .= sprintf( ' data-id-attr="%1$s" id="%1$s"', esc_attr( $unique_id ) );
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
if ( ! in_array( $element, array( 'a', 'button', 'input' ), true ) ) {
[81] Fix | Delete
$element = 'a';
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
if ( 'a' === $element ) {
[85] Fix | Delete
$button_attributes .= sprintf( ' href="%s" target="_blank" role="button" rel="noopener noreferrer"', esc_url( $url ) );
[86] Fix | Delete
} elseif ( 'button' === $element ) {
[87] Fix | Delete
$button_attributes .= ' type="submit"';
[88] Fix | Delete
} elseif ( 'input' === $element ) {
[89] Fix | Delete
$button_attributes .= sprintf( ' type="submit" value="%s"', esc_attr( wp_strip_all_tags( $text, true ) ) );
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
$button_attributes .= ' data-wp-class--is-submitting="state.isSubmitting" data-wp-bind--aria-disabled="state.isAriaDisabled"';
[93] Fix | Delete
[94] Fix | Delete
$svg = '<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z" opacity=".25"/><path d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"><animateTransform attributeName="transform" type="rotate" dur="0.75s" values="0 12 12;360 12 12" repeatCount="indefinite"/></path></svg>';
[95] Fix | Delete
$form_submitting_text = '<span class="is-visually-hidden">' . __( 'Submitting form', 'jetpack' ) . '</span>';
[96] Fix | Delete
$spinner = '<span class="spinner" aria-hidden="true">' . $svg . $form_submitting_text . '</span>';
[97] Fix | Delete
[98] Fix | Delete
$button = 'input' === $element
[99] Fix | Delete
? '<' . $element . $button_attributes . ' />'
[100] Fix | Delete
: '<' . $element . $button_attributes . '>' . $text . $spinner . '</' . $element . '>';
[101] Fix | Delete
[102] Fix | Delete
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[103] Fix | Delete
return '<div' . $wrapper_attributes . '>' . $button . '</div>';
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
/**
[107] Fix | Delete
* Get the Button block classes.
[108] Fix | Delete
*
[109] Fix | Delete
* @param array $attributes Array containing the block attributes.
[110] Fix | Delete
*
[111] Fix | Delete
* @return string
[112] Fix | Delete
*/
[113] Fix | Delete
function get_button_classes( $attributes ) {
[114] Fix | Delete
$classes = array( 'wp-block-button__link' );
[115] Fix | Delete
$has_class_name = array_key_exists( 'className', $attributes );
[116] Fix | Delete
$has_named_text_color = array_key_exists( 'textColor', $attributes );
[117] Fix | Delete
$has_custom_text_color = array_key_exists( 'customTextColor', $attributes );
[118] Fix | Delete
$has_named_background_color = array_key_exists( 'backgroundColor', $attributes );
[119] Fix | Delete
$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes );
[120] Fix | Delete
$has_named_gradient = array_key_exists( 'gradient', $attributes );
[121] Fix | Delete
$has_custom_gradient = array_key_exists( 'customGradient', $attributes );
[122] Fix | Delete
$has_border_radius = array_key_exists( 'borderRadius', $attributes );
[123] Fix | Delete
$has_font_size = array_key_exists( 'fontSize', $attributes );
[124] Fix | Delete
$has_named_border_color = array_key_exists( 'borderColor', $attributes );
[125] Fix | Delete
[126] Fix | Delete
if ( $has_font_size ) {
[127] Fix | Delete
$classes[] = 'has-' . $attributes['fontSize'] . '-font-size';
[128] Fix | Delete
$classes[] = 'has-custom-font-size';
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
if ( $has_class_name ) {
[132] Fix | Delete
$classes[] = $attributes['className'];
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
if ( $has_named_text_color || $has_custom_text_color ) {
[136] Fix | Delete
$classes[] = 'has-text-color';
[137] Fix | Delete
}
[138] Fix | Delete
if ( $has_named_text_color ) {
[139] Fix | Delete
$classes[] = sprintf( 'has-%s-color', $attributes['textColor'] );
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
if ( $has_named_border_color ) {
[143] Fix | Delete
$classes[] = sprintf( 'has-%s-border-color', $attributes['borderColor'] );
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
if (
[147] Fix | Delete
$has_named_background_color ||
[148] Fix | Delete
$has_custom_background_color ||
[149] Fix | Delete
$has_named_gradient ||
[150] Fix | Delete
$has_custom_gradient
[151] Fix | Delete
) {
[152] Fix | Delete
$classes[] = 'has-background';
[153] Fix | Delete
}
[154] Fix | Delete
if ( $has_named_background_color && ! $has_custom_gradient ) {
[155] Fix | Delete
$classes[] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
[156] Fix | Delete
}
[157] Fix | Delete
if ( $has_named_gradient ) {
[158] Fix | Delete
$classes[] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] );
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
// phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
[162] Fix | Delete
if ( $has_border_radius && 0 == $attributes['borderRadius'] ) {
[163] Fix | Delete
$classes[] = 'no-border-radius';
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
return implode( ' ', $classes );
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
/**
[170] Fix | Delete
* Get the Button block styles.
[171] Fix | Delete
*
[172] Fix | Delete
* @param array $attributes Array containing the block attributes.
[173] Fix | Delete
*
[174] Fix | Delete
* @return string
[175] Fix | Delete
*/
[176] Fix | Delete
function get_button_styles( $attributes ) {
[177] Fix | Delete
$styles = array();
[178] Fix | Delete
$has_named_text_color = array_key_exists( 'textColor', $attributes );
[179] Fix | Delete
$has_custom_text_color = array_key_exists( 'customTextColor', $attributes );
[180] Fix | Delete
$has_named_background_color = array_key_exists( 'backgroundColor', $attributes );
[181] Fix | Delete
$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes );
[182] Fix | Delete
$has_named_gradient = array_key_exists( 'gradient', $attributes );
[183] Fix | Delete
$has_custom_gradient = array_key_exists( 'customGradient', $attributes );
[184] Fix | Delete
$has_border_radius = array_key_exists( 'borderRadius', $attributes );
[185] Fix | Delete
$has_font_family = array_key_exists( 'fontFamily', $attributes );
[186] Fix | Delete
$has_typography_styles = array_key_exists( 'style', $attributes ) && array_key_exists( 'typography', $attributes['style'] );
[187] Fix | Delete
$has_custom_font_size = $has_typography_styles && array_key_exists( 'fontSize', $attributes['style']['typography'] );
[188] Fix | Delete
$has_custom_text_transform = $has_typography_styles && array_key_exists( 'textTransform', $attributes['style']['typography'] );
[189] Fix | Delete
$border_styles = array();
[190] Fix | Delete
$border_attribute = $attributes['style']['border'] ?? null;
[191] Fix | Delete
$is_border_style_array = is_array( $border_attribute );
[192] Fix | Delete
[193] Fix | Delete
$has_custom_border_color = $is_border_style_array && isset( $border_attribute['color'] );
[194] Fix | Delete
$has_border_style = $is_border_style_array && isset( $border_attribute['style'] );
[195] Fix | Delete
$has_border_width = $is_border_style_array && isset( $border_attribute['width'] );
[196] Fix | Delete
$has_individual_borders = $is_border_style_array && (
[197] Fix | Delete
isset( $border_attribute['top'] ) ||
[198] Fix | Delete
isset( $border_attribute['right'] ) ||
[199] Fix | Delete
isset( $border_attribute['bottom'] ) ||
[200] Fix | Delete
isset( $border_attribute['left'] )
[201] Fix | Delete
);
[202] Fix | Delete
[203] Fix | Delete
if ( $has_font_family ) {
[204] Fix | Delete
$styles[] = sprintf( 'font-family: %s;', $attributes['fontFamily'] );
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
if ( $has_custom_font_size ) {
[208] Fix | Delete
$styles[] = sprintf( 'font-size: %s;', $attributes['style']['typography']['fontSize'] );
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
if ( $has_custom_text_transform ) {
[212] Fix | Delete
$styles[] = sprintf( 'text-transform: %s;', $attributes['style']['typography']['textTransform'] );
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
if ( ! $has_named_text_color && $has_custom_text_color ) {
[216] Fix | Delete
$styles[] = sprintf( 'color: %s;', $attributes['customTextColor'] );
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
if ( ! $has_named_background_color && ! $has_named_gradient && $has_custom_gradient ) {
[220] Fix | Delete
$styles[] = sprintf( 'background: %s;', $attributes['customGradient'] );
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
if (
[224] Fix | Delete
$has_custom_background_color &&
[225] Fix | Delete
! $has_named_background_color &&
[226] Fix | Delete
! $has_named_gradient &&
[227] Fix | Delete
! $has_custom_gradient
[228] Fix | Delete
) {
[229] Fix | Delete
$styles[] = sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] );
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
// phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual
[233] Fix | Delete
if ( $has_border_radius && 0 != $attributes['borderRadius'] ) {
[234] Fix | Delete
$styles[] = sprintf( 'border-radius: %spx;', $attributes['borderRadius'] );
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
if ( $has_custom_border_color ) {
[238] Fix | Delete
$border_styles['color'] = $attributes['style']['border']['color'];
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
if ( $has_border_style ) {
[242] Fix | Delete
$border_styles['style'] = $attributes['style']['border']['style'];
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
if ( $has_border_width ) {
[246] Fix | Delete
$border_styles['width'] = $attributes['style']['border']['width'];
[247] Fix | Delete
}
[248] Fix | Delete
[249] Fix | Delete
if ( $has_individual_borders ) {
[250] Fix | Delete
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
[251] Fix | Delete
$border = $attributes['style']['border'][ $side ] ?? null;
[252] Fix | Delete
if ( is_array( $border ) ) {
[253] Fix | Delete
$border_side_values = array(
[254] Fix | Delete
'width' => $border['width'] ?? null,
[255] Fix | Delete
'color' => $border['color'] ?? null,
[256] Fix | Delete
'style' => $border['style'] ?? null,
[257] Fix | Delete
);
[258] Fix | Delete
$border_styles[ $side ] = $border_side_values;
[259] Fix | Delete
}
[260] Fix | Delete
}
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
$border_styles = wp_style_engine_get_styles( array( 'border' => $border_styles ) );
[264] Fix | Delete
if ( isset( $border_styles['css'] ) ) {
[265] Fix | Delete
$styles[] = $border_styles['css'];
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
return implode( ' ', $styles );
[269] Fix | Delete
}
[270] Fix | Delete
[271] Fix | Delete
/**
[272] Fix | Delete
* Get the Button wrapper block styles.
[273] Fix | Delete
*
[274] Fix | Delete
* @param array $attributes Array containing the block attributes.
[275] Fix | Delete
*
[276] Fix | Delete
* @return string
[277] Fix | Delete
*/
[278] Fix | Delete
function get_button_wrapper_styles( $attributes ) {
[279] Fix | Delete
$styles = array();
[280] Fix | Delete
$has_width = array_key_exists( 'width', $attributes );
[281] Fix | Delete
[282] Fix | Delete
if ( $has_width && ! empty( $attributes['width'] ) ) {
[283] Fix | Delete
$styles[] = sprintf( 'width: %s;', $attributes['width'] );
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
return implode( ' ', $styles );
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
/**
[290] Fix | Delete
* Get filtered attributes.
[291] Fix | Delete
*
[292] Fix | Delete
* @param array $attributes Array containing the Button block attributes.
[293] Fix | Delete
* @param string $attribute_name String containing the attribute name to get.
[294] Fix | Delete
*
[295] Fix | Delete
* @return string
[296] Fix | Delete
*/
[297] Fix | Delete
function get_attribute( $attributes, $attribute_name ) {
[298] Fix | Delete
if ( isset( $attributes[ $attribute_name ] ) ) {
[299] Fix | Delete
return $attributes[ $attribute_name ];
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
$default_attributes = array(
[303] Fix | Delete
'url' => '#',
[304] Fix | Delete
'element' => 'a',
[305] Fix | Delete
'saveInPostContent' => false,
[306] Fix | Delete
);
[307] Fix | Delete
[308] Fix | Delete
if ( isset( $default_attributes[ $attribute_name ] ) ) {
[309] Fix | Delete
return $default_attributes[ $attribute_name ];
[310] Fix | Delete
}
[311] Fix | Delete
}
[312] Fix | Delete
[313] Fix | Delete
/**
[314] Fix | Delete
* Enqueue style for an existing block.
[315] Fix | Delete
*
[316] Fix | Delete
* The Jetpack Button block depends on styles from the core button block.
[317] Fix | Delete
* In case that block is not already within the post content, we can use
[318] Fix | Delete
* this function to ensure the block's style assets are enqueued.
[319] Fix | Delete
*
[320] Fix | Delete
* @param string $block_name Block type name including namespace.
[321] Fix | Delete
*/
[322] Fix | Delete
function enqueue_existing_button_style_dependency( $block_name ) {
[323] Fix | Delete
$existing_block = \WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
[324] Fix | Delete
if ( isset( $existing_block ) && ! empty( $existing_block->style ) ) {
[325] Fix | Delete
wp_enqueue_style( $existing_block->style );
[326] Fix | Delete
}
[327] Fix | Delete
}
[328] Fix | Delete
[329] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function