206 lines
7.6 KiB
PHP
206 lines
7.6 KiB
PHP
<?php
|
||
|
||
namespace Smilia\Core;
|
||
|
||
defined( 'ABSPATH' ) || exit;
|
||
|
||
final class Meta_Fields {
|
||
|
||
public const TONE = 'smilia_tone';
|
||
public const POSITION = 'smilia_position';
|
||
public const TOPIC = 'smilia_topic';
|
||
public const SOURCE = 'smilia_source';
|
||
public const SEO_TITLE = 'smilia_seo_title';
|
||
|
||
public const EDUCATION = 'smilia_education';
|
||
public const CREDENTIALS = 'smilia_credentials';
|
||
public const TOPICS = 'smilia_topics';
|
||
public const ROLE = 'smilia_role';
|
||
|
||
public static function tones(): array {
|
||
return array(
|
||
'teal' => __( 'Бирюзовый', 'smilia-core' ),
|
||
'pink' => __( 'Розовый', 'smilia-core' ),
|
||
'yellow' => __( 'Жёлтый', 'smilia-core' ),
|
||
'lavender' => __( 'Лавандовый', 'smilia-core' ),
|
||
'peach' => __( 'Персиковый', 'smilia-core' ),
|
||
);
|
||
}
|
||
|
||
public static function sources(): array {
|
||
return array(
|
||
'google' => 'Google',
|
||
'booksy' => 'Booksy',
|
||
'znanylekarz' => 'ZnanyLekarz',
|
||
);
|
||
}
|
||
|
||
public function register(): void {
|
||
add_action( 'init', array( $this, 'register_meta' ) );
|
||
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_panels' ) );
|
||
add_filter( 'pll_copy_post_metas', array( $this, 'translation_metas' ) );
|
||
}
|
||
|
||
public function register_meta(): void {
|
||
foreach ( $this->definitions() as $post_type => $panels ) {
|
||
foreach ( $panels as $panel ) {
|
||
foreach ( $panel['fields'] as $field ) {
|
||
register_post_meta(
|
||
$post_type,
|
||
$field['key'],
|
||
array(
|
||
'type' => 'string',
|
||
'single' => true,
|
||
'default' => $field['default'],
|
||
'show_in_rest' => true,
|
||
'sanitize_callback' => $field['sanitize'],
|
||
'auth_callback' => static fn( $allowed, $meta_key, $post_id ): bool => current_user_can( 'edit_post', (int) $post_id ),
|
||
)
|
||
);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public static function roles(): array {
|
||
return array(
|
||
'physician' => __( 'Врач (в разметке Physician)', 'smilia-core' ),
|
||
'person' => __( 'Специалист без врачебной степени (в разметке Person)', 'smilia-core' ),
|
||
);
|
||
}
|
||
|
||
public function sanitize_role( $value ): string {
|
||
$value = sanitize_key( (string) $value );
|
||
return array_key_exists( $value, self::roles() ) ? $value : 'physician';
|
||
}
|
||
|
||
public function sanitize_tone( $value ): string {
|
||
$value = sanitize_key( (string) $value );
|
||
return array_key_exists( $value, self::tones() ) ? $value : 'teal';
|
||
}
|
||
|
||
public function sanitize_source( $value ): string {
|
||
$value = sanitize_key( (string) $value );
|
||
return array_key_exists( $value, self::sources() ) ? $value : '';
|
||
}
|
||
|
||
public function translation_metas( array $keys ): array {
|
||
$local = array( self::POSITION, self::EDUCATION, self::CREDENTIALS, self::TOPICS, self::TOPIC, self::SEO_TITLE );
|
||
return array_values( array_unique( array_merge( array_diff( $keys, $local ), array( self::TONE, self::ROLE ) ) ) );
|
||
}
|
||
|
||
public function enqueue_panels(): void {
|
||
$screen = get_current_screen();
|
||
$panels = $this->panels();
|
||
if ( ! $screen || ! isset( $panels[ $screen->post_type ] ) ) {
|
||
return;
|
||
}
|
||
$asset = SMILIA_CORE_DIR . '/build/blocks/editor-fields/index.asset.php';
|
||
if ( ! is_readable( $asset ) ) {
|
||
return;
|
||
}
|
||
$data = include $asset;
|
||
wp_enqueue_script(
|
||
'smilia-editor-fields',
|
||
plugins_url( 'build/blocks/editor-fields/index.js', SMILIA_CORE_FILE ),
|
||
$data['dependencies'],
|
||
$data['version'],
|
||
true
|
||
);
|
||
wp_add_inline_script( 'smilia-editor-fields', 'window.smiliaMetaPanels = ' . wp_json_encode( $panels ) . ';', 'before' );
|
||
}
|
||
|
||
private function options( array $options ): array {
|
||
$list = array();
|
||
foreach ( $options as $value => $label ) {
|
||
$list[] = array(
|
||
'value' => (string) $value,
|
||
'label' => $label,
|
||
);
|
||
}
|
||
return $list;
|
||
}
|
||
|
||
private function field( string $key, string $control, string $label, callable|string $sanitize, array $extra = array() ): array {
|
||
return array_merge(
|
||
array(
|
||
'key' => $key,
|
||
'control' => $control,
|
||
'label' => $label,
|
||
'sanitize' => $sanitize,
|
||
'default' => '',
|
||
),
|
||
$extra
|
||
);
|
||
}
|
||
|
||
private function seo_panel( string $name ): array {
|
||
return array(
|
||
'name' => $name,
|
||
'title' => __( 'Для поисковиков', 'smilia-core' ),
|
||
'fields' => array(
|
||
$this->field( self::SEO_TITLE, 'text', __( 'Заголовок для поисковиков (до 50 знаков, название сайта добавится само)', 'smilia-core' ), 'sanitize_text_field' ),
|
||
),
|
||
'help' => __( 'Описание для поисковиков (120-160 знаков) вводится в поле «Отрывок».', 'smilia-core' ),
|
||
);
|
||
}
|
||
|
||
private function tone_field( string $label ): array {
|
||
return $this->field( self::TONE, 'select', $label, array( $this, 'sanitize_tone' ), array( 'options' => $this->options( self::tones() ), 'default' => 'teal' ) );
|
||
}
|
||
|
||
private function definitions(): array {
|
||
return array(
|
||
Post_Types::SERVICE => array(
|
||
array(
|
||
'name' => 'smilia-service',
|
||
'title' => __( 'Оформление', 'smilia-core' ),
|
||
'fields' => array( $this->tone_field( __( 'Цвет каймы', 'smilia-core' ) ) ),
|
||
),
|
||
$this->seo_panel( 'smilia-service-seo' ),
|
||
),
|
||
Post_Types::DOCTOR => array(
|
||
array(
|
||
'name' => 'smilia-doctor',
|
||
'title' => __( 'О враче', 'smilia-core' ),
|
||
'fields' => array(
|
||
$this->field( self::POSITION, 'text', __( 'Должность', 'smilia-core' ), 'sanitize_text_field' ),
|
||
$this->tone_field( __( 'Цвет карточки', 'smilia-core' ) ),
|
||
$this->field( self::ROLE, 'select', __( 'Кто это (для микроразметки)', 'smilia-core' ), array( $this, 'sanitize_role' ), array( 'options' => $this->options( self::roles() ), 'default' => 'physician' ) ),
|
||
$this->field( self::EDUCATION, 'textarea', __( 'Образование (каждое с новой строки)', 'smilia-core' ), 'sanitize_textarea_field' ),
|
||
$this->field( self::CREDENTIALS, 'textarea', __( 'Специализации и квалификации (каждая с новой строки)', 'smilia-core' ), 'sanitize_textarea_field' ),
|
||
$this->field( self::TOPICS, 'textarea', __( 'Чем занимается (темы, каждая с новой строки)', 'smilia-core' ), 'sanitize_textarea_field' ),
|
||
),
|
||
'help' => __( 'Эти поля попадают в микроразметку врача для поисковиков. Пишите только то, что указано в тексте страницы врача.', 'smilia-core' ),
|
||
),
|
||
$this->seo_panel( 'smilia-doctor-seo' ),
|
||
),
|
||
Post_Types::REVIEW => array(
|
||
array(
|
||
'name' => 'smilia-review',
|
||
'title' => __( 'Об отзыве', 'smilia-core' ),
|
||
'fields' => array(
|
||
$this->field( self::TOPIC, 'text', __( 'Тема (например, «Первый визит»)', 'smilia-core' ), 'sanitize_text_field' ),
|
||
$this->field( self::SOURCE, 'select', __( 'Где оставлен', 'smilia-core' ), array( $this, 'sanitize_source' ), array( 'options' => $this->options( array( '' => __( 'Не выбрано', 'smilia-core' ) ) + self::sources() ) ) ),
|
||
),
|
||
),
|
||
),
|
||
'page' => array(
|
||
$this->seo_panel( 'smilia-page-seo' ),
|
||
),
|
||
);
|
||
}
|
||
|
||
private function panels(): array {
|
||
$panels = $this->definitions();
|
||
foreach ( $panels as $post_type => $list ) {
|
||
foreach ( $list as $i => $panel ) {
|
||
foreach ( $panel['fields'] as $j => $field ) {
|
||
unset( $panels[ $post_type ][ $i ]['fields'][ $j ]['sanitize'] );
|
||
}
|
||
}
|
||
}
|
||
return $panels;
|
||
}
|
||
}
|