| 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( |
| 22 |
'WANotifier', |
| 23 |
'WANotifier', |
| 24 |
'manage_options', |
| 25 |
NOTIFIER_NAME, |
| 26 |
array( __CLASS__ , 'output'), |
| 27 |
'data:image/svg+xml;base64,' . base64_encode(file_get_contents(NOTIFIER_PATH . 'assets/images/menu-icon.svg')), |
| 28 |
'51' |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Output |
| 34 |
*/ |
| 35 |
public static function output() { |
| 36 |
include_once NOTIFIER_PATH . '/views/admin-dashboard.php'; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Handle displaimer form |
| 41 |
*/ |
| 42 |
public static function handle_webhook_validation_form () { |
| 43 |
if ( ! isset( $_POST['webhook_validation'] ) ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
//phpcs:ignore |
| 48 |
if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], NOTIFIER_NAME . '-webhook-validation' ) ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
$api_key = (isset($_POST['notifier_api_key'])) ? sanitize_text_field(wp_unslash($_POST['notifier_api_key'])) : ''; |
| 53 |
|
| 54 |
if('' == trim($api_key)) { |
| 55 |
$notices[] = array( |
| 56 |
'message' => 'Please enter API key.', |
| 57 |
'type' => 'error' |
| 58 |
); |
| 59 |
new Notifier_Admin_Notices($notices, true); |
| 60 |
wp_redirect(admin_url('admin.php?page=notifier')); |
| 61 |
die; |
| 62 |
} |
| 63 |
|
| 64 |
update_option('notifier_api_key', $api_key); |
| 65 |
delete_option('notifier_enabled_triggers'); |
| 66 |
|
| 67 |
$params = array( |
| 68 |
'site_url' => site_url(), |
| 69 |
'source' => 'wp' |
| 70 |
); |
| 71 |
|
| 72 |
$response = Notifier::send_api_request( 'verify_api', $params, 'POST' ); |
| 73 |
|
| 74 |
if(isset($response->error) && $response->error == false){ |
| 75 |
update_option('notifier_api_activated', 'yes'); |
| 76 |
$message_text = isset($response->data) ? $response->data : 'API key validated and saved successfully'; |
| 77 |
$message_type = 'success'; |
| 78 |
} |
| 79 |
else { |
| 80 |
$message_text = isset($response->message) ? $response->message : 'There was an error validating your API key.'; |
| 81 |
$message_type = 'error'; |
| 82 |
} |
| 83 |
|
| 84 |
$notices[] = array( |
| 85 |
'message' => $message_text, |
| 86 |
'type' => $message_type |
| 87 |
); |
| 88 |
new Notifier_Admin_Notices($notices, true); |
| 89 |
wp_redirect(admin_url('admin.php?page=notifier')); |
| 90 |
die; |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|