From 591cb8b9a54688369a546b46f207a73392e3ceb5 Mon Sep 17 00:00:00 2001 From: Everaldo Matias Date: Wed, 5 Jun 2024 19:00:29 -0300 Subject: [PATCH] Fix multiline phones and exceptions on admin config --- README.md | 3 + admin/class-open-whatsapp-chat-settings.php | 92 +++++++++------------ open-whatsapp-chat.php | 4 +- 3 files changed, 46 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 1f012dc..13c8d6e 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,9 @@ With this, I keep the plugin here on Github and will continue to improve it acco ## Changelog ## +#### 3.3.2 - 2024-06-05 #### +- Fix multiline phones and exceptions on admin config + #### 3.3.1 - 2023-04-09 #### - Fix PHP error when exceptions is not an array on admin diff --git a/admin/class-open-whatsapp-chat-settings.php b/admin/class-open-whatsapp-chat-settings.php index 9a353ae..6c6304b 100644 --- a/admin/class-open-whatsapp-chat-settings.php +++ b/admin/class-open-whatsapp-chat-settings.php @@ -54,9 +54,9 @@ public function create_admin_page() { settings_fields( 'owc_option_group' ); do_settings_sections( 'open-whatsapp-chat' ); echo '
'; - $text_button = __( 'Save Settings', 'open-whatsapp-chat' ); - submit_button( $text_button ); - echo '
'; + $text_button = __( 'Save Settings', 'open-whatsapp-chat' ); + submit_button( $text_button ); + echo ''; ?> @@ -89,17 +89,17 @@ public function page_init() { ); add_settings_field( - 'owc_button', // ID - __( 'WhatsApp Button Text', 'open-whatsapp-chat' ), // Title - array( $this, 'owc_button_callback' ), // Callback + 'owc_message', // ID + __( 'WhatsApp Message', 'open-whatsapp-chat' ), // Title + array( $this, 'owc_message_callback' ), // Callback 'open-whatsapp-chat', // Page 'setting_section_id' // Section ); add_settings_field( - 'owc_message', // ID - __( 'WhatsApp Message', 'open-whatsapp-chat' ), // Title - array( $this, 'owc_message_callback' ), // Callback + 'owc_button', // ID + __( 'WhatsApp Button Text', 'open-whatsapp-chat' ), // Title + array( $this, 'owc_button_callback' ), // Callback 'open-whatsapp-chat', // Page 'setting_section_id' // Section ); @@ -123,13 +123,7 @@ public function sanitize( $input ) { $new_input = array(); if ( isset( $input['owc_number'] ) ) { - - $owc_numbers = preg_split( '/\r\n|\r|\n/', $input['owc_number'] ); - $owc_numbers = array_filter( $owc_numbers ); - $owc_numbers = array_values( $owc_numbers ); - - $new_input['owc_number'] = $owc_numbers; - + $new_input['owc_number'] = $this->sanitize_multiline_input( $input['owc_number'] ); } if ( isset( $input['owc_button'] ) ) @@ -138,17 +132,8 @@ public function sanitize( $input ) { if ( isset( $input['owc_message'] ) ) $new_input['owc_message'] = sanitize_text_field( $input['owc_message'] ); - if ( isset( $input['owc_exceptions'] ) ) - $new_input['owc_exceptions'] = sanitize_text_field( $input['owc_exceptions'] ); - if ( isset( $input['owc_exceptions'] ) ) { - - $owc_exceptions = preg_split( '/\r\n|\r|\n/', $input['owc_exceptions'] ); - $owc_exceptions = array_filter( $owc_exceptions ); - $owc_exceptions = array_values( $owc_exceptions ); - - $new_input['owc_exceptions'] = $owc_exceptions; - + $new_input['owc_exceptions'] = $this->sanitize_multiline_input( $input['owc_exceptions'] ); } return $new_input; @@ -156,22 +141,36 @@ public function sanitize( $input ) { } /** - * Get the settings option array and print one of its values + * Sanitize multiline input (e.g., phone numbers, exception URLs) + * + * @param mixed $input + * @return array */ - public function owc_number_callback() { - - echo '' . __( 'Only numbers, example 5511988887777. Add one number per line. When adding more than one number, they will be used sequentially.', 'open-whatsapp-chat' ) . ''; + $lines = preg_split( '/\r\n|\r|\n/', $input_str ); + $lines = array_map( 'sanitize_text_field', $lines ); + $lines = array_filter( $lines ); + $lines = array_values( $lines ); + + return $lines; + } + + /** + * Get the settings option array and print one of its values + */ + public function owc_number_callback() { + $numbers = isset( $this->options['owc_number'] ) ? $this->options['owc_number'] : array(); + echo ''; + echo '' . __( 'Only numbers, example 5511988887777. Add one number per line. When adding more than one number, they will be used sequentially.', 'open-whatsapp-chat' ) . ''; } /** @@ -197,21 +196,12 @@ public function owc_message_callback() { } public function owc_exceptions_callback() { - - echo '' . __( 'Add one URL by line.', 'open-whatsapp-chat' ) . ''; - + $exceptions = isset( $this->options['owc_exceptions'] ) ? $this->options['owc_exceptions'] : array(); + echo ''; + echo '' . __( 'Add one URL by line.', 'open-whatsapp-chat' ) . ''; } } -if ( is_admin() ) +if ( is_admin() ) { $owc_settings_page = new Open_Whatsapp_Chat_Settings(); +} diff --git a/open-whatsapp-chat.php b/open-whatsapp-chat.php index f9a37a2..65ae948 100644 --- a/open-whatsapp-chat.php +++ b/open-whatsapp-chat.php @@ -4,7 +4,7 @@ * Plugin Name: Open WhatsApp Chat * Plugin URI: https://github.com/everaldomatias/open-whatsapp-chat * Description: Add a simple button to open WhatsApp chat. - * Version: 3.3.0 + * Version: 3.3.2 * Requires at least: 4.5 * Requires PHP: 7.0 * Author: Everaldo Matias @@ -24,7 +24,7 @@ * Currently plugin version. * Start at version 1.0.0 and use SemVer - https://semver.org */ -define( 'OWC_VERSION', '3.3.0' ); +define( 'OWC_VERSION', '3.3.2' ); define( 'OWC_FILE', __FILE__ ); if ( ! class_exists( 'Open_WhatsApp_Chat' ) ) {