summaryrefslogtreecommitdiff
blob: f2308523020912593e0da0efef8eba3a088958b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Status;

/**
 * Methods for accessing data through the WPCOM REST API
 *
 * @since 4.5.0
 */
class WordAds_API {

	private static $wordads_status = null;

	/**
	 * Returns site's WordAds status
	 *
	 * @return array boolean values for 'approved' and 'active'
	 *
	 * @since 4.5.0
	 */
	public static function get_wordads_status() {
		global $wordads_status_response;
		if ( ( new Status() )->is_development_mode() ) {
			self::$wordads_status = array(
				'approved' => true,
				'active'   => true,
				'house'    => true,
				'unsafe'   => false,
			);

			return self::$wordads_status;
		}

		$endpoint                = sprintf( '/sites/%d/wordads/status', Jetpack::get_option( 'id' ) );
		$wordads_status_response = $response = Client::wpcom_json_api_request_as_blog( $endpoint );
		if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
			return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response );
		}

		$body                 = json_decode( wp_remote_retrieve_body( $response ) );
		self::$wordads_status = array(
			'approved' => $body->approved,
			'active'   => $body->active,
			'house'    => $body->house,
			'unsafe'   => $body->unsafe,
		);

		return self::$wordads_status;
	}

	/**
	 * Returns the ads.txt content needed to run WordAds.
	 *
	 * @return array string contents of the ads.txt file.
	 *
	 * @since 6.1.0
	 */
	public static function get_wordads_ads_txt() {
		$endpoint                = sprintf( '/sites/%d/wordads/ads-txt', Jetpack::get_option( 'id' ) );
		$wordads_status_response = $response = Client::wpcom_json_api_request_as_blog( $endpoint );
		if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
			return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response );
		}

		$body    = json_decode( wp_remote_retrieve_body( $response ) );
		$ads_txt = str_replace( '\\n', PHP_EOL, $body->adstxt );
		return $ads_txt;
	}

	/**
	 * Returns status of WordAds approval.
	 *
	 * @return boolean true if site is WordAds approved
	 *
	 * @since 4.5.0
	 */
	public static function is_wordads_approved() {
		if ( is_null( self::$wordads_status ) ) {
			self::get_wordads_status();
		}

		return self::$wordads_status['approved'] ? '1' : '0';
	}

	/**
	 * Returns status of WordAds active.
	 *
	 * @return boolean true if ads are active on site
	 *
	 * @since 4.5.0
	 */
	public static function is_wordads_active() {
		if ( is_null( self::$wordads_status ) ) {
			self::get_wordads_status();
		}

		return self::$wordads_status['active'] ? '1' : '0';
	}

	/**
	 * Returns status of WordAds house ads.
	 *
	 * @return boolean true if WP.com house ads should be shown
	 *
	 * @since 4.5.0
	 */
	public static function is_wordads_house() {
		if ( is_null( self::$wordads_status ) ) {
			self::get_wordads_status();
		}

		return self::$wordads_status['house'] ? '1' : '0';
	}


	/**
	 * Returns whether or not this site is safe to run ads on.
	 *
	 * @return boolean true if ads shown not be shown on this site.
	 *
	 * @since 6.5.0
	 */
	public static function is_wordads_unsafe() {
		if ( is_null( self::$wordads_status ) ) {
			self::get_wordads_status();
		}

		return self::$wordads_status['unsafe'] ? '1' : '0';
	}

	/**
	 * Grab WordAds status from WP.com API and store as option
	 *
	 * @since 4.5.0
	 */
	static function update_wordads_status_from_api() {
		$status = self::get_wordads_status();
		if ( ! is_wp_error( $status ) ) {
			update_option( 'wordads_approved', self::is_wordads_approved(), true );
			update_option( 'wordads_active', self::is_wordads_active(), true );
			update_option( 'wordads_house', self::is_wordads_house(), true );
			update_option( 'wordads_unsafe', self::is_wordads_unsafe(), true );
		}
	}
}