Skip to content

Instantly share code, notes, and snippets.

@mklasen
Created September 24, 2019 16:32
Show Gist options
  • Save mklasen/c6428af7495f7f6158d36889c3d3fb90 to your computer and use it in GitHub Desktop.
Save mklasen/c6428af7495f7f6158d36889c3d3fb90 to your computer and use it in GitHub Desktop.
Serve updates with EDD for free downloads
<?php
class EDD_Updates_Without_Licenses {
public function __construct() {
$this->hooks();
}
public function hooks() {
add_filter( 'edd_sl_id_license_match', array( $this, 'check_free_license' ), 10, 4 );
add_filter( 'edd_sl_generate_license_key', array( $this, 'generate_free_license_key' ), 10, 5 );
}
/**
* Checks for free license and create license
*/
public function check_free_license( $license_match, $download_id, $license_download, $license_key ) {
// Is the download free?
$download = edd_get_download( $download_id );
if ( $download->is_free() ) {
// Check if license exists.
$license = edd_software_licensing()->get_license( 'free-download', true );
if ( ! $license ) {
// This code will just run once, ever.
// I just don't want it to be a manual action.
// First get a customer.
$customer = new EDD_Customer( '[email protected]' );
if ( ! $customer->id ) {
// Create a new customer
$customer = new EDD_Customer();
$customer->create( array(
'name' => 'Free Downloads',
'email' => '[email protected]',
) );
}
// Now the payment.
$payments = $customer->payment_ids;
if ( ! empty( $payments ) ) {
$payment_ids = explode( ',', $payments );
$payment_id = reset( $payment_ids );
$payment = new EDD_Payment( $payment_id );
} else {
$payment = new EDD_Payment();
$payment->email = '[email protected]';
$payment->status = 'complete';
$payment->save();
}
// Finally create the license
$license = new EDD_SL_License();
$license->create( 782, $payment->ID, false, 0, array(
'activation_limit' => 0,
'is_lifetime' => 0
) );
}
return true;
}
}
/**
* When generating a free license, set the key.
*/
public function generate_free_license_key( $key, $license_id, $download_id, $payment_id, $cart_index ) {
$customer = new EDD_Customer( '[email protected]' );
$payments = $customer->payment_ids;
$payment_ids = explode( ',', $payments );
if ( in_array( $payment_id, $payment_ids ) ) {
$key = 'free-download';
}
return $key;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment