Skip to content

Commit

Permalink
Merge pull request #60 from sectsect/feature/debug
Browse files Browse the repository at this point in the history
feat(debug): add configurable debug mode for spreadsheet save process
  • Loading branch information
sectsect authored Nov 30, 2024
2 parents f1b0bb7 + 55a91e6 commit 9d437cc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,23 @@ $args = array(

* This Plugin does not hosting on the [wordpress.org](https://wordpress.org/) repo in order to prevent a flood of support requests from wide audience.

## Debug Mode

To enable debug mode, add the following constant to your `wp-config.php`:

```php
define( 'GOOGLE_SS2DB_DEBUG', true );
```

When debug mode is enabled, importing a spreadsheet will return a detailed JSON response instead of the usual redirect. The response includes the following information:

- `result`: A boolean indicating the success or failure of the process
- `data`: Details of the saved data
- `post_data`: Sanitized post data
- `referer`: The redirect URL

**Note**: Always set `GOOGLE_SS2DB_DEBUG` to `false` or remove the constant in production environments.

## Troubleshooting

This plugin depends on [Guzzle](https://github.com/guzzle/guzzle) **v7**, which may conflict with other WordPress plugins or Composer packages using Guzzle **v6**.
Expand Down
25 changes: 23 additions & 2 deletions includes/save.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,34 @@
wp_die( 'Our Site is protected!!' );
}

// Sanitize all POST data.
$sanitized_post_data = array_map( fn( $value ) => is_string( $value ) ? sanitize_text_field( $value ) : $value, $_POST );
$data = google_ss2db_save_spreadsheet( $sanitized_post_data );
$data = apply_filters( 'google_ss2db_after_save', $data );

// Process and save spreadsheet data.
$data = google_ss2db_save_spreadsheet( $sanitized_post_data );
$data = apply_filters( 'google_ss2db_after_save', $data );

$bool = (bool) $data['result'];
$referer = wp_unslash( $http_referer );
$referer = str_replace( '&settings-updated=true', '', $referer );
$referer = $referer . '&ss2dbupdated=' . $bool;

// Check if debug mode is enabled.
if ( defined( 'GOOGLE_SS2DB_DEBUG' ) && true === GOOGLE_SS2DB_DEBUG ) {
// Return detailed debug information as JSON.
header( 'Content-Type: application/json' );
echo wp_json_encode(
array(
'result' => $bool,
'data' => $data,
'post_data' => $sanitized_post_data,
'referer' => $referer,
),
JSON_PRETTY_PRINT
);
exit;
}

// Default redirect for non-debug mode.
wp_redirect( $referer );
exit;

0 comments on commit 9d437cc

Please sign in to comment.