Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
28 / 35
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Ads
84.85% covered (warning)
84.85%
28 / 33
20.00% covered (danger)
20.00%
1 / 5
8.22
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
100.00% covered (success)
100.00%
24 / 24
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\Monetize\Ads_Wpcom_Settings;
12use Automattic\Jetpack\Monetize\Dashboard;
13use Automattic\Jetpack\Monetize\Dashboard_Section;
14use Automattic\Jetpack\Monetize\WPCOM_Proxy;
15use WP_Error;
16
17if ( ! defined( 'ABSPATH' ) ) {
18    exit( 0 );
19}
20
21/**
22 * Payout details on WordPress.com, and keeping its copy of the site's Ads settings caught up.
23 *
24 * @since 0.2.0
25 */
26class Ads implements Dashboard_Section {
27
28    /**
29     * The section's key.
30     *
31     * @return string
32     */
33    public function get_key() {
34        return 'ads';
35    }
36
37    /**
38     * Whether WordPress.com is behind the site's Ads settings.
39     *
40     * @return array
41     */
42    public function get_state() {
43        return array( 'wpcomOutOfDate' => Ads_Wpcom_Settings::is_out_of_date() );
44    }
45
46    /**
47     * Register the payout and sync routes.
48     *
49     * @return void
50     */
51    public function register_routes() {
52        // Payout details only: the site's own Ads settings reach WordPress.com through the sync route.
53        WPCOM_Proxy::register_route(
54            'wpcom/v2',
55            'wordads/settings',
56            array( 'GET', 'POST' ),
57            array(
58                'paypal' => array(
59                    'type'   => 'string',
60                    'format' => 'email',
61                ),
62                'tos'    => array(
63                    'type' => 'string',
64                    'enum' => array( 'signed' ),
65                ),
66            )
67        );
68
69        register_rest_route(
70            WPCOM_Proxy::REST_NAMESPACE,
71            '/' . WPCOM_Proxy::REST_BASE . '/ads/sync',
72            array(
73                'methods'             => 'POST',
74                'callback'            => array( __CLASS__, 'sync' ),
75                'permission_callback' => array( __CLASS__, 'can_write_to_wpcom' ),
76            )
77        );
78    }
79
80    /**
81     * Admins whose own WordPress.com account is connected, since every request goes out with their token.
82     *
83     * @return true|WP_Error
84     */
85    public static function can_write_to_wpcom() {
86        if ( ! Dashboard::can_manage() ) {
87            return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to do that.', 'jetpack-monetize' ), array( 'status' => rest_authorization_required_code() ) );
88        }
89
90        if ( ! ( new Connection_Manager() )->is_user_connected() ) {
91            return new WP_Error( 'rest_unauthorized', __( 'Connect your WordPress.com account first.', 'jetpack-monetize' ), array( 'status' => 403 ) );
92        }
93
94        return true;
95    }
96
97    /**
98     * POST to catch WordPress.com up with the site's Ads settings.
99     *
100     * @return \WP_REST_Response|WP_Error
101     */
102    public static function sync() {
103        $synced = Ads_Wpcom_Settings::sync();
104        return is_wp_error( $synced ) ? $synced : rest_ensure_response( array( 'synced' => $synced ) );
105    }
106}