Completed
Push — update/wpcom-block-editor-shor... ( 9897fe...f43ed6 )
by Jeremy
148:26 queued 138:20
created

Pre_Connection_JITM::dismiss()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
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
/**
11
 * Jetpack pre-connection just in time messaging through out the admin.
12
 */
13
class Pre_Connection_JITM extends JITM {
14
15
	/**
16
	 * Filters and formats the messages for the client-side JS renderer
17
	 *
18
	 * @param string $message_path Current message path.
19
	 *
20
	 * @return array Formatted messages.
21
	 */
22
	private function filter_messages( $message_path ) {
23
		/**
24
		 * Allows filtering of the pre-connection JITMs.
25
		 *
26
		 * This filter allows plugins to add pre-connection JITMs that will be
27
		 * displayed by the JITM package.
28
		 *
29
		 * @since 9.6.0
30
		 *
31
		 * @param array An array of pre-connection messages.
32
		 */
33
		$messages = apply_filters( 'jetpack_pre_connection_jitms', array() );
34
35
		$messages = $this->validate_messages( $messages );
36
37
		$formatted_messages = array();
38
39
		foreach ( $messages as $message ) {
40
			if ( ! preg_match( $message['message_path'], $message_path ) ) {
41
				continue;
42
			}
43
44
			$obj                 = new \stdClass();
45
			$obj->CTA            = array( // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
46
				'message'   => $message['button_caption'],
47
				'newWindow' => false,
48
			);
49
			$obj->url            = $message['button_link'];
50
			$obj->id             = $message['id'];
51
			$obj->is_dismissible = true;
52
			$obj->content        = array(
53
				'message'     => $message['message'],
54
				'description' => $message['description'],
55
				'list'        => array(),
56
				'icon'        => 'jetpack',
57
			);
58
59
			$formatted_messages[] = $obj;
60
		}
61
62
		return $formatted_messages;
63
	}
64
65
	/**
66
	 * Validates that each of the messages contains all of the required keys:
67
	 *   - id
68
	 *   - message_path
69
	 *   - message
70
	 *   - description
71
	 *   - button_link
72
	 *   - button_caption
73
	 *
74
	 * @param array $messages An array of JITM messages.
75
	 *
76
	 * @return array An array of JITM messages that contain all of the required keys.
77
	 */
78
	private function validate_messages( $messages ) {
79
		if ( ! is_array( $messages ) ) {
80
			return array();
81
		}
82
83
		$expected_keys = array_flip( array( 'id', 'message_path', 'message', 'description', 'button_link', 'button_caption' ) );
84
85
		foreach ( $messages as $index => $message ) {
86
			if ( count( array_intersect_key( $expected_keys, $message ) ) !== count( $expected_keys ) ) {
87
				// Remove any messages that are missing expected keys.
88
				unset( $messages[ $index ] );
89
			}
90
		}
91
92
		return $messages;
93
	}
94
95
	/**
96
	 * Retrieve the current message to display keyed on query string and message path
97
	 *
98
	 * @param string $message_path The message path to ask for.
99
	 * @param string $query The query string originally from the front end. Unused in this subclass.
100
	 * @param bool   $full_jp_logo_exists If there is a full Jetpack logo already on the page.
101
	 *
102
	 * @return array The JITMs to show, or an empty array if there is nothing to show
103
	 */
104
	public function get_messages( $message_path, $query, $full_jp_logo_exists ) {
105
		/** This filter is documented in  class.jetpack-connection-banner.php */
106
		if ( ! apply_filters( 'jetpack_pre_connection_prompt_helpers', false ) ) {
107
			// If filter jetpack_pre_connection_prompt_helpers is not set, return an empty array.
108
			return array();
109
		}
110
111
		if ( ! current_user_can( 'install_plugins' ) ) {
112
			return array();
113
		}
114
115
		$messages = $this->filter_messages( $message_path );
116
117
		if ( empty( $messages ) ) {
118
			return array();
119
		}
120
121
		$hidden_jitms = \Jetpack_Options::get_option( 'hide_jitm' );
122
123
		foreach ( $messages as $idx => &$envelope ) {
124
			$dismissed_feature = isset( $hidden_jitms[ 'pre-connection-' . $envelope->id ] ) &&
125
				is_array( $hidden_jitms[ 'pre-connection-' . $envelope->id ] ) ? $hidden_jitms[ 'pre-connection-' . $envelope->id ] : null;
126
127
			if ( is_array( $dismissed_feature ) ) {
128
				unset( $messages[ $idx ] );
129
				continue;
130
			}
131
132
			$envelope->content['icon'] = $this->generate_icon( $envelope->content['icon'], $full_jp_logo_exists );
133
		}
134
135
		return $messages;
136
	}
137
138
	/**
139
	 * Dismisses a JITM ID so that it will no longer be shown.
140
	 *
141
	 * @param string $id The id of the JITM that was dismissed.
142
	 *
143
	 * @return bool Always true
144
	 */
145
	public function dismiss( $id ) {
146
		$this->save_dismiss( 'pre-connection-' . $id );
147
		return true;
148
	}
149
}
150