Skip to content

Instantly share code, notes, and snippets.

@Zagaz
Last active July 28, 2024 22:09
Show Gist options
  • Save Zagaz/ae4a74f38a803c587e023f5c800eff5f to your computer and use it in GitHub Desktop.
Save Zagaz/ae4a74f38a803c587e023f5c800eff5f to your computer and use it in GitHub Desktop.
WP-CLI Plugin for Managing Product Access Dates
<?php
/*
* Plugin Name: Remove Early Access
* Description: Remove early access tag from products when public access date is past.
* Version: 1.0.0
* Author: Andre Ranulfo
* Author URI: https://www.linkedin.com/in/andre-ranulfo/
*/
if (!defined('ABSPATH'))
{
exit;
}
if (!defined('WP_CLI') || !WP_CLI)
{
return;
}
/**
* Product_cli class
*
* @package Ndevr
* @since 1.0.0
*/
class Product_CLI extends \WP_CLI_Command
{
/**
* Remove early access tag from products when public access date is past.
*/
public function remove_early_access()
{
/**
* Untag all early access classes when public access date is past.
*
*
*/
// Get current date.
$current_date = current_time('Y-m-d H:i:s');
// Query for all products with the 'early-access' term.
$query = new WP_Query(array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'early-access',
) ,
) ,
'meta_query' => array(
array(
'key' => 'public_access_date',
'value' => $current_date,
'compare' => '<',
'type' => 'DATETIME',
) ,
) ,
'posts_per_page' => - 1, // Retrieve all posts.
));
if ($query->have_posts())
{
while ($query->have_posts())
{
$query->the_post();
$post_id = get_the_ID();
// Remove the 'early-access' term from the product.
wp_remove_object_terms($post_id, 'early-access', 'product_cat');
// Products removed log
WP_CLI::log("Removed 'early-access' from product ID: $post_id");
}
}
else
{
// NO Products removed log
WP_CLI::log("No products found with 'early-access' term and past public access date.");
}
// Reset post data.
wp_reset_postdata();
}
}
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