PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
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 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-jitm / src / class-jitm.php

class-jitm.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2, at jetpack_vendor/automattic/jetpack-jitm/src/class-jitm.php

484 lines 17.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.0.2';
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 * Sets up JITM action callbacks if needed.
122 */
123 public function register() {
124 if ( did_action( 'jetpack_registered_jitms' ) ) {
125 // JITMs have already been registered.
126 return;
127 }
128
129 if ( ! $this->jitms_enabled() ) {
130 // Do nothing.
131 return;
132 }
133
134 add_action( 'rest_api_init', array( __NAMESPACE__ . '\\Rest_Api_Endpoints', 'register_endpoints' ) );
135
136 add_action( 'current_screen', array( $this, 'prepare_jitms' ) );
137
138 /**
139 * These are sync actions that we need to keep track of for jitms.
140 */
141 add_filter( 'jetpack_sync_before_send_updated_option', array( $this, 'jetpack_track_last_sync_callback' ), 99 );
142
143 /**
144 * Fires when the JITMs are registered. This action is used to ensure that
145 * JITMs are registered only once.
146 *
147 * @since 1.16.0
148 */
149 do_action( 'jetpack_registered_jitms' );
150 }
151
152 /**
153 * Checks the jetpack_just_in_time_msgs filters and whether the site
154 * is offline to determine whether JITMs are enabled.
155 *
156 * @return bool True if JITMs are enabled, else false.
157 */
158 public function jitms_enabled() {
159 return self::are_jitms_enabled();
160 }
161
162 /**
163 * Static variant of jitms_enabled() so the enabled state can be checked
164 * without building a JITM instance (used by configure()).
165 *
166 * @return bool True if JITMs are enabled, else false.
167 */
168 private static function are_jitms_enabled() {
169 /**
170 * Filter to turn off all just in time messages
171 *
172 * @since 1.1.0
173 * @since-jetpack 3.7.0
174 * @since-jetpack 5.4.0 Correct docblock to reflect default arg value
175 *
176 * @param bool true Whether to show just in time messages.
177 */
178 if ( ! apply_filters( 'jetpack_just_in_time_msgs', true ) ) {
179 return false;
180 }
181
182 // Folks cannot connect to WordPress.com and won't really be able to act on the pre-connection messages. So bail.
183 if ( ( new Status() )->is_offline_mode() ) {
184 return false;
185 }
186
187 return true;
188 }
189
190 /**
191 * Prepare actions according to screen and post type.
192 *
193 * @since 1.1.0
194 * @since-jetpack 3.8.2
195 *
196 * @uses Jetpack_Autoupdate::get_possible_failures()
197 *
198 * @param \WP_Screen $screen WP Core's screen object.
199 */
200 public function prepare_jitms( $screen ) {
201 /**
202 * Filter to hide JITMs on certain screens.
203 *
204 * @since 1.14.0
205 *
206 * @param bool true Whether to show just in time messages.
207 * @param string $string->id The ID of the current screen.
208 */
209 if ( apply_filters( 'jetpack_display_jitms_on_screen', true, $screen->id ) ) {
210 add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) );
211 add_action( 'admin_notices', array( $this, 'ajax_message' ) );
212 add_action( 'edit_form_top', array( $this, 'ajax_message' ) );
213 }
214 }
215
216 /**
217 * Check if the current page is a Jetpack or WooCommerce admin page.
218 * Noting that this is a very basic check, and pages from other plugins may also match.
219 *
220 * @since 3.1.0
221 *
222 * @return bool True if the current page is a Jetpack or WooCommerce admin page, else false.
223 */
224 public function is_a8c_admin_page() {
225 if ( ! function_exists( 'get_current_screen' ) ) {
226 return false;
227 }
228
229 $current_screen = get_current_screen();
230
231 // We never want to show JITMs on the block editor.
232 if (
233 method_exists( $current_screen, 'is_block_editor' )
234 && $current_screen->is_block_editor()
235 ) {
236 return false;
237 }
238
239 return (
240 $current_screen
241 && $current_screen->id
242 && (bool) preg_match(
243 '/' . implode( '|', self::APPROVED_SCREEN_IDS ) . '/',
244 $current_screen->id
245 )
246 );
247 }
248
249 /**
250 * Function to enqueue jitm css and js
251 */
252 public function jitm_enqueue_files() {
253 // Only load those files on the Jetpack or Woo admin pages.
254 if ( ! $this->is_a8c_admin_page() ) {
255 return;
256 }
257
258 Assets::register_script(
259 'jetpack-jitm',
260 '../build/index.js',
261 __FILE__,
262 array(
263 'in_footer' => true,
264 'dependencies' => array(),
265 'enqueue' => true,
266 )
267 );
268 wp_localize_script(
269 'jetpack-jitm',
270 'jitm_config',
271 array(
272 'api_root' => esc_url_raw( rest_url() ),
273 'activate_module_text' => esc_html__( 'Activate', 'jetpack-jitm' ),
274 'activated_module_text' => esc_html__( 'Activated', 'jetpack-jitm' ),
275 'activating_module_text' => esc_html__( 'Activating', 'jetpack-jitm' ),
276 'settings_module_text' => esc_html__( 'Settings', 'jetpack-jitm' ),
277 'nonce' => wp_create_nonce( 'wp_rest' ),
278 )
279 );
280 }
281
282 /**
283 * Is the current page a block editor page?
284 *
285 * @since 1.1.0
286 * @since-jetpack 8.0.0
287 *
288 * @deprecated 3.1.0
289 */
290 public function is_gutenberg_page() {
291 _deprecated_function( __METHOD__, '3.1.0' );
292 $current_screen = get_current_screen();
293 return ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() );
294 }
295
296 /**
297 * Get's the current message path for display of a JITM
298 *
299 * @return string The message path
300 */
301 public function get_message_path() {
302 $screen = get_current_screen();
303
304 return 'wp:' . $screen->id . ':' . current_filter();
305 }
306
307 /**
308 * Injects the dom to show a JITM inside of wp-admin.
309 */
310 public function ajax_message() {
311 if ( ! is_admin() ) {
312 return;
313 }
314
315 // Only add this to specifically allowed pages.
316 if ( ! $this->is_a8c_admin_page() ) {
317 return;
318 }
319
320 $message_path = $this->get_message_path();
321 $query_string = _http_build_query( $_GET, '', ',' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
322 $current_screen = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- Escaped below
323 ?>
324 <div class="jetpack-jitm-message"
325 data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_rest' ) ); ?>"
326 data-ajax-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_ajax_action' ) ); ?>"
327 data-message-path="<?php echo esc_attr( $message_path ); ?>"
328 data-query="<?php echo urlencode_deep( $query_string ); ?>"
329 data-redirect="<?php echo urlencode_deep( $current_screen ); ?>"
330 ></div>
331 <?php
332 }
333
334 /**
335 * Generate the icon to display on the JITM.
336 *
337 * All icons supported in this method should be included in the array returned by
338 * JITM::get_supported_icons.
339 *
340 * @param string $content_icon Icon type name.
341 * @param bool $full_jp_logo_exists Is there a big JP logo already displayed on this screen.
342 */
343 public function generate_icon( $content_icon, $full_jp_logo_exists ) {
344 $date_now = new \DateTime( 'now', wp_timezone() );
345 $feb_4_date = new \DateTime( '02-04-2025', wp_timezone() );
346 // Don't show the new Woo svg logo until after Feb 4th, 2025
347 $show_new_logo = $date_now >= $feb_4_date;
348
349 switch ( $content_icon ) {
350 case 'jetpack':
351 $jetpack_logo = new Jetpack_Logo();
352 $content_icon = '<div class="jp-emblem">' . ( ( $full_jp_logo_exists ) ? $jetpack_logo->get_jp_emblem() : $jetpack_logo->get_jp_emblem_larger() ) . '</div>';
353 break;
354 case 'woocommerce':
355 // After Feb 4th 2025, we can remove this date condition check ( & filter) and the old svg logo.
356 /**
357 * Filter to force display the new Woo logo in Woo JITM's, or not.
358 *
359 * @since 4.0.5
360 *
361 * @param bool $show_woo_logo Whether to show the new Woo logo or not.
362 */
363 $content_icon = apply_filters( 'jetpack_jitm_use_new_woo_logo', $show_new_logo )
364 // New Woo logo
365 ? '<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"
366 viewBox="0 0 183.6 47.5" style="enable-background:new 0 0 183.6 47.5;" xml:space="preserve">
367 <style type="text/css">
368 .st0{fill-rule:evenodd;clip-rule:evenodd;fill:#873EFF;}
369 .st1{fill-rule:evenodd;clip-rule:evenodd;}
370 .st2{fill:#873EFF;}
371 .st3{fill-rule:evenodd;clip-rule:evenodd;fill:#FFFFFF;}
372 .st4{fill:#FFFFFF;}
373 </style>
374 <g>
375 <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
376 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
377 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"/>
378 <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
379 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"/>
380 <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
381 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"/>
382 </g>
383 </svg></div>'
384 // Old Woo logo
385 : '<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">
386 .st0{clip-path:url(#SVGID_2_);enable-background:new ;}
387 .st1{clip-path:url(#SVGID_4_);}
388 .st2{clip-path:url(#SVGID_6_);}
389 .st3{clip-path:url(#SVGID_8_);fill:#7F54B3;}
390 .st4{clip-path:url(#SVGID_10_);fill:#FFFFFE;}
391 .st5{clip-path:url(#SVGID_12_);fill:#FFFFFE;}
392 .st6{clip-path:url(#SVGID_14_);fill:#FFFFFE;}
393 </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>';
394 break;
395 default:
396 $content_icon = '';
397 break;
398 }
399 return $content_icon;
400 }
401
402 /**
403 * Returns an array containing the supported icons for JITMs.
404 *
405 * The list includes an empty string, which is used when no icon should be displayed.
406 *
407 * @return array The list of supported icons.
408 */
409 public function get_supported_icons() {
410 return array(
411 'jetpack',
412 'woocommerce',
413 '',
414 );
415 }
416
417 /**
418 * Stores dismiss data into an option
419 *
420 * @param string $key Dismiss key.
421 */
422 public function save_dismiss( $key ) {
423 $hide_jitm = \Jetpack_Options::get_option( 'hide_jitm' );
424 if ( ! is_array( $hide_jitm ) ) {
425 $hide_jitm = array();
426 }
427
428 if ( ! isset( $hide_jitm[ $key ] ) || ! is_array( $hide_jitm[ $key ] ) ) {
429 $hide_jitm[ $key ] = array(
430 'last_dismissal' => 0,
431 'number' => 0,
432 );
433 }
434
435 $hide_jitm[ $key ] = array(
436 'last_dismissal' => time(),
437 'number' => $hide_jitm[ $key ]['number'] + 1,
438 );
439
440 \Jetpack_Options::update_option( 'hide_jitm', $hide_jitm );
441 }
442
443 /**
444 * Sets the 'jetpack_last_plugin_sync' transient when the active_plugins option is synced.
445 *
446 * @param array $params The action parameters.
447 *
448 * @return array Returns the action parameters unchanged.
449 */
450 public function jetpack_track_last_sync_callback( $params ) {
451 /**
452 * This filter is documented in the Automattic\Jetpack\JITMS\Post_Connection_JITM class.
453 */
454 if ( ! apply_filters( 'jetpack_just_in_time_msg_cache', true ) ) {
455 return $params;
456 }
457
458 if ( is_array( $params ) && isset( $params[0] ) ) {
459 $option = $params[0];
460 if ( 'active_plugins' === $option ) {
461 // Use the cache if we can, but not terribly important if it gets evicted.
462 set_transient( 'jetpack_last_plugin_sync', time(), HOUR_IN_SECONDS );
463 }
464 }
465
466 return $params;
467 }
468
469 /**
470 * Check if the current site is connected.
471 * On WordPress.com Simple, it is always connected.
472 *
473 * @return bool true if the site is connected, false otherwise.
474 */
475 private static function is_connected() {
476 if ( ( new Host() )->is_wpcom_simple() ) {
477 return true;
478 }
479
480 $connection = new Connection_Manager();
481 return $connection->is_connected();
482 }
483 }
484