| 1 |
<?php |
| 2 |
namespace WeDevs\ERP; |
| 3 |
|
| 4 |
use WeDevs\ERP\Framework\Traits\Hooker; |
| 5 |
|
| 6 |
/** |
| 7 |
* ERP License handler class |
| 8 |
* |
| 9 |
* @author WP ERP |
| 10 |
*/ |
| 11 |
class License { |
| 12 |
|
| 13 |
use Hooker; |
| 14 |
|
| 15 |
private $api_url = 'https://wperp.com/'; |
| 16 |
|
| 17 |
function __construct( $file, $addon_name, $version, $author, $api_url = null ) { |
| 18 |
|
| 19 |
// bail out if it's a local server |
| 20 |
if ( $this->is_local_server() ) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
$this->file = $file; |
| 25 |
$this->item_name = $addon_name; |
| 26 |
$this->version = $version; |
| 27 |
$this->author = $author; |
| 28 |
$this->api_url = is_null( $api_url ) ? $this->api_url : $api_url; |
| 29 |
$this->short_name = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->item_name ) ) ); |
| 30 |
|
| 31 |
// include and hooks |
| 32 |
$this->includes(); |
| 33 |
$this->init_hooks(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Include the EDD updater class |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
private function includes() { |
| 42 |
if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) { |
| 43 |
require_once __DIR__ . '/lib/EDD_SL_Plugin_Updater.php'; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Check if the current server is localhost |
| 49 |
* |
| 50 |
* @return boolean |
| 51 |
*/ |
| 52 |
private function is_local_server() { |
| 53 |
$is_local = ( in_array( $_SERVER['REMOTE_ADDR'], array( '127.0.0.1', '::1' ) ) || substr( $_SERVER['HTTP_HOST'], -4 ) == '.dev' ); |
| 54 |
|
| 55 |
return apply_filters( 'erp_lc_is_local_server', $is_local ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Init required hooks |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
private function init_hooks() { |
| 64 |
$this->action( 'admin_init', 'init_updater' ); |
| 65 |
$this->action( 'admin_notices', 'license_nag' ); |
| 66 |
|
| 67 |
// scheduled events and checking |
| 68 |
$this->action( 'erp_weekly_scheduled_events', 'license_status_check' ); |
| 69 |
// $this->action( 'admin_init', 'license_status_check' ); // for testing |
| 70 |
$this->action( 'admin_init', 'save_and_activate_license' ); |
| 71 |
|
| 72 |
$this->action( 'in_plugin_update_message-' . plugin_basename( $this->file ), 'plugin_row_license_missing', 10, 2 ); |
| 73 |
|
| 74 |
// register for settings page |
| 75 |
$this->filter( 'erp_settings_licenses', 'register_addon' ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* The option id for the license key |
| 80 |
* |
| 81 |
* @return string |
| 82 |
*/ |
| 83 |
public function get_license_option_key() { |
| 84 |
return 'erp_license_' . $this->short_name; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* The option id for the license key status |
| 89 |
* |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
public function get_license_status_option_key() { |
| 93 |
return $this->get_license_option_key() . '_status'; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get the addon license key |
| 98 |
* |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public function get_license_key() { |
| 102 |
return get_option( $this->get_license_option_key(), '' ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get license status |
| 107 |
* |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
public function get_license_status() { |
| 111 |
return get_option( $this->get_license_status_option_key(), [] ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Register the add-on for the license settings page |
| 116 |
* |
| 117 |
* @param array $settings |
| 118 |
* |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
public function register_addon( $settings ) { |
| 122 |
$settings[] = [ |
| 123 |
'name' => $this->item_name, |
| 124 |
'id' => $this->get_license_option_key(), |
| 125 |
'license' => $this->get_license_key(), |
| 126 |
'version' => $this->version, |
| 127 |
'status' => $this->get_license_status() |
| 128 |
]; |
| 129 |
|
| 130 |
return $settings; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Initialize the plugin updater |
| 135 |
* |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
public function init_updater() { |
| 139 |
$args = [ |
| 140 |
'version' => $this->version, |
| 141 |
'license' => $this->get_license_key(), |
| 142 |
'author' => $this->author, |
| 143 |
'item_name' => $this->item_name, |
| 144 |
'url' => home_url() |
| 145 |
]; |
| 146 |
|
| 147 |
// Setup the updater |
| 148 |
$edd_updater = new \EDD_SL_Plugin_Updater( |
| 149 |
$this->api_url, |
| 150 |
$this->file, |
| 151 |
$args |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
public function api_request( $action = 'check_license' ) { |
| 156 |
// data to send in our API request |
| 157 |
$api_params = array( |
| 158 |
'edd_action'=> $action, |
| 159 |
'license' => $this->get_license_key(), |
| 160 |
'item_name' => urlencode( $this->item_name ), |
| 161 |
'url' => home_url() |
| 162 |
); |
| 163 |
|
| 164 |
// Call the API |
| 165 |
$response = wp_remote_post( $this->api_url, array( |
| 166 |
'timeout' => 15, |
| 167 |
'sslverify' => false, |
| 168 |
'body' => $api_params |
| 169 |
) ); |
| 170 |
|
| 171 |
// make sure the response came back okay |
| 172 |
if ( is_wp_error( $response ) ) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 177 |
|
| 178 |
return $license_data; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Check license status |
| 183 |
* |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
public function license_status_check() { |
| 187 |
$license_key = $this->get_license_key(); |
| 188 |
|
| 189 |
if ( empty( $license_key ) ) { |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
$license_data = $this->api_request(); |
| 194 |
|
| 195 |
if ( $license_data ) { |
| 196 |
update_option( $this->get_license_status_option_key(), $license_data ); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Try activating the license once saved |
| 202 |
* |
| 203 |
* @return void |
| 204 |
*/ |
| 205 |
public function save_and_activate_license() { |
| 206 |
if ( isset( $_POST[ $this->get_license_option_key() ] ) ) { |
| 207 |
$old_key = $this->get_license_key(); |
| 208 |
$new_key = sanitize_text_field( $_POST[ $this->get_license_option_key() ] ); |
| 209 |
|
| 210 |
// delete license status if differs |
| 211 |
if ( $old_key != $new_key ) { |
| 212 |
delete_option( $this->get_license_status_option_key() ); |
| 213 |
update_option( $this->get_license_option_key(), $new_key ); |
| 214 |
} |
| 215 |
|
| 216 |
// don't do anything if we have a valid license |
| 217 |
$license_status = $this->get_license_status(); |
| 218 |
if ( is_object( $license_status ) && 'valid' === $license_status->license ) { |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
// if we have a license key, try to activate now |
| 223 |
$license_key = $this->get_license_key(); |
| 224 |
|
| 225 |
if ( ! empty( $license_key ) ) { |
| 226 |
$license_data = $this->api_request( 'activate_license' ); |
| 227 |
|
| 228 |
if ( $license_data ) { |
| 229 |
update_option( $this->get_license_status_option_key(), $license_data ); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Show license key notice |
| 237 |
* |
| 238 |
* @return void |
| 239 |
*/ |
| 240 |
public function license_nag() { |
| 241 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
$license_key = $this->get_license_key(); |
| 246 |
$url = esc_url( admin_url( 'admin.php?page=erp-settings&tab=erp-license' ) ); |
| 247 |
|
| 248 |
if ( empty( $license_key ) ) { |
| 249 |
$this->show_error( sprintf( __( 'Please <a href="%s">enter</a> <strong>%s</strong> license key to get automatic updates and support', 'erp' ), $url, $this->item_name ) ); |
| 250 |
return; |
| 251 |
} |
| 252 |
|
| 253 |
$license_status = $this->get_license_status(); |
| 254 |
|
| 255 |
if ( is_object( $license_status ) && 'valid' !== $license_status->license ) { |
| 256 |
$this->show_error( sprintf( |
| 257 |
__( 'You have invalid or expired license keys for %s. Please go to the <a href="%s" title="Go to Licenses page">Licenses page</a> to correct this issue.', 'erp' ), |
| 258 |
$this->item_name, |
| 259 |
admin_url( 'admin.php?page=erp-settings&tab=erp-license' ) |
| 260 |
) ); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Show a error message |
| 266 |
* |
| 267 |
* @param string $message |
| 268 |
* |
| 269 |
* @return void |
| 270 |
*/ |
| 271 |
public function show_error( $message ) { |
| 272 |
echo '<div class="error">'; |
| 273 |
echo '<p>' . $message . '</p>'; |
| 274 |
echo '</div>'; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Displays message inline on plugin row that the license key is missing |
| 279 |
* |
| 280 |
* @return void |
| 281 |
*/ |
| 282 |
public function plugin_row_license_missing( $plugin_data, $version_info ) { |
| 283 |
static $showed_imissing_key_message; |
| 284 |
|
| 285 |
$license = $this->get_license_status(); |
| 286 |
|
| 287 |
if ( ( ! is_object( $license ) || 'valid' !== $license->license ) && empty( $showed_imissing_key_message[ $this->get_license_option_key() ] ) ) { |
| 288 |
|
| 289 |
echo ' <strong><a href="' . esc_url( admin_url( 'admin.php?page=erp-settings&tab=erp-license' ) ) . '">' . __( 'Enter valid license key for automatic updates.', 'erp' ) . '</a></strong>'; |
| 290 |
$showed_imissing_key_message[ $this->get_license_option_key() ] = true; |
| 291 |
} |
| 292 |
|
| 293 |
} |
| 294 |
} |
| 295 |
|