| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* A class to easily manage licenses for Polylang Pro and addons |
| 5 |
* |
| 6 |
* @since 1.9 |
| 7 |
*/ |
| 8 |
class PLL_License { |
| 9 |
public $id, $name, $license_key, $license_data; |
| 10 |
private $file, $version, $author; |
| 11 |
private $api_url = 'https://polylang.pro'; |
| 12 |
|
| 13 |
/** |
| 14 |
* Constructor |
| 15 |
* |
| 16 |
* @since 1.9 |
| 17 |
* |
| 18 |
* @param string $file |
| 19 |
* @param string $item_name |
| 20 |
* @param string $version |
| 21 |
* @param string $author |
| 22 |
* @param string $api_url optional |
| 23 |
*/ |
| 24 |
function __construct( $file, $item_name, $version, $author, $api_url = null ) { |
| 25 |
$this->id = sanitize_title( $item_name ); |
| 26 |
$this->file = $file; |
| 27 |
$this->name = $item_name; |
| 28 |
$this->version = $version; |
| 29 |
$this->author = $author; |
| 30 |
$this->api_url = empty( $api_url ) ? $this->api_url : $api_url; |
| 31 |
|
| 32 |
$licenses = get_option( 'polylang_licenses' ); |
| 33 |
$this->license_key = empty( $licenses[ $this->id ]['key'] ) ? '' : $licenses[ $this->id ]['key']; |
| 34 |
if ( ! empty( $licenses[ $this->id ]['data'] ) ) { |
| 35 |
$this->license_data = $licenses[ $this->id ]['data']; |
| 36 |
} |
| 37 |
|
| 38 |
// Updater |
| 39 |
add_action( 'admin_init', array( $this, 'auto_updater' ), 0 ); |
| 40 |
|
| 41 |
// Register settings |
| 42 |
add_filter( 'pll_settings_licenses', array( $this, 'settings' ) ); |
| 43 |
|
| 44 |
// Weekly schedule |
| 45 |
if ( ! wp_next_scheduled( 'polylang_check_licenses' ) ) { |
| 46 |
wp_schedule_event( time(), 'weekly', 'polylang_check_licenses' ); |
| 47 |
} |
| 48 |
|
| 49 |
add_action( 'polylang_check_licenses', array( $this, 'check_license' ) ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Auto updater |
| 54 |
* |
| 55 |
* @since 1.9 |
| 56 |
*/ |
| 57 |
public function auto_updater() { |
| 58 |
$args = array( |
| 59 |
'version' => $this->version, |
| 60 |
'license' => $this->license_key, |
| 61 |
'author' => $this->author, |
| 62 |
'item_name' => $this->name, |
| 63 |
); |
| 64 |
|
| 65 |
// Setup the updater |
| 66 |
new PLL_Plugin_Updater( $this->api_url, $this->file, $args ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Registers the licence in the Settings |
| 71 |
* |
| 72 |
* @since 1.9 |
| 73 |
* |
| 74 |
* @param array $items |
| 75 |
* @return array |
| 76 |
*/ |
| 77 |
public function settings( $items ) { |
| 78 |
$items[ $this->id ] = $this; |
| 79 |
return $items; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Activate the license key |
| 84 |
* |
| 85 |
* @since 1.9 |
| 86 |
* |
| 87 |
* @param string $license_key activation key |
| 88 |
* @return object updated $this |
| 89 |
*/ |
| 90 |
public function activate_license( $license_key ) { |
| 91 |
$this->license_key = $license_key; |
| 92 |
$this->api_request( 'activate_license' ); |
| 93 |
|
| 94 |
// Tell WordPress to look for updates |
| 95 |
set_site_transient( 'update_plugins', null ); |
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Deactivate the license key |
| 102 |
* |
| 103 |
* @since 1.9 |
| 104 |
* |
| 105 |
* @return object updated $this |
| 106 |
*/ |
| 107 |
public function deactivate_license() { |
| 108 |
$this->api_request( 'deactivate_license' ); |
| 109 |
return $this; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Check if license key is valid |
| 114 |
* |
| 115 |
* @since 1.9 |
| 116 |
* |
| 117 |
* @return object updated $this |
| 118 |
*/ |
| 119 |
public function check_license() { |
| 120 |
$this->api_request( 'check_license' ); |
| 121 |
return $this; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Sends an api request to check, activate or deactivate the license |
| 126 |
* Updates the licenses option according to the status |
| 127 |
* |
| 128 |
* @since 1.9 |
| 129 |
* |
| 130 |
* @param string $request check_license | activate_license | deactivate_license |
| 131 |
*/ |
| 132 |
private function api_request( $request ) { |
| 133 |
$licenses = get_option( 'polylang_licenses' ); |
| 134 |
unset( $licenses[ $this->id ], $this->license_data ); |
| 135 |
|
| 136 |
if ( ! empty( $this->license_key ) ) { |
| 137 |
// Data to send in our API request |
| 138 |
$api_params = array( |
| 139 |
'edd_action' => $request, |
| 140 |
'license' => $this->license_key, |
| 141 |
'item_name' => urlencode( $this->name ), |
| 142 |
'url' => home_url(), |
| 143 |
); |
| 144 |
|
| 145 |
// Call the API |
| 146 |
$response = wp_remote_post( |
| 147 |
$this->api_url, |
| 148 |
array( |
| 149 |
'timeout' => 15, |
| 150 |
'sslverify' => false, |
| 151 |
'body' => $api_params, |
| 152 |
) |
| 153 |
); |
| 154 |
|
| 155 |
// Update the option only if we got a response |
| 156 |
if ( is_wp_error( $response ) ) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
// Save new license info |
| 161 |
$licenses[ $this->id ] = array( 'key' => $this->license_key ); |
| 162 |
$data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 163 |
|
| 164 |
if ( 'deactivated' !== $data->license ) { |
| 165 |
$licenses[ $this->id ]['data'] = $this->license_data = $data; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
update_option( 'polylang_licenses', $licenses ); // FIXME called multiple times when saving all licenses |
| 170 |
} |
| 171 |
} |
| 172 |
|