>|null */ private $events = null; /** * Every scheduled event, soonest first. * * @since 1.0.0 * @return array */ public function all(): array { if ( null !== $this->events ) { return $this->events; } $out = array(); $cron = _get_cron_array(); foreach ( is_array( $cron ) ? $cron : array() as $timestamp => $hooks ) { if ( ! is_int( $timestamp ) && ! ctype_digit( (string) $timestamp ) ) { continue; } foreach ( is_array( $hooks ) ? $hooks : array() as $hook => $events ) { $hook = (string) $hook; // PHP turns a numeric hook name like "123" into an int key. if ( '' === $hook ) { continue; } foreach ( is_array( $events ) ? $events : array() as $sig => $event ) { if ( ! is_string( $sig ) ) { continue; } $event = is_array( $event ) ? $event : array(); $out[] = array( 'timestamp' => (int) $timestamp, 'hook' => $hook, 'sig' => $sig, 'args' => $event['args'] ?? array(), 'schedule' => isset( $event['schedule'] ) && is_string( $event['schedule'] ) ? $event['schedule'] : '', 'interval' => isset( $event['interval'] ) && is_numeric( $event['interval'] ) ? (int) $event['interval'] : 0, ); } } } usort( $out, static fn( $a, $b ) => $a['timestamp'] <=> $b['timestamp'] ); $this->events = $out; return $out; } /** * Forget the cached read (after a change in the same request). * * @since 1.0.0 * @return void */ public function flush() { $this->events = null; } /** * One event read fresh from the cron array, or null when it isn't scheduled any more. * * @since 1.0.0 * @param string $hook Hook. * @param int $timestamp Timestamp. * @param string $sig Args signature (md5 of the serialized args). * @return array{timestamp: int, hook: string, sig: string, args: mixed, schedule: string, interval: int}|null */ public static function find( string $hook, int $timestamp, string $sig ) { $cron = _get_cron_array(); if ( ! isset( $cron[ $timestamp ][ $hook ][ $sig ] ) || ! is_array( $cron[ $timestamp ][ $hook ][ $sig ] ) ) { return null; } $event = $cron[ $timestamp ][ $hook ][ $sig ]; return array( 'timestamp' => $timestamp, 'hook' => $hook, 'sig' => $sig, 'args' => $event['args'] ?? array(), 'schedule' => isset( $event['schedule'] ) && is_string( $event['schedule'] ) ? $event['schedule'] : '', 'interval' => isset( $event['interval'] ) && is_numeric( $event['interval'] ) ? (int) $event['interval'] : 0, ); } /** * Seconds past its time before an event counts as overdue. * * @since 1.0.0 * @return int */ public static function grace(): int { /** * Filter how late (in seconds) a cron event may be before the Cron inspector calls it * overdue. WP-Cron only runs on page visits, so a few minutes late is normal. * * @since 1.0.0 * @param int $seconds Grace period. Default 600 (10 minutes). */ $grace = apply_filters( 'apwc_cron_overdue_grace', 10 * MINUTE_IN_SECONDS ); return is_numeric( $grace ) ? max( 0, (int) $grace ) : 10 * MINUTE_IN_SECONDS; } /** * Whether a hook has a callback in this request. * * @since 1.0.0 * @param string $hook Hook. * @return bool */ public static function has_callback( string $hook ): bool { return false !== has_action( $hook ); } /** * Whether another plugin replaces WP-Cron here (any of REPLACE_FILTERS has a callback). * * @since 1.0.0 * @return string[] The filters that have callbacks (empty when WP-Cron is the default one). */ public static function replaced_by(): array { $out = array(); foreach ( self::REPLACE_FILTERS as $filter ) { if ( false !== has_filter( $filter ) ) { $out[] = $filter; } } return $out; } /** * Counts for the filter menu. * * @since 1.0.0 * @param int|null $now Current time (tests). * @return array{all: int, due: int, overdue: int, orphaned: int} */ public function counts( $now = null ): array { $now = null === $now ? time() : (int) $now; $grace = self::grace(); $counts = array( 'all' => 0, 'due' => 0, 'overdue' => 0, 'orphaned' => 0, ); $hooks = array(); foreach ( $this->all() as $event ) { ++$counts['all']; if ( $event['timestamp'] <= $now ) { ++$counts['due']; } if ( $event['timestamp'] < $now - $grace ) { ++$counts['overdue']; } if ( ! isset( $hooks[ $event['hook'] ] ) ) { $hooks[ $event['hook'] ] = self::has_callback( $event['hook'] ); } if ( ! $hooks[ $event['hook'] ] ) { ++$counts['orphaned']; } } return $counts; } /** * Events matching a filter and search, soonest first. * * @since 1.0.0 * @param string $filter '' (all), `due`, `overdue` or `orphaned`. * @param string $search Text the hook must contain (any case). * @param int|null $now Current time (tests). * @return array> */ public function filtered( string $filter, string $search = '', $now = null ): array { $now = null === $now ? time() : (int) $now; $grace = self::grace(); $search = strtolower( trim( $search ) ); $hooks = array(); $out = array(); foreach ( $this->all() as $event ) { if ( '' !== $search && false === strpos( strtolower( $event['hook'] ), $search ) ) { continue; } if ( 'due' === $filter && $event['timestamp'] > $now ) { continue; } if ( 'overdue' === $filter && $event['timestamp'] >= $now - $grace ) { continue; } if ( 'orphaned' === $filter ) { if ( ! isset( $hooks[ $event['hook'] ] ) ) { $hooks[ $event['hook'] ] = self::has_callback( $event['hook'] ); } if ( $hooks[ $event['hook'] ] ) { continue; } } $out[] = $event; } return $out; } /** * Hooks by number of events, most first. * * @since 1.0.0 * @return array Hook => events. */ public function hook_counts(): array { $counts = array(); foreach ( $this->all() as $event ) { $counts[ $event['hook'] ] = ( $counts[ $event['hook'] ] ?? 0 ) + 1; } uksort( $counts, static function ( $a, $b ) use ( $counts ) { return array( $counts[ $b ], $a ) <=> array( $counts[ $a ], $b ); } ); return $counts; } /** * Hooks with no callback in this request, and whether "Delete all" is allowed for each. * * @since 1.0.0 * @param int $max Most hooks to return. * @return array, deletable: bool}> */ public function orphaned_hooks( int $max = 50 ): array { $out = array(); foreach ( $this->hook_counts() as $hook => $count ) { if ( self::has_callback( (string) $hook ) ) { continue; } $hook = (string) $hook; $protected = ProtectedHooks::reason( $hook ); $owner = Owners::guess( $hook, 'cron_hook' ); $out[] = array( 'hook' => $hook, 'count' => (int) $count, 'protected' => $protected, 'owner' => $owner, 'deletable' => '' === $protected && self::owner_allows_bulk_delete( $owner ), ); if ( count( $out ) >= $max ) { break; } } return $out; } /** * Whether "Delete all events" is allowed for a hook with this owner guess: only when the * owner is clearly a plugin or theme that is inactive or not installed. An active, core, * WooCommerce or unknown owner may add its callback only while cron runs, so its events stay. * * @since 1.0.0 * @param array $owner Owners::guess() answer. * @return bool */ public static function owner_allows_bulk_delete( array $owner ): bool { return in_array( $owner['type'] ?? '', array( 'plugin', 'theme' ), true ) && in_array( $owner['state'] ?? '', array( 'inactive', 'not-installed' ), true ) && in_array( $owner['confidence'] ?? '', array( 'exact', 'likely' ), true ); } /** * Display details for the rows on one page: schedule label, state, callbacks, owner and * safe args. Callbacks and owners are looked up for these rows only. * * @since 1.0.0 * @param array> $events Events on the page. * @param int|null $now Current time (tests). * @return array> */ public function rows( array $events, $now = null ): array { $now = null === $now ? time() : (int) $now; $grace = self::grace(); $schedules = wp_get_schedules(); $out = array(); foreach ( $events as $event ) { $hook = (string) $event['hook']; $schedule = (string) $event['schedule']; $known = '' === $schedule || isset( $schedules[ $schedule ] ); $label = '' === $schedule ? __( 'Once', 'admin-powerups-for-woocommerce' ) : ( $known && isset( $schedules[ $schedule ]['display'] ) && is_scalar( $schedules[ $schedule ]['display'] ) ? Str::scalar( $schedules[ $schedule ]['display'] ) : $schedule ); $callbacks = Callbacks::describe( $hook ); $out[] = array_merge( $event, array( 'args' => self::safe_args( $event['args'] ), 'args_count' => is_array( $event['args'] ) ? count( $event['args'] ) : 1, 'schedule_label' => $label, 'known_schedule' => $known, 'recurring' => '' !== $schedule, 'due' => $event['timestamp'] <= $now, 'overdue' => $event['timestamp'] < $now - $grace, 'has_callback' => self::has_callback( $hook ), 'callbacks' => $callbacks, 'protected' => ProtectedHooks::reason( $hook ), 'owner' => self::row_owner( $hook, $callbacks ), ) ); } return $out; } /** * Args ready to show or JSON-encode: SafeDump (no object code runs), then values under * sensitive keys masked. * * @since 1.0.0 * @param mixed $args Raw event args. * @return mixed */ public static function safe_args( $args ) { return self::mask( SafeDump::to_array( $args, 8, 2000, 1000 ), self::mask_patterns() ); } /** * Key patterns whose values are masked in shown args. * * @since 1.0.0 * @return string[] */ public static function mask_patterns(): array { return MetaMask::arg_patterns( 'cron' ); // Filtered with `apwc_sensitive_arg_patterns`. } /** * Mask values whose string key matches a pattern, at any depth. * * @since 1.0.0 * @param mixed $value SafeDump output. * @param string[] $patterns Patterns. * @return mixed */ private static function mask( $value, array $patterns ) { if ( ! is_array( $value ) || ! $patterns ) { return $value; } foreach ( $value as $key => $item ) { if ( is_string( $key ) && MetaMask::matches( $key, $patterns ) ) { $value[ $key ] = __( '[masked]', 'admin-powerups-for-woocommerce' ); } elseif ( is_array( $item ) ) { $value[ $key ] = self::mask( $item, $patterns ); } } return $value; } /** * Who a row's hook belongs to: the owner of its first callback file, else a guess from the * hook name. * * @since 1.0.0 * @param string $hook Hook. * @param array> $callbacks Callbacks::describe() output. * @return array{type: string, label: string} */ private static function row_owner( string $hook, array $callbacks ): array { foreach ( $callbacks as $callback ) { $owner = $callback['owner']; if ( 'unknown' !== $owner['type'] ) { return array( 'type' => $owner['type'], 'label' => self::owner_label( $owner['type'], $owner['slug'], $owner['name'] ), ); } } $guess = Owners::guess( $hook, 'cron_hook' ); if ( 'unknown' === $guess['type'] ) { return array( 'type' => 'unknown', 'label' => '', ); } $label = self::owner_label( $guess['type'], $guess['slug'], $guess['name'] ); return array( 'type' => $guess['type'], 'label' => 'likely' === $guess['confidence'] /* translators: %s: plugin, theme or "WordPress". */ ? sprintf( __( 'Probably %s', 'admin-powerups-for-woocommerce' ), $label ) : $label, ); } /** * "WordPress", "WooCommerce", "Plugin: name", "Theme: name", "Must-use plugin: name". * * @since 1.0.0 * @param string $type Owner type. * @param string $slug Slug. * @param string $name Name. * @return string */ public static function owner_label( string $type, string $slug, string $name ): string { $name = '' !== $name ? $name : $slug; switch ( $type ) { case 'core': return __( 'WordPress', 'admin-powerups-for-woocommerce' ); case 'woocommerce': return __( 'WooCommerce', 'admin-powerups-for-woocommerce' ); case 'plugin': /* translators: %s: plugin name or folder. */ return sprintf( __( 'Plugin: %s', 'admin-powerups-for-woocommerce' ), $name ); case 'mu-plugin': /* translators: %s: must-use plugin file or folder. */ return sprintf( __( 'Must-use plugin: %s', 'admin-powerups-for-woocommerce' ), $name ); case 'theme': /* translators: %s: theme name or folder. */ return sprintf( __( 'Theme: %s', 'admin-powerups-for-woocommerce' ), $name ); default: return ''; } } /** * Status facts for the top of the tab. * * @since 1.0.0 * @param int|null $now Current time (tests). * @return array */ public function status( $now = null ): array { global $wpdb; $now = null === $now ? time() : (int) $now; $counts = $this->counts( $now ); $oldest = null; $grace = self::grace(); foreach ( $this->all() as $event ) { if ( $event['timestamp'] < $now - $grace ) { $oldest = $event; } break; // Sorted: only the first event can be the oldest. } $lock = get_transient( 'doing_cron' ); $lock = is_numeric( $lock ) ? (float) $lock : 0.0; // One indexed read of the stored size (never re-serialize the args: that runs object code). // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- admin tool screen only; size isn't cached anywhere. $size = (int) $wpdb->get_var( $wpdb->prepare( "SELECT LENGTH(option_value) FROM {$wpdb->options} WHERE option_name = %s", 'cron' ) ); return array( 'disabled' => defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON, 'alternate' => defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON, 'lock_timeout' => defined( 'WP_CRON_LOCK_TIMEOUT' ) ? (int) WP_CRON_LOCK_TIMEOUT : MINUTE_IN_SECONDS, 'lock' => $lock, 'lock_age' => $lock > 0 ? max( 0, (int) round( microtime( true ) - $lock ) ) : null, 'counts' => $counts, 'oldest' => $oldest, 'size' => $size, 'big' => $size > self::BIG_OPTION, 'replaced' => self::replaced_by(), 'probe' => Probe::last(), ); } }