Skip to content

Instantly share code, notes, and snippets.

@vovafeldman
Forked from shamim2883/sp-edd-fs.php
Created September 10, 2020 11:10
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 vovafeldman/692a2584b3742eedc4f5d218ca60afae to your computer and use it in GitHub Desktop.
Save vovafeldman/692a2584b3742eedc4f5d218ca60afae to your computer and use it in GitHub Desktop.
Redirect EDD license renew request to Freemius
<?php
add_action('add_meta_boxes', function(){
add_meta_box('sp_edd_fs_metabox', 'Freemius data', 'sp_edd_fs_metabox', 'download', 'side', 'default' );
});
function sp_edd_fs_metabox( $post ){
$plugin_id = get_post_meta( $post->ID, '_sp_edd_fs_plugin_id', true );
$coupon = get_post_meta( $post->ID, '_sp_edd_fs_coupon', true );
echo '<p><label for="_sp_edd_fs_plugin_id">Plugin ID</label><input id="_sp_edd_fs_plugin_id" type="number" value="' . esc_attr( $plugin_id ) . '" name="_sp_edd_fs_plugin_id"></p>';
echo '<p><label for="_sp_edd_fs_coupon">Coupon</label><input id="_sp_edd_fs_coupon" type="text" value="' . esc_attr( $coupon ) . '" name="_sp_edd_fs_coupon"></p>';
echo '<p class="description">Set a coupon if you want to give users a discount</p>';
}
add_filter( 'edd_metabox_fields_save', function($fields){
$fields[] = '_sp_edd_fs_plugin_id';
$fields[] = '_sp_edd_fs_coupon';
return $fields;
});
add_action( 'template_redirect', function(){
if( ! function_exists( 'edd_is_checkout' ) || ! function_exists( 'edd_software_licensing' ) || ! edd_is_checkout() ){
return;
}
$license_key = isset( $_GET['edd_license_key'] ) ? $_GET['edd_license_key'] : '';
$download_id = edd_software_licensing()->get_download_id_by_license( $license_key );
if( ! $download_id ){
return;
}
$plugin_id = get_post_meta( $download_id, '_sp_edd_fs_plugin_id', true );
$coupon = get_post_meta( $download_id, '_sp_edd_fs_coupon', true );
if( !$plugin_id ){
return;
}
$args = [
'license_key' => substr( $license_key, 0, 32 ),
];
if( $coupon ){
$args['coupon'] = $coupon;
}
$url = add_query_arg( $args, "https://checkout.freemius.com/mode/dialog/plugin/{$plugin_id}" );
wp_redirect( $url );
exit;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment