installed plugin Easy Digital Downloads
version 3.1.0.3
This commit is contained in:
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
/**
|
||||
* License Registry
|
||||
*
|
||||
* Responsible for holding information about premium EDD extensions in use on this site.
|
||||
*
|
||||
* @package easy-digital-downloads
|
||||
* @copyright Copyright (c) 2021, Easy Digital Downloads
|
||||
* @license GPL2+
|
||||
* @since 2.11.4
|
||||
*/
|
||||
|
||||
namespace EDD\Extensions;
|
||||
|
||||
class ExtensionRegistry extends \ArrayObject {
|
||||
|
||||
/**
|
||||
* Adds an extension to the registry.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @param string $pluginFile Path to the plugin's main file.
|
||||
* @param string $pluginName Display name of the plugin.
|
||||
* @param int $pluginId EDD product ID for the plugin.
|
||||
* @param string $currentVersion Current version number.
|
||||
* @param string|null $optionName Option name where the license key is stored. If omitted, automatically generated.
|
||||
*/
|
||||
public function addExtension( $pluginFile, $pluginName, $pluginId, $currentVersion, $optionName = null ) {
|
||||
if ( $this->offsetExists( $pluginId ) ) {
|
||||
throw new \InvalidArgumentException( sprintf(
|
||||
'The extension %d is already registered.',
|
||||
$pluginId
|
||||
) );
|
||||
}
|
||||
|
||||
$this->offsetSet(
|
||||
$pluginId,
|
||||
new \EDD_License( $pluginFile, $pluginName, $currentVersion, 'Easy Digital Downloads', $optionName, null, $pluginId )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all registered extensions, regardless of whether they have licenses activated.
|
||||
*
|
||||
* At some point we could make this public, just making it private for now so that we have
|
||||
* flexibility to change exactly what it returns in the future.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @return \EDD_License[]
|
||||
*/
|
||||
private function getExtensions() {
|
||||
return $this->getArrayCopy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the number of licensed extensions active on this site.
|
||||
* Note: This only looks at extensions registered via this registry, then filters down
|
||||
* to those that have a license key entered. It does not check to verify if the license
|
||||
* key is actually valid / not expired.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countLicensedExtensions() {
|
||||
$licensedExtensions = array_filter( $this->getExtensions(), function ( \EDD_License $license ) {
|
||||
return ! empty( $license->license );
|
||||
} );
|
||||
|
||||
return count( $licensedExtensions );
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensing Functions
|
||||
*
|
||||
* Functions used for saving and/or retrieving information about EDD licensed products
|
||||
* running on the site.
|
||||
*
|
||||
* @package easy-digital-downloads
|
||||
* @copyright Copyright (c) 2021, Easy Digital Downloads
|
||||
* @license GPL2+
|
||||
* @since 2.11.4
|
||||
*/
|
||||
|
||||
namespace EDD\Extensions;
|
||||
|
||||
/**
|
||||
* Saves an option in the database with the information from the `$edd_licensed_products`
|
||||
* global. This only runs in the context of WP-Admin, because that's the only place we
|
||||
* can be certain that the information is accurate.
|
||||
*
|
||||
* @link https://github.com/awesomemotive/easy-digital-downloads/issues/8969
|
||||
*
|
||||
* @global $edd_licensed_products
|
||||
*/
|
||||
add_action( 'admin_init', function () {
|
||||
if ( ! is_admin() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$saved_products = get_option( 'edd_licensed_extensions', array() );
|
||||
|
||||
/*
|
||||
* We only want to update this option once per day. If the timeout has expired
|
||||
* then update it with product information.
|
||||
*
|
||||
* Note: We always save this option, even if `$edd_licensed_products` is empty.
|
||||
* This is to help designate that the information has been checked and saved,
|
||||
* even if there are no licensed products.
|
||||
*/
|
||||
if ( empty( $saved_products['timeout'] ) || $saved_products['timeout'] < time() ) {
|
||||
global $edd_licensed_products;
|
||||
|
||||
update_option( 'edd_licensed_extensions', json_encode( array(
|
||||
'timeout' => strtotime( '+1 day' ),
|
||||
'products' => $edd_licensed_products,
|
||||
) ) );
|
||||
}
|
||||
}, 200 );
|
||||
|
||||
/**
|
||||
* Returns licensed EDD extensions that are active on this site.
|
||||
* Array values are the `$item_shortname` from `\EDD_License`
|
||||
*
|
||||
* @see \EDD_License::$item_shortname
|
||||
*
|
||||
* @since 2.11.4
|
||||
* @return array
|
||||
*/
|
||||
function get_licensed_extension_slugs() {
|
||||
$products = get_option( 'edd_licensed_extensions' );
|
||||
|
||||
/*
|
||||
* If this isn't set for some reason, fall back to trying the global. There are
|
||||
* probably a very limited number of cases where the option would be empty when
|
||||
* the global is not, but worth a shot.
|
||||
*/
|
||||
if ( empty( $products ) ) {
|
||||
global $edd_licensed_products;
|
||||
|
||||
return ! empty( $edd_licensed_products ) && is_array( $edd_licensed_products )
|
||||
? $edd_licensed_products
|
||||
: array();
|
||||
}
|
||||
|
||||
$products = json_decode( $products, true );
|
||||
|
||||
return isset( $products['products'] ) && is_array( $products['products'] )
|
||||
? $products['products']
|
||||
: array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers our hook for registering extensions.
|
||||
* This needs to run after all plugins have definitely been loaded.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*/
|
||||
add_action( 'plugins_loaded', function() {
|
||||
/**
|
||||
* Extensions should hook in here to register themselves.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @param ExtensionRegistry
|
||||
*/
|
||||
do_action( 'edd_extension_license_init', EDD()->extensionRegistry );
|
||||
}, PHP_INT_MAX );
|
Reference in New Issue
Block a user