'POST', 'callback' => array( $this, 'post' ), 'permission_callback' => array( self::class, 'permission' ), ), ) ); } /** * WordPress calls a permission callback a second time, from rest_send_allow_header() in serve_request(). That * second call fails harmlessly because the token is already consumed, so this method must never log or count a * failed attempt. * * @return true|\WP_Error */ public static function permission( \WP_REST_Request $request ) { if ( Options::consume_run_token( (string) $request->get_header( 'x-cortex-run-token' ) ) ) { return true; } return new \WP_Error( 'cortexcommerce_rest_forbidden', __( 'Sorry, you are not allowed to do that.', 'cortexcommerce' ), array( 'status' => 403 ) ); } public function post(): \WP_REST_Response { // The caller hangs up at once, as wp-cron's spawn does: keep running so the push is not cut off mid-run. ignore_user_abort( true ); ProductSyncSender::run(); return new \WP_REST_Response( null, 202 ); } }