__( 'Cron events', 'admin-powerups-for-woocommerce' ) ); } /** * {@inheritDoc} * * @since 1.0.0 * @return array> */ public function data() { return array_merge( parent::data(), array( array( 'id' => 'loopback', 'kind' => 'cache', 'label' => __( 'Last loopback check', 'admin-powerups-for-woocommerce' ), 'description' => __( 'The result of the last “Check loopback”, kept for 12 hours.', 'admin-powerups-for-woocommerce' ), 'count' => static fn() => false === get_transient( Probe::TRANSIENT ) ? 0 : 1, 'summary' => static fn( $count ) => $count ? __( 'Stored', 'admin-powerups-for-woocommerce' ) : __( 'Nothing stored', 'admin-powerups-for-woocommerce' ), 'delete' => static function () { delete_transient( Probe::TRANSIENT ); return 0; }, ), Cleanup::record( 'events', __( 'Run or deleted events', 'admin-powerups-for-woocommerce' ), __( 'Events you run now or delete change WordPress’s own cron schedule. That stays when the power-up is off or removed.', 'admin-powerups-for-woocommerce' ) ), ) ); } /** * {@inheritDoc} REST routes only. */ public function boot() { add_action( 'rest_api_init', array( $this, 'register_routes' ) ); } /** * Tab URL. * * @since 1.0.0 * @param array $args Query args (status, s, paged). * @return string */ public static function url( array $args = array() ) { $args = array_filter( $args, static fn( $v ) => '' !== $v && null !== $v && 0 !== $v ); // add_query_arg() doesn't encode values: a hook name with "&" or "#" would break the link. return add_query_arg( array_map( static fn( $v ) => rawurlencode( (string) $v ), $args ), AdminPage::url( 'cron-inspector', self::VIEW ) ); } /** * {@inheritDoc} * * @since 1.0.0 * @param string $view View ID. */ public function render_view( $view ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- read-only screen filters. $get = static fn( $key ) => isset( $_GET[ $key ] ) && is_string( $_GET[ $key ] ) ? sanitize_text_field( wp_unslash( $_GET[ $key ] ) ) : ''; $filter = array( 'status' => in_array( $get( 'status' ), Events::FILTERS, true ) ? $get( 'status' ) : '', 's' => mb_substr( $get( 's' ), 0, self::MAX_HOOK ), 'paged' => Paging::page( $get( 'paged' ), Events::PER_PAGE ), ); // phpcs:enable $events = new Events(); $now = time(); $status = $events->status( $now ); $matches = $events->filtered( $filter['status'], $filter['s'], $now ); $total = count( $matches ); $pages = max( 1, (int) ceil( $total / Events::PER_PAGE ) ); if ( $filter['paged'] > $pages ) { $filter['paged'] = $pages; } $rows = $events->rows( array_slice( $matches, ( $filter['paged'] - 1 ) * Events::PER_PAGE, Events::PER_PAGE ), $now ); $top = array_slice( $events->hook_counts(), 0, 5, true ); $orphans = $events->orphaned_hooks(); $readonly = (bool) $status['replaced']; $advanced = DevGuard::can( $this, DevGuard::ADVANCED ); $confirm = DevGuard::confirm_word( 'delete' ); include __DIR__ . '/views/events.php'; } /** * {@inheritDoc} * * @since 1.0.0 * @param string $view View ID. */ public function enqueue_view_assets( $view ) { wp_enqueue_style( 'apwc-cron-inspector', Assets::url( 'assets/css/cron-inspector.css' ), array( 'apwc-admin' ), Assets::ver( 'assets/css/cron-inspector.css' ) ); wp_enqueue_script( 'apwc-cron-inspector', Assets::url( 'assets/js/cron-inspector.js' ), array( 'wp-api-fetch', 'wp-i18n', 'apwc-dev-tools', 'apwc-admin' ), Assets::ver( 'assets/js/cron-inspector.js' ), true ); wp_set_script_translations( 'apwc-cron-inspector', 'admin-powerups-for-woocommerce' ); } /* ------------------------------------------------------------------ REST */ /** * REST routes (registered only while the power-up is on). * * @since 1.0.0 * @return void */ public function register_routes() { $event_args = array( 'hook' => array( 'required' => true, 'type' => 'string', 'validate_callback' => array( $this, 'valid_hook' ), ), 'timestamp' => array( 'required' => true, 'validate_callback' => array( $this, 'valid_timestamp' ), ), 'sig' => array( 'required' => true, 'type' => 'string', 'validate_callback' => array( $this, 'valid_sig' ), ), ); // Run now: Review; WordPress and WooCommerce hooks need Advanced + the typed word, other // protected hooks are refused (rest_run(), Actions::run_level()). register_rest_route( 'apwc/v1', '/cron/run', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'rest_run' ), 'permission_callback' => DevGuard::permission( $this, DevGuard::REVIEW ), 'args' => $event_args + array( 'confirm' => array( 'type' => 'string', 'default' => '', ), ), ) ); register_rest_route( 'apwc/v1', '/cron/delete', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'rest_delete' ), 'permission_callback' => DevGuard::permission( $this, DevGuard::ADVANCED, '', 'delete' ), 'args' => array( 'events' => array( 'required' => true, 'validate_callback' => array( $this, 'valid_events' ), ), 'confirm' => array( 'type' => 'string', 'default' => '', ), ), ) ); register_rest_route( 'apwc/v1', '/cron/delete-hook', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'rest_delete_hook' ), 'permission_callback' => DevGuard::permission( $this, DevGuard::ADVANCED, '', 'delete' ), 'args' => array( 'hook' => $event_args['hook'], 'confirm' => array( 'type' => 'string', 'default' => '', ), ), ) ); register_rest_route( 'apwc/v1', '/cron/loopback', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'rest_loopback' ), 'permission_callback' => DevGuard::permission( $this, DevGuard::SAFE ), ) ); } /** * Validate a hook name. * * @since 1.0.0 * @param mixed $value Value. * @return bool */ public function valid_hook( $value ) { return is_string( $value ) && '' !== $value && strlen( $value ) <= self::MAX_HOOK && ! preg_match( '/[\x00-\x1F\x7F]/', $value ); } /** * Validate a timestamp (positive whole number, as an int or digits). * * @since 1.0.0 * @param mixed $value Value. * @return bool */ public function valid_timestamp( $value ) { if ( is_int( $value ) ) { return $value > 0; } return is_string( $value ) && 1 === preg_match( '/^[1-9][0-9]{0,11}$/', $value ); } /** * Validate an args signature (md5 hex). * * @since 1.0.0 * @param mixed $value Value. * @return bool */ public function valid_sig( $value ) { return is_string( $value ) && 1 === preg_match( '/^[a-f0-9]{32}$/', $value ); } /** * Validate the events list of a delete: 1–20 items of { hook, timestamp, sig }. * * @since 1.0.0 * @param mixed $value Value. * @return true|WP_Error */ public function valid_events( $value ) { if ( ! is_array( $value ) || ! $value || count( $value ) > Actions::MAX_DELETE || ! array_is_list( $value ) ) { return new WP_Error( 'rest_invalid_param', /* translators: %d: most events per request. */ sprintf( __( 'Choose between 1 and %d events.', 'admin-powerups-for-woocommerce' ), Actions::MAX_DELETE ), array( 'status' => 400 ) ); } foreach ( $value as $item ) { if ( ! is_array( $item ) || ! $this->valid_hook( $item['hook'] ?? null ) || ! $this->valid_timestamp( $item['timestamp'] ?? null ) || ! $this->valid_sig( $item['sig'] ?? null ) ) { return new WP_Error( 'rest_invalid_param', __( 'An event in the list isn’t valid.', 'admin-powerups-for-woocommerce' ), array( 'status' => 400 ) ); } } return true; } /** * POST /cron/run. * * @since 1.0.0 * @param WP_REST_Request $request Request. * @return \WP_REST_Response|WP_Error */ public function rest_run( WP_REST_Request $request ) { if ( DevGuard::ADVANCED === Actions::run_level( (string) $request['hook'] ) ) { if ( ! DevGuard::can( $this, DevGuard::ADVANCED ) ) { return new WP_Error( 'apwc_forbidden', __( 'Running WordPress or WooCommerce events early needs an admin who can make Advanced changes.', 'admin-powerups-for-woocommerce' ), array( 'status' => 403 ) ); } $confirmed = DevGuard::check_confirm( $request, DevGuard::confirm_word( 'run' ) ); if ( is_wp_error( $confirmed ) ) { return $confirmed; } } $result = Actions::run( (string) $request['hook'], (int) $request['timestamp'], (string) $request['sig'] ); return is_wp_error( $result ) ? $result : rest_ensure_response( $result ); } /** * POST /cron/delete (Advanced: typed word checked here, whatever the page did). * * @since 1.0.0 * @param WP_REST_Request $request Request. * @return \WP_REST_Response|WP_Error */ public function rest_delete( WP_REST_Request $request ) { $confirmed = DevGuard::check_confirm( $request, DevGuard::confirm_word( 'delete' ) ); if ( is_wp_error( $confirmed ) ) { return $confirmed; } $blocked = Actions::check_writable(); if ( $blocked ) { return $blocked; } $refs = array(); $seen = array(); foreach ( (array) $request['events'] as $item ) { $ref = array( 'hook' => (string) $item['hook'], 'timestamp' => (int) $item['timestamp'], 'sig' => (string) $item['sig'], ); $key = $ref['timestamp'] . '|' . $ref['hook'] . '|' . $ref['sig']; if ( ! isset( $seen[ $key ] ) ) { $seen[ $key ] = true; $refs[] = $ref; } } $results = Actions::delete( $refs ); if ( 1 === count( $results ) && 'deleted' !== $results[0]['result'] ) { $status = array( 'gone' => 404, 'protected' => 403, 'came_back' => 409, 'unmatched' => 409, 'failed' => 409, ); $code = $results[0]['result']; return new WP_Error( 'apwc_cron_' . $code, $results[0]['message'], array( 'status' => $status[ $code ] ?? 409 ) ); } $deleted = count( array_filter( $results, static fn( $r ) => 'deleted' === $r['result'] ) ); $message = count( $results ) === $deleted /* translators: %s: number of events. */ ? sprintf( _n( 'Deleted %s event.', 'Deleted %s events.', $deleted, 'admin-powerups-for-woocommerce' ), number_format_i18n( $deleted ) ) /* translators: 1: events deleted, 2: events chosen. */ : sprintf( __( 'Deleted %1$s of %2$s events. Reload the list to see the rest.', 'admin-powerups-for-woocommerce' ), number_format_i18n( $deleted ), number_format_i18n( count( $results ) ) ); return rest_ensure_response( array( 'deleted' => $deleted, 'results' => $results, 'message' => $message, ) ); } /** * POST /cron/delete-hook (Advanced: typed word checked here). * * @since 1.0.0 * @param WP_REST_Request $request Request. * @return \WP_REST_Response|WP_Error */ public function rest_delete_hook( WP_REST_Request $request ) { $confirmed = DevGuard::check_confirm( $request, DevGuard::confirm_word( 'delete' ) ); if ( is_wp_error( $confirmed ) ) { return $confirmed; } $result = Actions::delete_hook( (string) $request['hook'] ); return is_wp_error( $result ) ? $result : rest_ensure_response( $result ); } /** * POST /cron/loopback. * * @since 1.0.0 * @return \WP_REST_Response */ public function rest_loopback() { return rest_ensure_response( Probe::run() ); } }