Make it a proper daily scheduled event

This commit is contained in:
Allen Snook 2018-10-18 16:41:43 -07:00
parent 04b33f9e6b
commit 3d7b92f58f
2 changed files with 24 additions and 8 deletions

View File

@ -29,7 +29,7 @@ class WC_Admin_Notes_New_Sales_Record {
* @param string $date Date for sales to sum (i.e. YYYY-MM-DD).
* @return floatval
*/
public function sum_sales_for_date( $date ) {
public static function sum_sales_for_date( $date ) {
$order_query = new WC_Order_Query( array( 'date_created' => $date ) );
$orders = $order_query->get_orders();
$total = 0;
@ -44,9 +44,9 @@ class WC_Admin_Notes_New_Sales_Record {
/**
* Possibly add a sales record note.
*/
public function possibly_add_sales_record_note() {
public static function possibly_add_sales_record_note() {
$yesterday = date( 'Y-m-d', current_time( 'timestamp', 0 ) - DAY_IN_SECONDS );
$total = $this->sum_sales_for_date( $yesterday );
$total = self::sum_sales_for_date( $yesterday );
// No sales yesterday? Bail.
if ( 0 >= $total ) {
@ -107,6 +107,3 @@ class WC_Admin_Notes_New_Sales_Record {
}
}
}
// TODO Call schedule_recurring for once each day at 1 am or something.
new WC_Admin_Notes_New_Sales_Record();

View File

@ -47,6 +47,14 @@ function dependencies_satisfied() {
&& class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.5', '>' );
}
/**
* Daily events to run.
*/
function wc_admin_daily() {
WC_Admin_Notes_New_Sales_Record::possibly_add_sales_record_note();
}
add_action( 'wc_admin_daily', 'wc_admin_daily' );
/**
* Activates wc-admin plugin when installed.
*/
@ -59,19 +67,30 @@ function activate_wc_admin_plugin() {
WC_Admin_Api_Init::install();
if ( ! wp_next_scheduled( 'wc_admin_daily' ) ) {
wp_schedule_event( time(), 'daily', 'wc_admin_daily' );
}
}
register_activation_hook( WC_ADMIN_PLUGIN_FILE, 'activate_wc_admin_plugin' );
/**
* Deactivate wc-admin plugin if dependencies not satisfied.
*/
function deactivate_wc_admin_plugin() {
function possibly_deactivate_wc_admin_plugin() {
if ( ! dependencies_satisfied() ) {
deactivate_plugins( plugin_basename( WC_ADMIN_PLUGIN_FILE ) );
unset( $_GET['activate'] );
}
}
add_action( 'admin_init', 'deactivate_wc_admin_plugin' );
add_action( 'admin_init', 'possibly_deactivate_wc_admin_plugin' );
/**
* On deactivating the wc-admin plugin.
*/
function deactivate_wc_admin_plugin() {
wp_clear_scheduled_hook( 'wc_admin_daily' );
}
register_deactivation_hook( WC_ADMIN_PLUGIN_FILE, 'deactivate_wc_admin_plugin' );
/**
* Set up the plugin, only if we can detect both Gutenberg and WooCommerce