Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.45% covered (warning)
70.45%
31 / 44
16.67% covered (danger)
16.67%
1 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Ads
73.81% covered (warning)
73.81%
31 / 42
16.67% covered (danger)
16.67%
1 / 6
13.17
0.00% covered (danger)
0.00%
0 / 1
 get_key
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_state
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 can_join_wordads
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 register_routes
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
1
 can_write_to_wpcom
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 sync
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * The Ads section of the Monetize dashboard.
4 *
5 * @package automattic/jetpack-monetize
6 */
7
8namespace Automattic\Jetpack\Monetize\Sections;
9
10use Automattic\Jetpack\Connection\Manager as Connection_Manager;
11use Automattic\Jetpack\Current_Plan;
12use Automattic\Jetpack\Monetize\Ads_Wpcom_Settings;
13use Automattic\Jetpack\Monetize\Dashboard;
14use Automattic\Jetpack\Monetize\Dashboard_Section;
15use Automattic\Jetpack\Monetize\WPCOM_Proxy;
16use Automattic\Jetpack\Status\Host;
17use WP_Error;
18
19if ( ! defined( 'ABSPATH' ) ) {
20    exit( 0 );
21}
22
23/**
24 * Ad earnings and payouts on WordPress.com, and keeping its copy of the site's Ads settings caught up.
25 *
26 * @since 0.2.0
27 */
28class Ads implements Dashboard_Section {
29
30    /**
31     * The section's key.
32     *
33     * @return string
34     */
35    public function get_key() {
36        return 'ads';
37    }
38
39    /**
40     * Whether the plan includes WordAds, who can join it, and whether WordPress.com is behind the site's Ads settings.
41     *
42     * @return array
43     */
44    public function get_state() {
45        return array(
46            'hasWordAds'     => Current_Plan::supports( 'wordads' ),
47            'canJoinWordAds' => self::can_join_wordads(),
48            // Simple keeps its Ads settings on WordPress.com, so there is nothing to sync.
49            'wpcomOutOfDate' => ! ( new Host() )->is_wpcom_simple() && Ads_Wpcom_Settings::is_out_of_date(),
50        );
51    }
52
53    /**
54     * Whether the current user may join WordAds on Simple, which WordPress.com allows only the site owner.
55     *
56     * @return bool
57     */
58    private static function can_join_wordads() {
59        return function_exists( 'wpcom_get_blog_owner' )
60            && get_current_user_id() === (int) wpcom_get_blog_owner();
61    }
62
63    /**
64     * Register the payout, earnings and sync routes.
65     *
66     * @return void
67     */
68    public function register_routes() {
69        // Payout details only: the site's own Ads settings reach WordPress.com through the sync route.
70        WPCOM_Proxy::register_route(
71            'wpcom/v2',
72            'wordads/settings',
73            array( 'GET', 'POST' ),
74            array(
75                'paypal' => array(
76                    'type'   => 'string',
77                    'format' => 'email',
78                ),
79                'tos'    => array(
80                    'type' => 'string',
81                    'enum' => array( 'signed' ),
82                ),
83            )
84        );
85
86        WPCOM_Proxy::register_route( 'wpcom/v2', 'wordads/earnings', 'GET' );
87        WPCOM_Proxy::register_route( 'wpcom/v2', 'wordads/payments', 'GET' );
88
89        // Approval and whether WordPress.com considers the site safe for ads.
90        WPCOM_Proxy::register_route( 'rest/v1.1', 'wordads/account', 'GET' );
91
92        register_rest_route(
93            WPCOM_Proxy::REST_NAMESPACE,
94            '/' . WPCOM_Proxy::REST_BASE . '/ads/sync',
95            array(
96                'methods'             => 'POST',
97                'callback'            => array( __CLASS__, 'sync' ),
98                'permission_callback' => array( __CLASS__, 'can_write_to_wpcom' ),
99            )
100        );
101    }
102
103    /**
104     * Admins whose own WordPress.com account is connected, since every request goes out with their token.
105     *
106     * @return true|WP_Error
107     */
108    public static function can_write_to_wpcom() {
109        if ( ! Dashboard::can_manage() ) {
110            return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to do that.', 'jetpack-monetize' ), array( 'status' => rest_authorization_required_code() ) );
111        }
112
113        if ( ! ( new Connection_Manager() )->is_user_connected() ) {
114            return new WP_Error( 'rest_unauthorized', __( 'Connect your WordPress.com account first.', 'jetpack-monetize' ), array( 'status' => rest_authorization_required_code() ) );
115        }
116
117        return true;
118    }
119
120    /**
121     * POST to catch WordPress.com up with the site's Ads settings.
122     *
123     * @return \WP_REST_Response|WP_Error
124     */
125    public static function sync() {
126        $synced = Ads_Wpcom_Settings::sync();
127        return is_wp_error( $synced ) ? $synced : rest_ensure_response( array( 'synced' => $synced ) );
128    }
129}