api.php 3.6 KB

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