Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add license limit UI for variable prices and show license key limit in [purchase_history] shortcode.
<?php
/**
* Show license key limit in [purchase_history] shortcode.
*
* @since 0.1.0
*/
function edd_site_count_downloads_license_limit_th() {
echo '<th class="edd-site-count-site-count">' . __( 'Site Count', 'edd-site-count' ) . '</th>';
}
add_action( 'edd_purchase_history_header_after', 'edd_site_count_downloads_license_limit_th', 11 );
/**
* Show license key limit in [purchase_history] shortcode.
*
* @since 0.1.0
*/
function edd_site_count_downloads_license_limit_td( $payment_id, $purchase_data ) {
/* Get license limit. */
$license_limit = edd_site_count_get_license_limit( $payment_id, $purchase_data );
/* translators: If license limit is infinite (0), set it as infinite sign. */
if ( 0 == $license_limit )
$license_limit = __( '&#8734;', 'edd-site-count' );
/* Get site count. How many sites have been activated with this key. */
$site_count = edd_site_count_get_license_count( $payment_id, $purchase_data );
/* If there is no site count, set it to 0. */
if ( empty( $site_count ) )
$site_count = 0;
/* Echo site count and license limit to [purchase_history] shortcode. */
echo '<td class="edd-site-count-site-count">'. $site_count . '/' . $license_limit .'</td>';
}
add_action( 'edd_purchase_history_row_end', 'edd_site_count_downloads_license_limit_td', 11, 2 );
/**
* Get site count.
*
* @since 0.1.0
*/
function edd_site_count_get_license_count( $payment_id, $purchase_data) {
$licensing = edd_software_licensing();
$downloads = edd_get_payment_meta_downloads( $payment_id );
/* Get license. */
foreach( $downloads as $download ) {
$license = $licensing->get_license_by_purchase( $payment_id, $download['id'] );
if( $license ) {
return absint( get_post_meta( $license->ID, '_edd_sl_site_count', true ) );
}
}
}
/**
* Get license limit. This is based on price id in variable pricing. This is 0, 1, 2 etc.
*
* @since 0.1.0
*/
function edd_site_count_get_license_limit( $payment_id, $purchase_data ) {
$licensing = edd_software_licensing();
$downloads = edd_get_payment_meta_downloads( $payment_id );
/* Get license. */
foreach( $downloads as $download ) {
$license = $licensing->get_license_by_purchase( $payment_id, $download['id'] );
if( $license ) {
/* Get license limit. */
if ( edd_has_variable_prices( $download['id'] ) ) {
/* Get purchase details. */
$meta = edd_get_payment_meta( $payment_id );
$cart = edd_get_payment_meta_cart_details( $payment_id, true );
$price_id = false;
/* Get price id from variable pricing. This is 0, 1, 2. */
foreach( $cart as $key => $item ) {
$price_id = edd_get_cart_item_price_id( $item );
/* Get license limits. */
if( $price_id !== false && ! empty( $price_id ) ) {
$prices = edd_get_variable_prices( $download['id'] );
$license_limit_value = '';
if( $prices && is_array( $prices ) ) {
$license_limit_value = $prices[ $price_id ]['license_limit'];
}
}
}
/* Return license limit. */
return absint( $license_limit_value );
}
else {
return absint( get_post_meta( $download['id'], '_edd_sl_limit', true ) );
}
}
}
}
/**
* Adds the table header for license limit.
*
* @since 0.1.0
*/
function edd_site_count_license_limit_variable( $post_id ) { ?>
<th style="width: 90px"><?php _e( 'License limit', 'edd-site-count' ); ?></th>
<?php
}
add_filter( 'edd_download_price_table_head', 'edd_site_count_license_limit_variable' );
/**
* Adds the table cell for license limit.
*
* @since 0.1.0
*/
function edd_site_count_row_license_limit( $post_id, $key, $args ) {
$license_limit = isset( $args['license_limit'] ) ? $args['license_limit'] : null;
?>
<td>
<input type="text" class="edd_site_count_license_limit" value="<?php echo esc_attr( $license_limit ); ?>" placeholder="<?php _e( 'License limit', 'edd-site-count' ); ?>" name="edd_variable_prices[<?php echo $key; ?>][license_limit]" id="edd_variable_prices[<?php echo $key; ?>][license_limit]" size="20" style="width:90px" />
</td>
<?php
}
add_action( 'edd_download_price_table_row', 'edd_site_count_row_license_limit', 10, 3 );
/**
* Adds license limit to edd_price_row_args.
*
* @since 0.1.0
*/
function edd_site_count_row_args_license_limit( $args, $value ) {
$args['license_limit'] = isset( $value['license_limit'] ) ? $value['license_limit'] : '';
return $args;
}
add_filter( 'edd_price_row_args', 'edd_site_count_row_args_license_limit', 10, 2 );
?>
@samikeijonen

This comment has been minimized.

Copy link
Owner Author

samikeijonen commented Jul 31, 2013

get_license_limit function in Software Licensing needs to check variable prices limit also. Fallback could always be _edd_sl_limit.

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.