$ids ] ) ) )->ids( count( $ids ) ); } /** * Store a selection for one user. * * @param int $user_id User the selection belongs to. * @param int[] $ids Order IDs, already checked with visible(). * @return string Token for the builder URL. */ public static function store( int $user_id, array $ids ): string { $token = strtolower( wp_generate_password( 24, false, false ) ); set_transient( self::key( $user_id, $token ), Filters::sanitize_ids( $ids ), self::TTL ); return $token; } /** * Read a selection back. Only the user who made it can. * * @param int $user_id Current user. * @param string $token Token from the URL. * @return int[]|null Null when the token is malformed, unknown or expired. */ public static function load( int $user_id, string $token ): ?array { if ( $user_id <= 0 || ! self::valid_token( $token ) ) { return null; } $ids = get_transient( self::key( $user_id, $token ) ); return is_array( $ids ) ? Filters::sanitize_ids( $ids ) : null; } /** * The builder, opened on a stored selection. * * @param string $token Token from store(). */ public static function builder_url( string $token ): string { return add_query_arg( [ 'page' => Page::SLUG, self::QUERY_ARG => $token, ], admin_url( 'admin.php' ) ); } /** * WooCommerce → Orders, in whichever order storage the store uses. */ public static function orders_url(): string { if ( class_exists( OrderUtil::class ) && OrderUtil::custom_orders_table_usage_is_enabled() ) { return admin_url( 'admin.php?page=wc-orders' ); } return admin_url( 'edit.php?post_type=shop_order' ); } /** * The selection for the builder's bootstrap data. * * Null when the page was opened normally. Otherwise the IDs that are * still exportable (orders may have been trashed since), and `expired` * when nothing is left, so the app can say so instead of silently * showing every order. * * @return array{ids: int[], expired: bool}|null */ public static function for_bootstrap(): ?array { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only lookup: the token is random and bound to the current user. if ( ! isset( $_GET[ self::QUERY_ARG ] ) ) { return null; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- As above. $token = sanitize_key( wp_unslash( $_GET[ self::QUERY_ARG ] ) ); $ids = self::load( get_current_user_id(), $token ); $ids = null === $ids ? [] : self::visible( $ids ); return [ 'ids' => $ids, 'expired' => [] === $ids, ]; } /** * Tokens are 24 lowercase letters and digits. * * @param string $token Token. */ private static function valid_token( string $token ): bool { return 1 === preg_match( '/^[a-z0-9]{24}$/', $token ); } /** * Transient name for one user's selection. * * @param int $user_id User. * @param string $token Token. */ private static function key( int $user_id, string $token ): string { return self::PREFIX . $user_id . '_' . $token; } }