function wr2x_wp_ajax_wr2x_replace() { $tmpfname = wr2x_check_get_ajax_uploaded_file(); $attachmentId = (int) $_POST['attachmentId']; $meta = wp_get_attachment_metadata($attachmentId); $current_file = get_attached_file($attachmentId); wr2x_delete_attachment($attachmentId); $pathinfo = pathinfo($current_file); $basepath = $pathinfo['dirname']; // Let's clean everything first if (wp_attachment_is_image($attachmentId)) { $sizes = wr2x_get_image_sizes(); foreach ($sizes as $name => $attr) { if (isset($meta['sizes'][$name]) && isset($meta['sizes'][$name]['file']) && file_exists(trailingslashit($basepath) . $meta['sizes'][$name]['file'])) { $normal_file = trailingslashit($basepath) . $meta['sizes'][$name]['file']; $pathinfo = pathinfo($normal_file); $retina_file = trailingslashit($pathinfo['dirname']) . $pathinfo['filename'] . wr2x_retina_extension() . $pathinfo['extension']; // Test if the file exists and if it is actually a file (and not a dir) // Some old WordPress Media Library are sometimes broken and link to directories if (file_exists($normal_file) && is_file($normal_file)) { unlink($normal_file); } if (file_exists($retina_file) && is_file($retina_file)) { unlink($retina_file); } } } } if (file_exists($current_file)) { unlink($current_file); } // Insert the new file and delete the temporary one rename($tmpfname, $current_file); chmod($current_file, 0644); // Generate the images wp_update_attachment_metadata($attachmentId, wp_generate_attachment_metadata($attachmentId, $current_file)); $meta = wp_get_attachment_metadata($attachmentId); wr2x_generate_images($meta); // Get the results $info = wr2x_retina_info($attachmentId); $results[$attachmentId] = wpr2x_html_get_basic_retina_info($attachmentId, $info); echo json_encode(array('success' => true, 'results' => $results, 'message' => __("Replaced successfully.", 'wp-retina-2x'))); die; }
function wr2x_wp_ajax_wr2x_replace() { if (!current_user_can('upload_files')) { echo json_encode(array('success' => false, 'message' => __("You do not have permission to upload files.", 'wp-retina-2x'))); die; } $data = $_POST['data']; // Create the file as a TMP $tmpfname = tempnam(sys_get_temp_dir(), "wpx_"); if ($tmpfname == FALSE) { $tmpdir = sys_get_temp_dir(); if (!is_writable($tmpdir)) { echo json_encode(array('success' => false, 'message' => __("You don't have the rights to use a temporary directory.", 'wp-retina-2x'))); } else { echo json_encode(array('success' => false, 'message' => __("The temporary directory could not be created.", 'wp-retina-2x'))); } die; } $handle = fopen($tmpfname, "w"); fwrite($handle, base64_decode($data)); fclose($handle); // Check if it is an image $file_info = getimagesize($tmpfname); if (empty($file_info)) { unlink($tmpfname); echo json_encode(array('success' => false, 'message' => __("The file is not an image or the upload went wrong.", 'wp-retina-2x'))); die; } $filedata = wp_check_filetype_and_ext($tmpfname, $_POST['filename']); if ($filedata["ext"] == "") { unlink($current_file); echo json_encode(array('success' => false, 'message' => __("You cannot use this file (wrong extension? wrong type?).", 'wp-retina-2x'))); die; } $attachmentId = (int) $_POST['attachmentId']; $meta = wp_get_attachment_metadata($attachmentId); $current_file = get_attached_file($attachmentId); wr2x_delete_attachment($attachmentId); $pathinfo = pathinfo($current_file); $basepath = $pathinfo['dirname']; // Let's clean everything first if (wp_attachment_is_image($attachmentId)) { $sizes = wr2x_get_image_sizes(); foreach ($sizes as $name => $attr) { if (isset($meta['sizes'][$name]) && isset($meta['sizes'][$name]['file']) && file_exists(trailingslashit($basepath) . $meta['sizes'][$name]['file'])) { $normal_file = trailingslashit($basepath) . $meta['sizes'][$name]['file']; $pathinfo = pathinfo($normal_file); $retina_file = trailingslashit($pathinfo['dirname']) . $pathinfo['filename'] . wr2x_retina_extension() . $pathinfo['extension']; // Test if the file exists and if it is actually a file (and not a dir) // Some old WordPress Media Library are sometimes broken and link to directories if (file_exists($normal_file) && is_file($normal_file)) { unlink($normal_file); } if (file_exists($retina_file) && is_file($retina_file)) { unlink($retina_file); } } } } if (file_exists($current_file)) { unlink($current_file); } // Insert the new file and delete the temporary one rename($tmpfname, $current_file); chmod($current_file, 0644); // Generate the images wp_update_attachment_metadata($attachmentId, wp_generate_attachment_metadata($attachmentId, $current_file)); $meta = wp_get_attachment_metadata($attachmentId); wr2x_generate_images($meta); // Get the results $info = wr2x_retina_info($attachmentId); $results[$attachmentId] = $info; echo json_encode(array('success' => true, 'results' => $results, 'message' => __("Replaced successfully.", 'wp-retina-2x'))); die; }