/**
 * saves attachment order using "menu_order" field
 *
 * @since 1.0
 */
function file_gallery_save_menu()
{
    global $wpdb;
    $updates = '';
    check_ajax_referer('file-gallery');
    $order = explode(',', $_POST['attachment_order']);
    foreach ($order as $mo => $ID) {
        $updates .= sprintf(" WHEN %d THEN %d ", $ID, $mo);
    }
    if (false !== $wpdb->query("UPDATE {$wpdb->posts} SET `menu_order` = CASE `ID` " . $updates . " ELSE `menu_order` END")) {
        echo __('Attachment order saved successfully.', 'file-gallery');
    } else {
        $error = __('Database error! Function: file_gallery_save_menu', 'file-gallery');
        file_gallery_write_log($error);
        echo $error;
    }
    exit;
}
Ejemplo n.º 2
0
/**
 * copies all attachments from one post to another
 */
function file_gallery_copy_all_attachments()
{
    global $wpdb;
    $from_id = $_POST['from_id'];
    $to_id = $_POST['to_id'];
    $thumb_id = false;
    if (!is_numeric($from_id) || !is_numeric($to_id) || 0 === $from_id || 0 === $to_id) {
        exit('ID not numeric or zero! (file_gallery_copy_all_attachments)');
    }
    $attachments = $wpdb->get_results(sprintf("SELECT `ID` FROM {$wpdb->posts} WHERE `post_type`='attachment' AND `post_parent`=%d", $from_id));
    if (false === $attachments) {
        $error = __('Database error! (file_gallery_copy_all_attachments)', 'file-gallery');
        file_gallery_write_log($error);
        exit($error);
    }
    if (0 === count($attachments)) {
        exit(sprintf(__('Uh-oh. No attachments were found for post ID %d.', 'file-gallery'), $from_id));
    }
    // if the post we're copying all the attachments to has no attachments...
    if (0 === count($wpdb->get_results($wpdb->prepare("SELECT `ID` FROM {$wpdb->posts} WHERE `post_type`='attachment' AND `post_parent`=%d", $to_id)))) {
        $thumb_id = get_post_meta($from_id, '_thumbnail_id', true);
        // ...automatically set the original post's thumb to the new one
    }
    do_action('file_gallery_copy_all_attachments', $from_id, $to_id);
    foreach ($attachments as $aid) {
        $r = file_gallery_copy_attachment_to_post($aid->ID, $to_id);
        if (-1 === $r) {
            $errors[] = $aid->ID;
        }
        // set post thumb
        if ($aid->ID === $thumb_id) {
            update_post_meta($to_id, '_thumbnail_id', $r);
        }
    }
    if (!isset($errors)) {
        echo sprintf(__('All attachments were successfully copied from post %d.', 'file-gallery'), $from_id);
    } else {
        echo 'error ids: ' . implode(', ', $errors);
    }
    exit;
}