Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 134
0.00% covered (danger)
0.00%
0 / 10
CRAP
n/a
0 / 0
wpcom_normalize_site_logo_id
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
30
wpcom_site_logo_edit_link
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
42
wpcom_site_logo_edit_description
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
12
wpcom_fiverr_logo
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
wpcom_fiverr_cta_button
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
wpcom_fiverr_cta
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 1
42
wpcom_fiverr
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
wpcom_site_management_panel_link
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
wpcom_site_management_panel
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
wpcom_enqueue_options_general_assets
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Override /wp-admin/options-general.php
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8use Automattic\Jetpack\Jetpack_Mu_Wpcom;
9
10/**
11 * Normalize a site logo value to a positive attachment ID.
12 *
13 * The value may hold an attachment ID, a legacy `array( 'id' => ... )`, or a WP_Error,
14 * so anything that is not a positive scalar resolves to 0.
15 *
16 * @param mixed $value The raw site_logo option or custom_logo theme mod value.
17 * @return int A positive attachment ID, or 0 when none is usable.
18 */
19function wpcom_normalize_site_logo_id( $value ) {
20    if ( is_array( $value ) && isset( $value['id'] ) ) {
21        $value = $value['id'];
22    }
23    return ( is_scalar( $value ) && (int) $value > 0 ) ? (int) $value : 0;
24}
25
26/**
27 * Resolve the URL for editing the current site logo.
28 *
29 * Block themes use the Site Editor's Identity screen; classic themes use the
30 * Customizer control matching their logo support (mirroring the logo-tool feature).
31 * The URL is empty when no reliable destination exists, so callers can omit the link.
32 *
33 * @return array{url:string, is_block:bool} The edit URL and block-theme flag.
34 */
35function wpcom_site_logo_edit_link() {
36    if ( wp_is_block_theme() ) {
37        // The Identity screen shipped in Gutenberg 22.8 / WordPress 7.1; omit the
38        // link when it is unavailable.
39        $has_identity = defined( 'GUTENBERG_VERSION' )
40            ? version_compare( GUTENBERG_VERSION, '22.8', '>=' )
41            : version_compare( get_bloginfo( 'version' ), '7.1', '>=' );
42
43        return array(
44            'url'      => $has_identity ? admin_url( 'site-editor.php?p=%2Fidentity' ) : '',
45            'is_block' => true,
46        );
47    }
48
49    if ( current_theme_supports( 'custom-logo' ) ) {
50        $url = admin_url( 'customize.php?autofocus[control]=custom_logo' );
51    } elseif ( current_theme_supports( 'site-logo' ) ) {
52        $url = admin_url( 'customize.php?autofocus[control]=site_logo' );
53    } else {
54        $url = '';
55    }
56
57    return array(
58        'url'      => $url,
59        'is_block' => false,
60    );
61}
62
63/**
64 * The description pointing at where the site logo can be set.
65 *
66 * Rendered whether or not a logo exists, so the empty state still offers a way
67 * to upload one. That is the case where someone is most likely looking for it.
68 * The wording says "set" so it reads correctly for both adding and replacing.
69 */
70function wpcom_site_logo_edit_description() {
71    $logo_edit_link = wpcom_site_logo_edit_link();
72
73    // Only promise an edit path when a known destination exists.
74    if ( ! $logo_edit_link['url'] ) {
75        return;
76    }
77    ?>
78    <p class="description wpcom-site-logo-description">
79        <?php
80        if ( $logo_edit_link['is_block'] ) {
81            printf(
82                /* translators: %1$s: opening link tag to the Site Editor, %2$s: closing link tag. */
83                esc_html__( 'Displays in your site\'s layout via the Site Logo block. %1$sYou can set your site logo in the site editor%2$s.', 'jetpack-mu-wpcom' ),
84                '<a href="' . esc_url( $logo_edit_link['url'] ) . '">',
85                '</a>'
86            );
87        } else {
88            printf(
89                /* translators: %1$s: opening link tag to the Customizer, %2$s: closing link tag. */
90                esc_html__( '%1$sYou can set your site logo in the Customizer%2$s.', 'jetpack-mu-wpcom' ),
91                '<a href="' . esc_url( $logo_edit_link['url'] ) . '">',
92                '</a>'
93            );
94        }
95        ?>
96    </p>
97    <?php
98}
99
100/**
101 * The Fiverr logo mark.
102 *
103 * Decorative in every current use, since the adjacent text always names Fiverr.
104 */
105function wpcom_fiverr_logo() {
106    ?>
107    <svg class="wpcom-fiverr-cta-logo" width="20" height="20" viewBox="0 0 500 500" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false">
108        <circle cx="250" cy="250" r="177" fill="white"/>
109        <path d="M500 250C500 111.93 388.07 0 250 0C111.93 0 0 111.93 0 250C0 388.07 111.93 500 250 500C388.07 500 500 388.07 500 250ZM360.42 382.5H294.77V237.2H231.94V382.5H165.9V237.2H128.45V183.45H165.9V167.13C165.9 124.54 198.12 95.48 246.05 95.48H294.78V149.22H256.93C241.62 149.22 231.95 157.58 231.95 171.12V183.45H360.43V382.5H360.42Z" fill="#1DBF73"/>
110    </svg>
111    <?php
112}
113
114/**
115 * The Fiverr logo-maker CTA button's DOM.
116 */
117function wpcom_fiverr_cta_button() {
118    ?>
119    <button class="wpcom-fiverr-cta-button button" type="button">
120        <?php wpcom_fiverr_logo(); ?>
121        <?php esc_html_e( 'Try Fiverr Logo Maker', 'jetpack-mu-wpcom' ); ?>
122    </button>
123    <?php
124}
125
126/**
127 * The fiverr cta's DOM.
128 */
129function wpcom_fiverr_cta() {
130    // Core keeps the site_logo option and custom_logo theme mod in sync; read either.
131    $logo_id = wpcom_normalize_site_logo_id( get_option( 'site_logo' ) );
132    if ( ! $logo_id ) {
133        $logo_id = wpcom_normalize_site_logo_id( get_theme_mod( 'custom_logo' ) );
134    }
135
136    $logo_img = '';
137    if ( $logo_id ) {
138        // Fall back to the site title when the logo has no alt text, so the preview
139        // is still announced to assistive technology.
140        $logo_alt = trim( wp_strip_all_tags( (string) get_post_meta( $logo_id, '_wp_attachment_image_alt', true ) ) );
141        if ( '' === $logo_alt ) {
142            $site_name = get_bloginfo( 'name' );
143            $logo_alt  = $site_name
144                /* translators: %s: The site title. */
145                ? sprintf( __( 'Current site logo for %s', 'jetpack-mu-wpcom' ), $site_name )
146                : __( 'Current site logo', 'jetpack-mu-wpcom' );
147        }
148        // Request a proportional size, not a square, so wide logos aren't shown as a
149        // center-cropped thumbnail; CSS caps the dimensions.
150        $logo_img = wp_get_attachment_image(
151            $logo_id,
152            'medium',
153            false,
154            array(
155                'class' => 'wpcom-site-logo-preview-image',
156                'alt'   => $logo_alt,
157            )
158        );
159    }
160    ?>
161    <div id="wpcom-fiverr-cta">
162        <?php if ( $logo_img ) : ?>
163            <div class="wpcom-site-logo">
164                <div class="wpcom-site-logo-preview">
165                    <?php echo $logo_img; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_get_attachment_image() returns escaped markup. ?>
166                </div>
167                <?php wpcom_fiverr_cta_button(); ?>
168            </div>
169        <?php else : ?>
170            <div class="wpcom-fiverr-cta-box">
171                <?php wpcom_fiverr_logo(); ?>
172                <div class="wpcom-fiverr-cta-description">
173                    <p>
174                        <?php
175                        printf(
176                            /* translators: %1$s: opening link tag to the Fiverr logo maker, %2$s: closing link tag. */
177                            esc_html__( 'No logo yet? Make one in minutes with %1$sFiverr Logo Maker%2$s.', 'jetpack-mu-wpcom' ),
178                            '<a class="wpcom-fiverr-cta-link" href="' . esc_url( 'https://wp.me/logo-maker/?utm_campaign=general_settings' ) . '" target="_blank" rel="noopener noreferrer">',
179                            '</a>'
180                        );
181                        ?>
182                    </p>
183                    <p><?php esc_html_e( 'Pre-designed by top talent. Just add your touch.', 'jetpack-mu-wpcom' ); ?></p>
184                </div>
185            </div>
186        <?php endif; ?>
187    </div>
188    <?php
189    // Sits outside the Fiverr CTA: it describes the Site Logo setting itself,
190    // not the upsell.
191    wpcom_site_logo_edit_description();
192}
193
194/**
195 * Add the fiverr cta to the general settings page.
196 */
197function wpcom_fiverr() {
198    add_settings_field( 'wpcom_fiverr_cta', __( 'Site Logo', 'jetpack-mu-wpcom' ), 'wpcom_fiverr_cta', 'general', 'default' );
199}
200add_action( 'load-options-general.php', 'wpcom_fiverr' );
201
202/**
203 * DOM for the link to the Site Management Panel on WordPress.com.
204 */
205function wpcom_site_management_panel_link() {
206    ?>
207    <a href="https://wordpress.com/sites/<?php echo esc_attr( wpcom_get_site_slug() ); ?>/settings">
208        <?php esc_html_e( 'Manage gift subscriptions, ownership, and other site tools on WordPress.com ↗', 'jetpack-mu-wpcom' ); ?>
209    </a>
210    <?php
211}
212
213/**
214 * Add the link to the Site Management Panel on WordPress.com to the general settings page.
215 */
216function wpcom_site_management_panel() {
217    $current_screen = wpcom_admin_get_current_screen();
218    if ( in_array( $current_screen, WPCOM_DUPLICATED_VIEW, true ) ) {
219        add_settings_field( 'wpcom_site_management_panel_link', __( 'WordPress.com Site Settings', 'jetpack-mu-wpcom' ), 'wpcom_site_management_panel_link', 'general' );
220    }
221}
222add_action( 'load-options-general.php', 'wpcom_site_management_panel' );
223
224/**
225 * Display the site URL in General Settings on Simple Classic sites.
226 */
227function wpcom_enqueue_options_general_assets() {
228    $asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-options-general/wpcom-options-general.asset.php';
229
230    wp_enqueue_style(
231        'wpcom-options-general',
232        plugins_url( 'build/wpcom-options-general/wpcom-options-general.css', Jetpack_Mu_Wpcom::BASE_FILE ),
233        array(),
234        $asset_file['version'] ?? filemtime( Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-options-general/wpcom-options-general.css' )
235    );
236
237    wp_enqueue_script(
238        'wpcom-options-general',
239        plugins_url( 'build/wpcom-options-general/wpcom-options-general.js', Jetpack_Mu_Wpcom::BASE_FILE ),
240        $asset_file['dependencies'] ?? array(),
241        $asset_file['version'] ?? filemtime( Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-options-general/wpcom-options-general.js' ),
242        true
243    );
244    wp_set_script_translations( 'wpcom-options-general', 'jetpack-mu-wpcom' );
245    $site_slug           = wpcom_get_site_slug();
246    $options_general_url = admin_url( 'options-general.php' );
247    wp_add_inline_script(
248        'wpcom-options-general',
249        'window.wpcomSiteUrl = ' . wp_json_encode(
250            array(
251                'siteUrl'           => get_site_url(),
252                'homeUrl'           => home_url(),
253                'siteSlug'          => $site_slug,
254                'optionsGeneralUrl' => $options_general_url,
255            ),
256            JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP
257        ) . ';',
258        'before'
259    );
260}
261add_action( 'admin_enqueue_scripts', 'wpcom_enqueue_options_general_assets' );