'woocommerce', 'wc_' => 'woocommerce', 'wc-admin_' => 'woocommerce', 'wc_admin_' => 'woocommerce', 'action_scheduler' => 'action_scheduler', 'apwc_' => 'apwc', ); /** * Per-request answers, by hook. * * @var array */ private static $cache = array(); /** * Why a hook is protected, or '' when it isn't. * * @since 1.0.0 * @param string $hook Cron hook. * @return string `core`, `woocommerce`, `action_scheduler`, `apwc`, `callback_core`, * `callback_woocommerce`, `filter` or ''. */ public static function reason( string $hook ): string { if ( isset( self::$cache[ $hook ] ) ) { return self::$cache[ $hook ]; } $reason = self::find( $hook ); self::$cache[ $hook ] = $reason; return $reason; } /** * Whether a hook is protected. * * @since 1.0.0 * @param string $hook Cron hook. * @return bool */ public static function is_protected( string $hook ): bool { return '' !== self::reason( $hook ); } /** * Plain-text explanation for a reason. * * @since 1.0.0 * @param string $reason Reason from reason(). * @return string */ public static function label( string $reason ): string { switch ( $reason ) { case 'core': case 'callback_core': return __( 'Scheduled by WordPress. It can’t be deleted here.', 'admin-powerups-for-woocommerce' ); case 'woocommerce': case 'callback_woocommerce': return __( 'Scheduled by WooCommerce. It can’t be deleted here.', 'admin-powerups-for-woocommerce' ); case 'action_scheduler': return __( 'Runs the Action Scheduler queue. It can’t be deleted here.', 'admin-powerups-for-woocommerce' ); case 'apwc': return __( 'Scheduled by Admin Powerups. Switch the power-up off to remove it.', 'admin-powerups-for-woocommerce' ); case 'filter': return __( 'Protected by a site filter. It can’t be deleted here.', 'admin-powerups-for-woocommerce' ); default: return ''; } } /** * Forget per-request answers (tests, or after hooks change). * * @since 1.0.0 * @return void */ public static function flush() { self::$cache = array(); } /** * Work out the reason. * * @since 1.0.0 * @param string $hook Cron hook. * @return string */ private static function find( string $hook ): string { $key = strtolower( $hook ); if ( in_array( $key, self::CORE, true ) ) { return 'core'; } if ( in_array( $key, self::WOOCOMMERCE, true ) || preg_match( '/^wp_\d+_wc_[a-z0-9_]+$/', $key ) ) { return 'woocommerce'; // Also WooCommerce's background processes: wp_{blog id}_wc_{updater|emailer|…}_cron. } foreach ( self::PREFIXES as $prefix => $reason ) { if ( 0 === strpos( $key, $prefix ) ) { return $reason; } } if ( self::matches_filtered( $key ) ) { return 'filter'; } return self::callback_reason( $hook ); } /** * Whether a hook is on the list added with `apwc_protected_cron_hooks`. * * @since 1.0.0 * @param string $key Lowercased hook. * @return bool */ private static function matches_filtered( string $key ): bool { /** * Filter extra cron hooks the Cron inspector never deletes or runs early (since 1.0.0, * "Run now" is refused for them too). Add exact hook names, or * prefixes ending in `*` (for example `my_plugin_*`). The built-in list (WordPress core, * WooCommerce, Action Scheduler, Admin Powerups, and any hook with a core or WooCommerce * callback) always applies: this filter can only add. * * @since 1.0.0 * @param string[] $hooks Hook names or `prefix*` patterns. Empty by default. */ $extra = apply_filters( 'apwc_protected_cron_hooks', array() ); foreach ( is_array( $extra ) ? $extra : array() as $entry ) { if ( ! is_string( $entry ) || '' === trim( $entry ) ) { continue; } $entry = strtolower( trim( $entry ) ); if ( '*' === substr( $entry, -1 ) ) { $prefix = substr( $entry, 0, -1 ); if ( '' !== $prefix && 0 === strpos( $key, $prefix ) ) { return true; } } elseif ( $entry === $key ) { return true; } } return false; } /** * `callback_core` / `callback_woocommerce` when a callback on the hook is defined in WordPress * core or WooCommerce files, else ''. * * @since 1.0.0 * @param string $hook Cron hook. * @return string */ private static function callback_reason( string $hook ): string { foreach ( Callbacks::files( $hook ) as $file ) { $owner = Callbacks::owner_of_file( $file ); if ( 'core' === $owner['type'] ) { return 'callback_core'; } if ( 'woocommerce' === $owner['type'] ) { return 'callback_woocommerce'; } } return ''; } }