Service code => default name. */ public static function all(): array { return array( 'DOM.RP' => __( 'Regular Parcel', 'maple-parcel' ), 'DOM.EP' => __( 'Expedited Parcel', 'maple-parcel' ), 'DOM.XP' => __( 'Xpresspost', 'maple-parcel' ), 'DOM.XP.CERT' => __( 'Xpresspost Certified', 'maple-parcel' ), 'DOM.PC' => __( 'Priority', 'maple-parcel' ), 'DOM.LIB' => __( 'Library Materials', 'maple-parcel' ), 'USA.EP' => __( 'Expedited Parcel USA', 'maple-parcel' ), 'USA.SP.AIR' => __( 'Small Packet USA Air', 'maple-parcel' ), 'USA.TP' => __( 'Tracked Packet USA', 'maple-parcel' ), 'USA.TP.LVM' => __( 'Tracked Packet USA (LVM)', 'maple-parcel' ), 'USA.XP' => __( 'Xpresspost USA', 'maple-parcel' ), 'USA.PW.PARCEL' => __( 'Priority Worldwide USA', 'maple-parcel' ), 'INT.XP' => __( 'Xpresspost International', 'maple-parcel' ), 'INT.IP.AIR' => __( 'International Parcel Air', 'maple-parcel' ), 'INT.IP.SURF' => __( 'International Parcel Surface', 'maple-parcel' ), 'INT.SP.AIR' => __( 'Small Packet International Air', 'maple-parcel' ), 'INT.SP.SURF' => __( 'Small Packet International Surface', 'maple-parcel' ), 'INT.TP' => __( 'Tracked Packet International', 'maple-parcel' ), 'INT.PW.PARCEL' => __( 'Priority Worldwide International', 'maple-parcel' ), ); } public static function group( string $code ): string { $group = strtok( $code, '.' ); return false === $group ? '' : $group; } /** * The zone's saved choices, with every known service present: services * added to the catalogue after the zone was saved take their default. * * @param mixed $saved The zone's `services` setting. * @return array */ public static function choices( mixed $saved ): array { $saved = is_array( $saved ) ? $saved : array(); $choices = array(); foreach ( array_keys( self::all() ) as $code ) { $row = isset( $saved[ $code ] ) && is_array( $saved[ $code ] ) ? $saved[ $code ] : null; $choices[ $code ] = array( 'enabled' => null === $row ? ! in_array( $code, self::OFF_BY_DEFAULT, true ) : ! empty( $row['enabled'] ), 'name' => null !== $row && is_string( $row['name'] ?? null ) ? $row['name'] : '', ); } return $choices; } /** * Ticked services for one destination group, in catalogue order. * * @param array $choices From choices(). * @return list */ public static function enabledFor( string $group, array $choices ): array { $codes = array(); foreach ( $choices as $code => $choice ) { if ( $choice['enabled'] && self::group( $code ) === $group ) { $codes[] = $code; } } return $codes; } /** * What the customer sees: the store's own name for the service, or else * Canada Post's. * * @param array $choices */ public static function label( string $code, string $apiName, array $choices ): string { $custom = trim( $choices[ $code ]['name'] ?? '' ); if ( '' !== $custom ) { return $custom; } return '' !== $apiName ? $apiName : ( self::all()[ $code ] ?? $code ); } }