| @@ -6,10 +6,15 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack\Extensions\Sharing; |
| 9 | 9 | |
| 10 | +use Automattic\Jetpack\Modules; | |
| 10 | 11 | use Jetpack_Gutenberg; |
| 11 | 12 | |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 14 | + exit( 0 ); | |
| 15 | +} | |
| 16 | + | |
| 12 | 17 | /** |
| 13 | 18 | * Register Sharing plugin. |
| 14 | 19 | * |
| 15 | 20 | * @return void |
| @@ -14,9 +19,18 @@ | ||
| 14 | 19 | * |
| 15 | 20 | * @return void |
| 16 | 21 | */ |
| 17 | 22 | function register_plugins() { |
| 18 | - // Register Sharing. | |
| 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 | + * We skip that nudge for non-admins, who cannot activate modules, and on block themes, | |
| 27 | + * where the answer is the Sharing Buttons block in a template rather than the legacy module. | |
| 28 | + */ | |
| 29 | + if ( ! ( new Modules() )->is_active( 'sharedaddy' ) && ( ! current_user_can( 'jetpack_activate_modules' ) || wp_is_block_theme() ) ) { | |
| 30 | + return; | |
| 31 | + } | |
| 32 | + | |
| 19 | 33 | Jetpack_Gutenberg::set_extension_available( 'sharing' ); |
| 20 | 34 | } |
| 21 | 35 | |
| 22 | 36 | add_action( 'jetpack_register_gutenberg_extensions', __NAMESPACE__ . '\register_plugins' ); |
| @@ -28,9 +42,9 @@ | ||
| 28 | 42 | */ |
| 29 | 43 | add_action( |
| 30 | 44 | 'rest_api_init', |
| 31 | 45 | function () { |
| 32 | - if ( ! \Jetpack::is_module_active( 'sharedaddy' ) ) { | |
| 46 | + if ( ! ( new Modules() )->is_active( 'sharedaddy' ) ) { | |
| 33 | 47 | $post_types = get_post_types( array( 'public' => true ) ); |
| 34 | 48 | |
| 35 | 49 | foreach ( $post_types as $post_type ) { |
| 36 | 50 | register_rest_field( |
| @@ -37,9 +51,13 @@ | ||
| 37 | 51 | $post_type, |
| 38 | 52 | 'jetpack_sharing_enabled', |
| 39 | 53 | array( |
| 40 | 54 | 'get_callback' => function ( array $post ) { |
| 41 | - return (bool) ! get_post_meta( $post['id'], 'sharing_disabled', true ); | |
| 55 | + if ( ! isset( $post['id'] ) ) { | |
| 56 | + return false; | |
| 57 | + } | |
| 58 | + | |
| 59 | + return ! get_post_meta( $post['id'], 'sharing_disabled', true ); | |
| 42 | 60 | }, |
| 43 | 61 | 'schema' => array( |
| 44 | 62 | 'description' => __( 'Are sharing buttons enabled?', 'jetpack' ), |
| 45 | 63 | 'type' => 'boolean', |