У меня есть магазин WooCommerce, где все продукты находятся в iframe. Когда вы нажимаете кнопку «Добавить в корзину», iframe отправит файл, в который, скорее всего, будет помещена фотография. Мне нужно отобразить этот файл в порядке деталей в admin. У меня есть file_storage. php, в котором зарегистрированы методы POST и GET
<?php
const FILES_SUBDIR = 'files/';
function idea_api_upload_file(WP_REST_Request $request) {
if (!empty($request->get_body())) {
$upload_dir = idea_get_upload_dir().FILES_SUBDIR;
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
$id = time();
$filename = $upload_dir.$id.'.json';
$success = file_put_contents($filename, $request->get_body());
if ($success)
$response = new WP_REST_Response(array(
'id' => $id,
'link' => rest_url('idea/v1/get_file/'.$id),
'size' => filesize($filename)
));
else
$response = new WP_REST_Response(array('error' => 'Unable to store file'), 500);
} else {
$response = new WP_REST_Response(array('error' => 'Bad request'), 400);
}
/*if (get_option('v3d_cross_domain'))
$response->header('Access-Control-Allow-Origin', '*');*/
return $response;
}
function idea_api_get_file(WP_REST_Request $request) {
$id = intval($request->get_param('id'));
if (!empty($id)) {
$upload_dir = idea_get_upload_dir().FILES_SUBDIR;
$file = $upload_dir.$id.'.json';
if (is_file($file)) {
// hack to prevent excessive JSON encoding
header('Content-Type: application/json');
if (get_option('v3d_cross_domain'))
header('Access-Control-Allow-Origin: *');
print_r(file_get_contents($file));
exit();
} else
$response = new WP_REST_Response(array('error' => 'File not found'), 500);
} else {
$response = new WP_REST_Response(array('error' => 'Bad request'), 400);
}
/*if (get_option('v3d_cross_domain'))
$response->header('Access-Control-Allow-Origin', '*');*/
return $response;
}
add_action('rest_api_init', function () {
register_rest_route('idea/v1', '/upload_file', array(
'methods' => 'POST',
'callback' => 'idea_api_upload_file',
));
register_rest_route('idea/v1', '/get_file/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'idea_api_get_file',
'args' => array(
'id' => array(
'validate_callback' => function($param, $request, $key) {
return is_numeric($param);
}
),
),
));
});
Все файлы сохраняются в папке 'idea'
function idea_get_upload_dir() {
$upload_dir = wp_upload_dir()['basedir'].'/idea/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
return $upload_dir;
}
Но я не понимаю, как разместить файл в порядке (админка). Помогите мне пожалуйста