sharing.php
| 1 | <?php |
| 2 | /** |
| 3 | * Block Editor - Sharing feature. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Extensions\Sharing; |
| 9 | |
| 10 | use Automattic\Jetpack\Modules; |
| 11 | use Jetpack_Gutenberg; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit( 0 ); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Register Sharing plugin. |
| 19 | * |
| 20 | * @return void |
| 21 | */ |
| 22 | function register_plugins() { |
| 23 | /* |
| 24 | * The extension is available even when the module is not active, |
| 25 | * so we can display a nudge to activate the module instead of the block. |
| 26 | * However, since non-admins cannot activate modules, we do not display the empty block for them. |
| 27 | */ |
| 28 | if ( ! ( new Modules() )->is_active( 'sharedaddy' ) && ! current_user_can( 'jetpack_activate_modules' ) ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | Jetpack_Gutenberg::set_extension_available( 'sharing' ); |
| 33 | } |
| 34 | |
| 35 | add_action( 'jetpack_register_gutenberg_extensions', __NAMESPACE__ . '\register_plugins' ); |
| 36 | |
| 37 | /** |
| 38 | * The Sharing panel is only displayed for post types that support sharing. |
| 39 | * The sharing module declares support for sharing for all the public post types. |
| 40 | * Let's do the same thing when the module isn't active yet. |
| 41 | */ |
| 42 | add_action( |
| 43 | 'rest_api_init', |
| 44 | function () { |
| 45 | if ( ! ( new Modules() )->is_active( 'sharedaddy' ) ) { |
| 46 | $post_types = get_post_types( array( 'public' => true ) ); |
| 47 | |
| 48 | foreach ( $post_types as $post_type ) { |
| 49 | register_rest_field( |
| 50 | $post_type, |
| 51 | 'jetpack_sharing_enabled', |
| 52 | array( |
| 53 | 'get_callback' => function ( array $post ) { |
| 54 | if ( ! isset( $post['id'] ) ) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | return ! get_post_meta( $post['id'], 'sharing_disabled', true ); |
| 59 | }, |
| 60 | 'schema' => array( |
| 61 | 'description' => __( 'Are sharing buttons enabled?', 'jetpack' ), |
| 62 | 'type' => 'boolean', |
| 63 | ), |
| 64 | ) |
| 65 | ); |
| 66 | add_post_type_support( $post_type, 'jetpack-sharing-buttons' ); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | ); |
| 71 |