PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 8.2.3
Jetpack – WP Security, Backup, Speed, & Growth v8.2.3
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 8.2.3, at modules/wordads/php/api.php

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