Skip to content

Instantly share code, notes, and snippets.

@decodist
Last active August 23, 2023 13:26
Show Gist options
  • Save decodist/a046cbd6364d94250d438a04c863a869 to your computer and use it in GitHub Desktop.
Save decodist/a046cbd6364d94250d438a04c863a869 to your computer and use it in GitHub Desktop.
WordPress CLI Class
<?php
/**
* Custom WordPress CLI Command Plugin
*
* @package decodist
* @author Jason Shaw
* @since 1.0.0
* @license GPL
*
*/
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
return;
}
class Product_CLI extends \WP_CLI_Command {
/**
* Untag all early access classes when public access date is past.
*
* ## OPTIONS
*
* [--logging]
* : Display additional feedback to the terminal for debugging purposes. Default is false.
*
* ## EXAMPLES
*
* wp product untag_product
* wp product untag_product --logging
*
*/
public function untag_product( $args, $assoc_args ) {
try {
//determine the logging level
$logging = WP_CLI\Utils\get_flag_value($assoc_args, 'logging', false);
//provide user feedback that the process has started
$output = $logging ? WP_CLI::line( 'Searching for expired early access classes...' ) : null;
//run a query to find all 'product' content that is 'early access' and has a public access date older than today
$query_args = array(
'post_type' => 'product',
'tax_query' => array(
array (
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'early-access',
),
),
'meta_query' => array(
array(
'key' => 'public_access_date',
'value' => date('Y-m-d'), //assume standard PHP date format
'type' => 'date',
'compare' => '<'
),
),
);
$the_query = new WP_Query( $query_args );
//if we have any content that fits the above criteria, remove the early access term
if ( $the_query->have_posts() ) {
$class_count = $the_query->found_posts;
//create a progress bar in case there are many posts that need to be processed
$progress = \WP_CLI\Utils\make_progress_bar( "Untagging expired classes (".$class_count.")", $class_count );
//loop over the expired classes and remove the 'early access' term
while ( $the_query->have_posts() ) {
$the_query->the_post();
$result = wp_remove_object_terms( get_the_ID(), 'early-access', 'product_cat' );
if( is_wp_error( $result ) ) {
$output = $logging ? WP_CLI::line( $result->get_error_message() ) : null;
} else {
//provide user feedback that the content has been modified
$output = $logging ? WP_CLI::line( "Post ID ".get_the_ID()." has been untagged" ) : null;
}
//update the progress bar
$progress->tick();
}
//close the progress bar
$progress->finish();
} else {
//no content fits the above criteria
$output = $logging ? WP_CLI::line( "No expired classes found" ) : null;
}
//provide user feedback that the process has finished
WP_CLI::success( "Process has completed" );
} catch ( \Throwable $e ) {
//handle situations where WP throws an error
WP_CLI::error( $e->getMessage() );
throw $e;
}
}
}
WP_CLI::add_command( 'product', 'Product_CLI' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment