Created
September 17, 2015 01:20
-
-
Save dtbaker/33797a2feb267a08c3b4 to your computer and use it in GitHub Desktop.
Server side Visual Composer update script
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
<?php | |
/** | |
* Visual Composer Extended License Updater | |
* Server Side Update Script | |
* Author: dtbaker | |
* Details: http://dtbaker.net/web-development/visual-composer-plugin-updates-in-3rd-party-themes/ | |
* | |
* Upload this script to your website (e.g. yourwebsite.com/vc_update.php) | |
* Adjust the API key to your own | |
* Add the other code into your WordPress theme | |
* Enjoy | |
* | |
*/ | |
define('_VC_API_PERSONAL_TOKEN', 'dfasdfasdfasdfasdfasdf'); // ****** CHANGE ME ******* put YOUR OWN personal token from build.envato.com here | |
define('_VC_EXTENDED_LICENSE_CODE', '12345334-2594-4228-8c61-77773333333'); // ****** CHANGE ME ******* put YOUR OWN Visual Composer extended license code here | |
$json_result = array(); | |
// we get the purchase code from the users theme purchase. | |
// this lets us validate we have a legit theme buyer before we go and give them the latest version of visual composer | |
$purchase_code = isset($_GET['purchase']) ? $_GET['purchase'] : ''; | |
$purchase_code = strtolower(preg_replace('#([a-z0-9]{8})-?([a-z0-9]{4})-?([a-z0-9]{4})-?([a-z0-9]{4})-?([a-z0-9]{12})#','$1-$2-$3-$4-$5',$purchase_code)); | |
if(strlen($purchase_code)==36){ | |
// we have a correctly formatted purchase code! ask the API if it's legit | |
// should really cache results but this URL will only be hit occasionally | |
$result = basic_envato_api( 'v2/market/author/sale?code='.$purchase_code ); | |
if($result && !empty($result['item']['id'])){ | |
// we have a verified purchase! yay! | |
// now we make sure that particular item is allowed to receive free updates to visual composer | |
switch($result['item']['id']){ | |
case '123455': // ****** CHANGE ME ******* Put your Visual Composer enabled Envato item ids in here. | |
case '123456': // ****** CHANGE ME ******* Put your Visual Composer enabled Envato item ids in here. | |
case '123457': // ****** CHANGE ME ******* Put your Visual Composer enabled Envato item ids in here. | |
$vc_update_allowed = true; | |
break; | |
default: | |
// the user hasn't purchased one of our visual composer compatible themes, so don't give it to them! | |
$vc_update_allowed = false; | |
} | |
if($vc_update_allowed){ | |
// all good, we can go and send the latest version of visual composer to this buyer | |
// we get the latest download URL from Envato API and pass that back to the theme PHP code which will handle the download. | |
$download_result = basic_envato_api('v1/market/private/user/download-purchase:'._VC_EXTENDED_LICENSE_CODE.'.json'); | |
if($download_result && !empty($download_result['download-purchase']['wordpress_plugin'])){ | |
$json_result['download_url'] = $download_result['download-purchase']['wordpress_plugin']; | |
} | |
} | |
} | |
} | |
// send our data back to the theme PHP script for download handling. | |
header("Content-type: text/javascript"); | |
echo json_encode($json_result); | |
function basic_envato_api($endpoint){ | |
$ch = curl_init('https://api.envato.com/'.$endpoint); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Authorization: Bearer '. _VC_API_PERSONAL_TOKEN | |
)); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'dtbaker Visual Composer Update'); | |
$response = curl_exec($ch); | |
return json_decode($response,true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment