PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.1.4
Jetpack – WP Security, Backup, Speed, & Growth v7.1.4
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 14.4.2 All 500 releases
jetpack / modules / wordads / php / api.php
api.php
144 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 /**
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 *
15 * @return array boolean values for 'approved' and 'active'
16 *
17 * @since 4.5.0
18 */
19 public static function get_wordads_status() {
20 global $wordads_status_response;
21 if ( Jetpack::is_development_mode() ) {
22 self::$wordads_status = array(
23 'approved' => true,
24 'active' => true,
25 'house' => true,
26 'unsafe' => false,
27 );
28
29 return self::$wordads_status;
30 }
31
32 $endpoint = sprintf( '/sites/%d/wordads/status', Jetpack::get_option( 'id' ) );
33 $wordads_status_response = $response = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint );
34 if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
35 return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response );
36 }
37
38 $body = json_decode( wp_remote_retrieve_body( $response ) );
39 self::$wordads_status = array(
40 'approved' => $body->approved,
41 'active' => $body->active,
42 'house' => $body->house,
43 'unsafe' => $body->unsafe,
44 );
45
46 return self::$wordads_status;
47 }
48
49 /**
50 * Returns the ads.txt content needed to run WordAds.
51 *
52 * @return array string contents of the ads.txt file.
53 *
54 * @since 6.1.0
55 */
56 public static function get_wordads_ads_txt() {
57 $endpoint = sprintf( '/sites/%d/wordads/ads-txt', Jetpack::get_option( 'id' ) );
58 $wordads_status_response = $response = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint );
59 if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
60 return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response );
61 }
62
63 $body = json_decode( wp_remote_retrieve_body( $response ) );
64 $ads_txt = str_replace( '\\n', PHP_EOL, $body->adstxt );
65 return $ads_txt;
66 }
67
68 /**
69 * Returns status of WordAds approval.
70 *
71 * @return boolean true if site is WordAds approved
72 *
73 * @since 4.5.0
74 */
75 public static function is_wordads_approved() {
76 if ( is_null( self::$wordads_status ) ) {
77 self::get_wordads_status();
78 }
79
80 return self::$wordads_status['approved'] ? '1' : '0';
81 }
82
83 /**
84 * Returns status of WordAds active.
85 *
86 * @return boolean true if ads are active on site
87 *
88 * @since 4.5.0
89 */
90 public static function is_wordads_active() {
91 if ( is_null( self::$wordads_status ) ) {
92 self::get_wordads_status();
93 }
94
95 return self::$wordads_status['active'] ? '1' : '0';
96 }
97
98 /**
99 * Returns status of WordAds house ads.
100 *
101 * @return boolean true if WP.com house ads should be shown
102 *
103 * @since 4.5.0
104 */
105 public static function is_wordads_house() {
106 if ( is_null( self::$wordads_status ) ) {
107 self::get_wordads_status();
108 }
109
110 return self::$wordads_status['house'] ? '1' : '0';
111 }
112
113
114 /**
115 * Returns whether or not this site is safe to run ads on.
116 *
117 * @return boolean true if ads shown not be shown on this site.
118 *
119 * @since 6.5.0
120 */
121 public static function is_wordads_unsafe() {
122 if ( is_null( self::$wordads_status ) ) {
123 self::get_wordads_status();
124 }
125
126 return self::$wordads_status['unsafe'] ? '1' : '0';
127 }
128
129 /**
130 * Grab WordAds status from WP.com API and store as option
131 *
132 * @since 4.5.0
133 */
134 static function update_wordads_status_from_api() {
135 $status = self::get_wordads_status();
136 if ( ! is_wp_error( $status ) ) {
137 update_option( 'wordads_approved', self::is_wordads_approved(), true );
138 update_option( 'wordads_active', self::is_wordads_active(), true );
139 update_option( 'wordads_house', self::is_wordads_house(), true );
140 update_option( 'wordads_unsafe', self::is_wordads_unsafe(), true );
141 }
142 }
143 }
144