Created
December 1, 2012 23:22
-
-
Save thefrosty/4185877 to your computer and use it in GitHub Desktop.
Activation of plugin through EDD
This file contains 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
function validate_license() { | |
//print '<pre>' . print_r( $_POST, true ) . '</pre>'; | |
//print '<pre>' . print_r( $this->settings_sections, true ) . '</pre>'; | |
//exit; | |
$data = array(); | |
foreach ( $this->settings_sections as $section ) { | |
if ( !isset( $_POST[$section['id']] ) ) | |
return; | |
if ( !isset( $_POST[$section['id']]['license_key'] ) || empty( $_POST[$section['id']]['license_key'] ) ) | |
return; | |
$is_active = $this->get_option( 'license_active', $section['id'], '' ); | |
if ( 'active' == $is_active ) | |
return; | |
$license = sanitize_text_field( $_POST[$section['id']]['license_key'] ); | |
// data to send in our API request | |
$api_params = array( | |
'edd_action'=> 'activate_license', | |
'license' => $license, | |
'item_name' => urlencode( $section['title'] ) // the name of our product in EDD | |
); | |
// Call the custom API. | |
$response = wp_remote_get( add_query_arg( $api_params, $this->api_url ) ); | |
// make sure the response came back okay | |
if ( is_wp_error( $response ) ) { | |
wp_redirect( $this->get_settings_url( 'response_error' ) ); | |
exit; | |
} | |
// decode the license data | |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); | |
$settings = get_option( $section['id'], array() ); | |
$settings['license_key'] = trim( $license ); | |
$settings['license_active'] = trim( $license_data->license ); | |
update_option( $section['id'], $settings ); | |
unset( $settings ); | |
if ( 'active' == $license_data->license ) { | |
$data[] = 'activated'; | |
wp_redirect( $this->get_settings_url( 'valid_api_key', urlencode( $section['title'] ) ) ); | |
exit; | |
} else { | |
wp_redirect( $this->get_settings_url( 'invalid_api_key', urlencode( $section['title'] ) ) ); | |
exit; | |
} | |
} | |
if ( is_wp_error( $errors ) ) { | |
} | |
return false; | |
} |
I would record the errors inside of the foreach and then grab all of the errors and append them as query args to the redirect after the foreach.
That might do the trick. I'll play with that.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Right now everything works fine. This is in a class that each of my plugins can extend and add it's own option (hence the $this->settings_sections). As I added a second section the first one redirects and never gets to the second. I know I need to remove the wp_redirect's now, just want to trigger each loop then pass the redirect keys for notification.