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