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