Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
wpcom_themes_show_banner
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
6
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    $site_slug        = wp_parse_url( home_url(), PHP_URL_HOST );
21    $wpcom_logo       = plugins_url( 'images/wpcom-logo.svg', __FILE__ );
22    $background_image = plugins_url( 'images/banner-background.webp', __FILE__ );
23
24    wp_enqueue_script(
25        'wpcom-themes-banner',
26        plugins_url( 'js/banner.js', __FILE__ ),
27        array(),
28        Jetpack_Mu_Wpcom::PACKAGE_VERSION,
29        array(
30            'strategy'  => 'defer',
31            'in_footer' => true,
32        )
33    );
34    wp_localize_script(
35        'wpcom-themes-banner',
36        'wpcomThemesBanner',
37        array(
38            'logo'             => esc_url( $wpcom_logo ),
39            'title'            => esc_html__( 'Find the perfect theme for your site', 'jetpack-mu-wpcom' ),
40            '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' ),
41            'actionUrl'        => esc_url( "https://wordpress.com/themes/$site_slug?ref=wpcom-themes-banner" ),
42            'actionText'       => esc_html__( 'Explore themes', 'jetpack-mu-wpcom' ),
43            'bannerBackground' => esc_url( $background_image ),
44        )
45    );
46    wp_enqueue_style(
47        'wpcom-themes-banner',
48        plugins_url( 'css/banner.css', __FILE__ ),
49        array(),
50        Jetpack_Mu_Wpcom::PACKAGE_VERSION
51    );
52}
53add_action( 'load-theme-install.php', 'wpcom_themes_show_banner' );
54
55/**
56 * Registers an "Appearance > Theme Showcase" menu.
57 */
58function wpcom_themes_add_theme_showcase_menu() {
59    if ( get_option( 'wpcom_admin_interface' ) !== 'wp-admin' ) {
60        return;
61    }
62
63    $site_slug = wp_parse_url( home_url(), PHP_URL_HOST );
64    add_submenu_page(
65        'themes.php',
66        esc_attr__( 'Theme Showcase', 'jetpack-mu-wpcom' ),
67        __( 'Theme Showcase', 'jetpack-mu-wpcom' ),
68        current_user_can( 'switch_themes' ) ? 'switch_themes' : 'edit_theme_options',
69        "https://wordpress.com/themes/$site_slug?ref=wpcom-themes-menu"
70    );
71}
72add_action( 'admin_menu', 'wpcom_themes_add_theme_showcase_menu' );
73
74/**
75 * Automatically opens the "Upload Theme" dialog on the theme installation page based on a 'wpcom-upload' query parameter.
76 */
77function wpcom_auto_open_upload_theme() {
78    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
79    if ( isset( $_GET['wpcom-upload'] ) && $_GET['wpcom-upload'] === '1' ) {
80        if ( ! current_user_can( 'install_themes' ) ) {
81            return;
82        }
83        add_filter(
84            'admin_body_class',
85            function ( $classes ) {
86                return $classes . ' show-upload-view ';
87            }
88        );
89    }
90}
91add_action( 'load-theme-install.php', 'wpcom_auto_open_upload_theme' );