Completed
Push — e2e/e2e-runner-groups ( 261a6b...698261 )
by
unknown
65:29 queued 54:32
created

Jetpack_Pre_Connection_JITMs   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 118
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_raw_messages() 0 51 3
A generate_description_with_tos() 0 17 1
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
use Automattic\Jetpack\Redirect;
9
10
/**
11
 * Jetpack's Pre-Connection JITMs. These can be displayed with the JITM package.
12
 */
13
class Jetpack_Pre_Connection_JITMs {
14
15
	/**
16
	 * Returns all the pre-connection messages.
17
	 *
18
	 * @return array An array containing the pre-connection JITM messages.
19
	 */
20
	private function get_raw_messages() {
21
		$button_caption = __( 'Set up Jetpack', 'jetpack' );
22
		/* Translators: placeholders are links. */
23
		$media_description = __( 'Click on the <strong>Set up Jetpack</strong> button to agree to our <a href="%1$s" target="_blank" rel="noopener noreferrer">Terms of Service</a> and to <a href="%2$s" target="_blank" rel="noopener noreferrer">share details</a> with WordPress.com, and gain access to Site Accelerator.', 'jetpack' );
24
		/* Translators: placeholders are links. */
25
		$widgets_description = __( 'Click on the <strong>Set up Jetpack</strong> button to agree to our <a href="%1$s" target="_blank" rel="noopener noreferrer">Terms of Service</a> and to <a href="%2$s" target="_blank" rel="noopener noreferrer">share details</a> with WordPress.com, and gain access to great additional widgets.', 'jetpack' );
26
		/* Translators: placeholders are links. */
27
		$posts_description = __( 'Click on the <strong>Set up Jetpack</strong> button to agree to our <a href="%1$s" target="_blank" rel="noopener noreferrer">Terms of Service</a> and to <a href="%2$s" target="_blank" rel="noopener noreferrer">share details</a> with WordPress.com, and gain access to in-depth stats about your site.', 'jetpack' );
28
29
		$messages = array(
30
			array(
31
				'id'             => 'jpsetup-upload',
32
				'message_path'   => '/wp:upload:admin_notices/',
33
				'message'        => __( 'Do you want lightning-fast images?', 'jetpack' ),
34
				'description'    => $this->generate_description_with_tos( $media_description ),
35
				'button_caption' => $button_caption,
36
			),
37
			array(
38
				'id'             => 'jpsetup-widgets',
39
				'message_path'   => '/wp:widgets:admin_notices/',
40
				'message'        => __( 'Looking for even more widgets?', 'jetpack' ),
41
				'description'    => $this->generate_description_with_tos( $widgets_description ),
42
				'button_caption' => $button_caption,
43
			),
44
		);
45
46
		if ( wp_count_posts()->publish >= 5 ) {
47
			$messages[] = array(
48
				'id'             => 'jpsetup-posts',
49
				'message_path'   => '/wp:edit-post:admin_notices/',
50
				'message'        => __( 'Do you know which of these posts gets the most traffic?', 'jetpack' ),
51
				'description'    => $this->generate_description_with_tos( $posts_description ),
52
				'button_caption' => $button_caption,
53
			);
54
		}
55
56
		foreach ( $messages as $key => $message ) {
57
			/*
58
			 * Add Connect URL to each message, with from including jitm id.
59
			 */
60
			$jetpack_setup_url               = $this->generate_admin_url(
61
				array(
62
					'page' => 'jetpack#/setup',
63
					'from' => sprintf( 'pre-connection-jitm-%s', $message['id'] ),
64
				)
65
			);
66
			$messages[ $key ]['button_link'] = $jetpack_setup_url;
67
		}
68
69
		return $messages;
70
	}
71
72
	/**
73
	 * Generate a description text with links to ToS documents.
74
	 *
75
	 * Those messages must mention the ToS agreement message,
76
	 * but do not use the standard message defined in jetpack_render_tos_blurb.
77
	 * Instead, they use their own custom messages.
78
	 *
79
	 * @param string $description Description string with placeholders.
80
	 *
81
	 * @return string
82
	 */
83
	private function generate_description_with_tos( $description ) {
84
		return sprintf(
85
			wp_kses(
86
				$description,
87
				array(
88
					'a'      => array(
89
						'href'   => array(),
90
						'target' => array(),
91
						'rel'    => array(),
92
					),
93
					'strong' => true,
94
				)
95
			),
96
			esc_url( Redirect::get_url( 'wpcom-tos' ) ),
97
			esc_url( Redirect::get_url( 'jetpack-support-what-data-does-jetpack-sync' ) )
98
		);
99
	}
100
101
	/**
102
	 * Adds the input query arguments to the admin url.
103
	 *
104
	 * @param array $args The query arguments.
105
	 *
106
	 * @return string The admin url.
107
	 */
108
	private function generate_admin_url( $args ) {
109
		$url = add_query_arg( $args, admin_url( 'admin.php' ) );
110
		return $url;
111
	}
112
113
	/**
114
	 * Add the Jetpack pre-connection JITMs to the list of pre-connection JITM messages.
115
	 *
116
	 * @param array $pre_connection_messages An array of pre-connection JITMs.
117
	 *
118
	 * @return array The array of pre-connection JITMs.
119
	 */
120
	public function add_pre_connection_jitms( $pre_connection_messages ) {
121
		$jetpack_messages = $this->get_raw_messages();
122
123
		if ( ! is_array( $pre_connection_messages ) ) {
124
			// The incoming messages aren't an array, so just return Jetpack's messages.
125
			return $jetpack_messages;
126
		}
127
128
		return array_merge( $pre_connection_messages, $jetpack_messages );
129
	}
130
}
131