Skip to content

Commit

Permalink
Merge pull request hissy#37 from piwi/feature-support-attachment
Browse files Browse the repository at this point in the history
Feature: attachment support
  • Loading branch information
hissy committed Jun 2, 2015
2 parents 83806a0 + d0bfe94 commit a13d9e8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 32 deletions.
86 changes: 55 additions & 31 deletions class-rscsv_import_post_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ public static function getByID($post_id)
public static function add($data)
{
$object = new RSCSV_Import_Post_Helper();
$post_id = wp_insert_post($data, true);

if ($data['post_type'] == 'attachment') {
$post_id = $object->addMediaFile($data['media_file'], $data);
} else {
$post_id = wp_insert_post($data, true);
}
if (is_wp_error($post_id)) {
$object->addError($post_id->get_error_code(), $post_id->get_error_message());
} else {
Expand All @@ -127,6 +132,10 @@ public function update($data)
if ($post instanceof WP_Post) {
$data['ID'] = $post->ID;
}
if ($data['post_type'] == 'attachment' && !empty($data['media_file'])) {
$this->updateAttachment($data['media_file']);
unset($data['media_file']);
}
$post_id = wp_update_post($data, true);
if (is_wp_error($post_id)) {
$this->addError($post_id->get_error_code(), $post_id->get_error_message());
Expand Down Expand Up @@ -291,16 +300,17 @@ public function setObjectTerms($taxonomy, $terms)
* Add attachment file. Automatically get remote file
*
* @param (string) $file
* @param (array) $data
* @return (boolean) True on success, false on failure.
*/
public function addMediaFile($file)
public function addMediaFile($file, $data = null)
{
if (parse_url($file, PHP_URL_SCHEME)) {
$file = $this->remoteGet($file);
}
$id = $this->setAttachment($file);
$id = $this->setAttachment($file, $data);
if ($id) {
return true;
return $id;
}

return false;
Expand Down Expand Up @@ -335,41 +345,55 @@ public function addThumbnail($file)
* A wrapper of wp_insert_attachment and wp_update_attachment_metadata
*
* @param (string) $file
* @param (array) $data
* @return (int) Return the attachment id on success, 0 on failure.
*/
public function setAttachment($file)
public function setAttachment($file, $data = array())
{
$post = $this->getPost();
if ($post instanceof WP_Post) {
if ( $file && file_exists($file) ) {
$filename = basename($file);
$wp_filetype = wp_check_filetype_and_ext($file, $filename);
$ext = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext'];
$type = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type'];
$proper_filename = empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename'];
$filename = ($proper_filename) ? $proper_filename : $filename;
$filename = sanitize_file_name($filename);

$upload_dir = wp_upload_dir();
$guid = $upload_dir['baseurl'] . '/' . _wp_relative_upload_path($file);

$attachment = array(
'post_mime_type' => $type,
'guid' => $guid,
'post_title' => $filename,
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment($attachment, $file, $post->ID);
$attachment_metadata = wp_generate_attachment_metadata( $attachment_id, $file );
wp_update_attachment_metadata($attachment_id, $attachment_metadata);
return $attachment_id;
}
if ( $file && file_exists($file) ) {
$filename = basename($file);
$wp_filetype = wp_check_filetype_and_ext($file, $filename);
$ext = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext'];
$type = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type'];
$proper_filename= empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename'];
$filename = ($proper_filename) ? $proper_filename : $filename;
$filename = sanitize_file_name($filename);

$upload_dir = wp_upload_dir();
$guid = $upload_dir['baseurl'] . '/' . _wp_relative_upload_path($file);

$attachment = array_merge(array(
'post_mime_type' => $type,
'guid' => $guid,
'post_title' => $filename,
'post_content' => '',
'post_status' => 'inherit'
), $data);
$attachment_id = wp_insert_attachment($attachment, $file, ($post instanceof WP_Post) ? $post->ID : null);
$attachment_metadata = wp_generate_attachment_metadata( $attachment_id, $file );
wp_update_attachment_metadata($attachment_id, $attachment_metadata);
return $attachment_id;
}
// On failure
return 0;
}


/**
* A wrapper of update_attached_file
*
* @param (string) $value
*/
protected function updateAttachment($value)
{
$post = $this->getPost();
if ($post instanceof WP_Post) {
update_attached_file($post->ID, $value);
} else {
$this->addError('post_is_not_set', __('WP_Post object is not set.', 'really-simple-csv-importer'));
}
}

/**
* A wrapper of wp_safe_remote_get
*
Expand Down
8 changes: 7 additions & 1 deletion rs-csv-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ public function save_post($post,$meta,$terms,$thumbnail,$is_update) {
$post_tags = $post['post_tags'];
unset($post['post_tags']);
}


// Special handling of attachments
if (!empty($thumbnail) && $post['post_type'] == 'attachment') {
$post['media_file'] = $thumbnail;
$thumbnail = null;
}

// Add or update the post
if ($is_update) {
$h = RSCSV_Import_Post_Helper::getByID($post['ID']);
Expand Down
2 changes: 2 additions & 0 deletions sample/import_attachment.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"post_title","post_type","post_content","post_excerpt","post_thumbnail","_wp_attachment_image_alt"
"Attachment test","attachment","Image presentation","Image caption","http://upload.wikimedia.org/wikipedia/commons/a/a2/WordPress_MP6_dashboard.png","ALT text"
Binary file added sample/import_attachment.ods
Binary file not shown.

0 comments on commit a13d9e8

Please sign in to comment.