General API functions for scheduling actions */ /** * Schedule an action to run one time * * @param int $timestamp When the job will run * @param string $hook The hook to trigger * @param array $args Arguments to pass when the hook triggers * @param string $group The group to assign this job to * * @return string The job ID */ function wc_schedule_single_action( $timestamp, $hook, $args = array(), $group = '' ) { return ActionScheduler::factory()->single( $hook, $args, $timestamp, $group ); } /** * Schedule a recurring action * * @param int $timestamp When the first instance of the job will run * @param int $interval_in_seconds How long to wait between runs * @param string $hook The hook to trigger * @param array $args Arguments to pass when the hook triggers * @param string $group The group to assign this job to * * @return string The job ID */ function wc_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) { return ActionScheduler::factory()->recurring( $hook, $args, $timestamp, $interval_in_seconds, $group ); } /** * Schedule an action that recurs on a cron-like schedule. * * @param int $timestamp The schedule will start on or after this time * @param string $schedule A cron-link schedule string * @see http://en.wikipedia.org/wiki/Cron * * * * * * * * ┬ ┬ ┬ ┬ ┬ ┬ * | | | | | | * | | | | | + year [optional] * | | | | +----- day of week (0 - 7) (Sunday=0 or 7) * | | | +---------- month (1 - 12) * | | +--------------- day of month (1 - 31) * | +-------------------- hour (0 - 23) * +------------------------- min (0 - 59) * @param string $hook The hook to trigger * @param array $args Arguments to pass when the hook triggers * @param string $group The group to assign this job to * * @return string The job ID */ function wc_schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '' ) { return ActionScheduler::factory()->cron( $hook, $args, $timestamp, $schedule, $group ); } /** * Cancel the next occurrence of a job. * * @param string $hook The hook that the job will trigger * @param array $args Args that would have been passed to the job * @param string $group * * @return void */ function wc_unschedule_action( $hook, $args = array(), $group = '' ) { $params = array(); if ( is_array($args) ) { $params['args'] = $args; } if ( !empty($group) ) { $params['group'] = $group; } $job_id = ActionScheduler::store()->find_action( $hook, $params ); if ( empty($job_id) ) { return; } ActionScheduler::store()->cancel_action( $job_id ); } /** * @param string $hook * @param array $args * @param string $group * * @return int|bool The timestamp for the next occurrence, or false if nothing was found */ function wc_next_scheduled_action( $hook, $args = NULL, $group = '' ) { $params = array(); if ( is_array($args) ) { $params['args'] = $args; } if ( !empty($group) ) { $params['group'] = $group; } $job_id = ActionScheduler::store()->find_action( $hook, $params ); if ( empty($job_id) ) { return false; } $job = ActionScheduler::store()->fetch_action( $job_id ); $next = $job->get_schedule()->next(); if ( $next ) { return (int)($next->format('U')); } return false; } /** * Find scheduled actions * * @param array $args Possible arguments, with their default values: * 'hook' => '' - the name of the action that will be triggered * 'args' => NULL - the args array that will be passed with the action * 'date' => NULL - the scheduled date of the action. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone. * 'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '=' * 'modified' => NULL - the date the action was last updated. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone. * 'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '=' * 'group' => '' - the group the action belongs to * 'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING * 'claimed' => NULL - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID * 'per_page' => 5 - Number of results to return * 'offset' => 0 * 'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', or 'date' * 'order' => 'ASC' * * @param string $return_format OBJECT, ARRAY_A, or ids * * @return array */ function wc_get_scheduled_actions( $args = array(), $return_format = OBJECT ) { $store = ActionScheduler::store(); foreach ( array('date', 'modified') as $key ) { if ( isset($args[$key]) ) { $args[$key] = as_get_datetime_object($args[$key]); } } $ids = $store->query_actions( $args ); if ( $return_format == 'ids' || $return_format == 'int' ) { return $ids; } $actions = array(); foreach ( $ids as $action_id ) { $actions[$action_id] = $store->fetch_action( $action_id ); } if ( $return_format == ARRAY_A ) { foreach ( $actions as $action_id => $action_object ) { $actions[$action_id] = get_object_vars($action_object); } } return $actions; } /** * Helper function to create an instance of DateTime based on a given * string and timezone. By default, will return the current date/time * in the UTC timezone. * * Needed because new DateTime() called without an explicit timezone * will create a date/time in PHP's timezone, but we need to have * assurance that a date/time uses the right timezone (which we almost * always want to be UTC), which means we need to always include the * timezone when instantiating datetimes rather than leaving it up to * the PHP default. * * @param mixed $date_string A date/time string. Valid formats are explained in http://php.net/manual/en/datetime.formats.php * @param string $timezone A timezone identifier, like UTC or Europe/Lisbon. The list of valid identifiers is available http://php.net/manual/en/timezones.php * * @return DateTime */ function as_get_datetime_object( $date_string = null, $timezone = 'UTC' ) { if ( is_object($date_string) && $date_string instanceof DateTime ) { $date = $date_string->setTimezone(new DateTimeZone( $timezone ) ); } elseif ( is_numeric( $date_string ) ) { $date = new DateTime( '@'.$date_string, new DateTimeZone( $timezone ) ); } else { $date = new DateTime( $date_string, new DateTimeZone( $timezone ) ); } return $date; } Subscription Product Variation Class * * The subscription product variation class extends the WC_Product_Variation product class * to create subscription product variations. * * @class WC_Product_Subscription * @package WooCommerce Subscriptions * @category Class * @since 1.3 * */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } class WC_Product_Subscription_Variation extends WC_Product_Variation { var $product_type; /** * Create a simple subscription product object. * * @access public * @param mixed $product */ public function __construct( $product, $args = array() ) { parent::__construct( $product, $args = array() ); $this->parent_product_type = $this->product_type; $this->product_type = 'subscription_variation'; $this->subscription_variation_level_meta_data = array( 'subscription_price' => 0, 'subscription_period' => '', 'subscription_period_interval' => 'day', 'subscription_length' => 0, 'subscription_trial_length' => 0, 'subscription_trial_period' => 'day', 'subscription_sign_up_fee' => 0, 'subscription_payment_sync_date' => 0, ); $this->variation_level_meta_data = array_merge( $this->variation_level_meta_data, $this->subscription_variation_level_meta_data ); } /** * Get variation price HTML. Prices are not inherited from parents. * * @return string containing the formatted price */ public function get_price_html( $price = '' ) { $price = parent::get_price_html( $price ); if ( ! empty( $price ) ) { $price = WC_Subscriptions_Product::get_price_string( $this, array( 'price' => $price ) ); } return $price; } /** * Get the add to cart button text * * @access public * @return string */ public function add_to_cart_text() { if ( $this->is_purchasable() && $this->is_in_stock() ) { $text = get_option( WC_Subscriptions_Admin::$option_prefix . '_add_to_cart_button_text', __( 'Sign Up Now', 'woocommerce-subscriptions' ) ); } else { $text = parent::add_to_cart_text(); // translated "Read More" } return apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this ); } /** * Get the add to cart button text for the single page * * @access public * @return string */ public function single_add_to_cart_text() { return apply_filters( 'woocommerce_product_single_add_to_cart_text', self::add_to_cart_text(), $this ); } /** * Returns the sign up fee (including tax) by filtering the products price used in * @see WC_Product::get_price_including_tax( $qty ) * * @return string */ public function get_sign_up_fee_including_tax( $qty = 1, $price = '' ) { add_filter( 'woocommerce_get_price', array( &$this, 'get_sign_up_fee' ), 100, 0 ); $sign_up_fee_including_tax = parent::get_price_including_tax( $qty, $price ); remove_filter( 'woocommerce_get_price', array( &$this, 'get_sign_up_fee' ), 100, 0 ); return $sign_up_fee_including_tax; } /** * Returns the sign up fee (excluding tax) by filtering the products price used in * @see WC_Product::get_price_excluding_tax( $qty ) * * @return string */ public function get_sign_up_fee_excluding_tax( $qty = 1, $price = '' ) { add_filter( 'woocommerce_get_price', array( &$this, 'get_sign_up_fee' ), 100, 0 ); $sign_up_fee_excluding_tax = parent::get_price_excluding_tax( $qty, $price ); remove_filter( 'woocommerce_get_price', array( &$this, 'get_sign_up_fee' ), 100, 0 ); return $sign_up_fee_excluding_tax; } /** * Return the sign-up fee for this product * * @return string */ public function get_sign_up_fee() { return WC_Subscriptions_Product::get_sign_up_fee( $this ); } /** * Checks if the variable product this variation belongs to is purchasable. * * @access public * @return bool */ function is_purchasable() { $purchasable = WCS_Limiter::is_purchasable( $this->parent->is_purchasable(), $this ); return apply_filters( 'woocommerce_subscription_variation_is_purchasable', $purchasable, $this ); } /** * Checks the product type to see if it is either this product's type or the parent's * product type. * * @access public * @param mixed $type Array or string of types * @return bool */ public function is_type( $type ) { if ( $this->product_type == $type || ( is_array( $type ) && in_array( $this->product_type, $type ) ) ) { return true; } elseif ( $this->parent_product_type == $type || ( is_array( $type ) && in_array( $this->parent_product_type, $type ) ) ) { return true; } else { return false; } } }