Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
add_logotool_button
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
240
1<?php
2/**
3 * Add a 'Create Logo' button to the Customizer when the theme supports a logo . The button directs customers to Fiverr .
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8if ( ! function_exists( 'add_logotool_button' ) ) {
9    /**
10     * Activate the Site Logo plugin.
11     *
12     * @uses current_theme_supports()
13     *
14     * @param WP_Customize_Manager $wp_customize Control manager for the Customizer.
15     */
16    function add_logotool_button( $wp_customize ) {
17        if ( ( function_exists( 'wpcom_is_vip' ) && wpcom_is_vip() ) || ! is_admin() ) {
18            return;
19        }
20
21        $logo_control = null;
22
23        if ( current_theme_supports( 'custom-logo' ) ) {
24            // WP Core logo integration.
25            $logo_control = $wp_customize->get_control( 'custom_logo' );
26        } elseif ( current_theme_supports( 'site-logo' ) ) {
27            // Jetpack logo integration.
28            $logo_control = $wp_customize->get_control( 'site_logo' );
29        } else {
30            // Other custom logo integrations, for example in the dotorg versions of Lovecraft or Radcliffe themes.
31            foreach ( $wp_customize->controls() as $control ) {
32                if (
33                // Control has the name logo in it.
34                false !== strpos( $control->id, 'logo' ) &&
35                // Control is not a `site_logo` or `custom_logo` (those are handled above).
36                ! in_array( $control->id, array( 'custom_logo', 'site_logo' ), true ) &&
37                // Control is an instance of `WP_Customize_Image_Control` so we know how the UI is rendered to add the button.
38                is_a( $control, 'WP_Customize_Image_Control' )
39                ) {
40                    $logo_control = $control;
41                    break;
42                }
43            }
44        }
45
46        // Make sure we have a valid Customize Control.
47        if ( $logo_control === null || ! is_a( $logo_control, 'WP_Customize_Control' ) ) {
48            return;
49        }
50
51        // And we have a valid setting attached to the control.
52        if ( ! is_a( $logo_control->setting, 'WP_Customize_Setting' ) ) {
53            return;
54        }
55
56        $logo_control->description = __( 'Add a logo to display on your site. No logo? Buy a pro one today, powered by Fiverr — Click “Create logo” to start.', 'jetpack-mu-wpcom' );
57        // Adding it back just overwrites the previous control instance.
58        $wp_customize->add_control( $logo_control );
59
60        add_action(
61            'customize_controls_enqueue_scripts',
62            function () use ( $logo_control ) {
63                wp_enqueue_script( 'jetpack-mu-wpcom-logo-tool', plugins_url( 'js/customizer.js', __FILE__ ), array( 'customize-controls' ), '20210706', true );
64                wp_enqueue_style( 'jetpack-mu-wpcom-logo-tool', plugins_url( 'css/logo-tool.css', __FILE__ ), array(), '20220108' );
65                wp_localize_script(
66                    'jetpack-mu-wpcom-logo-tool',
67                    '_LogoTool_',
68                    array(
69                        'l10n'         => array( 'create' => __( 'Create logo in minutes', 'jetpack-mu-wpcom' ) ),
70                        'controlId'    => $logo_control->id,
71                        'settingId'    => $logo_control->setting->id,
72                        'referralLink' => 'https://wp.me/logo-maker/?utm_campaign=customizer' . ( defined( 'IS_ATOMIC' ) && IS_ATOMIC === true ? '-atomic' : '' ),
73                    )
74                );
75            }
76        );
77    }
78    add_action( 'customize_register', 'add_logotool_button', 20 );
79}