Skip to content

Commit

Permalink
Add WP-CLI command and register a subcommand for changing the state o…
Browse files Browse the repository at this point in the history
…f posts

See #11
  • Loading branch information
danielbachhuber committed Jul 10, 2012
1 parent 408e963 commit 75868e0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions p2-resolved-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
50 changes: 50 additions & 0 deletions php/class-wp-cli.php
Original file line number Diff line number Diff line change
@@ -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.