get_header( 'X-WP-Nonce' ); if ( '' === $nonce ) { $nonce = (string) $request->get_param( '_wpnonce' ); } return self::authorizeNonce( $nonce ); } /** * Login, capability and nonce, in that order, for a nonce the caller has already read. * * @return true|\WP_Error */ public static function authorizeNonce( string $nonce ) { $allowed = self::authorizeUser(); if ( is_wp_error( $allowed ) ) { return $allowed; } if ( '' === $nonce || ! wp_verify_nonce( $nonce, self::NONCE_ACTION ) ) { return new \WP_Error( 'om_bad_nonce', __( 'Security token expired. Reload the page and try again.', 'migratex' ), array( 'status' => 403 ) ); } return true; } /** * Logged in, and allowed to run migrations: the part of authorization that does not depend on a token. * * @return true|\WP_Error */ public static function authorizeUser() { if ( ! is_user_logged_in() ) { return new \WP_Error( 'om_unauthenticated', __( 'You must be logged in.', 'migratex' ), array( 'status' => 401 ) ); } if ( ! current_user_can( 'manage_options' ) || ( is_multisite() && ! is_super_admin() ) ) { return new \WP_Error( 'om_forbidden', __( 'You are not allowed to run migrations.', 'migratex' ), array( 'status' => 403 ) ); } return true; } public static function createNonce(): string { return wp_create_nonce( self::NONCE_ACTION ); } public static function createAjaxNonce(): string { return wp_create_nonce( self::AJAX_NONCE_ACTION ); } }