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