Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
Automattic\Jetpack\Extensions\Sharing\register_plugins
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Block Editor - Sharing feature.
4 *
5 * @package automattic/jetpack
6 */
7
8namespace Automattic\Jetpack\Extensions\Sharing;
9
10use Automattic\Jetpack\Modules;
11use Jetpack_Gutenberg;
12
13if ( ! defined( 'ABSPATH' ) ) {
14    exit( 0 );
15}
16
17/**
18 * Register Sharing plugin.
19 *
20 * @return void
21 */
22function 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     * 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
33    Jetpack_Gutenberg::set_extension_available( 'sharing' );
34}
35
36add_action( 'jetpack_register_gutenberg_extensions', __NAMESPACE__ . '\register_plugins' );
37
38/**
39 * The Sharing panel is only displayed for post types that support sharing.
40 * The sharing module declares support for sharing for all the public post types.
41 * Let's do the same thing when the module isn't active yet.
42 */
43add_action(
44    'rest_api_init',
45    function () {
46        if ( ! ( new Modules() )->is_active( 'sharedaddy' ) ) {
47            $post_types = get_post_types( array( 'public' => true ) );
48
49            foreach ( $post_types as $post_type ) {
50                register_rest_field(
51                    $post_type,
52                    'jetpack_sharing_enabled',
53                    array(
54                        'get_callback' => function ( array $post ) {
55                            if ( ! isset( $post['id'] ) ) {
56                                return false;
57                            }
58
59                            return ! get_post_meta( $post['id'], 'sharing_disabled', true );
60                        },
61                        'schema'       => array(
62                            'description' => __( 'Are sharing buttons enabled?', 'jetpack' ),
63                            'type'        => 'boolean',
64                        ),
65                    )
66                );
67                add_post_type_support( $post_type, 'jetpack-sharing-buttons' );
68            }
69        }
70    }
71);