Skip to content

Instantly share code, notes, and snippets.

@cyberwani
Forked from xplodedthemes/edd.php
Created October 8, 2021 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyberwani/4826a6d60dd615b7d84b9c42f6283ae9 to your computer and use it in GitHub Desktop.
Save cyberwani/4826a6d60dd615b7d84b9c42f6283ae9 to your computer and use it in GitHub Desktop.
Freemius EDD Migration Helper in case EDD License keys have a custom prefix and are over 32 chars.
<?php
/*
....
Within plugin EDD Migration Client edd.php
*/
/**
* You should use your own unique CLASS name, and be sure to replace it
* throughout this file. For example, if your product's name is "Awesome Product"
* then you can rename it to "Awesome_Product_EDD_License_Key".
*/
class Awesome_Product_EDD_License_Key extends FS_Client_License_Abstract_v1 {
// Previous key prefix
protected $old_key_prefix = 'old-license-prefix-';
// Previous license option key
protected $old_license_option = 'local-license-key-option-name';
function __construct()
{
my_freemius()->add_filter( 'license_key', array($this, 'license_key_filter' ));
my_freemius()->add_filter( 'license_key_maxlength', array($this, 'license_key_maxlength' ));
}
function license_key_filter( $license_key ) {
if ( false !== strpos( $license_key, $this->old_key_prefix ) ) {
$license_key = substr( $license_key, -32 );
}
return trim($license_key);
}
function license_key_maxlength( $maxlength ) {
// The max length of your previous keys.
return 32 + strlen($this->old_key_prefix);
}
/**
* @author Vova Feldman (@svovaf)
* @since 1.0.3
*
* @param int|null $blog_id
*
* @return string
*/
function get( $blog_id = null ) {
$new_license_key = '';
// Get local license
$old_license_key = get_site_option( $this->old_license_option, '' );
// If local key found and still over 32 chars
if(!empty($old_license_key) && is_string($old_license_key) && strlen($old_license_key) > 32) {
// We need to strip the prefix and make it 32 chars on the EDD store
// Apply filter and get new key without old prefix, valid 32 chars
$new_license_key = my_freemius()->apply_filters('license_key', $old_license_key);
// Update the key on the EDD Store by sending both old / new key.
$response = wp_remote_post('https://your-edd-store.com',
array(
'body' => array(
'prefix_edd_update_license' => array(
'old' => $old_license_key,
'new' => $new_license_key
)
)
)
);
if ( !is_wp_error( $response ) ) {
$response = wp_remote_retrieve_body( $response );
$result = !empty($response) ? json_decode($response) : null;
// If the update succeeded, save the new key locally
if(!empty($result->success)) {
$this->set( $new_license_key );
}
}
}
return $new_license_key;
}
/**
* @author Vova Feldman (@svovaf)
* @since 1.0.3
*
* @param string $license_key
* @param int|null $blog_id
*
* @return bool True if successfully updated.
*/
function set( $license_key, $blog_id = null ) {
return update_site_option( $this->old_license_option, $license_key );
}
// ....... //
}
<?php
/*
....
Within theme functions.php
*/
function prefix_edd_update_license() {
global $wpdb;
$data = $_POST['prefix_edd_update_license'];
$old = $data['old']; // OLD KEY
$new = $data['new']; // NEW KEY
// Find license by using old key, if found, update the key with the new one.
$success = $wpdb->update('wp_edd_licenses', array('license_key' => $new), array('license_key'=>$old));
// If license was not found using the old key, maybe it was already replaced by the new key, find it using the new key instead.
if(!$success) {
$found = $wpdb->get_var( $wpdb->prepare("SELECT license_key FROM {$wpdb->prefix}edd_licenses WHERE license_key = %s", $new) );
$success = !empty($found);
}
echo json_encode(array('success' => $success));
die();
}
if(!empty($_POST['prefix_edd_update_license'])) {
add_action('init', 'prefix_edd_update_license');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment