Я добавил свой пользовательский тип сообщения через форму.Этот CPT включает в себя фотогалерею ACF.Добавление работы приятно.но когда я хочу отредактировать сообщение, галерея ACF возвращает только новые обновленные изображения в формате: base64, (. *) / ...
Функция добавления сообщения:
function addAdvertisement() {
header('Content-Type: application/html;charset=utf-8');
$uploadDir = wp_upload_dir();
// Create post object
$my_post = array(
'post_title' => $_POST['name'],
'post_content' => 'Some testing content',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'advertisements'
);
$post_id = wp_insert_post( $my_post);
$uploadedImages = [];
foreach ($_POST['image'] as $encodedImage) {
$encodedImage = str_replace('\"', '"', $encodedImage);
$image = json_decode($encodedImage, true);
$type = wp_check_filetype($image['name'], null);
if (!in_array($type['ext'], ['jpg', 'jpeg', 'png'])) {
continue;
}
$filename = sprintf(
"%s.%s",
hash('md5', random_bytes(32)),
$type['ext']
);
$imageMatches = [];
preg_match('/base64,(.*)/', $image['src'], $imageMatches);
if (!isset($imageMatches[1])) {
continue;
}
$destination = $uploadDir['path'] . DIRECTORY_SEPARATOR . $filename;
file_put_contents($destination, base64_decode($imageMatches[1]));
$uploadedImage = sprintf(
"%s%s/%s",
$uploadDir['baseurl'],
$uploadDir['subdir'],
$filename
);
$attachment = array(
'guid' => $uploadedImage,
'post_mime_type' => $type['type'],
'post_title' => $filename,
'post_name' => $filename,
'post_content' => '',
'post_status' => 'inherit'
);
$uploadedImages[] = wp_insert_attachment(
$attachment,
$destination,
$post_id
);
}
// Insert the post into the database
add_post_meta($post_id, 'popis_inzeratu', $_POST['description'], true);
add_post_meta($post_id, 'fotka', implode(',', $uploadedImages));
add_post_meta($post_id, 'id_inzerat', $post_id);
echo get_permalink($post_id);
// echo json_encode($uploadedImages);
wp_die();
}
функция для редактированиямой пост:
function editAdvertisement() {
header('Content-Type: application/html;charset=utf-8');
$uploadDir = wp_upload_dir();
// Create post object
$my_post = array(
'ID' => $_POST['id'],
'post_title' => $_POST['name'],
'post_content' => 'Some testing content',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'advertisements'
);
$post_id = wp_update_post($my_post);
$uploadedImages = [];
foreach ($_POST['image'] as $encodedImage) {
$encodedImage = str_replace('\"', '"', $encodedImage);
$image = json_decode($encodedImage, true);
$type = wp_check_filetype($image['name'], null);
if (!in_array($type['ext'], ['jpg', 'jpeg', 'png'])) {
continue;
}
$filename = sprintf(
"%s.%s",
hash('md5', random_bytes(32)),
$type['ext']
);
$imageMatches = [];
preg_match('/base64,(.*)/', $image['src'], $imageMatches);
if (!isset($imageMatches[1])) {
continue;
}
$destination = $uploadDir['path'] . DIRECTORY_SEPARATOR . $filename;
file_put_contents($destination, base64_decode($imageMatches[1]));
$uploadedImage = sprintf(
"%s%s/%s",
$uploadDir['baseurl'],
$uploadDir['subdir'],
$filename
);
$attachment = array(
'guid' => $uploadedImage,
'post_mime_type' => $type['type'],
'post_title' => $filename,
'post_name' => $filename,
'post_content' => '',
'post_status' => 'inherit'
);
$uploadedImages = wp_insert_attachment(
$attachment,
$destination,
$post_id
);
}
// Update the post into the database
// $newArray = array_merge($uploadedImages, $arrayExistedId);
update_post_meta($post_id, 'popis_inzeratu', $_POST['description'], true);
update_post_meta($post_id, 'fotka', implode(',', $uploadedImages));
update_post_meta($post_id, 'id_inzerat', $post_id);
// echo get_permalink($post_id);
echo get_permalink($post_id);
wp_die();
}
Проблема в том, что в галерее уже установлены картинки src: wp-content / uploads / ... уже есть в библиотеке вложений.Спасибо за любой совет