PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.9
Jetpack – WP Security, Backup, Speed, & Growth v7.9
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / wordads / php / api.php

api.php in Jetpack – WP Security, Backup, Speed, & Growth 7.9, at modules/wordads/php/api.php

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