PluginProbe
WANotifier for Forms and Actions / 0.1.1
WANotifier for Forms and Actions v0.1.1
3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 1.0.3 All 66 releases
notifier / includes / classes / class-notifier-dashboard.php

class-notifier-dashboard.php in WANotifier for Forms and Actions 0.1.1, at includes/classes/class-notifier-dashboard.php

161 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_disclaimer_form' ) );
15 add_action( 'admin_init', array( __CLASS__ , 'handle_validation_form' ) );
16 }
17
18 /**
19 * Add dashboard page to admin menu
20 */
21 public static function setup_admin_page () {
22 add_menu_page( 'Notifier', '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 forms
34 */
35 public static function handle_disclaimer_form () {
36 if ( ! self::is_dashboard_page() ) {
37 return;
38 }
39 if ( ! isset( $_POST['disclaimer'] ) ) {
40 return;
41 }
42 //phpcs:ignore
43 if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], NOTIFIER_NAME . '-disclaimer' ) ) {
44 return;
45 }
46
47 update_option(NOTIFIER_PREFIX . 'disclaimer', 'accepted');
48 }
49
50 /**
51 * Handle validation forms
52 */
53 public static function handle_validation_form () {
54 if ( ! self::is_dashboard_page() ) {
55 return;
56 }
57 if ( ! isset( $_POST['validate'] ) ) {
58 return;
59 }
60 //phpcs:ignore
61 if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], NOTIFIER_NAME . '-validate' ) ) {
62 return;
63 }
64
65 $phone_number_id = get_option( NOTIFIER_PREFIX . 'phone_number_id' );
66 $business_account_id = get_option( NOTIFIER_PREFIX . 'business_account_id' );
67 $permanent_access_token = get_option( NOTIFIER_PREFIX . 'permanent_access_token' );
68
69 if ('' == $phone_number_id || '' == $business_account_id || '' == $permanent_access_token) {
70 $notices[] = array(
71 'message' => 'Setup not complete. Please follow the steps shown below to create <b>Phone number ID</b> , <b>Business Account ID</b> and <b>Permanent Access Token</b>. Then add these details on the <a href="admin.php?page=' . NOTIFIER_NAME . '-settings">Settings</a> page before proceeding for validation.',
72 'type' => 'error'
73 );
74 new Notifier_Admin_Notices($notices);
75 return;
76 }
77
78 // Fetch phone number details
79 $response = Notifier::wa_cloud_api_request('', array(), 'GET');
80 if ($response->error) {
81 $notices[] = array(
82 'message' => 'API request can not be validated. Error Code ' . $response->error->code . ': ' . $response->error->message ,
83 'type' => 'error'
84 );
85 new Notifier_Admin_Notices($notices);
86 return;
87 } else {
88 $phone_number_details[$phone_number_id] = array (
89 'display_num' => $response->display_phone_number,
90 'display_name' => $response->verified_name,
91 'phone_num_status' => $response->code_verification_status,
92 'quality_rating' => $response->quality_rating
93 );
94 update_option( NOTIFIER_PREFIX . 'phone_number_details', $phone_number_details );
95 }
96
97 $response = ''; // reset
98
99 // Fetch message templates
100 $response = Notifier::wa_business_api_request('message_templates', array(), 'GET');
101 if ($response->error) {
102 $notices[] = array(
103 'message' => 'API request can not be validated. Error Code ' . $response->error->code . ': ' . $response->error->message ,
104 'type' => 'error'
105 );
106 new Notifier_Admin_Notices($notices);
107 return;
108 } else {
109 $message_templates = $response->data;
110 foreach ($message_templates as $template) {
111 if ('hello_world' != $template->name) {
112 continue;
113 }
114
115 $post_id = wp_insert_post ( array (
116 'post_title' => 'Hello World!',
117 'post_status' => 'publish',
118 'post_type' => 'wa_message_template'
119 ) );
120
121 update_post_meta ( $post_id, NOTIFIER_PREFIX . 'template_name', $template->name);
122 update_post_meta ( $post_id, NOTIFIER_PREFIX . 'category', $template->category);
123 update_post_meta ( $post_id, NOTIFIER_PREFIX . 'status', $template->status);
124 update_post_meta ( $post_id, NOTIFIER_PREFIX . 'template_id', $template->id);
125 update_post_meta ( $post_id, NOTIFIER_PREFIX . 'language', $template->language);
126
127 foreach ($template->components as $component) {
128 switch ($component->type) {
129 case 'HEADER':
130 update_post_meta ( $post_id, NOTIFIER_PREFIX . 'header_type', 'text');
131 update_post_meta ( $post_id, NOTIFIER_PREFIX . 'header_text', $component->text);
132 break;
133
134 case 'BODY':
135 update_post_meta ( $post_id, NOTIFIER_PREFIX . 'body_text', $component->text);
136 break;
137
138 case 'FOOTER':
139 update_post_meta ( $post_id, NOTIFIER_PREFIX . 'footer_text', $component->text);
140 }
141 }
142 }
143 update_option( NOTIFIER_PREFIX . 'api_credentials_validated', 'yes');
144 wp_redirect( admin_url( '/admin.php?page=' . NOTIFIER_NAME ) );
145 exit;
146 }
147 }
148
149 /**
150 * Check if on dashboard page
151 */
152 public static function is_dashboard_page() {
153 $current_page = isset($_GET['page']) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
154 if (NOTIFIER_NAME === $current_page) {
155 return true;
156 }
157 return false;
158 }
159
160 }
161