Plugin Directory

Changeset 773031


Ignore:
Timestamp:
09/16/2013 02:27:19 AM (13 years ago)
Author:
codebykat
Message:

added support for attachments

Location:
post-by-email/trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • post-by-email/trunk/class-post-by-email.php

    r773030 r773031  
    381381            add_post_meta( $post_ID, 'original_author', $from_email );
    382382
     383            /* attachments */
     384            $attachment_count = $this->save_attachments( $uid, $post_ID );
     385            if ( $attachment_count > 0 ) {
     386                // add gallery to posts with attachments
     387                $post_info = array(
     388                    'ID' => $post_ID,
     389                    'post_content' => $post_content . '[gallery]'
     390                );
     391                wp_update_post( $post_info );
     392            }
     393
    383394            do_action( 'publish_phone', $post_ID );
    384395
     
    625636
    626637    /**
     638     * Get a message's attachments from the mail server and associate them with the post.
     639     *
     640     * @since    1.0.3
     641     *
     642     * @param    int       Message UID
     643     * @param    int       ID of the Post to attach to
     644     *
     645     * @return   int       Number of attachments saved
     646     */
     647    protected function save_attachments( $uid, $postID ) {
     648        $query = new Horde_Imap_Client_Fetch_Query();
     649        $query->structure();
     650
     651        $list = $this->connection->fetch( 'INBOX', $query, array(
     652            'ids' => $uid
     653        ));
     654
     655        $part = $list->first()->getStructure();
     656        $map = $part->ContentTypeMap();
     657
     658        $attachment_count = 0;
     659        $post_thumbnail = false;
     660
     661        foreach ( $map as $key => $value ) {
     662            $p = $part->getPart( $key );
     663
     664            if ( 'attachment' == $p->getDisposition() ) {
     665                $mime_id = $key;
     666                $filename = $p->getName();
     667                $filetype = $p->getType();
     668
     669                $query2 = new Horde_Imap_Client_Fetch_Query();
     670                $query2->bodyPart( $mime_id, array(
     671                    'decode' => true,
     672                    'peek' => true
     673                ));
     674
     675                $list2 = $this->connection->fetch( 'INBOX', $query2, array(
     676                    'ids' => $uid
     677                ));
     678
     679                $message = $list2->first();
     680
     681                $image_data = $message->getBodyPart( $mime_id );
     682                $image_data_decoded = base64_decode( $image_data );
     683
     684                $upload_dir = wp_upload_dir();
     685                $directory = $upload_dir['basedir'] . $upload_dir['subdir'];
     686
     687                wp_mkdir_p( $directory );
     688                file_put_contents( $directory . '/' . $filename, $image_data_decoded );
     689
     690                // add attachment to the post
     691                $attachment_args = array(
     692                    'post_title' => $filename,
     693                    'post_content' => '',
     694                    'post_status' => 'publish',
     695                    'post_mime_type' => $filetype,
     696                );
     697
     698                $attachment_id = wp_insert_attachment( $attachment_args, $directory . '/' . $filename, $postID );
     699                $attachment_metadata = wp_generate_attachment_metadata( $attachment_id, $directory . '/' . $filename );
     700                wp_update_attachment_metadata( $attachment_id, $attachment_metadata );
     701                $attachment_count++;
     702
     703                // make the first image attachment the featured image
     704                $image_types = array( 'image/jpeg', 'image/jpg', 'image/png', 'image/gif' );
     705                if ( false == $post_thumbnail && in_array( $filetype, $image_types ) ) {
     706                    set_post_thumbnail( $postID, $attachment_id );
     707                    $post_thumbnail = true;
     708                }
     709            }
     710        }
     711
     712        return $attachment_count;
     713    }
     714
     715    /**
    627716     * Mark a list of messages read on the server.
    628717     *
Note: See TracChangeset for help on using the changeset viewer.