PluginProbe
WP Crontrol / trunk
WP Crontrol vtrunk
1.21.2 trunk 0.1 0.2 0.3 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.1 1.10.0 1.11.0 1.12.0 1.12.1 1.13.0 1.13.1 1.13.2 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 All 57 releases
wp-crontrol / src / Event / PHPCronEvent.php

PHPCronEvent.php in WP Crontrol trunk, at src/Event/PHPCronEvent.php

126 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Represents a PHP cron event.
4 */
5
6 namespace Crontrol\Event;
7
8 use Crontrol\Context\UserContext;
9 use Crontrol\Context\FeatureContext;
10
11 /**
12 * Represents a PHP cron event.
13 */
14 final class PHPCronEvent extends CrontrolEvent {
15 /**
16 * The hook name for PHP cron events.
17 */
18 public const HOOK_NAME = 'crontrol_cron_job';
19
20 #[\Override]
21 public function integrity_failed(): bool {
22 $args = $this->args[0] ?? array();
23
24 // This is a PHP cron event saved prior to WP Crontrol 1.16.2.
25 if ( isset( $this->args['code'] ) ) {
26 return true;
27 }
28
29 return ! check_integrity( $args['code'] ?? null, $args['hash'] ?? null );
30 }
31
32 #[\Override]
33 public function has_error(): bool {
34 if ( isset( $this->args[0]['syntax_error_message'] ) ) {
35 return true;
36 }
37
38 return $this->integrity_failed();
39 }
40
41 #[\Override]
42 public function editable( UserContext $user, FeatureContext $features ): bool {
43 return $features->php_crons_enabled() && $user->can_edit_php_cron_events();
44 }
45
46 #[\Override]
47 public function runnable( UserContext $user, FeatureContext $features ): bool {
48 return $features->php_crons_enabled()
49 && $user->can_run_php_cron_events()
50 && ! $this->has_error()
51 && ! $this->is_paused();
52 }
53
54 /**
55 * Check if this event can be deleted.
56 *
57 * Per caps.md: Feature flag is NOT checked for delete operations.
58 *
59 * @param UserContext $user User capability context.
60 * @param FeatureContext $features Feature flag context (not used for delete).
61 */
62 #[\Override]
63 public function deletable( UserContext $user, FeatureContext $features ): bool {
64 return $user->can_delete_php_cron_events();
65 }
66
67 #[\Override]
68 public function get_hook_name_label(): string {
69 if ( ! empty( $this->args[0]['name'] ) ) {
70 return esc_html(
71 sprintf(
72 /* translators: %s: Details about the PHP cron event. */
73 __( 'PHP cron event (%s)', 'wp-crontrol' ),
74 $this->args[0]['name']
75 )
76 );
77 }
78
79 if ( ! empty( $this->args[0]['code'] ) ) {
80 $lines = explode( "\n", trim( $this->args[0]['code'] ) );
81 $code = reset( $lines );
82 $code = substr( $code, 0, 50 );
83
84 $php = sprintf(
85 '<code>%s</code>&hellip;',
86 esc_html( $code )
87 );
88
89 return sprintf(
90 /* translators: %s: Details about the PHP cron event. */
91 esc_html__( 'PHP cron event (%s)', 'wp-crontrol' ),
92 $php
93 );
94 }
95
96 return esc_html__( 'PHP cron event', 'wp-crontrol' );
97 }
98
99 #[\Override]
100 public function get_error_status_html(): string {
101 if ( ! isset( $this->args[0]['syntax_error_message'], $this->args[0]['syntax_error_line'] ) ) {
102 return '';
103 }
104
105 return sprintf(
106 '<br><span class="status-crontrol-error"><span class="dashicons dashicons-warning" aria-hidden="true"></span> %s</span>',
107 sprintf(
108 /* translators: 1: Line number, 2: Error message text */
109 esc_html__( 'Line %1$s: %2$s', 'wp-crontrol' ),
110 esc_html( number_format_i18n( $this->args[0]['syntax_error_line'] ) ),
111 esc_html( $this->args[0]['syntax_error_message'] )
112 )
113 );
114 }
115
116 #[\Override]
117 public function get_args_display(): string {
118 return __( 'PHP Code', 'wp-crontrol' );
119 }
120
121 #[\Override]
122 public function is_enabled( FeatureContext $features ): bool {
123 return $features->php_crons_enabled();
124 }
125 }
126