Skip to content

Instantly share code, notes, and snippets.

@DeoThemes
Last active February 23, 2022 04:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DeoThemes/d76c870f252551aee51d15f2207fde87 to your computer and use it in GitHub Desktop.
Save DeoThemes/d76c870f252551aee51d15f2207fde87 to your computer and use it in GitHub Desktop.
<?php
// AppSumo AJAX form submission
add_action( 'wp_ajax_nopriv_deo_appsumo_submission', 'deo_appsumo_submission' );
add_action( 'wp_ajax_deo_appsumo_submission', 'deo_appsumo_submission' );
function deo_appsumo_submission() {
check_ajax_referer( 'deo_ajax_nonce', 'nonce' );
$consent = false;
// Sanitize input data
if ( ! empty( $_POST['name'] ) ) {
$name = filter_var( $_POST['name'], FILTER_SANITIZE_STRING );
}
if ( ! empty( $_POST['email'] ) ) {
$email = filter_var( $_POST['email'], FILTER_SANITIZE_EMAIL );
}
if ( ! empty( $_POST['appsumoCode'] ) ) {
$appsumo_code = $_POST['appsumoCode'];
}
if ( 'true' === $_POST['consent'] ) {
$consent = true;
}
// Use Freemius API to redeem the license key and associate it with the buyer’s details
require_once DEOTHEME_DIR . '/includes/freemius-php-sdk-master/freemius/FreemiusBase.php';
require_once DEOTHEME_DIR . '/includes/freemius-php-sdk-master/freemius/Freemius.php';
define( 'FS__API_SCOPE', 'developer' );
define( 'FS__API_DEV_ID', 1234 );
define( 'FS__API_PUBLIC_KEY', 'pk_YOUR_PUBLIC_KEY' );
define( 'FS__API_SECRET_KEY', 'sk_YOUR_SECRET_KEY' );
// Init SDK.
$api = new Freemius_Api(FS__API_SCOPE, FS__API_DEV_ID, FS__API_PUBLIC_KEY, FS__API_SECRET_KEY);
// Create license
$license = $api->Api("/plugins/6922/licenses.json", 'PUT', array(
'license_key' => urlencode($appsumo_code),
'email' => $email,
'name' => $name,
'is_marketing_allowed' => $consent,
));
if ( is_object( $license ) && ! empty( $license->user_id ) && is_numeric( $license->user_id ) ) {
// Successful activation, email sent.
wp_send_json_success( esc_html__( 'Successfuly redeemed, check your email.', 'elementorkit' ) );
} else if ( ! empty( $license->error ) ) {
$error = $license->error->message;
wp_send_json_error( esc_html( $error ) );
} else {
// Unexpected message.
wp_send_json_error( esc_html__( 'Something went wrong, please try again later.', 'elementorkit' ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment