Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
45.73% covered (danger)
45.73%
75 / 164
16.67% covered (danger)
16.67%
3 / 18
CRAP
0.00% covered (danger)
0.00%
0 / 1
JITM
45.68% covered (danger)
45.68%
74 / 162
16.67% covered (danger)
16.67%
3 / 18
678.15
0.00% covered (danger)
0.00%
0 / 1
 configure
84.21% covered (warning)
84.21%
16 / 19
0.00% covered (danger)
0.00%
0 / 1
3.04
 get_instance
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 get_configured_instance
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 suppress_foreign_admin_notices
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 collect_own_callbacks
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
72
 register
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 jitms_enabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 are_jitms_enabled
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 prepare_jitms
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 is_a8c_admin_page
85.71% covered (warning)
85.71%
12 / 14
0.00% covered (danger)
0.00%
0 / 1
6.10
 jitm_enqueue_files
96.00% covered (success)
96.00%
24 / 25
0.00% covered (danger)
0.00%
0 / 1
3
 is_gutenberg_page
n/a
0 / 0
n/a
0 / 0
2
 get_message_path
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 ajax_message
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 generate_icon
42.11% covered (danger)
42.11%
8 / 19
0.00% covered (danger)
0.00%
0 / 1
12.99
 get_supported_icons
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 save_dismiss
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 jetpack_track_last_sync_callback
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 is_connected
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Jetpack's JITM class.
4 *
5 * @package automattic/jetpack-jitm
6 */
7
8namespace Automattic\Jetpack\JITMS;
9
10use Automattic\Jetpack\Assets;
11use Automattic\Jetpack\Assets\Logo as Jetpack_Logo;
12use Automattic\Jetpack\Connection\Manager as Connection_Manager;
13use Automattic\Jetpack\Status;
14use Automattic\Jetpack\Status\Host;
15
16if ( ! 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 */
26class 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                // Only WordPress.com and the Jetpack plugin serve wpcom/v3/jitm; elsewhere the script uses this package's own route.
338                'has_wpcom_endpoint'     => ( new Host() )->is_wpcom_simple() || class_exists( 'Jetpack' ),
339            )
340        );
341    }
342
343    /**
344     * Is the current page a block editor page?
345     *
346     * @since 1.1.0
347     * @since-jetpack 8.0.0
348     *
349     * @deprecated 3.1.0
350     */
351    public function is_gutenberg_page() {
352        _deprecated_function( __METHOD__, '3.1.0' );
353        $current_screen = get_current_screen();
354        return ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() );
355    }
356
357    /**
358     * Get's the current message path for display of a JITM
359     *
360     * @return string The message path
361     */
362    public function get_message_path() {
363        $screen = get_current_screen();
364
365        return 'wp:' . $screen->id . ':' . current_filter();
366    }
367
368    /**
369     * Injects the dom to show a JITM inside of wp-admin.
370     */
371    public function ajax_message() {
372        if ( ! is_admin() ) {
373            return;
374        }
375
376        // Only add this to specifically allowed pages.
377        if ( ! $this->is_a8c_admin_page() ) {
378            return;
379        }
380
381        $message_path   = $this->get_message_path();
382        $query_string   = _http_build_query( $_GET, '', ',' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
383        $current_screen = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- Escaped below
384        ?>
385        <div class="jetpack-jitm-message"
386            data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_rest' ) ); ?>"
387            data-ajax-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_ajax_action' ) ); ?>"
388            data-message-path="<?php echo esc_attr( $message_path ); ?>"
389            data-query="<?php echo urlencode_deep( $query_string ); ?>"
390            data-redirect="<?php echo urlencode_deep( $current_screen ); ?>"
391        ></div>
392        <?php
393    }
394
395    /**
396     * Generate the icon to display on the JITM.
397     *
398     * All icons supported in this method should be included in the array returned by
399     * JITM::get_supported_icons.
400     *
401     * @param string $content_icon Icon type name.
402     * @param bool   $full_jp_logo_exists Is there a big JP logo already displayed on this screen.
403     */
404    public function generate_icon( $content_icon, $full_jp_logo_exists ) {
405        $date_now   = new \DateTime( 'now', wp_timezone() );
406        $feb_4_date = new \DateTime( '02-04-2025', wp_timezone() );
407        // Don't show the new Woo svg logo until after Feb 4th, 2025
408        $show_new_logo = $date_now >= $feb_4_date;
409
410        switch ( $content_icon ) {
411            case 'jetpack':
412                $jetpack_logo = new Jetpack_Logo();
413                $content_icon = '<div class="jp-emblem">' . ( ( $full_jp_logo_exists ) ? $jetpack_logo->get_jp_emblem() : $jetpack_logo->get_jp_emblem_larger() ) . '</div>';
414                break;
415            case 'woocommerce':
416                // After Feb 4th 2025, we can remove this date condition check ( & filter) and the old svg logo.
417                /**
418                 * Filter to force display the new Woo logo in Woo JITM's, or not.
419                 *
420                 * @since 4.0.5
421                 *
422                 * @param bool $show_woo_logo Whether to show the new Woo logo or not.
423                 */
424                $content_icon = apply_filters( 'jetpack_jitm_use_new_woo_logo', $show_new_logo )
425                    // New Woo logo
426                    ? '<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"
427                            viewBox="0 0 183.6 47.5" style="enable-background:new 0 0 183.6 47.5;" xml:space="preserve">
428                        <style type="text/css">
429                            .st0{fill-rule:evenodd;clip-rule:evenodd;fill:#873EFF;}
430                            .st1{fill-rule:evenodd;clip-rule:evenodd;}
431                            .st2{fill:#873EFF;}
432                            .st3{fill-rule:evenodd;clip-rule:evenodd;fill:#FFFFFF;}
433                            .st4{fill:#FFFFFF;}
434                        </style>
435                        <g>
436                            <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
437                                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
438                                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"/>
439                            <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
440                                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"/>
441                            <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
442                                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"/>
443                        </g>
444                        </svg></div>'
445                    // Old Woo logo
446                    : '<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">
447                            .st0{clip-path:url(#SVGID_2_);enable-background:new    ;}
448                            .st1{clip-path:url(#SVGID_4_);}
449                            .st2{clip-path:url(#SVGID_6_);}
450                            .st3{clip-path:url(#SVGID_8_);fill:#7F54B3;}
451                            .st4{clip-path:url(#SVGID_10_);fill:#FFFFFE;}
452                            .st5{clip-path:url(#SVGID_12_);fill:#FFFFFE;}
453                            .st6{clip-path:url(#SVGID_14_);fill:#FFFFFE;}
454                        </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>';
455                break;
456            default:
457                $content_icon = '';
458                break;
459        }
460        return $content_icon;
461    }
462
463    /**
464     * Returns an array containing the supported icons for JITMs.
465     *
466     * The list includes an empty string, which is used when no icon should be displayed.
467     *
468     * @return array The list of supported icons.
469     */
470    public function get_supported_icons() {
471        return array(
472            'jetpack',
473            'woocommerce',
474            '',
475        );
476    }
477
478    /**
479     * Stores dismiss data into an option
480     *
481     * @param string $key Dismiss key.
482     */
483    public function save_dismiss( $key ) {
484        $hide_jitm = \Jetpack_Options::get_option( 'hide_jitm' );
485        if ( ! is_array( $hide_jitm ) ) {
486            $hide_jitm = array();
487        }
488
489        if ( ! isset( $hide_jitm[ $key ] ) || ! is_array( $hide_jitm[ $key ] ) ) {
490            $hide_jitm[ $key ] = array(
491                'last_dismissal' => 0,
492                'number'         => 0,
493            );
494        }
495
496        $hide_jitm[ $key ] = array(
497            'last_dismissal' => time(),
498            'number'         => $hide_jitm[ $key ]['number'] + 1,
499        );
500
501        \Jetpack_Options::update_option( 'hide_jitm', $hide_jitm );
502    }
503
504    /**
505     * Sets the 'jetpack_last_plugin_sync' transient when the active_plugins option is synced.
506     *
507     * @param array $params The action parameters.
508     *
509     * @return array Returns the action parameters unchanged.
510     */
511    public function jetpack_track_last_sync_callback( $params ) {
512        /**
513         * This filter is documented in the Automattic\Jetpack\JITMS\Post_Connection_JITM class.
514         */
515        if ( ! apply_filters( 'jetpack_just_in_time_msg_cache', true ) ) {
516            return $params;
517        }
518
519        if ( is_array( $params ) && isset( $params[0] ) ) {
520            $option = $params[0];
521            if ( 'active_plugins' === $option ) {
522                // Use the cache if we can, but not terribly important if it gets evicted.
523                set_transient( 'jetpack_last_plugin_sync', time(), HOUR_IN_SECONDS );
524            }
525        }
526
527        return $params;
528    }
529
530    /**
531     * Check if the current site is connected.
532     * On WordPress.com Simple, it is always connected.
533     *
534     * @return bool true if the site is connected, false otherwise.
535     */
536    private static function is_connected() {
537        if ( ( new Host() )->is_wpcom_simple() ) {
538            return true;
539        }
540
541        $connection = new Connection_Manager();
542        return $connection->is_connected();
543    }
544}