forked from wikimedia/wikipediapreview-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanner.php
121 lines (107 loc) · 3.52 KB
/
banner.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
<?php
/*
* This option will contain the UNIX timestamp of when the banner
* was dismissed or the value 0 to indicate it should never be shown again.
*/
DEFINE( 'WIKIPEDIA_PREVIEW_BANNER_OPTION', 'wikipediapreview_banner_dismissed' );
DEFINE( 'WIKIPEDIA_PREVIEW_INIT_TIMESTAMP', 'wikipediapreview_init_timestamp' );
function should_show_banner() {
if ( ! is_admin() ) {
// Only for admin site
return false;
}
// Show banner after 7 days after plugin initialization
$init_timestamp = get_option( WIKIPEDIA_PREVIEW_INIT_TIMESTAMP );
if ( ! $init_timestamp ) {
update_option( WIKIPEDIA_PREVIEW_INIT_TIMESTAMP, time() );
return false;
} elseif ( ( time() - $init_timestamp ) / ( 60 * 60 * 24 ) < 7 ) {
return false;
}
// Show banner after 7 days when user dismiss the dialog
// or dismiss banner forever when user press rate button
$default = -1;
$value = get_option( WIKIPEDIA_PREVIEW_BANNER_OPTION, $default );
if ( $value === $default ) {
// not dismissed yet
return true;
}
if ( '0' === $value ) {
// dismiss forever
return false;
}
// remind later
$days = ( time() - $value ) / ( 60 * 60 * 24 );
return $days >= 7;
}
function review_banner() {
if ( ! should_show_banner() ) {
return;
}
$msg = __( 'Enjoying Wikipedia Preview on your site? Drop a note and rating so that others can discover it.', 'wikipedia-preview' );
$rate_btn = __( 'Rate Wikipedia Preview', 'wikipedia-preview' );
$remind_btn = __( 'Remind me later', 'wikipedia-preview' );
$rate_url = 'https://wordpress.org/support/plugin/wikipedia-preview/reviews/#new-post';
$html = <<<HTML
<div class="notice notice-wikipediapreview notice-info is-dismissible">
<p style="font-size: 1.15em;">{$msg}</p>
<p>
<a href="{$rate_url}" target="_blank" class="button button-primary button-rate">{$rate_btn}</a>
<button class="button button-secondary button-remind">{$remind_btn}</button>
</p>
</div>
HTML;
$allowed_tags = array(
'div' => array( 'class' => array() ),
'p' => array( 'style' => array() ),
'a' => array(
'class' => array(),
'href' => array(),
'target' => array(),
),
'button' => array( 'class' => array() ),
'span' => array( 'class' => array() ),
);
echo wp_kses( $html, $allowed_tags );
}
function review_banner_script() {
if ( ! should_show_banner() ) {
return;
}
$nonce = wp_create_nonce( 'wikipediapreview-banner-dismiss' );
$html = <<<HTML
<script type='text/javascript'>
jQuery( function( $ ) {
$( '.notice-wikipediapreview' ).on(
'click',
'.button-rate, .notice-dismiss, .button-remind',
function () {
jQuery.post( ajaxurl, {
_ajax_nonce: '{$nonce}',
action: 'dismiss_review_banner',
remind: $( this ).hasClass( 'button-remind' )
} );
$( '.notice-wikipediapreview' ).hide();
}
);
} );
</script>
HTML;
echo wp_kses( $html, array( 'script' => array( 'type' => array() ) ) );
}
function dismiss_review_banner() {
check_ajax_referer( 'wikipediapreview-banner-dismiss' );
$remind = isset( $_POST['remind'] ) ? sanitize_key( $_POST['remind'] ) : 'false';
update_option(
WIKIPEDIA_PREVIEW_BANNER_OPTION,
'true' === $remind ? time() : 0
);
wp_die();
}
function remove_init_timestamp_options() {
delete_option( 'wikipediapreview_init_timestamp' );
}
add_action( 'admin_notices', 'review_banner' );
add_action( 'admin_footer', 'review_banner_script' );
add_action( 'wp_ajax_dismiss_review_banner', 'dismiss_review_banner' );
register_uninstall_hook( __FILE__, 'remove_init_timestamp_options' );