Skip to content

Instantly share code, notes, and snippets.

@dlxsnippets
Created July 3, 2022 04:31
Show Gist options
  • Save dlxsnippets/8b63a36bdfaa20c2ea34865afe92b84c to your computer and use it in GitHub Desktop.
Save dlxsnippets/8b63a36bdfaa20c2ea34865afe92b84c to your computer and use it in GitHub Desktop.
Gravity Forms and Easy Digital Downloads Licensing: Sample licensing class that you can use to set up licenses with a Gravity Forms add-on.
<?php
// don't load directly.
if ( ! defined( 'ABSPATH' ) ) {
die();
}
/**
* Gravity Forms ClickUp Add-On.
*
* @since 1.0.0
* @package tazker-for-clickup
* @author Ronald Huereca
* @copyright Copyright (c) 2022, MediaRon
*/
/**
* Class License Check.
*/
class GF_Tazker_Clickup_License {
/**
* Holds the product name.
*
* @var string Plugin name to check.
*/
private $plugin_name = 'Tazker for ClickUp';
/**
* Holds the domain to check for the license.
*
* @var string Domain to check for the license.
*/
private $license_domain = 'https://dlxplugins.com';
/**
* Holds the license value.
*
* @var string $license The license value.
*/
private $license = '';
/**
* Class constructor.
*
* @param string $license The site license.
*/
public function __construct( string $license = '' ) {
if ( rgblank( $license ) ) {
$options = gf_tazker_clickup()::get_options( true );
$license = rgar( $options, 'license' );
}
$this->license = sanitize_text_field( $license );
}
/**
* Perform a license action and return the result.
*
* @param string $action Action to take (check_license, activate_license, deactivate_license).
* @param string $license (Optional) License key to process.
* @param bool $force Whether to skip cache or not.
*
* @return array $args {
* @bool $errors Whether there are errors or not.
* @string $license License key used.
* @bool $license_valid Whether the license is valid or not.
* @string $message Message to display to user.
* @array $data Raw response data.
* @string $action The action that was performed (check_license, activate_license, deactivate_license).
* }
*/
public function perform_action( string $action, bool $force = true ) {
$license = sanitize_text_field( $this->license );
// Perform license action.
return $this->perform_license_action( $action, $force );
}
/**
* Perform a license check based on action.
*
* @param string $action Action to take.
* @param bool $force Whether to skip any caching.
*/
private function perform_license_action( string $action, bool $force ) {
$options = gf_tazker_clickup()::get_options( true );
$maybe_check = get_site_transient( 'gform_tazker_clickup_license_check' );
$license_status = rgar( $options, 'license_status' );
$license = rgar( $options, 'license' );
$license_valid = rgar( $options, 'license_valid' );
if ( 'check_license' === $action && ! $force && $maybe_check ) {
return $maybe_check;
}
// Set transient for checking.
if ( rgblank( $this->license ) && 'check_license' === $action ) {
return array(
'errors' => true,
'license' => $this->license,
'license_valid' => false,
'message' => __( 'It appears the license key is blank.', 'toggl-plan-gravity-forms' ),
'data' => array(),
'action' => $action,
);
}
// Check for valid license.
$store_url = $this->license_domain;
$api_params = array(
'edd_action' => $action,
'license' => $this->license,
'item_name' => rawurlencode( $this->plugin_name ),
'url' => home_url(),
);
// Call the custom API.
$response = wp_remote_post(
$store_url,
array(
'timeout' => 15,
'sslverify' => false,
'body' => $api_params,
)
);
// If an error, return response.
if ( is_wp_error( $response ) ) {
return $response;
}
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return new \WP_Error( 'tazker_clickup_license_communications_error', __( 'We could not communicate with the update server. Please try again later.', 'toggl-plan-gravity-forms' ) );
}
// Succeeded, let's check the response.
$response = $this->get_response( gf_tazker_clickup()->maybe_decode_json( wp_remote_retrieve_body( $response ) ), $action );
if ( ! rgar( $response, 'errors' ) ) {
if ( 'deactivate_license' === $action ) {
// Clear license.
$options = wp_parse_args(
array(
'license' => '',
'license_valid' => false,
),
$options
);
} else {
$options = wp_parse_args(
array(
'license' => rgar( $response, 'license' ),
'license_valid' => rgar( $response, 'license_valid' ),
),
$options
);
}
delete_site_transient( 'gform_tazker_clickup_license_check' );
gf_tazker_clickup()::update_options( $options );
}
// Add query flag for checking the license so we don't ping the API every time.
if ( 'check_license' === $action ) {
set_site_transient( 'gform_tazker_clickup_license_check', $response, 12 * HOUR_IN_SECONDS );
}
// Perform an action based on response/action.
do_action( 'gform_tazker_clickup_license_' . $action, $response );
// Return any custom data.
$data = apply_filters( 'gform_tazker_clickup_license_data_' . $action, $response );
return $data;
}
/**
* Retrieve response data in array format.
*
* @param array $response Raw response data.
* @param string $action Action to take (check_license, activate_license, deactivate_license).
*
* @return array $args {
* @bool $errors Whether there are errors or not.
* @string $license License key used.
* @bool $license_valid Whether the license is valid or not.
* @string $message Message to display to user.
* @array $data Raw response data.
* @string $action The action that was performed (check_license, activate_license, deactivate_license).
* }
*/
private function get_response( array $response, string $action ) {
$errors = false;
$return_defaults = array(
'errors' => false,
'license' => $this->license,
'license_valid' => true,
'message' => '',
'data' => $response,
'action' => $action,
);
// Format the date.
if ( rgars( $return_defaults, 'data/expires' ) ) {
$return_defaults['data']['expires_human_time_diff'] = human_time_diff( time(), strtotime( rgars( $return_defaults, 'data/expires' ) ) );
$return_defaults['data']['expires'] = date_i18n( 'F jS, Y', strtotime( rgars( $return_defaults, 'data/expires' ) ) );
}
if ( true === rgar( $response, 'success' ) ) {
if ( 'deactivate_license' === $action ) {
delete_site_transient( 'gform_tazker_clickup_license_check' );
return wp_parse_args(
array(
'message' => __( 'Your license key has been deactivated.', 'toggl-plan-gravity-forms' ),
),
$return_defaults
);
}
if ( 'activate_license' === $action || 'check_license' === $action ) {
return wp_parse_args(
array(
'message' => __( 'Your license key is active and valid.', 'toggl-plan-gravity-forms' ),
),
$return_defaults
);
}
}
// There are errors.
$error_message = __( 'An error occurred, please try again.', 'toggl-plan-gravity-forms' );
if ( rgar( $response, 'error' ) ) {
switch ( rgar( $response, 'error' ) ) {
case 'expired':
$error_message = sprintf(
/* Translators: %s is a date format placeholder */
__( 'Your license key expired on %s.', 'toggl-plan-gravity-forms' ),
date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) ) // phpcs:ignore
);
break;
case 'disabled':
case 'revoked':
$error_message = __( 'Your license key has been disabled.', 'toggl-plan-gravity-forms' );
break;
case 'missing':
$error_message = __( 'Invalid license.', 'toggl-plan-gravity-forms' );
break;
case 'invalid':
case 'site_inactive':
$error_message = __( 'Your license is not active for this URL.', 'toggl-plan-gravity-forms' );
break;
case 'item_name_mismatch':
/* Translators: %s is the plugin name */
$error_message = sprintf( __( 'This appears to be an invalid license key for %s.', 'toggl-plan-gravity-forms' ), 'Toggl Plan for Gravity Forms' );
break;
case 'no_activations_left':
$error_message = __( 'Your license key has reached its activation limit.', 'toggl-plan-gravity-forms' );
break;
}
}
return wp_parse_args(
array(
'errors' => true,
'license_valid' => false,
'message' => $error_message,
),
$return_defaults
);
}
}
@dlxsnippets
Copy link
Author

Screen Shot 2022-07-02 at 23 21 31

A license helper class. Feel free to abstract it or customize it for your own use-case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment