Completed
Push — add/jetpack-assistant-ui ( a6f776...33ce41 )
by Jeremy
202:03 queued 191:10
created

update_recommendations_step()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 5
rs 10
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
		$recommendations_enabled = Jetpack_Options::get_option( 'recommendations_enabled', null );
32
33
		// If the option is already set, just return the cached value.
34
		// Otherwise calculate it and store it before returning it.
35
		if ( null !== $recommendations_enabled ) {
36
			return $recommendations_enabled;
37
		}
38
39
		if ( ! Jetpack::connection()->is_connected() ) {
40
			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...
41
		}
42
43
		$blog_id = Jetpack_Options::get_option( 'id' );
44
45
		$request_path = sprintf( '/sites/%s/jetpack-recommendations/site-registered-date', $blog_id );
46
		$result       = Client::wpcom_json_api_request_as_blog(
47
			$request_path,
48
			2,
49
			array(
50
				'headers' => array( 'content-type' => 'application/json' ),
51
			),
52
			null,
53
			'wpcom'
54
		);
55
56
		$body = json_decode( wp_remote_retrieve_body( $result ) );
57
		if ( 200 === wp_remote_retrieve_response_code( $result ) ) {
58
			$site_registered_date = $body->site_registered_date;
59
		} else {
60
			$connection           = new Connection_Manager( 'jetpack' );
61
			$site_registered_date = $connection->get_assumed_site_creation_date();
62
		}
63
64
		$recommendations_start_date = gmdate( 'Y-m-d H:i:s', strtotime( '2020-12-01 00:00:00' ) );
65
		$recommendations_enabled    = $site_registered_date > $recommendations_start_date;
66
67
		Jetpack_Options::update_option( 'recommendations_enabled', $recommendations_enabled );
68
69
		return $recommendations_enabled;
70
	}
71
72
	/**
73
	 * Initializes the Recommendations step according to the Setup Wizard state.
74
	 */
75
	private static function initialize_jetpack_recommendations() {
76
		if ( Jetpack_Options::get_option( 'recommendations_step' ) ) {
77
			return;
78
		}
79
80
		$setup_wizard_status = Jetpack_Options::get_option( 'setup_wizard_status' );
81
		if ( 'completed' === $setup_wizard_status ) {
82
			Jetpack_Options::update_option( 'recommendations_enabled', false );
83
			Jetpack_Options::update_option( 'recommendations_step', 'setup_wizard_completed' );
84
		}
85
	}
86
87
	/**
88
	 * Get the data for the recommendations
89
	 *
90
	 * @return array Recommendations data
91
	 */
92
	public static function get_recommendations_data() {
93
		self::initialize_jetpack_recommendations();
94
95
		return Jetpack_Options::get_option( 'recommendations_data', (object) array() );
96
	}
97
98
	/**
99
	 * Update the data for the recommendations
100
	 *
101
	 * @param WP_REST_Request $data The data.
102
	 */
103
	public static function update_recommendations_data( $data ) {
104
		if ( ! empty( $data ) ) {
105
			Jetpack_Options::update_option( 'recommendations_data', $data );
106
		}
107
	}
108
109
	/**
110
	 * Get the data for the recommendations
111
	 *
112
	 * @return array Recommendations data
113
	 */
114
	public static function get_recommendations_step() {
115
		self::initialize_jetpack_recommendations();
116
117
		return array(
118
			'step' => Jetpack_Options::get_option( 'recommendations_step', 'not-started' ),
119
		);
120
	}
121
122
	/**
123
	 * Update the step for the recommendations
124
	 *
125
	 * @param WP_REST_Request $step The step.
126
	 */
127
	public static function update_recommendations_step( $step ) {
128
		if ( ! empty( $step ) ) {
129
			Jetpack_Options::update_option( 'recommendations_step', $step );
130
		}
131
	}
132
}
133