Forked from DeoThemes/freemius-appsumo-ajax-form.php
Created
February 23, 2022 04:51
-
-
Save lkoudal/62da3a60ff2987d8fc45eb8ae782df68 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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