| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin dashboard page class |
| 4 |
* |
| 5 |
* @package Wa_Notifier |
| 6 |
*/ |
| 7 |
class Notifier_Dashboard { |
| 8 |
|
| 9 |
/** |
| 10 |
* Init |
| 11 |
*/ |
| 12 |
public static function init() { |
| 13 |
add_action( 'admin_menu', array( __CLASS__ , 'setup_admin_page') ); |
| 14 |
add_action( 'admin_init', array( __CLASS__ , 'handle_webhook_validation_form' ) ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Add dashboard page to admin menu |
| 19 |
*/ |
| 20 |
public static function setup_admin_page () { |
| 21 |
add_menu_page( 'WANotifier', 'WANotifier', 'manage_options', NOTIFIER_NAME, array( __CLASS__ , 'output'), 'dashicons-megaphone', '51' ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Output |
| 26 |
*/ |
| 27 |
public static function output() { |
| 28 |
include_once NOTIFIER_PATH . '/views/admin-dashboard.php'; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Handle displaimer form |
| 33 |
*/ |
| 34 |
public static function handle_webhook_validation_form () { |
| 35 |
if ( ! isset( $_POST['webhook_validation'] ) ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
//phpcs:ignore |
| 40 |
if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], NOTIFIER_NAME . '-webhook-validation' ) ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
$api_key = (isset($_POST['notifier_api_key'])) ? sanitize_text_field(wp_unslash($_POST['notifier_api_key'])) : ''; |
| 45 |
|
| 46 |
if('' == trim($api_key)) { |
| 47 |
$notices[] = array( |
| 48 |
'message' => 'Please enter API key.', |
| 49 |
'type' => 'error' |
| 50 |
); |
| 51 |
new Notifier_Admin_Notices($notices, true); |
| 52 |
wp_redirect(admin_url('admin.php?page=notifier')); |
| 53 |
die; |
| 54 |
} |
| 55 |
|
| 56 |
update_option('notifier_api_key', $api_key); |
| 57 |
delete_option('notifier_enabled_triggers'); |
| 58 |
|
| 59 |
$params = array( |
| 60 |
'site_url' => site_url(), |
| 61 |
'source' => 'wp' |
| 62 |
); |
| 63 |
|
| 64 |
$response = Notifier::send_api_request( 'verify_api', $params, 'POST' ); |
| 65 |
|
| 66 |
if($response->error){ |
| 67 |
$notices[] = array( |
| 68 |
'message' => $response->message, |
| 69 |
'type' => 'error' |
| 70 |
); |
| 71 |
new Notifier_Admin_Notices($notices, true); |
| 72 |
wp_redirect(admin_url('admin.php?page=notifier')); |
| 73 |
die; |
| 74 |
} |
| 75 |
|
| 76 |
update_option('notifier_api_activated', 'yes'); |
| 77 |
|
| 78 |
$notices[] = array( |
| 79 |
'message' => $response->data, |
| 80 |
'type' => 'success' |
| 81 |
); |
| 82 |
new Notifier_Admin_Notices($notices, true); |
| 83 |
wp_redirect(admin_url('admin.php?page=notifier')); |
| 84 |
die; |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
|