license_manager = new BSF_License_Manager();
$action = isset( $_GET['license_action'] ) ? esc_attr( $_GET['license_action'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( 'activate_license' === $action ) {
$this->process_envato_activation();
}
add_filter( 'update_footer', array( $this, 'alternate_method_link' ), 20 );
add_action( 'bsf_inlne_license_envato_after_form', array( $this, 'inline_alternate_method_link' ), 20, 2 );
}
/**
* Envato Register.
*
* @param array $args Arguments.
*/
public function envato_register( $args ) {
// Check if alternate method is to be used.
$method = ( isset( $_GET['activation_method'] ) && isset( $_GET['bsf_activation_nonce'] ) && wp_verify_nonce( $_GET['bsf_activation_nonce'], 'bsf_activation_nonce' ) ) ? esc_attr( $_GET['activation_method'] ) : 'oauth';
$html = '';
$product_id = isset( $args['product_id'] ) ? $args['product_id'] : '';
$is_active = $this->license_manager->bsf_is_active_license( $product_id );
$product_name = $this->license_manager->bsf_get_product_info( $product_id, 'name' );
$purchase_url = $this->license_manager->bsf_get_product_info( $product_id, 'purchase_url' );
$bundled = BSF_Update_Manager::bsf_is_product_bundled( $product_id );
if ( ! empty( $bundled ) ) {
$parent_id = $bundled[0];
$is_active = $this->license_manager->bsf_is_active_license( $parent_id );
$parent_name = brainstrom_product_name( $parent_id );
$registration_page = bsf_registration_page_url( '', $parent_id );
$html .= '
';
$html .= '
';
if ( $is_active ) {
$html .= 'License Active!
';
$html .= '' . sprintf(
'Your license is activated, you will receive updates for %s when they are available.',
$product_name
) . '
';
} else {
$html .= 'Updates Unavailable!
';
$html .= '' . sprintf(
'This plugin is came bundled with the %1$s. For receiving updates, you need to activate license of %2$s here.',
$parent_name,
$parent_name,
$registration_page
) . '
';
}
$html .= '';
$html .= '
';
return $html;
}
if ( 'license-key' === $method ) {
$html .= bsf_license_activation_form( $args );
return $html;
}
// Licence activation button.
$form_action = ( isset( $args['form_action'] ) && ! is_null( $args['form_action'] ) ) ? $args['form_action'] : '';
$form_class = ( isset( $args['form_class'] ) && ! is_null( $args['form_class'] ) ) ? $args['form_class'] : "bsf-license-form-{$product_id}";
$submit_button_class = ( isset( $args['submit_button_class'] ) && ! is_null( $args['submit_button_class'] ) ) ? $args['submit_button_class'] : '';
$license_form_heading_class = ( isset( $args['bsf_license_form_heading_class'] ) && ! is_null( $args['bsf_license_form_heading_class'] ) ) ? $args['bsf_license_form_heading_class'] : '';
$license_active_class = ( isset( $args['bsf_license_active_class'] ) && ! is_null( $args['bsf_license_active_class'] ) ) ? $args['bsf_license_active_class'] : '';
$license_not_activate_message = ( isset( $args['bsf_license_not_activate_message'] ) && ! is_null( $args['bsf_license_not_activate_message'] ) ) ? $args['bsf_license_not_activate_message'] : '';
$size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
$button_text_activate = ( isset( $args['button_text_activate'] ) && ! is_null( $args['button_text_activate'] ) ) ? __( 'Sign Up & Activate', 'bsf' ) : __( 'Sign Up & Activate', 'bsf' );
$button_text_deactivate = ( isset( $args['button_text_deactivate'] ) && ! is_null( $args['button_text_deactivate'] ) ) ? $args['button_text_deactivate'] : __( 'Deactivate License', 'bsf' );
$placeholder = ( isset( $args['placeholder'] ) && ! is_null( $args['placeholder'] ) ) ? $args['placeholder'] : 'Enter your license key..';
$popup_license_form = ( isset( $args['popup_license_form'] ) ) ? $args['popup_license_form'] : false;
$bsf_license_allow_email = ( isset( $args['bsf_license_allow_email'] ) && ! is_null( $args['bsf_license_allow_email'] ) ) ? $args['bsf_license_allow_email'] : true;
if ( true === $bsf_license_allow_email ) {
$form_class .= ' license-form-allow-email ';
if ( ! $is_active ) {
$submit_button_class .= ' button-primary button-hero bsf-envato-form-activation ';
}
}
if ( true !== $is_active ) {
$form_action = bsf_get_api_site() . 'envato-validation-callback/?wp-envato-validate';
} else {
$form_action = bsf_registration_page_url( '', $product_id );
}
$html .= '';
$html .= '
';
$html = apply_filters( 'bsf_inlne_license_envato_after_form', $html, $product_id );
$html .= '
';
if ( isset( $_GET['debug'] ) ) {
$html .= get_bsf_systeminfo();
}
return $html;
}
/**
* Envato activation URL.
*
* @param array $form_data Form data.
* @return $envato_activation_url.
*/
public function envato_activation_url( $form_data ) {
$product_id = isset( $form_data['product_id'] ) ? esc_attr( $form_data['product_id'] ) : '';
// deepcode ignore InsecureHash: Will be updated in the BSF Core Library.
$form_data['token'] = sha1( $this->create_token( $product_id ) );
$url = bsf_get_api_site() . 'envato-validation-callback/?wp-envato-validate';
$envato_activation_url = add_query_arg(
$form_data,
$url
);
return $envato_activation_url;
}
/**
* Get redirect URL.
*
* @param int $product_id Product ID.
* @return $current_url.
*/
protected function get_redirect_url( $product_id = '' ) {
if ( is_ssl() ) {
$current_url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
} else {
$current_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
$current_url = esc_url( remove_query_arg( array( 'license_action', 'token', 'product_id', 'purchase_key', 'success', 'status', 'message' ), $current_url ) );
if ( '' !== $product_id ) {
$current_url = add_query_arg(
array(
'bsf-inline-license-form' => $product_id,
),
$current_url
);
}
return $current_url;
}
/**
* Create Token.
*
* @param int $product_id Product ID.
* @return $token.
*/
protected function create_token( $product_id ) {
$token = $product_id . '|' . current_time( 'timestamp' ) . '|' . bsf_generate_rand_token();
update_site_option( "bsf_envato_token_$product_id", $token );
return $token;
}
/**
* Validate Token.
*
* @param string $token Token.
* @param int $product_id Product ID.
* @return bool.
*/
protected function validate_token( $token, $product_id ) {
$stored_token = get_site_option( "bsf_envato_token_$product_id", '' );
// deepcode ignore InsecureHash: Will be updated in the BSF Core Library.
if ( sha1( $stored_token ) === $token ) {
$token_atts = explode( '|', $stored_token );
$stored_id = $token_atts[0];
if ( $stored_id !== $product_id ) {
// Token is invalid.
return false;
}
$timestamp = (int) $token_atts[1];
$valid_ultil = $timestamp + 900;
if ( current_time( 'timestamp' ) > $valid_ultil ) {
// Timestamp has expired.
return false;
}
// If above conditions did not meet, the token is valid.
return true;
}
return false;
}
/**
* Process envato activation.
*/
protected function process_envato_activation() {
$token = isset( $_GET['token'] ) ? esc_attr( $_GET['token'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$product_id = isset( $_GET['product_id'] ) ? esc_attr( $_GET['product_id'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( $this->validate_token( $token, $product_id ) ) {
$args = array();
$args['purchase_key'] = isset( $_GET['purchase_key'] ) ? esc_attr( $_GET['purchase_key'] ) : '';// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$args['status'] = isset( $_GET['status'] ) ? esc_attr( $_GET['status'] ) : '';// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$this->license_manager->bsf_update_product_info( $product_id, $args );
$this->set_message(
array(
'status' => 'success',
'message' => 'License successfully activated!',
)
);
} else {
$this->set_message(
array(
'status' => 'error',
'message' => 'The token is invalid or is expired, please try again.',
)
);
}
}
/**
* Set message.
*
* @param array $message Message.
*/
protected function set_message( $message = array() ) {
$this->message_box = $message;
}
/**
* Get message.
*
* @param string $key key.
* @return $mesage
*/
protected function get_message( $key ) {
$message = $this->message_box;
return isset( $message[ $key ] ) ? $message[ $key ] : '';
}
/**
* Inline alternate method link.
*
* @param string $html HTML.
* @param int $bsf_product_id Product ID.
* @return $html.
*/
public function inline_alternate_method_link( $html, $bsf_product_id ) {
$privacy_policy_link = $this->license_manager->bsf_get_product_info( $bsf_product_id, 'privacy_policy' );
$terms_conditions_link = $this->license_manager->bsf_get_product_info( $bsf_product_id, 'terms_conditions' );
if ( isset( $privacy_policy_link ) ) {
$html .= sprintf(
'Privacy Policy | ',
$privacy_policy_link
);
}
if ( isset( $terms_conditions_link ) ) {
$html .= sprintf(
'Terms & Conditions',
$terms_conditions_link
);
}
return $html;
}
/**
* ALternate method link.
*
* @param string $content Content.
* @return $content.
*/
public function alternate_method_link( $content ) {
$bsf_activation_nonce = wp_create_nonce( 'bsf_activation_nonce' );
$content = sprintf(
'Activate license using purchase key',
add_query_arg(
array(
'activation_method' => 'license-key',
'bsf_activation_nonce' => $bsf_activation_nonce,
)
)
);
return $content;
}
}
/**
* BSF envato register.
*
* @param string $args Arguments..
* @return envato_register().
*/
function bsf_envato_register( $args ) {
$bsf_envato_activate = BSF_Envato_Activate::instance();
return $bsf_envato_activate->envato_register( $args );
}