Skip to content

Commit

Permalink
Added validation for taxonomies.json.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottgonzalez committed May 14, 2012
1 parent 0c5523a commit 6f78c5e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
10 changes: 6 additions & 4 deletions README.md
Expand Up @@ -115,14 +115,11 @@ The post type and parent are determined based on the [directory structure](#dire

Walks through the `wordpress.dir` directory and performs various validations, such as:

<!-- TODO: update list when validation is implemented -->
* Verifying that XML-RPC is enabled for the WordPress site.
* Verifying that the custom XML-RPC methods for grunt-wordpress are installed.
* Verifying the taxonomies and terms in `taxonomies.json`.
* Verifying that child-parent relationships for posts are valid.
* Verifying metadata for each post.

*Note: some of the validation is not implemented yet.*
* Verifying data for each post.

#### wordpress-sync

Expand Down Expand Up @@ -167,6 +164,11 @@ Verifies that the XML-RPC extensions for grunt-wordpress are installed in WordPr

* `callback` (`function( error )`): Callback to invoke after verifying.

#### wordpress-validate-terms( path, callback )

* `path`: The path to the taxonomies JSON file.
* `callback` (`function( error )`): Callback to invoke after validating the terms.

#### wordpress-validate-posts( path, callback )

* `path`: The directory of posts to validate.
Expand Down
8 changes: 4 additions & 4 deletions tasks/wordpress.js
Expand Up @@ -104,16 +104,16 @@ grunt.registerTask( "wordpress-validate", "Validate HTML files for synchronizing

// TODO:
// - Verify that jQuery Slugs plugin exists (should really merge into gw)
// - Verify taxonomies.json
// - Requires name, slug
// - Slug must be [a-z0-9.-], no consecutive dashes
// - Check for existing terms with same name, but different slug

async.waterfall([
function( fn ) {
grunt.helper( "wordpress-validate-xmlrpc-version", fn );
},

function( fn ) {
grunt.helper( "wordpress-validate-terms", path.join( dir, "taxonomies.json" ), fn );
},

function( fn ) {
grunt.helper( "wordpress-validate-posts", path.join( dir, "posts/" ), fn );
}
Expand Down
55 changes: 55 additions & 0 deletions tasks/wordpress/taxonomies.js
Expand Up @@ -8,6 +8,61 @@ function prettyTermName( term ) {
return term.taxonomy + " " + term.slug;
}

// TODO: Check for existing terms with same name, but different slug
grunt.registerHelper( "wordpress-validate-terms", function( filepath, fn ) {
var taxonomies,
client = grunt.helper( "wordpress-client" ),
count = 0;

function complete() {
grunt.log.writeln( "Validated " + count + " terms." );
fn( null );
}

if ( !path.existsSync( filepath ) ) {
return complete();
}

// Check if the taxonomies JSON format is valid
try {
taxonomies = grunt.file.readJSON( filepath );
} catch( error ) {
grunt.log.error( "Invalid taxonomy definitions file." );
return fn( error );
}

async.forEachSeries( Object.keys( taxonomies ), function( taxonomy, fn ) {
function process( terms, fn ) {
async.forEachSeries( terms, function( term, fn ) {
if ( !term.name ) {
return fn( new Error( "A " + taxonomy + " term has no name." ) );
}
if ( !term.slug ) {
return fn( new Error( "The " + taxonomy + " term " + term.name + " has no slug." ) );
}
if ( !(/^([a-zA-Z0-9]+[.\-]?)+$/).test( term.slug ) ) {
return fn( new Error( "Invalid slug: " + term.slug + "." ) );
}

count++;
if ( term.children ) {
return process( term.children, fn );
}

fn( null );
}, fn );
}

process( taxonomies[ taxonomy ], fn );
}, function( error ) {
if ( error ) {
return fn( error );
}

complete();
});
});

grunt.registerHelper( "wordpress-get-terms", function( fn ) {
var client = grunt.helper( "wordpress-client" );

Expand Down

0 comments on commit 6f78c5e

Please sign in to comment.