Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Feature_Actions
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 switch_to_block
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 activate
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 remove_all_sharing_services
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * The "Switch to the … block" and "Turn on" actions on Settings > Sharing.
4 *
5 * @package automattic/jetpack-sharing-likes
6 */
7
8declare( strict_types = 1 );
9
10namespace Automattic\Jetpack\Sharing_Likes\Settings;
11
12use Automattic\Jetpack\Modules;
13use Automattic\Jetpack\Sharing_Likes\Hooked_Blocks\Sharing_Buttons_Markup;
14
15/**
16 * Actions that change which variant a feature section renders, rather than a setting.
17 *
18 * Callers check authorization, and whether the section offers the action.
19 */
20final class Feature_Actions {
21
22    /**
23     * Module each feature's legacy output comes from.
24     */
25    private const MODULES = array(
26        Placement_Section::FEATURE_SHARING => 'sharedaddy',
27        Placement_Section::FEATURE_LIKES   => 'likes',
28    );
29
30    /**
31     * Stop producing a feature's legacy buttons, so the block can take over.
32     *
33     * A migration, not the section's off switch: it leaves the block itself untouched.
34     * Simple has no modules, so it switches the feature off through settings instead.
35     *
36     * @param string $feature One of the `Placement_Section::FEATURE_*` constants.
37     */
38    public static function switch_to_block( string $feature ): void {
39        if ( ! Environment::is_simple_site() ) {
40            ( new Modules() )->deactivate( self::MODULES[ $feature ] );
41            return;
42        }
43
44        if ( Placement_Section::FEATURE_SHARING === $feature ) {
45            self::remove_all_sharing_services();
46            return;
47        }
48
49        // The legacy widget renders for either button. Posts that opted in individually keep
50        // their buttons, and Comment Likes has no block to move to, so it is left alone.
51        Likes_Options::set_likes_enabled( false );
52        Likes_Options::set_reblogs_enabled( false );
53    }
54
55    /**
56     * Turn a feature's module back on.
57     *
58     * Only for the OFF variant, where no block route exists and nothing else on
59     * the site will bring the feature back.
60     *
61     * @param string $feature One of the `Placement_Section::FEATURE_*` constants.
62     * @return bool Whether the module is now active.
63     */
64    public static function activate( string $feature ): bool {
65        $modules = new Modules();
66        $modules->activate( self::MODULES[ $feature ], false, false );
67
68        // A host can force the module off, and `activate()` still saves it as active.
69        return $modules->is_active( self::MODULES[ $feature ] );
70    }
71
72    /**
73     * Leave sharedaddy no services to render, keeping a copy for the block's buttons.
74     */
75    private static function remove_all_sharing_services(): void {
76        Sharing_Buttons_Markup::remember_legacy_services();
77
78        // Preferred over writing the option, because wpcom hooks the state change it announces.
79        if ( class_exists( 'Sharing_Service' ) ) {
80            ( new \Sharing_Service() )->set_blog_services( array(), array() );
81            return;
82        }
83
84        update_option(
85            'sharing-services',
86            array(
87                'visible' => array(),
88                'hidden'  => array(),
89            )
90        );
91    }
92}