Completed
Push — fix/improve-search-product-lay... ( e4fb73...5c8387 )
by
unknown
146:14 queued 136:16
created

Pre_Connection_JITM::get_raw_messages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

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