class-cookie-law-info-activator.php
1 month ago
class-cookie-law-info-cookieyes.php
1 month ago
class-cookie-law-info-deactivator.php
1 month ago
class-cookie-law-info-i18n.php
1 month ago
class-cookie-law-info-languages.php
1 month ago
class-cookie-law-info-loader.php
1 month ago
class-cookie-law-info-review-request.php
1 month ago
class-cookie-law-info.php
1 month ago
index.php
1 month ago
class-cookie-law-info-review-request.php
284 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Review request |
| 5 | * |
| 6 | * @package Cookie_Law_Info |
| 7 | */ |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | class Cookie_Law_Info_Review_Request { |
| 12 | |
| 13 | /** |
| 14 | * config options |
| 15 | */ |
| 16 | private $plugin_title = 'GDPR Cookie Consent (CCPA Ready)'; |
| 17 | private $review_url = 'https://wordpress.org/support/plugin/cookie-law-info/reviews/#new-post'; |
| 18 | private $plugin_prefix = 'wt_cli'; /* must be unique name */ |
| 19 | private $days_to_show_banner = 60; /* when did the banner to show */ |
| 20 | private $remind_days = 60; /* remind interval in days */ |
| 21 | |
| 22 | |
| 23 | |
| 24 | private $start_date = 0; /* banner to show count start date. plugin installed date, remind me later added date */ |
| 25 | private $current_banner_state = 2; /* 1: active, 2: waiting to show(first after installation), 3: closed by user/not interested to review, 4: user done the review, 5:remind me later */ |
| 26 | private $banner_state_option_name = ''; /* WP option name to save banner state */ |
| 27 | private $start_date_option_name = ''; /* WP option name to save start date */ |
| 28 | private $banner_css_class = ''; /* CSS class name for Banner HTML element. */ |
| 29 | private $banner_message = ''; /* Banner message. */ |
| 30 | private $later_btn_text = ''; /* Remind me later button text */ |
| 31 | private $never_btn_text = ''; /* Never review button text. */ |
| 32 | private $review_btn_text = ''; /* Review now button text. */ |
| 33 | private $ajax_action_name = ''; /* Name of ajax action to save banner state. */ |
| 34 | private $allowed_action_type_arr = array( |
| 35 | 'later', /* remind me later */ |
| 36 | 'never', /* never */ |
| 37 | 'review', /* review now */ |
| 38 | 'closed', /* not interested */ |
| 39 | ); |
| 40 | |
| 41 | public function __construct() { |
| 42 | // Set config vars |
| 43 | $this->set_vars(); |
| 44 | |
| 45 | register_activation_hook( CLI_PLUGIN_FILENAME, array( $this, 'on_activate' ) ); |
| 46 | register_deactivation_hook( CLI_PLUGIN_FILENAME, array( $this, 'on_deactivate' ) ); |
| 47 | |
| 48 | if ( $this->check_condition() ) { /* checks the banner is active now */ |
| 49 | |
| 50 | add_action( 'init', array( $this, 'init' ) ); |
| 51 | add_action( 'admin_notices', array( $this, 'show_banner' ) ); /* show banner */ |
| 52 | add_action( 'admin_print_footer_scripts', array( $this, 'add_banner_scripts' ) ); /* add banner scripts */ |
| 53 | add_action( 'wp_ajax_' . $this->ajax_action_name, array( $this, 'process_user_action' ) ); /* process banner user action */ |
| 54 | } |
| 55 | add_filter( 'admin_footer_text', array( $this, 'add_footer_review_link' ) ); |
| 56 | } |
| 57 | |
| 58 | public function init() { |
| 59 | /* translators: %1$s: opening bold tag, %2$s: closing bold tag */ |
| 60 | $this->banner_message = sprintf( __( 'Hey, we at %1$sCookieYes%2$s would like to thank you for using our plugin. We would really appreciate if you could take a moment to drop a quick review that will inspire us to keep going.', 'cookie-law-info' ), '<b>', '</b>' ); |
| 61 | |
| 62 | /* button texts */ |
| 63 | $this->later_btn_text = __( 'Remind me later', 'cookie-law-info' ); |
| 64 | $this->never_btn_text = __( 'Never show again', 'cookie-law-info' ); |
| 65 | $this->review_btn_text = __( 'Review now', 'cookie-law-info' ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Set config vars |
| 70 | */ |
| 71 | public function set_vars() { |
| 72 | $this->ajax_action_name = $this->plugin_prefix . '_process_user_review_action'; |
| 73 | $this->banner_state_option_name = $this->plugin_prefix . '_review_request'; |
| 74 | $this->start_date_option_name = $this->plugin_prefix . '_start_date'; |
| 75 | $this->banner_css_class = $this->plugin_prefix . '_review_request'; |
| 76 | |
| 77 | $this->start_date = absint( get_option( $this->start_date_option_name ) ); |
| 78 | $banner_state = absint( get_option( $this->banner_state_option_name ) ); |
| 79 | $this->current_banner_state = ( $banner_state == 0 ? $this->current_banner_state : $banner_state ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Actions on plugin activation |
| 84 | * Saves activation date |
| 85 | */ |
| 86 | public function on_activate() { |
| 87 | if ( $this->start_date == 0 ) { |
| 88 | $this->reset_start_date(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Actions on plugin deactivation |
| 94 | * Removes activation date |
| 95 | */ |
| 96 | public function on_deactivate() { |
| 97 | delete_option( $this->start_date_option_name ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Reset the start date. |
| 102 | */ |
| 103 | private function reset_start_date() { |
| 104 | update_option( $this->start_date_option_name, time() ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Update the banner state |
| 109 | */ |
| 110 | private function update_banner_state( $val ) { |
| 111 | update_option( $this->banner_state_option_name, $val ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Prints the banner |
| 116 | */ |
| 117 | public function show_banner() { |
| 118 | // Check if we are on plugin screens or WordPress plugins page |
| 119 | $screen = get_current_screen(); |
| 120 | if ( ! ( preg_match( '/cookielawinfo/' , $screen->id ) || $screen->id === 'plugins' ) ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | $this->update_banner_state( 1 ); /* update banner active state */ |
| 125 | ?> |
| 126 | <div class="<?php echo esc_attr( $this->banner_css_class ); ?> notice-info notice is-dismissible"> |
| 127 | <p> |
| 128 | <?php echo wp_kses_post( $this->banner_message ); ?> |
| 129 | </p> |
| 130 | <p> |
| 131 | <a class="button button-secondary" style="color:#333; border-color:#ccc; background:#efefef;" data-type="never"><?php echo esc_html( $this->never_btn_text ); ?></a> |
| 132 | <a class="button button-primary" data-type="review"><?php echo esc_html( $this->review_btn_text ); ?></a> |
| 133 | </p> |
| 134 | </div> |
| 135 | <?php |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Ajax hook to process user action on the banner |
| 140 | */ |
| 141 | public function process_user_action() { |
| 142 | check_ajax_referer( $this->plugin_prefix ); |
| 143 | if ( isset( $_POST['wt_review_action_type'] ) ) { |
| 144 | $action_type = sanitize_text_field( wp_unslash( $_POST['wt_review_action_type'] ) ); |
| 145 | |
| 146 | /* current action is in allowed action list */ |
| 147 | if ( in_array( $action_type, $this->allowed_action_type_arr ) ) { |
| 148 | if ( $action_type == 'never' ) { |
| 149 | $new_banner_state = 3; |
| 150 | } elseif ( $action_type == 'review' ) { |
| 151 | $new_banner_state = 1; |
| 152 | } else { |
| 153 | /* reset start date to current date */ |
| 154 | $this->reset_start_date(); |
| 155 | $new_banner_state = 5; /* remind me later */ |
| 156 | } |
| 157 | $this->update_banner_state( $new_banner_state ); |
| 158 | } |
| 159 | } |
| 160 | exit(); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Add banner JS to admin footer |
| 165 | */ |
| 166 | public function add_banner_scripts() { |
| 167 | $ajax_url = admin_url( 'admin-ajax.php' ); |
| 168 | $nonce = wp_create_nonce( $this->plugin_prefix ); |
| 169 | ?> |
| 170 | <script type="text/javascript"> |
| 171 | (function($) { |
| 172 | "use strict"; |
| 173 | |
| 174 | /* prepare data object */ |
| 175 | var data_obj = { |
| 176 | _wpnonce: '<?php echo esc_js( $nonce ); ?>', |
| 177 | action: '<?php echo esc_js( $this->ajax_action_name ); ?>', |
| 178 | wt_review_action_type: '' |
| 179 | }; |
| 180 | |
| 181 | $(document).on('click', '.<?php echo esc_js( $this->banner_css_class ); ?> a.button', function(e) { |
| 182 | e.preventDefault(); |
| 183 | var elm = $(this); |
| 184 | var btn_type = elm.attr('data-type'); |
| 185 | if (btn_type == 'review') { |
| 186 | window.open('<?php echo esc_js( $this->review_url ); ?>'); |
| 187 | } else { |
| 188 | elm.parents('.<?php echo esc_js( $this->banner_css_class ); ?>').hide(); |
| 189 | } |
| 190 | |
| 191 | data_obj['wt_review_action_type'] = btn_type; |
| 192 | $.ajax({ |
| 193 | url: '<?php echo esc_js( $ajax_url ); ?>', |
| 194 | data: data_obj, |
| 195 | type: 'POST' |
| 196 | }); |
| 197 | |
| 198 | }).on('click', '.<?php echo esc_js( $this->banner_css_class ); ?> .notice-dismiss', function(e) { |
| 199 | e.preventDefault(); |
| 200 | data_obj['wt_review_action_type'] = 'closed'; |
| 201 | $.ajax({ |
| 202 | url: '<?php echo esc_js( $ajax_url ); ?>', |
| 203 | data: data_obj, |
| 204 | type: 'POST', |
| 205 | }); |
| 206 | |
| 207 | }).on('click', '.cli-button-review', function(e) { |
| 208 | e.preventDefault(); |
| 209 | window.open('<?php echo esc_js( $this->review_url ); ?>'); |
| 210 | data_obj['wt_review_action_type'] = "review"; |
| 211 | $.ajax({ |
| 212 | url: '<?php echo esc_js( $ajax_url ); ?>', |
| 213 | data: data_obj, |
| 214 | type: 'POST' |
| 215 | }); |
| 216 | }); |
| 217 | |
| 218 | })(jQuery) |
| 219 | </script> |
| 220 | <?php |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Checks the condition to show the banner |
| 225 | */ |
| 226 | private function check_condition() { |
| 227 | |
| 228 | if ( $this->current_banner_state == 1 ) { /* currently showing then return true */ |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | if ( $this->current_banner_state == 2 || $this->current_banner_state == 5 ) { /* only waiting/remind later state */ |
| 233 | if ( $this->start_date == 0 ) { /* |
| 234 | unable to get activated date */ |
| 235 | /* set current date as activation date*/ |
| 236 | $this->reset_start_date(); |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | $days = ( $this->current_banner_state == 2 ? $this->days_to_show_banner : $this->remind_days ); |
| 241 | |
| 242 | $date_to_check = $this->start_date + ( 86400 * $days ); |
| 243 | if ( $date_to_check <= time() ) { /* time reached to show the banner */ |
| 244 | return true; |
| 245 | } else { |
| 246 | return false; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | function add_footer_review_link($footer_text) { |
| 254 | // Check if we are on the plugin page |
| 255 | $screen = get_current_screen(); |
| 256 | if ( preg_match( '/cookielawinfo/' , $screen->id ) ) { |
| 257 | $link_text = esc_html__( 'Give us a 5-star rating!', 'cookie-law-info' ); |
| 258 | $link1 = sprintf( |
| 259 | '<a class="cli-button-review" href="%2$s" title="%1$s" target="_blank">★★★★★</a>', |
| 260 | $link_text, |
| 261 | $this->review_url |
| 262 | ); |
| 263 | $link2 = sprintf( |
| 264 | '<a class="cli-button-review" href="%2$s" title="%1$s" target="_blank">WordPress.org</a>', |
| 265 | $link_text, |
| 266 | $this->review_url |
| 267 | ); |
| 268 | |
| 269 | return sprintf( |
| 270 | /* translators: %1$s: CookieYes plugin name in bold, %2$s: star rating link, %3$s: WordPress.org link */ |
| 271 | esc_html__( |
| 272 | 'Please rate %1$s %2$s on %3$s to help us spread the word. Thank you from the team CookieYes!', |
| 273 | 'cookie-law-info' |
| 274 | ), |
| 275 | sprintf( '<strong>%1$s</strong>', 'CookieYes' ), |
| 276 | wp_kses_post( $link1 ), |
| 277 | wp_kses_post( $link2 ) |
| 278 | ); |
| 279 | } |
| 280 | return $footer_text; |
| 281 | } |
| 282 | } |
| 283 | new Cookie_Law_Info_Review_Request(); |
| 284 |