Last active
March 12, 2019 07:54
-
-
Save amostajo/b0bd8794e4fe996ec8e3f6ee7d7e8799 to your computer and use it in GitHub Desktop.
PHP license key client: Quick script tutorial
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 | |
use LicenseKeys\Utility\Api; | |
use LicenseKeys\Utility\Client; | |
use LicenseKeys\Utility\LicenseRequest; | |
// ------------------------------------- | |
// ------------------------------------- | |
// ------------------------------------- | |
// ------------------------------------- | |
// NO WORDPRESS -> START | |
/** | |
* Saves and updates the license string. | |
* @param string $key The key used as filename | |
* @param string $license License string. | |
*/ | |
function update_option( $key, $license ) { | |
$filename = $key . '.key'; | |
// Delete if license is null | |
if ( $license === null && is_file( $filename ) ) { | |
return unlink( $filename ); | |
} | |
// Open or create and save | |
$file = fopen( $filename, 'w' ) or die( 'Unable to open or create file: '.$filename ); | |
fwrite( $file , $license ); | |
fclose( $file ); | |
} | |
/** | |
* Returns license string found. | |
* @param string $key The key used as filename | |
*/ | |
function get_option( $key ) { | |
$filename = $key . '.key'; | |
// If no file return null | |
if ( ! is_file( $filename ) ) return; | |
// Read and return | |
$file = fopen( $filename, 'r' ) or die( 'Unable to open file: ' . $filename ); | |
$license = fread( $file, filesize( $filename ) ); | |
fclose( $file ); | |
return $license; | |
} | |
// NO WORDPRESS -> END | |
// ------------------------------------- | |
// ------------------------------------- | |
// ------------------------------------- | |
// ------------------------------------- | |
define( 'PHOTOMEME_OPTION_KEY', '_photomeme_string' ); | |
/** | |
* Method used to activate the product. Returns true or false. | |
* @param string $license_key A customer license key. | |
* @return bool | |
*/ | |
function photomeme_activate( $license_key ) { | |
$response = Api::activate( | |
Client::instance(), | |
function() use( $license_key ) { | |
return LicenseRequest::create( | |
'https://tutorials.net/wp-admin/admin-ajax.php', | |
'fake007', | |
'PHOTOMEME', | |
$license_key , | |
LicenseRequest::DAILY_FREQUENCY | |
); | |
}, | |
function( $license ) { | |
update_option( PHOTOMEME_OPTION_KEY, $license, true ); | |
} | |
); | |
return $response->error === false; | |
} | |
/** | |
* Returns true or false. | |
* @return bool | |
*/ | |
function photomeme_is_valid() { | |
return Api::validate( | |
Client::instance(), | |
function() { | |
return new LicenseRequest( get_option( PHOTOMEME_OPTION_KEY) ); | |
}, | |
function( $license ) { | |
update_option( PHOTOMEME_OPTION_KEY, $license ); | |
} | |
); | |
} | |
/** | |
* Returns true or false. | |
* @return bool | |
*/ | |
function photomeme_deactivate() { | |
$response = Api::deactivate( | |
Client::instance(), | |
function() { | |
return new LicenseRequest( get_option( PHOTOMEME_OPTION_KEY) ); | |
}, | |
function( $license ) { | |
if ($license === null) | |
update_option( PHOTOMEME_OPTION_KEY, null ); | |
} | |
); | |
return $response->error === false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment