Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
12.50% covered (danger)
12.50%
7 / 56
33.33% covered (danger)
33.33%
1 / 3
CRAP
n/a
0 / 0
wpcom_plugins_should_load_agents_manager
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
wpcom_plugins_show_banner
4.35% covered (danger)
4.35%
2 / 46
0.00% covered (danger)
0.00%
0 / 1
79.89
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 * Load Agents Manager on the plugin management and installation screens.
12 *
13 * @param bool $should_load Whether another integration already requested Agents Manager.
14 * @return bool Whether Agents Manager should load.
15 */
16function wpcom_plugins_should_load_agents_manager( $should_load ) {
17    global $pagenow;
18
19    if ( $should_load || ! is_admin() ) {
20        return $should_load;
21    }
22
23    if ( ! in_array( $pagenow, array( 'plugins.php', 'plugin-install.php' ), true ) ) {
24        return false;
25    }
26
27    return (bool) get_option( 'big_sky_enable', false );
28}
29add_filter( 'agents_manager_should_load', 'wpcom_plugins_should_load_agents_manager' );
30
31/**
32 * Displays a banner before the plugin browser that links to the WP.com Plugins Marketplace.
33 */
34function wpcom_plugins_show_banner() {
35    // The banner sends people to Calypso's marketplace, which the Marketplace tab replaces.
36    if ( function_exists( 'wpcom_marketplace_tab_enabled' ) && wpcom_marketplace_tab_enabled() ) {
37        return;
38    }
39
40    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
41    if ( isset( $_GET['tab'] ) && 'favorites' === $_GET['tab'] ) {
42        // no banner on the favorites tab, it's a bit overbearing given they presumably want
43        // something specific.
44        return;
45    }
46
47    $site_slug        = wp_parse_url( home_url(), PHP_URL_HOST );
48    $wpcom_logo       = plugins_url( 'images/wpcom-logo.svg', __FILE__ );
49    $background_image = plugins_url( 'images/banner-background.webp', __FILE__ );
50
51    $asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-plugins-banner/wpcom-plugins-banner.asset.php';
52
53    /**
54     * Check to see if a string has been translated. This is for the purposes of changing the banner
55     * copies below, this function and checks can be removed after new copies are translated.
56     *
57     * @param string $string The string to check.
58     * @return bool True if the string has been translated, false otherwise.
59     */
60    function should_use_new_translation( $string ) { // phpcs:ignore Squiz.PHP.InnerFunctions.NotAllowed
61        if ( function_exists( 'wpcom_launchpad_has_translation' ) ) {
62            return wpcom_launchpad_has_translation( $string, 'jetpack-mu-wpcom' );
63        }
64        // If that function no longer exists in this context, we can assume the new strings have
65        // been translated by now.
66        return true;
67    }
68
69    $banner_title = should_use_new_translation( 'Unlock more with premium and free plugins' ) ?
70        esc_html__( 'Unlock more with premium and free plugins', 'jetpack-mu-wpcom' ) :
71        esc_html__( "Flex your site's features with plugins", 'jetpack-mu-wpcom' );
72
73    $banner_description = should_use_new_translation( "Discover a curated selection of free and premium plugins designed to enhance your site's functionality and features." ) ?
74        esc_html__( "Discover a curated selection of free and premium plugins designed to enhance your site's functionality and features.", 'jetpack-mu-wpcom' ) :
75        esc_html__( "Access a variety of free and paid plugins that can enhance your site's functionality and features.", 'jetpack-mu-wpcom' );
76
77    $banner_cta = should_use_new_translation( 'Explore marketplace plugins' ) ?
78        esc_html__( 'Explore marketplace plugins', 'jetpack-mu-wpcom' ) :
79        esc_html__( 'Explore plugins', 'jetpack-mu-wpcom' );
80
81    wp_enqueue_script(
82        'wpcom-plugins-banner',
83        plugins_url( 'build/wpcom-plugins-banner/wpcom-plugins-banner.js', Jetpack_Mu_Wpcom::BASE_FILE ),
84        $asset_file['dependencies'] ?? array(),
85        $asset_file['version'] ?? filemtime( Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-plugins-banner/wpcom-plugins-banner.js' ),
86        array(
87            'strategy'  => 'defer',
88            'in_footer' => true,
89        )
90    );
91    wp_localize_script(
92        'wpcom-plugins-banner',
93        'wpcomPluginsBanner',
94        array(
95            'logo'             => esc_url( $wpcom_logo ),
96            'title'            => $banner_title,
97            'description'      => $banner_description,
98            'actionUrl'        => esc_url( "https://wordpress.com/plugins/$site_slug?ref=woa-plugin-banner" ),
99            'actionText'       => $banner_cta,
100            'bannerBackground' => esc_url( $background_image ),
101        )
102    );
103
104    $asset_file_style = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-plugins-banner/wpcom-plugins-banner.asset.php';
105    wp_enqueue_style(
106        'wpcom-plugins-banner-style',
107        plugins_url( 'build/wpcom-plugins-banner-style/wpcom-plugins-banner-style.css', Jetpack_Mu_Wpcom::BASE_FILE ),
108        array(),
109        $asset_file_style['version'] ?? filemtime( Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-plugins-banner-style/wpcom-plugins-banner-style.css' )
110    );
111}
112add_action( 'load-plugin-install.php', 'wpcom_plugins_show_banner' );