false, 'message' => __('This post is not exists', 'wpmf'))); } $args = array( 'orderby' => 'name', 'order' => 'ASC', 'fields' => 'ids' ); $terms_parent = wp_get_object_terms($post->ID, WPMF_TAXO, $args); $alt_post = get_post_meta($id, '_wp_attachment_image_alt', true); $file_path = get_attached_file($id); if (!file_exists($file_path)) { wp_send_json(array('status' => false, 'message' => __('File does not exist', 'wpmf'))); } $infos_url = pathinfo($post->guid); $mime_type = get_post_mime_type($id); $infos_path = pathinfo($file_path); $name = $infos_path['basename']; $filename = wp_unique_filename($infos_path['dirname'], $name); $upload = copy($file_path, $infos_path['dirname'] . '/' . $filename); if ($upload) { $attachment = array( 'guid' => $infos_url['dirname'] . '/' . $filename, 'post_mime_type' => $mime_type, 'post_title' => $post->post_title, 'post_content' => $post->post_content, 'post_excerpt' => $post->post_excerpt, 'post_status' => 'inherit' ); // insert attachment $attach_id = wp_insert_attachment($attachment, $infos_path['dirname'] . '/' . $filename); // set term if (!empty($terms_parent)) { foreach ($terms_parent as $term_id) { wp_set_object_terms($attach_id, $term_id, WPMF_TAXO, true); /** * Duplicate an attachment * * @param integer Attachment ID * @param integer Target folder */ do_action('wpmf_duplicate_attachment', $attach_id, $term_id); } } $attach_data = wp_generate_attachment_metadata($attach_id, $infos_path['dirname'] . '/' . $filename); wp_update_attachment_metadata($attach_id, $attach_data); update_post_meta($attach_id, '_wp_attachment_image_alt', $alt_post); wp_send_json(array('status' => true, 'message' => __('Duplicated file ', 'wpmf') . $name)); } wp_send_json(array('status' => false, 'message' => __('Error duplicated file', 'wpmf'))); } } }