Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Global_Styles_Fonts_Message_Control
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 6
240
0.00% covered (danger)
0.00%
0 / 1
 render_content
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 render_intro_text
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 render_learn_more_link
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 maybe_render_block_editor_link
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 get_link_to_editor_with_global_styles_sidebar
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
72
 get_site_slug
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * A WP_Customize_Control for giving instructions on how to edit your fonts from within the Global Styles plugin.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\Global_Styles;
9
10/**
11 * Class Global_Styles_Fonts_Message_Control
12 */
13class Global_Styles_Fonts_Message_Control extends \WP_Customize_Control {
14    /**
15     * Render the customizer help document content
16     */
17    public function render_content() {
18        $this->render_intro_text();
19        $this->maybe_render_block_editor_link();
20        $this->render_learn_more_link();
21    }
22
23    /**
24     * Render the intro text to the customizer help document content
25     */
26    private function render_intro_text() {
27        $intro_text = __( 'You can change your fonts using Global Styles, which can be found in the Block Editor.', 'jetpack-mu-wpcom' );
28        ?>
29            <p>
30                <?php echo esc_html( $intro_text ); ?>
31            </p>
32        <?php
33    }
34
35    /**
36     * Render a link to the global styles fonts page help support document
37     */
38    private function render_learn_more_link() {
39        // Translators: This is a link which opens: https://wordpress.com/support/custom-fonts/#changing-fonts-with-global-styles.
40        $learn_more_link_text = __( 'Learn more about changing fonts using Global Styles.', 'jetpack-mu-wpcom' );
41        ?>
42            <p>
43                <a
44                    id="customizer_global_styles_support_link"
45                    href="https://wordpress.com/support/custom-fonts/#changing-fonts-with-global-styles"
46                    target="_blank"
47                >
48                    <?php echo esc_html( $learn_more_link_text ); ?>
49                </a>
50            </p>
51        <?php
52    }
53
54    /**
55     * Render a link to the global styles section of the block editor, if a homepage exists
56     */
57    private function maybe_render_block_editor_link() {
58        $block_editor_with_global_styles_url = $this->get_link_to_editor_with_global_styles_sidebar();
59        if ( null === $block_editor_with_global_styles_url ) {
60            return;
61        }
62        // Translators: This is a link which opens the block editor, and then opens the global styles sidebar.
63        $block_editor_link_text = __( 'Click here to open the Block Editor and change your fonts.', 'jetpack-mu-wpcom' );
64        ?>
65            <p>
66                <a
67                    id="customizer_global_styles_block_editor_link"
68                    href="<?php echo esc_url( $block_editor_with_global_styles_url ); ?>"
69                    target="_blank">
70                    <?php echo esc_html( $block_editor_link_text ); ?>
71                </a>
72            </p>
73        <?php
74    }
75
76    /**
77     * Creates a link to the page editor for the user's homepage with the global styles sidebar opened.
78     */
79    private function get_link_to_editor_with_global_styles_sidebar() {
80        $homepage_id = \get_option( 'page_on_front' );
81        if ( empty( $homepage_id ) ) {
82            return null;
83        }
84
85        $base_url = null;
86        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
87        if ( isset( $_GET['calypsoOrigin'] ) && 'http://calypso.localhost:3000' === $_GET['calypsoOrigin'] ) {
88            $base_url = 'http://calypso.localhost:3000/';
89            // phpcs:ignore WordPress.Security.NonceVerification.Recommended
90        } elseif ( isset( $_GET['calypsoOrigin'] ) && 'https://horizon.wordpress.com' === $_GET['calypsoOrigin'] ) {
91            $base_url = 'https://horizon.wordpress.com/';
92            // phpcs:ignore WordPress.Security.NonceVerification.Recommended
93        } elseif ( isset( $_GET['calypsoOrigin'] ) && 'https://wpcalypso.wordpress.com' === $_GET['calypsoOrigin'] ) {
94            $base_url = 'https://wpcalypso.wordpress.com/';
95        } else {
96            $base_url = 'https://www.wordpress.com/';
97        }
98
99        $site_slug = $this->get_site_slug();
100
101        $url_components = array(
102            $base_url,
103            'page/',
104            $site_slug . '/',
105            $homepage_id,
106            '?openSidebar=global-styles',
107        );
108
109        return implode( '', $url_components );
110    }
111
112    /**
113     * Get the site slug
114     */
115    private function get_site_slug() {
116        if ( method_exists( '\WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
117            return \WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
118        }
119
120        $home_url  = home_url( '/' );
121        $site_slug = wp_parse_url( $home_url, PHP_URL_HOST );
122
123        return $site_slug;
124    }
125}