Skip to content

Commit da3384f

Browse files
authored
Enhance channel name handling by adding sanitization methods (#486)
* 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.
1 parent 94f714b commit da3384f

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/BigCommerce/Settings/Sections/Channel_Select.php

+30-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function register_settings_section() {
3131
register_setting(
3232
Connect_Channel_Screen::NAME,
3333
self::NEW_NAME,
34-
'__return_false'
34+
[ 'sanitize_callback' => [ $this, 'sanitize_channel_name' ] ]
3535
);
3636

3737
add_settings_field(
@@ -51,7 +51,7 @@ public function register_settings_section() {
5151
[
5252
'type' => 'text',
5353
'option' => self::NEW_NAME,
54-
'default' => parse_url( home_url(), PHP_URL_HOST ),
54+
'default' => $this->sanitize_channel_name( parse_url( home_url(), PHP_URL_HOST ) ),
5555
'class' => 'bc-create-channel-wrapper',
5656
]
5757
);
@@ -87,4 +87,31 @@ private function get_channel_list() {
8787

8888
return $list;
8989
}
90-
}
90+
91+
/**
92+
* Sanitize the channel name before saving or using
93+
*
94+
* @param string $name The channel name to sanitize
95+
* @return string The sanitized channel name
96+
*/
97+
public function sanitize_channel_name( $name ) {
98+
if ( empty( $name ) ) {
99+
$name = parse_url( home_url(), PHP_URL_HOST );
100+
}
101+
$name = str_replace( '.', '-', $name );
102+
return trim( $name );
103+
}
104+
105+
/**
106+
* Override parent render_field to ensure channel name is sanitized
107+
*/
108+
public function render_field( $args ) {
109+
if ($args['option'] === self::NEW_NAME) {
110+
$args['default'] = $this->sanitize_channel_name($args['default']);
111+
if (isset($_POST[self::NEW_NAME])) {
112+
$_POST[self::NEW_NAME] = $this->sanitize_channel_name($_POST[self::NEW_NAME]);
113+
}
114+
}
115+
parent::render_field($args);
116+
}
117+
}

0 commit comments

Comments
 (0)