Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.37% covered (warning)
85.37%
35 / 41
77.78% covered (warning)
77.78%
7 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Ads_Settings_Home
92.11% covered (success)
92.11%
35 / 38
77.78% covered (warning)
77.78%
7 / 9
24.28
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 is_on_monetize_page
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 is_moved_off_settings
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get_url
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 filter_configure_url
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 add_script_data
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
6.03
 is_tab_request
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_consolidation_on
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 get_dashboard_data
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Where the Ads settings live once they leave Jetpack › Settings.
4 *
5 * @package automattic/jetpack
6 */
7
8use Automattic\Jetpack\Connection\Manager as Connection_Manager;
9use Automattic\Jetpack\Current_Plan;
10use Automattic\Jetpack\Monetize\Dashboard;
11use Automattic\Jetpack\My_Jetpack\Initializer;
12use Automattic\Jetpack\Status;
13
14if ( ! defined( 'ABSPATH' ) ) {
15    exit( 0 );
16}
17
18/**
19 * Picks this site's home for the Ads settings, for the Settings app, the Monetize page and configure_url.
20 *
21 * @since 16.5
22 */
23class Jetpack_Ads_Settings_Home {
24
25    /**
26     * Monetize › Settings, for a Monetize package older than `Dashboard::get_tab_url()`.
27     *
28     * @var string
29     */
30    const MONETIZE_SETTINGS_PATH = 'admin.php?page=jetpack-monetize&p=%2F%3Ftab%3Dsettings';
31
32    /**
33     * Hook the configure URL and the script data.
34     *
35     * @return void
36     */
37    public static function init() {
38        add_filter( 'jetpack_module_configuration_url_wordads', array( __CLASS__, 'filter_configure_url' ) );
39        add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'add_script_data' ) );
40    }
41
42    /**
43     * Whether the Monetize page shows the Ads settings. Needs no admin menu, so REST requests agree.
44     *
45     * @return bool
46     */
47    public static function is_on_monetize_page() {
48        return class_exists( Dashboard::class )
49            && Dashboard::is_enabled()
50            && Jetpack::is_module_active( 'monetize' )
51            // The module needs a connection: it stays listed as active, but never loads offline or disconnected.
52            && Jetpack::is_connection_ready()
53            && ! ( new Status() )->is_offline_mode();
54    }
55
56    /**
57     * Whether Jetpack › Settings drops its Ads card for Monetize › Settings.
58     *
59     * @return bool
60     */
61    public static function is_moved_off_settings() {
62        return self::is_consolidation_on() && self::is_on_monetize_page();
63    }
64
65    /**
66     * Monetize › Settings, or null where the Monetize page doesn't show the Ads settings.
67     *
68     * @return string|null
69     */
70    public static function get_url() {
71        if ( ! self::is_on_monetize_page() ) {
72            return null;
73        }
74
75        return method_exists( Dashboard::class, 'get_tab_url' )
76            ? Dashboard::get_tab_url( 'settings' )
77            : admin_url( self::MONETIZE_SETTINGS_PATH );
78    }
79
80    /**
81     * Point the WordAds configure URL at its home.
82     *
83     * @param string $url Jetpack's settings-search default.
84     * @return string
85     */
86    public static function filter_configure_url( $url ) {
87        return self::get_url() ?? $url;
88    }
89
90    /**
91     * Add the tab's data on the Monetize page, and the URL Jetpack › Settings points to once it drops its card.
92     *
93     * @param array $data Script data.
94     * @return array
95     */
96    public static function add_script_data( $data ) {
97        $is_tab_request = self::is_tab_request();
98        $is_moved       = self::is_moved_off_settings();
99        if ( ! $is_tab_request && ! $is_moved ) {
100            return $data;
101        }
102
103        if ( ! is_array( $data ) ) {
104            $data = array();
105        }
106
107        if ( $is_moved ) {
108            $data['monetize']['adsSettingsUrl'] = self::get_url();
109        }
110
111        if ( $is_tab_request ) {
112            $data['monetize']['adsSettings'] = self::get_dashboard_data();
113        }
114
115        return $data;
116    }
117
118    /**
119     * Whether this request opens the Monetize page that holds the Ads settings.
120     *
121     * @return bool
122     */
123    private static function is_tab_request() {
124        // Do not reorder: is_on_monetize_page() checks that Dashboard exists.
125        return self::is_on_monetize_page() && Dashboard::is_dashboard_request();
126    }
127
128    /**
129     * Whether settings consolidation is on for the site, guarded against an older My Jetpack.
130     *
131     * @return bool
132     */
133    private static function is_consolidation_on() {
134        return class_exists( Initializer::class )
135            && method_exists( Initializer::class, 'is_settings_consolidation_on_for_site' )
136            && Initializer::is_settings_consolidation_on_for_site();
137    }
138
139    /**
140     * What the Settings tab needs from the server, built as the Settings app's Ads card builds it.
141     *
142     * @return array
143     */
144    private static function get_dashboard_data() {
145        $connection = new Connection_Manager();
146        $suffix     = ( new Status() )->get_site_suffix();
147
148        return array(
149            'hasWordAds'         => Current_Plan::supports( 'wordads' ),
150            'hasConnectedOwner'  => $connection->has_connected_owner(),
151            'isSubdirectorySite' => str_contains( $suffix, '::' ),
152            'moduleOverride'     => Jetpack_Modules_Overrides::instance()->get_module_override( 'wordads' ),
153        );
154    }
155}
156
157Jetpack_Ads_Settings_Home::init();