License. Every feature bundled in the WordPress.org plugin is
* available without a license. This screen manages optional account services,
* support entitlements, and activation allowances for separately distributed
* products. Rendered as a Settings_Page section (see
* TOPLEVEL_SECTIONS there) rather than its own top-level menu item, matching
* where a license screen naturally sits relative to the rest of Global
* Settings.
*/
class License_Page {
public function __construct() {
add_action( 'admin_post_chargeforms_activate_license', [ $this, 'handle_activate' ] );
add_action( 'admin_post_chargeforms_deactivate_license', [ $this, 'handle_deactivate' ] );
}
public function render( array $s ) {
$key = $s['license_key'];
$status = $s['license_status']; // '' | 'active' | 'error'
if ( 'active' === $status && ! Settings::has_pro() ) { $status = 'error'; }
?>
plans.', 'chargeforms' )
),
'https://chargeforms.com/pricing'
);
?>
render_what_pro_adds(); ?>
CHRG-SNGL-••••-••••-XXXX — enough to confirm which key without displaying the whole thing on screen. */
private static function mask_key( $key ) {
$parts = explode( '-', $key );
$last = count( $parts ) - 1;
foreach ( $parts as $i => &$part ) {
if ( $i > 1 && $i !== $last ) {
$part = str_repeat( '•', strlen( $part ) );
}
}
return implode( '-', $parts );
}
public function handle_activate() {
if ( ! current_user_can( 'chargeforms_manage_forms' ) || ! check_admin_referer( 'chargeforms_license_action' ) ) {
wp_die( esc_html__( 'Not allowed.', 'chargeforms' ), 403 );
}
$key = isset( $_POST['license_key'] ) ? strtoupper( trim( sanitize_text_field( wp_unslash( $_POST['license_key'] ) ) ) ) : '';
if ( '' === $key ) {
Settings::set_license( [
'license_key' => '',
'license_status' => '',
'license_last_error' => '',
] );
$this->redirect();
}
// Only ever called on an explicit submit of this form — never
// automatically (see License_Client's doc comment / the task spec
// this screen implements).
$result = License_Client::activate( $key, home_url() );
if ( $result['success'] ) {
$pending = array_values( array_filter( License_Cron::pending_deactivations(), static function( $record ) use ( $key ) {
return $record['key'] !== $key || License_Client::site_hash( $record['site'] ) !== License_Client::site_hash( home_url() );
} ) );
Settings::set_license( [
'license_key' => $key,
'license_status' => 'active',
'license_plan' => $result['plan'],
'license_activation_limit' => $result['activation_limit'],
'license_last_error' => '',
'license_last_verified' => time(),
'license_last_validated' => time(),
'license_site_hash' => $result['site_hash'],
'license_expires_at' => $result['expires_at'],
'license_pending_deactivation' => $pending,
] );
} else {
Settings::set_license( [
'license_key' => $key, // Keep what they typed so they can see/correct it.
'license_status' => 'error',
'license_last_error' => $result['error'],
'license_last_validated' => 0,
'license_site_hash' => '',
] );
}
$this->redirect();
}
public function handle_deactivate() {
if ( ! current_user_can( 'chargeforms_manage_forms' ) || ! check_admin_referer( 'chargeforms_license_action' ) ) {
wp_die( esc_html__( 'Not allowed.', 'chargeforms' ), 403 );
}
$key = Settings::get( 'license_key' );
$pending = License_Cron::pending_deactivations();
if ( $key ) {
// Best-effort — the site is freed locally either way, so a
// site's own license screen never gets stuck because the
// license server happened to be unreachable at this moment.
$result = License_Client::deactivate( $key, home_url() );
if ( ! $result['success'] ) {
$pending[] = [ 'key' => $key, 'site' => home_url() ];
$pending = array_values( array_unique( $pending, SORT_REGULAR ) );
}
}
Settings::set_license( [
'license_key' => '',
'license_status' => '',
'license_plan' => '',
'license_activation_limit' => null,
'license_last_error' => '',
'license_last_verified' => 0,
'license_last_validated' => 0,
'license_site_hash' => '',
'license_expires_at' => null,
'license_pending_deactivation' => $pending,
] );
$this->redirect();
}
private function redirect() {
wp_safe_redirect( admin_url( 'admin.php?page=chargeforms-settings§ion=license&chargeforms_notice=1' ) );
exit;
}
}