is_string( $name ) && $name !== '' && mb_strlen( $name ) <= 80 ) ); } public static function key( string $name ): string { return 'favorite_' . substr( hash( 'sha256', mb_strtolower( $name ) ), 0, 16 ); } public static function read(): array { Store::require_cap( 'manage_woocommerce' ); return array( 'names'=>self::names(), 'limit'=>self::LIMIT, 'choices'=>Shipping::public_carriers(), 'default'=>Shipping::default_carrier() ); } public static function save( array $data ): array { Store::require_cap( 'manage_woocommerce' ); if ( ! isset( $data['names'] ) || ! is_string( $data['names'] ) || strlen( $data['names'] ) > 1200 ) { throw new \InvalidArgumentException( I18n::t( 'Enter carrier names, one per line.' ) ); } $lines = preg_split( '/\r\n|\r|\n/', $data['names'] ); if ( count( $lines ) > self::LIMIT + 8 ) { throw new \InvalidArgumentException( I18n::t( 'Too many carrier names. Keep only your most-used carriers.' ) ); } $names = array(); $seen = array(); foreach ( $lines as $line ) { $name = trim( sanitize_text_field( $line ) ); if ( $name === '' ) { continue; } if ( mb_strlen( $name ) > 80 ) { throw new \InvalidArgumentException( I18n::t( 'A carrier name is too long.' ) ); } $key = mb_strtolower( $name ); if ( isset( $seen[$key] ) ) { continue; } $seen[$key] = true; $names[] = $name; } if ( count( $names ) > self::LIMIT ) { throw new \InvalidArgumentException( I18n::t( 'Too many carrier names. Keep only your most-used carriers.' ) ); } update_option( self::OPTION, $names, false ); return self::read(); } }