Permalink
Browse files

Added validation for posts and XML-RPC extensions.

  • Loading branch information...
1 parent a09f30c commit 18c3daf72a45df8a9049d0d2cd194a6c7ae84c58 @scottgonzalez committed May 13, 2012
Showing with 93 additions and 16 deletions.
  1. +12 −1 README.md
  2. +37 −15 tasks/wordpress.js
  3. +44 −0 tasks/wordpress/posts.js
View
@@ -122,7 +122,7 @@ Walks through the `wordpress.dir` directory and performs various validations, su
* Verifying that child-parent relationships for posts are valid.
* Verifying metadata for each post.
-*Note: most of the validation is not implemented yet.*
+*Note: some of the validation is not implemented yet.*
#### wordpress-sync
@@ -161,6 +161,17 @@ Walks through all files in `path` (asynchronous and in series ).
Gets a client for connecting to the WordPress site via XML-RPC.
+#### wordpress-validate-xmlrpc-version( callback )
+
+Verifies that the XML-RPC extensions for grunt-wordpress are installed in WordPress.
+
+* `callback` (`function( error )`): Callback to invoke after verifying.
+
+#### wordpress-validate-posts( path, callback )
+
+* `path`: The directory of posts to validate.
+* `callback` (`function( error )`): Callback to invoke after validating the posts.
+
#### wordpress-get-postpaths( callback )
Gets the post paths for all existing posts in WordPress.
View
@@ -15,7 +15,8 @@ require( grunt.task.getFile( "wordpress/resources.js" ) )( grunt );
var _client,
path = require( "path" ),
wordpress = require( "wordpress" ),
- async = grunt.utils.async;
+ async = grunt.utils.async,
+ version = require( "../package" ).version;
// Async directory recursion, always walks all files before recursing
grunt.registerHelper( "wordpress-recurse", function recurse( rootdir, fn, complete ) {
@@ -38,6 +39,30 @@ grunt.registerHelper( "wordpress-client", function() {
return _client;
});
+grunt.registerHelper( "wordpress-validate-xmlrpc-version", function( fn ) {
+ var client = grunt.helper( "wordpress-client" );
+ grunt.verbose.write( "Verifying XML-RPC version..." );
+ client.call( "gw.getVersion", function( error, xmlrpcVersion ) {
+ if ( error ) {
+ grunt.verbose.error();
+ if ( error.code === -32601 ) {
+ return fn( new Error(
+ "XML-RPC extensions for grunt-wordpress are not installed." ) );
+ }
+
+ return fn( error );
+ }
+
+ if ( xmlrpcVersion !== version ) {
+ return fn( new Error( "Mismatching versions. " +
+ "grunt-wordpress: " + version + "; XML-RPC version: " + xmlrpcVersion ) );
+ }
+
+ grunt.verbose.ok();
+ fn( null );
+ });
+});
+
grunt.registerTask( "wordpress-sync", "Synchronize WordPress with local content", function() {
this.requires( "wordpress-validate" );
@@ -73,32 +98,29 @@ grunt.registerTask( "wordpress-sync", "Synchronize WordPress with local content"
grunt.registerTask( "wordpress-validate", "Validate HTML files for synchronizing WordPress", function() {
var done = this.async(),
- dir = grunt.config( "wordpress.dir" ),
- count = 0;
+ dir = grunt.config( "wordpress.dir" );
// TODO:
- // - Verify that all child posts actually have parents
- // - All directories must have a matching file
- // - Verify that all files have .html extension
- // - Verify required metadata
- // - Title, anything else?
- // - Verify gw.getPostPaths exists
- // - Verify that jQuery Slugs plugin exists
+ // - 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
- grunt.helper( "wordpress-walk-posts", path.join( dir, "posts/" ), function( post, fn ) {
- count++;
- fn( null );
- }, function( error ) {
+ async.waterfall([
+ function( fn ) {
+ grunt.helper( "wordpress-validate-xmlrpc-version", fn );
+ },
+
+ function( fn ) {
+ grunt.helper( "wordpress-validate-posts", path.join( dir, "posts/" ), fn );
+ }
+ ], function( error ) {
if ( error ) {
grunt.log.error( error );
return done( false );
}
- grunt.log.writeln( "Validated " + count + " files." );
done();
});
});
@@ -76,6 +76,7 @@ grunt.registerHelper( "wordpress-walk-posts", function( dir, walkFn, complete )
post.name = name;
post.__parent = parent;
post.__postPath = postPath;
+ post.__file = file;
walkFn( post, fn );
}, complete );
@@ -104,6 +105,49 @@ grunt.registerHelper( "wordpress-parse-post", function( path ) {
return post;
});
+grunt.registerHelper( "wordpress-validate-posts", function( dir, fn ) {
+ var count = 0,
+ postPaths = {};
+
+ grunt.helper( "wordpress-walk-posts", dir, function( post, fn ) {
+ // If there's a problem parsing the content of the file, then wordpress-walk-posts
+ // will return an error and we'll automatically stop walking. So we know that the
+ // content and structure of the metadata is already valid.
+ var file = post.__file;
+
+ postPaths[ post.__postPath ] = true;
+
+ // Verify file extension
+ if ( file.substr( file.length - 5 ) !== ".html" ) {
+ return fn( new Error( "Invalid file extension for " + file + "; must be .html." ) );
+ }
+
+ // Verify parent
+ if ( post.__parent && !postPaths[ post.__parent ] ) {
+ return fn( new Error( file + " does not have a parent." ) );
+ }
+
+ // Verify required data
+ if ( !post.title ) {
+ return fn( new Error( file + " is missing required data: title" ) );
+ }
+
+ count++;
+ fn( null );
+ }, function( error ) {
+ if ( error ) {
+ grunt.log.error( error );
+ return fn( error );
+ }
+
+ var msg = "Validated " + (count === 1 ?
+ "one post" :
+ (count + " posts"));
+ grunt.log.writeln( msg );
+ fn( null );
+ });
+});
+
// Publish (create or update) a post to WordPress.
grunt.registerHelper( "wordpress-publish-post", function( post, fn ) {
var client = grunt.helper( "wordpress-client" ),

0 comments on commit 18c3daf

Please sign in to comment.