Completed
Push — add/e2e-mailchimp-block-test ( e217db...6066d0 )
by Yaroslav
98:30 queued 85:55
created

modules/wordads/php/api.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'api_error'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'api_error'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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