Skip to content

Instantly share code, notes, and snippets.

@spencermorin
Last active October 9, 2019 15:26
Show Gist options
  • Save spencermorin/55f50fcb026cdf70035b8bd701118694 to your computer and use it in GitHub Desktop.
Save spencermorin/55f50fcb026cdf70035b8bd701118694 to your computer and use it in GitHub Desktop.
<?php
/*
* Plugin Name: Import Meta Backup
* Plugin URI: http://somesite.com/where-this-plugin-lives/
* Description: Backs up original data in meta.
* Author: Automattic
* Author URI: http://theauthor.com/
* Version: 1.0
* Text Domain: import-meta-backup
* License: GPLv3 or later
*/
class Import_Meta_Backup {
function __construct() {
// Set the import origin from -origin=someorigin
$args= $_SERVER['argv'];
foreach( $args as $arg ) {
if( false !== strpos( $arg, '-origin=' ) ) {
$this->origin = str_replace( '-origin=', '', $arg );
break;
}
}
add_filter( 'wp_import_post_data_raw', function( $post ) {
if( ! isset( $post['postmeta'] ) )
$post['postmeta'] = array();
$post['postmeta'][] = array(
'key' => '_original_post_id',
'value' => $post['post_id'],
);
$post['postmeta'][] = array(
'key' => '_original_import_author',
'value' => $post['post_author'],
);
if ( ! empty( $post['post_parent'] ) ) {
$original_post_parent = $post['post_parent'];
$post['postmeta'][] = array(
'key' => '_original_parent_id',
'value' => $post['post_parent'],
);
}
if( ! empty( $this->origin ) ) {
$post['postmeta'][] = array(
'key' => '_original_import_origin',
'value' => $this->origin,
);
}
foreach( $post['postmeta'] as $meta ) {
if ( $meta['key'] == '_thumbnail_id' ) {
$post['postmeta'][] = array(
'key' => '_original_thumbnail_id',
'value' => $meta['value'],
);
break;
}
}
if ( 'attachment' == $post['post_type'] ) {
$original_attachment_url = ! empty( $post['attachment_url'] ) ? $post['attachment_url'] : $post['guid'];
$post['postmeta'][] = array(
'key' => '_original_import_url',
'value' => $original_attachment_url,
);
}
return $post;
});
}
}
new Import_Meta_Backup;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment