Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added validation for taxonomies.json.

  • Loading branch information...
commit 6f78c5ed0c7e62f78f18ad1634803bfbb7c1d16e 1 parent 0c5523a
@scottgonzalez authored
Showing with 65 additions and 8 deletions.
  1. +6 −4 README.md
  2. +4 −4 tasks/wordpress.js
  3. +55 −0 tasks/wordpress/taxonomies.js
View
10 README.md
@@ -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
@@ -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.
View
8 tasks/wordpress.js
@@ -104,10 +104,6 @@ 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 ) {
@@ -115,6 +111,10 @@ grunt.registerTask( "wordpress-validate", "Validate HTML files for synchronizing
},
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 );
}
], function( error ) {
View
55 tasks/wordpress/taxonomies.js
@@ -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" );
Please sign in to comment.
Something went wrong with that request. Please try again.