1+ <?php
2+
3+ class Fancybox_Review {
4+
5+ private $ value ;
6+ private $ messages ;
7+ private $ link = 'https://wordpress.org/plugins/fancybox-for-wordpress/#reviews ' ;
8+ private $ slug = 'mfbfw ' ;
9+
10+ function __construct () {
11+
12+ $ this ->messages = array (
13+ 'notice ' => __ ( "Hi there! Stoked to see you're using Fancybox for a few days now - hope you like it! And if you do, please consider rating it. It would mean the world to us. Keep on rocking! " , 'mfbfw ' ),
14+ 'rate ' => __ ( 'Rate the plugin ' , 'mfbfw ' ),
15+ 'rated ' => __ ( 'Remind me later ' , 'mfbfw ' ),
16+ 'no_rate ' => __ ( 'Don \'t show again ' , 'mfbfw ' ),
17+ );
18+
19+ if ( isset ( $ args ['messages ' ] ) ) {
20+ $ this ->messages = wp_parse_args ( $ args ['messages ' ], $ this ->messages );
21+ }
22+
23+ add_action ( 'init ' , array ( $ this , 'init ' ) );
24+
25+ }
26+
27+ public function init () {
28+ if ( ! is_admin () ) {
29+ return ;
30+ }
31+
32+ $ this ->value = $ this ->value ();
33+
34+ if ( $ this ->check () ) {
35+ add_action ( 'admin_notices ' , array ( $ this , 'five_star_wp_rate_notice ' ) );
36+ add_action ( 'wp_ajax_epsilon_mfbfw_review ' , array ( $ this , 'ajax ' ) );
37+ add_action ( 'admin_enqueue_scripts ' , array ( $ this , 'enqueue ' ) );
38+ add_action ( 'admin_print_footer_scripts ' , array ( $ this , 'ajax_script ' ) );
39+ }
40+
41+ }
42+
43+ private function check () {
44+
45+ if ( ! current_user_can ('manage_options ' ) ) {
46+ return false ;
47+ }
48+
49+ return ( time () > $ this ->value );
50+
51+ }
52+
53+ private function value () {
54+
55+ $ value = get_option ( 'mfbfw-rate-time ' , false );
56+
57+ if ( $ value ) {
58+ return $ value ;
59+ }
60+
61+ $ value = time () + DAY_IN_SECONDS ;
62+ update_option ( 'mfbfw-rate-time ' , $ value );
63+
64+ return $ value ;
65+
66+ }
67+
68+ public function five_star_wp_rate_notice () {
69+
70+ $ url = sprintf ( $ this ->link , $ this ->slug );
71+
72+ ?>
73+ <div id="<?php echo esc_attr ($ this ->slug ) ?> -epsilon-review-notice" class="notice notice-success is-dismissible" style="margin-top:30px;">
74+ <p><?php echo sprintf ( esc_html ( $ this ->messages ['notice ' ] ), $ this ->value ) ; ?> </p>
75+ <p class="actions">
76+ <a id="epsilon-rate" href="<?php echo esc_url ( $ url ) ?> " target="_blank" class="button button-primary epsilon-review-button">
77+ <?php echo esc_html ( $ this ->messages ['rate ' ] ); ?>
78+ </a>
79+ <a id="epsilon-later" href="#" style="margin-left:10px" class="epsilon-review-button"><?php echo esc_html ( $ this ->messages ['rated ' ] ); ?> </a>
80+ <a id="epsilon-no-rate" href="#" style="margin-left:10px" class="epsilon-review-button"><?php echo esc_html ( $ this ->messages ['no_rate ' ] ); ?> </a>
81+ </p>
82+ </div>
83+ <?php
84+ }
85+
86+ public function ajax () {
87+
88+ check_ajax_referer ( 'epsilon-mfbfw-review ' , 'security ' );
89+
90+ if ( ! isset ( $ _POST ['check ' ] ) ) {
91+ wp_die ( 'ok ' );
92+ }
93+
94+ $ time = get_option ( 'mfbfw-rate-time ' );
95+
96+ if ( 'epsilon-rate ' == $ _POST ['check ' ] ) {
97+ $ time = time () + YEAR_IN_SECONDS * 5 ;
98+ }elseif ( 'epsilon-later ' == $ _POST ['check ' ] ) {
99+ $ time = time () + WEEK_IN_SECONDS ;
100+ }elseif ( 'epsilon-no-rate ' == $ _POST ['check ' ] ) {
101+ $ time = time () + YEAR_IN_SECONDS * 5 ;
102+ }
103+
104+ update_option ( 'mfbfw-rate-time ' , $ time );
105+ wp_die ( 'ok ' );
106+
107+ }
108+
109+ public function enqueue () {
110+ wp_enqueue_script ( 'jquery ' );
111+ }
112+
113+ public function ajax_script () {
114+
115+ $ ajax_nonce = wp_create_nonce ( "epsilon-mfbfw-review " );
116+
117+ ?>
118+
119+ <script type="text/javascript">
120+ jQuery( document ).ready( function( $ ){
121+
122+ $( '.epsilon-review-button' ).click( function( evt ){
123+ var href = $(this).attr('href'),
124+ id = $(this).attr('id');
125+
126+ if ( 'epsilon-rate' != id ) {
127+ evt.preventDefault();
128+ }
129+
130+ var data = {
131+ action: 'epsilon_mfbfw_review',
132+ security: '<?php echo $ ajax_nonce ; ?> ',
133+ check: id
134+ };
135+
136+ if ( 'epsilon-rated' === id || 'epsilon-rate' === id ) {
137+ data['epsilon-review'] = 1;
138+ }
139+
140+ $.post( '<?php echo admin_url ( 'admin-ajax.php ' ) ?> ', data, function( response ) {
141+ $( '#<?php echo $ this ->slug ?> -epsilon-review-notice' ).slideUp( 'fast', function() {
142+ $( this ).remove();
143+ } );
144+ });
145+
146+ } );
147+
148+ $('#mfbfw-epsilon-review-notice .notice-dismiss').click(function(){
149+
150+ var data = {
151+ action: 'epsilon_mfbfw_review',
152+ security: '<?php echo $ ajax_nonce ; ?> ',
153+ check: 'epsilon-later'
154+ };
155+
156+ $.post( '<?php echo admin_url ( 'admin-ajax.php ' ) ?> ', data, function( response ) {
157+ $( '#<?php echo $ this ->slug ?> -epsilon-review-notice' ).slideUp( 'fast', function() {
158+ $( this ).remove();
159+ } );
160+ });
161+ });
162+
163+ });
164+ </script>
165+
166+ <?php
167+ }
168+ }
169+
170+ new Fancybox_Review ();
0 commit comments