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