Example #1
0
function exchange_set_attachments_post_tag($post_id, $post, $update)
{
    $attachments = get_children(array('post_parent' => $post_id, 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => 'any'));
    if (count($attachments) > 0) {
        $tag = exchange_check_for_post_tag($post_id);
        foreach ($attachments as $a) {
            exchange_set_tax_to_match_term($a->ID, 'post_tag', $tag);
        }
    }
}
function exchange_image_importer()
{
    $result = array();
    $collab_args = array('post_type' => 'collaboration', 'posts_per_page' => -1, 'post_status' => 'publish');
    $query = new WP_Query($collab_args);
    $collabs = $query->posts;
    $upload_path = wp_get_upload_dir();
    if (!empty($collabs)) {
        $fail = 0;
        $success = 0;
        foreach ($collabs as $collab) {
            //print_r( $collab );
            // lookup collab ID
            $name = $collab->post_name;
            // set as metavalue
            $name_lower = sanitize_title(strtolower($name));
            $name_clean = str_replace('-', '_', $name_lower);
            $path = ABSPATH . 'collaborations/' . $name_clean . '.jpg';
            $file_clean = $name_clean . '.jpg';
            $path_clean = ABSPATH . 'collaborations/' . $file_clean;
            $post_id = $collab->ID;
            $post_tag = exchange_get_post_tag_from_parent_id($post_id);
            $post_tag_folder = $post_tag->slug;
            $upload_path_clean = $upload_path['path'] . '/' . $post_tag_folder . '/' . $file_clean;
            echo 'post_id: ' . $post_id . '<br />';
            echo 'path: ' . $path . '<br />';
            echo 'file_clean: ' . $file_clean . '<br />';
            echo 'path_clean: ' . $path_clean . '<br />';
            echo 'upload_path_clean: ' . $upload_path_clean . '<br />';
            if (file_exists($path_clean)) {
                if (has_post_thumbnail($post_id)) {
                    echo "yes<br />";
                } else {
                    rename($path_clean, $upload_path_clean);
                    // Check the type of file. We'll use this as the 'post_mime_type'.
                    $filetype = wp_check_filetype($upload_path_clean, null);
                    // Prepare an array of post data for the attachment.
                    $attachment = array('guid' => $upload_path_clean, 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', $collab->post_title), 'post_content' => '', 'post_status' => 'inherit');
                    print_r($attachment);
                    //
                    // // Insert the attachment.
                    $attach_id = wp_insert_attachment($attachment, $upload_path_clean, $post_id);
                    //
                    // // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
                    require_once ABSPATH . 'wp-admin/includes/image.php';
                    //
                    // // Generate the metadata for the attachment, and update the database record.
                    $attach_data = wp_generate_attachment_metadata($attach_id, $upload_path_clean);
                    wp_update_attachment_metadata($attach_id, $attach_data);
                    //
                    set_post_thumbnail($post_id, $attach_id);
                    exchange_check_for_post_tag($attach_id);
                }
            } else {
                echo "no<br />";
                $fail++;
            }
        }
        echo "<hr>" . $success . " successes and " . $fail . " failures.";
    }
}