Skip to content

Commit

Permalink
Enhance channel name handling by adding sanitization methods (#486)
Browse files Browse the repository at this point in the history
* Enhance channel name handling by adding sanitization methods

- Implemented `sanitize_channel_name` method to sanitize channel names before saving or using them.
- Updated the settings registration to include a sanitize callback.
- Modified the default value for the channel name to use the sanitized version of the home URL.
- Overrode `render_field` method to ensure the channel name is sanitized during rendering.

These changes ensure that the default channel name does not include periods which leads to an error creating the channel

* Spacing

Proper spacing for wp standards

* Remove regex
Remove the unnecessary regex checking for the channel name. This PR should just be replacing the periods. There are API-level sanitizations that prevent these characters from being included in channel names.
  • Loading branch information
jazzsequence authored Dec 3, 2024
1 parent 94f714b commit da3384f
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/BigCommerce/Settings/Sections/Channel_Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function register_settings_section() {
register_setting(
Connect_Channel_Screen::NAME,
self::NEW_NAME,
'__return_false'
[ 'sanitize_callback' => [ $this, 'sanitize_channel_name' ] ]
);

add_settings_field(
Expand All @@ -51,7 +51,7 @@ public function register_settings_section() {
[
'type' => 'text',
'option' => self::NEW_NAME,
'default' => parse_url( home_url(), PHP_URL_HOST ),
'default' => $this->sanitize_channel_name( parse_url( home_url(), PHP_URL_HOST ) ),
'class' => 'bc-create-channel-wrapper',
]
);
Expand Down Expand Up @@ -87,4 +87,31 @@ private function get_channel_list() {

return $list;
}
}

/**
* Sanitize the channel name before saving or using
*
* @param string $name The channel name to sanitize
* @return string The sanitized channel name
*/
public function sanitize_channel_name( $name ) {
if ( empty( $name ) ) {
$name = parse_url( home_url(), PHP_URL_HOST );
}
$name = str_replace( '.', '-', $name );
return trim( $name );
}

/**
* Override parent render_field to ensure channel name is sanitized
*/
public function render_field( $args ) {
if ($args['option'] === self::NEW_NAME) {
$args['default'] = $this->sanitize_channel_name($args['default']);
if (isset($_POST[self::NEW_NAME])) {
$_POST[self::NEW_NAME] = $this->sanitize_channel_name($_POST[self::NEW_NAME]);
}
}
parent::render_field($args);
}
}

0 comments on commit da3384f

Please sign in to comment.