| 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 |
add_action( 'admin_init', array( __CLASS__ , 'handle_save_triggers_form' ) ); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Add dashboard page to admin menu |
| 20 |
*/ |
| 21 |
public static function setup_admin_page () { |
| 22 |
add_menu_page( 'WA Notifier', 'WA Notifier', 'manage_options', NOTIFIER_NAME, array( __CLASS__ , 'output'), 'dashicons-megaphone', '51' ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Output |
| 27 |
*/ |
| 28 |
public static function output() { |
| 29 |
include_once NOTIFIER_PATH . '/views/admin-dashboard.php'; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Handle displaimer form |
| 34 |
*/ |
| 35 |
public static function handle_webhook_validation_form () { |
| 36 |
if ( ! isset( $_POST['webhook_validation'] ) ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
//phpcs:ignore |
| 41 |
if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], NOTIFIER_NAME . '-webhook-validation' ) ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$api_key = (isset($_POST['notifier_api_key'])) ? sanitize_text_field(wp_unslash($_POST['notifier_api_key'])) : ''; |
| 46 |
|
| 47 |
if('' == trim($api_key)) { |
| 48 |
$notices[] = array( |
| 49 |
'message' => 'Please enter API key.', |
| 50 |
'type' => 'error' |
| 51 |
); |
| 52 |
new Notifier_Admin_Notices($notices, true); |
| 53 |
wp_redirect(admin_url('admin.php?page=notifier')); |
| 54 |
die; |
| 55 |
} |
| 56 |
|
| 57 |
update_option('notifier_api_key', $api_key); |
| 58 |
delete_option('notifier_enabled_triggers'); |
| 59 |
|
| 60 |
$params = array( |
| 61 |
'action' => 'verify_api', |
| 62 |
'site_url' => site_url(), |
| 63 |
'source' => 'wp' |
| 64 |
); |
| 65 |
|
| 66 |
$response = Notifier::send_api_request( $params, 'POST' ); |
| 67 |
|
| 68 |
if($response->error){ |
| 69 |
$notices[] = array( |
| 70 |
'message' => $response->message, |
| 71 |
'type' => 'error' |
| 72 |
); |
| 73 |
new Notifier_Admin_Notices($notices, true); |
| 74 |
wp_redirect(admin_url('admin.php?page=notifier')); |
| 75 |
die; |
| 76 |
} |
| 77 |
|
| 78 |
update_option('notifier_api_activated', 'yes'); |
| 79 |
|
| 80 |
$notices[] = array( |
| 81 |
'message' => $response->data, |
| 82 |
'type' => 'success' |
| 83 |
); |
| 84 |
new Notifier_Admin_Notices($notices, true); |
| 85 |
wp_redirect(admin_url('admin.php?page=notifier')); |
| 86 |
die; |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Handle save triggers form |
| 92 |
*/ |
| 93 |
public static function handle_save_triggers_form () { |
| 94 |
if ( ! isset( $_POST['notifier_save_triggers'] ) ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
//phpcs:ignore |
| 99 |
if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], NOTIFIER_NAME . '-save-triggers' ) ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
$notifier_triggers = (!empty($_POST['notifier_triggers'])) ? notifier_sanitize_array($_POST['notifier_triggers']) : array(); |
| 104 |
$all_triggers = Notifier_Notification_Triggers::get_notification_triggers(); |
| 105 |
$selected_triggers = array(); |
| 106 |
foreach ($all_triggers as $key => $triggers) { |
| 107 |
foreach ($triggers as $trigger){ |
| 108 |
if( ! in_array($trigger['id'], $notifier_triggers) ){ |
| 109 |
continue; |
| 110 |
} |
| 111 |
unset($trigger['action']); |
| 112 |
$selected_triggers[$key][] = $trigger; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
$params = array( |
| 117 |
'action' => 'update_triggers', |
| 118 |
'site_url' => site_url(), |
| 119 |
'source' => 'wp', |
| 120 |
'triggers' => $selected_triggers |
| 121 |
); |
| 122 |
|
| 123 |
$response = Notifier::send_api_request( $params, 'POST' ); |
| 124 |
|
| 125 |
if($response->error){ |
| 126 |
$notices[] = array( |
| 127 |
'message' => $response->message, |
| 128 |
'type' => 'error' |
| 129 |
); |
| 130 |
new Notifier_Admin_Notices($notices, true); |
| 131 |
wp_redirect(admin_url('admin.php?page=notifier')); |
| 132 |
die; |
| 133 |
} |
| 134 |
|
| 135 |
update_option('notifier_enabled_triggers', $notifier_triggers); |
| 136 |
|
| 137 |
$notices[] = array( |
| 138 |
'message' => $response->data, |
| 139 |
'type' => 'success' |
| 140 |
); |
| 141 |
new Notifier_Admin_Notices($notices, true); |
| 142 |
wp_redirect(admin_url('admin.php?page=notifier')); |
| 143 |
die; |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
} |
| 148 |
|