| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Controller; |
| 4 |
|
| 5 |
use Calotes\Component\Request; |
| 6 |
use Calotes\Component\Response; |
| 7 |
use Calotes\Helper\HTTP; |
| 8 |
use WP_Defender\Component\Config\Config_Hub_Helper; |
| 9 |
use WP_Defender\Controller; |
| 10 |
use WP_Defender\Traits\Formats; |
| 11 |
use WP_Defender\Traits\User; |
| 12 |
use WP_Defender\Model\Notification as Model_Notification; |
| 13 |
|
| 14 |
class Notification extends Controller { |
| 15 |
use User, Formats; |
| 16 |
|
| 17 |
public $slug = 'wdf-notification'; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var \WP_Defender\Component\Notification |
| 21 |
*/ |
| 22 |
protected $service; |
| 23 |
|
| 24 |
public function __construct() { |
| 25 |
$this->register_page( |
| 26 |
esc_html__( 'Notifications', 'wpdef' ), |
| 27 |
$this->slug, |
| 28 |
array( |
| 29 |
&$this, |
| 30 |
'main_view', |
| 31 |
), |
| 32 |
$this->parent_slug |
| 33 |
); |
| 34 |
$this->register_routes(); |
| 35 |
$this->service = wd_di()->get( \WP_Defender\Component\Notification::class ); |
| 36 |
add_action( 'defender_enqueue_assets', array( &$this, 'enqueue_assets' ) ); |
| 37 |
// We use custom ajax endpoint here as the nonce would fail with other user. |
| 38 |
add_action( 'wp_ajax_defender_listen_user_subscribe', array( &$this, 'verify_subscriber' ) ); |
| 39 |
add_action( 'wp_ajax_nopriv_defender_listen_user_subscribe', array( &$this, 'verify_subscriber' ) ); |
| 40 |
add_action( 'wp_ajax_defender_listen_user_unsubscribe', array( &$this, 'unsubscribe_and_send_email' ) ); |
| 41 |
add_action( 'wp_ajax_nopriv_defender_listen_user_unsubscribe', array( &$this, 'unsubscribe_and_send_email' ) ); |
| 42 |
add_action( 'defender_notify', array( &$this, 'send_notify' ), 10, 2 ); |
| 43 |
// We will schedule the time to send reports. |
| 44 |
if ( ! wp_next_scheduled( 'wdf_maybe_send_report' ) ) { |
| 45 |
$timestamp = gmmktime( gmdate( 'H' ), 0, 0 ); |
| 46 |
wp_schedule_event( $timestamp, 'thirty_minutes', 'wdf_maybe_send_report' ); |
| 47 |
} |
| 48 |
add_action( 'wdf_maybe_send_report', array( &$this, 'report_sender' ) ); |
| 49 |
add_action( 'admin_notices', array( &$this, 'show_actions_with_subscription' ) ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* For users who have subscribed or unsubscribed confirmation. |
| 54 |
* |
| 55 |
* @return null |
| 56 |
*/ |
| 57 |
public function show_actions_with_subscription() { |
| 58 |
if ( ! defined( 'IS_PROFILE_PAGE' ) || false === constant( 'IS_PROFILE_PAGE' ) ) { |
| 59 |
return null; |
| 60 |
} |
| 61 |
$slug = isset( $_GET['slug'] ) ? sanitize_text_field( $_GET['slug'] ) : false; |
| 62 |
if ( empty( $slug ) ) { |
| 63 |
return null; |
| 64 |
} |
| 65 |
$m = $this->service->find_module_by_slug( $slug ); |
| 66 |
if ( ! is_object( $m ) ) { |
| 67 |
return null; |
| 68 |
} |
| 69 |
$context = isset( $_GET['context'] ) ? sanitize_text_field( $_GET['context'] ) : false; |
| 70 |
if ( 'subscribed' === $context ) { |
| 71 |
$unsubscribe_link = $this->service->create_unsubscribe_url( $m->slug, $this->get_current_user_email() ); |
| 72 |
$strings = sprintf( |
| 73 |
/* translators: %s - module title, %s - unsubscribed link */ |
| 74 |
__( 'You are now subscribed to receive <strong>%1$s</strong>. Made a mistake? <a href="%2$s">Unsubscribe</a>', 'wpdef' ), |
| 75 |
$m->title, |
| 76 |
$unsubscribe_link |
| 77 |
); |
| 78 |
} elseif ( 'unsubscribe' === $context ) { |
| 79 |
$strings = sprintf( |
| 80 |
/* translators: %s - module title */ |
| 81 |
__( 'You are now unsubscribed from <strong>%s</strong>.', 'wpdef' ), |
| 82 |
$m->title |
| 83 |
); |
| 84 |
} else { |
| 85 |
return null; |
| 86 |
} |
| 87 |
?> |
| 88 |
<div class="notice notice-success" style="position:relative;"> |
| 89 |
<p><?php echo $strings; ?></p> |
| 90 |
<a href="<?php echo get_edit_profile_url(); ?>" class="notice-dismiss" style="text-decoration: none"> |
| 91 |
<span class="screen-reader-text"><?php _e( 'Dismiss this notice.', 'wpdef' ); ?></span> |
| 92 |
</a> |
| 93 |
</div> |
| 94 |
<?php |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Trigger report check signals. |
| 99 |
*/ |
| 100 |
public function report_sender() { |
| 101 |
$this->service->maybe_dispatch_report(); |
| 102 |
} |
| 103 |
|
| 104 |
public function main_view() { |
| 105 |
$this->render( 'main' ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Dispatch notification. |
| 110 |
* |
| 111 |
* @param string $slug |
| 112 |
* @param object $args |
| 113 |
*/ |
| 114 |
public function send_notify( $slug, $args ) { |
| 115 |
$this->service->dispatch_notification( $slug, $args ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @param Request $request |
| 120 |
* |
| 121 |
* @defender_route |
| 122 |
* @return Response |
| 123 |
*/ |
| 124 |
public function validate_email( Request $request ) { |
| 125 |
$data = $request->get_data( |
| 126 |
array( |
| 127 |
'email' => array( |
| 128 |
'type' => 'string', |
| 129 |
'sanitize' => 'sanitize_text_field', |
| 130 |
), |
| 131 |
) |
| 132 |
); |
| 133 |
$email = $data['email'] ?? false; |
| 134 |
if ( filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { |
| 135 |
return new Response( |
| 136 |
true, |
| 137 |
array( |
| 138 |
'error' => false, |
| 139 |
'avatar' => get_avatar_url( $data['email'] ), |
| 140 |
) |
| 141 |
); |
| 142 |
} else { |
| 143 |
return new Response( |
| 144 |
false, |
| 145 |
array( |
| 146 |
'error' => __( 'Invalid email address.', 'wpdef' ), |
| 147 |
) |
| 148 |
); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Unsubscribe process. |
| 154 |
*/ |
| 155 |
public function unsubscribe_and_send_email() { |
| 156 |
$slug = HTTP::get( 'slug', '' ); |
| 157 |
$hash = HTTP::get( 'hash', '' ); |
| 158 |
$slug = sanitize_text_field( $slug ); |
| 159 |
if ( empty( $slug ) || empty( $hash ) ) { |
| 160 |
wp_die( __( 'You shall not pass.', 'wpdef' ) ); |
| 161 |
} |
| 162 |
$m = $this->service->find_module_by_slug( $slug ); |
| 163 |
if ( ! is_object( $m ) ) { |
| 164 |
wp_die( __( 'You shall not pass.', 'wpdef' ) ); |
| 165 |
} |
| 166 |
$inhouse = false; |
| 167 |
foreach ( $m->in_house_recipients as &$recipient ) { |
| 168 |
$email = $recipient['email']; |
| 169 |
if ( hash_equals( $hash, hash( 'sha256', $email . AUTH_SALT ) ) ) { |
| 170 |
if ( ! is_user_logged_in() ) { |
| 171 |
auth_redirect(); |
| 172 |
} |
| 173 |
if ( $email !== $this->get_current_user_email() ) { |
| 174 |
wp_die( __( 'Invalid request.', 'wpdef' ) ); |
| 175 |
} |
| 176 |
$recipient['status'] = Model_Notification::USER_SUBSCRIBE_CANCELED; |
| 177 |
$m->save(); |
| 178 |
$inhouse = true; |
| 179 |
// Send email. |
| 180 |
$this->service->send_unsubscribe_email( $m, $email, $inhouse, $recipient['name'] ); |
| 181 |
break; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
if ( false === $inhouse ) { |
| 186 |
// No match on in-house, check the outhouse list. |
| 187 |
foreach ( $m->out_house_recipients as &$recipient ) { |
| 188 |
$email = $recipient['email']; |
| 189 |
if ( hash_equals( $hash, hash( 'sha256', $email . AUTH_SALT ) ) ) { |
| 190 |
$recipient['status'] = Model_Notification::USER_SUBSCRIBE_CANCELED; |
| 191 |
$m->save(); |
| 192 |
$this->service->send_unsubscribe_email( $m, $email, $inhouse, $recipient['name'] ); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
if ( $inhouse ) { |
| 197 |
wp_redirect( |
| 198 |
add_query_arg( |
| 199 |
array( |
| 200 |
'slug' => $slug, |
| 201 |
'context' => 'unsubscribe', |
| 202 |
), |
| 203 |
get_edit_profile_url() |
| 204 |
) |
| 205 |
); |
| 206 |
} else { |
| 207 |
wp_redirect( get_home_url() ); |
| 208 |
} |
| 209 |
exit; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* An endpoint for saving single config from frontend. |
| 214 |
* |
| 215 |
* @param Request $request |
| 216 |
* |
| 217 |
* @defender_route |
| 218 |
* @return Response |
| 219 |
*/ |
| 220 |
public function save( Request $request ) { |
| 221 |
$raw_data = $request->get_data(); |
| 222 |
$slug = sanitize_textarea_field( $raw_data['slug'] ); |
| 223 |
$model = $this->service->find_module_by_slug( $slug ); |
| 224 |
|
| 225 |
if ( ! is_object( $model ) ) { |
| 226 |
// Should never be here. |
| 227 |
die; |
| 228 |
} |
| 229 |
$data = $request->get_data_by_model( $model ); |
| 230 |
// Check config-values. |
| 231 |
$data['configs'] = $model->type_casting( $data['configs'] ); |
| 232 |
|
| 233 |
$model->import( $data ); |
| 234 |
$model->status = Model_Notification::STATUS_ACTIVE; |
| 235 |
if ( $model->validate() ) { |
| 236 |
if ( 0 === $model->last_sent ) { |
| 237 |
// This means that the notification or report never sent, we will use the moment that it get activate. |
| 238 |
$model->last_sent = time(); |
| 239 |
} |
| 240 |
$model->save(); |
| 241 |
$this->service->send_subscription_confirm_email( $model ); |
| 242 |
Config_Hub_Helper::set_clear_active_flag(); |
| 243 |
|
| 244 |
return new Response( |
| 245 |
true, |
| 246 |
array_merge( |
| 247 |
array( |
| 248 |
'message' => __( |
| 249 |
'You have activated the notification successfully. Note, recipients will need to confirm their subscriptions to begin receiving notifications.', |
| 250 |
'wpdef' |
| 251 |
), |
| 252 |
), |
| 253 |
$this->data_frontend() |
| 254 |
) |
| 255 |
); |
| 256 |
} |
| 257 |
|
| 258 |
return new Response( |
| 259 |
false, |
| 260 |
array( |
| 261 |
'message' => $model->get_formatted_errors(), |
| 262 |
) |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Bulk update and save changes. |
| 268 |
* |
| 269 |
* @param Request $request |
| 270 |
* |
| 271 |
* @defender_route |
| 272 |
* @return Response |
| 273 |
*/ |
| 274 |
public function save_bulk( Request $request ) { |
| 275 |
$data = $request->get_data( |
| 276 |
array( |
| 277 |
'reports' => array( |
| 278 |
'type' => 'array', |
| 279 |
'sanitize' => 'sanitize_textarea_field', |
| 280 |
), |
| 281 |
'notifications' => array( |
| 282 |
'type' => 'array', |
| 283 |
'sanitize' => 'sanitize_textarea_field', |
| 284 |
), |
| 285 |
) |
| 286 |
); |
| 287 |
$this->save_reports( $data['reports'] ); |
| 288 |
$this->save_notifications( $data['notifications'] ); |
| 289 |
Config_Hub_Helper::set_clear_active_flag(); |
| 290 |
|
| 291 |
return new Response( |
| 292 |
true, |
| 293 |
array_merge( |
| 294 |
$this->data_frontend(), |
| 295 |
array( |
| 296 |
'message' => __( |
| 297 |
'Your settings have been updated successfully. Any new recipients will receive an email to confirm their subscription.', |
| 298 |
'wpdef' |
| 299 |
), |
| 300 |
) |
| 301 |
) |
| 302 |
); |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Process bulk reports saving. |
| 307 |
* |
| 308 |
* @param array $data |
| 309 |
*/ |
| 310 |
private function save_reports( $data ) { |
| 311 |
foreach ( $data['configs'] as $datum ) { |
| 312 |
$slug = $datum['slug']; |
| 313 |
$model = $this->service->find_module_by_slug( $slug ); |
| 314 |
if ( ! is_object( $model ) ) { |
| 315 |
continue; |
| 316 |
} |
| 317 |
|
| 318 |
$import = array( |
| 319 |
// Saving after Bulk-Update must always change the status to Active. |
| 320 |
'status' => Model_Notification::STATUS_ACTIVE, |
| 321 |
'configs' => $model->type_casting( $datum ), |
| 322 |
'in_house_recipients' => $data['in_house_recipients'], |
| 323 |
'out_house_recipients' => $data['out_house_recipients'], |
| 324 |
); |
| 325 |
// @since 2.7.0. |
| 326 |
if ( 'malware-report' !== $slug ) { |
| 327 |
$import['frequency'] = $data['frequency']; |
| 328 |
$import['day_n'] = $data['day_n']; |
| 329 |
$import['day'] = $data['day']; |
| 330 |
$import['time'] = $data['time']; |
| 331 |
} |
| 332 |
foreach ( $import['out_house_recipients'] as $key => $val ) { |
| 333 |
if ( ! filter_var( $val['email'], FILTER_VALIDATE_EMAIL ) ) { |
| 334 |
unset( $import['out_house_recipients'][ $key ] ); |
| 335 |
} |
| 336 |
} |
| 337 |
$model->import( $import ); |
| 338 |
if ( $model->validate() ) { |
| 339 |
if ( 0 === $model->last_sent ) { |
| 340 |
$model->last_sent = time(); |
| 341 |
} |
| 342 |
$model->save(); |
| 343 |
$this->service->send_subscription_confirm_email( $model ); |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* @param array $data |
| 350 |
* |
| 351 |
* @throws \DI\DependencyException |
| 352 |
* @throws \DI\NotFoundException |
| 353 |
* @throws \ReflectionException |
| 354 |
*/ |
| 355 |
private function save_notifications( $data ) { |
| 356 |
foreach ( $data['configs'] as $datum ) { |
| 357 |
$slug = $datum['slug']; |
| 358 |
$model = $this->service->find_module_by_slug( $slug ); |
| 359 |
if ( ! is_object( $model ) ) { |
| 360 |
continue; |
| 361 |
} |
| 362 |
$import = array( |
| 363 |
// Saving after Bulk-Update must always change the status to Active. |
| 364 |
'status' => Model_Notification::STATUS_ACTIVE, |
| 365 |
'configs' => $model->type_casting( $datum ), |
| 366 |
'in_house_recipients' => $data['in_house_recipients'], |
| 367 |
'out_house_recipients' => $data['out_house_recipients'], |
| 368 |
); |
| 369 |
foreach ( $import['out_house_recipients'] as $key => $val ) { |
| 370 |
if ( ! filter_var( $val['email'], FILTER_VALIDATE_EMAIL ) ) { |
| 371 |
unset( $import['out_house_recipients'][ $key ] ); |
| 372 |
} |
| 373 |
} |
| 374 |
$model->import( $import ); |
| 375 |
if ( $model->validate() ) { |
| 376 |
if ( 0 === $model->last_sent ) { |
| 377 |
$model->last_sent = time(); |
| 378 |
} |
| 379 |
$model->save(); |
| 380 |
$this->service->send_subscription_confirm_email( $model ); |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Bulk activate. |
| 387 |
* |
| 388 |
* @param Request $request |
| 389 |
* |
| 390 |
* @defender_route |
| 391 |
* @return Response |
| 392 |
* @throws \Exception |
| 393 |
*/ |
| 394 |
public function bulk_activate( Request $request ) { |
| 395 |
$data = $request->get_data( |
| 396 |
array( |
| 397 |
'slugs' => array( |
| 398 |
'type' => 'array', |
| 399 |
'sanitize' => 'sanitize_text_field', |
| 400 |
), |
| 401 |
) |
| 402 |
); |
| 403 |
$slugs = $data['slugs']; |
| 404 |
if ( empty( $slugs ) ) { |
| 405 |
return new Response( false, array() ); |
| 406 |
} |
| 407 |
|
| 408 |
foreach ( $slugs as $slug ) { |
| 409 |
$model = $this->service->find_module_by_slug( $slug ); |
| 410 |
if ( is_object( $model ) ) { |
| 411 |
$model->status = Model_Notification::STATUS_ACTIVE; |
| 412 |
if ( 0 === $model->last_sent ) { |
| 413 |
// This means that the notification or report never sent, we will use the moment that it get activate. |
| 414 |
$model->last_sent = time(); |
| 415 |
} |
| 416 |
$model->save(); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
return new Response( |
| 421 |
true, |
| 422 |
array_merge( |
| 423 |
array( |
| 424 |
'message' => 'You have activated the notification successfully. Note, recipients will need to confirm their subscriptions to begin receiving notifications.', |
| 425 |
), |
| 426 |
$this->data_frontend() |
| 427 |
) |
| 428 |
); |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* @param Request $request |
| 433 |
* |
| 434 |
* @defender_route |
| 435 |
* @return Response |
| 436 |
*/ |
| 437 |
public function bulk_deactivate( Request $request ) { |
| 438 |
$data = $request->get_data( |
| 439 |
array( |
| 440 |
'slugs' => array( |
| 441 |
'type' => 'array', |
| 442 |
'sanitize' => 'sanitize_text_field', |
| 443 |
), |
| 444 |
) |
| 445 |
); |
| 446 |
$slugs = $data['slugs']; |
| 447 |
if ( empty( $slugs ) ) { |
| 448 |
return new Response( false, array() ); |
| 449 |
} |
| 450 |
|
| 451 |
foreach ( $slugs as $slug ) { |
| 452 |
$model = $this->service->find_module_by_slug( $slug ); |
| 453 |
if ( is_object( $model ) ) { |
| 454 |
$model->status = Model_Notification::STATUS_DISABLED; |
| 455 |
$model->save(); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
return new Response( |
| 460 |
true, |
| 461 |
array_merge( |
| 462 |
array( |
| 463 |
'message' => __( 'You have deactivated the notifications successfully.', 'wpdef' ), |
| 464 |
), |
| 465 |
$this->data_frontend() |
| 466 |
) |
| 467 |
); |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* @param Request $request |
| 472 |
* |
| 473 |
* @defender_route |
| 474 |
* @return Response |
| 475 |
*/ |
| 476 |
public function disable( Request $request ) { |
| 477 |
$data = $request->get_data( |
| 478 |
array( |
| 479 |
'slug' => array( |
| 480 |
'type' => 'string', |
| 481 |
'sanitize' => 'sanitize_text_field', |
| 482 |
), |
| 483 |
) |
| 484 |
); |
| 485 |
|
| 486 |
$slug = $data['slug']; |
| 487 |
$model = $this->service->find_module_by_slug( $slug ); |
| 488 |
if ( ! is_object( $model ) ) { |
| 489 |
// Should never be here. |
| 490 |
die; |
| 491 |
} |
| 492 |
$model->status = Model_Notification::STATUS_DISABLED; |
| 493 |
$model->save(); |
| 494 |
|
| 495 |
return new Response( |
| 496 |
true, |
| 497 |
array_merge( |
| 498 |
$this->data_frontend(), |
| 499 |
array( |
| 500 |
'message' => __( 'You have deactivated the notification successfully.', 'wpdef' ), |
| 501 |
) |
| 502 |
) |
| 503 |
); |
| 504 |
} |
| 505 |
|
| 506 |
/** |
| 507 |
* This is a receiver to process subscribe confirmation from email. |
| 508 |
*/ |
| 509 |
public function verify_subscriber() { |
| 510 |
$hash = HTTP::get( 'hash', false ); |
| 511 |
$slug = HTTP::get( 'uid', false ); |
| 512 |
$inhouse = HTTP::get( 'inhouse', 0 ); |
| 513 |
if ( $inhouse && ! is_user_logged_in() ) { |
| 514 |
// This is in-house, so we need to redirect. |
| 515 |
auth_redirect(); |
| 516 |
} |
| 517 |
if ( false === $hash || false === $slug ) { |
| 518 |
wp_die( __( 'You shall not pass.', 'wpdef' ) ); |
| 519 |
} |
| 520 |
$m = $this->service->find_module_by_slug( $slug ); |
| 521 |
if ( ! is_object( $m ) ) { |
| 522 |
wp_die( __( 'You shall not pass.', 'wpdef' ) ); |
| 523 |
} |
| 524 |
if ( $inhouse ) { |
| 525 |
$processed = false; |
| 526 |
foreach ( $m->in_house_recipients as &$recipient ) { |
| 527 |
if ( Model_Notification::USER_SUBSCRIBED === $recipient['status'] ) { |
| 528 |
continue; |
| 529 |
} |
| 530 |
|
| 531 |
$email = $recipient['email']; |
| 532 |
$name = $recipient['name']; |
| 533 |
if ( hash_equals( $hash, hash( 'sha256', $email . AUTH_SALT ) ) |
| 534 |
&& $email === $this->get_current_user_email() ) { |
| 535 |
$recipient['status'] = Model_Notification::USER_SUBSCRIBED; |
| 536 |
$this->service->send_subscribed_email( $email, $m, $name ); |
| 537 |
$processed = true; |
| 538 |
} |
| 539 |
} |
| 540 |
} else { |
| 541 |
foreach ( $m->out_house_recipients as &$recipient ) { |
| 542 |
if ( Model_Notification::USER_SUBSCRIBED === $recipient['status'] ) { |
| 543 |
continue; |
| 544 |
} |
| 545 |
|
| 546 |
$email = $recipient['email']; |
| 547 |
$name = $recipient['name']; |
| 548 |
if ( hash_equals( $hash, hash( 'sha256', $email . AUTH_SALT ) ) ) { |
| 549 |
$recipient['status'] = Model_Notification::USER_SUBSCRIBED; |
| 550 |
$this->service->send_subscribed_email( $email, $m, $name ); |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
$m->save(); |
| 555 |
if ( $inhouse ) { |
| 556 |
if ( $processed ) { |
| 557 |
wp_redirect( |
| 558 |
add_query_arg( |
| 559 |
array( |
| 560 |
'slug' => $m->slug, |
| 561 |
'context' => 'subscribed', |
| 562 |
), |
| 563 |
get_edit_profile_url() |
| 564 |
) |
| 565 |
); |
| 566 |
} else { |
| 567 |
wp_redirect( home_url() ); |
| 568 |
} |
| 569 |
} else { |
| 570 |
wp_redirect( home_url() ); |
| 571 |
} |
| 572 |
exit; |
| 573 |
} |
| 574 |
|
| 575 |
/** |
| 576 |
* Enqueue assets & output data. |
| 577 |
*/ |
| 578 |
public function enqueue_assets() { |
| 579 |
if ( ! $this->is_page_active() ) { |
| 580 |
return; |
| 581 |
} |
| 582 |
wp_localize_script( |
| 583 |
'def-notification', |
| 584 |
'notification', |
| 585 |
array_merge( $this->data_frontend(), $this->dump_routes_and_nonces() ) |
| 586 |
); |
| 587 |
wp_enqueue_script( 'def-momentjs', defender_asset_url( '/assets/js/vendor/moment/moment.min.js' ) ); |
| 588 |
wp_enqueue_script( 'def-notification' ); |
| 589 |
$this->enqueue_main_assets(); |
| 590 |
wp_enqueue_style( |
| 591 |
'def-select2', |
| 592 |
defender_asset_url( '/assets/css/select2.min.css' ) |
| 593 |
); |
| 594 |
} |
| 595 |
|
| 596 |
/** |
| 597 |
* An endpoint for fetching users pool. |
| 598 |
* |
| 599 |
* @param Request $request Request data. |
| 600 |
* |
| 601 |
* @defender_route |
| 602 |
*/ |
| 603 |
public function get_users( Request $request ) { |
| 604 |
$data = $request->get_data( |
| 605 |
array( |
| 606 |
'paged' => array( |
| 607 |
'type' => 'int', |
| 608 |
'sanitize' => 'sanitize_text_field', |
| 609 |
), |
| 610 |
'search' => array( |
| 611 |
'type' => 'string', |
| 612 |
'sanitize' => 'sanitize_text_field', |
| 613 |
), |
| 614 |
'exclude' => array( |
| 615 |
'type' => 'array', |
| 616 |
), |
| 617 |
'module' => array( |
| 618 |
'type' => 'string', |
| 619 |
'sanitize' => 'sanitize_text_field', |
| 620 |
), |
| 621 |
'user_role_filter' => array( |
| 622 |
'type' => 'string', |
| 623 |
'sanitize' => 'sanitize_text_field', |
| 624 |
), |
| 625 |
'user_sort' => array( |
| 626 |
'type' => 'string', |
| 627 |
'sanitize' => 'sanitize_text_field', |
| 628 |
), |
| 629 |
) |
| 630 |
); |
| 631 |
$paged = 1; |
| 632 |
$exclude = $data['exclude'] ?? array(); |
| 633 |
$username = $data['search'] ?? ''; |
| 634 |
$slug = $data['module'] ?? null; |
| 635 |
$role = ''; |
| 636 |
|
| 637 |
if ( |
| 638 |
isset( $data['user_role_filter'] ) && |
| 639 |
'all' !== $data['user_role_filter'] |
| 640 |
) { |
| 641 |
$role = $data['user_role_filter']; |
| 642 |
} |
| 643 |
|
| 644 |
$order_by = 'ID'; |
| 645 |
$order = 'DESC'; |
| 646 |
if ( isset( $data['user_sort'] ) ) { |
| 647 |
switch ( $data['user_sort'] ) { |
| 648 |
case 'recent': |
| 649 |
$order_by = 'registered'; |
| 650 |
$order = 'DESC'; |
| 651 |
break; |
| 652 |
case 'alpha_asc': |
| 653 |
$order_by = 'display_name'; |
| 654 |
$order = 'ASC'; |
| 655 |
break; |
| 656 |
case 'alpha_desc': |
| 657 |
default: |
| 658 |
$order_by = 'display_name'; |
| 659 |
$order = 'DESC'; |
| 660 |
break; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
if ( strlen( $username ) ) { |
| 665 |
$username = "*$username*"; |
| 666 |
} |
| 667 |
|
| 668 |
$users = $this->service->get_users_pool( |
| 669 |
$exclude, |
| 670 |
$role, |
| 671 |
$username, |
| 672 |
$order_by, |
| 673 |
$order, |
| 674 |
10, |
| 675 |
$paged |
| 676 |
); |
| 677 |
|
| 678 |
if ( ! is_null( $slug ) ) { |
| 679 |
$notification = $this->service->find_module_by_slug( $slug ); |
| 680 |
if ( is_object( $notification ) ) { |
| 681 |
foreach ( $notification->in_house_recipients as $recipient ) { |
| 682 |
foreach ( $users as &$user ) { |
| 683 |
if ( $user['email'] === $recipient['email'] ) { |
| 684 |
$user['status'] = $recipient['status']; |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
wp_send_json_success( $users ); |
| 692 |
} |
| 693 |
|
| 694 |
public function remove_settings() { |
| 695 |
foreach ( $this->service->get_modules_as_objects() as $module ) { |
| 696 |
$module->delete(); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
public function remove_data() {} |
| 701 |
|
| 702 |
public function to_array() {} |
| 703 |
|
| 704 |
/** |
| 705 |
* All the variables that we will show on frontend, both in the main page, or dashboard widget. |
| 706 |
* |
| 707 |
* @return array |
| 708 |
*/ |
| 709 |
public function data_frontend() { |
| 710 |
return array( |
| 711 |
'notifications' => $this->service->get_modules(), |
| 712 |
'inactive_notifications' => $this->service->get_inactive_modules(), |
| 713 |
'active_count' => $this->service->count_active(), |
| 714 |
'next_run' => $this->service->get_next_run(), |
| 715 |
'misc' => array( |
| 716 |
'days_of_week' => $this->get_days_of_week(), |
| 717 |
'times_of_day' => $this->get_times(), |
| 718 |
'timezone_text' => sprintf( |
| 719 |
/* translators: %s - timezone, %s - time */ |
| 720 |
__( |
| 721 |
'Your timezone is set to <strong>%1$s</strong>, so your current time is <strong>%2$s</strong>.', |
| 722 |
'wpdef' |
| 723 |
), |
| 724 |
wp_timezone_string(), |
| 725 |
date( 'H:i', current_time( 'timestamp' ) )// phpcs:ignore |
| 726 |
), |
| 727 |
'default_recipient' => $this->get_default_recipient(), |
| 728 |
), |
| 729 |
); |
| 730 |
} |
| 731 |
|
| 732 |
/** |
| 733 |
* Import the data of other source into this, it can be when HUB trigger the import, or user apply a preset. |
| 734 |
* |
| 735 |
* @param array $data |
| 736 |
*/ |
| 737 |
public function import_data( $data ) {} |
| 738 |
|
| 739 |
/** |
| 740 |
* @return array |
| 741 |
*/ |
| 742 |
public function export_strings() { |
| 743 |
$modules = wd_di()->get( Notification::class )->service->get_modules_as_objects(); |
| 744 |
$strings = array(); |
| 745 |
foreach ( $modules as $module ) { |
| 746 |
/* translators: %s - module title, %s - module status */ |
| 747 |
$string = __( '%1$s: %2$s', 'wpdef' ); |
| 748 |
if ( 'notification' === $module->type ) { |
| 749 |
$string = sprintf( |
| 750 |
$string, |
| 751 |
$module->title, |
| 752 |
Model_Notification::STATUS_ACTIVE === $module->status ? __( 'Enabled', 'wpdef' ) : __( 'Disabled', 'wpdef' ) |
| 753 |
); |
| 754 |
} else { |
| 755 |
$string = sprintf( |
| 756 |
$string, |
| 757 |
$module->title, |
| 758 |
Model_Notification::STATUS_ACTIVE === $module->status ? $module->to_string() : __( 'Disabled', 'wpdef' ) |
| 759 |
); |
| 760 |
} |
| 761 |
$strings[] = $string; |
| 762 |
} |
| 763 |
|
| 764 |
return $strings; |
| 765 |
} |
| 766 |
|
| 767 |
/** |
| 768 |
* Resend invite email. |
| 769 |
* |
| 770 |
* @param Request $request |
| 771 |
* |
| 772 |
* @defender_route |
| 773 |
* @return Response |
| 774 |
*/ |
| 775 |
public function resend_invite_email( Request $request ) { |
| 776 |
$data = $request->get_data( |
| 777 |
array( |
| 778 |
'slug' => array( |
| 779 |
'type' => 'string', |
| 780 |
'sanitize' => 'sanitize_textarea_field', |
| 781 |
), |
| 782 |
'email' => array( |
| 783 |
'type' => 'string', |
| 784 |
'sanitize' => 'sanitize_text_field', |
| 785 |
), |
| 786 |
'id' => array( |
| 787 |
'type' => 'integer', |
| 788 |
), |
| 789 |
'name' => array( |
| 790 |
'type' => 'string', |
| 791 |
'sanitize' => 'sanitize_text_field', |
| 792 |
), |
| 793 |
) |
| 794 |
); |
| 795 |
|
| 796 |
$model = $this->service->find_module_by_slug( $data['slug'] ); |
| 797 |
|
| 798 |
if ( ! is_object( $model ) ) { |
| 799 |
return new Response( false, [ |
| 800 |
'message' => __( 'Module not found.', 'wpdef' ), |
| 801 |
] ); |
| 802 |
} |
| 803 |
|
| 804 |
$subscriber = array( |
| 805 |
'email' => $data['email'], |
| 806 |
'name' => $data['name'], |
| 807 |
); |
| 808 |
|
| 809 |
if ( ! empty( $data['id'] ) ) { |
| 810 |
$subscriber['id'] = $data['id']; |
| 811 |
} |
| 812 |
|
| 813 |
// Resend invite email now. |
| 814 |
$sent = $this->service->send_email( $subscriber, $model ); |
| 815 |
|
| 816 |
if ( $sent ) { |
| 817 |
return new Response( true, [ |
| 818 |
'message' => __( 'Invitation sent successfully.', 'wpdef' ), |
| 819 |
] ); |
| 820 |
} |
| 821 |
|
| 822 |
return new Response( false, [ |
| 823 |
'message' => __( 'Sorry! We could not send the invitation, Please try again later.', 'wpdef' ), |
| 824 |
] ); |
| 825 |
} |
| 826 |
|
| 827 |
/** |
| 828 |
* Get user roles with count. |
| 829 |
* |
| 830 |
* @defender_route |
| 831 |
*/ |
| 832 |
public function get_user_roles() { |
| 833 |
$user_roles = $this->service->get_user_roles(); |
| 834 |
|
| 835 |
wp_send_json_success( $user_roles ); |
| 836 |
} |
| 837 |
} |
| 838 |
|