Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 2
CRAP
n/a
0 / 0
wpcom_plugins_show_banner
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 1
56
should_use_new_translation
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Adds a tiny WordPress.com Plugins integration to the plugin list.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8use Automattic\Jetpack\Jetpack_Mu_Wpcom;
9
10/**
11 * Displays a banner before the plugin browser that links to the WP.com Plugins Marketplace.
12 */
13function wpcom_plugins_show_banner() {
14    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
15    if ( isset( $_GET['tab'] ) && 'favorites' === $_GET['tab'] ) {
16        // no banner on the favorites tab, it's a bit overbearing given they presumably want
17        // something specific.
18        return;
19    }
20
21    $site_slug        = wp_parse_url( home_url(), PHP_URL_HOST );
22    $wpcom_logo       = plugins_url( 'images/wpcom-logo.svg', __FILE__ );
23    $background_image = plugins_url( 'images/banner-background.webp', __FILE__ );
24
25    $asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-plugins-banner/wpcom-plugins-banner.asset.php';
26
27    /**
28     * Check to see if a string has been translated. This is for the purposes of changing the banner
29     * copies below, this function and checks can be removed after new copies are translated.
30     *
31     * @param string $string The string to check.
32     * @return bool True if the string has been translated, false otherwise.
33     */
34    function should_use_new_translation( $string ) { // phpcs:ignore Squiz.PHP.InnerFunctions.NotAllowed
35        if ( function_exists( 'wpcom_launchpad_has_translation' ) ) {
36            return wpcom_launchpad_has_translation( $string, 'jetpack-mu-wpcom' );
37        }
38        // If that function no longer exists in this context, we can assume the new strings have
39        // been translated by now.
40        return true;
41    }
42
43    $banner_title = should_use_new_translation( 'Unlock more with premium and free plugins' ) ?
44        esc_html__( 'Unlock more with premium and free plugins', 'jetpack-mu-wpcom' ) :
45        esc_html__( "Flex your site's features with plugins", 'jetpack-mu-wpcom' );
46
47    $banner_description = should_use_new_translation( "Discover a curated selection of free and premium plugins designed to enhance your site's functionality and features." ) ?
48        esc_html__( "Discover a curated selection of free and premium plugins designed to enhance your site's functionality and features.", 'jetpack-mu-wpcom' ) :
49        esc_html__( "Access a variety of free and paid plugins that can enhance your site's functionality and features.", 'jetpack-mu-wpcom' );
50
51    $banner_cta = should_use_new_translation( 'Explore marketplace plugins' ) ?
52        esc_html__( 'Explore marketplace plugins', 'jetpack-mu-wpcom' ) :
53        esc_html__( 'Explore plugins', 'jetpack-mu-wpcom' );
54
55    wp_enqueue_script(
56        'wpcom-plugins-banner',
57        plugins_url( 'build/wpcom-plugins-banner/wpcom-plugins-banner.js', Jetpack_Mu_Wpcom::BASE_FILE ),
58        $asset_file['dependencies'] ?? array(),
59        $asset_file['version'] ?? filemtime( Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-plugins-banner/wpcom-plugins-banner.js' ),
60        array(
61            'strategy'  => 'defer',
62            'in_footer' => true,
63        )
64    );
65    wp_localize_script(
66        'wpcom-plugins-banner',
67        'wpcomPluginsBanner',
68        array(
69            'logo'             => esc_url( $wpcom_logo ),
70            'title'            => $banner_title,
71            'description'      => $banner_description,
72            'actionUrl'        => esc_url( "https://wordpress.com/plugins/$site_slug?ref=woa-plugin-banner" ),
73            'actionText'       => $banner_cta,
74            'bannerBackground' => esc_url( $background_image ),
75        )
76    );
77
78    $asset_file_style = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-plugins-banner/wpcom-plugins-banner.asset.php';
79    wp_enqueue_style(
80        'wpcom-plugins-banner-style',
81        plugins_url( 'build/wpcom-plugins-banner-style/wpcom-plugins-banner-style.css', Jetpack_Mu_Wpcom::BASE_FILE ),
82        array(),
83        $asset_file_style['version'] ?? filemtime( Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-plugins-banner-style/wpcom-plugins-banner-style.css' )
84    );
85}
86add_action( 'load-plugin-install.php', 'wpcom_plugins_show_banner' );