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

WordAds_API   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 136
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_wordads_status() 0 29 3
A get_wordads_ads_txt() 0 11 2
A is_wordads_approved() 0 7 3
A is_wordads_active() 0 7 3
A is_wordads_house() 0 7 3
A is_wordads_unsafe() 0 7 3
A update_wordads_status_from_api() 0 9 2
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
Unused Code introduced by
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 );
0 ignored issues
show
Unused Code introduced by
$wordads_status_response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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
Unused Code introduced by
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