Matt Johnson / @xmatt / alleyinteractive.com
http://xmattus.github.io/object-oriented-content-migration
class Processor { public $migrateable_class; public function __construct() { $this->migrateable_class = 'Migrateable'; } abstract function advance_cursor(); abstract function get_next_item(); public function load_migrateable() { $item = $this->get_next_item(); $this->migrateable = new $this->migrateable_class( $item ); $this->advance_cursor(); } public function save_migrateable() { $this->migrateable->save(); } }
// Example where $item is a simple assoc array, e.g. parsed JSON. class Migrateable { public $item; public function __construct( $item ) { $this->item = $item; } public function get_title() { return empty( $this->item['title'] ) ? $this->item['title'] : ''; } public function get_content() { return empty( $this->item['content'] ) ? $this->item['content'] : ''; } public function get_legacy_url() { return empty( $this->item['url'] ) ? $this->item['url'] : ''; } public function save() { $post = $this->get_post_by_legacy_url(); if ( ! $post ) { $post = array(); } $post['post_title'] = $this->get_title(); $post['post_content'] = $this->get_content(); // More fields and other fun transformations here $post_id = wp_update_post( $post ); update_post_meta( $post_id, 'legacy_url', $this->get_legacy_url() ); } public function get_post_by_legacy_url() { $post = big_ugly_meta_query( $this->get_legacy_url() ); if ( $post && ! is_wp_error( $post ) ) { return $post; } return false; } }
Want to do cool stuff like this?We're hiring.
info@alleyinteractive.com