Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/extensio.../blocks/sharing-...
File: sharing-button.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Sharing Buttons Block.
[2] Fix | Delete
*
[3] Fix | Delete
* @since 13.1
[4] Fix | Delete
*
[5] Fix | Delete
* @package automattic/jetpack
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
namespace Automattic\Jetpack\Extensions\Sharing_Button_Block;
[9] Fix | Delete
[10] Fix | Delete
use Automattic\Jetpack\Blocks;
[11] Fix | Delete
use Automattic\Jetpack\Modules;
[12] Fix | Delete
use Automattic\Jetpack\Status\Host;
[13] Fix | Delete
use Jetpack_Gutenberg;
[14] Fix | Delete
use WP_Block_Template;
[15] Fix | Delete
[16] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[17] Fix | Delete
exit( 0 );
[18] Fix | Delete
}
[19] Fix | Delete
[20] Fix | Delete
require_once __DIR__ . '/class-sharing-source-block.php';
[21] Fix | Delete
require_once __DIR__ . '/components/social-icons.php';
[22] Fix | Delete
[23] Fix | Delete
const PARENT_BLOCK_NAME = 'jetpack/sharing-buttons';
[24] Fix | Delete
const INNER_BLOCK_NAME = 'jetpack/sharing-button';
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Registers the block for use in Gutenberg
[28] Fix | Delete
* This is done via an action so that we can disable
[29] Fix | Delete
* registration if we need to.
[30] Fix | Delete
*/
[31] Fix | Delete
function register_block() {
[32] Fix | Delete
Blocks::jetpack_register_block(
[33] Fix | Delete
__DIR__,
[34] Fix | Delete
array( 'render_callback' => __NAMESPACE__ . '\render_block' )
[35] Fix | Delete
);
[36] Fix | Delete
[37] Fix | Delete
/*
[38] Fix | Delete
* Automatically add the sharing block to the end of single posts.
[39] Fix | Delete
*/
[40] Fix | Delete
add_filter( 'hooked_block_types', __NAMESPACE__ . '\add_block_to_single_posts_template', 10, 4 );
[41] Fix | Delete
add_filter( 'hooked_block_' . PARENT_BLOCK_NAME, __NAMESPACE__ . '\add_default_services_to_block', 10, 5 );
[42] Fix | Delete
}
[43] Fix | Delete
add_action( 'init', __NAMESPACE__ . '\register_block' );
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* Sharing Buttons block registration/dependency declaration.
[47] Fix | Delete
*
[48] Fix | Delete
* @param array $attr Array containing the Sharing Buttons block attributes.
[49] Fix | Delete
* @param string $content String containing the Sharing Buttons block content.
[50] Fix | Delete
* @param object $block Object containing block data.
[51] Fix | Delete
*
[52] Fix | Delete
* @return string
[53] Fix | Delete
*/
[54] Fix | Delete
function render_block( $attr, $content, $block ) {
[55] Fix | Delete
$service_name = $attr['service'];
[56] Fix | Delete
$title = $attr['label'] ?? $service_name;
[57] Fix | Delete
$icon = get_social_logo( $service_name );
[58] Fix | Delete
$style_type = $block->context['styleType'] ?? 'icon-text';
[59] Fix | Delete
$post_id = $block->context['postId'] ?? 0;
[60] Fix | Delete
$data_shared = sprintf(
[61] Fix | Delete
'sharing-%1$s-%2$d',
[62] Fix | Delete
$service_name,
[63] Fix | Delete
$post_id
[64] Fix | Delete
);
[65] Fix | Delete
[66] Fix | Delete
$services = get_services();
[67] Fix | Delete
if ( ! array_key_exists( $service_name, $services ) ) {
[68] Fix | Delete
return $content;
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
$service = new $services[ $service_name ]( $service_name, array() );
[72] Fix | Delete
$link_props = $service->get_link(
[73] Fix | Delete
$post_id,
[74] Fix | Delete
'share=' . esc_attr( $service_name ) . '&nb=1',
[75] Fix | Delete
esc_attr( $data_shared )
[76] Fix | Delete
);
[77] Fix | Delete
$link_url = $link_props['url'];
[78] Fix | Delete
$link_classes = sprintf(
[79] Fix | Delete
'jetpack-sharing-button__button style-%1$s share-%2$s',
[80] Fix | Delete
$style_type,
[81] Fix | Delete
$service_name
[82] Fix | Delete
);
[83] Fix | Delete
[84] Fix | Delete
$accessible_name = sprintf(
[85] Fix | Delete
/* translators: %s refers to a string representation of sharing service, e.g. Facebook */
[86] Fix | Delete
esc_html__( 'Share on %s', 'jetpack' ),
[87] Fix | Delete
esc_html( $title )
[88] Fix | Delete
);
[89] Fix | Delete
[90] Fix | Delete
$accessible_name .= __( ' (Opens in new window)', 'jetpack' );
[91] Fix | Delete
[92] Fix | Delete
$block_class_name = 'jetpack-sharing-button__list-item';
[93] Fix | Delete
[94] Fix | Delete
if ( $service_name === 'share' ) {
[95] Fix | Delete
$block_class_name .= ' tooltip';
[96] Fix | Delete
/* translators: aria label for SMS sharing button */
[97] Fix | Delete
$accessible_name = esc_attr__( 'Share using Native tools', 'jetpack' );
[98] Fix | Delete
$link_props = $service->get_link( $post_id );
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
$link_url = $link_props['url'];
[102] Fix | Delete
$link_classes = sprintf(
[103] Fix | Delete
'jetpack-sharing-button__button style-%1$s share-%2$s',
[104] Fix | Delete
$style_type,
[105] Fix | Delete
$service_name
[106] Fix | Delete
);
[107] Fix | Delete
[108] Fix | Delete
$styles = array();
[109] Fix | Delete
if (
[110] Fix | Delete
array_key_exists( 'iconColorValue', $block->context )
[111] Fix | Delete
&& ! empty( $block->context['iconColorValue'] )
[112] Fix | Delete
) {
[113] Fix | Delete
$styles['color'] = $block->context['iconColorValue'];
[114] Fix | Delete
}
[115] Fix | Delete
if (
[116] Fix | Delete
array_key_exists( 'iconBackgroundColorValue', $block->context )
[117] Fix | Delete
&& ! empty( $block->context['iconBackgroundColorValue'] )
[118] Fix | Delete
) {
[119] Fix | Delete
$styles['background-color'] = $block->context['iconBackgroundColorValue'];
[120] Fix | Delete
}
[121] Fix | Delete
$link_styles = '';
[122] Fix | Delete
foreach ( $styles as $property => $value ) {
[123] Fix | Delete
$link_styles .= $property . ':' . $value . ';';
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
[127] Fix | Delete
[128] Fix | Delete
$component = '<li class="' . esc_attr( $block_class_name ) . '">';
[129] Fix | Delete
$component .= sprintf(
[130] Fix | Delete
'<a href="%1$s" target="_blank" rel="nofollow noopener noreferrer" class="%2$s" style="%3$s" data-service="%4$s" data-shared="%5$s" aria-labelledby="%5$s">',
[131] Fix | Delete
esc_url( $link_url ),
[132] Fix | Delete
esc_attr( $link_classes ),
[133] Fix | Delete
esc_attr( $link_styles ),
[134] Fix | Delete
esc_attr( $service_name ),
[135] Fix | Delete
esc_attr( $data_shared )
[136] Fix | Delete
);
[137] Fix | Delete
[138] Fix | Delete
// Add hidden span with accessible name
[139] Fix | Delete
$component .= sprintf(
[140] Fix | Delete
'<span id="%s" hidden>%s</span>',
[141] Fix | Delete
esc_attr( $data_shared ),
[142] Fix | Delete
esc_html( $accessible_name )
[143] Fix | Delete
);
[144] Fix | Delete
[145] Fix | Delete
$component .= $style_type !== 'text' ? $icon : '';
[146] Fix | Delete
$component .= '<span class="jetpack-sharing-button__service-label" aria-hidden="true">' . esc_html( $title ) . '</span>';
[147] Fix | Delete
if ( $service_name === 'share' ) {
[148] Fix | Delete
$component .= '<span class="tooltiptext" aria-live="assertive">' . esc_html__( 'Copied to clipboard', 'jetpack' ) . '</span>';
[149] Fix | Delete
}
[150] Fix | Delete
$component .= '</a>';
[151] Fix | Delete
$component .= '</li>';
[152] Fix | Delete
[153] Fix | Delete
return $component;
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
/**
[157] Fix | Delete
* Get services for the Sharing Buttons block.
[158] Fix | Delete
*
[159] Fix | Delete
* @return array Array of services.
[160] Fix | Delete
*/
[161] Fix | Delete
function get_services() {
[162] Fix | Delete
$services = array(
[163] Fix | Delete
'print' => Share_Print_Block::class,
[164] Fix | Delete
'mail' => Share_Email_Block::class,
[165] Fix | Delete
'facebook' => Share_Facebook_Block::class,
[166] Fix | Delete
'linkedin' => Share_LinkedIn_Block::class,
[167] Fix | Delete
'reddit' => Share_Reddit_Block::class,
[168] Fix | Delete
'twitter' => Share_Twitter_Block::class,
[169] Fix | Delete
'tumblr' => Share_Tumblr_Block::class,
[170] Fix | Delete
'pinterest' => Share_Pinterest_Block::class,
[171] Fix | Delete
'telegram' => Share_Telegram_Block::class,
[172] Fix | Delete
'threads' => Share_Threads_Block::class,
[173] Fix | Delete
'whatsapp' => Jetpack_Share_WhatsApp_Block::class,
[174] Fix | Delete
'mastodon' => Share_Mastodon_Block::class,
[175] Fix | Delete
'nextdoor' => Share_Nextdoor_Block::class,
[176] Fix | Delete
'bluesky' => Share_Bluesky_Block::class,
[177] Fix | Delete
'x' => Share_X_Block::class,
[178] Fix | Delete
'share' => Share_Native_Block::class,
[179] Fix | Delete
);
[180] Fix | Delete
[181] Fix | Delete
return $services;
[182] Fix | Delete
}
[183] Fix | Delete
[184] Fix | Delete
/**
[185] Fix | Delete
* Launch sharing requests on page load when a specific query string is used.
[186] Fix | Delete
*
[187] Fix | Delete
* @return void
[188] Fix | Delete
*/
[189] Fix | Delete
function sharing_process_requests() {
[190] Fix | Delete
global $post;
[191] Fix | Delete
[192] Fix | Delete
// Only process if: single post and share={service} defined
[193] Fix | Delete
if ( ( is_page() || is_single() ) && isset( $_GET['share'] ) && is_string( $_GET['share'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
[194] Fix | Delete
$services = get_services();
[195] Fix | Delete
$service_name = sanitize_text_field( wp_unslash( $_GET['share'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
[196] Fix | Delete
[197] Fix | Delete
// Only allow services that have been defined in get_services().
[198] Fix | Delete
if ( ! array_key_exists( $service_name, $services ) ) {
[199] Fix | Delete
return;
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
$service = new $services[ ( $service_name ) ]( $service_name, array() ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
[203] Fix | Delete
$service->process_request( $post, $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
[204] Fix | Delete
}
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only checking for the data being present.
[208] Fix | Delete
if ( isset( $_GET['share'] ) ) {
[209] Fix | Delete
add_action( 'template_redirect', __NAMESPACE__ . '\sharing_process_requests', 9 );
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
/**
[213] Fix | Delete
* Automatically add the Sharing Buttons block to the end of the Single Posts template.
[214] Fix | Delete
*
[215] Fix | Delete
* @since 13.2
[216] Fix | Delete
*
[217] Fix | Delete
* @param array $hooked_block_types The list of hooked block types.
[218] Fix | Delete
* @param string $relative_position The relative position of the hooked blocks. Can be one of 'before', 'after', 'first_child', or 'last_child'.
[219] Fix | Delete
* @param string $anchor_block_type The anchor block type.
[220] Fix | Delete
* @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to.
[221] Fix | Delete
*
[222] Fix | Delete
* @return array
[223] Fix | Delete
*/
[224] Fix | Delete
function add_block_to_single_posts_template( $hooked_block_types, $relative_position, $anchor_block_type, $context ) {
[225] Fix | Delete
// Add the block at the end of the post content.
[226] Fix | Delete
if (
[227] Fix | Delete
'after' !== $relative_position
[228] Fix | Delete
|| 'core/post-content' !== $anchor_block_type
[229] Fix | Delete
) {
[230] Fix | Delete
return $hooked_block_types;
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
// Only automate the addition of the block in block-based themes.
[234] Fix | Delete
if ( ! wp_is_block_theme() ) {
[235] Fix | Delete
return $hooked_block_types;
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
// Proceed if the user has toggled the auto-addition in Jetpack settings.
[239] Fix | Delete
if ( ! get_option( 'jetpack_sharing_buttons_auto_add' ) ) {
[240] Fix | Delete
return $hooked_block_types;
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
/*
[244] Fix | Delete
* The Sharing module must be disabled.
[245] Fix | Delete
* We do not want to automatically insert sharing buttons twice.
[246] Fix | Delete
* On WordPress.com Simple the module is always active so we must check differently.
[247] Fix | Delete
* There, we check if buttons are enabled on single posts and pages.
[248] Fix | Delete
*/
[249] Fix | Delete
if ( ( new Host() )->is_wpcom_simple() ) {
[250] Fix | Delete
if ( ! class_exists( 'Sharing_Service' ) ) {
[251] Fix | Delete
include_once JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-service.php';
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
$sharer = new \Sharing_Service();
[255] Fix | Delete
$global = $sharer->get_global_options();
[256] Fix | Delete
if (
[257] Fix | Delete
! $global['show']
[258] Fix | Delete
|| in_array( 'post', $global['show'], true )
[259] Fix | Delete
|| in_array( 'page', $global['show'], true )
[260] Fix | Delete
) {
[261] Fix | Delete
return $hooked_block_types;
[262] Fix | Delete
}
[263] Fix | Delete
} elseif ( ( new Modules() )->is_active( 'sharedaddy' ) ) {
[264] Fix | Delete
return $hooked_block_types;
[265] Fix | Delete
}
[266] Fix | Delete
[267] Fix | Delete
// Only hook into page and single post templates.
[268] Fix | Delete
if (
[269] Fix | Delete
! $context instanceof WP_Block_Template
[270] Fix | Delete
|| ! property_exists( $context, 'slug' )
[271] Fix | Delete
|| empty( $context->slug )
[272] Fix | Delete
|| ! preg_match( '/^(page|single)/', $context->slug )
[273] Fix | Delete
) {
[274] Fix | Delete
return $hooked_block_types;
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
$hooked_block_types[] = PARENT_BLOCK_NAME;
[278] Fix | Delete
return $hooked_block_types;
[279] Fix | Delete
}
[280] Fix | Delete
[281] Fix | Delete
/**
[282] Fix | Delete
* Add default services to the block we add to the post content by default.
[283] Fix | Delete
*
[284] Fix | Delete
* @since 13.2
[285] Fix | Delete
*
[286] Fix | Delete
* @param array $parsed_hooked_block The parsed block array for the given hooked block type.
[287] Fix | Delete
* @param string $hooked_block_type The hooked block type name.
[288] Fix | Delete
* @param string $relative_position The relative position of the hooked block.
[289] Fix | Delete
* @param array $parsed_anchor_block The anchor block, in parsed block array format.
[290] Fix | Delete
* @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block.
[291] Fix | Delete
*
[292] Fix | Delete
* @return array
[293] Fix | Delete
*/
[294] Fix | Delete
function add_default_services_to_block( $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
[295] Fix | Delete
// Is the hooked block adjacent to the anchor block?
[296] Fix | Delete
if ( 'after' !== $relative_position ) {
[297] Fix | Delete
return $parsed_hooked_block;
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
// Use the icon style by default.
[301] Fix | Delete
$parsed_hooked_block['attrs']['styleType'] = 'icon';
[302] Fix | Delete
[303] Fix | Delete
// Add default services (inner blocks) to the block.
[304] Fix | Delete
$parsed_hooked_block['innerBlocks'] = array(
[305] Fix | Delete
array(
[306] Fix | Delete
'blockName' => INNER_BLOCK_NAME,
[307] Fix | Delete
'innerContent' => array(),
[308] Fix | Delete
'attrs' => array(
[309] Fix | Delete
'service' => 'facebook',
[310] Fix | Delete
'label' => esc_html__( 'Facebook', 'jetpack' ),
[311] Fix | Delete
),
[312] Fix | Delete
),
[313] Fix | Delete
array(
[314] Fix | Delete
'blockName' => INNER_BLOCK_NAME,
[315] Fix | Delete
'innerContent' => array(),
[316] Fix | Delete
'attrs' => array(
[317] Fix | Delete
'service' => 'x',
[318] Fix | Delete
'label' => esc_html__( 'X', 'jetpack' ),
[319] Fix | Delete
),
[320] Fix | Delete
),
[321] Fix | Delete
array(
[322] Fix | Delete
'blockName' => INNER_BLOCK_NAME,
[323] Fix | Delete
'innerContent' => array(),
[324] Fix | Delete
'attrs' => array(
[325] Fix | Delete
'service' => 'mastodon',
[326] Fix | Delete
'label' => esc_html__( 'Mastodon', 'jetpack' ),
[327] Fix | Delete
),
[328] Fix | Delete
),
[329] Fix | Delete
);
[330] Fix | Delete
[331] Fix | Delete
// Wrap inner blocks in our sharing buttons markup.
[332] Fix | Delete
$parsed_hooked_block['innerContent'] = array(
[333] Fix | Delete
'<ul class="wp-block-jetpack-sharing-buttons has-normal-icon-size jetpack-sharing-buttons__services-list" id="jetpack-sharing-serivces-list">',
[334] Fix | Delete
null,
[335] Fix | Delete
null,
[336] Fix | Delete
null,
[337] Fix | Delete
'</ul>',
[338] Fix | Delete
);
[339] Fix | Delete
[340] Fix | Delete
// Wrap the whole thing in a group block.
[341] Fix | Delete
return array(
[342] Fix | Delete
'blockName' => 'core/group',
[343] Fix | Delete
'attrs' => array(
[344] Fix | Delete
// Does the anchor block have a layout attribute? If so, use it in the group to maintain the same alignment.
[345] Fix | Delete
'layout' => $parsed_anchor_block['attrs']['layout'] ?? 'null',
[346] Fix | Delete
),
[347] Fix | Delete
'innerBlocks' => array( $parsed_hooked_block ),
[348] Fix | Delete
'innerContent' => array(
[349] Fix | Delete
'<div class="wp-block-group">',
[350] Fix | Delete
null,
[351] Fix | Delete
'</div>',
[352] Fix | Delete
),
[353] Fix | Delete
);
[354] Fix | Delete
}
[355] Fix | Delete
[356] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function