Completed
Push — update/wordads-brand-safety ( 32a060 )
by
unknown
09:42
created

WordAds_API::is_wordads_unsafe()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Methods for accessing data through the WPCOM REST API
5
 *
6
 * @since 4.5.0
7
 */
8
class WordAds_API {
9
10
	private static $wordads_status = null;
11
12
	/**
13
	 * Returns site's WordAds status
14
	 * @return array boolean values for 'approved' and 'active'
15
	 *
16
	 * @since 4.5.0
17
	 */
18
	public static function get_wordads_status() {
19
		global $wordads_status_response;
20
		if ( Jetpack::is_development_mode() ) {
21
			self::$wordads_status = array(
22
				'approved' => true,
23
				'active'   => true,
24
				'house'    => true,
25
				'unsafe'   => false,
26
			);
27
28
			return self::$wordads_status;
29
		}
30
31
		$endpoint = sprintf( '/sites/%d/wordads/status', Jetpack::get_option( 'id' ) );
32
		$wordads_status_response = $response = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint );
33
		if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
34
			return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response );
35
		}
36
37
		$body = json_decode( wp_remote_retrieve_body( $response ) );
38
		self::$wordads_status = array(
39
			'approved' => $body->approved,
40
			'active'   => $body->active,
41
			'house'    => $body->house,
42
			'unsafe'   => $body->unsafe,
43
		);
44
45
		return self::$wordads_status;
46
	}
47
48
	/**
49
	 * Returns the ads.txt content needed to run WordAds.
50
	 * @return array string contents of the ads.txt file.
51
	 *
52
	 * @since 6.1.0
53
	 */
54
	public static function get_wordads_ads_txt() {
55
		$endpoint = sprintf( '/sites/%d/wordads/ads-txt', Jetpack::get_option( 'id' ) );
56
		$wordads_status_response = $response = Jetpack_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...
57
		if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
58
			return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response );
59
		}
60
61
		$body = json_decode( wp_remote_retrieve_body( $response ) );
62
		$ads_txt = str_replace( '\\n', PHP_EOL, $body->adstxt );
63
		return $ads_txt;
64
	}
65
66
	/**
67
	 * Returns status of WordAds approval.
68
	 * @return boolean true if site is WordAds approved
69
	 *
70
	 * @since 4.5.0
71
	 */
72
	public static function is_wordads_approved() {
73
		if ( is_null( self::$wordads_status ) ) {
74
			self::get_wordads_status();
75
		}
76
77
		return self::$wordads_status['approved'] ? '1' : '0';
78
	}
79
80
	/**
81
	 * Returns status of WordAds active.
82
	 * @return boolean true if ads are active on site
83
	 *
84
	 * @since 4.5.0
85
	 */
86
	public static function is_wordads_active() {
87
		if ( is_null( self::$wordads_status ) ) {
88
			self::get_wordads_status();
89
		}
90
91
		return self::$wordads_status['active'] ? '1' : '0';
92
	}
93
94
	/**
95
	 * Returns status of WordAds house ads.
96
	 * @return boolean true if WP.com house ads should be shown
97
	 *
98
	 * @since 4.5.0
99
	 */
100
	public static function is_wordads_house() {
101
		if ( is_null( self::$wordads_status ) ) {
102
			self::get_wordads_status();
103
		}
104
105
		return self::$wordads_status['house'] ? '1' : '0';
106
	}
107
108
109
	/**
110
	 * Returns whether or not this site is safe to run ads on.
111
	 * @return boolean true if ads shown not be shown on this site.
112
	 *
113
	 * @since @next @TODO
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
114
	 */
115
	public static function is_wordads_unsafe() {
116
		if ( is_null( self::$wordads_status ) ) {
117
			self::get_wordads_status();
118
		}
119
120
		return self::$wordads_status['unsafe'] ? '1' : '0';
121
	}
122
123
	/**
124
	 * Grab WordAds status from WP.com API and store as option
125
	 *
126
	 * @since 4.5.0
127
	 */
128
	static function update_wordads_status_from_api() {
129
		$status = self::get_wordads_status();
130
		if ( ! is_wp_error( $status ) ) {
131
			update_option( 'wordads_approved', self::is_wordads_approved(), true );
132
			update_option( 'wordads_active', self::is_wordads_active(), true );
133
			update_option( 'wordads_house', self::is_wordads_house(), true );
134
			update_option( 'wordads_unsafe', self::is_wordads_unsafe(), true );
135
		}
136
	}
137
}
138