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