][], so one form can carry the settings of several channels. * * @package Tidelist */ namespace Tidelist\Admin; use Tidelist\Channel\ChannelInterface; /** * Channel settings fields. */ final class ChannelFields { const FIELD = 'tidelist_channel'; /** * The fields of a channel in input order. * * @param ChannelInterface $channel Channel. * @param string[] $keys Only these keys, in this order (empty = all). * @return array */ public static function fields( ChannelInterface $channel, array $keys = array() ): array { $fields = $channel->settings_schema(); if ( ! $keys ) { return $fields; } $by_key = array_column( $fields, null, 'key' ); $out = array(); foreach ( $keys as $key ) { if ( isset( $by_key[ $key ] ) ) { $out[] = $by_key[ $key ]; } } return $out; } /** * Fields grouped by their heading, in order. * * @param array $fields Fields. * @return array> */ public static function by_group( array $fields ): array { $groups = array(); foreach ( $fields as $field ) { $groups[ (string) ( $field['group'] ?? '' ) ][] = $field; } return $groups; } /** * The submitted values of one channel from $_POST (unslashed, not yet sanitized: the channel does that). * * @param string $channel_id Channel ID. */ public static function posted( string $channel_id ): array { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- nonce checked by the caller; the channel sanitizes every field it accepts. $all = isset( $_POST[ self::FIELD ] ) && is_array( $_POST[ self::FIELD ] ) ? wp_unslash( $_POST[ self::FIELD ] ) : array(); return isset( $all[ $channel_id ] ) && is_array( $all[ $channel_id ] ) ? $all[ $channel_id ] : array(); } /** * Prints table rows (th + td) for the fields. * * @param ChannelInterface $channel Channel. * @param array $fields Fields to draw. * @param string $id_prefix Prefix for element IDs (unique on the page). */ public static function rows( ChannelInterface $channel, array $fields, string $id_prefix ): void { $values = $channel->settings_values(); foreach ( $fields as $field ) { $key = (string) $field['key']; $name = self::FIELD . '[' . $channel->id() . '][' . $key . ']'; $id = $id_prefix . '-' . $key; $type = (string) ( $field['type'] ?? 'text' ); $value = $values[ $key ] ?? ( $field['default'] ?? '' ); $help = (string) ( $field['help'] ?? '' ); ?> ' . esc_html( $help ) . '

'; } if ( 'password' === $type && $value ) { ?>