| 1 |
<?php |
| 2 |
/** |
| 3 |
* Represents a WordPress core cron event. |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace Crontrol\Event; |
| 7 |
|
| 8 |
use Crontrol\Context\UserContext; |
| 9 |
use Crontrol\Context\FeatureContext; |
| 10 |
|
| 11 |
use function Crontrol\get_persistent_core_hooks; |
| 12 |
use function Crontrol\json_output; |
| 13 |
|
| 14 |
/** |
| 15 |
* Represents a WordPress core cron event. |
| 16 |
*/ |
| 17 |
final class CoreCronEvent extends Event { |
| 18 |
#[\Override] |
| 19 |
public function hook_name_editable(): bool { |
| 20 |
return false; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Core events are persistent if they're in the persistent core hooks list. |
| 25 |
*/ |
| 26 |
#[\Override] |
| 27 |
public function persistent(): bool { |
| 28 |
return in_array( $this->hook, get_persistent_core_hooks(), true ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Gets the message explaining why this event is persistent. |
| 33 |
* |
| 34 |
* @return string The persistent reason message. |
| 35 |
*/ |
| 36 |
#[\Override] |
| 37 |
public function get_persistent_message(): string { |
| 38 |
return __( 'This is a WordPress core event and cannot be deleted', 'wp-crontrol' ); |
| 39 |
} |
| 40 |
|
| 41 |
#[\Override] |
| 42 |
public function editable( UserContext $user, FeatureContext $features ): bool { |
| 43 |
return $user->can_edit_standard_cron_events(); |
| 44 |
} |
| 45 |
|
| 46 |
#[\Override] |
| 47 |
public function runnable( UserContext $user, FeatureContext $features ): bool { |
| 48 |
return $user->can_run_standard_cron_events() |
| 49 |
&& ! $this->has_error() |
| 50 |
&& ! $this->is_paused(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Check if this event can be deleted. |
| 55 |
* |
| 56 |
* Persistent core events cannot be deleted. Non-persistent core events |
| 57 |
* can be deleted if the user has manage_options capability. |
| 58 |
* |
| 59 |
* @param UserContext $user User capability context. |
| 60 |
* @param FeatureContext $features Feature flag context (not used). |
| 61 |
*/ |
| 62 |
#[\Override] |
| 63 |
public function deletable( UserContext $user, FeatureContext $features ): bool { |
| 64 |
if ( $this->persistent() ) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
return $user->can_delete_standard_cron_events(); |
| 69 |
} |
| 70 |
|
| 71 |
#[\Override] |
| 72 |
public function pausable(): bool { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
#[\Override] |
| 77 |
public function get_args_display(): string { |
| 78 |
if ( empty( $this->args ) ) { |
| 79 |
return ''; |
| 80 |
} |
| 81 |
return json_output( $this->args, false ); |
| 82 |
} |
| 83 |
|
| 84 |
#[\Override] |
| 85 |
public function is_enabled( FeatureContext $features ): bool { |
| 86 |
return true; |
| 87 |
} |
| 88 |
} |
| 89 |
|