WordAds_API

Methods for accessing data through the WPCOM REST API.

Defined (1)

The class is defined in the following location(s).

/modules/wordads/php/api.php  
  1. class WordAds_API { 
  2.  
  3. private static $wordads_status = null; 
  4.  
  5. /** 
  6. * Returns site's WordAds status 
  7. * @return array boolean values for 'approved' and 'active' 
  8. * @since 4.5.0 
  9. */ 
  10. public static function get_wordads_status() { 
  11. global $wordads_status_response; 
  12. if ( Jetpack::is_development_mode() ) { 
  13. self::$wordads_status = array( 
  14. 'approved' => true,  
  15. 'active' => true,  
  16. 'house' => true,  
  17. ); 
  18.  
  19. return self::$wordads_status; 
  20.  
  21. $endpoint = sprintf( '/sites/%d/wordads/status', Jetpack::get_option( 'id' ) ); 
  22. $wordads_status_response = $response = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint ); 
  23. if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { 
  24. return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response ); 
  25.  
  26. $body = json_decode( wp_remote_retrieve_body( $response ) ); 
  27. self::$wordads_status = array( 
  28. 'approved' => $body->approved,  
  29. 'active' => $body->active,  
  30. 'house' => $body->house,  
  31. ); 
  32.  
  33. return self::$wordads_status; 
  34.  
  35. /** 
  36. * Returns status of WordAds approval. 
  37. * @return boolean true if site is WordAds approved 
  38. * @since 4.5.0 
  39. */ 
  40. public static function is_wordads_approved() { 
  41. if ( is_null( self::$wordads_status ) ) { 
  42. self::get_wordads_status(); 
  43.  
  44. return self::$wordads_status['approved'] ? '1' : '0'; 
  45.  
  46. /** 
  47. * Returns status of WordAds active. 
  48. * @return boolean true if ads are active on site 
  49. * @since 4.5.0 
  50. */ 
  51. public static function is_wordads_active() { 
  52. if ( is_null( self::$wordads_status ) ) { 
  53. self::get_wordads_status(); 
  54.  
  55. return self::$wordads_status['active'] ? '1' : '0'; 
  56.  
  57. /** 
  58. * Returns status of WordAds house ads. 
  59. * @return boolean true if WP.com house ads should be shown 
  60. * @since 4.5.0 
  61. */ 
  62. public static function is_wordads_house() { 
  63. if ( is_null( self::$wordads_status ) ) { 
  64. self::get_wordads_status(); 
  65.  
  66. return self::$wordads_status['house'] ? '1' : '0'; 
  67.  
  68. /** 
  69. * Grab WordAds status from WP.com API and store as option 
  70. * @since 4.5.0 
  71. */ 
  72. static function update_wordads_status_from_api() { 
  73. $status = self::get_wordads_status(); 
  74. if ( ! is_wp_error( $status ) ) { 
  75. update_option( 'wordads_approved', self::is_wordads_approved(), true ); 
  76. update_option( 'wordads_active', self::is_wordads_active(), true ); 
  77. update_option( 'wordads_house', self::is_wordads_house(), true );