Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Section_State
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
3 / 3
10
100.00% covered (success)
100.00%
1 / 1
 for_section
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
5
 configures
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 shows_placement
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Resolves which variant a Settings > Sharing section should render.
4 *
5 * @package automattic/jetpack-sharing-likes
6 */
7
8declare( strict_types = 1 );
9
10namespace Automattic\Jetpack\Sharing_Likes\Settings;
11
12/**
13 * Which variant a Settings > Sharing section renders, and whether the shared
14 * placement section renders at all.
15 *
16 * Kept free of WordPress calls so every combination can be asserted directly.
17 * Callers read the environment and pass the result in.
18 */
19final class Section_State {
20
21    /**
22     * The feature's own configuration options.
23     */
24    public const CONFIGURE = 'configure';
25
26    /**
27     * Configuration options, plus a recommendation to use the block instead.
28     *
29     * The legacy output is still on the site, so the options stay usable.
30     */
31    public const CONFIGURE_WITH_BLOCK_NUDGE = 'configure_with_block_nudge';
32
33    /**
34     * No options: an invitation to add the block, and a link to the Site Editor.
35     */
36    public const BLOCK_CALL_TO_ACTION = 'block_call_to_action';
37
38    /**
39     * No options: a note that the feature is off.
40     */
41    public const OFF = 'off';
42
43    /**
44     * Resolve the variant for one section.
45     *
46     * Both sections follow the same rule; only their contents differ.
47     *
48     * @param bool $can_offer_block Whether the block is a route this site can be sent down.
49     *                              A block theme is necessary but not sufficient, which is why
50     *                              callers resolve this rather than passing the theme type.
51     * @param bool $feature_running Whether the legacy feature can still produce output.
52     *                              Always true on WordPress.com Simple, which has no modules.
53     * @param bool $switched_off    Whether the feature's own settings leave it nothing to show,
54     *                              which is how Simple switches to the block.
55     * @return string One of the class constants.
56     */
57    public static function for_section( bool $can_offer_block, bool $feature_running, bool $switched_off = false ): string {
58        // Only with a block to move to: elsewhere the options are the one way back.
59        if ( $can_offer_block ) {
60            return $feature_running && ! $switched_off ? self::CONFIGURE_WITH_BLOCK_NUDGE : self::BLOCK_CALL_TO_ACTION;
61        }
62
63        return $feature_running ? self::CONFIGURE : self::OFF;
64    }
65
66    /**
67     * Whether a variant shows the feature's own options.
68     *
69     * @param string $state One of the class constants.
70     */
71    public static function configures( string $state ): bool {
72        return self::CONFIGURE === $state || self::CONFIGURE_WITH_BLOCK_NUDGE === $state;
73    }
74
75    /**
76     * Whether the shared "Show buttons on" section renders.
77     *
78     * It only governs legacy output, so it is hidden once nothing on the site reads it.
79     *
80     * @param string $sharing_state        Variant the Sharing buttons section renders.
81     * @param string $likes_state          Variant the Like buttons section renders.
82     * @param bool   $comment_likes_follow Whether Comment Likes read the placement too.
83     * @return bool
84     */
85    public static function shows_placement( string $sharing_state, string $likes_state, bool $comment_likes_follow = false ): bool {
86        return self::configures( $sharing_state ) || self::configures( $likes_state ) || $comment_likes_follow;
87    }
88}