Completed
Push — update/add_grunion_after_feedb... ( 614f06...9f6c1a )
by
unknown
07:04
created

Pre_Connection_JITM   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 123
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_raw_messages() 0 28 1
A filter_messages() 0 30 3
B get_messages() 0 29 7
A dismiss() 0 4 1
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
12
/**
13
 * Jetpack pre-connection just in time messaging through out the admin.
14
 */
15
class Pre_Connection_JITM extends JITM {
16
17
	/**
18
	 * Returns all the pre-connection messages.
19
	 */
20
	private function get_raw_messages() {
21
		return array(
22
			array(
23
				'id'             => 'jpsetup-posts',
24
				'message_path'   => '/wp:edit-post:admin_notices/',
25
				'message'        => __( 'Do you know which of these posts gets the most traffic?', 'jetpack' ),
26
				'description'    => __( 'Setup Jetpack to get in-depth stats about your content and visitors.', 'jetpack' ),
27
				'button_link'    => esc_url( \Jetpack::init()->build_connect_url( true, false, 'pre-connection-jitm-posts' ) ),
28
				'button_caption' => __( 'Setup Jetpack', 'jetpack' ),
29
			),
30
			array(
31
				'id'             => 'jpsetup-upload',
32
				'message_path'   => '/wp:upload:admin_notices/',
33
				'message'        => __( 'Do you want lightning-fast images?', 'jetpack' ),
34
				'description'    => __( 'Setup Jetpack, enable Site Accelerator, and start serving your images lightning fast, for free.', 'jetpack' ),
35
				'button_link'    => esc_url( \Jetpack::init()->build_connect_url( true, false, 'pre-connection-jitm-upload' ) ),
36
				'button_caption' => __( 'Setup Jetpack', 'jetpack' ),
37
			),
38
			array(
39
				'id'             => 'jpsetup-widgets',
40
				'message_path'   => '/wp:widgets:admin_notices/',
41
				'message'        => __( 'Looking for even more widgets?', 'jetpack' ),
42
				'description'    => __( 'Setup Jetpack for great additional widgets that display business contact info and maps, blog stats, and top posts.', 'jetpack' ),
43
				'button_link'    => esc_url( \Jetpack::init()->build_connect_url( true, false, 'pre-connection-jitm-widgets' ) ),
44
				'button_caption' => __( 'Setup Jetpack', 'jetpack' ),
45
			),
46
		);
47
	}
48
49
	/**
50
	 * Filters and formats the messages for the client-side JS renderer
51
	 *
52
	 * @param string $message_path Current message path.
53
	 *
54
	 * @return array Formatted messages.
55
	 */
56
	private function filter_messages( $message_path ) {
57
		$messages = $this->get_raw_messages();
58
59
		$formatted_messages = array();
60
61
		foreach ( $messages as $message ) {
62
			if ( ! preg_match( $message['message_path'], $message_path ) ) {
63
				continue;
64
			}
65
66
			$obj                 = new \stdClass();
67
			$obj->CTA            = array( // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
68
				'message'   => $message['button_caption'],
69
				'newWindow' => false,
70
			);
71
			$obj->url            = $message['button_link'];
72
			$obj->id             = $message['id'];
73
			$obj->is_dismissible = true;
74
			$obj->content        = array(
75
				'message'     => $message['message'],
76
				'description' => $message['description'],
77
				'list'        => array(),
78
				'icon'        => 'jetpack',
79
			);
80
81
			$formatted_messages[] = $obj;
82
		}
83
84
		return $formatted_messages;
85
	}
86
87
	/**
88
	 * Retrieve the current message to display keyed on query string and message path
89
	 *
90
	 * @param string $message_path The message path to ask for.
91
	 * @param string $query The query string originally from the front end. Unused in this subclass.
92
	 * @param bool   $full_jp_logo_exists If there is a full Jetpack logo already on the page.
93
	 *
94
	 * @return array The JITMs to show, or an empty array if there is nothing to show
95
	 */
96
	public function get_messages( $message_path, $query, $full_jp_logo_exists ) {
97
		/** This filter is documented in  class.jetpack-connection-banner.php */
98
		if ( ! apply_filters( 'jetpack_pre_connection_prompt_helpers', false ) ) {
99
			// If filter jetpack_pre_connection_prompt_helpers is not set, return an empty array.
100
			return array();
101
		}
102
103
		$messages = $this->filter_messages( $message_path );
104
105
		if ( empty( $messages ) ) {
106
			return array();
107
		}
108
109
		$hidden_jitms = \Jetpack_Options::get_option( 'hide_jitm' );
110
111
		foreach ( $messages as $idx => &$envelope ) {
112
			$dismissed_feature = isset( $hidden_jitms[ 'pre-connection-' . $envelope->id ] ) &&
113
				is_array( $hidden_jitms[ 'pre-connection-' . $envelope->id ] ) ? $hidden_jitms[ 'pre-connection-' . $envelope->id ] : null;
114
115
			if ( is_array( $dismissed_feature ) ) {
116
				unset( $messages[ $idx ] );
117
				continue;
118
			}
119
120
			$envelope->content['icon'] = $this->generate_icon( $envelope->content['icon'], $full_jp_logo_exists );
121
		}
122
123
		return $messages;
124
	}
125
126
	/**
127
	 * Dismisses a JITM ID so that it will no longer be shown.
128
	 *
129
	 * @param string $id The id of the JITM that was dismissed.
130
	 *
131
	 * @return bool Always true
132
	 */
133
	public function dismiss( $id ) {
134
		$this->save_dismiss( 'pre-connection-' . $id );
135
		return true;
136
	}
137
}
138