-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclass.wp-help-pointers.php
More file actions
154 lines (120 loc) · 5.04 KB
/
class.wp-help-pointers.php
File metadata and controls
154 lines (120 loc) · 5.04 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* How to Use:
* Pointers are defined in an associative array and passed to the class upon instantiation.
* First we hook into the 'admin_enqueue_scripts' hook with our function:
*
* add_action('admin_enqueue_scripts', 'myHelpPointers');
*
* function myHelpPointers() {
* //First we define our pointers
* $pointers = array(
* array(
* 'id' => 'xyz123', // unique id for this pointer
* 'screen' => 'page', // this is the page hook we want our pointer to show on
* 'target' => '#element-selector', // the css selector for the pointer to be tied to, best to use ID's
* 'title' => 'My ToolTip',
* 'content' => 'My tooltips Description',
* 'position' => array(
* 'edge' => 'top', //top, bottom, left, right
* 'align' => 'middle' //top, bottom, left, right, middle
* )
* )
* // more as needed
* );
* //Now we instantiate the class and pass our pointer array to the constructor
* $myPointers = new WP_Help_Pointer($pointers);
* }
*
*
* @package WP_Help_Pointer
* @version 0.1
* @author Tim Debo <tim@rawcreativestudios.com>
* @copyright Copyright (c) 2012, Raw Creative Studios
* @link https://github.com/rawcreative/wp-help-pointers
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
class WP_Help_Pointer {
public $screen_id;
public $valid;
public $pointers;
public function __construct( $pntrs = array() ) {
// Don't run on WP < 3.3
if ( get_bloginfo( 'version' ) < '3.3' )
return;
$screen = get_current_screen();
$this->screen_id = $screen->id;
$this->register_pointers($pntrs);
add_action( 'admin_enqueue_scripts', array( &$this, 'add_pointers' ), 1000 );
add_action( 'admin_head', array( &$this, 'add_scripts' ) );
}
public function register_pointers( $pntrs ) {
foreach( $pntrs as $ptr ) {
if( $ptr['screen'] == $this->screen_id ) {
$pointers[$ptr['id']] = array(
'screen' => $ptr['screen'],
'target' => $ptr['target'],
'options' => array(
'content' => sprintf( '<h3> %s </h3> <p> %s </p>',
__( $ptr['title'] , 'plugindomain' ),
__( $ptr['content'], 'plugindomain' )
),
'position' => $ptr['position']
)
);
}
}
$this->pointers = $pointers;
}
public function add_pointers() {
$pointers = $this->pointers;
if ( ! $pointers || ! is_array( $pointers ) )
return;
// Get dismissed pointers
$dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
$valid_pointers = array();
// Check pointers and remove dismissed ones.
foreach ( $pointers as $pointer_id => $pointer ) {
// Make sure we have pointers & check if they have been dismissed
if ( in_array( $pointer_id, $dismissed ) || empty( $pointer ) || empty( $pointer_id ) || empty( $pointer['target'] ) || empty( $pointer['options'] ) )
continue;
$pointer['pointer_id'] = $pointer_id;
// Add the pointer to $valid_pointers array
$valid_pointers['pointers'][] = $pointer;
}
// No valid pointers? Stop here.
if ( empty( $valid_pointers ) )
return;
$this->valid = $valid_pointers;
wp_enqueue_style( 'wp-pointer' );
wp_enqueue_script( 'wp-pointer' );
}
public function add_scripts() {
$pointers = $this->valid;
if( empty( $pointers ) )
return;
$pointers = json_encode( $pointers );
echo <<<HTML
<script>
jQuery(document).ready( function($) {
var WPHelpPointer = {$pointers};
$.each(WPHelpPointer.pointers, function(i) {
wp_help_pointer_open(i);
});
function wp_help_pointer_open(i) {
pointer = WPHelpPointer.pointers[i];
options = $.extend( pointer.options, {
close: function() {
$.post( ajaxurl, {
pointer: pointer.pointer_id,
action: 'dismiss-wp-pointer'
});
}
});
$(pointer.target).pointer( options ).pointer('open');
}
});
</script>
HTML;
}
} // end class