seancojr
8/6/2013 - 5:01 PM

activation-deactivation.php

<?php
   add_action( 'admin_init', 'wprss_et_activate_deactivate_license' );    
    /**
     * Handles the activation/deactivation process 
     * 
     * @since 1.1
     */
    function wprss_et_activate_deactivate_license() {
     
        // listen for our activate button to be clicked
        if( isset( $_POST['wprss_et_license_activate'] ) || isset( $_POST['wprss_et_license_deactivate'] ) ) {
    
            // run a quick security check 
            if( ! check_admin_referer( 'edd_sample_nonce', 'edd_sample_nonce' ) )   
                return; // get out if we didn't click the Activate/Deactivate button
     
            // retrieve the license from the database
            $license = trim( get_option( 'wprss_et_license_key' ) );
     
            if ( isset( $_POST['wprss_et_license_activate'] ) ) {
                // data to send in our API request
                $api_params = array( 
                    'edd_action'=> 'activate_license', 
                    'license'   => $license, 
                    'item_name' => urlencode( WPRSS_ET_SL_ITEM_NAME ) // the name of our product in EDD
                );
            }

            else if ( isset( $_POST['wprss_et_license_deactivate'] ) ) {
                // data to send in our API request
                $api_params = array( 
                    'edd_action'=> 'deactivate_license', 
                    'license'   => $license, 
                    'item_name' => urlencode( WPRSS_ET_SL_ITEM_NAME ) // the name of our product in EDD
                );
            }

            // Call the custom API.
            $response = wp_remote_get( add_query_arg( $api_params, WPRSS_ET_SL_STORE_URL ) );
     
            // make sure the response came back okay
            if ( is_wp_error( $response ) )
                return false;
     
            // decode the license data
            $license_data = json_decode( wp_remote_retrieve_body( $response ) );
     
            // $license_data->license will be either "active" or "inactive"
     
            update_option( 'wprss_et_license_status', $license_data->license );
     
        }
    }