class-cookie-law-info-activator.php
2 years ago
class-cookie-law-info-cookieyes.php
2 years ago
class-cookie-law-info-deactivator.php
2 years ago
class-cookie-law-info-i18n.php
2 years ago
class-cookie-law-info-languages.php
2 years ago
class-cookie-law-info-loader.php
2 years ago
class-cookie-law-info-review-request.php
2 years ago
class-cookie-law-info.php
2 years ago
index.php
2 years ago
class-cookie-law-info-review-request.php
245 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 = 15; /* when did the banner to show */ |
| 20 | private $remind_days = 15; /* remind interval in days */ |
| 21 | private $webtoffee_logo_url = ''; |
| 22 | |
| 23 | |
| 24 | |
| 25 | private $start_date = 0; /* banner to show count start date. plugin installed date, remind me later added date */ |
| 26 | 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 */ |
| 27 | private $banner_state_option_name = ''; /* WP option name to save banner state */ |
| 28 | private $start_date_option_name = ''; /* WP option name to save start date */ |
| 29 | private $banner_css_class = ''; /* CSS class name for Banner HTML element. */ |
| 30 | private $banner_message = ''; /* Banner message. */ |
| 31 | private $later_btn_text = ''; /* Remind me later button text */ |
| 32 | private $never_btn_text = ''; /* Never review button text. */ |
| 33 | private $review_btn_text = ''; /* Review now button text. */ |
| 34 | private $ajax_action_name = ''; /* Name of ajax action to save banner state. */ |
| 35 | private $allowed_action_type_arr = array( |
| 36 | 'later', /* remind me later */ |
| 37 | 'never', /* never */ |
| 38 | 'review', /* review now */ |
| 39 | 'closed', /* not interested */ |
| 40 | ); |
| 41 | |
| 42 | public function __construct() { |
| 43 | // Set config vars |
| 44 | $this->set_vars(); |
| 45 | |
| 46 | register_activation_hook( CLI_PLUGIN_FILENAME, array( $this, 'on_activate' ) ); |
| 47 | register_deactivation_hook( CLI_PLUGIN_FILENAME, array( $this, 'on_deactivate' ) ); |
| 48 | |
| 49 | if ( $this->check_condition() ) { /* checks the banner is active now */ |
| 50 | $this->banner_message = sprintf( __( 'Hey, we at %1$sWebToffee%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>' ); |
| 51 | |
| 52 | /* button texts */ |
| 53 | $this->later_btn_text = __( 'Remind me later', 'cookie-law-info' ); |
| 54 | $this->never_btn_text = __( 'Not interested', 'cookie-law-info' ); |
| 55 | $this->review_btn_text = __( 'Review now', 'cookie-law-info' ); |
| 56 | |
| 57 | add_action( 'admin_notices', array( $this, 'show_banner' ) ); /* show banner */ |
| 58 | add_action( 'admin_print_footer_scripts', array( $this, 'add_banner_scripts' ) ); /* add banner scripts */ |
| 59 | add_action( 'wp_ajax_' . $this->ajax_action_name, array( $this, 'process_user_action' ) ); /* process banner user action */ |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Set config vars |
| 65 | */ |
| 66 | public function set_vars() { |
| 67 | $this->ajax_action_name = $this->plugin_prefix . '_process_user_review_action'; |
| 68 | $this->banner_state_option_name = $this->plugin_prefix . '_review_request'; |
| 69 | $this->start_date_option_name = $this->plugin_prefix . '_start_date'; |
| 70 | $this->banner_css_class = $this->plugin_prefix . '_review_request'; |
| 71 | |
| 72 | $this->start_date = absint( get_option( $this->start_date_option_name ) ); |
| 73 | $banner_state = absint( get_option( $this->banner_state_option_name ) ); |
| 74 | $this->current_banner_state = ( $banner_state == 0 ? $this->current_banner_state : $banner_state ); |
| 75 | $this->webtoffee_logo_url = CLI_PLUGIN_URL . 'images/webtoffee-logo_small.png'; |
| 76 | |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Actions on plugin activation |
| 81 | * Saves activation date |
| 82 | */ |
| 83 | public function on_activate() { |
| 84 | if ( $this->start_date == 0 ) { |
| 85 | $this->reset_start_date(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Actions on plugin deactivation |
| 91 | * Removes activation date |
| 92 | */ |
| 93 | public function on_deactivate() { |
| 94 | delete_option( $this->start_date_option_name ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Reset the start date. |
| 99 | */ |
| 100 | private function reset_start_date() { |
| 101 | update_option( $this->start_date_option_name, time() ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Update the banner state |
| 106 | */ |
| 107 | private function update_banner_state( $val ) { |
| 108 | update_option( $this->banner_state_option_name, $val ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Prints the banner |
| 113 | */ |
| 114 | public function show_banner() { |
| 115 | $this->update_banner_state( 1 ); /* update banner active state */ |
| 116 | ?> |
| 117 | <div class="<?php echo esc_attr( $this->banner_css_class ); ?> notice-info notice is-dismissible"> |
| 118 | <?php |
| 119 | if ( $this->webtoffee_logo_url != '' ) { |
| 120 | ?> |
| 121 | <h3 style="margin: 10px 0;"><?php echo esc_html( $this->plugin_title ); ?></h3> |
| 122 | <?php |
| 123 | } |
| 124 | ?> |
| 125 | <p> |
| 126 | <?php echo wp_kses_post( $this->banner_message ); ?> |
| 127 | </p> |
| 128 | <p> |
| 129 | <a class="button button-secondary" style="color:#333; border-color:#ccc; background:#efefef;" data-type="later"><?php echo esc_html( $this->later_btn_text ); ?></a> |
| 130 | <a class="button button-primary" data-type="review"><?php echo esc_html( $this->review_btn_text ); ?></a> |
| 131 | </p> |
| 132 | <div class="wt-cli-review-footer" style="position: relative;"> |
| 133 | <span class="wt-cli-footer-icon" style="position: absolute;right: 0;bottom: 10px;"><img src="<?php echo esc_url( $this->webtoffee_logo_url ); ?>" style="max-width:100px;"></span> |
| 134 | </div> |
| 135 | </div> |
| 136 | <?php |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Ajax hook to process user action on the banner |
| 141 | */ |
| 142 | public function process_user_action() { |
| 143 | check_ajax_referer( $this->plugin_prefix ); |
| 144 | if ( isset( $_POST['wt_review_action_type'] ) ) { |
| 145 | $action_type = sanitize_text_field( wp_unslash( $_POST['wt_review_action_type'] ) ); |
| 146 | |
| 147 | /* current action is in allowed action list */ |
| 148 | if ( in_array( $action_type, $this->allowed_action_type_arr ) ) { |
| 149 | if ( $action_type == 'never' || $action_type == 'closed' ) { |
| 150 | $new_banner_state = 3; |
| 151 | } elseif ( $action_type == 'review' ) { |
| 152 | $new_banner_state = 4; |
| 153 | } else { |
| 154 | /* reset start date to current date */ |
| 155 | $this->reset_start_date(); |
| 156 | $new_banner_state = 5; /* remind me later */ |
| 157 | } |
| 158 | $this->update_banner_state( $new_banner_state ); |
| 159 | } |
| 160 | } |
| 161 | exit(); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Add banner JS to admin footer |
| 166 | */ |
| 167 | public function add_banner_scripts() { |
| 168 | $ajax_url = admin_url( 'admin-ajax.php' ); |
| 169 | $nonce = wp_create_nonce( $this->plugin_prefix ); |
| 170 | ?> |
| 171 | <script type="text/javascript"> |
| 172 | (function($) { |
| 173 | "use strict"; |
| 174 | |
| 175 | /* prepare data object */ |
| 176 | var data_obj = { |
| 177 | _wpnonce: '<?php echo esc_js( $nonce ); ?>', |
| 178 | action: '<?php echo esc_js( $this->ajax_action_name ); ?>', |
| 179 | wt_review_action_type: '' |
| 180 | }; |
| 181 | |
| 182 | $(document).on('click', '.<?php echo esc_js( $this->banner_css_class ); ?> a.button', function(e) { |
| 183 | e.preventDefault(); |
| 184 | var elm = $(this); |
| 185 | var btn_type = elm.attr('data-type'); |
| 186 | if (btn_type == 'review') { |
| 187 | window.open('<?php echo esc_js( $this->review_url ); ?>'); |
| 188 | } |
| 189 | elm.parents('.<?php echo esc_js( $this->banner_css_class ); ?>').hide(); |
| 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 | }); |
| 208 | |
| 209 | })(jQuery) |
| 210 | </script> |
| 211 | <?php |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Checks the condition to show the banner |
| 216 | */ |
| 217 | private function check_condition() { |
| 218 | |
| 219 | if ( $this->current_banner_state == 1 ) { /* currently showing then return true */ |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | if ( $this->current_banner_state == 2 || $this->current_banner_state == 5 ) { /* only waiting/remind later state */ |
| 224 | if ( $this->start_date == 0 ) { /* |
| 225 | unable to get activated date */ |
| 226 | /* set current date as activation date*/ |
| 227 | $this->reset_start_date(); |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | $days = ( $this->current_banner_state == 2 ? $this->days_to_show_banner : $this->remind_days ); |
| 232 | |
| 233 | $date_to_check = $this->start_date + ( 86400 * $days ); |
| 234 | if ( $date_to_check <= time() ) { /* time reached to show the banner */ |
| 235 | return true; |
| 236 | } else { |
| 237 | return false; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return false; |
| 242 | } |
| 243 | } |
| 244 | new Cookie_Law_Info_Review_Request(); |
| 245 |