Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
brian is c00l
<?php
/**
* Enable Subscriptions Options for supporting product types and payment gateways
* @package exchange-addon-recurring-payments
* @since 1.0.0
*/
if( class_exists( 'IT_Exchange_Recurring_Payments' ) ) {
class IT_Exchange_Subscriptions extends IT_Exchange_Recurring_Payments {
/**
* This echos the base price metabox.
*
* @since 0.1.0
* @param object $post Product
* @return void
*/
function print_metabox( $post ) {
// Grab the iThemes Exchange Product object from the WP $post object
$product = it_exchange_get_product( $post );
// Get product feature values
$product_feature_interval = it_exchange_get_product_feature( $product->ID, 'subscriptions', array( 'setting' => 'interval' ) );
$product_feature_term = it_exchange_get_product_feature( $product->ID, 'subscriptions', array( 'setting' => 'term' ) );
$product_feature_duration = it_exchange_get_product_feature( $product->ID, 'subscriptions', array( 'setting' => 'duration' ) );
$product_feature_sign_up_fee = it_exchange_get_product_feature( $product->ID, 'subscriptions', array( 'setting' => 'sign-up-fee' ) ); // optional
$product_feature_auto_renew = it_exchange_get_product_feature( $product->ID, 'subscriptions', array( 'setting' => 'auto-renew' ) );
$intervals = apply_filters( 'it_exchange_subscriptions_intervals', array(
'every' => __( 'Per', 'it-l10n-exchange-addon-subscriptions' ),
'2nd' => __( 'Every 2nd', 'it-l10n-exchange-addon-subscriptions' ),
'3rd' => __( 'Every 3rd', 'it-l10n-exchange-addon-subscriptions' ),
'4th' => __( 'Every 4th', 'it-l10n-exchange-addon-subscriptions' ),
'5th' => __( 'Every 5th', 'it-l10n-exchange-addon-subscriptions' ),
'6th' => __( 'Every 6th', 'it-l10n-exchange-addon-subscriptions' ),
) );
$terms = apply_filters( 'it_exchange_subscriptions_terms', array(
'day' => __( 'Day', 'it-l10n-exchange-addon-subscriptions' ),
'week' => __( 'Week', 'it-l10n-exchange-addon-subscriptions' ),
'month' => __( 'Month', 'it-l10n-exchange-addon-subscriptions' ),
'year' => __( 'Year', 'it-l10n-exchange-addon-subscriptions' ),
) );
$durations = array( 'forever' => __( 'Forever', 'it-l10n-exchange-addon-subscriptions' ) );
for ( $i = 1; $i <= 24; $i++ ) {
$durations[ $i ] = $i;
}
$durations = apply_filters( 'it_exchange_subscriptions_durations', $durations );
if ( ! $product_feature_duration || 'forever' === $product_feature_duration ) {
$hidden = 'hidden';
$product_feature_auto_renew = 'off';
} else {
$hidden = '';
}
// Echo the form field
?>
<div id="it-exchange-subscriptions-settings">
<select class="it-exchange-subscriptions-interval-options" name="it-exchange-subscriptions-interval">
<?php
foreach ( $intervals as $key => $interval ) {
echo '<option value="' . $key . '" ' . selected( $product_feature_interval, $key, false ) . '>' . $interval . '</option>';
}
?>
</select>
<select class="it-exchange-subscriptions-term-options" name="it-exchange-subscriptions-term">
<?php
foreach ( $terms as $key => $term ) {
echo '<option value="' . $key . '" ' . selected( $product_feature_term, $key, false ) . '>' . $term . '</option>';
}
?>
</select>
<span> for </span>
<select class="it-exchange-subscriptions-duration-options" name="it-exchange-subscriptions-duration">
<?php
foreach ( $durations as $key => $duration ) {
echo '<option value="' . $key . '" ' . selected( $product_feature_duration, $key, false ) . '>' . $duration . '</option>';
}
?>
</select>
<span> with a $</span>
<input type="text" class="it-exchange-subscriptions-sign-up-fee" name="it-exchange-subscriptions-sign-up-fee" placeholder="0.00" value="<?php echo ( $product_feature_sign_up_fee ) ? $product_feature_sign_up_fee : ''; ?>">
<span> sign-up fee.</span>
<span class="it-exchange-subscriptions-auto-renew <?php echo $hidden; ?> auto-renew-<?php echo $product_feature_auto_renew; ?>" title="<?php printf( __( 'Auto-Renew: %s', 'it-l10n-exchange-addon-membership' ), strtoupper( $product_feature_auto_renew ) ); ?>">
<input type="hidden" name="it-exchange-subscriptions-auto-renew" value="<?php echo $product_feature_auto_renew; ?>" />
</span>
</div>
<?php
}
/*
* Save subscription features on product save
*
* @since 0.1.0
*
* @return void
*/
function save_feature_on_product_save() {
// Abort if we can't determine a product type
if ( ! $product_type = it_exchange_get_product_type() )
return;
// Abort if we don't have a product ID
$product_id = empty( $_POST['ID'] ) ? false : $_POST['ID'];
if ( ! $product_id )
return;
// Abort if this product type doesn't support this feature
if ( ! it_exchange_product_type_supports_feature( $product_type, 'subscriptions' ) )
return;
it_exchange_update_product_feature( $product_id, 'subscriptions', $_POST['it-exchange-subscriptions-interval'], array( 'setting' => 'interval' ) );
it_exchange_update_product_feature( $product_id, 'subscriptions', $_POST['it-exchange-subscriptions-term'], array( 'setting' => 'term' ) );
it_exchange_update_product_feature( $product_id, 'subscriptions', $_POST['it-exchange-subscriptions-duration'], array( 'setting' => 'duration' ) );
it_exchange_update_product_feature( $product_id, 'subscriptions', $_POST['it-exchange-subscriptions-sign-up-fee'], array( 'setting' => 'sign-up-fee' ) );
it_exchange_update_product_feature( $product_id, 'subscriptions', $_POST['it-exchange-subscriptions-auto-renew'], array( 'setting' => 'auto-renew' ) );
}
/**
* Return subscription features
*
* @since 0.1.0
* @param mixed $existing the values passed in by the WP Filter API. Ignored here.
* @param integer product_id the WordPress post ID
* @param array $options Optional arguments used to specify which feature is gotten
* @return string
*/
function get_feature( $existing, $product_id, $options=array() ) {
// Is the the add / edit product page?
$current_screen = is_admin() ? get_current_screen() : false;
$editing_product = ( ! empty( $current_screen->id ) && 'it_exchange_prod' == $current_screen->id );
/*
TODO: what are the lines of code below doing and why? how come auto-renew doesn't need to be passed to ITUtility::merge_defaults() ?
ITUtility::merge_defaults is defined in ithemes-exchange/lib/classes/it-utility.php, line 86
*/
// Using options to determine if we're getting the enabled setting or the actual time setting // TODO: what is the "actual time setting?"
$defaults = array( 'setting' => array( 'interval', 'term', 'duration', 'sign-up-fee' ) );
$options = ITUtility::merge_defaults( $options, $defaults );
if ( 'interval' == $options['setting'] ) {
return get_post_meta( $product_id, '_it-exchange-subscriptions-interval', true );
} else if ( 'term' == $options['setting'] ) {
return get_post_meta( $product_id, '_it-exchange-subscriptions-term', true );
} else if ( 'duration' == $options['setting'] ) {
return get_post_meta( $product_id, '_it-exchange-subscriptions-duration', true );
} else if ( 'sign-up-fee' == $options['setting'] ) {
return get_post_meta( $product_id, '_it-exchange-subscriptions-sign-up-fee', true );
} else if ( 'auto-renew' == $options['setting'] ) {
$autorenew = get_post_meta( $product_id, '_it-exchange-product-recurring-auto-renew', true );
if ( ! in_array( $autorenew, array( 'on', 'off' ) ) )
$autorenew = 'off';
return $autorenew;
}
return false;
}
/**
* Does the product have the feature?
*
* @since 0.1.0
* @param mixed $result Not used by core
* @param integer $product_id
* @param array $options Optional arguments used to specify which feature is checked
* @return boolean
*/
function product_has_feature( $result, $product_id, $options=array() ) {
$defaults = array( 'setting' => array( 'interval', 'term', 'duration', 'sign-up-fee' ) );
$options = ITUtility::merge_defaults( $options, $defaults );
// Does this product type support this feature?
if ( false === $this->product_supports_feature( false, $product_id, $options ) )
return false;
// If it does support it, does product have the feature?
$feature = $this->get_feature( false, $product_id, $options );
if ( ! empty( $feature ) )
return true;
else
return false;
}
/**
* Does the product support this feature?
*
* This is different than if it has the feature, a product can
* support a feature but might not have the feature set.
*
* @since 1.0.0
* @param mixed $result Not used by core
* @param integer $product_id
* @param array $options Optional arguments used to specify which feature is checked
* @return boolean
*/
function product_supports_feature( $result, $product_id, $options=array() ) {
$defaults = array( 'setting' => array( 'interval', 'term', 'duration', 'sign-up-fee' ) );
$options = ITUtility::merge_defaults( $options, $defaults );
// Does this product type support this feature?
$product_type = it_exchange_get_product_type( $product_id );
if ( ! it_exchange_product_type_supports_feature( $product_type, 'recurring-payments' ) ) // TODO: should this be 'subscriptions' ?
return false;
if ( 'auto-renew' === $options['setting'] ) {
if ( 'off' === it_exchange_get_product_feature( $product_id, 'recurring-payments', array( 'setting' => $options['setting'] ) ) )
return false;
}
return true;
}
}
$IT_Exchange_Subscriptions = new IT_Exchange_Subscriptions();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.