| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') or die('Jog on!'); |
| 4 |
|
| 5 |
/** |
| 6 |
* Premium license? |
| 7 |
* |
| 8 |
* @return bool |
| 9 |
*/ |
| 10 |
function sh_cd_license_is_premium() { |
| 11 |
return (bool) get_option( 'sh-cd-license-valid', false ); |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Return a link to the upgrade page |
| 16 |
* |
| 17 |
* @return string |
| 18 |
*/ |
| 19 |
function sh_cd_license_upgrade_link() { |
| 20 |
|
| 21 |
$link = admin_url('admin.php?page=sh-cd-shortcode-variables-license'); |
| 22 |
|
| 23 |
return esc_url( $link ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Check an existing license's hash is still valid |
| 28 |
**/ |
| 29 |
function sh_cd_license_validate( $license ) { |
| 30 |
|
| 31 |
if ( true === empty( $license ) ) { |
| 32 |
return __( 'License missing', SH_CD_SLUG ); |
| 33 |
} |
| 34 |
|
| 35 |
// Decode license |
| 36 |
$license = sh_cd_license_decode( $license ); |
| 37 |
|
| 38 |
if ( true === empty( $license ) ) { |
| 39 |
return __( 'Could not decode / verify license', SH_CD_SLUG ); |
| 40 |
} |
| 41 |
|
| 42 |
// Does site hash in license meet this site's actual hash? |
| 43 |
if ( true === empty( $license['site-hash'] ) ) { |
| 44 |
return __( 'Invalid license hash', SH_CD_SLUG ); |
| 45 |
} |
| 46 |
|
| 47 |
// Match this site hash? |
| 48 |
if ( sh_cd_generate_site_hash() !== $license['site-hash']) { |
| 49 |
return __( 'This license doesn\'t appear to be for this site (no match on site hash).', SH_CD_SLUG ); |
| 50 |
} |
| 51 |
|
| 52 |
// Valid date? |
| 53 |
$today_time = strtotime( date( 'Y-m-d' ) ); |
| 54 |
$expire_time = strtotime( $license['expiry-date'] ); |
| 55 |
|
| 56 |
if ( $expire_time < $today_time ) { |
| 57 |
return __( 'This license has expired.', SH_CD_SLUG ); |
| 58 |
} |
| 59 |
|
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Validate and decode a license |
| 65 |
**/ |
| 66 |
function sh_cd_license_decode( $license ) { |
| 67 |
|
| 68 |
if( true === empty( $license ) ) { |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
|
| 72 |
// Base64 and JSON decode |
| 73 |
$license = base64_decode( $license ); |
| 74 |
|
| 75 |
if( false === $license ) { |
| 76 |
return NULL; |
| 77 |
} |
| 78 |
|
| 79 |
$license = json_decode( $license, true ); |
| 80 |
|
| 81 |
if( true === empty( $license ) ) { |
| 82 |
return NULL; |
| 83 |
} |
| 84 |
|
| 85 |
// Validate hash! |
| 86 |
$verify_hash = md5( 'yeken.uk' . $license['type'] . $license['expiry-days'] . $license['site-hash'] . $license['expiry-date'] ); |
| 87 |
|
| 88 |
return ( $license['hash'] == $verify_hash && false === empty( $license ) ) ? $license : NULL; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
/** |
| 93 |
* Validate and apply a license |
| 94 |
**/ |
| 95 |
function sh_cd_license_apply( $license ) { |
| 96 |
|
| 97 |
// Validate license |
| 98 |
$license_result = sh_cd_license_validate($license); |
| 99 |
|
| 100 |
if( true === $license_result ) { |
| 101 |
|
| 102 |
update_option( 'sh-cd-license', $license ); |
| 103 |
update_option( 'sh-cd-license-valid', true ); |
| 104 |
|
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Remove a license |
| 113 |
**/ |
| 114 |
function sh_cd_license_remove() { |
| 115 |
|
| 116 |
delete_option( 'sh-cd-license' ); |
| 117 |
delete_option( 'sh-cd-license-valid' ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Generate a site hash to identify this site. |
| 122 |
**/ |
| 123 |
function sh_cd_generate_site_hash() { |
| 124 |
|
| 125 |
$site_hash = get_option( 'sh-cd-hash' ); |
| 126 |
|
| 127 |
// Generate a basic site key from URL and plugin slug |
| 128 |
if( false == $site_hash ) { |
| 129 |
|
| 130 |
$site_hash = md5( 'yeken-sh-cd-' . site_url() ); |
| 131 |
$site_hash = substr( $site_hash, 0, 6 ); |
| 132 |
|
| 133 |
update_option( 'sh-cd-hash', $site_hash ); |
| 134 |
|
| 135 |
} |
| 136 |
return $site_hash; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Fetch license |
| 141 |
* |
| 142 |
* @return mixed |
| 143 |
*/ |
| 144 |
function sh_cd_license() { |
| 145 |
return get_option( 'sh-cd-license', '' ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Fetch license price |
| 150 |
* |
| 151 |
* @return float|null |
| 152 |
*/ |
| 153 |
function sh_cd_license_price() { |
| 154 |
|
| 155 |
$price = yeken_license_price( 'sv-premium' ); |
| 156 |
|
| 157 |
return ( false === empty( $price ) ) ? $price : SH_CD_PREMIUM_PRICE; |
| 158 |
} |
| 159 |
|
| 160 |
if ( false === function_exists( 'yeken_license_api_fetch_licenses' ) ) { |
| 161 |
|
| 162 |
/** |
| 163 |
* Call out to YeKen API for license prices |
| 164 |
*/ |
| 165 |
function yeken_license_api_fetch_licenses() { |
| 166 |
|
| 167 |
if ( $cache = get_transient( 'yeken_api_prices' ) ) { |
| 168 |
return $cache; |
| 169 |
} |
| 170 |
|
| 171 |
$response = wp_remote_get( 'https://shop.yeken.uk/wp-json/yeken/v1/license-prices/' ); |
| 172 |
|
| 173 |
// All ok? |
| 174 |
if ( 200 === wp_remote_retrieve_response_code( $response ) ) { |
| 175 |
|
| 176 |
$body = wp_remote_retrieve_body( $response ); |
| 177 |
|
| 178 |
if ( false === empty( $body ) ) { |
| 179 |
|
| 180 |
$body = json_decode( $body, true ); |
| 181 |
set_transient( 'yeken_api_prices', $body, 216000 ); // Cache for 6 hours |
| 182 |
|
| 183 |
return $body; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return NULL; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Fetch a certain product price |
| 192 |
* @param $sku |
| 193 |
* @param string $type |
| 194 |
*/ |
| 195 |
function yeken_license_price( $sku, $type = 'yearly' ) { |
| 196 |
|
| 197 |
$licenses = yeken_license_api_fetch_licenses(); |
| 198 |
|
| 199 |
return ( false === empty( $licenses[ $sku ][ $type ] ) ) ? $licenses[ $sku ][ $type ] : NULL; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Render out license prices |
| 204 |
* |
| 205 |
* @param $args |
| 206 |
* @return mixed|string |
| 207 |
*/ |
| 208 |
function yeken_license_shortcode( $args ) { |
| 209 |
|
| 210 |
$args = wp_parse_args( $args, [ 'sku' => 'sv-premium', 'type' => 'yearly', 'prefix' => '£' ] ); |
| 211 |
|
| 212 |
$price = yeken_license_price( $args[ 'sku' ], $args[ 'type' ] ); |
| 213 |
|
| 214 |
if ( false === empty( $price ) ) { |
| 215 |
return sprintf( '%s%d', esc_html( $args[ 'prefix' ] ), $price ); |
| 216 |
} |
| 217 |
|
| 218 |
return ''; |
| 219 |
} |
| 220 |
add_shortcode( 'yeken-license-price', 'yeken_license_shortcode' ); |
| 221 |
|
| 222 |
} |
| 223 |
|