Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
59.65% covered (warning)
59.65%
34 / 57
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
wpcom_themes_show_banner
97.14% covered (success)
97.14%
34 / 35
0.00% covered (danger)
0.00%
0 / 1
3
wpcom_themes_add_theme_showcase_menu
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
wpcom_auto_open_upload_theme
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * WordPress.com Themes
4 *
5 * Adds a WordPress.com themes integration to the theme-related pages.
6 *
7 * @package automattic/jetpack-mu-wpcom
8 */
9
10use Automattic\Jetpack\Jetpack_Mu_Wpcom;
11
12/**
13 * Displays a banner before the theme browser that links to the WP.com Theme Showcase.
14 */
15function wpcom_themes_show_banner() {
16    if ( get_option( 'wpcom_admin_interface' ) !== 'wp-admin' ) {
17        return;
18    }
19
20    // The Marketplace tab lists these themes on the screen itself, so the banner is hidden on every tab.
21    if ( wpcom_themes_tab_enabled() ) {
22        return;
23    }
24
25    $site_slug        = wp_parse_url( home_url(), PHP_URL_HOST );
26    $wpcom_logo       = plugins_url( 'images/wpcom-logo.svg', __FILE__ );
27    $background_image = plugins_url( 'images/banner-background.webp', __FILE__ );
28
29    wp_enqueue_script(
30        'wpcom-themes-banner',
31        plugins_url( 'js/banner.js', __FILE__ ),
32        array(),
33        Jetpack_Mu_Wpcom::PACKAGE_VERSION,
34        array(
35            'strategy'  => 'defer',
36            'in_footer' => true,
37        )
38    );
39    wp_localize_script(
40        'wpcom-themes-banner',
41        'wpcomThemesBanner',
42        array(
43            'logo'             => esc_url( $wpcom_logo ),
44            'title'            => esc_html__( 'Find the perfect theme for your site', 'jetpack-mu-wpcom' ),
45            'description'      => esc_html__( 'Dive deep into the world of WordPress.com themes. Discover the responsive and stunning designs waiting to bring your site to life.', 'jetpack-mu-wpcom' ),
46            'actionUrl'        => esc_url( "https://wordpress.com/themes/$site_slug?ref=wpcom-themes-banner" ),
47            'actionText'       => esc_html__( 'Explore themes', 'jetpack-mu-wpcom' ),
48            'bannerBackground' => esc_url( $background_image ),
49        )
50    );
51    wp_enqueue_style(
52        'wpcom-themes-banner',
53        plugins_url( 'css/banner.css', __FILE__ ),
54        array(),
55        Jetpack_Mu_Wpcom::PACKAGE_VERSION
56    );
57}
58add_action( 'load-theme-install.php', 'wpcom_themes_show_banner' );
59
60/**
61 * Registers an "Appearance > Theme Showcase" menu.
62 */
63function wpcom_themes_add_theme_showcase_menu() {
64    if ( get_option( 'wpcom_admin_interface' ) !== 'wp-admin' ) {
65        return;
66    }
67
68    $site_slug = wp_parse_url( home_url(), PHP_URL_HOST );
69    add_submenu_page(
70        'themes.php',
71        esc_attr__( 'Theme Showcase', 'jetpack-mu-wpcom' ),
72        __( 'Theme Showcase', 'jetpack-mu-wpcom' ),
73        current_user_can( 'switch_themes' ) ? 'switch_themes' : 'edit_theme_options',
74        "https://wordpress.com/themes/$site_slug?ref=wpcom-themes-menu"
75    );
76}
77add_action( 'admin_menu', 'wpcom_themes_add_theme_showcase_menu' );
78
79/**
80 * Automatically opens the "Upload Theme" dialog on the theme installation page based on a 'wpcom-upload' query parameter.
81 */
82function wpcom_auto_open_upload_theme() {
83    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
84    if ( isset( $_GET['wpcom-upload'] ) && $_GET['wpcom-upload'] === '1' ) {
85        if ( ! current_user_can( 'install_themes' ) ) {
86            return;
87        }
88        add_filter(
89            'admin_body_class',
90            function ( $classes ) {
91                return $classes . ' show-upload-view ';
92            }
93        );
94    }
95}
96add_action( 'load-theme-install.php', 'wpcom_auto_open_upload_theme' );