PluginProbe
WANotifier for Forms and Actions / 2.0.7
WANotifier for Forms and Actions v2.0.7
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-settings.php

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

416 lines 13.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Settings page class
4 *
5 * @package Wa_Notifier
6 */
7 class Notifier_Settings {
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__ , 'save_settings_fields' ) );
15 add_action( 'admin_init', array( __CLASS__ , 'disconnect_notifier' ) );
16 }
17
18 /**
19 * Add settings page to men
20 */
21 public static function setup_admin_page () {
22 if (!Notifier::is_api_active()){
23 return;
24 }
25
26 add_submenu_page( NOTIFIER_NAME, 'Settings', 'Settings', 'manage_options', NOTIFIER_NAME . '-settings', array( __CLASS__, 'output' ) );
27 }
28
29 /**
30 * Output
31 */
32 public static function output() {
33 include_once NOTIFIER_PATH . '/views/admin-settings.php';
34 }
35
36 /**
37 * Get settings tabs
38 */
39 private static function get_settings_tabs() {
40 $tabs = array(
41 'general' => 'General'
42 );
43 return $tabs;
44 }
45
46 /**
47 * Check if on settings page
48 */
49 public static function is_settings_page() {
50 $current_page = isset($_GET['page']) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
51 return strpos($current_page, NOTIFIER_NAME . '-settings') !== false;
52 }
53
54 /**
55 * Settings fields
56 */
57 private static function settings_fields($tab) {
58 $api_key = get_option(NOTIFIER_PREFIX . 'api_key');
59 if(Notifier::is_api_active() && '' != $api_key) {
60 $disabled = true;
61 $description = 'You are successfully connected to WANotifier.com. <a href="?notifier_disconnect=1">Disconnect</a>.';
62 }
63 else{
64 $disabled = false;
65 $description = 'You are not connected to WANotifier.com yet. Please enter valid API key and save the settings. You can get your WANotifier.com API Key from <a href="https://app.wanotifier.com/settings/api/" target="_blank">here</a>.';
66 }
67 $settings = array();
68 switch ($tab) {
69 case 'general':
70 $settings = array(
71 array(
72 'title' => 'General',
73 'description' => '',
74 'type' => 'title',
75 ),
76 array(
77 'id' => 'api_key',
78 'title' => 'WANotifier.com API key',
79 'type' => 'text',
80 'placeholder' => 'Enter your WANotifier.com API key here',
81 'default' => '',
82 'description' => $description,
83 'disabled' => $disabled
84 ),
85 );
86 break;
87 }
88 $settings = apply_filters( 'notifier_$tab_settings_fields', $settings );
89 return $settings;
90 }
91
92 /**
93 * Generate HTML for displaying individual fields
94 */
95 public static function display_field( $field ) {
96 $html = '';
97
98 if (isset($field['id'])) {
99 $option_name = NOTIFIER_PREFIX . $field['id'];
100 $option = get_option( $option_name );
101 }
102
103 $data = '';
104 if ( isset( $field['default'] ) ) {
105 $data = $field['default'];
106 if ( isset( $option ) && '' != $option) {
107 $data = $option;
108 }
109 }
110
111 $custom_attributes = array();
112 if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
113 foreach ( $field['custom_attributes'] as $attribute => $value ) {
114 $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
115 }
116 }
117
118 if ( isset($field['required']) && $field['required'] ) {
119 $custom_attributes[] = 'required="required"';
120 }
121
122 if ( isset($field['disabled']) && $field['disabled'] ) {
123 $custom_attributes[] = 'disabled="disabled"';
124 }
125
126 $custom_attributes_string = implode( ' ', $custom_attributes );
127
128 if ( 'title' === $field['type']) {
129 $html .= '<tr><th class="section-title" colspan="2"><h3>' . $field['title'] . '</h3>';
130 $html .= '<p>' . $field['description'] . '</p></th></tr>';
131 } else {
132 $html .= '<tr>';
133 $html .= '<th>' . $field['title'] . '</th>';
134 $html .= '<td>';
135
136 switch ( $field['type'] ) {
137
138 case 'text':
139 case 'password':
140 case 'number':
141 $html .= '<input id="' . esc_attr( $option_name ) . '" type="' . $field['type'] . '" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" value="' . $data . '" '.$custom_attributes_string.'/>';
142 break;
143
144 case 'textarea':
145 $html .= '<textarea id="' . esc_attr( $option_name ) . '" rows="5" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" '.$custom_attributes_string.'>' . $data . '</textarea><br/>';
146 break;
147
148 case 'checkbox':
149 $checked = '';
150 if ( $option && 'on' == $option ) {
151 $checked = 'checked="checked"';
152 }
153 $html .= '<label><input id="' . esc_attr( $option_name ) . '" type="' . $field['type'] . '" name="' . esc_attr( $option_name ) . '" ' . $checked . ' '.$custom_attributes_string.'/>' . $field['label'] . '</label>';
154 break;
155
156 case 'checkbox_multi':
157 foreach ( $field['options'] as $k => $v ) {
158 $checked = false;
159 if ( in_array( $k, $data ) ) {
160 $checked = true;
161 }
162 $html .= '<p><label for="' . esc_attr( $field['id'] . '_' . $k ) . '"><input type="checkbox" ' . checked( $checked, true, false ) . ' name="' . esc_attr( $option_name ) . '[]" value="' . esc_attr( $k ) . '" id="' . esc_attr( $option_name . '_' . $k ) . '" '.$custom_attributes_string.'/> ' . $v . '</label></p>';
163 }
164 break;
165
166 case 'radio':
167 foreach ( $field['options'] as $k => $v ) {
168 $checked = false;
169 if ( $k == $data ) {
170 $checked = true;
171 }
172 $html .= '<p><label for="' . esc_attr( $field['id'] . '_' . $k ) . '"><input type="radio" ' . checked( $checked, true, false ) . ' name="' . esc_attr( $option_name ) . '" value="' . esc_attr( $k ) . '" id="' . esc_attr( $option_name . '_' . $k ) . '" '.$custom_attributes_string.'/> ' . $v . '</label></p>';
173 }
174 break;
175
176 case 'select':
177 $html .= '<select name="' . esc_attr( $option_name ) . '" id="' . esc_attr( $field['id'] ) . '" '.$custom_attributes_string.'>';
178 foreach ( $field['options'] as $k => $v ) {
179 $selected = false;
180 if ( $k == $data ) {
181 $selected = true;
182 }
183 $html .= '<option ' . selected( $selected, true, false ) . ' value="' . esc_attr( $k ) . '">' . $v . '</option>';
184 }
185 $html .= '</select> ';
186 break;
187
188 case 'select_multi':
189 $html .= '<select name="' . esc_attr( $option_name ) . '[]" id="' . esc_attr( $field['id'] ) . '" multiple="multiple" '.$custom_attributes_string.'>';
190 foreach ( $field['options'] as $k => $v ) {
191 $selected = false;
192 if ( in_array( $k, $data ) ) {
193 $selected = true;
194 }
195 $html .= '<option ' . selected( $selected, true, false ) . ' value="' . esc_attr( $k ) . '" />' . $v . '</label> ';
196 }
197 $html .= '</select> ';
198 break;
199
200 case 'file':
201 $uploader_title = isset($field['uploader_title']) ? $field['uploader_title'] : 'Upload media';
202 $uploader_button_text = isset($field['uploader_button_text']) ? $field['uploader_button_text'] : 'Select';
203 $file_types = isset($field['uploader_supported_file_types']) ? $field['uploader_supported_file_types'] : array();
204
205 $image_thumb = '';
206 $video_url = '';
207 $show_image = 'hide';
208 $show_video = 'hide';
209
210 $html .= '<span class="notifier-media-preview">';
211 if ( '' != $data ) {
212 $file_type = $field['uploader_supported_file_types'];
213 switch ($file_type) {
214 case 'image':
215 case 'image/jpeg':
216 case 'image/png':
217 case 'application/pdf':
218 $image_thumb = wp_get_attachment_thumb_url( $data );
219 $show_image = '';
220 $show_video = 'hide';
221 break;
222
223 case 'video/mp4':
224 $video_url = wp_get_attachment_url( $data );
225 $show_image = 'hide';
226 $show_video = '';
227 break;
228
229 default:
230 $show_image = 'hide';
231 $show_video = 'hide';
232 }
233 }
234 $html .= '<img id="' . esc_attr( $option_name ) . '_preview_image" class="notifier-media-preview-item '. esc_attr($show_image) .'" src="' . esc_url($image_thumb) . '" />';
235 $html .= '<video id="' . esc_attr( $option_name ) . '_preview_video" class="notifier-media-preview-item '. esc_attr($show_video) .'" width="300" height="169" controls muted><source src="' . esc_url($video_url) . '" type="video/mp4"></video><br/>';
236 $html .= '</span>';
237
238 $html .= '<input id="' . esc_attr( $option_name ) . '_button" type="button" data-uploader_title="' . esc_attr($uploader_title) . '" data-uploader_button_text="' . esc_attr($uploader_button_text) . '" data-uploader_supported_file_types="' . esc_attr($file_types) . '" class="notifier-media-upload-button button" value="' . 'Upload' . '" /> ';
239 $html .= '<input id="' . esc_attr( $option_name ) . '_delete" type="button" class="notifier-media-delete-button button" value="' . 'Remove' . '" />';
240 $html .= '<input id="' . esc_attr( $option_name ) . '" class="notifier-media-attachment-id" type="hidden" name="' . esc_attr( $option_name ) . '" value="' . esc_attr( $data ) . '"/><br/>';
241 break;
242
243 case 'color':
244 ?>
245 <div class="color-picker" style="position:relative;">
246 <input type="text" name="<?php echo esc_attr( $option_name ); ?>" class="color" value="<?php echo esc_attr( $data ); ?>" />
247 <div style="position:absolute;background:#FFF;z-index:99;border-radius:100%;" class="colorpicker"></div>
248 </div>
249 <?php
250 break;
251
252 }
253
254 if ( '' != $field['description'] ) {
255 $html .= '<p class="description">' . wp_kses_post($field['description']) . '</p>';
256 }
257 }
258
259 //phpcs:ignore
260 echo $html;
261 }
262
263 /**
264 * Show the setting fields
265 */
266 public static function show_settings_fields ($tab) {
267 $setting_fields = self::settings_fields($tab);
268 echo "<table class='notifier-fields-table'>";
269 foreach ($setting_fields as $field) {
270 self::display_field( $field );
271 }
272 echo '</table>';
273 }
274
275 /**
276 * Save the settings.
277 *
278 * TODO - check fields other than text and textarea
279 */
280 public static function save_settings_fields() {
281 if ( ! self::is_settings_page() ) {
282 return;
283 }
284
285 if ( ! isset( $_POST['save'] ) ) {
286 return;
287 }
288
289 //phpcs:ignore
290 if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], NOTIFIER_NAME . '-settings' ) ) {
291 return;
292 }
293
294 $tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'general';
295
296 $settings_fields = self::settings_fields($tab);
297 $data = $_POST;
298 $update_options = array();
299
300 // Loop options and get values to save.
301 foreach ( $settings_fields as $option ) {
302 if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) {
303 continue;
304 }
305
306 if ( isset( $option['disabled'] ) && $option['disabled'] ) {
307 continue;
308 }
309
310 $option_name = NOTIFIER_PREFIX . $option['id'];
311 $setting_name = '';
312 $raw_value = isset( $data[ $option_name ] ) ? wp_unslash( $data[ $option_name ] ) : null;
313
314 // Format the value based on option type.
315 switch ( $option['type'] ) {
316 case 'checkbox':
317 $value = '1' === $raw_value || 'yes' === $raw_value ? 'yes' : 'no';
318 break;
319 case 'textarea':
320 $value = wp_kses_post( trim( $raw_value ) );
321 break;
322 case 'select':
323 $allowed_values = empty( $option['options'] ) ? array() : array_map( 'strval', array_keys( $option['options'] ) );
324 if ( empty( $option['default'] ) && empty( $allowed_values ) ) {
325 $value = null;
326 break;
327 }
328 $default = ( empty( $option['default'] ) ? $allowed_values[0] : $option['default'] );
329 $value = in_array( $raw_value, $allowed_values, true ) ? $raw_value : $default;
330 break;
331 default:
332 $value = sanitize_text_field( $raw_value );
333 break;
334 }
335
336 // Check if option is an array and handle that differently to single values.
337 if ( $option_name && $setting_name ) {
338 if ( ! isset( $update_options[ $option_name ] ) ) {
339 $update_options[ $option_name ] = get_option( $option_name, array() );
340 }
341 if ( ! is_array( $update_options[ $option_name ] ) ) {
342 $update_options[ $option_name ] = array();
343 }
344 $update_options[ $option_name ][ $setting_name ] = $value;
345 } else {
346 $update_options[ $option_name ] = $value;
347 }
348 }
349
350 // Save all options in our array.
351 foreach ( $update_options as $name => $value ) {
352 if('notifier_api_key' == $name){
353 $current_api_key = get_option($name);
354 if($current_api_key == $value){
355 continue;
356 }
357
358 update_option( NOTIFIER_PREFIX . 'api_key', $value);
359 delete_option( NOTIFIER_PREFIX . 'enabled_triggers' );
360 delete_option( NOTIFIER_PREFIX . 'api_activated' );
361
362 $params = array(
363 'site_url' => site_url(),
364 'source' => 'wp'
365 );
366
367 $response = Notifier::send_api_request( 'verify_api', $params, 'POST' );
368
369 if($response->error){
370 $notices[] = array(
371 'message' => 'There was an error validating API key. Error: ' . $response->message,
372 'type' => 'error'
373 );
374 }
375 else{
376 update_option('notifier_api_activated', 'yes');
377 $notices[] = array(
378 'message' => 'API key validated and saved successfully. Your triggers have been reset. Please enable and save your triggers again from the <a href="'.admin_url('admin.php?page=notifier').'">WA Notifier</a> page.',
379 'type' => 'success'
380 );
381 }
382 }
383 else{
384 update_option( $name, $value, 'yes' );
385 }
386 }
387
388 if(empty($notices)){
389 $notices[] = array(
390 'message' => 'Settings updated successfully.',
391 'type' => 'success'
392 );
393 }
394
395 new Notifier_Admin_Notices($notices);
396 }
397
398 /**
399 * Disconnect WANotifier.com connection
400 */
401 public static function disconnect_notifier(){
402 if(isset($_GET['notifier_disconnect']) && '1' == $_GET['notifier_disconnect']){
403 delete_option( NOTIFIER_PREFIX . 'api_key' );
404 delete_option( NOTIFIER_PREFIX . 'enabled_triggers' );
405 delete_option( NOTIFIER_PREFIX . 'api_activated' );
406 $notices[] = array(
407 'message' => 'Disconnected successfully. To re-connect with WANotifier.com, enter API key below and click on <b>Save and validate</b>.',
408 'type' => 'success'
409 );
410 new Notifier_Admin_Notices($notices, true);
411 wp_redirect(admin_url('admin.php?page=notifier'));
412 }
413 }
414
415 }
416