Repository navigation
Example Importer
Sander van Dragt edited this page Dec 19, 2019
·
2 revisions
Please find below an example importer scaffold (untested).
<?php
/**
* Plugin Name: My Content Importer
* Description: WordPress Importer
* Version: 1.0
*
* Activate the migrator plugins: `wp plugin activate hm-content-import my-content-import`
*
* #### Run the import
*
* To run the import: `time wp hmci import my-importer --verbose --url="mysite.local" --export_path="my-import"`
*
* Note that the progress bar does not begin to indicate progress until it reaches 1%.
*/
namespace MY\Importer;
use HMCI\Master;
/**
* Handles importing content.
*/
class MyContentImporter extends \HMCI\Iterator\Files\JSON {
/**
* Processes the import item.
*
* @param array $item The import item.
* @return bool False on failure, true on success.
*/
protected function process_item( $item ) {
$this->args['debugger'] = 'error_log';
// Note: create this function yourself
$content = my_process_content( $item );
// Manually tick the progress bar, as we're dealing with multiple items per file.
\HMCI\CLI\HMCI::$progressbar->tick( 1 );
\HMCI\Utils\clear_local_object_cache();
if ( is_wp_error( $content ) ) {
$this->debug( $content->get_error_message() );
return false;
}
return true;
}
}
add_action( 'plugins_loaded', function() {
if ( ! class_exists( '\HMCI\Master', false ) ) {
add_action( 'admin_notices', function() {
echo '<div class="notice notice-error"><p>My Content Importer requires the HM Content Importer plugin to be active.</p></div>';
} );
return;
}
// Register the importer.
Master::add_importer( 'my-importer', __NAMESPACE__ . '\\MyContentImporter' );
} );