-
Notifications
You must be signed in to change notification settings - Fork 5
/
pointerplus.php
187 lines (166 loc) · 5.04 KB
/
pointerplus.php
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* @package PointerPlus
* @author QueryLoop & Mte90
* @license GPL-3.0+
* @link https://daniele.tech
* @copyright 2014-2022 GPL
*/
/**
* Super pointer creation for WP Admin
*/
class PointerPlus {
/**
* Prefix strings like styles, scripts and pointers IDs
* @var string
*/
var $prefix = 'pointerplus';
var $pointers = array();
/**
* Construct the class parameter
*
* @param array $args Parameters of class.
* @return void
*/
function __construct( $args = array() ) {
if ( isset( $args[ 'prefix' ] ) ) {
$this->prefix = $args[ 'prefix' ];
}
add_action( 'current_screen', array( $this, 'maybe_add_pointers' ) );
}
/**
* Set pointers and its options
*
* @since 1.0.0
*/
function initial_pointers() {
global $pagenow;
$defaults = array(
'class' => '',
'width' => 300, //only fixed value
'align' => 'middle',
'edge' => 'left',
'post_type' => array(),
'pages' => array(),
'icon_class' => ''
);
$screen = \get_current_screen();
$current_post_type = isset( $screen->post_type ) ? $screen->post_type : false;
$search_pt = false;
$pointers = \apply_filters( $this->prefix . '-pointerplus_list', array(
// Pointers are added through the 'initial_pointerplus' filter
), $this->prefix );
foreach ( $pointers as $key => $pointer ) {
$pointers[ $key ] = wp_parse_args( $pointer, $defaults );
$search_pt = false;
// Clean from null ecc
$pointers[ $key ][ 'post_type' ] = array_filter( $pointers[ $key ][ 'post_type' ] );
if ( !empty( $pointers[ $key ][ 'post_type' ] ) ) {
if ( !empty( $current_post_type ) ) {
if ( is_array( $pointers[ $key ][ 'post_type' ] ) ) {
// Search the post_type
foreach ( $pointers[ $key ][ 'post_type' ] as $value ) {
if ( $value === $current_post_type ) {
$search_pt = true;
}
}
if ( $search_pt === false ) {
unset( $pointers[ $key ] );
}
} else {
new \WP_Error( 'broke', \__( 'PointerPlus Error: post_type is not an array!' ) );
}
// If not in CPT view remove all the pointers with post_type
} else {
unset( $pointers[ $key ] );
}
}
// Clean from null ecc
if ( isset( $pointers[ $key ][ 'pages' ] ) ) {
if ( is_array( $pointers[ $key ][ 'pages' ] ) ) {
$pointers[ $key ][ 'pages' ] = array_filter( $pointers[ $key ][ 'pages' ] );
}
if ( !empty( $pointers[ $key ][ 'pages' ] ) ) {
if ( is_array( $pointers[ $key ][ 'pages' ] ) ) {
// Search the page
foreach ( $pointers[ $key ][ 'pages' ] as $value ) {
if ( $pagenow === $value ) {
$search_pt = true;
}
}
if ( $search_pt === false ) {
unset( $pointers[ $key ] );
}
} else {
new \WP_Error( 'broke', __( 'PointerPlus Error: pages is not an array!' ) );
}
}
}
}
return $pointers;
}
/**
* Check that pointers haven't been dismissed already. If there are pointers to show, enqueue assets.
*
* @since 1.0.0
*/
function maybe_add_pointers() {
// Get default pointers that we want to create
$default_keys = $this->initial_pointers();
// Get pointers dismissed by user
$dismissed = explode( ',', \get_user_meta( \get_current_user_id(), 'dismissed_wp_pointers', true ) );
// Check that our pointers haven't been dismissed already
$diff = array_diff_key( $default_keys, array_combine( $dismissed, $dismissed ) );
// If we have some pointers to show, save them and start enqueuing assets to display them
if ( !empty( $diff ) ) {
$this->pointers = $diff;
\add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_assets' ) );
foreach ( $diff as $pointer ) {
if ( isset( $pointer[ 'phpcode' ] ) ) {
\add_action( 'admin_notices', $pointer[ 'phpcode' ] );
}
}
}
$this->pointers[ 'l10n' ] = array(
'next' => __( 'Next' ),
'dismiss' => __( 'Dismiss' ),
);
}
/**
* Enqueue pointer styles and scripts to display them.
*
* @since 1.0.0
*/
function admin_enqueue_assets() {
$base_url = \plugins_url( '', __FILE__ );
\wp_enqueue_style( $this->prefix, $base_url . '/pointerplus.css', array( 'wp-pointer' ) );
\wp_enqueue_script( $this->prefix, $base_url . '/pointerplus.js?var=' . str_replace( '-', '_', $this->prefix ) . '_pointerplus', array( 'wp-pointer' ) );
\wp_localize_script( $this->prefix, str_replace( '-', '_', $this->prefix ) . '_pointerplus', \apply_filters( $this->prefix . '_pointerplus_js_vars', $this->pointers ) );
}
/**
* Reset pointer
*
* @since 1.0.0
*/
function reset_pointer() {
\add_action( 'current_screen', array( $this, '_reset_pointer' ), 0 );
}
/**
* Reset pointer in hook
*
* @since 1.0.0
*/
function _reset_pointer( $id = 'me' ) {
if ( $id === 'me' ) {
$id = \get_current_user_id();
}
$pointers = explode( ',', \get_user_meta( $id, 'dismissed_wp_pointers', true ) );
foreach ( $pointers as $key => $pointer ) {
if ( strpos( $pointer, $this->prefix ) === 0 ) {
unset( $pointers[ $key ] );
}
}
$meta = implode( ',', $pointers );
\update_user_meta( \get_current_user_id(), 'dismissed_wp_pointers', $meta );
}
}