Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 37 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| WordAds_API | |
0.00% |
0 / 35 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
| get_wordads_status | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
12 | |||
| update_wordads_status_from_api | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| get_wordads_ads_txt | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * The WordAds API. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\Connection\Client; |
| 9 | use Automattic\Jetpack\Status; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit( 0 ); |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Methods for accessing data through the WPCOM REST API |
| 17 | * |
| 18 | * @since 4.5.0 |
| 19 | */ |
| 20 | class WordAds_API { |
| 21 | |
| 22 | /** |
| 23 | * Get the site's WordAds status |
| 24 | * |
| 25 | * @return array|WP_Error Array of site status values, or WP_Error if no response from the API. |
| 26 | * |
| 27 | * @since 4.5.0 |
| 28 | */ |
| 29 | public static function get_wordads_status() { |
| 30 | global $wordads_status_response; |
| 31 | |
| 32 | // If the site is not connected, we can put it in a safe "house ad" mode. |
| 33 | if ( ( new Status() )->is_offline_mode() ) { |
| 34 | return array( |
| 35 | 'approved' => true, |
| 36 | 'active' => true, |
| 37 | 'house' => true, |
| 38 | 'unsafe' => false, |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | // Fetch the status from WPCOM endpoint. |
| 43 | $endpoint = sprintf( '/sites/%d/wordads/status', Jetpack::get_option( 'id' ) ); |
| 44 | $response = Client::wpcom_json_api_request_as_blog( $endpoint ); |
| 45 | $wordads_status_response = $response; |
| 46 | |
| 47 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 48 | return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response ); |
| 49 | } |
| 50 | |
| 51 | $body = json_decode( wp_remote_retrieve_body( $response ) ); |
| 52 | |
| 53 | return array( |
| 54 | 'approved' => (bool) $body->approved, |
| 55 | 'active' => (bool) $body->active, |
| 56 | 'house' => (bool) $body->house, |
| 57 | 'unsafe' => (bool) $body->unsafe, |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Grab WordAds status from WP.com API and store as option |
| 63 | * |
| 64 | * @since 4.5.0 |
| 65 | */ |
| 66 | public static function update_wordads_status_from_api() { |
| 67 | $status = self::get_wordads_status(); |
| 68 | |
| 69 | if ( ! is_wp_error( $status ) ) { |
| 70 | |
| 71 | // Convert boolean options to string first to work around update_option not setting the option if the value is false. |
| 72 | // This sets the option to either '1' if true or '' if false. |
| 73 | update_option( 'wordads_approved', (string) $status['approved'], true ); |
| 74 | update_option( 'wordads_active', (string) $status['active'], true ); |
| 75 | update_option( 'wordads_house', (string) $status['house'], true ); |
| 76 | update_option( 'wordads_unsafe', (string) $status['unsafe'], true ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns the ads.txt content needed to run WordAds. |
| 82 | * |
| 83 | * @return array string contents of the ads.txt file. |
| 84 | * |
| 85 | * @since 6.1.0 |
| 86 | */ |
| 87 | public static function get_wordads_ads_txt() { |
| 88 | global $wordads_status_response; |
| 89 | |
| 90 | $endpoint = sprintf( '/sites/%d/wordads/ads-txt', Jetpack::get_option( 'id' ) ); |
| 91 | $response = Client::wpcom_json_api_request_as_blog( $endpoint ); |
| 92 | $wordads_status_response = $response; |
| 93 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 94 | return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response ); |
| 95 | } |
| 96 | |
| 97 | $body = json_decode( wp_remote_retrieve_body( $response ) ); |
| 98 | $ads_txt = str_replace( '\\n', PHP_EOL, $body->adstxt ); |
| 99 | |
| 100 | return $ads_txt; |
| 101 | } |
| 102 | } |