| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class that sets up admin pointers / tooltips for Charitable. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Admin |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.8.1.5 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Admin_Pointers' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Admin_Pointers |
| 21 |
* |
| 22 |
* @since 1.8.1.5 |
| 23 |
*/ |
| 24 |
class Charitable_Admin_Pointers { |
| 25 |
|
| 26 |
/** |
| 27 |
* Pointers. |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
public $pointers = array(); |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor. |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
add_action( 'admin_enqueue_scripts', array( $this, 'setup_pointers_for_screen' ) ); |
| 38 |
add_action( 'in_admin_header', array( $this, 'register_pointer' ) ); |
| 39 |
add_action( 'wp_ajax_charitable_dismiss_pointer', array( $this, 'ajax_charitable_dismiss_pointer' ) ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Dismiss pointer via ajax. |
| 44 |
* |
| 45 |
* @version 1.8.1.5 |
| 46 |
*/ |
| 47 |
public function ajax_charitable_dismiss_pointer() { |
| 48 |
|
| 49 |
$dismissed = $this->dismiss_pointer(); |
| 50 |
|
| 51 |
wp_send_json_success( $dismissed ); |
| 52 |
exit; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Load correct pointers if any. |
| 57 |
* |
| 58 |
* @version 1.8.1.5 |
| 59 |
*/ |
| 60 |
public function setup_pointers_for_screen() { |
| 61 |
|
| 62 |
if ( ! function_exists( 'get_current_screen' ) ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
$screen = get_current_screen(); |
| 67 |
if ( ! $screen || ! is_object( $screen ) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
if ( ! in_array( $screen->id, charitable_get_charitable_screens() ) ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
// Don't show this immediately for new users. |
| 76 |
$slug = 'donor-pointers'; |
| 77 |
|
| 78 |
// determine when to display this message. for now, there should be some sensible boundaries before showing the notification: a minimum of 14 days of use, created one donation form and received at least one donation. |
| 79 |
$activated_datetime = ( false !== get_option( 'wpcharitable_activated_datetime' ) ) ? get_option( 'wpcharitable_activated_datetime' ) : false; |
| 80 |
$activated_datetime = ( false === $activated_datetime ) ? get_option( 'charitable_activated', 0 ) : $activated_datetime; |
| 81 |
$days = 0; |
| 82 |
if ( $activated_datetime ) { |
| 83 |
$diff = current_time( 'timestamp' ) - $activated_datetime; |
| 84 |
$days = abs(ceil($diff / 86400)); |
| 85 |
} |
| 86 |
|
| 87 |
$count_campaigns = wp_count_posts( 'campaign' ); |
| 88 |
$total_campaigns = isset( $count_campaigns->publish ) ? $count_campaigns->publish : 0; |
| 89 |
|
| 90 |
// If days is at least 1 and the user isn't already on the 'charitable-donors' page, show the pointer. |
| 91 |
if ( $days >= apply_filters( 'charitable_days_since_activated', 1 ) && isset( $_GET['page'] ) && $_GET['page'] !== 'charitable-donors' ) { // phpcs:ignore |
| 92 |
// check transient. |
| 93 |
$help_pointers = get_transient( 'charitable_' . $slug . '_onboarding' ); |
| 94 |
|
| 95 |
// render five star rating banner/notice. |
| 96 |
if ( ! $help_pointers ) { |
| 97 |
wp_enqueue_style( 'wp-pointer' ); |
| 98 |
wp_enqueue_script( 'wp-pointer' ); |
| 99 |
$this->create_new_features_showoff(); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Pointers for creating a feature show off! |
| 106 |
* |
| 107 |
* @version 1.8.1.5 |
| 108 |
* @version 1.8.5.1 |
| 109 |
* |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
public function create_new_features_showoff() { |
| 113 |
|
| 114 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
if ( defined( 'CHARITABLE_DISABLE_ADMIN_POINTERS' ) && CHARITABLE_DISABLE_ADMIN_POINTERS ) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
// These pointers will chain - they will not be shown at once. |
| 123 |
$this->pointers = array( |
| 124 |
'pointers' => array( |
| 125 |
'tools' => array( |
| 126 |
'target' => '#toplevel_page_charitable ul li.donors', |
| 127 |
'next' => false, |
| 128 |
'next_trigger' => array(), |
| 129 |
'options' => array( |
| 130 |
'content' => '<h3>' . esc_html__( 'New Donor Management', 'charitable' ) . '</h3>' . |
| 131 |
'<p>' . sprintf( |
| 132 |
/* translators: %s: URL to upgrade to Pro */ |
| 133 |
esc_html__( 'Maintain detailed donor profiles, monitor donation histories, and streamline your communication with supporters. Consider upgrading to %s.', 'charitable' ), |
| 134 |
'<a href="' . charitable_utm_link( 'https://wpcharitable.com/pricing/', 'donor-pointers', 'upgrade-to-pro' ) . '">Charitable Pro</a>' |
| 135 |
) . '</p>', |
| 136 |
'position' => array( |
| 137 |
'edge' => 'left', |
| 138 |
'align' => 'left', |
| 139 |
), |
| 140 |
'visit_button' => array( |
| 141 |
'url' => admin_url( 'admin.php?page=charitable-donors' ), |
| 142 |
'label' => esc_html__( 'Go Here', 'charitable' ), |
| 143 |
), |
| 144 |
), |
| 145 |
), |
| 146 |
), |
| 147 |
); |
| 148 |
|
| 149 |
// if CHARITABLE_FORCE_ADMIN_POINTERS isn't defined, we'll check if the user has dismissed the pointers before. |
| 150 |
if ( ! defined( 'CHARITABLE_FORCE_ADMIN_POINTERS' ) || ! CHARITABLE_FORCE_ADMIN_POINTERS ) { |
| 151 |
|
| 152 |
// Check user meta - if the user has seen these before, don't show them again. |
| 153 |
$dismissed = get_user_meta( get_current_user_id(), 'charitable-pointer-slug-dismissed', true ); |
| 154 |
|
| 155 |
if ( ! empty( $dismissed ) ) { |
| 156 |
foreach ( $dismissed as $pointer ) { |
| 157 |
if ( isset( $this->pointers['pointers'][ $pointer ] ) ) { |
| 158 |
unset( $this->pointers['pointers'][ $pointer ] ); |
| 159 |
} |
| 160 |
} |
| 161 |
// if any of the pointer keys are found in dismissed remove all the keys. |
| 162 |
if ( in_array( array_keys( $this->pointers['pointers'] ), $dismissed, true ) ) { |
| 163 |
foreach ( $this->pointers['pointers'] as $pointer => $pointer_info ) { |
| 164 |
unset( $this->pointers['pointers'][ $pointer ] ); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Register pointers and added needed JavaScript. |
| 174 |
* |
| 175 |
* @version 1.8.1.5 |
| 176 |
* |
| 177 |
* @return void |
| 178 |
*/ |
| 179 |
public function register_pointer() { |
| 180 |
|
| 181 |
if ( empty( $this->pointers ) ) { |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
$pointers = wp_json_encode( $this->pointers ); |
| 186 |
|
| 187 |
// phpcs:disable |
| 188 |
echo( |
| 189 |
"<script> |
| 190 |
jQuery( function( $ ) { |
| 191 |
var wpchar_pointers = {$pointers}; |
| 192 |
|
| 193 |
setTimeout( init_wpchar_pointers, 800 ); |
| 194 |
|
| 195 |
function init_wpchar_pointers() { |
| 196 |
$.each( wpchar_pointers.pointers, function( i ) { |
| 197 |
show_wpchar_pointer( i ); |
| 198 |
return false; |
| 199 |
}); |
| 200 |
} |
| 201 |
|
| 202 |
function show_wpchar_pointer( id ) { |
| 203 |
// Check if WordPress pointer function is available |
| 204 |
if ( typeof $.fn.pointer === 'undefined' ) { |
| 205 |
console.warn( 'Charitable: WordPress pointer function not available' ); |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
var pointer = wpchar_pointers.pointers[ id ]; |
| 210 |
var options = $.extend( pointer.options, { |
| 211 |
pointerClass: 'wp-pointer charitable-pointer', |
| 212 |
pointerWidth: 420, |
| 213 |
close: function() { |
| 214 |
if ( pointer.next ) { |
| 215 |
show_wpchar_pointer( pointer.next ); |
| 216 |
} |
| 217 |
}, |
| 218 |
buttons: function( event, t ) { |
| 219 |
var close = '" . esc_js( __( 'Dismiss', 'charitable' ) ) . "', |
| 220 |
next = '" . esc_js( __( 'Next', 'charitable' ) ) . "', |
| 221 |
visit = '" . esc_js( __( 'Go Here', 'charitable' ) ) . "', |
| 222 |
button = $( '<a class=\"close\" href=\"#\">' + close + '</a>' ), |
| 223 |
button2 = pointer.next ? $( '<a class=\"button button-primary\" href=\"#\">' + next + '</a>' ) : ''; |
| 224 |
button_goto = $( '<a class=\"button button-primary button-visit\" href=\"' + pointer.options.visit_button.url + '\">' + pointer.options.visit_button.label + '</a>' ), |
| 225 |
wrapper = $( '<div class=\"charitable-pointer-buttons\" />' ); |
| 226 |
|
| 227 |
button.bind( 'click.pointer', function(e) { |
| 228 |
e.preventDefault(); |
| 229 |
t.element.pointer('destroy'); |
| 230 |
|
| 231 |
$.ajax({ |
| 232 |
type: 'POST', |
| 233 |
data: { |
| 234 |
action : 'charitable_dismiss_pointer', |
| 235 |
pointer : id, |
| 236 |
notice : '', |
| 237 |
}, |
| 238 |
dataType: 'json', |
| 239 |
url: ajaxurl |
| 240 |
}).fail(function ( response ) { |
| 241 |
if ( window.console && window.console.log ) { |
| 242 |
console.log( response ); |
| 243 |
} |
| 244 |
}); |
| 245 |
|
| 246 |
}); |
| 247 |
|
| 248 |
if ( button2 !== '' ) { |
| 249 |
button2.bind( 'click.pointer', function(e) { |
| 250 |
e.preventDefault(); |
| 251 |
t.element.pointer('close'); |
| 252 |
|
| 253 |
$.ajax({ |
| 254 |
type: 'POST', |
| 255 |
data: { |
| 256 |
action : 'charitable_dismiss_pointer', |
| 257 |
pointer : id, |
| 258 |
notice : '', |
| 259 |
}, |
| 260 |
dataType: 'json', |
| 261 |
url: ajaxurl |
| 262 |
}).fail(function ( response ) { |
| 263 |
if ( window.console && window.console.log ) { |
| 264 |
console.log( response ); |
| 265 |
} |
| 266 |
}); |
| 267 |
|
| 268 |
}); |
| 269 |
} |
| 270 |
|
| 271 |
wrapper.append( button ); |
| 272 |
if ( button2 !== '' ) { |
| 273 |
wrapper.append( button2 ); |
| 274 |
} |
| 275 |
wrapper.append( button_goto ); |
| 276 |
|
| 277 |
return wrapper; |
| 278 |
}, |
| 279 |
} ); |
| 280 |
var this_pointer = $( pointer.target ).pointer( options ); |
| 281 |
this_pointer.pointer( 'open' ); |
| 282 |
|
| 283 |
if ( pointer.next_trigger ) { |
| 284 |
$( pointer.next_trigger.target ).on( pointer.next_trigger.event, function() { |
| 285 |
setTimeout( function() { this_pointer.pointer( 'close' ); }, 400 ); |
| 286 |
}); |
| 287 |
} |
| 288 |
} |
| 289 |
});</script>" |
| 290 |
); |
| 291 |
// phpcs:enable |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Dismiss pointer. |
| 296 |
* |
| 297 |
* @version 1.8.1.5 |
| 298 |
* |
| 299 |
* @return bool |
| 300 |
*/ |
| 301 |
public function dismiss_pointer() { |
| 302 |
|
| 303 |
if ( isset( $_POST['action'] ) && 'charitable_dismiss_pointer' == $_POST['action'] ) { // phpcs:ignore |
| 304 |
// get dismissed pointer slugs from the current user. |
| 305 |
$dismissed = (array) get_user_meta( get_current_user_id(), 'charitable-pointer-slug-dismissed', true ); |
| 306 |
if ( false === $dismissed ) { |
| 307 |
$dismissed = array(); |
| 308 |
} |
| 309 |
$dismissed[] = esc_attr( $_POST['pointer'] ); // phpcs:ignore |
| 310 |
$dismissed = array_unique( $dismissed ); |
| 311 |
$dismissed = array_filter( $dismissed ); |
| 312 |
return update_user_meta( |
| 313 |
get_current_user_id(), |
| 314 |
'charitable-pointer-slug-dismissed', |
| 315 |
$dismissed |
| 316 |
); |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
new Charitable_Admin_Pointers(); |
| 322 |
|
| 323 |
endif; |
| 324 |
|