[ 'time' => 'midnight tomorrow', 'interval' => DAY_IN_SECONDS, ], ); /** * AtumQueues singleton constructor * * @since 1.5.8 */ private function __construct() { add_action( 'init', array( $this, 'check_queues' ) ); // Add the recurring hooks. add_action( 'update_expiring_product_props', array( $this, 'update_expiring_product_props_action' ) ); } /** * Schedule the recurring hooks that are not in the queue. * * @since 1.5.8 */ public function check_queues() { $wc = wc(); // Ensure that the current WC version supports queues. if ( ! is_callable( array( $wc, 'queue' ) ) ) { return; } $wc_queue = $wc->queue(); foreach ( $this->recurring_hooks as $hook_name => $hook_data ) { $next_scheduled_date = $wc_queue->get_next( $hook_name ); if ( is_null( $next_scheduled_date ) ) { $wc_queue->cancel_all( $hook_name ); // Ensure all the actions are cancelled before adding a new one. $wc_queue->schedule_recurring( strtotime( $hook_data['time'] ), $hook_data['interval'], $hook_name ); } } } /** * Recalculate the expiring props for all the products * * @since 1.5.8 */ public function update_expiring_product_props_action() { // Get all the products that weren't updated during the last 3 hours. global $wpdb; $atum_product_data_table = $wpdb->prefix . Globals::ATUM_PRODUCT_DATA_TABLE; $date_max = gmdate( 'Y-m-d H:i:s', strtotime( '3 hours ago' ) ); $outdated_products = $wpdb->get_col( $wpdb->prepare( " SELECT product_id FROM $atum_product_data_table WHERE update_date <= %s ", $date_max ) ); // WPCS: unprepared SQL ok. foreach ( $outdated_products as $product_id ) { $product = Helpers::get_atum_product( $product_id ); if ( is_a( $product, '\WC_Product' ) ) { Helpers::update_order_item_product_data( $product ); } } } /******************* * Instance methods *******************/ /** * Cannot be cloned * * @since 1.5.8 */ public function __clone() { _doing_it_wrong( __FUNCTION__, esc_attr__( 'Cheatin’ huh?', ATUM_TEXT_DOMAIN ), '1.0.0' ); } /** * Cannot be serialized * * @since 1.5.8 */ public function __sleep() { _doing_it_wrong( __FUNCTION__, esc_attr__( 'Cheatin’ huh?', ATUM_TEXT_DOMAIN ), '1.0.0' ); } /** * Get Singleton instance * * @since 1.5.8 * * @return AtumQueues instance */ public static function get_instance() { if ( ! ( self::$instance && is_a( self::$instance, __CLASS__ ) ) ) { self::$instance = new self(); } return self::$instance; } }