Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
61.29% covered (warning)
61.29%
19 / 31
50.00% covered (danger)
50.00%
9 / 18
CRAP
0.00% covered (danger)
0.00%
0 / 1
Environment
61.29% covered (warning)
61.29%
19 / 31
50.00% covered (danger)
50.00%
9 / 18
96.17
0.00% covered (danger)
0.00%
0 / 1
 is_block_theme
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_simple_site
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sharing_module_running
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 legacy_sharing_switched_off
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 legacy_sharing_supported
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
 settings_screen_supported
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 likes_supported
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_connected
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 likes_module_running
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 legacy_likes_switched_off
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 comment_likes_module_running
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 likes_settings_in_use
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 twitter_site_tag_used
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 module_active
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 sharing_block_registered
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 like_block_registered
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 single_template_editor_url
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
5.02
 block_is_registered
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Reads the site state that Settings > Sharing branches on.
4 *
5 * @package automattic/jetpack-sharing-likes
6 */
7
8declare( strict_types = 1 );
9
10namespace Automattic\Jetpack\Sharing_Likes\Settings;
11
12use Automattic\Jetpack\Connection\Manager as Connection_Manager;
13use Automattic\Jetpack\Modules;
14use Automattic\Jetpack\Status;
15use Automattic\Jetpack\Status\Host;
16
17/**
18 * The facts Settings > Sharing branches on, read in one place so the sections
19 * and the state resolver stay free of environment lookups.
20 */
21final class Environment {
22
23    /**
24     * Memoised Site Editor URL, since resolving the template hits the theme's files.
25     *
26     * @var string|null
27     */
28    private static $single_template_editor_url = null;
29
30    /**
31     * Whether the active theme is a block theme.
32     */
33    public static function is_block_theme(): bool {
34        return wp_is_block_theme();
35    }
36
37    /**
38     * Whether this is a WordPress.com Simple site.
39     */
40    public static function is_simple_site(): bool {
41        return ( new Host() )->is_wpcom_simple();
42    }
43
44    /**
45     * Whether sharing buttons can still produce output.
46     */
47    public static function sharing_module_running(): bool {
48        return self::legacy_sharing_supported() && self::module_active( 'sharedaddy' );
49    }
50
51    /**
52     * Whether every sharing service has been removed, so sharedaddy renders nothing.
53     *
54     * An absent option is not that: `Sharing_Service::get_blog_services()` falls
55     * back to its default services when the option was never saved.
56     */
57    public static function legacy_sharing_switched_off(): bool {
58        $services = get_option( 'sharing-services' );
59
60        if ( ! is_array( $services ) ) {
61            return false;
62        }
63
64        return empty( $services['visible'] ) && empty( $services['hidden'] );
65    }
66
67    /**
68     * Whether this site can have legacy sharing buttons at all.
69     *
70     * The module needs no connection, but `Jetpack::load_modules()` includes
71     * nothing on a site that is neither connected nor in offline mode, and
72     * `Modules::activate()` refuses there too: an active module renders no
73     * buttons, and offering to turn it on would do nothing. Simple loads
74     * sharedaddy from `post-flair.php` without either.
75     */
76    public static function legacy_sharing_supported(): bool {
77        return self::is_simple_site() || self::is_connected() || ( new Status() )->is_offline_mode();
78    }
79
80    /**
81     * Whether Settings > Sharing has anything to offer this site.
82     *
83     * Neither the legacy features nor their blocks load on a site that is neither
84     * connected nor in offline mode. Checking sharing alone is enough: any site
85     * that can have Like buttons (connected, or Simple) passes that check too.
86     */
87    public static function settings_screen_supported(): bool {
88        return self::legacy_sharing_supported();
89    }
90
91    /**
92     * Whether this site can have Like buttons at all.
93     *
94     * The Likes module declares `Requires Connection: Yes`, so without a
95     * WordPress.com connection it cannot be activated and its buttons cannot
96     * render: the widget is keyed on the blog ID a connection provides.
97     */
98    public static function likes_supported(): bool {
99        return self::is_simple_site() || self::is_connected();
100    }
101
102    /**
103     * Whether the site holds a WordPress.com connection.
104     */
105    private static function is_connected(): bool {
106        return ( new Connection_Manager( 'jetpack' ) )->is_connected();
107    }
108
109    /**
110     * Whether Like buttons can still produce output.
111     *
112     * The module can be left active in the options through a disconnect,
113     * which is why support is checked too.
114     */
115    public static function likes_module_running(): bool {
116        return self::likes_supported() && self::module_active( 'likes' );
117    }
118
119    /**
120     * Whether posts show neither a Like nor a Reblog button, bar those that opted in individually.
121     *
122     * Both count because the legacy widget renders for either.
123     */
124    public static function legacy_likes_switched_off(): bool {
125        return ! Likes_Options::likes_enabled_sitewide() && ! Likes_Options::reblogs_enabled_sitewide();
126    }
127
128    /**
129     * Whether comments can still be liked.
130     *
131     * Comment Likes is its own module, but it reads the Likes settings: both
132     * `disabled_likes` and the shared placement gate `is_likes_visible()`. So a
133     * site running it without the Likes module still needs those controls.
134     */
135    public static function comment_likes_module_running(): bool {
136        return self::likes_supported() && self::module_active( 'comment-likes' );
137    }
138
139    /**
140     * Whether anything on this site still reads the Likes settings.
141     *
142     * Both features are gated on `disabled_likes` and the shared placement, so
143     * either one keeps those controls worth showing. Switching the buttons off
144     * for every post does not end that: posts still opt in one by one, and
145     * Comment Likes reads the same settings either way.
146     */
147    public static function likes_settings_in_use(): bool {
148        return self::likes_module_running() || self::comment_likes_module_running();
149    }
150
151    /**
152     * Whether anything on this site reads the Twitter Site Tag option.
153     *
154     * Twitter Cards read it, and through them the Sharing Buttons block's X `via`.
155     * Simple's Twitter Cards read `twitter_via`, which wpcom serves and saves this option from.
156     */
157    public static function twitter_site_tag_used(): bool {
158        /** This filter is documented in projects/plugins/jetpack/class.jetpack.php */
159        return ! apply_filters( 'jetpack_disable_twitter_cards', false );
160    }
161
162    /**
163     * Whether a module is active.
164     *
165     * Simple has no modules: wpcom loads both features on every request, so
166     * every module counts as active there whatever the option holds.
167     *
168     * @param string $slug Module slug.
169     */
170    private static function module_active( string $slug ): bool {
171        return self::is_simple_site() || ( new Modules() )->is_active( $slug );
172    }
173
174    /**
175     * Whether the Sharing Buttons block is available to offer as an alternative.
176     */
177    public static function sharing_block_registered(): bool {
178        return self::block_is_registered( 'jetpack/sharing-buttons' );
179    }
180
181    /**
182     * Whether the Like block is available to offer as an alternative.
183     */
184    public static function like_block_registered(): bool {
185        return self::block_is_registered( 'jetpack/like' );
186    }
187
188    /**
189     * Site Editor URL for the active theme's single post template.
190     *
191     * Empty when the theme has no `single` template, which callers treat as
192     * "offer no Site Editor link" rather than linking somewhere that 404s: a
193     * block theme shipping only `index.html` is the case that hits this.
194     */
195    public static function single_template_editor_url(): string {
196        if ( null !== self::$single_template_editor_url ) {
197            return self::$single_template_editor_url;
198        }
199
200        $stylesheet = get_stylesheet();
201        $template   = $stylesheet ? $stylesheet . '//single' : '';
202
203        if ( ! $template || ! get_block_template( $template ) ) {
204            self::$single_template_editor_url = '';
205
206            return self::$single_template_editor_url;
207        }
208
209        // `add_query_arg()` does not encode, and the Site Editor router wants `p` percent-encoded.
210        self::$single_template_editor_url = admin_url(
211            'site-editor.php?p=' . rawurlencode( '/wp_template/' . $template ) . '&canvas=edit'
212        );
213
214        return self::$single_template_editor_url;
215    }
216
217    /**
218     * Whether a block is registered, and so available to add to a template.
219     *
220     * Registration happens on `init`, well before this screen renders.
221     *
222     * @param string $name Fully qualified block name.
223     */
224    private static function block_is_registered( string $name ): bool {
225        return \WP_Block_Type_Registry::get_instance()->is_registered( $name );
226    }
227}