Changeset 1212344
- Timestamp:
- 08/04/2015 06:54:00 AM (11 years ago)
- Location:
- really-simple-csv-importer/trunk
- Files:
-
- 5 added
- 3 edited
-
class-rscsv_import_post_helper.php (modified) (4 diffs)
-
languages/really-simple-csv-importer-fr_FR.mo (added)
-
languages/really-simple-csv-importer-fr_FR.po (added)
-
readme.txt (modified) (5 diffs)
-
rs-csv-importer.php (modified) (6 diffs)
-
sample/import_attachment.csv (added)
-
sample/import_attachment.ods (added)
-
sample/post_dates.csv (added)
Legend:
- Unmodified
- Added
- Removed
-
really-simple-csv-importer/trunk/class-rscsv_import_post_helper.php
r1152758 r1212344 108 108 { 109 109 $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 } 111 116 if (is_wp_error($post_id)) { 112 117 $object->addError($post_id->get_error_code(), $post_id->get_error_message()); … … 127 132 if ($post instanceof WP_Post) { 128 133 $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']); 129 138 } 130 139 $post_id = wp_update_post($data, true); … … 292 301 * 293 302 * @param (string) $file 303 * @param (array) $data 294 304 * @return (boolean) True on success, false on failure. 295 305 */ 296 public function addMediaFile($file )306 public function addMediaFile($file, $data = null) 297 307 { 298 308 if (parse_url($file, PHP_URL_SCHEME)) { 299 309 $file = $this->remoteGet($file); 300 310 } 301 $id = $this->setAttachment($file );311 $id = $this->setAttachment($file, $data); 302 312 if ($id) { 303 return true;313 return $id; 304 314 } 305 315 … … 336 346 * 337 347 * @param (string) $file 348 * @param (array) $data 338 349 * @return (int) Return the attachment id on success, 0 on failure. 339 350 */ 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; 368 377 } 369 378 // On failure 370 379 return 0; 371 380 } 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 373 397 /** 374 398 * A wrapper of wp_safe_remote_get -
really-simple-csv-importer/trunk/readme.txt
r1152758 r1212344 3 3 Tags: importer, csv, acf, cfs, scf 4 4 Requires at least: 3.6 5 Tested up to: 4. 2.16 Stable tag: 1. 25 Tested up to: 4.3 6 Stable tag: 1.3 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 37 37 * `post_excerpt`: (string) For all your post excerpt needs. 38 38 * `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. 39 40 * `post_name`: (string) The slug of the post. 40 41 * `post_parent`: (int) The post parent id. Used for page or hierarchical post type. … … 49 50 * `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. 50 51 * `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'. 51 53 52 54 Note: Empty cells in the csv file means "keep it", not "delete it". 53 55 Note: To set the page template of a page, use custom field key of `_wp_page_template`. 54 56 Note: If providing a post_status of 'future' you must specify the post_date in order for WordPress to know when to publish your post. 57 Note: If the post_type value is `attachment`, you can use `post_thumbnail` field to define media URL or path. 55 58 56 59 = Advanced Custom Fields plugin integrate = … … 209 212 ` 210 213 214 = really_simple_csv_importer_save_thumbnail = 215 216 This filter is applied to thumbnail data. 217 218 Parameters: 219 220 * `$post_thumbnail` - (string)(required) the thumbnail file path or distant URL 221 * `$post` - (array) post data 222 * `$is_update` - (bool) 223 224 Example: 225 226 ` 227 function 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 } 240 add_filter( 'really_simple_csv_importer_save_thumbnail', 'really_simple_csv_importer_save_thumbnail_filter', 10, 3 ); 241 ` 242 211 243 == How to customize the post data after importing to database == 212 244 … … 227 259 == Changelog == 228 260 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 229 268 = 1.2 = 230 269 * Enhancement: Smart Custom Fields support -
really-simple-csv-importer/trunk/rs-csv-importer.php
r1152758 r1212344 8 8 Text Domain: really-simple-csv-importer 9 9 License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 10 Version: 1. 210 Version: 1.3 11 11 */ 12 12 … … 110 110 unset($post['post_tags']); 111 111 } 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 113 119 // Add or update the post 114 120 if ($is_update) { … … 221 227 $post['post_date'] = date("Y-m-d H:i:s", strtotime($post_date)); 222 228 } 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 } 223 233 224 234 // (string) post status … … 230 240 } 231 241 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 232 248 // (string) post title 233 249 $post_title = $h->get_data($this,$data,'post_title'); … … 258 274 if ($menu_order) { 259 275 $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; 260 282 } 261 283 … … 323 345 */ 324 346 $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 326 358 /** 327 359 * Option for dry run testing
Note: See TracChangeset
for help on using the changeset viewer.