Skip to content

Commit

Permalink
wrapping up post transform
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Kadis committed Dec 23, 2016
1 parent 060b416 commit f6ffae0
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 6 deletions.
127 changes: 127 additions & 0 deletions active_plugins.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
---
# List of plugins that were active before migration
-
name: akismet
status: active
update: available
version: 3.1.7
-
name: cloudflare
status: active
update: available
version: 1.3.20
-
name: debug-bar
status: active
update: available
version: 0.8.2
-
name: debug-bar-console
status: active
update: none
version: "0.3"
-
name: disqus-comment-system
status: active
update: available
version: "2.84"
-
name: embedly
status: active
update: available
version: 4.0.7
-
name: feedburner_feedsmith_plugin_2.3
status: active
update: none
version: 2.3.1
-
name: flickr-widget
status: active
update: none
version: "0.1"
-
name: google-1
status: active
update: none
version: 0.1.1
-
name: google-analytics-for-wordpress
status: active
update: available
version: 5.4.6
-
name: hide-categories
status: active
update: none
version: "1.2"
-
name: inline-google-docs
status: active
update: none
version: "0.9"
-
name: jetpack
status: active
update: none
version: 3.1.1
-
name: just-the-page
status: active
update: none
version: "1.0"
-
name: my-page-order
status: active
update: none
version: 3.3.2
-
name: phplist-form-integration
status: active
update: none
version: "1.7"
-
name: simple-301-redirects
status: active
update: available
version: "1.06"
-
name: simple-pull-quote
status: active
update: available
version: "1.4"
-
name: simple-tags
status: active
update: available
version: 2.4.5
-
name: storify
status: active
update: none
version: 1.0.9
-
name: w3-total-cache
status: active
update: available
version: 0.9.4.1
-
name: wp-db-backup
status: active
update: available
version: 2.3.0
-
name: wordpress-notification-bar
status: active
update: available
version: 1.3.5
-
name: wp-emphasis
status: active
update: none
version: "0.7"
-
name: wordpress-seo
status: active
update: available
version: 3.0.7
20 changes: 19 additions & 1 deletion command.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,26 @@
}

class HH_Hugo_Command extends WP_CLI_Command {

protected $deactivate_plugins = array(
'w3-total-cache',
'wordpress-seo',
'google-analytics-for-wordpress',
);

function __construct() {
// Make sure these plugins are disabled
WP_CLI::runcommand( 'plugin deactivate ' . implode( ' ', $this->deactivate_plugins ) );
}

function transform_post( $args ) {
$migrate = new HH_Hugo\Migrate_Post( $args[0] );
$migrated = new HH_Hugo\Migrate_Post( $args[0] );
$result = $migrated->get( 'result' );
if ( 'success' === array_keys( $result)[0] ) {
WP_CLI::success( array_values( $result )[0] );
} else {
WP_CLI::warning( array_values( $result )[0] );
}
}
}

Expand Down
22 changes: 18 additions & 4 deletions inc/migrate-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,43 @@ class Migrate_Post {
*/
public function __construct( $post ) {
// just return the class for unit testing
if ( defined( 'HH_HUGO_UNIT_TESTS_RUNNING' ) ) {
if ( empty( $post ) && defined( 'HH_HUGO_UNIT_TESTS_RUNNING' ) ) {
return $this;
}

$this->output_dir = HH_HUGO_COMMAND_DIR . '/hugo-content';

$this->post = get_post( $post );
if ( empty( $this->post ) ) {
return array( 'error' => 'Invalid post ID or object.' );
$this->result = array( 'error' => 'Invalid post ID or object.' );
return;
}

$this->slug = $post->post_name;
$this->front_matter_src = $this->extract_front_matter( $this->post );
$this->front_matter = $this->transform_front_matter( $this->front_matter_src );
$this->markdown = $this->transform_post_content( $this->post->post_content );

// Check content for excessive HTML tags, flag for manual inspection
$this->num_tags = $this->count_tags( $this->markdown );
if ( 10 < $this->num_tags ) {
return array( 'error' => sprintf( 'Markdown for post %s contains %s HTML tags; manual inspection required.', $this->post->ID, $this->num_tags ) );
$this->result = array( 'error' => sprintf( 'Markdown for post %s contains %s HTML tags; manual inspection required.', $this->post->ID, $this->num_tags ) );
return;
}

// Write output to file
$file = new Write_File( $this->slug, $this->front_matter_src['date'], $this->front_matter, $this->markdown );

return array( 'success' => 'Migrated post ' . $this->post->ID );
if ( !$file ) {
$this->result = array( 'error' => sprintf( 'Error writing post %s to %s', $this->slug, $this->front_matter['date'] ) );
return;
}

$this->result = array( 'success' => sprintf( 'Migrated post %s to %s', $this->post->ID, $file ) );
}

public function get( $key ) {
return isset( $this->$key ) ? $this->$key : null;
}

/**
Expand All @@ -65,6 +78,7 @@ public function __construct( $post ) {
* @param int
*/
public function count_tags( $input ) {

// split string by html opening tags, doesn't need to be exact
$count = count( preg_split( '/<([A-Z][A-Z0-9]*)\b[^>]*>/i', $input ) );

Expand Down
2 changes: 1 addition & 1 deletion tests/test-migrate-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function setUp() {
'post_content' => 'lorem ipsum',
) );

$this->migrator = new HH_Hugo\Migrate_Post('test');
$this->migrator = new HH_Hugo\Migrate_Post( null );
}

/**
Expand Down

0 comments on commit f6ffae0

Please sign in to comment.