updated plugin Easy Digital Downloads
version 3.1.1.2
This commit is contained in:
@ -1,227 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* EnvironmentChecker.php
|
||||
*
|
||||
* Checks to see if the environment matches the passed conditions.
|
||||
* Supported conditions include:
|
||||
*
|
||||
* - EDD version number -- either specific versions or wildcards (e.g. "2.x").
|
||||
* - Type of license (pass level, à la carte, free).
|
||||
*
|
||||
* @package easy-digital-downloads
|
||||
* @copyright Copyright (c) 2021, Easy Digital Downloads
|
||||
* @license GPL2+
|
||||
* @since 2.11.4
|
||||
*/
|
||||
|
||||
namespace EDD\Utils;
|
||||
|
||||
use EDD\Admin\Pass_Manager;
|
||||
|
||||
class EnvironmentChecker {
|
||||
|
||||
/**
|
||||
* @var Pass_Manager
|
||||
*/
|
||||
protected $passManager;
|
||||
|
||||
/**
|
||||
* Types of license/pass conditions that we support.
|
||||
* The key is the condition slug and the value is the corresponding
|
||||
* method to call in the `Pass_Manager` class to check the condition.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $validLicenseConditions = array(
|
||||
'free' => 'isFree',
|
||||
'ala-carte' => 'hasIndividualLicense',
|
||||
'pass-personal' => 'hasPersonalPass',
|
||||
'pass-extended' => 'hasExtendedPass',
|
||||
'pass-professional' => 'hasProfessionalPass',
|
||||
'pass-all-access' => 'hasAllAccessPass',
|
||||
'pass-any' => 'has_pass',
|
||||
);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->passManager = new Pass_Manager();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if this environment meets the specified condition.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @param string $condition Condition to check. Can either be a type of license/pass or a version number.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function meetsCondition( $condition ) {
|
||||
if ( array_key_exists( $condition, $this->validLicenseConditions ) ) {
|
||||
return $this->hasLicenseType( $condition );
|
||||
} elseif ( $this->isPaymentGateway( $condition ) ) {
|
||||
return $this->paymentGatewayMatch( array_keys( edd_get_enabled_payment_gateways() ), $condition );
|
||||
} elseif ( $this->isVersionNumber( $condition ) ) {
|
||||
return $this->versionNumbersMatch( EDD_VERSION, $condition );
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException( 'Invalid condition. Must either be a type of license or a version number.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if this environment meets all the specified conditions. If any one condition
|
||||
* is not met then this returns false.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @param array $conditions
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function meetsConditions( $conditions ) {
|
||||
foreach ( $conditions as $condition ) {
|
||||
if ( ! $this->meetsCondition( $condition ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the site has the specified pass condition.
|
||||
*
|
||||
* @see EnvironmentChecker::$validLicenseConditions
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @param string $passLevel License type that we're checking to see if the system has.
|
||||
*
|
||||
* @return bool
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function hasLicenseType( $passLevel ) {
|
||||
$method = isset( $this->validLicenseConditions[ $passLevel ] )
|
||||
? $this->validLicenseConditions[ $passLevel ]
|
||||
: false;
|
||||
|
||||
if ( ! $method || ! method_exists( $this->passManager, $method ) ) {
|
||||
throw new \InvalidArgumentException( sprintf( 'Method %s not found in Pass_Manager.', $method ) );
|
||||
}
|
||||
|
||||
return call_user_func( array( $this->passManager, $method ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the provided condition is a payment gateway.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @param string $condition
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function isPaymentGateway( $condition ) {
|
||||
return 'gateway-' === substr( $condition, 0, 8 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the supplied gateway condition is applicable to this site.
|
||||
* Will return `true` if the condition is the slug of a payment gateway (potentially with a `gateway-` prefix)
|
||||
* that's enabled on this site.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @param array $enabledGateways Gateways that are enabled on this site.
|
||||
* @param string $condition Gateway we're checking to see if it's enabled.
|
||||
*
|
||||
* @return bool True if the gateway is enabled, false if not.
|
||||
*/
|
||||
public function paymentGatewayMatch( $enabledGateways, $condition ) {
|
||||
$gatewayToCheck = str_replace( 'gateway-', '', $condition );
|
||||
|
||||
return in_array( $gatewayToCheck, $enabledGateways, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the provided condition is a version number.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @param string $condition
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function isVersionNumber( $condition ) {
|
||||
// First character should always be numeric.
|
||||
if ( ! is_numeric( substr( $condition, 0, 1 ) ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Must contain at least one `.` or `-`.
|
||||
return false !== strpos( $condition, '.' ) || false !== strpos( $condition, '-' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if two version numbers match, or if the `$currentVersion` falls within the wildcard
|
||||
* range specified by `$compareVersion`.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @param string $currentVersion Version number currently in use. This must be a full, exact version number.
|
||||
* @param string $compareVersion Version to compare with. This can either be an exact version number or a
|
||||
* wildcard (e.g. `2.11.3` or `2.x`). Hyphens are also accepted in lieu of
|
||||
* full stops (e.g. `2-11-3` or `2-x`).
|
||||
*
|
||||
* @return bool
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function versionNumbersMatch( $currentVersion, $compareVersion ) {
|
||||
$currentVersionPieces = explode( '.', $currentVersion );
|
||||
|
||||
if ( false !== strpos( $compareVersion, '.' ) ) {
|
||||
$compareVersionPieces = explode( '.', $compareVersion );
|
||||
} else if ( false !== strpos( $compareVersion, '-' ) ) {
|
||||
$compareVersionPieces = explode( '-', $compareVersion );
|
||||
} else {
|
||||
throw new \InvalidArgumentException( sprintf(
|
||||
'Invalid version number: %s',
|
||||
$compareVersion
|
||||
) );
|
||||
}
|
||||
|
||||
$numberCurrentVersionParts = count( $currentVersionPieces );
|
||||
$numberCompareVersionParts = count( $compareVersionPieces );
|
||||
|
||||
/*
|
||||
* Normalize the two parts so that they have the same lengths and
|
||||
* wildcards (`x`) are removed.
|
||||
*/
|
||||
for ( $i = 0; $i < $numberCurrentVersionParts || $i < $numberCompareVersionParts; $i ++ ) {
|
||||
if ( isset( $compareVersionPieces[ $i ] ) && 'x' === strtolower( $compareVersionPieces[ $i ] ) ) {
|
||||
unset( $compareVersionPieces[ $i ] );
|
||||
}
|
||||
|
||||
if ( ! isset( $currentVersionPieces[ $i ] ) ) {
|
||||
unset( $compareVersionPieces[ $i ] );
|
||||
} elseif ( ! isset( $compareVersionPieces[ $i ] ) ) {
|
||||
unset( $currentVersionPieces[ $i ] );
|
||||
}
|
||||
}
|
||||
|
||||
// Now make sure all the numbers match.
|
||||
foreach ( $compareVersionPieces as $index => $versionPiece ) {
|
||||
if ( ! isset( $currentVersionPieces[ $index ] ) || $currentVersionPieces[ $index ] !== $versionPiece ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -1,239 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* NotificationImporter.php
|
||||
*
|
||||
* @package easy-digital-downloads
|
||||
* @copyright Copyright (c) 2021, Easy Digital Downloads
|
||||
* @license GPL2+
|
||||
* @since 2.11.4
|
||||
*/
|
||||
|
||||
namespace EDD\Utils;
|
||||
|
||||
class NotificationImporter {
|
||||
|
||||
/**
|
||||
* @var EnvironmentChecker
|
||||
*/
|
||||
protected $environmentChecker;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->environmentChecker = new EnvironmentChecker();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches notifications from the API and imports them locally.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*/
|
||||
public function run() {
|
||||
$request_timeout = get_option( 'edd_notification_req_timeout', false );
|
||||
if ( false !== $request_timeout && current_time( 'timestamp' ) < $request_timeout ) {
|
||||
edd_debug_log( 'Skipping notifications API request, timeout not reached' );
|
||||
return;
|
||||
}
|
||||
|
||||
edd_debug_log( 'Fetching notifications via ' . $this->getApiEndpoint() );
|
||||
|
||||
try {
|
||||
$notifications = $this->fetchNotifications();
|
||||
|
||||
// If successful, make it so we don't request for another 23 hours.
|
||||
update_option( 'edd_notification_req_timeout', current_time( 'timestamp' ) + ( HOUR_IN_SECONDS * 23 ), false );
|
||||
} catch ( \Exception $e ) {
|
||||
edd_debug_log( sprintf( 'Notification fetch exception: %s', $e->getMessage() ) );
|
||||
|
||||
// If for some reason our request failed, delay for 4 hours.
|
||||
update_option( 'edd_notification_req_timeout', current_time( 'timestamp' ) + ( HOUR_IN_SECONDS * 4 ), false );
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $notifications as $notification ) {
|
||||
$notificationId = isset( $notification->id ) ? $notification->id : 'unknown';
|
||||
|
||||
edd_debug_log( sprintf( 'Processing notification ID %s', $notificationId ) );
|
||||
|
||||
try {
|
||||
$this->validateNotification( $notification );
|
||||
|
||||
$existingId = EDD()->notifications->get_column_by( 'id', 'remote_id', $notification->id );
|
||||
if ( $existingId ) {
|
||||
edd_debug_log( '-- Updating existing notification.' );
|
||||
|
||||
$this->updateExistingNotification( $existingId, $notification );
|
||||
} else {
|
||||
edd_debug_log( '-- Inserting new notification.' );
|
||||
|
||||
$this->insertNewNotification( $notification );
|
||||
}
|
||||
} catch ( \Exception $e ) {
|
||||
edd_debug_log( sprintf( '-- Notification processing failure for ID %s: %s', $notificationId, $e->getMessage() ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the API endpoint to query.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getApiEndpoint() {
|
||||
if ( defined( 'EDD_NOTIFICATIONS_API_URL' ) ) {
|
||||
return EDD_NOTIFICATIONS_API_URL;
|
||||
}
|
||||
|
||||
return 'https://plugin.easydigitaldownloads.com/wp-content/notifications.json';
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves notifications from the remote API endpoint.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function fetchNotifications() {
|
||||
$response = wp_remote_get( $this->getApiEndpoint() );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
throw new \Exception( $response->get_error_message() );
|
||||
}
|
||||
|
||||
$notifications = wp_remote_retrieve_body( $response );
|
||||
|
||||
return ! empty( $notifications ) ? json_decode( $notifications ) : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the notification from the remote API to make sure we actually
|
||||
* want to save it.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @param object $notification
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function validateNotification( $notification ) {
|
||||
// Make sure we have all the required data.
|
||||
$requiredProperties = array(
|
||||
'id',
|
||||
'title',
|
||||
'content',
|
||||
);
|
||||
|
||||
$missing = array_diff( $requiredProperties, array_keys( get_object_vars( $notification ) ) );
|
||||
if ( $missing ) {
|
||||
throw new \Exception( sprintf( 'Missing required properties: %s', json_encode( array_values( $missing ) ) ) );
|
||||
}
|
||||
|
||||
// Don't save the notification if it has expired.
|
||||
if ( ! empty( $notification->end ) && time() > strtotime( $notification->end ) ) {
|
||||
throw new \Exception( 'Notification has expired.' );
|
||||
}
|
||||
|
||||
// Ignore if notification was created before EDD was installed.
|
||||
if ( ! empty( $notification->start ) && edd_get_activation_date() > strtotime( $notification->start ) ) {
|
||||
throw new \Exception( 'Notification created prior to EDD activation.' );
|
||||
}
|
||||
|
||||
if (
|
||||
! empty( $notification->type ) &&
|
||||
is_array( $notification->type ) &&
|
||||
! $this->environmentChecker->meetsConditions( $notification->type )
|
||||
) {
|
||||
throw new \Exception( 'Condition(s) not met.' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the array of notification data to insert into the database.
|
||||
* Use in both inserts and updates.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @param object $notification
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getNotificationData( $notification ) {
|
||||
return array(
|
||||
'remote_id' => $notification->id,
|
||||
'title' => $notification->title,
|
||||
'content' => $notification->content,
|
||||
'buttons' => $this->parseButtons( $notification ),
|
||||
'type' => ! empty( $notification->notification_type ) ? $notification->notification_type : 'success',
|
||||
'conditions' => ! empty( $notification->type ) ? $notification->type : null,
|
||||
'start' => ! empty( $notification->start ) ? $notification->start : null,
|
||||
'end' => ! empty( $notification->end ) ? $notification->end : null,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses and formats buttons from the remote notification object.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @param object $notification
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
protected function parseButtons( $notification ) {
|
||||
if ( empty( $notification->btns ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$buttons = array();
|
||||
|
||||
foreach ( (array) $notification->btns as $buttonType => $buttonInfo ) {
|
||||
if ( empty( $buttonInfo->url ) || empty( $buttonInfo->text ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$buttons[] = array(
|
||||
'type' => ( 'main' === $buttonType ) ? 'primary' : 'secondary',
|
||||
'url' => $buttonInfo->url,
|
||||
'text' => $buttonInfo->text,
|
||||
);
|
||||
}
|
||||
|
||||
return ! empty( $buttons ) ? $buttons : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a new notification into the database.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @param object $notification
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function insertNewNotification( $notification ) {
|
||||
$result = EDD()->notifications->insert( $this->getNotificationData( $notification ) );
|
||||
|
||||
if ( ! $result ) {
|
||||
throw new \Exception( 'Failed to insert into database.' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates an existing notification.
|
||||
*
|
||||
* @since 2.11.4
|
||||
*
|
||||
* @param int $existingId
|
||||
* @param object $notification
|
||||
*/
|
||||
protected function updateExistingNotification( $existingId, $notification ) {
|
||||
EDD()->notifications->update( $existingId, wp_parse_args( $this->getNotificationData( $notification ), array(
|
||||
'date_updated' => gmdate( 'Y-m-d H:i:s' ),
|
||||
) ) );
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user