Last active
April 15, 2024 10:06
-
-
Save artemsites/7c52ed87d1f772a4ca804a7d5e0e8a72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function write_csv_register_rest_route() | |
{ | |
register_rest_route('write_csv/v1', '/process', array( | |
'methods' => 'POST', | |
'callback' => 'write_csv_process_callback', | |
'permission_callback' => 'validate_request_origin' | |
)); | |
} | |
function write_csv_process_callback($request) | |
{ | |
$parameters = $request->get_json_params(); | |
$data = ""; | |
$first = true; | |
foreach ($parameters['data'] as $value) { | |
if ($first) $data .= "{$value}"; | |
else $data .= ",{$value}"; | |
$first = false; | |
} | |
$data.="\n"; | |
file_put_contents(__DIR__ . '/assets/'.$parameters['fileName'], | |
$data | |
,FILE_APPEND); | |
return new WP_REST_Response('success', 200); | |
} | |
add_action('rest_api_init', 'write_csv_register_rest_route'); | |
// validate_request_origin: | |
// https://gist.github.com/artemsites/30a03ed3047e63ccb0db20fd781474e4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment