Completed
Push — renovate/slack-web-api-5.x ( 42edfe...6ad45d )
by
unknown
184:59 queued 173:57
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 Jetpack
6
 */
7
8
use Automattic\Jetpack\Connection\Client;
9
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
10
11
/**
12
 * Jetpack_Recommendations class
13
 */
14
class Jetpack_Recommendations {
15
	/**
16
	 * Returns a boolean indicating if the Jetpack Recommendations are enabled.
17
	 *
18
	 * @since 9.3.0
19
	 *
20
	 * @return bool
21
	 */
22
	public static function is_enabled() {
23
		$recommendations_enabled = Jetpack_Options::get_option( 'recommendations_enabled', null );
24
25
		// If the option is already set, just return the cached value.
26
		// Otherwise calculate it and store it before returning it.
27
		if ( null !== $recommendations_enabled ) {
28
			return $recommendations_enabled;
29
		}
30
31
		if ( ! Jetpack::connection()->is_connected() ) {
32
			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...
33
		}
34
35
		$blog_id = Jetpack_Options::get_option( 'id' );
36
37
		$request_path = sprintf( '/sites/%s/jetpack-recommendations/site-registered-date', $blog_id );
38
		$result       = Client::wpcom_json_api_request_as_blog(
39
			$request_path,
40
			2,
41
			array(
42
				'headers' => array( 'content-type' => 'application/json' ),
43
			),
44
			null,
45
			'wpcom'
46
		);
47
48
		$body = json_decode( wp_remote_retrieve_body( $result ) );
49
		if ( 200 === wp_remote_retrieve_response_code( $result ) ) {
50
			$site_registered_date = $body->site_registered_date;
51
		} else {
52
			$connection           = new Connection_Manager( 'jetpack' );
53
			$site_registered_date = $connection->get_assumed_site_creation_date();
54
		}
55
56
		$recommendations_start_date = gmdate( 'Y-m-d H:i:s', strtotime( '2020-12-01 00:00:00' ) );
57
		$recommendations_enabled    = $site_registered_date > $recommendations_start_date;
58
59
		Jetpack_Options::update_option( 'recommendations_enabled', $recommendations_enabled );
60
61
		return $recommendations_enabled;
62
	}
63
64
	/**
65
	 * Initializes the Recommendations step according to the Setup Wizard state, and then clears the
66
	 * Setup Wizard state.
67
	 */
68
	private static function initialize_jetpack_recommendations() {
69
		if ( Jetpack_Options::get_option( 'recommendations_step' ) ) {
70
			return;
71
		}
72
73
		$setup_wizard_status = Jetpack_Options::get_option( 'setup_wizard_status' );
74
		if ( 'completed' === $setup_wizard_status ) {
75
			Jetpack_Options::update_option( 'recommendations_step', 'completed' );
76
		}
77
78
		Jetpack_Options::delete_option( array( 'setup_wizard_questionnaire', 'setup_wizard_status' ) );
79
	}
80
81
	/**
82
	 * Get the data for the recommendations
83
	 *
84
	 * @return array Recommendations data
85
	 */
86
	public static function get_recommendations_data() {
87
		self::initialize_jetpack_recommendations();
88
89
		return Jetpack_Options::get_option( 'recommendations_data', (object) array() );
90
	}
91
92
	/**
93
	 * Update the data for the recommendations
94
	 *
95
	 * @param WP_REST_Request $data The data.
96
	 */
97
	public static function update_recommendations_data( $data ) {
98
		if ( ! empty( $data ) ) {
99
			Jetpack_Options::update_option( 'recommendations_data', $data );
100
		}
101
	}
102
103
	/**
104
	 * Get the data for the recommendations
105
	 *
106
	 * @return array Recommendations data
107
	 */
108
	public static function get_recommendations_step() {
109
		self::initialize_jetpack_recommendations();
110
111
		return array(
112
			'step' => Jetpack_Options::get_option( 'recommendations_step', 'not-started' ),
113
		);
114
	}
115
116
	/**
117
	 * Update the step for the recommendations
118
	 *
119
	 * @param WP_REST_Request $step The step.
120
	 */
121
	public static function update_recommendations_step( $step ) {
122
		if ( ! empty( $step ) ) {
123
			Jetpack_Options::update_option( 'recommendations_step', $step );
124
		}
125
	}
126
}
127