Skip to content

Instantly share code, notes, and snippets.

@artemsites
Last active April 15, 2024 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artemsites/7c52ed87d1f772a4ca804a7d5e0e8a72 to your computer and use it in GitHub Desktop.
Save artemsites/7c52ed87d1f772a4ca804a7d5e0e8a72 to your computer and use it in GitHub Desktop.
<?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