PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | jetpack_vendor/automattic/jetpack-jitm/src/class-jitm.php +542 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,542 @@
1 +<?php
2 +/**
3 + * Jetpack's JITM class.
4 + *
5 + * @package automattic/jetpack-jitm
6 + */
7 +
8 +namespace Automattic\Jetpack\JITMS;
9 +
10 +use Automattic\Jetpack\Assets;
11 +use Automattic\Jetpack\Assets\Logo as Jetpack_Logo;
12 +use Automattic\Jetpack\Connection\Manager as Connection_Manager;
13 +use Automattic\Jetpack\Status;
14 +use Automattic\Jetpack\Status\Host;
15 +
16 +if ( ! defined( 'ABSPATH' ) ) {
17 + exit( 0 );
18 +}
19 +
20 +/**
21 + * Jetpack just in time messaging through out the admin
22 + *
23 + * @since 1.1.0
24 + * @since-jetpack 5.6.0
25 + */
26 +class JITM {
27 +
28 + const PACKAGE_VERSION = '5.1.1';
29 +
30 + /**
31 + * List of screen IDs where JITMs are allowed to display.
32 + *
33 + * @var string[]
34 + */
35 + const APPROVED_SCREEN_IDS = array(
36 + 'jetpack',
37 + 'woo',
38 + 'shop',
39 + 'product',
40 + );
41 +
42 + /**
43 + * The lazily-resolved JITM instance used by the hooks registered in configure().
44 + *
45 + * @var Post_Connection_JITM|Pre_Connection_JITM|null
46 + */
47 + private static $configured_instance;
48 +
49 + /**
50 + * The configuration method that is called from the jetpack-config package.
51 + *
52 + * Registers the JITM hooks directly (rather than building a JITM instance up
53 + * front) and binds the instance-dependent callbacks to a lazily-resolved
54 + * instance, so the connection-specific JITM subclass and its dependencies are
55 + * only loaded when one of these hooks actually fires instead of on every
56 + * request. JITMs only render on admin screens and via the REST API.
57 + */
58 + public static function configure() {
59 + if ( did_action( 'jetpack_registered_jitms' ) ) {
60 + // JITMs have already been registered.
61 + return;
62 + }
63 +
64 + if ( ! self::are_jitms_enabled() ) {
65 + // Do nothing.
66 + return;
67 + }
68 +
69 + add_action( 'rest_api_init', array( __NAMESPACE__ . '\\Rest_Api_Endpoints', 'register_endpoints' ) );
70 +
71 + add_action(
72 + 'current_screen',
73 + static function ( $screen ) {
74 + self::get_configured_instance()->prepare_jitms( $screen );
75 + }
76 + );
77 +
78 + /**
79 + * These are sync actions that we need to keep track of for jitms.
80 + */
81 + add_filter(
82 + 'jetpack_sync_before_send_updated_option',
83 + static function ( $params ) {
84 + return self::get_configured_instance()->jetpack_track_last_sync_callback( $params );
85 + },
86 + 99
87 + );
88 +
89 + /** This action is documented in projects/packages/jitm/src/class-jitm.php */
90 + do_action( 'jetpack_registered_jitms' );
91 + }
92 +
93 + /**
94 + * Pre/Post Connection JITM factory metod
95 + *
96 + * @return Post_Connection_JITM|Pre_Connection_JITM JITM instance.
97 + */
98 + public static function get_instance() {
99 + if ( self::is_connected() ) {
100 + $jitm = new Post_Connection_JITM();
101 + } else {
102 + $jitm = new Pre_Connection_JITM();
103 + }
104 + return $jitm;
105 + }
106 +
107 + /**
108 + * Lazily resolve and cache the connection-appropriate JITM instance used by
109 + * the hooks registered in configure().
110 + *
111 + * @return Post_Connection_JITM|Pre_Connection_JITM JITM instance.
112 + */
113 + private static function get_configured_instance() {
114 + if ( null === self::$configured_instance ) {
115 + self::$configured_instance = self::get_instance();
116 + }
117 + return self::$configured_instance;
118 + }
119 +
120 + /**
121 + * Clears admin notices from the current screen, keeping JITMs.
122 + *
123 + * A screen that opted out through `jetpack_display_jitms_on_screen` has nothing
124 + * to keep, so it stays silent.
125 + *
126 + * @since 5.1.0
127 + *
128 + * @return void
129 + */
130 + public static function suppress_foreign_admin_notices() {
131 + foreach ( array( 'admin_notices', 'all_admin_notices' ) as $hook ) {
132 + $ours = self::collect_own_callbacks( $hook );
133 +
134 + remove_all_actions( $hook );
135 +
136 + foreach ( $ours as $priority => $callbacks ) {
137 + foreach ( $callbacks as $callback ) {
138 + add_action( $hook, $callback['function'], $priority, $callback['accepted_args'] );
139 + }
140 + }
141 + }
142 + }
143 +
144 + /**
145 + * Returns our callbacks on $hook, keyed by priority.
146 + *
147 + * Matched by class: `prepare_jitms()` hooks the cached instance and
148 + * `get_instance()` builds a fresh one, so comparing objects would miss.
149 + *
150 + * @since 5.1.0
151 + *
152 + * @param string $hook Hook name.
153 + * @return array<int, array<int, array{function: callable, accepted_args: int}>>
154 + */
155 + private static function collect_own_callbacks( $hook ) {
156 + global $wp_filter;
157 +
158 + $kept = array();
159 +
160 + if ( ! isset( $wp_filter[ $hook ] ) || ! is_object( $wp_filter[ $hook ] ) ) {
161 + return $kept;
162 + }
163 +
164 + foreach ( $wp_filter[ $hook ]->callbacks as $priority => $callbacks ) {
165 + foreach ( $callbacks as $callback ) {
166 + if (
167 + is_array( $callback['function'] )
168 + && isset( $callback['function'][0] )
169 + && $callback['function'][0] instanceof self
170 + ) {
171 + $kept[ $priority ][] = $callback;
172 + }
173 + }
174 + }
175 +
176 + return $kept;
177 + }
178 +
179 + /**
180 + * Sets up JITM action callbacks if needed.
181 + */
182 + public function register() {
183 + if ( did_action( 'jetpack_registered_jitms' ) ) {
184 + // JITMs have already been registered.
185 + return;
186 + }
187 +
188 + if ( ! $this->jitms_enabled() ) {
189 + // Do nothing.
190 + return;
191 + }
192 +
193 + add_action( 'rest_api_init', array( __NAMESPACE__ . '\\Rest_Api_Endpoints', 'register_endpoints' ) );
194 +
195 + add_action( 'current_screen', array( $this, 'prepare_jitms' ) );
196 +
197 + /**
198 + * These are sync actions that we need to keep track of for jitms.
199 + */
200 + add_filter( 'jetpack_sync_before_send_updated_option', array( $this, 'jetpack_track_last_sync_callback' ), 99 );
201 +
202 + /**
203 + * Fires when the JITMs are registered. This action is used to ensure that
204 + * JITMs are registered only once.
205 + *
206 + * @since 1.16.0
207 + */
208 + do_action( 'jetpack_registered_jitms' );
209 + }
210 +
211 + /**
212 + * Checks the jetpack_just_in_time_msgs filters and whether the site
213 + * is offline to determine whether JITMs are enabled.
214 + *
215 + * @return bool True if JITMs are enabled, else false.
216 + */
217 + public function jitms_enabled() {
218 + return self::are_jitms_enabled();
219 + }
220 +
221 + /**
222 + * Static variant of jitms_enabled() so the enabled state can be checked
223 + * without building a JITM instance (used by configure()).
224 + *
225 + * @return bool True if JITMs are enabled, else false.
226 + */
227 + private static function are_jitms_enabled() {
228 + /**
229 + * Filter to turn off all just in time messages
230 + *
231 + * @since 1.1.0
232 + * @since-jetpack 3.7.0
233 + * @since-jetpack 5.4.0 Correct docblock to reflect default arg value
234 + *
235 + * @param bool true Whether to show just in time messages.
236 + */
237 + if ( ! apply_filters( 'jetpack_just_in_time_msgs', true ) ) {
238 + return false;
239 + }
240 +
241 + // Folks cannot connect to WordPress.com and won't really be able to act on the pre-connection messages. So bail.
242 + if ( ( new Status() )->is_offline_mode() ) {
243 + return false;
244 + }
245 +
246 + return true;
247 + }
248 +
249 + /**
250 + * Prepare actions according to screen and post type.
251 + *
252 + * @since 1.1.0
253 + * @since-jetpack 3.8.2
254 + *
255 + * @uses Jetpack_Autoupdate::get_possible_failures()
256 + *
257 + * @param \WP_Screen $screen WP Core's screen object.
258 + */
259 + public function prepare_jitms( $screen ) {
260 + /**
261 + * Filter to hide JITMs on certain screens.
262 + *
263 + * @since 1.14.0
264 + *
265 + * @param bool true Whether to show just in time messages.
266 + * @param string $string->id The ID of the current screen.
267 + */
268 + if ( apply_filters( 'jetpack_display_jitms_on_screen', true, $screen->id ) ) {
269 + add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
270 + add_action( 'admin_notices', array( $this, 'ajax_message' ) );
271 + add_action( 'edit_form_top', array( $this, 'ajax_message' ) );
272 + }
273 + }
274 +
275 + /**
276 + * Check if the current page is a Jetpack or WooCommerce admin page.
277 + * Noting that this is a very basic check, and pages from other plugins may also match.
278 + *
279 + * @since 3.1.0
280 + *
281 + * @return bool True if the current page is a Jetpack or WooCommerce admin page, else false.
282 + */
283 + public function is_a8c_admin_page() {
284 + if ( ! function_exists( 'get_current_screen' ) ) {
285 + return false;
286 + }
287 +
288 + $current_screen = get_current_screen();
289 +
290 + // We never want to show JITMs on the block editor.
291 + if (
292 + method_exists( $current_screen, 'is_block_editor' )
293 + && $current_screen->is_block_editor()
294 + ) {
295 + return false;
296 + }
297 +
298 + return (
299 + $current_screen
300 + && $current_screen->id
301 + && (bool) preg_match(
302 + '/' . implode( '|', self::APPROVED_SCREEN_IDS ) . '/',
303 + $current_screen->id
304 + )
305 + );
306 + }
307 +
308 + /**
309 + * Function to enqueue jitm css and js
310 + */
311 + public function jitm_enqueue_files() {
312 + // Only load those files on the Jetpack or Woo admin pages.
313 + if ( ! $this->is_a8c_admin_page() ) {
314 + return;
315 + }
316 +
317 + Assets::register_script(
318 + 'jetpack-jitm',
319 + '../build/index.js',
320 + __FILE__,
321 + array(
322 + 'in_footer' => true,
323 + 'dependencies' => array(),
324 + 'enqueue' => true,
325 + )
326 + );
327 + wp_localize_script(
328 + 'jetpack-jitm',
329 + 'jitm_config',
330 + array(
331 + 'api_root' => esc_url_raw( rest_url() ),
332 + 'activate_module_text' => esc_html__( 'Activate', 'jetpack-jitm' ),
333 + 'activated_module_text' => esc_html__( 'Activated', 'jetpack-jitm' ),
334 + 'activating_module_text' => esc_html__( 'Activating', 'jetpack-jitm' ),
335 + 'settings_module_text' => esc_html__( 'Settings', 'jetpack-jitm' ),
336 + 'nonce' => wp_create_nonce( 'wp_rest' ),
337 + )
338 + );
339 + }
340 +
341 + /**
342 + * Is the current page a block editor page?
343 + *
344 + * @since 1.1.0
345 + * @since-jetpack 8.0.0
346 + *
347 + * @deprecated 3.1.0
348 + */
349 + public function is_gutenberg_page() {
350 + _deprecated_function( __METHOD__, '3.1.0' );
351 + $current_screen = get_current_screen();
352 + return ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() );
353 + }
354 +
355 + /**
356 + * Get's the current message path for display of a JITM
357 + *
358 + * @return string The message path
359 + */
360 + public function get_message_path() {
361 + $screen = get_current_screen();
362 +
363 + return 'wp:' . $screen->id . ':' . current_filter();
364 + }
365 +
366 + /**
367 + * Injects the dom to show a JITM inside of wp-admin.
368 + */
369 + public function ajax_message() {
370 + if ( ! is_admin() ) {
371 + return;
372 + }
373 +
374 + // Only add this to specifically allowed pages.
375 + if ( ! $this->is_a8c_admin_page() ) {
376 + return;
377 + }
378 +
379 + $message_path = $this->get_message_path();
380 + $query_string = _http_build_query( $_GET, '', ',' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
381 + $current_screen = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- Escaped below
382 + ?>
383 + <div class="jetpack-jitm-message"
384 + data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_rest' ) ); ?>"
385 + data-ajax-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_ajax_action' ) ); ?>"
386 + data-message-path="<?php echo esc_attr( $message_path ); ?>"
387 + data-query="<?php echo urlencode_deep( $query_string ); ?>"
388 + data-redirect="<?php echo urlencode_deep( $current_screen ); ?>"
389 + ></div>
390 + <?php
391 + }
392 +
393 + /**
394 + * Generate the icon to display on the JITM.
395 + *
396 + * All icons supported in this method should be included in the array returned by
397 + * JITM::get_supported_icons.
398 + *
399 + * @param string $content_icon Icon type name.
400 + * @param bool $full_jp_logo_exists Is there a big JP logo already displayed on this screen.
401 + */
402 + public function generate_icon( $content_icon, $full_jp_logo_exists ) {
403 + $date_now = new \DateTime( 'now', wp_timezone() );
404 + $feb_4_date = new \DateTime( '02-04-2025', wp_timezone() );
405 + // Don't show the new Woo svg logo until after Feb 4th, 2025
406 + $show_new_logo = $date_now >= $feb_4_date;
407 +
408 + switch ( $content_icon ) {
409 + case 'jetpack':
410 + $jetpack_logo = new Jetpack_Logo();
411 + $content_icon = '<div class="jp-emblem">' . ( ( $full_jp_logo_exists ) ? $jetpack_logo->get_jp_emblem() : $jetpack_logo->get_jp_emblem_larger() ) . '</div>';
412 + break;
413 + case 'woocommerce':
414 + // After Feb 4th 2025, we can remove this date condition check ( & filter) and the old svg logo.
415 + /**
416 + * Filter to force display the new Woo logo in Woo JITM's, or not.
417 + *
418 + * @since 4.0.5
419 + *
420 + * @param bool $show_woo_logo Whether to show the new Woo logo or not.
421 + */
422 + $content_icon = apply_filters( 'jetpack_jitm_use_new_woo_logo', $show_new_logo )
423 + // New Woo logo
424 + ? '<div class="jp-emblem"><svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
425 + viewBox="0 0 183.6 47.5" style="enable-background:new 0 0 183.6 47.5;" xml:space="preserve">
426 + <style type="text/css">
427 + .st0{fill-rule:evenodd;clip-rule:evenodd;fill:#873EFF;}
428 + .st1{fill-rule:evenodd;clip-rule:evenodd;}
429 + .st2{fill:#873EFF;}
430 + .st3{fill-rule:evenodd;clip-rule:evenodd;fill:#FFFFFF;}
431 + .st4{fill:#FFFFFF;}
432 + </style>
433 + <g>
434 + <path class="st0" d="M77.4,0c-4.3,0-7.1,1.4-9.6,6.1L56.4,27.6V8.5c0-5.7-2.7-8.5-7.7-8.5s-7.1,1.7-9.6,6.5L28.3,27.6V8.7
435 + c0-6.1-2.5-8.7-8.6-8.7H7.3C2.6,0,0,2.2,0,6.2s2.5,6.4,7.1,6.4h5.1v24.1c0,6.8,4.6,10.8,11.2,10.8s9.6-2.6,12.9-8.7l7.2-13.5v11.4
436 + c0,6.7,4.4,10.8,11.1,10.8s9.2-2.3,13-8.7l16.6-28C87.8,4.7,85.3,0,77.3,0C77.3,0,77.3,0,77.4,0z"/>
437 + <path class="st0" d="M108.6,0C95,0,84.7,10.1,84.7,23.8s10.4,23.7,23.9,23.7s23.8-10.1,23.9-23.7C132.5,10.1,122.1,0,108.6,0z
438 + M108.6,32.9c-5.1,0-8.6-3.8-8.6-9.1s3.5-9.2,8.6-9.2s8.6,3.9,8.6,9.2S113.8,32.9,108.6,32.9z"/>
439 + <path class="st0" d="M159.7,0c-13.5,0-23.9,10.1-23.9,23.8s10.4,23.7,23.9,23.7s23.9-10.1,23.9-23.7S173.2,0,159.7,0z M159.7,32.9
440 + c-5.2,0-8.5-3.8-8.5-9.1s3.4-9.2,8.5-9.2s8.6,3.9,8.6,9.2S164.9,32.9,159.7,32.9z"/>
441 + </g>
442 + </svg></div>'
443 + // Old Woo logo
444 + : '<div class="jp-emblem"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" viewBox="0 0 168 100" xml:space="preserve" enable-background="new 0 0 168 100" width="50" height="30"><style type="text/css">
445 + .st0{clip-path:url(#SVGID_2_);enable-background:new ;}
446 + .st1{clip-path:url(#SVGID_4_);}
447 + .st2{clip-path:url(#SVGID_6_);}
448 + .st3{clip-path:url(#SVGID_8_);fill:#7F54B3;}
449 + .st4{clip-path:url(#SVGID_10_);fill:#FFFFFE;}
450 + .st5{clip-path:url(#SVGID_12_);fill:#FFFFFE;}
451 + .st6{clip-path:url(#SVGID_14_);fill:#FFFFFE;}
452 + </style><g><defs><polygon id="SVGID_1_" points="83.8 100 0 100 0 0.3 83.8 0.3 167.6 0.3 167.6 100 "/></defs><clipPath id="SVGID_2_"><use xlink:href="#SVGID_1_" overflow="visible"/></clipPath><g class="st0"><g><defs><rect id="SVGID_3_" width="168" height="100"/></defs><clipPath id="SVGID_4_"><use xlink:href="#SVGID_3_" overflow="visible"/></clipPath><g class="st1"><defs><path id="SVGID_5_" d="M15.6 0.3H152c8.6 0 15.6 7 15.6 15.6v52c0 8.6-7 15.6-15.6 15.6h-48.9l6.7 16.4L80.2 83.6H15.6C7 83.6 0 76.6 0 67.9v-52C0 7.3 7 0.3 15.6 0.3"/></defs><clipPath id="SVGID_6_"><use xlink:href="#SVGID_5_" overflow="visible"/></clipPath><g class="st2"><defs><rect id="SVGID_7_" width="168" height="100"/></defs><clipPath id="SVGID_8_"><use xlink:href="#SVGID_7_" overflow="visible"/></clipPath><rect x="-10" y="-9.7" class="st3" width="187.6" height="119.7"/></g></g></g></g></g><g><defs><path id="SVGID_9_" d="M8.4 14.5c1-1.3 2.4-2 4.3-2.1 3.5-0.2 5.5 1.4 6 4.9 2.1 14.3 4.4 26.4 6.9 36.4l15-28.6c1.4-2.6 3.1-3.9 5.2-4.1 3-0.2 4.9 1.7 5.6 5.7 1.7 9.1 3.9 16.9 6.5 23.4 1.8-17.4 4.8-30 9-37.7 1-1.9 2.5-2.9 4.5-3 1.6-0.1 3 0.3 4.3 1.4 1.3 1 2 2.3 2.1 3.9 0.1 1.2-0.1 2.3-0.7 3.3 -2.7 5-4.9 13.2-6.6 24.7 -1.7 11.1-2.3 19.8-1.9 26.1 0.1 1.7-0.1 3.2-0.8 4.5 -0.8 1.5-2 2.4-3.7 2.5 -1.8 0.1-3.6-0.7-5.4-2.5C52.4 66.7 47.4 57 43.7 44.1c-4.4 8.8-7.7 15.3-9.9 19.7 -4 7.7-7.5 11.7-10.3 11.9 -1.9 0.1-3.5-1.4-4.8-4.7 -3.5-9-7.3-26.3-11.3-52C7.1 17.3 7.5 15.8 8.4 14.5"/></defs><clipPath id="SVGID_10_"><use xlink:href="#SVGID_9_" overflow="visible"/></clipPath><rect x="-2.7" y="-0.6" class="st4" width="90.6" height="86.4"/></g><g><defs><path id="SVGID_11_" d="M155.6 25.2c-2.5-4.3-6.1-6.9-11-7.9 -1.3-0.3-2.5-0.4-3.7-0.4 -6.6 0-11.9 3.4-16.1 10.2 -3.6 5.8-5.3 12.3-5.3 19.3 0 5.3 1.1 9.8 3.3 13.6 2.5 4.3 6.1 6.9 11 7.9 1.3 0.3 2.5 0.4 3.7 0.4 6.6 0 12-3.4 16.1-10.2 3.6-5.9 5.3-12.4 5.3-19.4C159 33.4 157.9 28.9 155.6 25.2zM147 44.2c-0.9 4.5-2.7 7.9-5.2 10.1 -2 1.8-3.9 2.5-5.5 2.2 -1.7-0.3-3-1.8-4-4.4 -0.8-2.1-1.2-4.2-1.2-6.2 0-1.7 0.2-3.4 0.5-5 0.6-2.8 1.8-5.5 3.6-8.1 2.3-3.3 4.7-4.8 7.1-4.2 1.7 0.3 3 1.8 4 4.4 0.8 2.1 1.2 4.2 1.2 6.2C147.5 40.9 147.3 42.6 147 44.2z"/></defs><clipPath id="SVGID_12_"><use xlink:href="#SVGID_11_" overflow="visible"/></clipPath><rect x="109.6" y="6.9" class="st5" width="59.4" height="71.4"/></g><g><defs><path id="SVGID_13_" d="M112.7 25.2c-2.5-4.3-6.1-6.9-11-7.9 -1.3-0.3-2.5-0.4-3.7-0.4 -6.6 0-11.9 3.4-16.1 10.2 -3.5 5.8-5.3 12.3-5.3 19.3 0 5.3 1.1 9.8 3.3 13.6 2.5 4.3 6.1 6.9 11 7.9 1.3 0.3 2.5 0.4 3.7 0.4 6.6 0 12-3.4 16.1-10.2 3.5-5.9 5.3-12.4 5.3-19.4C116 33.4 114.9 28.9 112.7 25.2zM104.1 44.2c-0.9 4.5-2.7 7.9-5.2 10.1 -2 1.8-3.9 2.5-5.5 2.2 -1.7-0.3-3-1.8-4-4.4 -0.8-2.1-1.2-4.2-1.2-6.2 0-1.7 0.2-3.4 0.5-5 0.6-2.8 1.8-5.5 3.6-8.1 2.3-3.3 4.7-4.8 7.1-4.2 1.7 0.3 3 1.8 4 4.4 0.8 2.1 1.2 4.2 1.2 6.2C104.6 40.9 104.4 42.6 104.1 44.2z"/></defs><clipPath id="SVGID_14_"><use xlink:href="#SVGID_13_" overflow="visible"/></clipPath><rect x="66.7" y="6.9" class="st6" width="59.4" height="71.4"/></g></svg></div>';
453 + break;
454 + default:
455 + $content_icon = '';
456 + break;
457 + }
458 + return $content_icon;
459 + }
460 +
461 + /**
462 + * Returns an array containing the supported icons for JITMs.
463 + *
464 + * The list includes an empty string, which is used when no icon should be displayed.
465 + *
466 + * @return array The list of supported icons.
467 + */
468 + public function get_supported_icons() {
469 + return array(
470 + 'jetpack',
471 + 'woocommerce',
472 + '',
473 + );
474 + }
475 +
476 + /**
477 + * Stores dismiss data into an option
478 + *
479 + * @param string $key Dismiss key.
480 + */
481 + public function save_dismiss( $key ) {
482 + $hide_jitm = \Jetpack_Options::get_option( 'hide_jitm' );
483 + if ( ! is_array( $hide_jitm ) ) {
484 + $hide_jitm = array();
485 + }
486 +
487 + if ( ! isset( $hide_jitm[ $key ] ) || ! is_array( $hide_jitm[ $key ] ) ) {
488 + $hide_jitm[ $key ] = array(
489 + 'last_dismissal' => 0,
490 + 'number' => 0,
491 + );
492 + }
493 +
494 + $hide_jitm[ $key ] = array(
495 + 'last_dismissal' => time(),
496 + 'number' => $hide_jitm[ $key ]['number'] + 1,
497 + );
498 +
499 + \Jetpack_Options::update_option( 'hide_jitm', $hide_jitm );
500 + }
501 +
502 + /**
503 + * Sets the 'jetpack_last_plugin_sync' transient when the active_plugins option is synced.
504 + *
505 + * @param array $params The action parameters.
506 + *
507 + * @return array Returns the action parameters unchanged.
508 + */
509 + public function jetpack_track_last_sync_callback( $params ) {
510 + /**
511 + * This filter is documented in the Automattic\Jetpack\JITMS\Post_Connection_JITM class.
512 + */
513 + if ( ! apply_filters( 'jetpack_just_in_time_msg_cache', true ) ) {
514 + return $params;
515 + }
516 +
517 + if ( is_array( $params ) && isset( $params[0] ) ) {
518 + $option = $params[0];
519 + if ( 'active_plugins' === $option ) {
520 + // Use the cache if we can, but not terribly important if it gets evicted.
521 + set_transient( 'jetpack_last_plugin_sync', time(), HOUR_IN_SECONDS );
522 + }
523 + }
524 +
525 + return $params;
526 + }
527 +
528 + /**
529 + * Check if the current site is connected.
530 + * On WordPress.com Simple, it is always connected.
531 + *
532 + * @return bool true if the site is connected, false otherwise.
533 + */
534 + private static function is_connected() {
535 + if ( ( new Host() )->is_wpcom_simple() ) {
536 + return true;
537 + }
538 +
539 + $connection = new Connection_Manager();
540 + return $connection->is_connected();
541 + }
542 +}