| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles the functionality related to user ratings and notifications. |
| 4 |
* |
| 5 |
* @package WP_Defender\Component |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component; |
| 9 |
|
| 10 |
use Calotes\Base\Component; |
| 11 |
use WP_Defender\Model\Setting\Security_Tweaks; |
| 12 |
|
| 13 |
/** |
| 14 |
* Handles the functionality related to user ratings and notifications. |
| 15 |
* |
| 16 |
* @since 4.4.0 |
| 17 |
*/ |
| 18 |
class Rate extends Component { |
| 19 |
|
| 20 |
/** |
| 21 |
* URL for leaving a new review on WordPress.org. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
public const URL_PLUGIN_NEW_REVIEW_VCS = 'https://wordpress.org/support/plugin/defender-security/reviews/#new-post'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Number of completed scans required to trigger a rating prompt. |
| 29 |
* |
| 30 |
* @var int |
| 31 |
*/ |
| 32 |
public const NUMBER_COMPLETED_SCANS = 25; |
| 33 |
|
| 34 |
/** |
| 35 |
* Number of fixed scan issues required to trigger a rating prompt. |
| 36 |
* |
| 37 |
* @var int |
| 38 |
*/ |
| 39 |
public const NUMBER_FIXED_SCANS = 5; |
| 40 |
|
| 41 |
/** |
| 42 |
* Option name for tracking the number of completed scans. |
| 43 |
* |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
public const SLUG_COMPLETED_SCANS = 'defender_counter_completed_scans'; |
| 47 |
|
| 48 |
/** |
| 49 |
* Option name for tracking the number of fixed scan issues. |
| 50 |
* |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
public const SLUG_FIXED_SCAN_ISSUES = 'defender_counter_fixed_scan_issues'; |
| 54 |
|
| 55 |
/** |
| 56 |
* Option name for tracking if the rate button was clicked. |
| 57 |
* |
| 58 |
* @var string |
| 59 |
*/ |
| 60 |
public const SLUG_FOR_BUTTON_RATE = 'defender_rating_success'; |
| 61 |
|
| 62 |
/** |
| 63 |
* Option name for tracking if the later button was clicked. |
| 64 |
* |
| 65 |
* @var string |
| 66 |
*/ |
| 67 |
public const SLUG_FOR_BUTTON_THANKS = 'defender_days_rating_later_dismiss'; |
| 68 |
|
| 69 |
/** |
| 70 |
* Option name for storing the installation date of the plugin. |
| 71 |
* |
| 72 |
* @var string |
| 73 |
*/ |
| 74 |
public const SLUG_FREE_INSTALL_DATE = 'defender_free_install_date'; |
| 75 |
|
| 76 |
/** |
| 77 |
* Label for the rate button. |
| 78 |
* |
| 79 |
* @var string |
| 80 |
*/ |
| 81 |
public $button_rate; |
| 82 |
|
| 83 |
/** |
| 84 |
* Label for the later button. |
| 85 |
* |
| 86 |
* @var string |
| 87 |
*/ |
| 88 |
public $button_later; |
| 89 |
|
| 90 |
/** |
| 91 |
* Initializes the class by setting button labels and registering actions. |
| 92 |
*/ |
| 93 |
public function __construct() { |
| 94 |
$this->button_rate = esc_html__( 'Rate Defender', 'defender-security' ); |
| 95 |
$this->button_later = esc_html__( 'Maybe later', 'defender-security' ); |
| 96 |
add_action( 'wp_ajax_defender_dismiss_notification', array( $this, 'dismiss_notice' ) ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Retrieves the count of scans from the database. |
| 101 |
* |
| 102 |
* @param string $slug The option name to retrieve the count from. |
| 103 |
* |
| 104 |
* @return int The count of scans. |
| 105 |
*/ |
| 106 |
protected static function get_count_scans( $slug ): int { |
| 107 |
$scan_count = get_site_option( $slug, false ); |
| 108 |
|
| 109 |
return empty( $scan_count ) ? 0 : (int) $scan_count; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Count completed scans. |
| 114 |
*/ |
| 115 |
public static function run_counter_of_completed_scans(): void { |
| 116 |
$scan_count = self::get_count_scans( self::SLUG_COMPLETED_SCANS ); |
| 117 |
if ( $scan_count < self::NUMBER_COMPLETED_SCANS ) { |
| 118 |
update_site_option( self::SLUG_COMPLETED_SCANS, ++$scan_count ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Count fixed scans. |
| 124 |
*/ |
| 125 |
public static function run_counter_of_fixed_scans(): void { |
| 126 |
$scan_count = self::get_count_scans( self::SLUG_FIXED_SCAN_ISSUES ); |
| 127 |
if ( $scan_count < self::NUMBER_FIXED_SCANS ) { |
| 128 |
update_site_option( self::SLUG_FIXED_SCAN_ISSUES, ++$scan_count ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Display specific Scan notice. |
| 134 |
* |
| 135 |
* @return array |
| 136 |
*/ |
| 137 |
public static function what_scan_notice_display(): array { |
| 138 |
if ( self::get_count_scans( self::SLUG_COMPLETED_SCANS ) >= self::NUMBER_COMPLETED_SCANS ) { |
| 139 |
return array( |
| 140 |
'slug' => 'completed_scans', |
| 141 |
'text' => sprintf( |
| 142 |
/* translators: %d - Number of completed scans. */ |
| 143 |
esc_html__( |
| 144 |
'You`ve completed %d malware scans - that`s a lot of scans! We are happy to be a part of helping you secure your site and we would appreciate it if you dropped us a rating on wp.org to help us spread the word and boost our motivation.', |
| 145 |
'defender-security' |
| 146 |
), |
| 147 |
self::NUMBER_COMPLETED_SCANS |
| 148 |
), |
| 149 |
); |
| 150 |
} elseif ( self::get_count_scans( self::SLUG_FIXED_SCAN_ISSUES ) >= self::NUMBER_FIXED_SCANS ) { |
| 151 |
return array( |
| 152 |
'slug' => 'fixed_scans', |
| 153 |
'text' => sprintf( |
| 154 |
/* translators: %d - Number of completed scans. */ |
| 155 |
esc_html__( |
| 156 |
'You`ve successfully resolved %d malware scan issues! We are happy to be a part of helping you secure your site, and we would appreciate it if you dropped us a rating on wp.org to help us spread the word and boost our motivation.', |
| 157 |
'defender-security' |
| 158 |
), |
| 159 |
self::NUMBER_FIXED_SCANS |
| 160 |
), |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
return array( |
| 165 |
'slug' => '', |
| 166 |
'text' => '', |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Remove data of all prompts. |
| 172 |
*/ |
| 173 |
public static function clean_up(): void { |
| 174 |
delete_site_option( self::SLUG_COMPLETED_SCANS ); |
| 175 |
delete_site_option( self::SLUG_FIXED_SCAN_ISSUES ); |
| 176 |
delete_site_option( self::SLUG_FREE_INSTALL_DATE ); |
| 177 |
delete_site_option( self::SLUG_FOR_BUTTON_RATE ); |
| 178 |
delete_site_option( self::SLUG_FOR_BUTTON_THANKS ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Reset counter of all prompts. |
| 183 |
*/ |
| 184 |
public static function reset_counters(): void { |
| 185 |
update_site_option( self::SLUG_COMPLETED_SCANS, 0 ); |
| 186 |
update_site_option( self::SLUG_FIXED_SCAN_ISSUES, 0 ); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Initializing actions. |
| 191 |
*/ |
| 192 |
public function init(): void { |
| 193 |
if ( is_defender_page() ) { |
| 194 |
$is_multisite = is_multisite(); |
| 195 |
if ( ! $is_multisite ) { |
| 196 |
add_action( 'admin_notices', array( $this, 'show_notice_general_request' ) ); |
| 197 |
} elseif ( $is_multisite && is_main_site() ) { |
| 198 |
add_action( 'network_admin_notices', array( $this, 'show_notice_general_request' ) ); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Have there already been clicks on the Rate notices? |
| 205 |
* |
| 206 |
* @return bool |
| 207 |
*/ |
| 208 |
public static function was_rate_request(): bool { |
| 209 |
if ( get_site_option( self::SLUG_FOR_BUTTON_RATE, apply_filters( 'wd_display_rating', false ) ) ) { |
| 210 |
return true; |
| 211 |
} |
| 212 |
if ( get_site_option( self::SLUG_FOR_BUTTON_THANKS, apply_filters( 'wd_dismiss_rating', false ) ) ) { |
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Show the general rating notice after countless hours developing. |
| 221 |
*/ |
| 222 |
public function show_notice_general_request() { |
| 223 |
if ( self::was_rate_request() ) { |
| 224 |
return; |
| 225 |
} |
| 226 |
// Return if the condition is met on the Scan page. |
| 227 |
if ( |
| 228 |
'wdf-scan' === defender_get_current_page() |
| 229 |
&& ( |
| 230 |
self::get_count_scans( self::SLUG_COMPLETED_SCANS ) >= self::NUMBER_COMPLETED_SCANS |
| 231 |
|| self::get_count_scans( self::SLUG_FIXED_SCAN_ISSUES ) >= self::NUMBER_FIXED_SCANS |
| 232 |
) |
| 233 |
) { |
| 234 |
return; |
| 235 |
} |
| 236 |
// Return if the condition is met on the Tweaks page. |
| 237 |
$tweak_arr = wd_di()->get( Security_Tweaks::class )->get_tweak_types(); |
| 238 |
$total_tweaks = $tweak_arr['count_fixed'] + $tweak_arr['count_ignored'] + $tweak_arr['count_issues']; |
| 239 |
if ( |
| 240 |
'wdf-hardener' === defender_get_current_page() |
| 241 |
&& $tweak_arr['count_fixed'] === $total_tweaks |
| 242 |
) { |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
// Check by the installation date. |
| 247 |
$install_date = (int) get_site_option( self::SLUG_FREE_INSTALL_DATE, false ); |
| 248 |
if ( $install_date && time() > strtotime( '+7 days', $install_date ) ) { ?> |
| 249 |
<div id="defender-free-usage-notice" class="defender-rating-notice notice notice-info" |
| 250 |
data-nonce="<?php echo esc_attr( wp_create_nonce( 'defender_dismiss_notification' ) ); ?>"> |
| 251 |
|
| 252 |
<p style="color: #72777C; line-height: 22px;"> |
| 253 |
<?php |
| 254 |
esc_html_e( |
| 255 |
'We\'ve spent countless hours developing Defender and making it free for you to use. We would really appreciate it if you dropped us a quick rating!', |
| 256 |
'defender-security' |
| 257 |
); |
| 258 |
?> |
| 259 |
</p> |
| 260 |
|
| 261 |
<p> |
| 262 |
<button type="button" class="button button-primary button-large" |
| 263 |
data-prop="defender_rating_success"><?php echo esc_html( $this->button_rate ); ?></button> |
| 264 |
<a href="#" class="dismiss" |
| 265 |
style="margin-left: 11px; color: #555; line-height: 16px; font-weight: 500; text-decoration: none;" |
| 266 |
data-prop="defender_days_rating_later_dismiss"><?php echo esc_html( $this->button_later ); ?></a> |
| 267 |
</p> |
| 268 |
</div> |
| 269 |
<?php |
| 270 |
} |
| 271 |
?> |
| 272 |
|
| 273 |
<script type="text/javascript"> |
| 274 |
jQuery('.defender-rating-notice a, .defender-rating-notice button').on('click', function (e) { |
| 275 |
e.preventDefault(); |
| 276 |
|
| 277 |
var $notice = jQuery(e.currentTarget).closest('.defender-rating-notice'), |
| 278 |
prop = jQuery(this).data('prop'), |
| 279 |
ajaxUrl = '<?php echo esc_url_raw( admin_url( 'admin-ajax.php' ) ); ?>'; |
| 280 |
|
| 281 |
if ('defender_rating_success' === prop) { |
| 282 |
window.open('<?php echo esc_url_raw( self::URL_PLUGIN_NEW_REVIEW_VCS ); ?>', '_blank'); |
| 283 |
} |
| 284 |
|
| 285 |
jQuery.post( |
| 286 |
ajaxUrl, |
| 287 |
{ |
| 288 |
action: 'defender_dismiss_notification', |
| 289 |
prop: prop, |
| 290 |
_ajax_nonce: $notice.data('nonce') |
| 291 |
} |
| 292 |
).always(function () { |
| 293 |
$notice.hide(); |
| 294 |
}); |
| 295 |
}); |
| 296 |
</script> |
| 297 |
<?php |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Dismiss notice. |
| 302 |
*/ |
| 303 |
public function dismiss_notice() { |
| 304 |
if ( ! current_user_can( 'manage_options' ) || ! check_ajax_referer( 'defender_dismiss_notification' ) ) { |
| 305 |
wp_send_json_error( |
| 306 |
array( 'message' => esc_html__( 'Invalid request, you are not allowed to do that action.', 'defender-security' ) ) |
| 307 |
); |
| 308 |
} |
| 309 |
|
| 310 |
$notification_name = defender_get_data_from_request( 'prop', 'p' ); |
| 311 |
if ( false === $notification_name ) { |
| 312 |
wp_send_json_error( |
| 313 |
array( 'message' => esc_html__( 'Invalid request, allowed data not provided.', 'defender-security' ) ) |
| 314 |
); |
| 315 |
} |
| 316 |
update_site_option( $notification_name, true ); |
| 317 |
|
| 318 |
wp_send_json_success(); |
| 319 |
} |
| 320 |
} |
| 321 |
|