<?php
// This is the secret key for API authentication.
// This key will match the 'Secret Key for License Verification Requests' Key in the Software License Manager plugin settings
define( 'TS_EPHEMERIS_SECRET_KEY', '5dc29fc562f057.09528500' );
// This is the URL where API query request will be sent to.
// This should be the URL of the site where you have installed the Software License Manager plugin.
define( 'TS_STORE_URL', 'https://www.themeshopy.com/' );
// This is a value that will be recorded in the Software License Manager data so you can identify licenses for this item/product.
define( 'TS_ITEM_REFERENCE', 'Advance Education Pro' );
/**
 * Create our License Management settings page admin option and call the function to create our settings page
 */
function ts_product_license_menu() {
    
     add_theme_page('TS License Activation Menu', 'TS Key Activation', 'manage_options', 'ts-key-activation', 'ts_product_license_page');
}
add_action( 'admin_menu', 'ts_product_license_menu' );
/**
 * Create our License Management settings page
 */
function ts_product_license_page() {
    $license = get_option( 'ts_product_license_key' );
    $status = get_option( 'ts_product_license_status' );
    ?>
    <div class="wrap">
        <h2 class="key_head"><?php echo esc_html( 'Advance Education Pro License Management','advance-education-pro' ); ?></h2>
        <form method="post" action="options.php" class="ts_key_activation_form">

            <?php settings_fields( 'ts_product_license' ); ?>

            <table class="form-table">
                <tbody>
                    <?php if ( $status != 'valid' ) { ?>

                        <tr valign="top">
                            <th scope="row" valign="top">
                                <?php  echo esc_html( 'License Key','advance-education-pro' ); ?>
                            </th>
                            <td>
                                <input id="ts_product_license_key" name="ts_product_license_key" type="text" class="regular-text" value="<?php echo esc_attr( $license ); ?>" />
                                <p class="description" for="ts_product_license_key"><?php echo esc_html( '( Enter your license key here )','advance-education-pro' ); ?></p>
                                <div class="ts_info"></div>
                                
                            </td>
                        </tr>
                    <?php } ?>

                    <?php if ( $license != '' ) { ?>
                        <tr valign="top">

                                <?php if ( $status !== false && $status == 'valid' ) { ?>
                                    <th scope="row" valign="top">
                                        <?php echo esc_html( 'License Status','advance-education-pro' ); ?>
                                    </th>
                                    <td>
                                    <span class="license_ad_status" style="color:green; line-height:30px; padding-right:10px"><?php echo esc_html( 'Active' ); ?></span>
                                    
                                    <input type="hidden" class="button-secondary" name="ts_license_clientside_activation_status" value="<?php echo esc_html( 'Active','advance-education-pro' ); ?>"/>
                                    <?php wp_nonce_field( 'ts_product_nonce', 'ts_product_nonce' ); ?>
                                        <input type="submit" class="button-secondary" name="ts_license_deactivate" value="<?php echo esc_html( 'Deactivate License','advance-education-pro' ); ?>"/>
                                    </td>
                                <?php } else {
                                    wp_nonce_field( 'ts_product_nonce', 'ts_product_nonce' ); ?>
                                    <th scope="row" valign="top">
                                        <?php echo esc_html( 'Activate License','advance-education-pro' ); ?>
                                    </th>
                                    <td>
                                        <input type="submit" class="button-secondary" name="ts_license_activate" value="<?php echo esc_html( 'Activate License','advance-education-pro' ); ?>"/>
                                    </td>

                                <?php } ?>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
            <?php
            if ( empty( $license ) || $status != 'valid' ) {
                // If there's a valid license, we don't need to display the Save Changes submit button
                submit_button();
            }
            ?>

        </form>
    <?php
}
/**
 * Create our settings in the options table
 */
function ts_product_register_option() {
    register_setting( 'ts_product_license', 'ts_product_license_key', 'ts_product_sanitize_license' );
}
add_action( 'admin_init', 'ts_product_register_option' );
/**
 * If we've entered a new key, make sure the old status is removed first as it will need to be reactivated
 */
function ts_product_sanitize_license( $new_key ) {
    $old_key = get_option( 'ts_product_license_key' );
    if( $old_key && $old_key != $new_key ) {
        delete_option( 'ts_product_license_status' );
    }
    return $new_key;
}
/**
 * Check with our Software License Manager API if we can activate the License Key
 */
function ts_product_activate_license() {
    $return_val = false;
    // Listen for our activate button to be clicked
    if( isset( $_POST['ts_license_activate'] ) ) {
        // Check our nonce to validate the form request came from the current site and not somewhere else
        if( ! check_admin_referer( 'ts_product_nonce', 'ts_product_nonce' ) ) {
            return;
        }
        // Retrieve the License Key from the database
        $license = trim( get_option( 'ts_product_license_key' ) );
        $api_params = array(
            'secret_key' => TS_EPHEMERIS_SECRET_KEY,
            'slm_action' => 'slm_activate',
            'license_key' => $license,
            'registered_domain' => home_url(),
            'item_reference' => TS_ITEM_REFERENCE
            );
        // Call the Software License Manager API.
        $response = wp_remote_get(
            add_query_arg( $api_params, trailingslashit( TS_STORE_URL ) ),
            array(
                'timeout' => 15,
                'sslverify' => false
            )
        );

        //var_dump($response); exit();
        // Make sure the response returned ok before continuing any further
        if ( is_wp_error( $response ) ) {
            return false;
        }
        if ( is_array( $response ) ) {
            $json = $response['body'];
            $json = preg_replace( '/[\x00-\x1F\x80-\xFF]/', '', utf8_encode( $json ) );
            $license_data = json_decode( $json );
        }
        if ( $license_data->result == 'success' ) {
            // If activated successfully, update our status
            update_option( 'ts_product_license_status', 'valid' );
            update_option( 'ts_license_clientside_activation_status', 'Active' );

             $return_val = true;
        }
        else {
            update_option( 'ts_product_license_status', 'invalid' );
            $return_val = false;
        }
    }
     return $return_val;
}
add_action( 'admin_init', 'ts_product_activate_license' );
/**
 * Check with our Software License Manager API if we can deactivate the License Key
 */
function ts_product_deactivate_license() {
    $return_val = false;
    $domain_already_inactive = 80;
    // listen for our deactivate button to be clicked
    if( isset( $_POST['ts_license_deactivate'] ) ) {
        // Check our nonce to validate the form request came from the current site and not somewhere else
        if( ! check_admin_referer( 'ts_product_nonce', 'ts_product_nonce' ) ) {
            return;
        }
        // Retrieve the License Key from the database
        $license = trim( get_option( 'ts_product_license_key' ) );
        $api_params = array(
        'secret_key' => TS_EPHEMERIS_SECRET_KEY,
        'slm_action' => 'slm_deactivate',
        'license_key' => $license,
        'registered_domain' => home_url(),
        'item_reference' => TS_ITEM_REFERENCE
        );
        // Call the Software License Manager API.
        $response = wp_remote_get(
            add_query_arg( $api_params, trailingslashit( TS_STORE_URL ) ),
            array(
                'timeout' => 15,
                'sslverify' => false
            )
        );
        // Make sure the response returned ok before continuing any further
        if ( is_wp_error( $response ) ) {
            return false;
        }
        if ( is_array( $response ) ) {
            $json = $response['body'];
            $json = preg_replace( '/[\x00-\x1F\x80-\xFF]/', '', utf8_encode( $json ) );
            $license_data = json_decode( $json );
        }
        if ( $license_data->result == 'success' || $license_data->error_code == $domain_already_inactive ) {
            // If deactivated successfully or if it's already been deactivated previously, update our status
            delete_option( 'ts_product_license_status' );
            $return_val = true;
        }
        else {
            $return_val = false;
        }
    }
    return $return_val;
}
add_action( 'admin_init', 'ts_product_deactivate_license' );

/**
 * Check with our Software License Manager API to verify the License Key
 */
function ts_product_activated_license_verification() {

    $license = trim( get_option( 'ts_product_license_key' ) );

    if($license !=''){

        $status = get_option( 'ts_product_license_status' );
        $activation_status = get_option( 'ts_license_clientside_activation_status' );

        $api_params = array(
            'secret_key' => TS_EPHEMERIS_SECRET_KEY,
            'slm_action' => 'slm_check',
            'license_key' => $license,
            'registered_domain' => home_url(),
            'item_reference' => TS_ITEM_REFERENCE
            );
        // Call the Software License Manager API.
        $response = wp_remote_get(
            add_query_arg( $api_params, trailingslashit( TS_STORE_URL ) ),
            array(
                'timeout' => 15,
                'sslverify' => false
            )
        );

        // var_dump($response);

        // Make sure the response returned ok before continuing any further
        if ( is_wp_error( $response ) ) {
            return false;
        }
        if ( is_array( $response ) ) {
            $json = $response['body'];
            $json = preg_replace( '/[\x00-\x1F\x80-\xFF]/', '', utf8_encode( $json ) );
            $license_data = json_decode( $json );
        }
        
        if ( $license_data->result == 'error' ) {
            $license_data->status = '';
            add_action( 'admin_notices', 'license_key_invalid_notice' );
        }

        if ( $license_data->status == 'blocked' ) {
            delete_option( 'ts_product_license_status' );
            add_action( 'admin_notices', 'license_key_blocked_notice' );
        }
        elseif( $license_data->status == 'expired'){
            add_action( 'admin_notices', 'license_key_expired_notice' );
        }
    }

}
add_action( 'admin_init', 'ts_product_activated_license_verification' );

function varifying_status_key_activation(){
    $sample_license_key = get_option('ts_product_license_key');
    $status = get_option( 'ts_product_license_status' );

    if($status){
        return true;
    }
    else{
        return false;
    }

}
add_action('init','varifying_status_key_activation');

function license_key_blocked_notice() {
   ?>
   <div class="notice notice-error my-dismiss-notice is-dismissible">
      <p><?php echo esc_html( 'Your theme license key has blocked. Please contact your services provider for the License key activation.', 'advance-education-pro' );?></p>
   </div>
   <?php 
}

function license_key_expired_notice() {
   ?>
   <div class="notice notice-info my-dismiss-notice is-dismissible">
      <p><?php echo esc_html( 'Your theme license key has expired. Please renew it.', 'advance-education-pro' );?></p>
   </div>
   <?php 
}

function license_key_invalid_notice() {
   ?>
   <div class="notice notice-error my-dismiss-notice is-dismissible">
      <p><?php echo esc_html( 'License key is invalid. Please check it once.', 'advance-education-pro' );?></p>
   </div>
   <?php 
}