| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
die( 'You are not allowed to call this page directly.' ); |
| 5 |
} |
| 6 |
|
| 7 |
class FrmAddon { |
| 8 |
public $store_url = 'https://formidableforms.com'; |
| 9 |
public $download_id; |
| 10 |
public $plugin_file; |
| 11 |
public $plugin_folder; |
| 12 |
public $plugin_name; |
| 13 |
public $plugin_slug; |
| 14 |
public $option_name; |
| 15 |
public $version; |
| 16 |
public $author = 'Strategy11'; |
| 17 |
private $license; |
| 18 |
|
| 19 |
public function __construct() { |
| 20 |
|
| 21 |
if ( empty( $this->plugin_slug ) ) { |
| 22 |
$this->plugin_slug = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->plugin_name ) ) ); |
| 23 |
} |
| 24 |
if ( empty( $this->option_name ) ) { |
| 25 |
$this->option_name = 'edd_' . $this->plugin_slug . '_license_'; |
| 26 |
} |
| 27 |
|
| 28 |
$this->plugin_folder = plugin_basename( $this->plugin_file ); |
| 29 |
$this->license = $this->get_license(); |
| 30 |
|
| 31 |
add_filter( 'frm_installed_addons', array( &$this, 'insert_installed_addon' ) ); |
| 32 |
$this->edd_plugin_updater(); |
| 33 |
} |
| 34 |
|
| 35 |
public static function load_hooks() { |
| 36 |
add_filter( 'frm_include_addon_page', '__return_true' ); |
| 37 |
//new static(); |
| 38 |
} |
| 39 |
|
| 40 |
public function insert_installed_addon( $plugins ) { |
| 41 |
$plugins[ $this->plugin_slug ] = $this; |
| 42 |
return $plugins; |
| 43 |
} |
| 44 |
|
| 45 |
public static function get_addon( $plugin_slug ) { |
| 46 |
$plugins = apply_filters( 'frm_installed_addons', array() ); |
| 47 |
$plugin = false; |
| 48 |
if ( isset( $plugins[ $plugin_slug ] ) ) { |
| 49 |
$plugin = $plugins[ $plugin_slug ]; |
| 50 |
} |
| 51 |
return $plugin; |
| 52 |
} |
| 53 |
|
| 54 |
public function edd_plugin_updater() { |
| 55 |
|
| 56 |
$this->is_license_revoked(); |
| 57 |
$license = $this->license; |
| 58 |
|
| 59 |
if ( empty( $license ) ) { |
| 60 |
add_action( 'after_plugin_row_' . plugin_basename( $this->plugin_file ), array( $this, 'show_license_message' ), 10, 2 ); |
| 61 |
} else { |
| 62 |
|
| 63 |
// setup the updater |
| 64 |
$api_data = array( |
| 65 |
'version' => $this->version, |
| 66 |
'license' => $license, |
| 67 |
'author' => $this->author, |
| 68 |
); |
| 69 |
if ( is_numeric( $this->download_id ) ) { |
| 70 |
$api_data['item_id'] = $this->download_id; |
| 71 |
} |
| 72 |
|
| 73 |
$edd = new FrmEDD_SL_Plugin_Updater( $this->store_url, $this->plugin_file, $api_data ); |
| 74 |
if ( 'formidable/formidable.php' === $this->plugin_folder ) { |
| 75 |
remove_filter( 'plugins_api', array( $edd, 'plugins_api_filter' ), 10, 3 ); |
| 76 |
} |
| 77 |
|
| 78 |
add_filter( 'site_transient_update_plugins', array( &$this, 'clear_expired_download' ) ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public function get_license() { |
| 83 |
$license = trim( get_option( $this->option_name . 'key' ) ); |
| 84 |
if ( empty( $license ) ) { |
| 85 |
$license = $this->activate_defined_license(); |
| 86 |
} |
| 87 |
return $license; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Activate the license in wp-config.php |
| 92 |
* @since 2.04 |
| 93 |
*/ |
| 94 |
public function activate_defined_license() { |
| 95 |
$license = $this->get_defined_license(); |
| 96 |
if ( ! empty( $license ) && ! $this->is_active() && $this->is_time_to_auto_activate() ) { |
| 97 |
$response = $this->activate_license( $license ); |
| 98 |
$this->set_auto_activate_time(); |
| 99 |
if ( ! $response['success'] ) { |
| 100 |
$license = ''; |
| 101 |
} |
| 102 |
} |
| 103 |
return $license; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Check the wp-config.php for the license key |
| 108 |
* @since 2.04 |
| 109 |
*/ |
| 110 |
public function get_defined_license() { |
| 111 |
$consant_name = 'FRM_' . strtoupper( $this->plugin_slug ) . '_LICENSE'; |
| 112 |
return defined( $consant_name ) ? constant( $consant_name ) : false; |
| 113 |
} |
| 114 |
|
| 115 |
public function set_license( $license ) { |
| 116 |
update_option( $this->option_name . 'key', $license ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* If the license is in the config, limit the frequency of checks. |
| 121 |
* The license may be entered incorrectly, so we don't want to check on every page load. |
| 122 |
* @since 2.04 |
| 123 |
*/ |
| 124 |
private function is_time_to_auto_activate() { |
| 125 |
$last_try = get_option( $this->option_name . 'last_activate' ); |
| 126 |
return ( ! $last_try || $last_try < strtotime( '-1 day' ) ); |
| 127 |
} |
| 128 |
|
| 129 |
private function set_auto_activate_time() { |
| 130 |
update_option( $this->option_name . 'last_activate', time() ); |
| 131 |
} |
| 132 |
|
| 133 |
public function is_active() { |
| 134 |
return get_option( $this->option_name . 'active' ); |
| 135 |
} |
| 136 |
|
| 137 |
public function clear_license() { |
| 138 |
delete_option( $this->option_name . 'active' ); |
| 139 |
delete_option( $this->option_name . 'key' ); |
| 140 |
delete_site_transient( $this->transient_key() ); |
| 141 |
delete_transient( 'frm_api_licence' ); |
| 142 |
} |
| 143 |
|
| 144 |
public function set_active( $is_active ) { |
| 145 |
update_option( $this->option_name . 'active', $is_active ); |
| 146 |
delete_transient( 'frm_api_licence' ); |
| 147 |
} |
| 148 |
|
| 149 |
public function show_license_message( $file, $plugin ) { |
| 150 |
$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' ); |
| 151 |
echo '<tr class="plugin-update-tr active"><td colspan="' . esc_attr( $wp_list_table->get_column_count() ) . '" class="plugin-update colspanchange"><div class="update-message">'; |
| 152 |
|
| 153 |
/* translators: %1$s: Plugin name, %2$s: Start link HTML, %3$s: end link HTML */ |
| 154 |
printf( esc_html__( 'Your %1$s license key is missing. Please add it on the %2$slicenses page%3$s.', 'formidable' ), esc_html( $this->plugin_name ), '<a href="' . esc_url( admin_url('admin.php?page=formidable-settings&t=licenses_settings' ) ) . '">', '</a>' ); |
| 155 |
$id = sanitize_title( $plugin['Name'] ); |
| 156 |
echo '<script type="text/javascript">var d = document.getElementById("' . esc_attr( $id ) . '");if ( d !== null ){ d.className = d.className + " update"; }</script>'; |
| 157 |
echo '</div></td></tr>'; |
| 158 |
} |
| 159 |
|
| 160 |
public function clear_expired_download( $transient ) { |
| 161 |
if ( ! is_object( $transient ) ) { |
| 162 |
return $transient; |
| 163 |
} |
| 164 |
|
| 165 |
if ( $this->is_current_version( $transient ) ) { |
| 166 |
//make sure it doesn't show there is an update if plugin is up-to-date |
| 167 |
if ( isset( $transient->response[ $this->plugin_folder ] ) ) { |
| 168 |
unset( $transient->response[ $this->plugin_folder ] ); |
| 169 |
} |
| 170 |
} elseif ( isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) ) { |
| 171 |
$cache_key = $this->version_cache_key(); |
| 172 |
$version_info = get_transient( $cache_key ); |
| 173 |
|
| 174 |
$this->clear_old_plugin_version( $version_info ); |
| 175 |
|
| 176 |
if ( false !== $version_info && version_compare( $version_info->new_version, $this->version, '>' ) ) { |
| 177 |
$transient->response[ $this->plugin_folder ] = $version_info; |
| 178 |
} else { |
| 179 |
delete_transient( $cache_key ); |
| 180 |
if ( ! $this->has_been_cleared() ) { |
| 181 |
// if the transient has expired, clear the update and trigger it again |
| 182 |
$this->cleared_plugins(); |
| 183 |
$this->manually_queue_update(); |
| 184 |
} |
| 185 |
|
| 186 |
unset( $transient->response[ $this->plugin_folder ] ); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
return $transient; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* @since 2.05.05 |
| 196 |
*/ |
| 197 |
private function version_cache_key() { |
| 198 |
return 'edd_plugin_' . md5( sanitize_key( $this->license . $this->version ) . '_get_version' ); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* make sure transients don't stick around on sites that |
| 203 |
* don't save the transient expiration |
| 204 |
* |
| 205 |
* @since 2.05.05 |
| 206 |
*/ |
| 207 |
private function clear_old_plugin_version( &$version_info ) { |
| 208 |
if ( false !== $version_info ) { |
| 209 |
|
| 210 |
$cache_key = $this->version_cache_key(); |
| 211 |
$expiration = get_option( '_transient_timeout_' . $cache_key ); |
| 212 |
|
| 213 |
if ( false === $expiration ) { |
| 214 |
$last_checked = ( is_array( $version_info->sections ) && isset( $version_info->sections['last_checked'] ) ) ? $version_info->sections['last_checked'] : 0; |
| 215 |
|
| 216 |
if ( $last_checked < strtotime( '-48 hours' ) ) { |
| 217 |
$version_info = false; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
private function is_current_version( $transient ) { |
| 224 |
if ( empty( $transient->checked ) || ! isset( $transient->checked[ $this->plugin_folder ] ) ) { |
| 225 |
return false; |
| 226 |
} |
| 227 |
|
| 228 |
$response = ! isset( $transient->response ) || empty( $transient->response ); |
| 229 |
if ( $response ) { |
| 230 |
return true; |
| 231 |
} |
| 232 |
|
| 233 |
return isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) && $transient->checked[ $this->plugin_folder ] === $transient->response[ $this->plugin_folder ]->new_version; |
| 234 |
} |
| 235 |
|
| 236 |
private function has_been_cleared() { |
| 237 |
$last_cleared = get_option( 'frm_last_cleared' ); |
| 238 |
return ( $last_cleared && $last_cleared > date( 'Y-m-d H:i:s', strtotime( '-5 minutes' ) ) ); |
| 239 |
} |
| 240 |
|
| 241 |
private function cleared_plugins() { |
| 242 |
update_option( 'frm_last_cleared', date( 'Y-m-d H:i:s' ) ); |
| 243 |
} |
| 244 |
|
| 245 |
private function is_license_revoked() { |
| 246 |
if ( empty( $this->license ) || empty( $this->plugin_slug ) || isset( $_POST['license'] ) ) { |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
$last_checked = get_site_option( $this->transient_key() ); |
| 251 |
$seven_days_ago = date( 'Y-m-d H:i:s', strtotime( '-7 days' ) ); |
| 252 |
|
| 253 |
if ( ! $last_checked || $last_checked < $seven_days_ago ) { |
| 254 |
update_site_option( $this->transient_key(), date( 'Y-m-d H:i:s' ) ); // check weekly |
| 255 |
$response = $this->get_license_status(); |
| 256 |
if ( 'revoked' === $response['status'] ) { |
| 257 |
$this->clear_license(); |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
private function transient_key() { |
| 263 |
return 'frm_' . md5( sanitize_key( $this->license . '_' . $this->plugin_slug ) ); |
| 264 |
} |
| 265 |
|
| 266 |
public static function activate() { |
| 267 |
FrmAppHelper::permission_check( 'frm_change_settings' ); |
| 268 |
check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 269 |
|
| 270 |
if ( ! isset( $_POST['license'] ) || empty( $_POST['license'] ) ) { |
| 271 |
wp_die( json_encode( array( |
| 272 |
'message' => __( 'Oops! You forgot to enter your license number.', 'formidable' ), |
| 273 |
'success' => false, |
| 274 |
) ) ); |
| 275 |
} |
| 276 |
|
| 277 |
$license = stripslashes( sanitize_text_field( $_POST['license'] ) ); |
| 278 |
$plugin_slug = sanitize_text_field( $_POST['plugin'] ); |
| 279 |
$this_plugin = self::get_addon( $plugin_slug ); |
| 280 |
$response = $this_plugin->activate_license( $license ); |
| 281 |
|
| 282 |
echo json_encode( $response ); |
| 283 |
wp_die(); |
| 284 |
} |
| 285 |
|
| 286 |
private function activate_license( $license ) { |
| 287 |
$this->set_license( $license ); |
| 288 |
$this->license = $license; |
| 289 |
|
| 290 |
$response = $this->get_license_status(); |
| 291 |
$response['message'] = ''; |
| 292 |
$response['success'] = false; |
| 293 |
|
| 294 |
if ( $response['error'] ) { |
| 295 |
$response['message'] = $response['status']; |
| 296 |
} else { |
| 297 |
$messages = $this->get_messages(); |
| 298 |
if ( is_string( $response['status'] ) && isset( $messages[ $response['status'] ] ) ) { |
| 299 |
$response['message'] = $messages[ $response['status'] ]; |
| 300 |
} else { |
| 301 |
$response['message'] = FrmAppHelper::kses( $response['status'], array( 'a' ) ); |
| 302 |
} |
| 303 |
|
| 304 |
$is_valid = false; |
| 305 |
if ( 'valid' === $response['status'] ) { |
| 306 |
$is_valid = 'valid'; |
| 307 |
$response['success'] = true; |
| 308 |
} |
| 309 |
$this->set_active( $is_valid ); |
| 310 |
} |
| 311 |
|
| 312 |
return $response; |
| 313 |
} |
| 314 |
|
| 315 |
private function get_license_status() { |
| 316 |
$response = array( |
| 317 |
'status' => 'missing', |
| 318 |
'error' => true, |
| 319 |
); |
| 320 |
if ( empty( $this->license ) ) { |
| 321 |
$response['error'] = false; |
| 322 |
return $response; |
| 323 |
} |
| 324 |
|
| 325 |
try { |
| 326 |
$response['error'] = false; |
| 327 |
$license_data = $this->send_mothership_request( 'activate_license' ); |
| 328 |
|
| 329 |
// $license_data->license will be either "valid" or "invalid" |
| 330 |
if ( is_array( $license_data ) ) { |
| 331 |
if ( in_array( $license_data['license'], array( 'valid', 'invalid' ), true ) ) { |
| 332 |
$response['status'] = $license_data['license']; |
| 333 |
} |
| 334 |
} else { |
| 335 |
$response['status'] = $license_data; |
| 336 |
} |
| 337 |
} catch ( Exception $e ) { |
| 338 |
$response['status'] = $e->getMessage(); |
| 339 |
} |
| 340 |
|
| 341 |
return $response; |
| 342 |
} |
| 343 |
|
| 344 |
private function get_messages() { |
| 345 |
return array( |
| 346 |
'valid' => __( 'Your license has been activated. Enjoy!', 'formidable' ), |
| 347 |
'invalid' => __( 'That license key is invalid', 'formidable' ), |
| 348 |
'expired' => __( 'That license is expired', 'formidable' ), |
| 349 |
'revoked' => __( 'That license has been refunded', 'formidable' ), |
| 350 |
'no_activations_left' => __( 'That license has been used on too many sites', 'formidable' ), |
| 351 |
'invalid_item_id' => __( 'Oops! That is the wrong license key for this plugin.', 'formidable' ), |
| 352 |
'missing' => __( 'That license key is invalid', 'formidable' ), |
| 353 |
); |
| 354 |
} |
| 355 |
|
| 356 |
public static function deactivate() { |
| 357 |
FrmAppHelper::permission_check( 'frm_change_settings' ); |
| 358 |
check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 359 |
|
| 360 |
$plugin_slug = sanitize_text_field( $_POST['plugin'] ); |
| 361 |
$this_plugin = self::get_addon( $plugin_slug ); |
| 362 |
$license = $this_plugin->get_license(); |
| 363 |
$this_plugin->license = $license; |
| 364 |
|
| 365 |
$response = array( |
| 366 |
'success' => false, |
| 367 |
'message' => '', |
| 368 |
); |
| 369 |
try { |
| 370 |
// $license_data->license will be either "deactivated" or "failed" |
| 371 |
$license_data = $this_plugin->send_mothership_request( 'deactivate_license' ); |
| 372 |
if ( is_array( $license_data ) && 'deactivated' === $license_data['license'] ) { |
| 373 |
$response['success'] = true; |
| 374 |
$response['message'] = __( 'That license was removed successfully', 'formidable' ); |
| 375 |
} else { |
| 376 |
$response['message'] = __( 'There was an error deactivating your license.', 'formidable' ); |
| 377 |
} |
| 378 |
} catch ( Exception $e ) { |
| 379 |
$response['message'] = $e->getMessage(); |
| 380 |
} |
| 381 |
|
| 382 |
$this_plugin->clear_license(); |
| 383 |
|
| 384 |
echo json_encode( $response ); |
| 385 |
wp_die(); |
| 386 |
} |
| 387 |
|
| 388 |
public function send_mothership_request( $action ) { |
| 389 |
$api_params = array( |
| 390 |
'edd_action' => $action, |
| 391 |
'license' => $this->license, |
| 392 |
'url' => home_url(), |
| 393 |
); |
| 394 |
if ( is_numeric( $this->download_id ) ) { |
| 395 |
$api_params['item_id'] = absint( $this->download_id ); |
| 396 |
} else { |
| 397 |
$api_params['item_name'] = rawurlencode( $this->plugin_name ); |
| 398 |
} |
| 399 |
|
| 400 |
$arg_array = array( |
| 401 |
'body' => $api_params, |
| 402 |
'timeout' => 25, |
| 403 |
'sslverify' => false, |
| 404 |
'user-agent' => $this->plugin_slug . '/' . $this->version . '; ' . get_bloginfo( 'url' ), |
| 405 |
); |
| 406 |
|
| 407 |
$resp = wp_remote_post( $this->store_url, $arg_array ); |
| 408 |
$body = wp_remote_retrieve_body( $resp ); |
| 409 |
|
| 410 |
$message = __( 'Your License Key was invalid', 'formidable' ); |
| 411 |
if ( is_wp_error( $resp ) ) { |
| 412 |
/* translators: %1$s: Start link HTML, %2$s: End link HTML */ |
| 413 |
$message = sprintf( __( 'You had an error communicating with the Formidable API. %1$sClick here%2$s for more information.', 'formidable' ), '<a href="https://formidableforms.com/knowledgebase/why-cant-i-activate-formidable-pro/" target="_blank">', '</a>' ); |
| 414 |
$message .= ' ' . $resp->get_error_message(); |
| 415 |
} elseif ( 'error' === $body || is_wp_error( $body ) ) { |
| 416 |
$message = __( 'You had an HTTP error connecting to the Formidable API', 'formidable' ); |
| 417 |
} else { |
| 418 |
$json_res = json_decode( $body, true ); |
| 419 |
if ( null !== $json_res ) { |
| 420 |
if ( is_array( $json_res ) && isset( $json_res['error'] ) ) { |
| 421 |
$message = $json_res['error']; |
| 422 |
} else { |
| 423 |
$message = $json_res; |
| 424 |
} |
| 425 |
} elseif ( isset( $resp['response'] ) && isset( $resp['response']['code'] ) ) { |
| 426 |
/* translators: %1$s: Error code, %2$s: Error message */ |
| 427 |
$message = sprintf( __( 'There was a %1$s error: %2$s', 'formidable' ), $resp['response']['code'], $resp['response']['message'] . ' ' . $resp['body'] ); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
return $message; |
| 432 |
} |
| 433 |
|
| 434 |
public function manually_queue_update() { |
| 435 |
set_site_transient( 'update_plugins', null ); |
| 436 |
} |
| 437 |
} |
| 438 |
|