Plugin Directory

Changeset 1212344


Ignore:
Timestamp:
08/04/2015 06:54:00 AM (11 years ago)
Author:
hissy
Message:

Squashed commit

Location:
really-simple-csv-importer/trunk
Files:
5 added
3 edited

Legend:

Unmodified
Added
Removed
  • really-simple-csv-importer/trunk/class-rscsv_import_post_helper.php

    r1152758 r1212344  
    108108    {
    109109        $object = new RSCSV_Import_Post_Helper();
    110         $post_id = wp_insert_post($data, true);
     110
     111        if ($data['post_type'] == 'attachment') {
     112            $post_id = $object->addMediaFile($data['media_file'], $data);
     113        } else {
     114            $post_id = wp_insert_post($data, true);
     115        }
    111116        if (is_wp_error($post_id)) {
    112117            $object->addError($post_id->get_error_code(), $post_id->get_error_message());
     
    127132        if ($post instanceof WP_Post) {
    128133            $data['ID'] = $post->ID;
     134        }
     135        if ($data['post_type'] == 'attachment' && !empty($data['media_file'])) {
     136            $this->updateAttachment($data['media_file']);
     137            unset($data['media_file']);
    129138        }
    130139        $post_id = wp_update_post($data, true);
     
    292301     *
    293302     * @param (string) $file
     303     * @param (array) $data
    294304     * @return (boolean) True on success, false on failure.
    295305     */
    296     public function addMediaFile($file)
     306    public function addMediaFile($file, $data = null)
    297307    {
    298308        if (parse_url($file, PHP_URL_SCHEME)) {
    299309            $file = $this->remoteGet($file);
    300310        }
    301         $id = $this->setAttachment($file);
     311        $id = $this->setAttachment($file, $data);
    302312        if ($id) {
    303             return true;
     313            return $id;
    304314        }
    305315       
     
    336346     *
    337347     * @param (string) $file
     348     * @param (array) $data
    338349     * @return (int) Return the attachment id on success, 0 on failure.
    339350     */
    340     public function setAttachment($file)
    341     {
    342         $post = $this->getPost();
    343         if ($post instanceof WP_Post) {
    344             if ( $file && file_exists($file) ) {
    345                 $filename = basename($file);
    346                 $wp_filetype = wp_check_filetype_and_ext($file, $filename);
    347                 $ext = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext'];
    348                 $type = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type'];
    349                 $proper_filename = empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename'];
    350                 $filename = ($proper_filename) ? $proper_filename : $filename;
    351                 $filename = sanitize_file_name($filename);
    352                
    353                 $upload_dir = wp_upload_dir();
    354                 $guid = $upload_dir['baseurl'] . '/' . _wp_relative_upload_path($file);
    355                
    356                 $attachment = array(
    357                     'post_mime_type' => $type,
    358                     'guid' => $guid,
    359                     'post_title' => $filename,
    360                     'post_content' => '',
    361                     'post_status' => 'inherit'
    362                 );
    363                 $attachment_id = wp_insert_attachment($attachment, $file, $post->ID);
    364                 $attachment_metadata = wp_generate_attachment_metadata( $attachment_id, $file );
    365                 wp_update_attachment_metadata($attachment_id, $attachment_metadata);
    366                 return $attachment_id;
    367             }
     351    public function setAttachment($file, $data = array())
     352    {
     353        $post = $this->getPost();
     354        if ( $file && file_exists($file) ) {
     355            $filename       = basename($file);
     356            $wp_filetype    = wp_check_filetype_and_ext($file, $filename);
     357            $ext            = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext'];
     358            $type           = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type'];
     359            $proper_filename= empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename'];
     360            $filename       = ($proper_filename) ? $proper_filename : $filename;
     361            $filename       = sanitize_file_name($filename);
     362
     363            $upload_dir     = wp_upload_dir();
     364            $guid           = $upload_dir['baseurl'] . '/' . _wp_relative_upload_path($file);
     365
     366            $attachment = array_merge(array(
     367                'post_mime_type'    => $type,
     368                'guid'              => $guid,
     369                'post_title'        => $filename,
     370                'post_content'      => '',
     371                'post_status'       => 'inherit'
     372            ), $data);
     373            $attachment_id          = wp_insert_attachment($attachment, $file, ($post instanceof WP_Post) ? $post->ID : null);
     374            $attachment_metadata    = wp_generate_attachment_metadata( $attachment_id, $file );
     375            wp_update_attachment_metadata($attachment_id, $attachment_metadata);
     376            return $attachment_id;
    368377        }
    369378        // On failure
    370379        return 0;
    371380    }
    372    
     381
     382    /**
     383     * A wrapper of update_attached_file
     384     *
     385     * @param (string) $value
     386     */
     387    protected function updateAttachment($value)
     388    {
     389        $post = $this->getPost();
     390        if ($post instanceof WP_Post) {
     391            update_attached_file($post->ID, $value);
     392        } else {
     393            $this->addError('post_is_not_set', __('WP_Post object is not set.', 'really-simple-csv-importer'));
     394        }
     395    }
     396
    373397    /**
    374398     * A wrapper of wp_safe_remote_get
  • really-simple-csv-importer/trunk/readme.txt

    r1152758 r1212344  
    33Tags: importer, csv, acf, cfs, scf
    44Requires at least: 3.6
    5 Tested up to: 4.2.1
    6 Stable tag: 1.2
     5Tested up to: 4.3
     6Stable tag: 1.3
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    3737* `post_excerpt`: (string) For all your post excerpt needs.
    3838* `post_status`: ('draft' or 'publish' or 'pending' or 'future' or 'private' or custom registered status) The status of the post. 'draft' is default.
     39* `post_password`: (string) The password to protect the post. The password is limited to 20 characters.
    3940* `post_name`: (string) The slug of the post.
    4041* `post_parent`: (int) The post parent id. Used for page or hierarchical post type.
     
    4950* `cfs_{field_name}`: (string) If you would like to import data to custom fields set by Custom Field Suite, please add prefix `cfs_` to column header name.
    5051* `scf_{field_name}`: (string) If you would like to import data to custom fields set by Smart Custom Fields, please add prefix `scf_` to column header name.
     52* `comment_status`: ('closed' or 'open') Default is the option 'default_comment_status', or 'closed'.
    5153
    5254Note: Empty cells in the csv file means "keep it", not "delete it". 
    5355Note: To set the page template of a page, use custom field key of `_wp_page_template`. 
    5456Note: If providing a post_status of 'future' you must specify the post_date in order for WordPress to know when to publish your post.
     57Note: If the post_type value is `attachment`, you can use `post_thumbnail` field to define media URL or path.
    5558
    5659= Advanced Custom Fields plugin integrate =
     
    209212`
    210213
     214= really_simple_csv_importer_save_thumbnail =
     215
     216This filter is applied to thumbnail data.
     217
     218Parameters:
     219
     220* `$post_thumbnail` - (string)(required) the thumbnail file path or distant URL
     221* `$post` - (array) post data
     222* `$is_update` - (bool)
     223
     224Example:
     225
     226`
     227function really_simple_csv_importer_save_thumbnail_filter( $post_thumbnail, $post, $is_update ) {
     228
     229    // Import a local file from an FTP directory
     230    if (!empty($post_thumbnail) && file_exists($post_thumbnail)) {
     231        $upload_dir   = wp_upload_dir();
     232        $target_path  = $upload_dir['path'] . DIRECTORY_SEPARATOR . basename($post_thumbnail);
     233        if (copy($post_thumbnail, $target_path)) {
     234            $post_thumbnail = $target_path;
     235        }
     236    }
     237
     238    return $post_thumbnail;
     239}
     240add_filter( 'really_simple_csv_importer_save_thumbnail', 'really_simple_csv_importer_save_thumbnail_filter', 10, 3 );
     241`
     242
    211243== How to customize the post data after importing to database ==
    212244
     
    227259== Changelog ==
    228260
     261= 1.3 =
     262* Some Enhancements (Thanks @piwi!)
     263  * Attachment support
     264  * Add "really_simple_csv_importer_save_thumbnail" Filter
     265  * French translations
     266* Support post date GMT
     267* Support post password
    229268= 1.2 =
    230269* Enhancement: Smart Custom Fields support
  • really-simple-csv-importer/trunk/rs-csv-importer.php

    r1152758 r1212344  
    88Text Domain: really-simple-csv-importer
    99License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    10 Version: 1.2
     10Version: 1.3
    1111*/
    1212
     
    110110            unset($post['post_tags']);
    111111        }
    112        
     112
     113        // Special handling of attachments
     114        if (!empty($thumbnail) && $post['post_type'] == 'attachment') {
     115            $post['media_file'] = $thumbnail;
     116            $thumbnail = null;
     117        }
     118
    113119        // Add or update the post
    114120        if ($is_update) {
     
    221227                    $post['post_date'] = date("Y-m-d H:i:s", strtotime($post_date));
    222228                }
     229                $post_date_gmt = $h->get_data($this,$data,'post_date_gmt');
     230                if ($post_date_gmt) {
     231                    $post['post_date_gmt'] = date("Y-m-d H:i:s", strtotime($post_date_gmt));
     232                }
    223233               
    224234                // (string) post status
     
    230240                }
    231241               
     242                // (string) post password
     243                $post_password = $h->get_data($this,$data,'post_password');
     244                if ($post_password) {
     245                    $post['post_password'] = $post_password;
     246                }
     247               
    232248                // (string) post title
    233249                $post_title = $h->get_data($this,$data,'post_title');
     
    258274                if ($menu_order) {
    259275                    $post['menu_order'] = $menu_order;
     276                }
     277               
     278                // (string) comment status
     279                $comment_status = $h->get_data($this,$data,'comment_status');
     280                if ($comment_status) {
     281                    $post['comment_status'] = $comment_status;
    260282                }
    261283               
     
    323345                 */
    324346                $tax = apply_filters( 'really_simple_csv_importer_save_tax', $tax, $post, $is_update );
    325                
     347                /**
     348                 * Filter thumbnail URL or path.
     349                 *
     350                 * @since 1.3
     351                 *
     352                 * @param string $post_thumbnail (required)
     353                 * @param array $post
     354                 * @param bool $is_update
     355                 */
     356                $post_thumbnail = apply_filters( 'really_simple_csv_importer_save_thumbnail', $post_thumbnail, $post, $is_update );
     357
    326358                /**
    327359                 * Option for dry run testing
Note: See TracChangeset for help on using the changeset viewer.