Skip to content

Commit 14b85d0

Browse files
authored
Merge pull request #67 from sectsect/bugfix/fix-action-warning
ci: resolve Plugin Check warning
2 parents 5525ff1 + 2640acd commit 14b85d0

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

functions/functions.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,22 @@ function google_ss2db_save_spreadsheet( array $post_data ): array {
310310
throw $e;
311311
}
312312
}
313+
314+
/**
315+
* Cast mixed value to string.
316+
*
317+
* @param mixed $value Value to cast.
318+
* @return string
319+
* @throws InvalidArgumentException If the value cannot be cast to a string.
320+
*/
321+
function google_ss2db_cast_mixed_to_string( mixed $value ): string {
322+
if ( is_null( $value ) ) {
323+
throw new InvalidArgumentException( 'Value cannot be null' );
324+
}
325+
326+
if ( is_scalar( $value ) ) {
327+
return (string) $value;
328+
}
329+
330+
throw new InvalidArgumentException( 'Value must be a scalar type' );
331+
}

includes/admin.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
declare(strict_types=1);
1616

17+
// Add a constant for the register_setting arguments at the top of the file.
18+
const GOOGLE_SS2DB_SETTING_ARGS = array(
19+
'type' => 'string',
20+
'sanitize_callback' => 'google_ss2db_sanitize_dataformat',
21+
'default' => 'json',
22+
);
23+
1724
/**
1825
* The core plugin class.
1926
*
@@ -84,13 +91,35 @@ function google_ss2db_admin_scripts(): void {
8491
);
8592
}
8693

94+
/**
95+
* Sanitizes the data format option for the Google Spreadsheet to DB plugin.
96+
*
97+
* This callback ensures that only allowed values ('json' or 'json-unescp') are saved.
98+
* If the input is empty or does not match one of the allowed values, an empty string is returned.
99+
*
100+
* @param mixed $value The value to sanitize.
101+
* @return string Sanitized value; returns an empty string if the value is not allowed.
102+
*/
103+
function google_ss2db_sanitize_dataformat( mixed $value ): string {
104+
$value = sanitize_text_field( google_ss2db_cast_mixed_to_string( $value ) );
105+
$allowed = array( 'json', 'json-unescp' );
106+
if ( in_array( $value, $allowed, true ) ) {
107+
return $value;
108+
}
109+
return '';
110+
}
111+
87112
/**
88113
* Registers settings for the Google Spreadsheet to DB plugin within the WordPress settings API.
89114
*
90115
* @return void
91116
*/
92117
function register_google_ss2db_settings(): void {
93-
register_setting( 'google_ss2db-settings-group', 'google_ss2db_dataformat' );
118+
register_setting(
119+
'google_ss2db-settings-group',
120+
'google_ss2db_dataformat',
121+
GOOGLE_SS2DB_SETTING_ARGS
122+
);
94123
}
95124

96125
/**

0 commit comments

Comments
 (0)