Skip to content

Commit

Permalink
Merge pull request #49 from sectsect/feature/refactor-june2024
Browse files Browse the repository at this point in the history
refactor: code refactor
  • Loading branch information
sectsect committed Jun 16, 2024
2 parents eb93d01 + f8e34f3 commit 8109b2e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ Hit send. That’s it! :ok_hand:
You can edit the array got from Google API with `add_filter( 'google_ss2db_before_save', $function_to_add )` in your functions.php before saving to database.

```php
add_filter( 'google_ss2db_before_save', function ( $row, $worksheetid, $worksheetname, $sheetname ) {
add_filter( 'google_ss2db_before_save', function ( $row, $worksheet_id, $worksheet_name, $sheet_name ) {
// Example
if ( $worksheetname === 'My Spreadsheet' && $sheetname === 'Sheet1' ) {
if ( $worksheet_name === 'My Spreadsheet' && $sheet_name === 'Sheet1' ) {
// Do something.

return $something;
Expand Down
24 changes: 11 additions & 13 deletions includes/save.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,28 @@ function get_client(): Google_Client {
/**
* Retrieves data from a specified Google Spreadsheet.
*
* @param string $worksheetid The ID of the Google Spreadsheet.
* @param string $worksheetname The name of the Google Spreadsheet.
* @param string $sheetname The name of the individual sheet within the Spreadsheet.
* @param bool $hasheaderrow Indicates if the spreadsheet contains a header row.
* @param string $worksheet_id The ID of the Google Spreadsheet.
* @param string $worksheet_name The name of the Google Spreadsheet.
* @param string $sheet_name The name of the individual sheet within the Spreadsheet.
* @param bool $has_header_row Indicates if the spreadsheet contains a header row.
* @return array<string, mixed>|bool The spreadsheet data as an associative array if successful, or false if the sheet does not exist.
*/
function get_value_google_spreadsheet( string $worksheetid, string $worksheetname, string $sheetname, bool $hasheaderrow ): array|bool {
function get_value_google_spreadsheet( string $worksheet_id, string $worksheet_name, string $sheet_name, bool $has_header_row ): array|bool {
$client = get_client();
$service = new Google_Service_Sheets( $client );

$spreadsheet_id = $worksheetid;
$range = $sheetname;
$spreadsheet_id = $worksheet_id;
$range = $sheet_name;

$response = $service->spreadsheets->get( $spreadsheet_id );
$sheets = $response->getSheets();
$object = array(); // Initialize $object to prevent undefined variable issues.
if ( exist_sheet( $sheets, $sheetname ) ) {
if ( exist_sheet( $sheets, $sheet_name ) ) {
$response = $service->spreadsheets_values->get( $spreadsheet_id, $range );
$values = $response->getValues();

if ( ! empty( $values ) ) {
if ( $hasheaderrow ) {
if ( $has_header_row ) {
$header_row = $values[0];
// Remove the header row.
unset( $values[0] );
Expand All @@ -114,7 +114,7 @@ function get_value_google_spreadsheet( string $worksheetid, string $worksheetnam
$object = false;
}

$array = apply_filters( 'google_ss2db_before_save', $object, $worksheetid, $worksheetname, $sheetname );
$array = apply_filters( 'google_ss2db_before_save', $object, $worksheet_id, $worksheet_name, $sheet_name );

return $array;
}
Expand All @@ -139,9 +139,7 @@ function save_spreadsheet(): array {
$sheet_name = wp_unslash( $_POST['sheetname'] ?? '' );
$has_header_row = wp_unslash( $_POST['hasheaderrow'] ?? false );
$value = get_value_google_spreadsheet( $worksheet_id, $worksheet_name, $sheet_name, $has_header_row );

$value = get_value_google_spreadsheet( $worksheet_id, $worksheet_name, $sheet_name, $has_header_row );
$value = json_encode( $value, get_option( 'google_ss2db_dataformat' ) === 'json-unescp' ? JSON_UNESCAPED_UNICODE : 0 );
$value = json_encode( $value, get_option( 'google_ss2db_dataformat' ) === 'json-unescp' ? JSON_UNESCAPED_UNICODE : 0 );

$result = $wpdb->insert(
GOOGLE_SS2DB_TABLE_NAME,
Expand Down

0 comments on commit 8109b2e

Please sign in to comment.