Completed
Push — fix/pre-connection-jitm-connec... ( 12f616 )
by
unknown
53:25 queued 45:00
created

Pre_Connection_JITM::build_setup_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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