Browse files

Add WP-CLI command and register a subcommand for changing the state o…

…f posts

See #11
  • Loading branch information...
1 parent 408e963 commit 75868e0b4a5398dcb92ef22aa1352a38673890f7 @danielbachhuber danielbachhuber committed Jul 9, 2012
Showing with 53 additions and 0 deletions.
  1. +3 −0 p2-resolved-posts.php
  2. +50 −0 php/class-wp-cli.php
View
3 p2-resolved-posts.php
@@ -14,6 +14,9 @@
* Original source: https://gist.github.com/1353754
*/
+if ( defined('WP_CLI') && WP_CLI )
+ require_once( dirname( __FILE__ ) . '/php/class-wp-cli.php' );
+
/**
* @package P2_Resolved_Posts
*/
View
50 php/class-wp-cli.php
@@ -0,0 +1,50 @@
+<?php
+/**
+ * P2 Resolved Posts commands for the WP-CLI framework
+ *
+ * @package wp-cli
+ * @since 0.2
+ * @see https://github.com/wp-cli/wp-cli
+ */
+WP_CLI::addCommand( 'p2-resolved-posts', 'P2ResolvedPosts_Command' );
+class P2ResolvedPosts_Command extends WP_CLI_Command {
+
+ /**
+ * Help function for this command
+ */
+ public static function help() {
+
+ WP_CLI::line( <<<EOB
+usage: wp p2-resolved-posts <parameters>
+Possible subcommands:
+ change_state Change the state for a given post
+ --post_id=Post ID to affect
+ --state=State to change the post to 'resolved', 'unresolved', 'normal'
+EOB
+ );
+ }
+
+ /**
+ * Subcommand to change the state for a given post
+ */
+ public function change_state( $args, $assoc_args ) {
+ global $p2_resolved_posts;
+
+ $defaults = array(
+ 'post_id' => '',
+ 'state' => '',
+ );
+ $this->args = wp_parse_args( $assoc_args, $defaults );
+
+ if ( !get_post( $this->args['post_id'] ) )
+ WP_CLI::error( "Please specify a valid post_id" );
+
+ if ( !in_array( $this->args['state'], array( 'resolved', 'unresolved', 'normal' ) ) )
+ WP_CLI::error( "Please specify a valid state: " );
+
+ wp_set_post_terms( $this->args['post_id'], $this->args['state'], P2_Resolved_Posts::taxonomy );
+ clean_post_cache( $this->args['post_id'] );
+ WP_CLI::success( "Changed state for post #{$this->args['post_id']}: {$this->args['state']}" );
+ }
+
+}

0 comments on commit 75868e0

Please sign in to comment.