Skip to content

Commit 3950eca

Browse files
committed
Add class-fancybox-review
1 parent 7362b67 commit 3950eca

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed

class-fancybox-review.php

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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 ) {
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+
});
149+
</script>
150+
151+
<?php
152+
}
153+
}
154+
155+
new Fancybox_Review();

fancybox.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
$mfbfw = get_option( 'mfbfw' );
5050
$mfbfw_version = get_option( 'mfbfw_active_version' );
5151

52+
// Include review class
53+
include 'class-fancybox-review.php';
54+
55+
5256
// If previous version detected
5357
if ( is_admin() && isset( $mfbfw_version ) && $mfbfw_version < FBFW_VERSION ) {
5458

@@ -478,13 +482,17 @@ function mfbfw_admin_options() {
478482

479483
add_action( 'admin_init', 'mfbfw_admin_options' );
480484

485+
486+
487+
481488
/**
482489
* Admin options page
483490
*/
484491
function mfbfw_admin_menu() {
485492

486493
require FBFW_PATH . 'admin.php';
487494

495+
488496
$mfbfwadmin = add_submenu_page( 'options-general.php', 'Fancybox for WordPress Options', 'Fancybox for WP', 'manage_options', 'fancybox-for-wordpress', 'mfbfw_options_page' );
489497

490498
add_action( 'admin_print_styles-' . $mfbfwadmin, 'mfbfw_admin_styles' );

0 commit comments

Comments
 (0)