-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-admin-pointers.php
More file actions
117 lines (89 loc) · 3.18 KB
/
Copy pathclass-admin-pointers.php
File metadata and controls
117 lines (89 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Initializes pointer for the admin license page
*/
if ( ! class_exists('GambitAdminPointers') ) {
class GambitAdminPointers {
// Holds the number of pointers currently active
public static $pointersActive = 0;
function __construct( $settings = array() ) {
// Initialize default settings
$defaults = array(
'pointer_name' => 'gambit',
'header' => __( 'Automatic Updates', 'default' ),
'body' => __( 'Keep your plugin updated by entering your purchase code here.', 'default' ),
);
$this->settings = array_merge( $defaults, $settings );
// Pointers are only allowed to have names in small caps (WP requirement)
$this->settings['pointer_name'] = strtolower( $this->settings['pointer_name'] );
// Initialize admin point headers
add_action( 'admin_enqueue_scripts', array( $this, 'enqueuePointerScript' ) );
add_action( 'admin_print_footer_scripts', array( $this, 'printPointerScript' ) );
}
public function enqueuePointerScript() {
if ( $this->formAdminPointer() ) {
wp_enqueue_script( 'wp-pointer' );
wp_enqueue_style( 'wp-pointer' );
}
}
public function printPointerScript() {
if ( ! $this->formAdminPointer() ) {
return;
}
// Get the pointer
$adminPointer = $this->formAdminPointer();
// Only allow a single pointer to exist
// If another Gambit pointer has already been displayed, never show ours
// so we do not clutter the screen. (this might happen if multiple
// plugins are activated at the same time)
if ( self::$pointersActive > 0 ) {
$dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
if ( ! in_array( $adminPointer['name'], $dismissed ) ) {
$dismissed[] = $adminPointer['name'];
update_user_meta( get_current_user_id(), 'dismissed_wp_pointers', implode( ',', $dismissed ) );
}
return;
}
// Start the pointer
?>
<script type="text/javascript">
( function($) {
var $a = $('#menu-plugins');
if ( $('a[href="plugins.php?page=gambit_plugins"]').length > 0 ) {
if ( $('a[href="plugins.php?page=gambit_plugins"]').offset().top > 0 ) {
$a = $('a[href="plugins.php?page=gambit_plugins"]');
}
}
$a.pointer( {
content: '<?php echo $adminPointer['content'] ?>',
position: {
edge: 'left',
align: 'middle'
},
close: function() {
$.post( ajaxurl, {
pointer: '<?php echo esc_attr( $adminPointer['name'] ) ?>',
action: 'dismiss-wp-pointer'
} );
}
} ).pointer( 'open' );
} )(jQuery);
</script>
<?php
// Admin pointers displayed + 1
self::$pointersActive++;
}
public function formAdminPointer() {
$dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
if ( in_array( $this->settings['pointer_name'], $dismissed ) ) {
return false;
}
$content = '<h3>' . esc_attr( $this->settings['header'] ) . '</h3>';
$content .= '<p>' . esc_attr( $this->settings['body'] ) . '</p>';
return array(
'name' => $this->settings['pointer_name'],
'content' => $content,
);
}
}
}