PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.2 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 All 502 releases
jetpack / modules / wordads / php / class-wordads-api.php

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

103 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The WordAds API.
4 *
5 * @package automattic/jetpack
6 */
7
8 use Automattic\Jetpack\Connection\Client;
9 use Automattic\Jetpack\Status;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit( 0 );
13 }
14
15 /**
16 * Methods for accessing data through the WPCOM REST API
17 *
18 * @since 4.5.0
19 */
20 class WordAds_API {
21
22 /**
23 * Get the site's WordAds status
24 *
25 * @return array|WP_Error Array of site status values, or WP_Error if no response from the API.
26 *
27 * @since 4.5.0
28 */
29 public static function get_wordads_status() {
30 global $wordads_status_response;
31
32 // If the site is not connected, we can put it in a safe "house ad" mode.
33 if ( ( new Status() )->is_offline_mode() ) {
34 return array(
35 'approved' => true,
36 'active' => true,
37 'house' => true,
38 'unsafe' => false,
39 );
40 }
41
42 // Fetch the status from WPCOM endpoint.
43 $endpoint = sprintf( '/sites/%d/wordads/status', Jetpack::get_option( 'id' ) );
44 $response = Client::wpcom_json_api_request_as_blog( $endpoint );
45 $wordads_status_response = $response;
46
47 if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
48 return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response );
49 }
50
51 $body = json_decode( wp_remote_retrieve_body( $response ) );
52
53 return array(
54 'approved' => (bool) $body->approved,
55 'active' => (bool) $body->active,
56 'house' => (bool) $body->house,
57 'unsafe' => (bool) $body->unsafe,
58 );
59 }
60
61 /**
62 * Grab WordAds status from WP.com API and store as option
63 *
64 * @since 4.5.0
65 */
66 public static function update_wordads_status_from_api() {
67 $status = self::get_wordads_status();
68
69 if ( ! is_wp_error( $status ) ) {
70
71 // Convert boolean options to string first to work around update_option not setting the option if the value is false.
72 // This sets the option to either '1' if true or '' if false.
73 update_option( 'wordads_approved', (string) $status['approved'], true );
74 update_option( 'wordads_active', (string) $status['active'], true );
75 update_option( 'wordads_house', (string) $status['house'], true );
76 update_option( 'wordads_unsafe', (string) $status['unsafe'], true );
77 }
78 }
79
80 /**
81 * Returns the ads.txt content needed to run WordAds.
82 *
83 * @return array string contents of the ads.txt file.
84 *
85 * @since 6.1.0
86 */
87 public static function get_wordads_ads_txt() {
88 global $wordads_status_response;
89
90 $endpoint = sprintf( '/sites/%d/wordads/ads-txt', Jetpack::get_option( 'id' ) );
91 $response = Client::wpcom_json_api_request_as_blog( $endpoint );
92 $wordads_status_response = $response;
93 if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
94 return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response );
95 }
96
97 $body = json_decode( wp_remote_retrieve_body( $response ) );
98 $ads_txt = str_replace( '\\n', PHP_EOL, $body->adstxt );
99
100 return $ads_txt;
101 }
102 }
103