Skip to content

Instantly share code, notes, and snippets.

@vovafeldman
Last active August 18, 2019 18:48
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 vovafeldman/62c2c8492e2a19bd6e33a05a2f148bbd to your computer and use it in GitHub Desktop.
Save vovafeldman/62c2c8492e2a19bd6e33a05a2f148bbd to your computer and use it in GitHub Desktop.
A code snippet to automatically activate the locally stored license key from the previous eCommerce platform (EDD/WC) with the Freemius SDK

Description

This extra snippet will check if a license was previously activated, and if it does, will activate it through Freemius in the background.

Usage

  • You will need to replace my_fs() with your theme's relevant shortcode.
  • You will need to rename the function my_fs_license_key_migration() to a unique name.
  • You will need to rename 'my_fs_migrated2fs' to a unique option name.
  • You will need to replace my_get_license_key_from_prev_platform_storage() with some logic that fetches the theme's license key.
<?php
function my_fs_license_key_migration() {
if ( ! my_fs()->has_api_connectivity() || my_fs()->is_registered() ) {
// No connectivity OR the user already opted-in to Freemius.
return;
}
if ( 'pending' != get_option( 'my_fs_migrated2fs', 'pending' ) ) {
return;
}
// Get the license key from the previous eCommerce platform's storage.
$license_key = my_get_license_key_from_prev_platform_storage();
if ( empty( $license_key ) ) {
// No key to migrate.
return;
}
// Get the first 32 characters.
$license_key = substr( $license_key, 0, 32 );
try {
$next_page = my_fs()->activate_migrated_license( $license_key );
} catch (Exception $e) {
update_option( 'my_fs_migrated2fs', 'unexpected_error' );
return;
}
if ( my_fs()->can_use_premium_code() ) {
update_option( 'my_fs_migrated2fs', 'done' );
if ( is_string( $next_page ) ) {
fs_redirect( $next_page );
}
} else {
update_option( 'my_fs_migrated2fs', 'failed' );
}
}
add_action( 'admin_init', 'my_fs_license_key_migration' );
<?php
function my_fs_license_key_auto_activation() {
if ( ! my_fs()->has_api_connectivity() || my_fs()->is_registered() ) {
// No connectivity OR the user already opted-in to Freemius.
return;
}
if ( 'pending' != get_option( 'my_fs_auto_license_activation', 'pending' ) ) {
return;
}
// Get the license key from the previous eCommerce platform's storage.
$license_key = my_get_license_key_from_prev_platform_storage();
if ( empty( $license_key ) ) {
// No key to migrate.
return;
}
try {
$next_page = my_fs()->activate_migrated_license( WP__MY_FS__LICENSE_KEY );
} catch (Exception $e) {
update_option( 'my_fs_auto_license_activation', 'unexpected_error' );
return;
}
if ( my_fs()->can_use_premium_code() ) {
update_option( 'my_fs_auto_license_activation', 'done' );
if ( is_string( $next_page ) ) {
fs_redirect( $next_page );
}
} else {
update_option( 'my_fs_auto_license_activation', 'failed' );
}
}
if ( defined( 'WP__MY_FS__LICENSE_KEY' ) ) {
add_action( 'admin_init', 'my_fs_license_key_auto_activation' );
}
<?php
define( 'WP__MY_FS__LICENSE_KEY', '<YOUR_LICENSE_KEY>' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment