sharing-button.php
53 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; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Jetpack_Gutenberg; |
| 14 | |
| 15 | require_once JETPACK__PLUGIN_DIR . 'modules/sharedaddy/sharing-sources.php'; |
| 16 | |
| 17 | /** |
| 18 | * Registers the block for use in Gutenberg |
| 19 | * This is done via an action so that we can disable |
| 20 | * registration if we need to. |
| 21 | */ |
| 22 | function register_block() { |
| 23 | Blocks::jetpack_register_block( |
| 24 | __DIR__, |
| 25 | array( 'render_callback' => __NAMESPACE__ . '\render_block' ) |
| 26 | ); |
| 27 | } |
| 28 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 29 | |
| 30 | /** |
| 31 | * Sharing Buttons block registration/dependency declaration. |
| 32 | * |
| 33 | * @param array $attr Array containing the Sharing Buttons block attributes. |
| 34 | * @param string $content String containing the Sharing Buttons block content. |
| 35 | * @param array $block Array containing block data. |
| 36 | * |
| 37 | * @return string |
| 38 | */ |
| 39 | function render_block( $attr, $content, $block ) { |
| 40 | $post_id = $block->context['postId']; |
| 41 | |
| 42 | $style_type = $block->context['styleType']; |
| 43 | $style = 'style-' . $style_type; |
| 44 | $data_shared = 'sharing-' . $attr['service'] . '-' . $post_id . $attr['service']; |
| 45 | $link_url = get_permalink( $post_id ) . '?share=' . $attr['service'] . '&nb=1'; |
| 46 | |
| 47 | $content = str_replace( 'url_replaced_in_runtime', $link_url, $content ); |
| 48 | $content = str_replace( 'style_button_replace_at_runtime', $style, $content ); |
| 49 | $content = str_replace( 'data-shared_replaced_in_runtime', $data_shared, $content ); |
| 50 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 51 | return $content; |
| 52 | } |
| 53 |