| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Notification Onboarding Tour. |
| 4 |
* |
| 5 |
* Shows a one-time Shepherd.js tour on the Charitable dashboard after the site |
| 6 |
* upgrades to 1.8.14+, highlighting the notifications badge and the Latest |
| 7 |
* Updates dashboard section. Fires once per user; dismissed state stored in |
| 8 |
* user meta. |
| 9 |
* |
| 10 |
* @package Charitable/Classes/Charitable_Notification_Onboarding |
| 11 |
* @since 1.8.12 |
| 12 |
*/ |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
class Charitable_Notification_Onboarding { |
| 19 |
|
| 20 |
/** User meta key used to record dismissal. */ |
| 21 |
const TOUR_META_KEY = 'charitable_notifications_v2_tour_dismissed'; |
| 22 |
|
| 23 |
private static $instance = null; |
| 24 |
|
| 25 |
public static function get_instance() { |
| 26 |
if ( is_null( self::$instance ) ) { |
| 27 |
self::$instance = new self(); |
| 28 |
} |
| 29 |
return self::$instance; |
| 30 |
} |
| 31 |
|
| 32 |
public function init() { |
| 33 |
add_action( 'admin_enqueue_scripts', array( $this, 'maybe_enqueue_tour' ) ); |
| 34 |
add_action( 'wp_ajax_charitable_dismiss_notifications_tour', array( $this, 'ajax_dismiss' ) ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Enqueue the tour assets only when: |
| 39 |
* - We're on the Charitable dashboard page. |
| 40 |
* - The current user has not already dismissed the tour. |
| 41 |
*/ |
| 42 |
public function maybe_enqueue_tour() { |
| 43 |
$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 44 |
|
| 45 |
if ( 'charitable-dashboard' !== $page ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
// ?preview_notif_tour=1 clears the dismissal so the tour fires again. |
| 50 |
if ( ! empty( $_GET['preview_notif_tour'] ) && current_user_can( 'manage_options' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 51 |
delete_user_meta( get_current_user_id(), self::TOUR_META_KEY ); |
| 52 |
} |
| 53 |
|
| 54 |
if ( get_user_meta( get_current_user_id(), self::TOUR_META_KEY, true ) ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
$min = charitable_get_min_suffix(); |
| 59 |
$version = charitable()->get_version(); |
| 60 |
$dir = charitable()->get_path( 'directory', false ); |
| 61 |
$assets = charitable()->get_path( 'assets', false ); |
| 62 |
|
| 63 |
// Floating UI (Shepherd dependency). |
| 64 |
wp_enqueue_script( |
| 65 |
'charitable-float-ui-core', |
| 66 |
$dir . 'assets/js/libraries/floating-ui-core.min.js', |
| 67 |
array( 'jquery' ), |
| 68 |
$version |
| 69 |
); |
| 70 |
wp_enqueue_script( |
| 71 |
'charitable-float-ui-dom', |
| 72 |
$dir . 'assets/js/libraries/floating-ui-dom.min.js', |
| 73 |
array( 'charitable-float-ui-core' ), |
| 74 |
$version |
| 75 |
); |
| 76 |
|
| 77 |
// Shepherd.js. |
| 78 |
wp_enqueue_script( |
| 79 |
'charitable-shepherd', |
| 80 |
$dir . 'assets/js/libraries/shepherd.js', |
| 81 |
array( 'jquery', 'charitable-float-ui-core', 'charitable-float-ui-dom' ), |
| 82 |
$version |
| 83 |
); |
| 84 |
wp_enqueue_style( |
| 85 |
'charitable-shepherd', |
| 86 |
$dir . "assets/css/libraries/shepherd{$min}.css", |
| 87 |
array(), |
| 88 |
$version |
| 89 |
); |
| 90 |
|
| 91 |
// Tour script. |
| 92 |
wp_enqueue_script( |
| 93 |
'charitable-notifications-tour', |
| 94 |
$dir . "assets/js/admin/charitable-notifications-tour{$min}.js", |
| 95 |
array( 'jquery', 'charitable-shepherd' ), |
| 96 |
$version . '.' . filemtime( charitable()->get_path( 'directory' ) . "assets/js/admin/charitable-notifications-tour{$min}.js" ), |
| 97 |
true |
| 98 |
); |
| 99 |
|
| 100 |
wp_localize_script( |
| 101 |
'charitable-notifications-tour', |
| 102 |
'CHARITABLE_NOTIF_TOUR', |
| 103 |
array( |
| 104 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 105 |
'nonce' => wp_create_nonce( 'charitable_admin_nonce' ), |
| 106 |
) |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* AJAX — mark the tour dismissed for the current user. |
| 112 |
*/ |
| 113 |
public function ajax_dismiss() { |
| 114 |
check_ajax_referer( 'charitable_admin_nonce', 'nonce' ); |
| 115 |
update_user_meta( get_current_user_id(), self::TOUR_META_KEY, true ); |
| 116 |
wp_send_json_success(); |
| 117 |
} |
| 118 |
} |
| 119 |
|