| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Help wizard handler. |
| 16 |
* |
| 17 |
* @since 1.18.2 (J) - 1.8.2 (WP) |
| 18 |
*/ |
| 19 |
class VBOHelpWizard |
| 20 |
{ |
| 21 |
use VBOFactoryAware; |
| 22 |
|
| 23 |
/** |
| 24 |
* A list of supported instructions. |
| 25 |
* |
| 26 |
* @var VBOHelpWizardInstruction[] |
| 27 |
*/ |
| 28 |
protected $instructions = null; |
| 29 |
|
| 30 |
/** |
| 31 |
* The registry where the wizard settings should be saved. |
| 32 |
* |
| 33 |
* @var VBOConfigRegistry |
| 34 |
*/ |
| 35 |
protected $config; |
| 36 |
|
| 37 |
/** |
| 38 |
* The minutes of delay before to display a new instruction. |
| 39 |
* |
| 40 |
* @var int |
| 41 |
*/ |
| 42 |
protected $delay; |
| 43 |
|
| 44 |
/** |
| 45 |
* Class constructor. |
| 46 |
* |
| 47 |
* @param array $options (config, delay, paths). |
| 48 |
*/ |
| 49 |
public function __construct(array $options = []) |
| 50 |
{ |
| 51 |
if (!($options['config'] ?? null) instanceof VBOConfigRegistry) { |
| 52 |
$options['config'] = VBOFactory::getConfig(); |
| 53 |
} |
| 54 |
|
| 55 |
$this->config = $options['config']; |
| 56 |
|
| 57 |
$this->delay = abs((int) ($options['delay'] ?? 0)); |
| 58 |
|
| 59 |
if (!empty($options['paths'])) |
| 60 |
{ |
| 61 |
$this->setIncludePaths($options['paths']); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @see VBOFactoryAware |
| 66 |
*/ |
| 67 |
$this->instanceClassPrefix = 'VBOHelpWizardDriver'; |
| 68 |
$this->instanceNamespaceSeparator = '.'; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Returns a list of supported instructions. |
| 73 |
* |
| 74 |
* @return VBOHelpWizardInstruction[] |
| 75 |
*/ |
| 76 |
public function getInstructions() |
| 77 |
{ |
| 78 |
if ($this->instructions === null) |
| 79 |
{ |
| 80 |
// internally cache the instances |
| 81 |
$this->instructions = array_values($this->getInstances()); |
| 82 |
} |
| 83 |
|
| 84 |
return $this->instructions; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns the instruction matching the specified identifier. |
| 89 |
* |
| 90 |
* @param string $id |
| 91 |
* |
| 92 |
* @return VBOHelpWizardInstruction|null |
| 93 |
*/ |
| 94 |
public function getInstruction(string $id) |
| 95 |
{ |
| 96 |
foreach ($this->getInstructions() as $needle) { |
| 97 |
if ($needle->getID() == $id) { |
| 98 |
return $needle; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return null; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Returns the next eligible instruction (supported but not configured). |
| 107 |
* |
| 108 |
* @return VBOHelpWizardInstruction|null |
| 109 |
*/ |
| 110 |
public function getNextInstruction() |
| 111 |
{ |
| 112 |
$lastCheckDateTime = $this->config->getString('help_wizard_last_check', null); |
| 113 |
|
| 114 |
// wait for the number of specified minutes |
| 115 |
if (JFactory::getDate('-' . $this->delay . ' minutes')->toISO8601() < $lastCheckDateTime) { |
| 116 |
return null; |
| 117 |
} |
| 118 |
|
| 119 |
foreach ($this->getInstructions() as $instruction) { |
| 120 |
if ($this->isDismissed($instruction)) { |
| 121 |
// instruction dismissed, move on |
| 122 |
continue; |
| 123 |
} |
| 124 |
|
| 125 |
if (!$instruction->isSupported()) { |
| 126 |
// instruction not supported, move on |
| 127 |
continue; |
| 128 |
} |
| 129 |
|
| 130 |
if ($instruction->isConfigured()) { |
| 131 |
// instruction already configured, move on |
| 132 |
continue; |
| 133 |
} |
| 134 |
|
| 135 |
// eligible instruction found |
| 136 |
return $instruction; |
| 137 |
} |
| 138 |
|
| 139 |
return null; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Checks whether the specified instruction has been dismissed. |
| 144 |
* |
| 145 |
* @param VBOHelpWizardInstruction $instruction |
| 146 |
* |
| 147 |
* @return bool |
| 148 |
*/ |
| 149 |
public function isDismissed(VBOHelpWizardInstruction $instruction): bool |
| 150 |
{ |
| 151 |
$dismissed = $this->config->getArray('help_wizard_dismissed', []); |
| 152 |
|
| 153 |
if (!array_key_exists($instruction->getID(), $dismissed)) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
$datetime = $dismissed[$instruction->getID()]; |
| 158 |
|
| 159 |
if ($datetime == 0) { |
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
return $datetime > JFactory::getDate('now')->toISO8601(); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Dismisses the specified instruction. |
| 168 |
* |
| 169 |
* @param VBOHelpWizardInstruction $instruction The instruction to dismiss. |
| 170 |
* @param string|null $datetime How long the instruction should stay silent. |
| 171 |
* If not specified, the instruction will be |
| 172 |
* permanently dismissed. |
| 173 |
* |
| 174 |
* @return void |
| 175 |
*/ |
| 176 |
public function dismiss(VBOHelpWizardInstruction $instruction, ?string $datetime = null): void |
| 177 |
{ |
| 178 |
if (!$instruction->isDismissible()) { |
| 179 |
throw new RuntimeException('The instruction [' . $instruction->getID() . '] cannot be dismissed!', 403); |
| 180 |
} |
| 181 |
|
| 182 |
if ($datetime) { |
| 183 |
try { |
| 184 |
// make sure we have a valid date time string |
| 185 |
$datetime = JFactory::getDate($datetime)->toISO8601(); |
| 186 |
} catch (Exception $error) { |
| 187 |
// invalid date time, permanently dismiss |
| 188 |
$datetime = null; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
// obtain dismissed instructions lookup (id->datetime) |
| 193 |
$dismissed = $this->config->getArray('help_wizard_dismissed', []); |
| 194 |
|
| 195 |
// register new threshold for the current instruction |
| 196 |
$dismissed[$instruction->getID()] = $datetime ?: 0; |
| 197 |
|
| 198 |
// update registry |
| 199 |
$this->config->set('help_wizard_dismissed', $dismissed); |
| 200 |
|
| 201 |
// update last check flag |
| 202 |
$this->config->set('help_wizard_last_check', JFactory::getDate('now')->toISO8601()); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* @inheritDoc |
| 207 |
* |
| 208 |
* @see VBOFactoryAware |
| 209 |
*/ |
| 210 |
protected function rearrangeInstances(&$list) |
| 211 |
{ |
| 212 |
// rearrange instructions by descending priority |
| 213 |
usort($list, function($a, $b) { |
| 214 |
return (int) $b->getPriority() - (int) $a->getPriority(); |
| 215 |
}); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* @inheritDoc |
| 220 |
* |
| 221 |
* @see VBOFactoryAware |
| 222 |
*/ |
| 223 |
protected function isInstanceValid($object) |
| 224 |
{ |
| 225 |
return $object instanceof VBOHelpWizardInstruction; |
| 226 |
} |
| 227 |
} |
| 228 |
|