Completed
Push — add/implement_pre_connection_j... ( 9bf864...afacc2 )
by
unknown
07:50
created

Pre_Connection_JITM::dismiss()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
/**
3
 * Jetpack's Pre-Connection JITM class.
4
 *
5
 * @package automattic/jetpack-jitm
6
 */
7
8
namespace Automattic\Jetpack\JITMS;
9
10
use Automattic\Jetpack\JITMS\JITM;
11
use Automattic\Jetpack\JITMS\Engine;
12
13
/**
14
 * Jetpack just in time messaging through out the admin
15
 */
16
class Pre_Connection_JITM extends JITM {
17
18
	const PACKAGE_VERSION = '1.0'; // TODO: Keep in sync with version specified in composer.json.
19
20
	/**
21
	 * Determines if JITMs are enabled.
22
	 *
23
	 * @return bool Enable JITMs.
24
	 */
25
	public function register() {
26
		add_action( 'current_screen', array( $this, 'prepare_jitms' ) );
27
		return true;
28
	}
29
30
	/**
31
	 * Retrieve the current message to display keyed on query string and message path
32
	 *
33
	 * @param string $message_path The message path to ask for.
34
	 * @param string $query The query string originally from the front end. Unused in this subclass.
35
	 * @param bool   $full_jp_logo_exists If there is a full Jetpack logo already on the page.
36
	 *
37
	 * @return array The JITM's to show, or an empty array if there is nothing to show
38
	 */
39
	public function get_messages( $message_path, $query, $full_jp_logo_exists ) {
40
		$jitm_engine = new Engine();
41
42
		$envelopes = $jitm_engine->render_messages( $message_path );
43
44
		if ( ! is_array( $envelopes ) ) {
45
			return array();
46
		}
47
48
		$hidden_jitms = \Jetpack_Options::get_option( 'hide_jitm' );
49
50
		foreach ( $envelopes as $idx => &$envelope ) {
51
			$dismissed_feature = isset( $hidden_jitms[ 'pre-connection-' . $envelope->id ] ) &&
52
				is_array( $hidden_jitms[ 'pre-connection-' . $envelope->id ] ) ? $hidden_jitms[ 'pre-connection-' . $envelope->id ] : null;
53
54
			if ( is_array( $dismissed_feature ) ) {
55
				unset( $envelopes[ $idx ] );
56
				continue;
57
			}
58
59
			$envelope->content['icon'] = $this->generate_icon( $envelope->content['icon'], $full_jp_logo_exists );
60
		}
61
62
		return $envelopes;
63
	}
64
65
	/**
66
	 * Dismisses a JITM ID so that it will no longer be shown.
67
	 *
68
	 * @param string $id The id of the JITM that was dismissed.
69
	 * @param string $feature_class The feature class of the JITM that was dismissed. Unused in this subclass.
70
	 *
71
	 * @return bool Always true
72
	 */
73
	public function dismiss( $id, $feature_class ) {
74
		$hide_jitm = \Jetpack_Options::get_option( 'hide_jitm' );
75
		if ( ! is_array( $hide_jitm ) ) {
76
			$hide_jitm = array();
77
		}
78
79
		if ( empty( $hide_jitm[ 'pre-connection-' . $id ] ) || ! is_array( $hide_jitm[ 'pre-connection-' . $id ] ) ) {
80
			$hide_jitm[ $id ] = array(
81
				'last_dismissal' => 0,
82
				'number'         => 0,
83
			);
84
		}
85
86
		$hide_jitm[ 'pre-connection-' . $id ] = array(
87
			'last_dismissal' => time(),
88
			'number'         => intval( $hide_jitm[ 'pre-connection-' . $id ]['number'] ) + 1,
89
		);
90
91
		\Jetpack_Options::update_option( 'hide_jitm', $hide_jitm );
92
93
		return true;
94
	}
95
}
96