Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
7.55% covered (danger)
7.55%
4 / 53
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Ads
7.84% covered (danger)
7.84%
4 / 51
0.00% covered (danger)
0.00%
0 / 7
105.70
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 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
6
 can_write_to_wpcom
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 get_payout
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 save_payout
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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\Monetize\Ads_Wpcom_Settings;
12use Automattic\Jetpack\Monetize\Dashboard;
13use Automattic\Jetpack\Monetize\Dashboard_Section;
14use Automattic\Jetpack\Monetize\WPCOM_Proxy;
15use WP_Error;
16use WP_REST_Request;
17
18if ( ! defined( 'ABSPATH' ) ) {
19    exit( 0 );
20}
21
22/**
23 * Payout details on WordPress.com, and keeping its copy of the site's Ads settings caught up.
24 *
25 * @since $$next-version$$
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 WordPress.com is behind the site's Ads settings.
40     *
41     * @return array
42     */
43    public function get_state() {
44        return array( 'wpcomOutOfDate' => Ads_Wpcom_Settings::is_out_of_date() );
45    }
46
47    /**
48     * Register the payout and sync routes.
49     *
50     * @return void
51     */
52    public function register_routes() {
53        $base = '/' . WPCOM_Proxy::REST_BASE . '/ads';
54
55        register_rest_route(
56            WPCOM_Proxy::REST_NAMESPACE,
57            $base . '/payout',
58            array(
59                array(
60                    'methods'             => 'GET',
61                    'callback'            => array( __CLASS__, 'get_payout' ),
62                    'permission_callback' => array( __CLASS__, 'can_write_to_wpcom' ),
63                ),
64                array(
65                    'methods'             => 'POST',
66                    'callback'            => array( __CLASS__, 'save_payout' ),
67                    'permission_callback' => array( __CLASS__, 'can_write_to_wpcom' ),
68                    'args'                => array(
69                        'paypal' => array(
70                            'type'              => 'string',
71                            'required'          => true,
72                            'sanitize_callback' => 'sanitize_email',
73                            'validate_callback' => static function ( $value ) {
74                                return is_email( $value ) ? true : new WP_Error( 'invalid_paypal', __( 'Enter a valid PayPal email address.', 'jetpack-monetize' ), array( 'status' => 400 ) );
75                            },
76                        ),
77                        'tos'    => array(
78                            'type'    => 'boolean',
79                            'default' => false,
80                        ),
81                    ),
82                ),
83            )
84        );
85
86        register_rest_route(
87            WPCOM_Proxy::REST_NAMESPACE,
88            $base . '/sync',
89            array(
90                'methods'             => 'POST',
91                'callback'            => array( __CLASS__, 'sync' ),
92                'permission_callback' => array( __CLASS__, 'can_write_to_wpcom' ),
93            )
94        );
95    }
96
97    /**
98     * Admins whose own WordPress.com account is connected, since every request goes out with their token.
99     *
100     * @return true|WP_Error
101     */
102    public static function can_write_to_wpcom() {
103        if ( ! Dashboard::can_manage() ) {
104            return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to do that.', 'jetpack-monetize' ), array( 'status' => rest_authorization_required_code() ) );
105        }
106
107        if ( ! ( new Connection_Manager() )->is_user_connected() ) {
108            return new WP_Error( 'rest_unauthorized', __( 'Connect your WordPress.com account first.', 'jetpack-monetize' ), array( 'status' => 403 ) );
109        }
110
111        return true;
112    }
113
114    /**
115     * GET the payout details.
116     *
117     * @return \WP_REST_Response|WP_Error
118     */
119    public static function get_payout() {
120        return rest_ensure_response( Ads_Wpcom_Settings::get_payout() );
121    }
122
123    /**
124     * POST the payout details.
125     *
126     * @param WP_REST_Request $request Request.
127     * @return \WP_REST_Response|WP_Error
128     */
129    public static function save_payout( WP_REST_Request $request ) {
130        return rest_ensure_response( Ads_Wpcom_Settings::save_payout( $request['paypal'], (bool) $request['tos'] ) );
131    }
132
133    /**
134     * POST to catch WordPress.com up with the site's Ads settings.
135     *
136     * @return \WP_REST_Response|WP_Error
137     */
138    public static function sync() {
139        $synced = Ads_Wpcom_Settings::sync();
140        return is_wp_error( $synced ) ? $synced : rest_ensure_response( array( 'synced' => $synced ) );
141    }
142}