-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtooltip.php
109 lines (96 loc) · 2.89 KB
/
tooltip.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
<?php
/*
* The number of times the tooltip has been displayed, whether in full duration or not.
* This count can be incremented until we hit a limit (set on the client side).
*/
DEFINE( 'WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_COUNT', 'wikipediapreview_tooltip_count' );
/*
* Whether or not the tooltip has been displayed in full duration at least once,
* this acts as a boolean that's either 0 or 1. When set to 1, we infer the
* Wikipedia Preview plugin has been 'discovered' and hence we do not display it again
*/
DEFINE( 'WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_DURATION', 'wikipediapreview_tooltip_duration' );
function wikipediapreview_get_tooltip_count() {
return get_option(
WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_COUNT,
0
);
}
function wikipediapreview_increment_tooltip_count() {
$count = wikipediapreview_get_tooltip_count();
update_option(
WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_COUNT,
$count + 1
);
}
function wikipediapreview_update_tooltip_duration() {
update_option(
WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_DURATION,
1
);
}
// For debugging purposes
function wikipediapreview_reset_tooltip_properties() {
update_option(
WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_COUNT,
0
);
update_option(
WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_DURATION,
0
);
}
function wikipediapreview_set_rest_endpoint() {
register_rest_route(
'wikipediapreview/v1',
'/count/',
array(
'methods' => 'POST',
'callback' => 'wikipediapreview_increment_tooltip_count',
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
)
);
register_rest_route(
'wikipediapreview/v1',
'/duration/',
array(
'methods' => 'POST',
'callback' => 'wikipediapreview_update_tooltip_duration',
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
)
);
register_rest_route(
'wikipediapreview/v1',
'/reset/',
array(
'methods' => 'POST',
'callback' => 'wikipediapreview_reset_tooltip_properties',
'permission_callback' => '__return_true',
)
);
}
function wikipediapreview_tooltip_enqueue_script() {
$src_link_dir = plugin_dir_url( __FILE__ ) . 'src/link';
$no_dependencies = array();
$in_footer = true;
$nonce = wp_create_nonce( 'wp_rest' );
wp_enqueue_script(
'wikipedia-preview-tooltip',
$src_link_dir . 'tooltip.js',
$no_dependencies,
WIKIPEDIA_PREVIEW_PLUGIN_VERSION,
$in_footer
);
$options = array(
'tooltipCount' => wikipediapreview_get_tooltip_count(),
'tooltipDuration' => get_option( WIKIPEDIA_PREVIEW_TOOLTIP_DISPLAYED_DURATION, 0 ),
'nonce' => $nonce,
);
wp_localize_script( 'wikipedia-preview-tooltip', 'wikipediapreviewCustomTooltip', $options );
}
add_action( 'enqueue_block_editor_assets', 'wikipediapreview_tooltip_enqueue_script' );
add_action( 'rest_api_init', 'wikipediapreview_set_rest_endpoint' );