Last active
May 3, 2021 09:10
-
-
Save zackkatz/f881fd7e221ca3dd6eec to your computer and use it in GitHub Desktop.
Validate Easy Digital Downloads License Key Field in a Gravity Forms Submission
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
/** | |
* Validate a Gravity Forms license submission against EDD remote validation | |
* | |
* In Gravity Forms, for the text field you want to use as the license key entry, | |
* go to Advanced tab, check the "Allow field to be populated dynamically" checkbox, | |
* then enter the value as `edd-{download_id}`, where {download_id} is the, you guessed | |
* it, EDD Download ID. | |
* | |
* @param array $is_valid_and_form_array Gravity Forms passes an array with two keys: `is_valid` (boolean) and `form` (Gravity Forms form array) | |
* @return array Same format as incoming. | |
* @ref https://gist.github.com/zackkatz/f881fd7e221ca3dd6eec | |
*/ | |
function extendd_validate_edd_license( $is_valid_and_form_array ) { | |
extract( $is_valid_and_form_array ); | |
$edd_software_licensing = class_exists( 'EDD_Software_Licensing' ) ? edd_software_licensing() : false; | |
foreach ( $form['fields'] as &$field ) { | |
if ( strpos( $field['cssClass'], 'edd-license' ) !== false && $field['id'] == 6 ) { | |
$download_title = esc_attr( $_POST['input_4'] ); | |
$download = get_page_by_title( $download_title, null, $post_type = 'download' ); | |
$download_id = $download->ID; | |
// Get the submitted value from the posted data | |
$license = esc_attr( $_POST['input_' . $field['id'] ] ); | |
if ( empty( $license ) && !empty( $field['isRequired'] ) ) { | |
$field['validation_message'] = __( 'Your license key is required.' ); | |
break; | |
} | |
// Lets make sure the license key is part of the download selected | |
if ( $edd_software_licensing ) { | |
$temp_download_id = $edd_software_licensing->get_download_by_license( $license ); | |
$key_matches_plugin = $edd_software_licensing->check_item_name( $temp_download_id, $download_title ); | |
if ( !$key_matches_plugin ) { | |
$is_valid = false; | |
$field['failed_validation'] = true; | |
$field['validation_message'] = sprintf( 'Invalid license key for selected plugin "%s".', $download_title ); | |
break; | |
} | |
} | |
// data to send in our API request | |
$api_params = array( | |
'edd_action' => 'check_license', | |
'license' => $license, | |
'item_name' => urlencode( $download_title ), | |
); | |
// Call the custom API. | |
$response = wp_remote_post( 'http://gravityview.co', array( 'timeout' => 35, 'sslverify' => false, 'body' => $api_params ) ); | |
// make sure the response came back okay | |
if ( is_wp_error( $response ) ) { | |
continue; | |
} | |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); | |
// Invalid response | |
if ( !is_object( $license_data ) ) { | |
continue; | |
} | |
// The license data | |
switch( $license_data->license ) { | |
// Licenses are invalid if they're invalid or expired. | |
case 'invalid': | |
$is_valid = false; | |
$field['failed_validation'] = true; | |
$field['validation_message'] = __( 'The license you entered is invalid.' ); | |
break; | |
case 'expired': | |
$is_valid = false; | |
$field['failed_validation'] = true; | |
$field['validation_message'] = sprintf( 'The license you entered expired on %s. Please <a href="%s">login</a> and <a href="%s">renew your license</a> for continued updates and support.', | |
date( 'F j, Y', strtotime( $license_data->expires ) ), | |
esc_url( site_url( '/my-account/' ) ), | |
esc_url( add_query_arg( array( 'edd_license_key' => $license, 'download_id' => $download_id ), site_url( '/checkout/' ) ) ) | |
); | |
break; | |
} | |
} | |
} | |
return array( "is_valid" => $is_valid, "form" => $form ); | |
} | |
add_filter( 'gform_validation', 'extendd_validate_edd_license' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment