item_id = absint( $_item_id );
}
$this->file = $_file;
$this->item_name = $_item_name;
$this->plugin_basename = plugin_basename($this->file);
$this->plugin_dirname = dirname( plugin_basename( $this->file ) );
$this->item_shortname = self::get_short_name( $this->item_name );
$this->license_data = self::get_license_by_plugin_dirname( $this->plugin_dirname );
$this->version = $_version;
$this->license = ! empty( $this->license_data['license_key'] ) ? $this->license_data['license_key'] : '';
$this->bundled_license = self::get_bundled_license($this->file);
$this->author = $_author;
self::$api_url = is_null( $_api_url ) ? self::$api_url : $_api_url;
self::$checkout_url = is_null( $_checkout_url ) ? self::$checkout_url : $_checkout_url;
self::$account_url = is_null( $_account_url ) ? self::$account_url : $_account_url;
// Add plugin to registered licenses list.
array_push(self::$licensed_addons, $this);
}
/**
* Get plugin shortname
*
* @param $plugin_name
*
* @return string
* @since 2.1.0
* @access public
*/
public static function get_short_name( $plugin_name ) {
$plugin_name = trim( str_replace( 'Give - ', '', $plugin_name ) );
$plugin_name = 'give_' . preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $plugin_name ) ) );
return $plugin_name;
}
/**
* Checks to see if the license was bundled with the add-on and if so, returns it. This is meant to be a
* convenience, so any errors are ignored and the user can still manually enter their license key.
*
* @since 3.21.0
*/
public static function get_bundled_license(string $plugin_file): ?string
{
$license = null;
$file_path = plugin_dir_path($plugin_file) . 'PLUGIN_LICENSE.php';
if (is_readable($file_path)) {
$license = include $file_path;
}
return $license ?: null;
}
/**
* This sorts through all the registered add-ons and checks to see if any of them have bundled licenses, but
* aren't set up with a license key. If so, it will attempt to activate the license key for them.
*
* It's likely that many add-ons will have the same license key which is a plan, so the license key will only be
* activated once for all add-ons using it.
*
* If any sort of error occurs, an admin notice will be shown to the user providing the error and instructions
* to manually activate the license key.
*
* Note: much of this code is taken directly from give_get_license_info_handler()
*
* @since 3.21.0
*/
public static function activate_bundled_licenses(): void
{
$unactivated_bundled_licenses = [];
foreach (self::$licensed_addons as $addon) {
if ($addon->bundled_license && empty($addon->license)) {
// Store a unique associative array where the key is the license key and the value is an array of
// addons using that license
if ( ! isset($unactivated_bundled_licenses[$addon->bundled_license])) {
$unactivated_bundled_licenses[$addon->bundled_license] = [];
}
$unactivated_bundled_licenses[$addon->bundled_license][] = $addon;
}
}
if (empty($unactivated_bundled_licenses)) {
return;
}
$show_failed_activation_notice = static function ($license_key, $reason) {
AdminNotices::show(
"license-bundle-activation-error-$license_key",
"An error occurred while attempting to activate license $license_key. $reason. Please activate the license manually."
)->autoParagraph()->asError()->dismissible();
};
foreach ($unactivated_bundled_licenses as $license => $addons) {
$check_license_res = self::request_license_api(
[
'edd_action' => 'check_license',
'license' => $license,
],
true
);
$is_plan_license = ! empty($check_license_res['is_all_access_pass']);
if (is_wp_error($check_license_res)) {
$show_failed_activation_notice($license, $check_license_res->get_error_message());
continue;
}
if ( ! $check_license_res['success']) {
$reason = $check_license_res['error'] ?? 'The licensing server was unreachable';
$show_failed_activation_notice( $license, $reason );
continue;
}
if ( ! $is_plan_license) {
if (count($addons) > 1) {
$show_failed_activation_notice(
$license,
'A single add-on license cannot be used for multiple add-ons'
);
continue;
}
if ($check_license_res['plugin_slug'] !== $addons[0]->plugin_dirname) {
$show_failed_activation_notice(
$license,
'The license is not valid for this add-on'
);
continue;
}
}
$activate_license_res = self::request_license_api(
[
'edd_action' => 'activate_license',
'item_name' => $check_license_res['item_name'],
'license' => $license,
],
true
);
if (is_wp_error($activate_license_res)) {
$show_failed_activation_notice($license, $activate_license_res->get_error_message());
continue;
}
$check_license_res['license'] = $activate_license_res['license'];
$check_license_res['site_count'] = $activate_license_res['site_count'];
$check_license_res['activations_left'] = $activate_license_res['activations_left'];
$licenses = get_option('give_licenses', []);
if ($is_plan_license) {
$addonSlugs = self::getAddonSlugsFromAllAccessPassLicense($check_license_res);
foreach ($licenses as $license_key => $data) {
if (in_array($data['plugin_slug'], $addonSlugs, true) || ! empty($data['is_all_access_pass'])) {
unset($licenses[$license_key]);
}
}
}
$licenses[$check_license_res['license_key']] = $check_license_res;
update_option('give_licenses', $licenses);
}
// Have WordPress check for updates
give_refresh_licenses();
}
/**
* Activate License
*
* Activate the license key.
*
* @access public
* @return void
* @since 1.0
*/
public function activate_license() {
}
/**
* Deactivate License
*
* Deactivate the license key.
*
* @access public
* @return void
* @since 1.0
*/
public function deactivate_license() {
}
/**
* Get license information.
*
* @param string $edd_action
* @param bool $response_in_array
*
* @return mixed
* @deprecated 2.5.0 Use self::request_license_api instead.
*
* @since 1.8.9
* @access public
*/
public function get_license_info($edd_action = '', $response_in_array = false)
{
if ( empty($edd_action) ) {
return false;
}
give_doing_it_wrong(__FUNCTION__, 'Use self::request_license_api instead from GiveWP 2.5.0');
// Data to send to the API.
$api_params = [
'edd_action' => $edd_action, // never change from "edd_" to "give_"!
'license' => $this->license,
'item_name' => urlencode($this->item_name),
];
return self::request_license_api($api_params, $response_in_array);
}
/**
* Return licensed addons info
*
* Note: note only for internal logic
*
* @since 3.21.1 use array_map to safely access the private property
* @since 3.21.0 plucks the basename for backwards compatibility
* @since 2.1.4
* @return string[]
*/
static function get_licensed_addons(): array
{
return array_map( static function ( Give_License $license ) {
return $license->plugin_basename;
}, self::$licensed_addons );
}
/**
* Check if license key attached to subscription
*
* @since 2.5.0
*
* @param string $license_key
*
* @return array
*/
static function is_subscription($license_key = '')
{
// Check if current license is part of subscription or not.
$subscriptions = get_option('give_subscriptions');
$subscription = [];
if ( $subscriptions ) {
foreach ($subscriptions as $subs) {
if ( in_array($license_key, $subs['licenses']) ) {
$subscription = $subs;
break;
}
}
}
return $subscription;
}
/**
* Get license information.
*
* @since 1.8.9
* @since 2.18.0 log failed license api request information
*
* @param bool $response_in_array
*
* @param array $api_params
*
* @return array|WP_Error
*/
public static function request_license_api($api_params = [], $response_in_array = false)
{
// Bailout.
if ( empty($api_params['edd_action']) ) {
return new WP_Error('give-invalid-edd-action', __('Valid edd_action not defined', 'give'));
}
// Data to send to the API.
$default_api_params = [
// 'edd_action' => $edd_action, never change from "edd_" to "give_"!
// 'license' => $this->license,
// 'item_name' => urlencode( $this->item_name ),
'url' => home_url(),
];
$api_params = wp_parse_args($api_params, $default_api_params);
// Call the API.
$response = wp_remote_post(
self::get_api_url(),
apply_filters(
'give_request_license_api_args',
[
'timeout' => 15,
'sslverify' => false,
'body' => $api_params,
]
)
);
$statusCode = wp_remote_retrieve_response_code($response);
$body = json_decode(wp_remote_retrieve_body($response), $response_in_array);
if ( 200 !== $statusCode ) {
Log::http(
'License Api request failed',
[
'category' => 'License',
'api url' => self::get_api_url(),
'request' => $api_params,
'status code' => $statusCode,
'response' => $response
]
);
}
// Make sure there are no errors.
if ( is_wp_error($response) ) {
return $response;
}
return $body;
}
/**
* Get license by plugin dirname
*
* @param string $plugin_dirname
*
* @return array
*
* @since 4.15.0 add support for Harbor unified licenses
* @since 2.5.0
* @access public
*/
public static function get_license_by_plugin_dirname( $plugin_dirname ) {
$license = [];
$give_licenses = get_option( 'give_licenses', [] );
if ( ! empty( $give_licenses ) ) {
foreach ( $give_licenses as $give_license ) {
// Logic to match all access pass license to add-on.
$compares = $give_license['is_all_access_pass']
? $give_license['download']
// Prevent PHP notice if somehow automatic update does not run properly.
// Because plugin_slug will only define in updated license rest api response.
: [ [ 'plugin_slug' => ! empty( $give_license['plugin_slug'] ) ? $give_license['plugin_slug'] : '' ] ];
foreach ( $compares as $compare ) {
if ( ! empty( $compare['plugin_slug'] ) && $plugin_dirname === $compare['plugin_slug'] ) {
$license = $give_license;
break;
}
}
}
}
// Fall back to Harbor feature availability when no legacy license is found.
if ( empty( $license ) && lw_harbor_is_feature_available( $plugin_dirname ) ) {
$license = [ 'license' => 'valid' ];
}
return $license;
}
/**
* Get checkout url
*
* @return string|null
* @since 2.5.0
*/
public static function get_checkout_url() {
return self::$checkout_url;
}
/**
* Get account url
*
* @return string|null
* @since 2.5.0
*/
public static function get_account_url() {
return self::$account_url;
}
/**
* Get downloads url
*
* @return string|null
* @since 2.5.0
*/
public static function get_downloads_url() {
return self::$downloads_url;
}
/**
* Get account url
*
* @since 3.20.0 allows to override the API URL via constant
* @since 2.5.0
*/
public static function get_website_url(): ?string
{
if (defined('GIVE_SITE_URL')) {
return GIVE_SITE_URL;
}
return self::$site_url;
}
/**
* Get plugin information by id.
* Note: only for internal use
*
* @param string $plugin_slug
*
* @return array
* @since 2.5.0
*/
public static function get_plugin_by_slug( $plugin_slug ) {
$give_plugins = give_get_plugins();
$matching_list = wp_list_pluck( $give_plugins, 'Dir', 'Path' );
$is_match_found = array_search( $plugin_slug, $matching_list, true );
return $is_match_found ? $give_plugins[ $is_match_found ] : [];
}
/**
* Get plugin information by id.
* Note: only for internal use
*
* @param string $plugin_slug
*
* @return string
* @since 2.5.0
*/
public static function build_plugin_name_from_slug( $plugin_slug ) {
$plugin_name = str_replace( [ '-', 'give ' ], [ ' ', 'Give - ' ], $plugin_slug );
return ucwords( $plugin_name );
}
/**
* Render license section
*
* @return string
* @since 2.5.0
*/
public static function render_licenses_list() {
$give_plugins = give_get_plugins( [ 'only_premium_add_ons' => true ] );
$give_licenses = get_option( 'give_licenses', [] );
$licenses_without_addon = $give_licenses;
// Get all access pass licenses
$all_access_pass_licenses = [];
$all_access_pass_addon_list = [];
foreach ( $give_licenses as $key => $give_license ) {
if ( $give_license['is_all_access_pass'] ) {
$all_access_pass_licenses[ $key ] = $give_license;
unset( $licenses_without_addon[ $key ] );
foreach ( $give_license['download'] as $download ) {
$all_access_pass_addon_list[] = $download['plugin_slug'];
}
}
}
$html = [
'unlicensed' => '',
'licensed' => '',
'licenses_without_addon' => '',
'all_access_licensed' => '',
];
if ( ! empty( $give_plugins ) ) {
foreach ( $give_plugins as $give_plugin ) {
if ( in_array( $give_plugin['Dir'], $all_access_pass_addon_list ) ) {
continue;
}
$addon_license = self::get_license_by_plugin_dirname( $give_plugin['Dir'] );
$html_arr_key = 'unlicensed';
if ( $addon_license ) {
$html_arr_key = 'licensed';
unset( $licenses_without_addon[ $addon_license['license_key'] ] );
}
$html[ "{$html_arr_key}" ] .= self::html_by_plugin( $give_plugin );
}
}
if ( ! empty( $all_access_pass_licenses ) ) {
foreach ( $all_access_pass_licenses as $key => $all_access_pass_license ) {
$html['all_access_licensed'] .= self::html_by_license( $all_access_pass_license );
}
}
if ( ! empty( $licenses_without_addon ) ) {
foreach ( $licenses_without_addon as $key => $license ) {
if ( in_array( $license['plugin_slug'], $all_access_pass_addon_list ) ) {
continue;
}
$html['licenses_without_addon'] .= self::html_by_license( $license );
}
}
ksort( $html );
// After sorting order will be all_access_licensed -> licensed -> unlicensed
return implode( '', $html );
}
/**
* Get add-on item html
* Note: only for internal use
*
* @param $plugin
*
* @return string
* @since 2.5.0
*/
public static function html_by_plugin( $plugin ) {
// Bailout.
if ( empty( $plugin ) ) {
return '';
}
ob_start();
$license = self::get_license_by_plugin_dirname( $plugin['Dir'] );
$default_plugin = [
'ChangeLogSlug' => $plugin['Dir'],
'DownloadURL' => '',
];
if ( false !== strpos( $default_plugin['ChangeLogSlug'], '-gateway' ) ) {
// We found that each gateway addon does not have `-gateway` in changelog file slug
$default_plugin['ChangeLogSlug'] = str_replace( '-gateway', '', $default_plugin['ChangeLogSlug'] );
}
if ( $license ) {
$license['renew_url'] = self::$checkout_url . "?edd_license_key={$license['license_key']}";
$default_plugin['ChangeLogSlug'] = $license['readme'];
$default_plugin['DownloadURL'] = $license['download'];
}
$plugin['License'] = $license = wp_parse_args(
$license,
[
'item_name' => str_replace( 'give-', '', $plugin['Dir'] ),
'purchase_link' => $plugin['PluginURI'],
]
);
$plugin = wp_parse_args( $plugin, $default_plugin );
?>