Skip to content

Commit

Permalink
Fix multiline phones and exceptions on admin config
Browse files Browse the repository at this point in the history
  • Loading branch information
everaldomatias committed Jun 5, 2024
1 parent 084d43f commit 591cb8b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 53 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
92 changes: 41 additions & 51 deletions admin/class-open-whatsapp-chat-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public function create_admin_page() {
settings_fields( 'owc_option_group' );
do_settings_sections( 'open-whatsapp-chat' );
echo '<div class="owc-line">';
$text_button = __( 'Save Settings', 'open-whatsapp-chat' );
submit_button( $text_button );
echo '</div>';
$text_button = __( 'Save Settings', 'open-whatsapp-chat' );
submit_button( $text_button );
echo '</div>';
?>
</form>
</div>
Expand Down Expand Up @@ -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
);
Expand All @@ -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'] ) )
Expand All @@ -138,40 +132,45 @@ 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;

}

/**
* 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 '<textarea id="owc_number" name="owc_option[owc_number]">';
private function sanitize_multiline_input( $input ) {
$input_str = '';

if ( isset( $this->options['owc_number'] ) && is_array( $this->options['owc_number'] ) ) {
foreach( $this->options['owc_number'] as $each ) {
echo $each . "\n";
}
} else {
echo '';
if ( is_string( $input ) ) {
$input_str = $input;
} elseif ( is_array( $input ) ) {
$input_str = implode( "\n", $input );
}

echo '</textarea><span class="owc-desc">' . __( 'Only numbers, example 5511988887777. Add one number per line. When adding more than one number, they will be used sequentially.', 'open-whatsapp-chat' ) . '</span>';
$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 '<textarea id="owc_number" name="owc_option[owc_number]">' . implode( "\n", array_map( 'esc_attr', $numbers ) ) . '</textarea>';
echo '<span class="owc-desc">' . __( 'Only numbers, example 5511988887777. Add one number per line. When adding more than one number, they will be used sequentially.', 'open-whatsapp-chat' ) . '</span>';
}

/**
Expand All @@ -197,21 +196,12 @@ public function owc_message_callback() {
}

public function owc_exceptions_callback() {

echo '<textarea id="owc_exceptions" name="owc_option[owc_exceptions]">';

if ( isset( $this->options['owc_exceptions'] ) ) {
foreach( $this->options['owc_exceptions'] as $each ) {
echo $each . "\n";
}
} else {
echo '';
}

echo '</textarea><span class="owc-desc">' . __( 'Add one URL by line.', 'open-whatsapp-chat' ) . '</span>';

$exceptions = isset( $this->options['owc_exceptions'] ) ? $this->options['owc_exceptions'] : array();
echo '<textarea id="owc_exceptions" name="owc_option[owc_exceptions]">' . implode( "\n", array_map( 'esc_attr', $exceptions ) ) . '</textarea>';
echo '<span class="owc-desc">' . __( 'Add one URL by line.', 'open-whatsapp-chat' ) . '</span>';
}
}

if ( is_admin() )
if ( is_admin() ) {
$owc_settings_page = new Open_Whatsapp_Chat_Settings();
}
4 changes: 2 additions & 2 deletions open-whatsapp-chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' ) ) {
Expand Down

0 comments on commit 591cb8b

Please sign in to comment.