api.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Methods for accessing data through the WPCOM REST API |
| 5 | * |
| 6 | * @since 4.5.0 |
| 7 | */ |
| 8 | class WordAds_API { |
| 9 | |
| 10 | private static $wordads_status = null; |
| 11 | |
| 12 | /** |
| 13 | * Returns site's WordAds status |
| 14 | * @return array boolean values for 'approved' and 'active' |
| 15 | * |
| 16 | * @since 4.5.0 |
| 17 | */ |
| 18 | public static function get_wordads_status() { |
| 19 | global $wordads_status_response; |
| 20 | if ( Jetpack::is_development_mode() ) { |
| 21 | self::$wordads_status = array( |
| 22 | 'approved' => true, |
| 23 | 'active' => true, |
| 24 | 'house' => true, |
| 25 | ); |
| 26 | |
| 27 | return self::$wordads_status; |
| 28 | } |
| 29 | |
| 30 | $endpoint = sprintf( '/sites/%d/wordads/status', Jetpack::get_option( 'id' ) ); |
| 31 | $wordads_status_response = $response = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint ); |
| 32 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 33 | return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response ); |
| 34 | } |
| 35 | |
| 36 | $body = json_decode( wp_remote_retrieve_body( $response ) ); |
| 37 | self::$wordads_status = array( |
| 38 | 'approved' => $body->approved, |
| 39 | 'active' => $body->active, |
| 40 | 'house' => $body->house, |
| 41 | ); |
| 42 | |
| 43 | return self::$wordads_status; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns the ads.txt content needed to run WordAds. |
| 48 | * @return array string contents of the ads.txt file. |
| 49 | * |
| 50 | * @since 6.1.0 |
| 51 | */ |
| 52 | public static function get_wordads_ads_txt() { |
| 53 | $endpoint = sprintf( '/sites/%d/wordads/ads-txt', Jetpack::get_option( 'id' ) ); |
| 54 | $wordads_status_response = $response = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint ); |
| 55 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 56 | return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response ); |
| 57 | } |
| 58 | |
| 59 | $body = json_decode( wp_remote_retrieve_body( $response ) ); |
| 60 | $ads_txt = str_replace( '\\n', PHP_EOL, $body->adstxt ); |
| 61 | return $ads_txt; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns status of WordAds approval. |
| 66 | * @return boolean true if site is WordAds approved |
| 67 | * |
| 68 | * @since 4.5.0 |
| 69 | */ |
| 70 | public static function is_wordads_approved() { |
| 71 | if ( is_null( self::$wordads_status ) ) { |
| 72 | self::get_wordads_status(); |
| 73 | } |
| 74 | |
| 75 | return self::$wordads_status['approved'] ? '1' : '0'; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns status of WordAds active. |
| 80 | * @return boolean true if ads are active on site |
| 81 | * |
| 82 | * @since 4.5.0 |
| 83 | */ |
| 84 | public static function is_wordads_active() { |
| 85 | if ( is_null( self::$wordads_status ) ) { |
| 86 | self::get_wordads_status(); |
| 87 | } |
| 88 | |
| 89 | return self::$wordads_status['active'] ? '1' : '0'; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns status of WordAds house ads. |
| 94 | * @return boolean true if WP.com house ads should be shown |
| 95 | * |
| 96 | * @since 4.5.0 |
| 97 | */ |
| 98 | public static function is_wordads_house() { |
| 99 | if ( is_null( self::$wordads_status ) ) { |
| 100 | self::get_wordads_status(); |
| 101 | } |
| 102 | |
| 103 | return self::$wordads_status['house'] ? '1' : '0'; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Grab WordAds status from WP.com API and store as option |
| 108 | * |
| 109 | * @since 4.5.0 |
| 110 | */ |
| 111 | static function update_wordads_status_from_api() { |
| 112 | $status = self::get_wordads_status(); |
| 113 | if ( ! is_wp_error( $status ) ) { |
| 114 | update_option( 'wordads_approved', self::is_wordads_approved(), true ); |
| 115 | update_option( 'wordads_active', self::is_wordads_active(), true ); |
| 116 | update_option( 'wordads_house', self::is_wordads_house(), true ); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 |