| 1 |
<?php |
| 2 |
/** |
| 3 |
* Represents a URL cron event. |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace Crontrol\Event; |
| 7 |
|
| 8 |
use Crontrol\Context\UserContext; |
| 9 |
use Crontrol\Context\FeatureContext; |
| 10 |
|
| 11 |
/** |
| 12 |
* Represents a URL cron event. |
| 13 |
*/ |
| 14 |
final class URLCronEvent extends CrontrolEvent { |
| 15 |
/** |
| 16 |
* The hook name for URL cron events. |
| 17 |
*/ |
| 18 |
public const HOOK_NAME = 'crontrol_url_cron_job'; |
| 19 |
|
| 20 |
#[\Override] |
| 21 |
public function integrity_failed(): bool { |
| 22 |
$args = $this->args[0] ?? array(); |
| 23 |
return ! check_integrity( $args['url'] ?? null, $args['hash'] ?? null ); |
| 24 |
} |
| 25 |
|
| 26 |
#[\Override] |
| 27 |
public function has_error(): bool { |
| 28 |
if ( isset( $this->args[0]['url_error_message'] ) ) { |
| 29 |
return true; |
| 30 |
} |
| 31 |
|
| 32 |
return $this->integrity_failed(); |
| 33 |
} |
| 34 |
|
| 35 |
#[\Override] |
| 36 |
public function editable( UserContext $user, FeatureContext $features ): bool { |
| 37 |
return $features->url_crons_enabled() && $user->can_edit_url_cron_events(); |
| 38 |
} |
| 39 |
|
| 40 |
#[\Override] |
| 41 |
public function runnable( UserContext $user, FeatureContext $features ): bool { |
| 42 |
return $features->url_crons_enabled() |
| 43 |
&& $user->can_run_url_cron_events() |
| 44 |
&& ! $this->has_error() |
| 45 |
&& ! $this->is_paused(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Check if this event can be deleted. |
| 50 |
* |
| 51 |
* Per caps.md: Feature flag is NOT checked for delete operations. |
| 52 |
* |
| 53 |
* @param UserContext $user User capability context. |
| 54 |
* @param FeatureContext $features Feature flag context (not used for delete). |
| 55 |
*/ |
| 56 |
#[\Override] |
| 57 |
public function deletable( UserContext $user, FeatureContext $features ): bool { |
| 58 |
return $user->can_delete_url_cron_events(); |
| 59 |
} |
| 60 |
|
| 61 |
#[\Override] |
| 62 |
public function get_hook_name_label(): string { |
| 63 |
if ( ! empty( $this->args[0]['name'] ) ) { |
| 64 |
/* translators: %s: Details about the URL cron event. */ |
| 65 |
return esc_html( sprintf( __( 'URL cron event (%s)', 'wp-crontrol' ), $this->args[0]['name'] ) ); |
| 66 |
} |
| 67 |
|
| 68 |
if ( ! empty( $this->args[0]['url'] ) ) { |
| 69 |
$url = sprintf( |
| 70 |
'<code>%s</code>', |
| 71 |
esc_html( $this->args[0]['url'] ) |
| 72 |
); |
| 73 |
|
| 74 |
/* translators: %s: Details about the URL cron event. */ |
| 75 |
return sprintf( esc_html__( 'URL cron event (%s)', 'wp-crontrol' ), $url ); |
| 76 |
} |
| 77 |
|
| 78 |
return esc_html__( 'URL cron event', 'wp-crontrol' ); |
| 79 |
} |
| 80 |
|
| 81 |
#[\Override] |
| 82 |
public function get_error_status_html(): string { |
| 83 |
if ( ! isset( $this->args[0]['url_error_message'] ) ) { |
| 84 |
return ''; |
| 85 |
} |
| 86 |
|
| 87 |
return sprintf( |
| 88 |
'<br><span class="status-crontrol-error"><span class="dashicons dashicons-warning" aria-hidden="true"></span> %s</span>', |
| 89 |
esc_html( $this->args[0]['url_error_message'] ) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
#[\Override] |
| 94 |
public function get_args_display(): string { |
| 95 |
return $this->args[0]['method'] . ' ' . $this->args[0]['url']; |
| 96 |
} |
| 97 |
|
| 98 |
#[\Override] |
| 99 |
public function is_enabled( FeatureContext $features ): bool { |
| 100 |
return $features->url_crons_enabled(); |
| 101 |
} |
| 102 |
} |
| 103 |
|