Files

179 lines
7.4 KiB
PHP

<?php
namespace Smilia\Core;
defined( 'ABSPATH' ) || exit;
final class Settings {
public const OPTION = 'smilia_clinic';
private const PAGE = 'smilia-clinic';
public static function fields(): array {
return array(
'phone_main' => array( __( 'Телефон регистратуры', 'smilia-core' ), 'text' ),
'phone_new' => array( __( 'Телефон для новых пациентов', 'smilia-core' ), 'text' ),
'phone_current' => array( __( 'Телефон для действующих пациентов', 'smilia-core' ), 'text' ),
'whatsapp' => array( __( 'WhatsApp', 'smilia-core' ), 'text' ),
'email' => array( __( 'Email', 'smilia-core' ), 'email' ),
'address' => array( __( 'Адрес', 'smilia-core' ), 'text' ),
'geo' => array( __( 'Координаты клиники (широта, долгота)', 'smilia-core' ), 'text' ),
'instagram' => array( __( 'Instagram', 'smilia-core' ), 'url' ),
'facebook' => array( __( 'Facebook', 'smilia-core' ), 'url' ),
'review_google' => array( __( 'Google: страница для отзыва', 'smilia-core' ), 'url' ),
'review_booksy' => array( __( 'Booksy: страница для отзыва', 'smilia-core' ), 'url' ),
'review_znanylekarz' => array( __( 'ZnanyLekarz: страница для отзыва', 'smilia-core' ), 'url' ),
'hours' => array( __( 'Часы работы (семь строк «День|Время» с понедельника, один промежуток на день)', 'smilia-core' ), 'textarea' ),
'booking_title' => array( __( 'Заголовок', 'smilia-core' ), 'text', __( 'Запись на детский приём', 'smilia-core' ), 'smilia_booking' ),
'booking_lead' => array( __( 'Текст под заголовком', 'smilia-core' ), 'textarea', __( 'Администратор перезвонит и поможет подобрать удобное время.', 'smilia-core' ), 'smilia_booking' ),
'booking_submit' => array( __( 'Кнопка отправки', 'smilia-core' ), 'text', __( 'Отправить заявку', 'smilia-core' ), 'smilia_booking' ),
'booking_done' => array( __( 'Заголовок после отправки', 'smilia-core' ), 'text', __( 'Заявка отправлена', 'smilia-core' ), 'smilia_booking' ),
'booking_thanks' => array( __( 'Текст после отправки', 'smilia-core' ), 'textarea', __( 'Спасибо! Администратор перезвонит в рабочее время.', 'smilia-core' ), 'smilia_booking' ),
'booking_email' => array( __( 'Почта для заявок (пусто: почта администратора сайта)', 'smilia-core' ), 'email', '', 'smilia_booking' ),
);
}
public static function sections(): array {
return array(
'smilia_main' => '',
'smilia_booking' => __( 'Окно записи', 'smilia-core' ),
);
}
public static function get( string $key ): string {
$value = self::raw( $key );
if ( '' !== $value ) {
return I18n::translate( $key, $value );
}
$field = self::fields()[ $key ] ?? array();
return isset( $field[2] ) ? (string) $field[2] : '';
}
public static function profiles(): array {
return array_filter(
array(
'instagram' => self::get( 'instagram' ),
'facebook' => self::get( 'facebook' ),
),
static fn( string $link ): bool => '' !== trim( (string) wp_parse_url( $link, PHP_URL_PATH ), '/' )
);
}
public static function raw( string $key ): string {
$options = get_option( self::OPTION, array() );
return is_array( $options ) && isset( $options[ $key ] ) ? trim( (string) $options[ $key ] ) : '';
}
public static function hours(): array {
$rows = array();
foreach ( preg_split( '/\R/u', self::get( 'hours' ) ) as $line ) {
$parts = array_map( 'trim', explode( '|', $line, 2 ) );
if ( 2 === count( $parts ) && '' !== $parts[0] ) {
$rows[] = array(
'day' => $parts[0],
'time' => $parts[1],
);
}
}
return $rows;
}
public static function schedule(): array {
$result = array();
foreach ( self::hours() as $row ) {
$open = preg_match( '/(\d{1,2}):(\d{2})\D+(\d{1,2}):(\d{2})/u', $row['time'], $match );
$result[] = array(
'day' => $row['day'],
'open' => $open ? (int) $match[1] * 60 + (int) $match[2] : null,
'close' => $open ? (int) $match[3] * 60 + (int) $match[4] : null,
);
}
return $result;
}
public static function tel( string $phone ): string {
$plus = str_starts_with( trim( $phone ), '+' ) ? '+' : '';
return 'tel:' . $plus . preg_replace( '/\D/', '', $phone );
}
public function register(): void {
add_action( 'admin_init', array( $this, 'register_setting' ) );
add_action( 'admin_menu', array( $this, 'add_page' ) );
}
public function register_setting(): void {
register_setting(
self::PAGE,
self::OPTION,
array(
'type' => 'object',
'sanitize_callback' => array( $this, 'sanitize' ),
'default' => array(),
)
);
foreach ( self::sections() as $section => $title ) {
add_settings_section( $section, $title, '__return_false', self::PAGE );
}
foreach ( self::fields() as $key => $field ) {
add_settings_field(
$key,
$field[0],
array( $this, 'render_field' ),
self::PAGE,
$field[3] ?? 'smilia_main',
array(
'key' => $key,
'type' => $field[1],
'label_for' => self::OPTION . '_' . $key,
)
);
}
}
public function sanitize( $input ): array {
$clean = array();
if ( ! is_array( $input ) ) {
return $clean;
}
foreach ( self::fields() as $key => $field ) {
$value = isset( $input[ $key ] ) ? (string) $input[ $key ] : '';
$clean[ $key ] = match ( $field[1] ) {
'email' => sanitize_email( $value ),
'url' => esc_url_raw( $value ),
'number' => (string) absint( $value ),
'textarea' => sanitize_textarea_field( $value ),
default => sanitize_text_field( $value ),
};
}
return $clean;
}
public function add_page(): void {
add_options_page( __( 'Клиника', 'smilia-core' ), __( 'Клиника', 'smilia-core' ), 'manage_options', self::PAGE, array( $this, 'render_page' ) );
}
public function render_field( array $args ): void {
$name = self::OPTION . '[' . $args['key'] . ']';
$id = $args['label_for'];
$options = get_option( self::OPTION, array() );
$value = is_array( $options ) && isset( $options[ $args['key'] ] ) ? (string) $options[ $args['key'] ] : '';
$default = (string) ( self::fields()[ $args['key'] ][2] ?? '' );
if ( 'textarea' === $args['type'] ) {
printf( '<textarea class="large-text" rows="%d" id="%s" name="%s" placeholder="%s">%s</textarea>', 'hours' === $args['key'] ? 7 : 3, esc_attr( $id ), esc_attr( $name ), esc_attr( $default ), esc_textarea( $value ) );
return;
}
printf( '<input type="%s" class="regular-text" id="%s" name="%s" value="%s" placeholder="%s">', esc_attr( 'number' === $args['type'] ? 'number' : 'text' ), esc_attr( $id ), esc_attr( $name ), esc_attr( $value ), esc_attr( $default ) );
}
public function render_page(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
echo '<div class="wrap"><h1>' . esc_html__( 'Клиника', 'smilia-core' ) . '</h1><form method="post" action="options.php">';
settings_fields( self::PAGE );
do_settings_sections( self::PAGE );
submit_button();
echo '</form></div>';
}
}