sharing-buttons.php
69 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_Buttons; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | /** |
| 14 | * Registers the block for use in Gutenberg |
| 15 | * This is done via an action so that we can disable |
| 16 | * registration if we need to. |
| 17 | */ |
| 18 | function register_block() { |
| 19 | Blocks::jetpack_register_block( |
| 20 | __DIR__, |
| 21 | array( 'render_callback' => __NAMESPACE__ . '\render_block' ) |
| 22 | ); |
| 23 | } |
| 24 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 25 | |
| 26 | /** |
| 27 | * Sharing Buttons block registration/dependency declaration. |
| 28 | * |
| 29 | * @param array $attr Array containing the Sharing Buttons block attributes. |
| 30 | * @param string $content String containing the Sharing Buttons block content. |
| 31 | * |
| 32 | * @return string |
| 33 | */ |
| 34 | function render_block( $attr, $content ) { |
| 35 | // Render nothing in other contexts than frontend (i.e. feed, emails, API, etc.). |
| 36 | if ( ! jetpack_is_frontend() ) { |
| 37 | return ''; |
| 38 | } |
| 39 | return $content; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Add the services list to the block |
| 44 | */ |
| 45 | function add_sharing_buttons_block_data() { |
| 46 | $services = array( |
| 47 | 'print', |
| 48 | 'facebook', |
| 49 | 'linkedin', |
| 50 | 'mail', |
| 51 | 'mastodon', |
| 52 | 'pinterest', |
| 53 | 'pocket', |
| 54 | 'reddit', |
| 55 | 'telegram', |
| 56 | 'tumblr', |
| 57 | 'whatsapp', |
| 58 | 'x', |
| 59 | 'nextdoor', |
| 60 | ); |
| 61 | |
| 62 | wp_add_inline_script( |
| 63 | 'jetpack-block-sharing-button', |
| 64 | 'var jetpack_sharing_buttons_services = ' . wp_json_encode( $services, JSON_HEX_TAG | JSON_HEX_AMP ) . ';', |
| 65 | 'before' |
| 66 | ); |
| 67 | } |
| 68 | add_action( 'enqueue_block_assets', __NAMESPACE__ . '\add_sharing_buttons_block_data' ); |
| 69 |