Completed
Push — update/pre-connection-jitms ( 33b944...6a137c )
by Jeremy
28:48 queued 17:18
created

Jetpack_Pre_Connection_JITMs   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_raw_messages() 0 52 3
A generate_admin_url() 0 4 1
A add_pre_connection_jitms() 0 10 2
1
<?php
2
/**
3
 * Jetpack's Pre-Connection JITMs class.
4
 *
5
 * @package jetpack
6
 */
7
8
/**
9
 * Jetpack's Pre-Connection JITMs. These can be displayed with the JITM package.
10
 */
11
class Jetpack_Pre_Connection_JITMs {
12
13
	/**
14
	 * Returns all the pre-connection messages.
15
	 *
16
	 * @return array An array containing the pre-connection JITM messages.
17
	 */
18
	private function get_raw_messages() {
19
		$messages = array(
20
			array(
21
				'id'             => 'jpsetup-upload',
22
				'message_path'   => '/wp:upload:admin_notices/',
23
				'message'        => __( 'Do you want lightning-fast images?', 'jetpack' ),
24
				'description'    => __( 'Set up Jetpack, enable Site Accelerator, and start serving your images lightning fast, for free.', 'jetpack' ),
25
				'button_caption' => __( 'Set up Jetpack', 'jetpack' ),
26
			),
27
			array(
28
				'id'             => 'jpsetup-widgets',
29
				'message_path'   => '/wp:widgets:admin_notices/',
30
				'message'        => __( 'Looking for even more widgets?', 'jetpack' ),
31
				'description'    => __( 'Set up Jetpack for great additional widgets that display business contact info and maps, blog stats, and top posts.', 'jetpack' ),
32
				'button_caption' => __( 'Set up Jetpack', 'jetpack' ),
33
			),
34
		);
35
36
		if ( wp_count_posts()->publish >= 5 ) {
37
			$messages[] = array(
38
				'id'             => 'jpsetup-posts',
39
				'message_path'   => '/wp:edit-post:admin_notices/',
40
				'message'        => __( 'Do you know which of these posts gets the most traffic?', 'jetpack' ),
41
				'description'    => __( 'Set up Jetpack to get in-depth stats about your content and visitors.', 'jetpack' ),
42
				'button_caption' => __( 'Set up Jetpack', 'jetpack' ),
43
			);
44
		}
45
46
		foreach ( $messages as $key => $message ) {
47
			/*
48
			 * Add Connect URL to each message, with from including jitm id.
49
			 */
50
			$jetpack_setup_url               = $this->generate_admin_url(
51
				array(
52
					'page'    => 'jetpack',
53
					'#/setup' => '',
54
					'from'    => sprintf( 'pre-connection-jitm-%s', $message['id'] ),
55
				)
56
			);
57
			$messages[ $key ]['button_link'] = $jetpack_setup_url;
58
59
			/*
60
			 * Add ToS acceptance message to JITM description
61
			 */
62
			$messages[ $key ]['description'] .= sprintf(
63
				'<br /><br />%s',
64
				\jetpack_render_tos_blurb( false )
65
			);
66
		}
67
68
		return $messages;
69
	}
70
71
	/**
72
	 * Adds the input query arguments to the admin url.
73
	 *
74
	 * @param array $args The query arguments.
75
	 *
76
	 * @return string The admin url.
77
	 */
78
	private function generate_admin_url( $args ) {
79
		$url = add_query_arg( $args, admin_url( 'admin.php' ) );
80
		return $url;
81
	}
82
83
	/**
84
	 * Add the Jetpack pre-connection JITMs to the list of pre-connection JITM messages.
85
	 *
86
	 * @param array $pre_connection_messages An array of pre-connection JITMs.
87
	 *
88
	 * @return array The array of pre-connection JITMs.
89
	 */
90
	public function add_pre_connection_jitms( $pre_connection_messages ) {
91
		$jetpack_messages = $this->get_raw_messages();
92
93
		if ( ! is_array( $pre_connection_messages ) ) {
94
			// The incoming messages aren't an array, so just return Jetpack's messages.
95
			return $jetpack_messages;
96
		}
97
98
		return array_merge( $pre_connection_messages, $jetpack_messages );
99
	}
100
}
101