-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upd. Restrict functionality for expired licenses (#346)
* Ref. New FeatureRestriction service is used to on/off features. * Fix. Code. FeatureRestrictionState. Renamed method. * Upd. Functionality. Code review notices fixed. * Upd. Functionality. Functions restriction for outdated licenses fixed. * Upd. Settings. FS Watcher tab logic fixed. * Fix: Settings. Upgrading license link fixed. --------- Co-authored-by: Glomberg <[email protected]>
- Loading branch information
1 parent
7e25362
commit dc31a37
Showing
17 changed files
with
766 additions
and
465 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
lib/CleantalkSP/SpbctWP/FeatureRestriction/FeatureRestriction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace CleantalkSP\SpbctWP\FeatureRestriction; | ||
|
||
class FeatureRestriction | ||
{ | ||
/** | ||
* @var string | ||
* @psalm-suppress PossiblyUnusedProperty | ||
*/ | ||
public $name; | ||
/** | ||
* @var bool | ||
*/ | ||
public $on_moderate_fail; | ||
/** | ||
* @var bool | ||
*/ | ||
public $on_key_fail; | ||
|
||
/** | ||
* @param string $name | ||
* @param bool $on_moderate_fail | ||
* @param bool $on_key_fail | ||
*/ | ||
public function __construct($name, $on_moderate_fail = false, $on_key_fail = false) | ||
{ | ||
$this->name = $name; | ||
$this->on_moderate_fail = isset($on_moderate_fail) ? (bool) $on_moderate_fail : false; | ||
$this->on_key_fail = isset($on_key_fail) ? (bool) $on_key_fail : false; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
lib/CleantalkSP/SpbctWP/FeatureRestriction/FeatureRestrictionService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace CleantalkSP\SpbctWP\FeatureRestriction; | ||
|
||
use CleantalkSP\SpbctWP\State; | ||
|
||
class FeatureRestrictionService | ||
{ | ||
/** | ||
* @var FeatureRestriction[] | ||
*/ | ||
public $restrictions; | ||
|
||
/** | ||
* Construct service. Init all the restrictions. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->restrictions = $this->initRestrictions(); | ||
} | ||
|
||
/** | ||
* @return FeatureRestriction[] | ||
*/ | ||
public function initRestrictions() | ||
{ | ||
$restrictions[] = new FeatureRestriction('firewall_log', true, true); | ||
$restrictions[] = new FeatureRestriction('scanner', true, true); | ||
$restrictions[] = new FeatureRestriction('security_log', true, true); | ||
$restrictions[] = new FeatureRestriction('fswatcher', true, true); | ||
$restrictions[] = new FeatureRestriction('backups', true, true); | ||
return $restrictions; | ||
} | ||
|
||
|
||
/** | ||
* Get the state of a feature for the given SPBC state and feature name. | ||
* | ||
* @param State $spbc Global SPBC State object | ||
* @param string $feature_name The name of the feature. | ||
* @return FeatureRestrictionState The state of the feature. | ||
* @throws \Exception If called restriction name is not registered | ||
* @psalm-suppress PossiblyUnusedMethod | ||
*/ | ||
public function getState($spbc, $feature_name) | ||
{ | ||
$result = new FeatureRestrictionState(); | ||
|
||
$current_feature_restrictions = $this->getRestrictionByName($feature_name); | ||
|
||
if ( !$current_feature_restrictions) { | ||
throw new \Exception(__CLASS__ . ' error: Feature restriction name is not registered! ' . $feature_name); | ||
} | ||
|
||
if ( ! $spbc->key_is_ok ) { | ||
if ($current_feature_restrictions->on_key_fail) { | ||
$result->is_active = false; | ||
$result->info_html = FeatureRestrictionView::keyNotValid(); | ||
} | ||
} elseif ( ! $spbc->moderate ) { | ||
if ( $current_feature_restrictions->on_moderate_fail ) { | ||
$result->is_active = false; | ||
if ( $spbc->data['key_changed'] ) { | ||
// Here we need to check if the key was changed and the plugin is waiting for the sync. | ||
$result->info_html = FeatureRestrictionView::waitForSync(); | ||
} else { | ||
$result->info_html = FeatureRestrictionView::renewNotice(); | ||
} | ||
} | ||
} | ||
return $result; | ||
} | ||
|
||
|
||
/** | ||
* Get the restriction by name. | ||
* | ||
* @param string $name The name of the restriction to retrieve. | ||
* | ||
* @return FeatureRestriction|false The found restriction object if the name matches, or false if no restriction was found. | ||
*/ | ||
private function getRestrictionByName($name) | ||
{ | ||
foreach ($this->restrictions as $restriction) { | ||
if ($restriction->name === $name) { | ||
return $restriction; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
lib/CleantalkSP/SpbctWP/FeatureRestriction/FeatureRestrictionState.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace CleantalkSP\SpbctWP\FeatureRestriction; | ||
|
||
/** | ||
* Class FeatureRestrictionState | ||
* | ||
* Represents the restriction state of a feature. | ||
*/ | ||
class FeatureRestrictionState | ||
{ | ||
/** | ||
* @var bool | ||
* @psalm-suppress PossiblyUnusedProperty | ||
*/ | ||
public $is_active; | ||
/** | ||
* @var string | ||
*/ | ||
public $info_html; | ||
|
||
/** | ||
* Constructor method for the class. | ||
* | ||
* @param bool $is_active (Optional) The flag to indicate if the object is active. Defaults to true. | ||
* @param string $info_html (Optional) The HTML information string. Defaults to an empty string. | ||
* @return void | ||
*/ | ||
public function __construct($is_active = true, $info_html = '') | ||
{ | ||
$this->is_active = isset($is_active) ? (bool) $is_active : true; | ||
$this->info_html = isset($info_html) ? $info_html : ''; | ||
} | ||
|
||
/** | ||
* Sanitizes and escapes HTML output. | ||
* | ||
* This method uses the escKsesPreset() function from the Escape class | ||
* to sanitize and escape the given HTML output. | ||
* | ||
* @return string The sanitized and escaped HTML output. | ||
* @psalm-suppress PossiblyUnusedMethod | ||
*/ | ||
public function sanitizedReasonOutput() | ||
{ | ||
return \CleantalkSP\SpbctWP\Escape::escKsesPreset( | ||
$this->info_html, | ||
'spbc_settings__feature_restrictions', | ||
array(), | ||
array('display') | ||
); | ||
} | ||
} |
Oops, something went wrong.