Esempio n. 1
0
 public function publish_to_WordPress($title, $content, $author = false, $categories = false, $custom_fields = false)
 {
     //Find out if we are creating a draft or updating a doc
     $post_id = $this->post_exists_by_meta('_gdocID', $custom_fields['_gdocID']);
     //Find out if the collections the doc is in matches any categories
     $cats = array();
     foreach ($categories as $category) {
         $cat = term_exists($category, 'category');
         if (!empty($cat)) {
             $cats[] = $cat['term_id'];
         }
         if (empty($cats)) {
             $cats[] = get_option('default_link_category');
         }
     }
     //If the username in gdocs matches the username in WordPress, it will automatically apply the correct username
     $author_data = get_userdatabylogin($author);
     $author = $author_data->ID;
     $post_array = array('post_title' => $title, 'post_content' => $content, 'custom_fields' => $custom_fields);
     if (empty($post_id)) {
         $post_array = array_merge($post_array, array('post_author' => $author, 'post_category' => $cats));
     } else {
         $post_array = array_merge($post_array, array('ID' => $post_id));
     }
     //If you want all posts to be auto-published, for example, you can add a filter here
     $post_array = apply_filters('pre_docs_to_wp_insert', $post_array);
     gdocs_log($post_array['post_content']);
     //Add or update
     if (empty($post_id)) {
         gdocs_log("Added: ", "action");
         $post_id = wp_insert_post($post_array);
         // We need to insert images here as they require the post id to be set as attachments
         if (has_filter('attach_images_docs_to_wp_insert')) {
             $post_array['ID'] = $post_id;
             $post_array = apply_filters('attach_images_docs_to_wp_insert', $post_array);
             //MF
             $post_id = wp_update_post($post_array);
             // update again due to changes after we know the post ID
             gdocs_log("Had to run update on post again to insert images");
         }
     } else {
         gdocs_log("Updated: ", "action");
         $post_array = apply_filters('attach_images_docs_to_wp_insert', $post_array);
         //MF
         $post_id = wp_update_post($post_array);
     }
     //Update post meta, including the _gdocID field
     foreach ($post_array['custom_fields'] as $key => $value) {
         update_post_meta($post_id, $key, $value);
     }
     return $post_id;
 }
function dwtp_attach_images($post_array)
{
    global $image_settings;
    $post_id = $post_array['ID'];
    $post_content = $post_array['post_content'];
    if (empty($post_id)) {
        gdocs_log("Post ID was null when trying to insert images", "error");
        return false;
    }
    $attached_guids = array();
    if (!empty($post_id) && ($images = get_posts(array('post_parent' => $post_id, 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC')))) {
        foreach ($images as $image) {
            $attached_images[] = get_image_send_to_editor($image->ID, '', $image->post_title, $image_settings['image_alignment'], wp_get_attachment_url($image->ID), FALSE, $image_settings['image_size'], $image->post_content);
            if (preg_match('/gdocs.{10}_/', $image->guid, $guid_match) > 0) {
                //match guids with the gdocs id inserted
                $attached_guids[] = $guid_match[0];
                //for identifying gdocs images added since before
            } else {
                $attached_guids[] = $image->guid;
            }
        }
    }
    //MF
    preg_match_all('/<img(.*?)>/', $post_content, $doc_imgs, PREG_OFFSET_CAPTURE);
    gdocs_log("Image GUIDs: " . implode($attached_guids), "debug");
    $upload_dir = wp_upload_dir();
    $path = $upload_dir['path'] . '/' . $image_settings['gdocs_image_folder'];
    //assumed subdir of upload
    if (!file_exists($path)) {
        mkdir($path);
    }
    $replace_offset = 0;
    foreach ($doc_imgs[0] as $doc_img) {
        //$doc_imgs[0] because we only need first pattern, not the subpattern
        preg_match('/src="https(.*?)"/', $doc_img[0], $src_match);
        //Use doc_img[0] as we also match position in index [1]
        $img_hash = 'gdocs' . substr($src_match[1], -10) . '_';
        // Pick file name hash from last 10 of gdocs user content hash (hope it's static!)
        $new_img_tag = '';
        $existing_img = array_search($img_hash, $attached_guids);
        // see if we already have this img as attachment
        if ($existing_img === FALSE) {
            // Gdocs defaults to HTTPSing their images, but we can go with normal http for simplicity
            $headers = wp_get_http('http' . $src_match[1], $path . $img_hash);
            $contentdisposition = explode('"', $headers["content-disposition"]);
            //pattern is: inline;filename="xxx.jpg"
            if (count($contentdisposition) > 1) {
                $filename = urldecode($contentdisposition[1]);
                // filename may include URL characters that mess up later
                $file_ext = strripos($filename, '.');
                // look for extension from end of string
                $name = $file_ext > 0 ? substr($filename, 0, $file_ext) : $filename;
                //strip out file type from name
            } else {
                $filename = 'unknown.jpg';
                $name = "unknown";
            }
            $filename = $img_hash . $filename;
            // for uniqueness combine with hash
            $newpath = $path . $filename;
            rename($path . $img_hash, $newpath);
            $wp_filetype = wp_check_filetype(basename($newpath), null);
            $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => $name, 'post_name' => $name, 'guid' => $upload_dir['baseurl'] . '/gdocs/' . $filename, 'post_content' => '', 'post_status' => 'inherit');
            $attach_id = wp_insert_attachment($attachment, $newpath, $post_id);
            $attach_data = wp_generate_attachment_metadata($attach_id, $newpath);
            wp_update_attachment_metadata($attach_id, $attach_data);
            gdocs_log("Inserted attachment " . implode($attachment) . " to post {$post_id} and generated metadata:", "debug");
            $new_img_tag = get_image_send_to_editor($attach_id, '', $name, $image_settings['image_alignment'], wp_get_attachment_url($image->ID), FALSE, $image_settings['image_size'], '');
            gdocs_log("Downloaded https" . $src_match[1] . " as {$filename} (content-disp: " . $headers["content-disposition"] . ", hash {$img_hash} not found, index {$existing_img})\n", "debug");
        } else {
            $new_img_tag = $attached_images[$existing_img];
            gdocs_log("{$img_hash} already downloaded (index in guid: {$existing_img})\n", "debug");
        }
        // as we replace in post_content the originally matched positions will be offsetted, so we compensate
        $post_content = substr_replace($post_content, $new_img_tag, $doc_img[1] + $replace_offset, strlen($doc_img[0]));
        $replace_offset = $replace_offset + (strlen($new_img_tag) - strlen($doc_img[0]));
    }
    $post_array['post_content'] = $post_content;
    return $post_array;
}