Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Post_Sharing_Switch
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 init
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 register_rest_field
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
 get_value
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 update_value
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * The per-post Sharing switch in the REST API.
4 *
5 * @package automattic/jetpack-sharing-likes
6 */
7
8declare( strict_types = 1 );
9
10namespace Automattic\Jetpack\Sharing_Likes;
11
12/**
13 * Whether sharing buttons show on a given post, as a REST field on every public post type.
14 *
15 * A post can only opt out: without the meta it follows the sitewide setting, and it
16 * cannot turn sharing on where that setting has it off.
17 */
18final class Post_Sharing_Switch {
19
20    /**
21     * REST field the block editor reads and writes.
22     */
23    public const FIELD = 'jetpack_sharing_enabled';
24
25    /**
26     * Post meta that, when set, turns sharing buttons off on the post.
27     */
28    public const META_KEY = 'sharing_disabled';
29
30    /**
31     * Post type support the block editor checks before offering the switch.
32     */
33    public const POST_TYPE_SUPPORT = 'jetpack-sharing-buttons';
34
35    /**
36     * Register the field whenever the REST API starts. Safe to call more than once.
37     */
38    public static function init(): void {
39        add_action( 'rest_api_init', array( __CLASS__, 'register_rest_field' ) );
40        // Why this hook, at 20: see Post_Likes_Switch::init().
41        add_action( 'restapi_theme_init', array( __CLASS__, 'register_rest_field' ), 20 );
42    }
43
44    /**
45     * Add the field and the post type support to every public post type.
46     */
47    public static function register_rest_field(): void {
48        foreach ( get_post_types( array( 'public' => true ) ) as $post_type ) {
49            register_rest_field(
50                $post_type,
51                self::FIELD,
52                array(
53                    'get_callback'    => array( __CLASS__, 'get_value' ),
54                    'update_callback' => array( __CLASS__, 'update_value' ),
55                    'schema'          => array(
56                        'description' => __( 'Are sharing buttons enabled?', 'jetpack-sharing-likes' ),
57                        'type'        => 'boolean',
58                    ),
59                )
60            );
61
62            add_post_type_support( $post_type, self::POST_TYPE_SUPPORT );
63        }
64    }
65
66    /**
67     * Whether sharing buttons may show on the post.
68     *
69     * @param array $post Post data as the REST API prepared it.
70     */
71    public static function get_value( array $post ): bool {
72        if ( ! isset( $post['id'] ) ) {
73            return false;
74        }
75
76        return ! get_post_meta( $post['id'], self::META_KEY, true );
77    }
78
79    /**
80     * Clear the opt-out when enabling, set it when disabling.
81     *
82     * @param bool     $enable      Whether sharing buttons should show on the post.
83     * @param \WP_Post $post_object The post being updated.
84     *
85     * @return int|bool What `update_post_meta()` or `delete_post_meta()` returned.
86     */
87    public static function update_value( $enable, $post_object ) {
88        if ( $enable ) {
89            return delete_post_meta( $post_object->ID, self::META_KEY );
90        }
91
92        return update_post_meta( $post_object->ID, self::META_KEY, true );
93    }
94}