Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@thefrosty
Created November 8, 2014 01:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefrosty/bd7f58d8ab71e72702d8 to your computer and use it in GitHub Desktop.
Save thefrosty/bd7f58d8ab71e72702d8 to your computer and use it in GitHub Desktop.
Upgrade a license to a higher sites count on Extendd.com for Easy Digital Downloads
<?php
if ( !defined( 'ABSPATH' ) )
exit;
function extendd_add_key_column() {
echo '<th class="edd_license_key_upgrade">' . __( 'Upgrade License', 'extendd' ) . '</th>';
}
add_action( 'edd_purchase_history_header_after', 'extendd_add_key_column' );
/**
* Displays a Manage Licenses link in purchase history
*
* @since 2.7
*/
function extendd_site_management_links( $payment_id, $purchase_data ) {
$downloads = edd_get_payment_meta_downloads( $payment_id );
//var_dump( $purchase_data['downloads'][0]['id'] ); exit;
if ( $downloads) :
if ( count( $purchase_data['downloads'] ) > 1 ) {
echo '<td class="edd_license_key_upgrade">-</td>';
return;
}
$plugin_id = isset( $purchase_data['downloads'][0]['id'] ) ? $purchase_data['downloads'][0]['id'] : false;
if ( !$plugin_id ) {
echo '<td class="edd_license_key_upgrade">-</td>';
return;
}
$has_personal_license = edd_has_user_purchased( get_current_user_id(), array( $plugin_id ), 0 );
$has_business_license = edd_has_user_purchased( get_current_user_id(), array( $plugin_id ), 1 );
$has_developer_license = edd_has_user_purchased( get_current_user_id(), array( $plugin_id ), 2 );
echo '<td class="edd_license_key_upgrade">';
if ( edd_is_payment_complete( $payment_id ) ) {
if ( $has_developer_license ) {
echo '<strong>(unlimted sites)</strong>';
}
if ( $has_business_license ) {
echo '<strong>(2-5 sites)</strong><br/>';
printf( '<a title="Upgrade to Developer License" href="%s">Upgrade to Developer License (unlimited sites)</a>', extendd_get_license_upgrade_url( 'developer', $plugin_id ) );
}
if ( $has_personal_license ) {
echo '<strong>(1 site)</strong><br/>';
printf( '<a title="Upgrade to Business License" href="%s">Upgrade to Business License (2 - 5 sites)</a><br/>', extendd_get_license_upgrade_url( 'business', $plugin_id ) );
printf( '<a title="Upgrade to Developer License" href="%s">Upgrade to Developer License (unlimited sites)</a>', extendd_get_license_upgrade_url( 'developer', $plugin_id ) );
}
}
else {
echo '-';
}
echo '</td>';
endif;
}
add_action( 'edd_purchase_history_row_end', 'extendd_site_management_links', 10, 2 );
/**
* Returns the URL to upgrade a license from personal -> business or dev, or from business -> dev
*
* @since AffiliateWP 1.x
*
* @return string
*/
function extendd_get_license_upgrade_url( $type = '', $plugin_id = '' ) {
if ( ! function_exists( 'edd_get_checkout_uri' ) || ! $type ) {
return home_url( '/plugins' );
}
$args = array(
'edd_action' => 'upgrade_plugin_license',
'type' => $type,
'plugin_id' => $plugin_id
);
return add_query_arg( $args, edd_get_checkout_uri() );
}
/**
* Processes the license upgrade
*/
function extendd_process_license_upgrade() {
// get type. business or developer
$type = isset( $_GET['type'] ) ? $_GET['type'] : '';
$plugin_id = isset( $_GET['plugin_id'] ) ? $_GET['plugin_id'] : '';
if ( !is_user_logged_in() || !( 'business' == $type || 'developer' == $type ) || !$plugin_id ) {
// Isn't logged in, so go back to pricing
wp_redirect( home_url( '/plugins' ) ); exit;
}
switch ( $type ) {
case 'developer':
// user has developer license already
if ( edd_has_user_purchased( get_current_user_id(), $plugin_id, 2 ) ) {
wp_die( 'You already have a "Developer\'s" license', '', array( 'back_link' => true ) );
}
elseif ( edd_has_user_purchased( get_current_user_id(), $plugin_id, 1 ) ) {
// Has a business license
$discount = 99;
}
elseif ( edd_has_user_purchased( get_current_user_id(), $plugin_id ) ) {
// Has a personal license
$discount = 49;
}
else {
// Hasn't purchased, so go back to pricing
wp_redirect( home_url( '/plugins' ) ); exit;
}
$price_id = 2;
break;
case 'business':
// user has developer license already
if ( edd_has_user_purchased( get_current_user_id(), $plugin_id, 1 ) ) {
wp_die( 'You already have a Business license', '', array( 'back_link' => true ) );
}
elseif ( edd_has_user_purchased( get_current_user_id(), $plugin_id, 0 ) ) {
// Has a personal license
$discount = 49;
}
else {
// Hasn't purchased, so go back to pricing
wp_redirect( home_url( '/plugins' ) ); exit;
}
$price_id = 1;
break;
} // end switch
// Remove anything in the cart
edd_empty_cart();
// Add the correct license
edd_add_to_cart( $plugin_id, array( 'price_id' => $price_id ) );
// SEE renewals.php in 'edd-software-licensing/' for more info and fixes and/or more complex handeling.
EDD()->fees->add_fee( ( $discount * -1 ), 'License Upgrade Discount' );
//EDD()->session->set( 'is_upgrade', '1' );
wp_redirect( edd_get_checkout_uri() ); exit;
}
add_action( 'edd_upgrade_plugin_license', 'extendd_process_license_upgrade' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment