Completed
Push — jetpack/branch-9.4 ( dac9f4...d7da83 )
by Jeremy
32:09 queued 21:46
created

Jetpack_Recommendations::is_enabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * Utilities related to the Jetpack Recommendations
4
 *
5
 * @package Jetpack
6
 */
7
8
use Automattic\Jetpack\Connection\Client;
9
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
10
use Automattic\Jetpack\Status;
11
12
/**
13
 * Jetpack_Recommendations class
14
 */
15
class Jetpack_Recommendations {
16
	/**
17
	 * Returns a boolean indicating if the Jetpack Recommendations are enabled.
18
	 *
19
	 * @since 9.3.0
20
	 *
21
	 * @return bool
22
	 */
23
	public static function is_enabled() {
24
		// Shortcircuit early if we are in offline mode.
25
		if ( ( new Status() )->is_offline_mode() ) {
26
			return false;
27
		}
28
29
		self::initialize_jetpack_recommendations();
30
31
		return true;
32
	}
33
34
	/**
35
	 * Returns a boolean indicating if the Jetpack Banner is enabled.
36
	 *
37
	 * @since 9.3.0
38
	 *
39
	 * @return bool
40
	 */
41
	public static function is_banner_enabled() {
42
		// Shortcircuit early if the recommendations are not enabled at all.
43
		if ( ! self::is_enabled() ) {
44
			return false;
45
		}
46
47
		$recommendations_banner_enabled = Jetpack_Options::get_option( 'recommendations_banner_enabled', null );
48
49
		// If the option is already set, just return the cached value.
50
		// Otherwise calculate it and store it before returning it.
51
		if ( null !== $recommendations_banner_enabled ) {
52
			return $recommendations_banner_enabled;
53
		}
54
55
		if ( ! Jetpack::connection()->is_connected() ) {
56
			return new WP_Error( 'site_not_connected', esc_html__( 'Site not connected.', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'site_not_connected'.

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...
57
		}
58
59
		$blog_id = Jetpack_Options::get_option( 'id' );
60
61
		$request_path = sprintf( '/sites/%s/jetpack-recommendations/site-registered-date', $blog_id );
62
		$result       = Client::wpcom_json_api_request_as_blog(
63
			$request_path,
64
			2,
65
			array(
66
				'headers' => array( 'content-type' => 'application/json' ),
67
			),
68
			null,
69
			'wpcom'
70
		);
71
72
		$body = json_decode( wp_remote_retrieve_body( $result ) );
73
		if ( 200 === wp_remote_retrieve_response_code( $result ) ) {
74
			$site_registered_date = $body->site_registered_date;
75
		} else {
76
			$connection           = new Connection_Manager( 'jetpack' );
77
			$site_registered_date = $connection->get_assumed_site_creation_date();
78
		}
79
80
		$recommendations_start_date     = gmdate( 'Y-m-d H:i:s', strtotime( '2020-12-01 00:00:00' ) );
81
		$recommendations_banner_enabled = $site_registered_date > $recommendations_start_date;
82
83
		Jetpack_Options::update_option( 'recommendations_banner_enabled', $recommendations_banner_enabled );
84
85
		return $recommendations_banner_enabled;
86
	}
87
88
	/**
89
	 * Initializes the Recommendations step according to the Setup Wizard state.
90
	 */
91
	private static function initialize_jetpack_recommendations() {
92
		if ( Jetpack_Options::get_option( 'recommendations_step' ) ) {
93
			return;
94
		}
95
96
		$setup_wizard_status = Jetpack_Options::get_option( 'setup_wizard_status' );
97
		if ( 'completed' === $setup_wizard_status ) {
98
			Jetpack_Options::update_option( 'recommendations_banner_enabled', false );
99
			Jetpack_Options::update_option( 'recommendations_step', 'setup-wizard-completed' );
100
		}
101
	}
102
103
	/**
104
	 * Get the data for the recommendations
105
	 *
106
	 * @return array Recommendations data
107
	 */
108
	public static function get_recommendations_data() {
109
		self::initialize_jetpack_recommendations();
110
111
		return Jetpack_Options::get_option( 'recommendations_data', (object) array() );
112
	}
113
114
	/**
115
	 * Update the data for the recommendations
116
	 *
117
	 * @param WP_REST_Request $data The data.
118
	 */
119
	public static function update_recommendations_data( $data ) {
120
		if ( ! empty( $data ) ) {
121
			Jetpack_Options::update_option( 'recommendations_data', $data );
122
		}
123
	}
124
125
	/**
126
	 * Get the data for the recommendations
127
	 *
128
	 * @return array Recommendations data
129
	 */
130
	public static function get_recommendations_step() {
131
		self::initialize_jetpack_recommendations();
132
133
		return array(
134
			'step' => Jetpack_Options::get_option( 'recommendations_step', 'not-started' ),
135
		);
136
	}
137
138
	/**
139
	 * Update the step for the recommendations
140
	 *
141
	 * @param WP_REST_Request $step The step.
142
	 */
143
	public static function update_recommendations_step( $step ) {
144
		if ( ! empty( $step ) ) {
145
			Jetpack_Options::update_option( 'recommendations_step', $step );
146
		}
147
	}
148
}
149