Completed
Push — add/constants ( 627491...bc42a9 )
by
unknown
203:10 queued 193:38
created

update_recommendations_data()   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 automattic/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
		// No recommendations for Atomic sites, they already get onboarded in Calypso.
30
		if ( jetpack_is_atomic_site() ) {
31
			return false;
32
		}
33
34
		self::initialize_jetpack_recommendations();
35
36
		return true;
37
	}
38
39
	/**
40
	 * Returns a boolean indicating if the Jetpack Banner is enabled.
41
	 *
42
	 * @since 9.3.0
43
	 *
44
	 * @return bool
45
	 */
46
	public static function is_banner_enabled() {
47
		// Shortcircuit early if the recommendations are not enabled at all.
48
		if ( ! self::is_enabled() ) {
49
			return false;
50
		}
51
52
		$recommendations_banner_enabled = Jetpack_Options::get_option( 'recommendations_banner_enabled', null );
53
54
		// If the option is already set, just return the cached value.
55
		// Otherwise calculate it and store it before returning it.
56
		if ( null !== $recommendations_banner_enabled ) {
57
			return $recommendations_banner_enabled;
58
		}
59
60
		if ( ! Jetpack::connection()->is_connected() ) {
61
			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...
62
		}
63
64
		$blog_id = Jetpack_Options::get_option( 'id' );
65
66
		$request_path = sprintf( '/sites/%s/jetpack-recommendations/site-registered-date', $blog_id );
67
		$result       = Client::wpcom_json_api_request_as_blog(
68
			$request_path,
69
			2,
70
			array(
71
				'headers' => array( 'content-type' => 'application/json' ),
72
			),
73
			null,
74
			'wpcom'
75
		);
76
77
		$body = json_decode( wp_remote_retrieve_body( $result ) );
78
		if ( 200 === wp_remote_retrieve_response_code( $result ) ) {
79
			$site_registered_date = $body->site_registered_date;
80
		} else {
81
			$connection           = new Connection_Manager( 'jetpack' );
82
			$site_registered_date = $connection->get_assumed_site_creation_date();
83
		}
84
85
		$recommendations_start_date     = gmdate( 'Y-m-d H:i:s', strtotime( '2020-12-01 00:00:00' ) );
86
		$recommendations_banner_enabled = $site_registered_date > $recommendations_start_date;
87
88
		Jetpack_Options::update_option( 'recommendations_banner_enabled', $recommendations_banner_enabled );
89
90
		return $recommendations_banner_enabled;
91
	}
92
93
	/**
94
	 * Initializes the Recommendations step according to the Setup Wizard state.
95
	 */
96
	private static function initialize_jetpack_recommendations() {
97
		if ( Jetpack_Options::get_option( 'recommendations_step' ) ) {
98
			return;
99
		}
100
101
		$setup_wizard_status = Jetpack_Options::get_option( 'setup_wizard_status' );
102
		if ( 'completed' === $setup_wizard_status ) {
103
			Jetpack_Options::update_option( 'recommendations_banner_enabled', false );
104
			Jetpack_Options::update_option( 'recommendations_step', 'setup-wizard-completed' );
105
		}
106
	}
107
108
	/**
109
	 * Get the data for the recommendations
110
	 *
111
	 * @return array Recommendations data
112
	 */
113
	public static function get_recommendations_data() {
114
		self::initialize_jetpack_recommendations();
115
116
		return Jetpack_Options::get_option( 'recommendations_data', (object) array() );
117
	}
118
119
	/**
120
	 * Update the data for the recommendations
121
	 *
122
	 * @param WP_REST_Request $data The data.
123
	 */
124
	public static function update_recommendations_data( $data ) {
125
		if ( ! empty( $data ) ) {
126
			Jetpack_Options::update_option( 'recommendations_data', $data );
127
		}
128
	}
129
130
	/**
131
	 * Get the data for the recommendations
132
	 *
133
	 * @return array Recommendations data
134
	 */
135
	public static function get_recommendations_step() {
136
		self::initialize_jetpack_recommendations();
137
138
		return array(
139
			'step' => Jetpack_Options::get_option( 'recommendations_step', 'not-started' ),
140
		);
141
	}
142
143
	/**
144
	 * Update the step for the recommendations
145
	 *
146
	 * @param WP_REST_Request $step The step.
147
	 */
148
	public static function update_recommendations_step( $step ) {
149
		if ( ! empty( $step ) ) {
150
			Jetpack_Options::update_option( 'recommendations_step', $step );
151
		}
152
	}
153
}
154