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 status of WordAds approval. |
| 48 | * @return boolean true if site is WordAds approved |
| 49 | * |
| 50 | * @since 4.5.0 |
| 51 | */ |
| 52 | public static function is_wordads_approved() { |
| 53 | if ( is_null( self::$wordads_status ) ) { |
| 54 | self::get_wordads_status(); |
| 55 | } |
| 56 | |
| 57 | return self::$wordads_status['approved'] ? '1' : '0'; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns status of WordAds active. |
| 62 | * @return boolean true if ads are active on site |
| 63 | * |
| 64 | * @since 4.5.0 |
| 65 | */ |
| 66 | public static function is_wordads_active() { |
| 67 | if ( is_null( self::$wordads_status ) ) { |
| 68 | self::get_wordads_status(); |
| 69 | } |
| 70 | |
| 71 | return self::$wordads_status['active'] ? '1' : '0'; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns status of WordAds house ads. |
| 76 | * @return boolean true if WP.com house ads should be shown |
| 77 | * |
| 78 | * @since 4.5.0 |
| 79 | */ |
| 80 | public static function is_wordads_house() { |
| 81 | if ( is_null( self::$wordads_status ) ) { |
| 82 | self::get_wordads_status(); |
| 83 | } |
| 84 | |
| 85 | return self::$wordads_status['house'] ? '1' : '0'; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Grab WordAds status from WP.com API and store as option |
| 90 | * |
| 91 | * @since 4.5.0 |
| 92 | */ |
| 93 | static function update_wordads_status_from_api() { |
| 94 | $status = self::get_wordads_status(); |
| 95 | if ( ! is_wp_error( $status ) ) { |
| 96 | update_option( 'wordads_approved', self::is_wordads_approved(), true ); |
| 97 | update_option( 'wordads_active', self::is_wordads_active(), true ); |
| 98 | update_option( 'wordads_house', self::is_wordads_house(), true ); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 |