| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2025 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 |
* Task schedule helper implementation. |
| 16 |
* |
| 17 |
* @since 1.18.0 (J) - 1.8.0 (WP) |
| 18 |
*/ |
| 19 |
final class VBOTaskSchedule |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
private static $types = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
private static $taskScheduleClassPrefix = 'VBOTaskScheduleType'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Attempts to return the task schedule for the requested interval and booking. |
| 33 |
* |
| 34 |
* @param string $schedule The schedule enumeration type. |
| 35 |
* @param VBOTaskBooking $booking The current task booking registry. |
| 36 |
* |
| 37 |
* @return ?VBOTaskScheduleInterface |
| 38 |
*/ |
| 39 |
public static function getType(string $scheduleEnum, VBOTaskBooking $booking) |
| 40 |
{ |
| 41 |
if ($scheduleClassName = static::exists($scheduleEnum)) { |
| 42 |
return new $scheduleClassName($scheduleEnum, $booking); |
| 43 |
} |
| 44 |
|
| 45 |
return null; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Tells whether the requested task schedule type exists. |
| 50 |
* |
| 51 |
* @param string $schedule The schedule enumeration type. |
| 52 |
* |
| 53 |
* @return string The schedule type class name or empty string. |
| 54 |
*/ |
| 55 |
public static function exists(string $scheduleEnum) |
| 56 |
{ |
| 57 |
if (!static::$types) { |
| 58 |
// load supported task schedule types |
| 59 |
static::$types = static::load(); |
| 60 |
} |
| 61 |
|
| 62 |
// build class name |
| 63 |
$scheduleClassName = static::$taskScheduleClassPrefix . ucfirst(strtolower($scheduleEnum)); |
| 64 |
|
| 65 |
if (isset(static::$types[$scheduleEnum]) && class_exists($scheduleClassName)) { |
| 66 |
return $scheduleClassName; |
| 67 |
} |
| 68 |
|
| 69 |
return ''; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Loads and returns all the supported task schedule types. |
| 74 |
* |
| 75 |
* @return array |
| 76 |
*/ |
| 77 |
private static function load() |
| 78 |
{ |
| 79 |
$types_list = []; |
| 80 |
$types_base = implode(DIRECTORY_SEPARATOR, [VBO_ADMIN_PATH, 'helpers', 'src', 'task', 'schedule', 'type', '']); |
| 81 |
$types_files = glob($types_base . '*.php'); |
| 82 |
|
| 83 |
/** |
| 84 |
* Trigger event to let other plugins register additional task schedule types. |
| 85 |
* |
| 86 |
* @return array A list of supported task schedule types. |
| 87 |
*/ |
| 88 |
$list = VBOFactory::getPlatform()->getDispatcher()->filter('onLoadTaskManagerScheduleTypes'); |
| 89 |
foreach ($list as $chunk) { |
| 90 |
// merge default type files with the returned ones |
| 91 |
$types_files = array_merge($types_files, (array) $chunk); |
| 92 |
} |
| 93 |
|
| 94 |
foreach ($types_files as $df) { |
| 95 |
// push type file key identifier and set related path |
| 96 |
$type_base_name = basename($df, '.php'); |
| 97 |
$types_list[$type_base_name] = $df; |
| 98 |
} |
| 99 |
|
| 100 |
return $types_list; |
| 101 |
} |
| 102 |
} |
| 103 |
|