Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
58.18% covered (warning)
58.18%
32 / 55
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Data_Sharing
58.18% covered (warning)
58.18%
32 / 55
66.67% covered (warning)
66.67%
2 / 3
19.85
0.00% covered (danger)
0.00%
0 / 1
 register_routes
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
2
 get_status
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 save
8.00% covered (danger)
8.00%
2 / 25
0.00% covered (danger)
0.00%
0 / 1
34.03
1<?php
2/**
3 * WordPress.com content-sharing consent in SEO Settings.
4 *
5 * @package automattic/jetpack-seo-package
6 */
7
8namespace Automattic\Jetpack\SEO;
9
10use Automattic\Jetpack\Connection\Client;
11use Automattic\Jetpack\Status;
12use Automattic\Jetpack\Status\Host;
13
14/** Provides another interface to the platform's existing consent setting. */
15class Data_Sharing {
16    /**
17     * Register only where the platform owns this setting.
18     *
19     * @return void
20     */
21    public static function register_routes() {
22        if ( ! ( new Host() )->is_wpcom_platform() ) {
23            return;
24        }
25        register_rest_route(
26            Dashboard_Data::REST_NAMESPACE,
27            '/seo/data-sharing',
28            array(
29                array(
30                    'methods'             => \WP_REST_Server::READABLE,
31                    'permission_callback' => array( Dashboard_Data::class, 'permission_check' ),
32                    'callback'            => array( __CLASS__, 'get_status' ),
33                ),
34                array(
35                    'methods'             => \WP_REST_Server::CREATABLE,
36                    'permission_callback' => array( Dashboard_Data::class, 'permission_check' ),
37                    'callback'            => array( __CLASS__, 'save' ),
38                    'args'                => array(
39                        'prevent_sharing' => array(
40                            'type'     => 'boolean',
41                            'required' => true,
42                        ),
43                    ),
44                ),
45            )
46        );
47    }
48
49    /**
50     * Read the preference and the visibility restriction independently.
51     *
52     * @return array
53     */
54    public static function get_status() {
55        $locked = ! Ai_Crawlers::search_engines_allowed() || Ai_Crawlers::is_crawl_restricted_subdomain() || ( new Status() )->is_coming_soon();
56        return array(
57            'prevent_sharing' => Ai_Crawlers::has_data_sharing_opt_out(),
58            'locked'          => $locked,
59            'privacy_url'     => Ai_Crawlers::privacy_settings_url(),
60        );
61    }
62
63    /**
64     * Send only consent through the same platform path as Settings > Reading.
65     *
66     * @param \WP_REST_Request $request Consent request.
67     * @return array|\WP_Error
68     */
69    public static function save( $request ) {
70        if ( self::get_status()['locked'] ) {
71            return new \WP_Error( 'seo_sharing_visibility_locked', __( 'Sharing is prevented by your site visibility settings.', 'jetpack-seo' ), array( 'status' => 409 ) );
72        }
73        $host    = new Host();
74        $site_id = $host->get_wpcom_site_id();
75        if ( ! $site_id ) {
76            return new \WP_Error( 'seo_sharing_not_connected', __( 'Connect this site to WordPress.com before changing the sharing preference.', 'jetpack-seo' ), array( 'status' => 409 ) );
77        }
78        $method = $host->is_wpcom_simple() ? 'wpcom_json_api_request_as_blog' : 'wpcom_json_api_request_as_user';
79        $result = Client::$method(
80            '/sites/' . $site_id . '/site-visibility',
81            'v2',
82            array(
83                'method'  => 'POST',
84                'headers' => array( 'Content-Type' => 'application/json' ),
85            ),
86            wp_json_encode( array( 'wpcom_data_sharing_opt_out' => $request['prevent_sharing'] ), JSON_UNESCAPED_SLASHES ),
87            'wpcom'
88        );
89        if ( is_wp_error( $result ) ) {
90            return $result;
91        }
92        if ( 200 !== wp_remote_retrieve_response_code( $result ) ) {
93            return new \WP_Error( 'seo_sharing_save_failed', __( 'Unable to save the sharing preference. Please try again.', 'jetpack-seo' ), array( 'status' => 502 ) );
94        }
95        // Atomic's platform request updates the local option from another request.
96        wp_cache_delete( 'wpcom_data_sharing_opt_out', 'options' );
97        wp_cache_delete( 'alloptions', 'options' );
98        wp_cache_delete( 'notoptions', 'options' );
99        return self::get_status();
100    }
101}