PluginProbe
WANotifier for Forms and Actions / 1.0.5
WANotifier for Forms and Actions v1.0.5
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 1.0.5, at includes/classes/class-notifier-settings.php

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