sharing-button.php
124 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Sharing Buttons Block. |
| 4 | * |
| 5 | * @since 11.x |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\Sharing_Button_Block; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Jetpack_Gutenberg; |
| 14 | |
| 15 | require_once __DIR__ . '/class-sharing-source-block.php'; |
| 16 | require_once __DIR__ . '/components/social-icons.php'; |
| 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 | __DIR__, |
| 26 | array( 'render_callback' => __NAMESPACE__ . '\render_block' ) |
| 27 | ); |
| 28 | } |
| 29 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 30 | |
| 31 | /** |
| 32 | * Sharing Buttons block registration/dependency declaration. |
| 33 | * |
| 34 | * @param array $attr Array containing the Sharing Buttons block attributes. |
| 35 | * @param string $content String containing the Sharing Buttons block content. |
| 36 | * @param array $block Array containing block data. |
| 37 | * |
| 38 | * @return string |
| 39 | */ |
| 40 | function render_block( $attr, $content, $block ) { |
| 41 | global $post; |
| 42 | $post_id = $block->context['postId']; |
| 43 | $title = $attr['label'] ?? $attr['service']; |
| 44 | |
| 45 | $style_type = $block->context['styleType']; |
| 46 | $style = 'style-' . $style_type; |
| 47 | $query = 'share=' . $attr['service'] . '&nb=1'; |
| 48 | |
| 49 | $data_shared = 'sharing-' . $attr['service'] . '-' . $post_id; |
| 50 | |
| 51 | $services = get_services(); |
| 52 | $service = new $services[ $attr['service'] ]( $attr['service'], array() ); |
| 53 | $link_props = $service->get_link( $post, $query, $data_shared ); |
| 54 | |
| 55 | $link_url = $link_props['url']; |
| 56 | $icon = get_social_logo( $attr['service'] ); |
| 57 | $sharing_link_class = 'jetpack-sharing-button__button ' . $style . ' share-' . $attr['service']; |
| 58 | |
| 59 | $link_aria_label = sprintf( |
| 60 | /* translators: %s refers to a string representation of sharing service, e.g. Facebook */ |
| 61 | esc_html__( 'Share on %s', 'jetpack' ), |
| 62 | esc_html( $title ) |
| 63 | ); |
| 64 | |
| 65 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 66 | |
| 67 | $component = '<li class="jetpack-sharing-button__list-item">'; |
| 68 | $component .= '<a rel="nofollow noopener noreferrer" class="' . esc_attr( $sharing_link_class ) . '" href="' . esc_attr( $link_url ) . '" target="_blank" '; |
| 69 | $component .= 'data-service="' . esc_attr( $attr['service'] ) . '" data-shared="' . esc_attr( $data_shared ) . '" aria-label="' . esc_attr( $link_aria_label ) . '" primary>'; |
| 70 | $component .= $icon; |
| 71 | $component .= '<span class="jetpack-sharing-button__service-label" aria-hidden="true">' . esc_html( $title ) . '</span>'; |
| 72 | $component .= '</a>'; |
| 73 | $component .= '</li>'; |
| 74 | |
| 75 | return $component; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get services for the Sharing Buttons block. |
| 80 | * |
| 81 | * @return array Array of services. |
| 82 | */ |
| 83 | function get_services() { |
| 84 | $services = array( |
| 85 | 'print' => Share_Print_Block::class, |
| 86 | 'mail' => Share_Email_Block::class, |
| 87 | 'facebook' => Share_Facebook_Block::class, |
| 88 | 'linkedin' => Share_LinkedIn_Block::class, |
| 89 | 'reddit' => Share_Reddit_Block::class, |
| 90 | 'twitter' => Share_Twitter_Block::class, |
| 91 | 'tumblr' => Share_Tumblr_Block::class, |
| 92 | 'pinterest' => Share_Pinterest_Block::class, |
| 93 | 'pocket' => Share_Pocket_Block::class, |
| 94 | 'telegram' => Share_Telegram_Block::class, |
| 95 | 'whatsapp' => Jetpack_Share_WhatsApp_Block::class, |
| 96 | 'mastodon' => Share_Mastodon_Block::class, |
| 97 | 'nextdoor' => Share_Nextdoor_Block::class, |
| 98 | 'x' => Share_X_Block::class, |
| 99 | ); |
| 100 | |
| 101 | return $services; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Launch sharing requests on page load when a specific query string is used. |
| 106 | * |
| 107 | * @return void |
| 108 | */ |
| 109 | function sharing_process_requests() { |
| 110 | global $post; |
| 111 | |
| 112 | // Only process if: single post and share={service} defined |
| 113 | if ( ( is_page() || is_single() ) && isset( $_GET['share'] ) && is_string( $_GET['share'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 114 | $services = get_services(); |
| 115 | $service_name = sanitize_text_field( wp_unslash( $_GET['share'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 116 | $service = new $services[ ( $service_name ) ]( $service_name, array() ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 117 | if ( $service ) { |
| 118 | $service->process_request( $post, $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | add_action( 'template_redirect', __NAMESPACE__ . '\sharing_process_requests', 9 ); |
| 124 |