Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.31% |
36 / 39 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Ads_Wpcom_Settings | |
97.30% |
36 / 37 |
|
80.00% |
4 / 5 |
13 | |
0.00% |
0 / 1 |
| get_site_values | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| is_out_of_date | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sync | |
95.83% |
23 / 24 |
|
0.00% |
0 / 1 |
6 | |||
| get_blog_id | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hash | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * The WordPress.com copy of the site's Ads settings. |
| 4 | * |
| 5 | * @package automattic/jetpack-monetize |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Monetize; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | use Jetpack_Options; |
| 12 | use WP_Error; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit( 0 ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Keeps WordPress.com's Ads settings caught up with the site's, which always win. |
| 20 | * |
| 21 | * @since 0.2.0 |
| 22 | */ |
| 23 | class Ads_Wpcom_Settings { |
| 24 | |
| 25 | /** |
| 26 | * Hash of the site values WordPress.com last accepted. |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | const SYNCED_OPTION = 'jetpack_monetize_ads_wpcom_synced'; |
| 31 | |
| 32 | /** |
| 33 | * Site option => WordPress.com `display_options` key. |
| 34 | * |
| 35 | * @var array<string, string> |
| 36 | */ |
| 37 | const DISPLAY_OPTIONS = array( |
| 38 | 'wordads_display_front_page' => 'display_front_page', |
| 39 | 'wordads_display_post' => 'display_post', |
| 40 | 'wordads_display_page' => 'display_page', |
| 41 | 'wordads_display_archive' => 'display_archive', |
| 42 | 'enable_header_ad' => 'enable_header_ad', |
| 43 | 'wordads_second_belowpost' => 'second_belowpost', |
| 44 | 'wordads_inline_enabled' => 'inline_enabled', |
| 45 | ); |
| 46 | |
| 47 | /** |
| 48 | * The site's Ads settings in WordPress.com's shape, defaults included. |
| 49 | * |
| 50 | * @return array |
| 51 | */ |
| 52 | public static function get_site_values() { |
| 53 | // Defaults as in the Jetpack plugin's WordAds_Params, which stores them the first time ads load. |
| 54 | $display = array(); |
| 55 | foreach ( self::DISPLAY_OPTIONS as $option => $key ) { |
| 56 | $display[ $key ] = (bool) get_option( $option, true ); |
| 57 | } |
| 58 | |
| 59 | $privacy_page = (int) get_option( 'wp_page_for_privacy_policy' ); |
| 60 | $privacy_url = esc_url_raw( (string) get_option( 'wordads_ccpa_privacy_policy_url', $privacy_page ? get_permalink( $privacy_page ) : '' ), array( 'http', 'https' ) ); |
| 61 | |
| 62 | return array( |
| 63 | 'display_options' => $display, |
| 64 | 'ccpa_enabled' => (bool) get_option( 'wordads_ccpa_enabled', false ), |
| 65 | // WordPress.com only accepts an empty value or an absolute http(s) URL, and the site may hold a relative one. |
| 66 | 'ccpa_privacy_policy_url' => wp_parse_url( $privacy_url, PHP_URL_HOST ) ? $privacy_url : '', |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Whether the site's Ads settings changed since WordPress.com last accepted them. |
| 72 | * |
| 73 | * @return bool |
| 74 | */ |
| 75 | public static function is_out_of_date() { |
| 76 | return get_option( self::SYNCED_OPTION ) !== self::hash( self::get_site_values() ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Send the site's Ads settings to WordPress.com as the current user, if they changed since it last accepted them. |
| 81 | * |
| 82 | * @return true|WP_Error |
| 83 | */ |
| 84 | public static function sync() { |
| 85 | if ( ! self::is_out_of_date() ) { |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | $site = self::get_site_values(); |
| 90 | $response = Client::wpcom_json_api_request_as_user( |
| 91 | sprintf( '/sites/%d/wordads/settings', self::get_blog_id() ), |
| 92 | '2', |
| 93 | array( 'method' => 'POST' ), |
| 94 | $site |
| 95 | ); |
| 96 | |
| 97 | if ( is_wp_error( $response ) ) { |
| 98 | return $response; |
| 99 | } |
| 100 | |
| 101 | $status = (int) wp_remote_retrieve_response_code( $response ); |
| 102 | $data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 103 | $data = is_array( $data ) ? $data : array(); |
| 104 | |
| 105 | if ( $status >= 400 ) { |
| 106 | return new WP_Error( |
| 107 | (string) ( $data['code'] ?? 'wpcom_error' ), |
| 108 | (string) ( $data['message'] ?? '' ), |
| 109 | array( 'status' => $status ) |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | if ( ! is_array( $data['settings'] ?? null ) ) { |
| 114 | return new WP_Error( 'wpcom_invalid_response', __( 'WordPress.com sent an unexpected response.', 'jetpack-monetize' ), array( 'status' => 502 ) ); |
| 115 | } |
| 116 | |
| 117 | update_option( self::SYNCED_OPTION, self::hash( $site ), false ); |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * The site's WordPress.com blog ID. |
| 123 | * |
| 124 | * @return int |
| 125 | */ |
| 126 | private static function get_blog_id() { |
| 127 | return (int) Jetpack_Options::get_option( 'id' ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * A stable fingerprint of site values, tied to the WordPress.com site so a clone or reconnect catches up. |
| 132 | * |
| 133 | * @param array $values Site values. |
| 134 | * @return string |
| 135 | */ |
| 136 | private static function hash( array $values ) { |
| 137 | return md5( (string) wp_json_encode( array( self::get_blog_id(), $values ), JSON_UNESCAPED_SLASHES ) ); |
| 138 | } |
| 139 | } |