> */ public static function toggleSchema(): array { return array( self::SHOW_LICENSE_LENGTH => array( 'type' => 'boolean', 'default' => true, 'control' => 'toggle', 'label' => __( 'Show license length', 'releasecart' ), 'description' => __( "Show each plan's license duration.", 'releasecart' ), ), self::SHOW_ACTIVATION_LIMIT => array( 'type' => 'boolean', 'default' => true, 'control' => 'toggle', 'label' => __( 'Show activation limit', 'releasecart' ), 'description' => __( 'Show how many sites each plan may activate.', 'releasecart' ), ), ); } /** * Read tier metadata flags from component attributes. * * @since 1.0.0 * * @param array $attrs Resolved component attributes. * @return array */ public static function flagsFrom( array $attrs ): array { return array( self::SHOW_LICENSE_LENGTH => (bool) ( $attrs[ self::SHOW_LICENSE_LENGTH ] ?? true ), self::SHOW_ACTIVATION_LIMIT => (bool) ( $attrs[ self::SHOW_ACTIVATION_LIMIT ] ?? true ), ); } /** * Build metadata lines for one tier. * * @since 1.0.0 * * @param ProductPrice $price The tier to describe. * @param array $flags The display flags keyed by attribute name. * @param bool $licensed Whether the product issues licenses. The license * length and activation lines show only when it does. * @return array */ public static function lines( ProductPrice $price, array $flags, bool $licensed = true ): array { $lines = array(); if ( ! $licensed ) { $flags = array_merge( $flags, array( self::SHOW_LICENSE_LENGTH => false, self::SHOW_ACTIVATION_LIMIT => false, ) ); } if ( ! empty( $flags[ self::SHOW_LICENSE_LENGTH ] ) ) { $license = self::licenseLine( $price ); if ( null !== $license ) { $lines[] = $license; } } if ( ! empty( $flags[ self::SHOW_ACTIVATION_LIMIT ] ) ) { $lines[] = self::activationLine( $price ); } /** * Filters the plain-text metadata lines for a price tier. * * The license line comes first when its flag is on and the length reads * as a duration or lifetime, then the activation line. * * @since 1.0.0 * * @param array $lines The meta lines, in display order. * @param ProductPrice $price The tier being described. * @param array $flags The resolved display flags. */ $lines = apply_filters( 'releasecart_tier_meta_lines', $lines, $price, $flags ); $clean = array(); foreach ( (array) $lines as $line ) { if ( is_string( $line ) && '' !== $line ) { $clean[] = $line; } } return $clean; } /** * Build the license-duration line. * * @param ProductPrice $price The tier. * @return string|null */ private static function licenseLine( ProductPrice $price ): ?string { $raw = $price->licenseLength(); if ( null === $raw ) { return __( 'Lifetime license', 'releasecart' ); } // Accept stored values with surrounding whitespace. if ( 1 !== preg_match( '/^\+?(\d+) (day|week|month|year)s?$/i', trim( $raw ), $matches ) ) { return null; } $count = (int) $matches[1]; if ( $count <= 0 ) { return null; } switch ( strtolower( $matches[2] ) ) { case 'day': /* translators: %d: number of days. The plural form exists for locales that inflect the compound; English keeps the unit singular. */ return sprintf( _n( '%d-day license', '%d-day license', $count, 'releasecart' ), $count ); case 'week': /* translators: %d: number of weeks. The plural form exists for locales that inflect the compound; English keeps the unit singular. */ return sprintf( _n( '%d-week license', '%d-week license', $count, 'releasecart' ), $count ); case 'month': /* translators: %d: number of months. The plural form exists for locales that inflect the compound; English keeps the unit singular. */ return sprintf( _n( '%d-month license', '%d-month license', $count, 'releasecart' ), $count ); case 'year': default: /* translators: %d: number of years. The plural form exists for locales that inflect the compound; English keeps the unit singular. */ return sprintf( _n( '%d-year license', '%d-year license', $count, 'releasecart' ), $count ); } } /** * Build the activation-limit line. * * @param ProductPrice $price The tier. * @return string */ private static function activationLine( ProductPrice $price ): string { $limit = $price->activationLimit(); if ( null === $limit ) { return __( 'Unlimited activations', 'releasecart' ); } /* translators: %d: number of site activations the license allows. */ return sprintf( _n( '%d activation', '%d activations', $limit, 'releasecart' ), $limit ); } }