Smilia: children's dental clinic website (WordPress theme and plugin)
This commit is contained in:
commit
4f4b5f2c0a
266 files changed
+16459
No files matched your search
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
use WP_Block_Editor_Context;
|
||||
use WP_Post;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Blocks {
|
||||
|
||||
private const DIR = SMILIA_CORE_DIR . '/build/blocks';
|
||||
|
||||
public function register(): void {
|
||||
add_action( 'init', array( $this, 'register_blocks' ) );
|
||||
add_filter( 'block_type_metadata', array( $this, 'asset_version' ) );
|
||||
add_filter( 'block_categories_all', array( $this, 'add_category' ) );
|
||||
add_filter( 'block_editor_settings_all', array( $this, 'editor_settings' ), 10, 2 );
|
||||
add_filter( 'should_load_remote_block_patterns', '__return_false' );
|
||||
add_filter( 'wp_content_img_tag', array( $this, 'keep_wide_sizes' ), 20 );
|
||||
add_action( 'enqueue_block_editor_assets', array( $this, 'editor_data' ) );
|
||||
remove_action( 'enqueue_block_editor_assets', 'wp_enqueue_editor_block_directory_assets' );
|
||||
}
|
||||
|
||||
public function register_blocks(): void {
|
||||
if ( is_readable( self::DIR . '/blocks-manifest.php' ) ) {
|
||||
wp_register_block_types_from_metadata_collection( self::DIR, self::DIR . '/blocks-manifest.php' );
|
||||
}
|
||||
foreach ( \WP_Block_Type_Registry::get_instance()->get_all_registered() as $name => $type ) {
|
||||
if ( ! str_starts_with( $name, 'smilia/' ) ) {
|
||||
continue;
|
||||
}
|
||||
foreach ( (array) $type->editor_script_handles as $handle ) {
|
||||
wp_set_script_translations( $handle, 'smilia-core', SMILIA_CORE_DIR . '/languages' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function keep_wide_sizes( string $image ): string {
|
||||
if ( false === strpos( $image, 'service-page__method-photo' ) ) {
|
||||
return $image;
|
||||
}
|
||||
$processor = new \WP_HTML_Tag_Processor( $image );
|
||||
if ( $processor->next_tag( 'img' ) ) {
|
||||
$sizes = (string) $processor->get_attribute( 'sizes' );
|
||||
if ( str_starts_with( $sizes, 'auto,' ) ) {
|
||||
$processor->set_attribute( 'sizes', trim( substr( $sizes, 5 ) ) );
|
||||
}
|
||||
}
|
||||
return $processor->get_updated_html();
|
||||
}
|
||||
|
||||
public function asset_version( array $metadata ): array {
|
||||
if ( empty( $metadata['file'] ) || ! str_starts_with( wp_normalize_path( $metadata['file'] ), wp_normalize_path( self::DIR ) ) ) {
|
||||
return $metadata;
|
||||
}
|
||||
$asset = dirname( $metadata['file'] ) . '/index.asset.php';
|
||||
if ( is_readable( $asset ) ) {
|
||||
$data = include $asset;
|
||||
if ( is_array( $data ) && ! empty( $data['version'] ) ) {
|
||||
$metadata['version'] = (string) $data['version'];
|
||||
}
|
||||
}
|
||||
return $metadata;
|
||||
}
|
||||
|
||||
public function editor_data(): void {
|
||||
wp_add_inline_script(
|
||||
'wp-blocks',
|
||||
'window.smiliaBlocks = ' . wp_json_encode(
|
||||
array(
|
||||
'images' => plugins_url( 'assets/img/', SMILIA_CORE_FILE ),
|
||||
'admin' => admin_url(),
|
||||
)
|
||||
) . ';'
|
||||
);
|
||||
}
|
||||
|
||||
public function add_category( array $categories ): array {
|
||||
array_unshift(
|
||||
$categories,
|
||||
array(
|
||||
'slug' => 'smilia',
|
||||
'title' => __( 'Smilia', 'smilia-core' ),
|
||||
'icon' => null,
|
||||
)
|
||||
);
|
||||
return $categories;
|
||||
}
|
||||
|
||||
public function editor_settings( array $settings, WP_Block_Editor_Context $context ): array {
|
||||
$settings['enableOpenverseMediaCategory'] = false;
|
||||
if ( $context->post instanceof WP_Post && 'page' === $context->post->post_type ) {
|
||||
$settings['templateLock'] = current_user_can( 'manage_options' ) ? 'all' : 'contentOnly';
|
||||
}
|
||||
return $settings;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,352 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
use WP_Post;
|
||||
use WP_REST_Request;
|
||||
use WP_REST_Response;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Booking {
|
||||
|
||||
public const ROUTE_NAMESPACE = 'smilia/v1';
|
||||
public const ROUTE = '/booking';
|
||||
public const NONCE_ACTION = 'smilia_booking';
|
||||
public const CONSENT_VERSION = '2026-10-22';
|
||||
|
||||
private const CLEANUP_HOOK = 'smilia_booking_cleanup';
|
||||
private const RETENTION_DAYS = 365;
|
||||
private const MIN_SECONDS = 3;
|
||||
private const PHONE_CODE = '48';
|
||||
private const PHONE_DIGITS = 9;
|
||||
private const MAX_AGE = 2 * DAY_IN_SECONDS;
|
||||
|
||||
public static function concerns(): array {
|
||||
return array(
|
||||
'first-visit' => __( 'Первый визит, знакомство', 'smilia-core' ),
|
||||
'checkup' => __( 'Осмотр и гигиена', 'smilia-core' ),
|
||||
'pain' => __( 'Болит или сколот зуб', 'smilia-core' ),
|
||||
'caries' => __( 'Лечение кариеса', 'smilia-core' ),
|
||||
'bite' => __( 'Прикус, пластинка, брекеты', 'smilia-core' ),
|
||||
'other' => __( 'Другое', 'smilia-core' ),
|
||||
);
|
||||
}
|
||||
|
||||
public static function form_data(): array {
|
||||
$started = (string) time();
|
||||
return array(
|
||||
'endpoint' => function_exists( 'pll_current_language' ) && pll_current_language()
|
||||
? add_query_arg( 'lang', pll_current_language(), rest_url( self::ROUTE_NAMESPACE . self::ROUTE ) )
|
||||
: rest_url( self::ROUTE_NAMESPACE . self::ROUTE ),
|
||||
'nonce' => wp_create_nonce( self::NONCE_ACTION ),
|
||||
'rest' => wp_create_nonce( 'wp_rest' ),
|
||||
'started' => $started . '.' . self::sign( $started ),
|
||||
'captcha' => Captcha::endpoint(),
|
||||
'script' => Captcha::script(),
|
||||
'concerns' => self::concerns(),
|
||||
'phone' => array(
|
||||
'code' => self::PHONE_CODE,
|
||||
'digits' => self::PHONE_DIGITS,
|
||||
'example' => '+48 512 345 678',
|
||||
),
|
||||
'consent' => Markup::url( '/consent/' ),
|
||||
'privacy' => Markup::url( '/privacy-policy/' ),
|
||||
);
|
||||
}
|
||||
|
||||
public function register(): void {
|
||||
add_action( 'rest_api_init', array( $this, 'register_route' ) );
|
||||
add_action( self::CLEANUP_HOOK, array( $this, 'cleanup' ) );
|
||||
add_action( 'add_meta_boxes_' . Post_Types::BOOKING, array( $this, 'add_box' ) );
|
||||
add_filter( 'manage_' . Post_Types::BOOKING . '_posts_columns', array( $this, 'columns' ) );
|
||||
add_action( 'manage_' . Post_Types::BOOKING . '_posts_custom_column', array( $this, 'column' ), 10, 2 );
|
||||
}
|
||||
|
||||
private static function route_args(): array {
|
||||
$limits = array(
|
||||
'smilia_nonce' => 40,
|
||||
'started' => 100,
|
||||
'website' => 200,
|
||||
'name' => 120,
|
||||
'phone' => 40,
|
||||
'age' => 20,
|
||||
'concern' => 40,
|
||||
'page' => 2048,
|
||||
'consent' => 2,
|
||||
'altcha' => 4000,
|
||||
);
|
||||
$args = array();
|
||||
foreach ( $limits as $name => $length ) {
|
||||
$args[ $name ] = array(
|
||||
'type' => 'string',
|
||||
'maxLength' => $length,
|
||||
'default' => '',
|
||||
);
|
||||
}
|
||||
return $args;
|
||||
}
|
||||
|
||||
public function register_route(): void {
|
||||
register_rest_route(
|
||||
self::ROUTE_NAMESPACE,
|
||||
self::ROUTE,
|
||||
array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array( $this, 'handle' ),
|
||||
'permission_callback' => '__return_true',
|
||||
'args' => self::route_args(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function handle( WP_REST_Request $request ): WP_REST_Response {
|
||||
if ( ! $this->same_origin( $request ) ) {
|
||||
return $this->fail( 403, '', __( 'Заявку можно отправить только с сайта клиники.', 'smilia-core' ) );
|
||||
}
|
||||
|
||||
if ( ! wp_verify_nonce( (string) $request->get_param( 'smilia_nonce' ), self::NONCE_ACTION ) ) {
|
||||
return $this->fail( 403, '', __( 'Страница устарела. Обновите её и отправьте заявку ещё раз.', 'smilia-core' ) );
|
||||
}
|
||||
|
||||
$started = $this->started_at( (string) $request->get_param( 'started' ) );
|
||||
if ( null === $started || time() - $started > self::MAX_AGE ) {
|
||||
return $this->fail( 400, '', __( 'Страница устарела. Обновите её и отправьте заявку ещё раз.', 'smilia-core' ) );
|
||||
}
|
||||
|
||||
if ( '' !== trim( (string) $request->get_param( 'website' ) ) || time() - $started < self::MIN_SECONDS ) {
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
$name = sanitize_text_field( (string) $request->get_param( 'name' ) );
|
||||
$phone = sanitize_text_field( (string) $request->get_param( 'phone' ) );
|
||||
$age = sanitize_text_field( (string) $request->get_param( 'age' ) );
|
||||
$concern = sanitize_key( (string) $request->get_param( 'concern' ) );
|
||||
$page = esc_url_raw( (string) $request->get_param( 'page' ) );
|
||||
|
||||
if ( mb_strlen( $name ) < 2 || mb_strlen( $name ) > 80 ) {
|
||||
return $this->fail( 422, 'name', __( 'Проверьте имя.', 'smilia-core' ) );
|
||||
}
|
||||
$phone = self::normalize_phone( $phone );
|
||||
if ( '' === $phone ) {
|
||||
return $this->fail( 422, 'phone', __( 'Проверьте номер телефона.', 'smilia-core' ) );
|
||||
}
|
||||
if ( mb_strlen( $age ) > 20 ) {
|
||||
return $this->fail( 422, 'age', __( 'Проверьте возраст ребёнка.', 'smilia-core' ) );
|
||||
}
|
||||
if ( ! array_key_exists( $concern, self::concerns() ) ) {
|
||||
return $this->fail( 422, 'concern', __( 'Выберите, что беспокоит.', 'smilia-core' ) );
|
||||
}
|
||||
if ( '1' !== (string) $request->get_param( 'consent' ) ) {
|
||||
return $this->fail( 422, 'consent', __( 'Нужно ваше согласие на обработку персональных данных.', 'smilia-core' ) );
|
||||
}
|
||||
if ( ! Captcha::verify( (string) $request->get_param( 'altcha' ) ) ) {
|
||||
return $this->fail( 422, '', __( 'Не удалось подтвердить отправку. Обновите страницу и отправьте заявку ещё раз.', 'smilia-core' ) );
|
||||
}
|
||||
|
||||
$time = current_time( 'mysql' );
|
||||
$post_id = wp_insert_post(
|
||||
array(
|
||||
'post_type' => Post_Types::BOOKING,
|
||||
'post_status' => 'private',
|
||||
'post_title' => $name . ', ' . $phone,
|
||||
'meta_input' => array(
|
||||
'smilia_phone' => $phone,
|
||||
'smilia_age' => $age,
|
||||
'smilia_concern' => $concern,
|
||||
'smilia_page' => $page,
|
||||
'smilia_consent' => 'yes',
|
||||
'smilia_consent_version' => self::CONSENT_VERSION,
|
||||
'smilia_consent_url' => Markup::url( '/consent/' ),
|
||||
'smilia_consent_time' => $time,
|
||||
),
|
||||
),
|
||||
true
|
||||
);
|
||||
if ( is_wp_error( $post_id ) ) {
|
||||
return $this->fail( 500, '', __( 'Не получилось отправить заявку. Позвоните нам, пожалуйста.', 'smilia-core' ) );
|
||||
}
|
||||
|
||||
$this->log_consent( $time, $phone, $page );
|
||||
$this->notify( $name, $phone, $age, $concern, $page, $time );
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
private function same_origin( WP_REST_Request $request ): bool {
|
||||
$home = wp_parse_url( home_url(), PHP_URL_HOST );
|
||||
$source = (string) ( $request->get_header( 'origin' ) ?: $request->get_header( 'referer' ) );
|
||||
return '' !== $source && wp_parse_url( $source, PHP_URL_HOST ) === $home;
|
||||
}
|
||||
|
||||
private static function sign( string $value ): string {
|
||||
return substr( hash_hmac( 'sha256', $value, wp_salt( 'nonce' ) ), 0, 20 );
|
||||
}
|
||||
|
||||
private function started_at( string $token ): ?int {
|
||||
$parts = explode( '.', $token, 2 );
|
||||
if ( 2 !== count( $parts ) || ! ctype_digit( $parts[0] ) || ! hash_equals( self::sign( $parts[0] ), $parts[1] ) ) {
|
||||
return null;
|
||||
}
|
||||
return (int) $parts[0];
|
||||
}
|
||||
|
||||
private static function normalize_phone( string $phone ): string {
|
||||
$digits = (string) preg_replace( '/\D/', '', $phone );
|
||||
if ( str_starts_with( $digits, '00' . self::PHONE_CODE ) ) {
|
||||
$digits = substr( $digits, 2 );
|
||||
}
|
||||
if ( self::PHONE_DIGITS === strlen( $digits ) ) {
|
||||
$digits = self::PHONE_CODE . $digits;
|
||||
}
|
||||
return preg_match( '/^' . self::PHONE_CODE . '\d{' . self::PHONE_DIGITS . '}$/', $digits ) ? '+' . $digits : '';
|
||||
}
|
||||
|
||||
private function success(): WP_REST_Response {
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'ok' => true,
|
||||
'message' => Settings::get( 'booking_thanks' ),
|
||||
),
|
||||
200
|
||||
);
|
||||
}
|
||||
|
||||
private function fail( int $status, string $field, string $message ): WP_REST_Response {
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'ok' => false,
|
||||
'field' => $field,
|
||||
'message' => $message,
|
||||
),
|
||||
$status
|
||||
);
|
||||
}
|
||||
|
||||
private static function consent_dir(): string {
|
||||
return defined( 'SMILIA_CONSENT_DIR' ) ? (string) SMILIA_CONSENT_DIR : '';
|
||||
}
|
||||
|
||||
private function log_consent( string $time, string $phone, string $page ): void {
|
||||
$dir = self::consent_dir();
|
||||
if ( '' === $dir || ! is_dir( $dir ) || ! wp_is_writable( $dir ) ) {
|
||||
return;
|
||||
}
|
||||
$line = wp_json_encode(
|
||||
array(
|
||||
'time' => $time,
|
||||
'consent' => true,
|
||||
'consent_version' => self::CONSENT_VERSION,
|
||||
'consent_url' => Markup::url( '/consent/' ),
|
||||
'page' => $page,
|
||||
'phone' => $phone,
|
||||
),
|
||||
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
|
||||
);
|
||||
file_put_contents( trailingslashit( $dir ) . 'consents-' . gmdate( 'Y-m' ) . '.jsonl', $line . "\n", FILE_APPEND | LOCK_EX );
|
||||
}
|
||||
|
||||
private function notify( string $name, string $phone, string $age, string $concern, string $page, string $time ): void {
|
||||
$lines = array(
|
||||
__( 'Новая заявка на приём', 'smilia-core' ),
|
||||
__( 'Имя:', 'smilia-core' ) . ' ' . $name,
|
||||
__( 'Телефон:', 'smilia-core' ) . ' ' . $phone,
|
||||
__( 'Возраст ребёнка:', 'smilia-core' ) . ' ' . ( '' !== $age ? $age : '-' ),
|
||||
__( 'Что беспокоит:', 'smilia-core' ) . ' ' . self::concerns()[ $concern ],
|
||||
__( 'Страница:', 'smilia-core' ) . ' ' . $page,
|
||||
sprintf(
|
||||
__( 'Согласие на обработку персональных данных: да, %1$s, текст от %2$s, %3$s', 'smilia-core' ),
|
||||
$time,
|
||||
self::CONSENT_VERSION,
|
||||
Markup::url( '/consent/' )
|
||||
),
|
||||
);
|
||||
$email = Settings::get( 'booking_email' );
|
||||
wp_mail( is_email( $email ) ? $email : (string) get_option( 'admin_email' ),__( 'Smilia: новая заявка', 'smilia-core' ), implode( "\n", $lines ) );
|
||||
}
|
||||
|
||||
public function add_box(): void {
|
||||
add_meta_box( 'smilia_booking_box', __( 'Заявка', 'smilia-core' ), array( $this, 'render_box' ), Post_Types::BOOKING, 'normal' );
|
||||
remove_meta_box( 'submitdiv', Post_Types::BOOKING, 'side' );
|
||||
}
|
||||
|
||||
public function render_box( WP_Post $post ): void {
|
||||
$rows = array(
|
||||
__( 'Телефон', 'smilia-core' ) => get_post_meta( $post->ID, 'smilia_phone', true ),
|
||||
__( 'Возраст ребёнка', 'smilia-core' ) => get_post_meta( $post->ID, 'smilia_age', true ),
|
||||
__( 'Что беспокоит', 'smilia-core' ) => self::concerns()[ get_post_meta( $post->ID, 'smilia_concern', true ) ] ?? '',
|
||||
__( 'Страница', 'smilia-core' ) => get_post_meta( $post->ID, 'smilia_page', true ),
|
||||
__( 'Согласие', 'smilia-core' ) => sprintf( '%s, %s, %s', get_post_meta( $post->ID, 'smilia_consent_time', true ), get_post_meta( $post->ID, 'smilia_consent_version', true ), get_post_meta( $post->ID, 'smilia_consent_url', true ) ),
|
||||
);
|
||||
echo '<table class="widefat striped"><tbody>';
|
||||
foreach ( $rows as $label => $value ) {
|
||||
printf( '<tr><th>%s</th><td>%s</td></tr>', esc_html( $label ), esc_html( (string) $value ) );
|
||||
}
|
||||
echo '</tbody></table>';
|
||||
}
|
||||
|
||||
public function columns( array $columns ): array {
|
||||
return array(
|
||||
'cb' => $columns['cb'] ?? '',
|
||||
'title' => __( 'Имя и телефон', 'smilia-core' ),
|
||||
'smilia_concern' => __( 'Что беспокоит', 'smilia-core' ),
|
||||
'date' => $columns['date'] ?? __( 'Дата', 'smilia-core' ),
|
||||
);
|
||||
}
|
||||
|
||||
public function column( string $column, int $post_id ): void {
|
||||
if ( 'smilia_concern' === $column ) {
|
||||
echo esc_html( self::concerns()[ get_post_meta( $post_id, 'smilia_concern', true ) ] ?? '' );
|
||||
}
|
||||
}
|
||||
|
||||
public static function schedule_cleanup(): void {
|
||||
if ( ! wp_next_scheduled( self::CLEANUP_HOOK ) ) {
|
||||
wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::CLEANUP_HOOK );
|
||||
}
|
||||
}
|
||||
|
||||
public static function unschedule_cleanup(): void {
|
||||
wp_clear_scheduled_hook( self::CLEANUP_HOOK );
|
||||
}
|
||||
|
||||
public function cleanup(): void {
|
||||
$old = get_posts(
|
||||
array(
|
||||
'post_type' => Post_Types::BOOKING,
|
||||
'post_status' => 'any',
|
||||
'posts_per_page' => 200,
|
||||
'fields' => 'ids',
|
||||
'date_query' => array(
|
||||
array(
|
||||
'before' => self::RETENTION_DAYS . ' days ago',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
foreach ( $old as $post_id ) {
|
||||
wp_delete_post( (int) $post_id, true );
|
||||
}
|
||||
Captcha::cleanup();
|
||||
|
||||
$dir = self::consent_dir();
|
||||
if ( '' === $dir || ! is_dir( $dir ) ) {
|
||||
return;
|
||||
}
|
||||
$cutoff = wp_date( 'Y-m-d H:i:s', time() - self::RETENTION_DAYS * DAY_IN_SECONDS );
|
||||
foreach ( glob( trailingslashit( $dir ) . 'consents-*.jsonl' ) ?: array() as $file ) {
|
||||
$kept = array();
|
||||
foreach ( file( $file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) ?: array() as $line ) {
|
||||
$row = json_decode( $line, true );
|
||||
if ( is_array( $row ) && isset( $row['time'] ) && (string) $row['time'] >= $cutoff ) {
|
||||
$kept[] = $line;
|
||||
}
|
||||
}
|
||||
if ( ! $kept ) {
|
||||
wp_delete_file( $file );
|
||||
} elseif ( count( $kept ) !== count( file( $file ) ) ) {
|
||||
file_put_contents( $file, implode( "\n", $kept ) . "\n", LOCK_EX );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
use AltchaOrg\Altcha\Algorithm\Pbkdf2;
|
||||
use AltchaOrg\Altcha\Altcha;
|
||||
use AltchaOrg\Altcha\CreateChallengeOptions;
|
||||
use AltchaOrg\Altcha\Payload;
|
||||
use AltchaOrg\Altcha\VerifySolutionOptions;
|
||||
use WP_REST_Response;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Captcha {
|
||||
|
||||
public const ROUTE = '/booking-challenge';
|
||||
|
||||
private const COST = 2000;
|
||||
private const COUNTER_MIN = 1000;
|
||||
private const COUNTER_MAX = 2000;
|
||||
private const TTL = 1200;
|
||||
private const MAX_PAYLOAD = 4000;
|
||||
private const USED_PREFIX = 'smilia_used_';
|
||||
private const USED_KEEP = HOUR_IN_SECONDS;
|
||||
|
||||
public function register(): void {
|
||||
add_action( 'rest_api_init', array( $this, 'register_route' ) );
|
||||
}
|
||||
|
||||
public function register_route(): void {
|
||||
register_rest_route(
|
||||
Booking::ROUTE_NAMESPACE,
|
||||
self::ROUTE,
|
||||
array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array( $this, 'handle' ),
|
||||
'permission_callback' => '__return_true',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function handle(): WP_REST_Response {
|
||||
$response = new WP_REST_Response( self::challenge(), 200 );
|
||||
$response->header( 'Cache-Control', 'no-store' );
|
||||
return $response;
|
||||
}
|
||||
|
||||
public static function endpoint(): string {
|
||||
return rest_url( Booking::ROUTE_NAMESPACE . self::ROUTE );
|
||||
}
|
||||
|
||||
public static function script(): string {
|
||||
$path = 'build/blocks/altcha/altcha.min.js';
|
||||
return plugins_url( $path, SMILIA_CORE_FILE ) . '?ver=' . (int) filemtime( SMILIA_CORE_DIR . '/' . $path );
|
||||
}
|
||||
|
||||
public static function challenge(): array {
|
||||
return ( new Altcha( hmacSignatureSecret: self::secret() ) )->createChallenge(
|
||||
new CreateChallengeOptions(
|
||||
algorithm: new Pbkdf2(),
|
||||
cost: self::COST,
|
||||
counter: random_int( self::COUNTER_MIN, self::COUNTER_MAX ),
|
||||
expiresAt: time() + self::TTL,
|
||||
)
|
||||
)->toArray();
|
||||
}
|
||||
|
||||
public static function verify( string $raw ): bool {
|
||||
if ( '' === $raw || strlen( $raw ) > self::MAX_PAYLOAD ) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$payload = Payload::fromBase64( $raw );
|
||||
$result = ( new Altcha( hmacSignatureSecret: self::secret() ) )->verifySolution(
|
||||
new VerifySolutionOptions(
|
||||
algorithm: new Pbkdf2(),
|
||||
payload: $payload,
|
||||
)
|
||||
);
|
||||
} catch ( \Throwable $error ) {
|
||||
return false;
|
||||
}
|
||||
if ( ! $result->verified || null === $payload->challenge->signature ) {
|
||||
return false;
|
||||
}
|
||||
return add_option( self::USED_PREFIX . hash( 'sha256', $payload->challenge->signature ), (string) time(), '', false );
|
||||
}
|
||||
|
||||
public static function cleanup(): void {
|
||||
global $wpdb;
|
||||
$wpdb->query(
|
||||
$wpdb->prepare(
|
||||
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s AND CAST(option_value AS UNSIGNED) < %d",
|
||||
$wpdb->esc_like( self::USED_PREFIX ) . '%',
|
||||
time() - self::USED_KEEP
|
||||
)
|
||||
);
|
||||
wp_cache_delete( 'alloptions', 'options' );
|
||||
}
|
||||
|
||||
private static function secret(): string {
|
||||
return hash_hmac( 'sha256', 'smilia-captcha', wp_salt( 'auth' ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
use WP_Post;
|
||||
use WP_Query;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Content {
|
||||
|
||||
private const CACHE_PREFIX = 'smilia_content_';
|
||||
private const CACHE_TTL = DAY_IN_SECONDS;
|
||||
private const VERSION = 'smilia_content_version';
|
||||
|
||||
private const TRACKED = array(
|
||||
Post_Types::SERVICE,
|
||||
Post_Types::DOCTOR,
|
||||
Post_Types::REVIEW,
|
||||
Post_Types::FAQ,
|
||||
);
|
||||
|
||||
public function register(): void {
|
||||
add_action( 'save_post', array( $this, 'flush_for_post' ), 20, 2 );
|
||||
add_action( 'deleted_post', array( $this, 'flush_for_post' ), 20, 2 );
|
||||
add_action( 'set_object_terms', array( self::class, 'flush_all' ) );
|
||||
add_action( 'update_option_' . Settings::OPTION, array( self::class, 'flush_all' ) );
|
||||
}
|
||||
|
||||
public function flush_for_post( int $post_id, $post = null ): void {
|
||||
$type = $post instanceof WP_Post ? $post->post_type : get_post_type( $post_id );
|
||||
if ( in_array( $type, self::TRACKED, true ) ) {
|
||||
self::flush_all();
|
||||
}
|
||||
}
|
||||
|
||||
public static function flush_all(): void {
|
||||
update_option( self::VERSION, (string) microtime( true ), true );
|
||||
}
|
||||
|
||||
private static function language(): ?string {
|
||||
if ( ! function_exists( 'pll_current_language' ) ) {
|
||||
return get_locale();
|
||||
}
|
||||
$language = pll_current_language();
|
||||
if ( ! $language && function_exists( 'pll_get_post_language' ) && get_post() ) {
|
||||
$language = pll_get_post_language( get_post()->ID );
|
||||
}
|
||||
return $language ? $language : null;
|
||||
}
|
||||
|
||||
private static function key( string $name, string $language ): string {
|
||||
return self::CACHE_PREFIX . md5( $name . '|' . get_option( self::VERSION, '0' ) . '|' . $language );
|
||||
}
|
||||
|
||||
private static function service_items(): array {
|
||||
return self::remember(
|
||||
'services',
|
||||
static function (): array {
|
||||
return array_map(
|
||||
static fn( WP_Post $post ): array => array(
|
||||
'id' => $post->ID,
|
||||
'slug' => $post->post_name,
|
||||
'title' => get_the_title( $post ),
|
||||
'url' => get_permalink( $post ),
|
||||
'summary' => get_the_excerpt( $post ),
|
||||
'image_id' => (int) get_post_thumbnail_id( $post ),
|
||||
'tone' => (string) get_post_meta( $post->ID, Meta_Fields::TONE, true ),
|
||||
),
|
||||
self::posts( Post_Types::SERVICE )
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public static function services(): array {
|
||||
return self::with_images( self::service_items() );
|
||||
}
|
||||
|
||||
public static function service_urls(): array {
|
||||
return array_column( self::service_items(), 'url', 'slug' );
|
||||
}
|
||||
|
||||
public static function doctors(): array {
|
||||
$items = self::remember(
|
||||
'doctors',
|
||||
static function (): array {
|
||||
return array_map(
|
||||
static fn( WP_Post $post ): array => array(
|
||||
'id' => $post->ID,
|
||||
'name' => get_the_title( $post ),
|
||||
'url' => get_permalink( $post ),
|
||||
'position' => (string) get_post_meta( $post->ID, Meta_Fields::POSITION, true ),
|
||||
'image_id' => (int) get_post_thumbnail_id( $post ),
|
||||
'tone' => (string) get_post_meta( $post->ID, Meta_Fields::TONE, true ),
|
||||
),
|
||||
self::posts( Post_Types::DOCTOR )
|
||||
);
|
||||
}
|
||||
);
|
||||
return self::with_images( $items );
|
||||
}
|
||||
|
||||
public static function reviews( int $limit = 3 ): array {
|
||||
$all = self::remember(
|
||||
'reviews',
|
||||
static function (): array {
|
||||
return array_map(
|
||||
static fn( WP_Post $post ): array => array(
|
||||
'id' => $post->ID,
|
||||
'author' => get_the_title( $post ),
|
||||
'text' => wp_strip_all_tags( $post->post_content ),
|
||||
'topic' => (string) get_post_meta( $post->ID, Meta_Fields::TOPIC, true ),
|
||||
'source' => (string) get_post_meta( $post->ID, Meta_Fields::SOURCE, true ),
|
||||
),
|
||||
self::posts( Post_Types::REVIEW )
|
||||
);
|
||||
}
|
||||
);
|
||||
return array_slice( $all, 0, $limit );
|
||||
}
|
||||
|
||||
public static function faq( string $group ): array {
|
||||
$group = sanitize_key( $group );
|
||||
return self::remember(
|
||||
'faq_' . $group,
|
||||
static function () use ( $group ): array {
|
||||
return array_map(
|
||||
static fn( WP_Post $post ): array => array(
|
||||
'id' => $post->ID,
|
||||
'question' => get_the_title( $post ),
|
||||
'answer' => apply_filters( 'the_content', $post->post_content ),
|
||||
),
|
||||
self::posts(
|
||||
Post_Types::FAQ,
|
||||
array(
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => Post_Types::FAQ_GROUP,
|
||||
'field' => 'slug',
|
||||
'terms' => $group,
|
||||
),
|
||||
),
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private static function posts( string $post_type, array $extra = array() ): array {
|
||||
$language = function_exists( 'pll_current_language' ) ? self::language() : null;
|
||||
if ( $language ) {
|
||||
$extra['lang'] = $language;
|
||||
}
|
||||
$query = new WP_Query(
|
||||
array_merge(
|
||||
array(
|
||||
'post_type' => $post_type,
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => 50,
|
||||
'orderby' => array(
|
||||
'menu_order' => 'ASC',
|
||||
'date' => 'DESC',
|
||||
),
|
||||
'no_found_rows' => true,
|
||||
'update_post_term_cache' => false,
|
||||
),
|
||||
$extra
|
||||
)
|
||||
);
|
||||
return $query->posts;
|
||||
}
|
||||
|
||||
private static function with_images( array $items ): array {
|
||||
$ids = array_filter( array_column( $items, 'image_id' ) );
|
||||
if ( $ids ) {
|
||||
_prime_post_caches( $ids, false, true );
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
private static function remember( string $key, callable $build ): array {
|
||||
$language = self::language();
|
||||
if ( null === $language ) {
|
||||
return $build();
|
||||
}
|
||||
$cache_key = self::key( $key, $language );
|
||||
$cached = get_transient( $cache_key );
|
||||
if ( is_array( $cached ) ) {
|
||||
return $cached;
|
||||
}
|
||||
$value = $build();
|
||||
set_transient( $cache_key, $value, self::CACHE_TTL );
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
use WP_Error;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Demo_Access {
|
||||
|
||||
public const ROLE = 'smilia_demo';
|
||||
|
||||
public const LOGIN = 'drillandfill';
|
||||
|
||||
private const LOCALES = array(
|
||||
'en' => 'en_US',
|
||||
'pl' => 'pl_PL',
|
||||
'ru' => 'ru_RU',
|
||||
);
|
||||
|
||||
private const LOCALE_COOKIE = 'smilia_demo_locale';
|
||||
|
||||
private const DENIED_CAPS = array(
|
||||
'install_plugins',
|
||||
'update_plugins',
|
||||
'delete_plugins',
|
||||
'edit_plugins',
|
||||
'upload_plugins',
|
||||
'install_themes',
|
||||
'update_themes',
|
||||
'delete_themes',
|
||||
'edit_themes',
|
||||
'upload_themes',
|
||||
'update_core',
|
||||
'update_languages',
|
||||
'install_languages',
|
||||
'create_users',
|
||||
'delete_users',
|
||||
'edit_users',
|
||||
'promote_users',
|
||||
'remove_users',
|
||||
'add_users',
|
||||
'export',
|
||||
'import',
|
||||
'edit_files',
|
||||
'unfiltered_upload',
|
||||
'export_others_personal_data',
|
||||
'erase_others_personal_data',
|
||||
'manage_privacy_options',
|
||||
);
|
||||
|
||||
private const WRITE_SQL = '/^\s*(?:INSERT|UPDATE|DELETE|REPLACE|ALTER|CREATE|DROP|TRUNCATE|RENAME|GRANT|REVOKE|LOCK|LOAD)\b/i';
|
||||
|
||||
public static function enabled(): bool {
|
||||
return defined( 'SMILIA_DEMO' ) && SMILIA_DEMO;
|
||||
}
|
||||
|
||||
public function register(): void {
|
||||
if ( ! self::enabled() ) {
|
||||
return;
|
||||
}
|
||||
add_filter( 'query', array( $this, 'guard_query' ), PHP_INT_MAX );
|
||||
add_filter( 'user_has_cap', array( $this, 'limit_caps' ), PHP_INT_MAX );
|
||||
add_filter( 'rest_pre_dispatch', array( $this, 'guard_rest' ), 0, 3 );
|
||||
add_action( 'admin_init', array( $this, 'guard_admin' ), 0 );
|
||||
add_action( 'template_redirect', array( $this, 'redirect_front' ), 0 );
|
||||
add_filter( 'rest_authentication_errors', array( $this, 'guard_rest_auth' ), PHP_INT_MAX );
|
||||
add_filter( 'allow_password_reset', '__return_false' );
|
||||
add_filter( 'show_password_fields', '__return_false' );
|
||||
add_filter( 'pre_wp_mail', '__return_false' );
|
||||
add_filter( 'pre_option_blog_public', '__return_zero' );
|
||||
add_action( 'admin_notices', array( $this, 'notice' ) );
|
||||
add_action( 'enqueue_block_editor_assets', array( $this, 'quiet_editor_errors' ) );
|
||||
add_filter( 'login_message', array( $this, 'login_message' ) );
|
||||
add_filter( 'lost_password_html_link', '__return_empty_string' );
|
||||
add_action( 'login_form_login', array( $this, 'auto_login' ) );
|
||||
add_filter( 'get_user_metadata', array( $this, 'user_locale' ), 10, 3 );
|
||||
add_action( 'init', array( $this, 'remember_locale' ), 0 );
|
||||
foreach ( array( 'lostpassword', 'retrievepassword', 'rp', 'resetpass', 'register' ) as $action ) {
|
||||
add_action( 'login_form_' . $action, array( $this, 'deny' ) );
|
||||
}
|
||||
}
|
||||
|
||||
public static function install(): int {
|
||||
global $wpdb;
|
||||
$admin = get_role( 'administrator' );
|
||||
if ( ! $admin ) {
|
||||
return 0;
|
||||
}
|
||||
remove_role( self::ROLE );
|
||||
add_role( self::ROLE, __( 'Демо-просмотр', 'smilia-core' ), array_diff_key( $admin->capabilities, array_flip( self::DENIED_CAPS ) ) );
|
||||
$user_id = wp_insert_user(
|
||||
array(
|
||||
'user_login' => self::LOGIN,
|
||||
'user_pass' => wp_generate_password( 64, true, true ),
|
||||
'user_email' => self::LOGIN . '@smilia.invalid',
|
||||
'display_name' => 'Drill and Fill',
|
||||
'role' => self::ROLE,
|
||||
'locale' => 'en_US',
|
||||
)
|
||||
);
|
||||
if ( is_wp_error( $user_id ) ) {
|
||||
return 0;
|
||||
}
|
||||
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->posts} SET post_author = %d", $user_id ) );
|
||||
return (int) $user_id;
|
||||
}
|
||||
|
||||
public function guard_query( $query ) {
|
||||
if ( ! is_string( $query ) || ! preg_match( self::WRITE_SQL, $query ) || $this->login_request() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
|
||||
return $query;
|
||||
}
|
||||
global $wpdb;
|
||||
if ( false !== strpos( $query, $wpdb->options ) && preg_match( "/'_(?:site_)?transient_/", $query ) ) {
|
||||
return $query;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
public function limit_caps( array $allcaps ): array {
|
||||
foreach ( self::DENIED_CAPS as $cap ) {
|
||||
unset( $allcaps[ $cap ] );
|
||||
}
|
||||
return $allcaps;
|
||||
}
|
||||
|
||||
public function guard_rest( $result, $server, $request ) {
|
||||
if ( in_array( $request->get_method(), array( 'GET', 'HEAD', 'OPTIONS' ), true ) ) {
|
||||
return $result;
|
||||
}
|
||||
return new WP_Error( 'smilia_demo', self::message(), array( 'status' => 403 ) );
|
||||
}
|
||||
|
||||
public function redirect_front(): void {
|
||||
$public = defined( 'SMILIA_PUBLIC_URL' ) ? (string) SMILIA_PUBLIC_URL : '';
|
||||
if ( '' === $public ) {
|
||||
status_header( 404 );
|
||||
exit;
|
||||
}
|
||||
$uri = isset( $_SERVER['REQUEST_URI'] ) ? '/' . ltrim( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ), '/' ) : '/';
|
||||
wp_redirect( untrailingslashit( $public ) . $uri, 301, 'Smilia' );
|
||||
exit;
|
||||
}
|
||||
|
||||
public function guard_rest_auth( $result ) {
|
||||
if ( is_wp_error( $result ) || is_user_logged_in() ) {
|
||||
return $result;
|
||||
}
|
||||
return new WP_Error( 'smilia_demo', self::message(), array( 'status' => 401 ) );
|
||||
}
|
||||
|
||||
public function guard_admin(): void {
|
||||
$method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_key( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : 'GET';
|
||||
if ( wp_doing_ajax() ) {
|
||||
$action = isset( $_REQUEST['action'] ) ? sanitize_key( wp_unslash( $_REQUEST['action'] ) ) : '';
|
||||
if ( in_array( $action, array( 'heartbeat', 'rest-nonce' ), true ) ) {
|
||||
return;
|
||||
}
|
||||
wp_send_json_error( array( 'message' => self::message() ), 403 );
|
||||
}
|
||||
$writes_on_load = array( 'post-new.php', 'media-new.php', 'erase-personal-data.php', 'export-personal-data.php' );
|
||||
if ( in_array( $GLOBALS['pagenow'] ?? '', $writes_on_load, true ) ) {
|
||||
$this->deny();
|
||||
}
|
||||
$read = in_array( $method, array( 'GET', 'HEAD' ), true ) && ! isset( $_REQUEST['_wpnonce'] ) && ! isset( $_REQUEST['_ajax_nonce'] );
|
||||
if ( ! $read ) {
|
||||
$this->deny();
|
||||
}
|
||||
}
|
||||
|
||||
public function auto_login(): void {
|
||||
$method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_key( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : 'GET';
|
||||
$user = get_user_by( 'login', self::LOGIN );
|
||||
if ( 'GET' !== $method || isset( $_GET['loggedout'] ) || ! $user ) {
|
||||
return;
|
||||
}
|
||||
wp_set_current_user( $user->ID );
|
||||
wp_set_auth_cookie( $user->ID, false, true );
|
||||
$redirect = isset( $_GET['redirect_to'] ) ? esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) : '';
|
||||
$redirect = wp_validate_redirect( $redirect, '' );
|
||||
wp_safe_redirect( '' !== $redirect ? $redirect : admin_url() );
|
||||
exit;
|
||||
}
|
||||
|
||||
public function user_locale( $value, $object_id, $meta_key ) {
|
||||
$locale = self::requested_locale();
|
||||
if ( 'locale' !== $meta_key || '' === $locale ) {
|
||||
return $value;
|
||||
}
|
||||
$user = get_userdata( (int) $object_id );
|
||||
return $user && self::LOGIN === $user->user_login ? array( $locale ) : $value;
|
||||
}
|
||||
|
||||
public function remember_locale(): void {
|
||||
$code = isset( $_GET['locale'] ) ? sanitize_key( wp_unslash( $_GET['locale'] ) ) : '';
|
||||
if ( isset( self::LOCALES[ $code ] ) && ! headers_sent() ) {
|
||||
setcookie( self::LOCALE_COOKIE, $code, 0, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, true, true );
|
||||
}
|
||||
}
|
||||
|
||||
private static function requested_locale(): string {
|
||||
$code = isset( $_GET['locale'] ) ? sanitize_key( wp_unslash( $_GET['locale'] ) ) : '';
|
||||
if ( ! isset( self::LOCALES[ $code ] ) ) {
|
||||
$code = isset( $_COOKIE[ self::LOCALE_COOKIE ] ) ? sanitize_key( wp_unslash( $_COOKIE[ self::LOCALE_COOKIE ] ) ) : '';
|
||||
}
|
||||
return self::LOCALES[ $code ] ?? '';
|
||||
}
|
||||
|
||||
public function deny(): void {
|
||||
wp_die( esc_html( self::message() ), esc_html__( 'Демо-доступ', 'smilia-core' ), array( 'response' => 403, 'back_link' => true ) );
|
||||
}
|
||||
|
||||
public function quiet_editor_errors(): void {
|
||||
$blank = array( ' ' );
|
||||
$data = array(
|
||||
'Updating failed.' => $blank,
|
||||
'Publishing failed.' => $blank,
|
||||
'Scheduling failed.' => $blank,
|
||||
);
|
||||
wp_add_inline_script( 'wp-editor', 'wp.i18n.setLocaleData( ' . wp_json_encode( $data ) . ' );' );
|
||||
wp_add_inline_script( 'wp-edit-post', "wp.data.dispatch( 'core/preferences' ).set( 'core/edit-post', 'welcomeGuide', false );" );
|
||||
}
|
||||
|
||||
public function notice(): void {
|
||||
printf( '<div class="notice notice-warning"><p>%s</p></div>', esc_html( self::message() ) );
|
||||
}
|
||||
|
||||
public function login_message( string $message ): string {
|
||||
return $message . sprintf( '<p class="message">%s</p>', esc_html( self::message() ) );
|
||||
}
|
||||
|
||||
private function login_request(): bool {
|
||||
return isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'];
|
||||
}
|
||||
|
||||
private static function message(): string {
|
||||
return __( 'Изменения не сохраняются.', 'smilia-core' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class I18n {
|
||||
|
||||
private const GROUP = 'Smilia';
|
||||
|
||||
public const STRINGS = array(
|
||||
'hours' => true,
|
||||
'booking_title' => false,
|
||||
'booking_lead' => true,
|
||||
'booking_submit' => false,
|
||||
'booking_done' => false,
|
||||
'booking_thanks' => true,
|
||||
);
|
||||
|
||||
public function register(): void {
|
||||
add_filter( 'pll_get_post_types', array( $this, 'post_types' ), 10, 2 );
|
||||
add_filter( 'pll_get_taxonomies', array( $this, 'taxonomies' ) );
|
||||
add_filter( 'pll_filtered_rest_routes', array( $this, 'rest_routes' ) );
|
||||
add_filter( 'block_core_breadcrumbs_items', array( $this, 'breadcrumb_home' ) );
|
||||
add_filter( 'do_redirect_guess_404_permalink', '__return_false' );
|
||||
add_filter( 'wp_unique_post_slug', array( $this, 'shared_slug' ), 10, 6 );
|
||||
add_action( 'pre_get_posts', array( $this, 'query_language' ) );
|
||||
add_filter( 'pll_rel_hreflang_attributes', array( $this, 'default_hreflang' ) );
|
||||
add_action( 'pll_language_defined', array( $this, 'rest_locale' ), 10, 2 );
|
||||
add_action( 'admin_init', array( $this, 'register_strings' ) );
|
||||
}
|
||||
|
||||
public function post_types( array $types, bool $is_settings ): array {
|
||||
$own = array( Post_Types::SERVICE, Post_Types::DOCTOR, Post_Types::REVIEW, Post_Types::FAQ );
|
||||
if ( $is_settings ) {
|
||||
return array_diff( $types, $own, array( Post_Types::BOOKING ) );
|
||||
}
|
||||
unset( $types[ Post_Types::BOOKING ] );
|
||||
return array_merge( $types, array_combine( $own, $own ) );
|
||||
}
|
||||
|
||||
public function rest_routes( array $routes ): array {
|
||||
$routes['block-renderer'] = 'wp/v2/block-renderer';
|
||||
return $routes;
|
||||
}
|
||||
|
||||
public function breadcrumb_home( array $items ): array {
|
||||
if ( isset( $items[0]['url'] ) && function_exists( 'pll_home_url' ) ) {
|
||||
$items[0]['url'] = pll_home_url();
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
public function taxonomies( array $taxonomies ): array {
|
||||
unset( $taxonomies[ Post_Types::FAQ_GROUP ] );
|
||||
return $taxonomies;
|
||||
}
|
||||
|
||||
public function shared_slug( string $slug, int $post_id, string $status, string $post_type, int $parent, string $original ): string {
|
||||
if ( $slug === $original || ! function_exists( 'pll_get_post_language' ) || ! pll_is_translated_post_type( $post_type ) ) {
|
||||
return $slug;
|
||||
}
|
||||
$language = pll_get_post_language( $post_id );
|
||||
if ( ! $language ) {
|
||||
return $slug;
|
||||
}
|
||||
global $wpdb;
|
||||
$same = $wpdb->get_col(
|
||||
$wpdb->prepare(
|
||||
"SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = %s AND post_parent = %d AND ID <> %d",
|
||||
$original,
|
||||
$post_type,
|
||||
$parent,
|
||||
$post_id
|
||||
)
|
||||
);
|
||||
foreach ( $same as $other ) {
|
||||
if ( pll_get_post_language( (int) $other ) === $language ) {
|
||||
return $slug;
|
||||
}
|
||||
}
|
||||
return $original;
|
||||
}
|
||||
|
||||
public function query_language( \WP_Query $query ): void {
|
||||
if ( is_admin() || ! $query->is_main_query() || ! function_exists( 'pll_current_language' ) || ! pll_current_language() ) {
|
||||
return;
|
||||
}
|
||||
$is_page = '' !== (string) $query->get( 'pagename' );
|
||||
$name = $is_page ? basename( (string) $query->get( 'pagename' ) ) : (string) $query->get( 'name' );
|
||||
$post_type = $is_page ? 'page' : $query->get( 'post_type' );
|
||||
if ( '' === $name || ! is_string( $post_type ) || '' === $post_type || ! pll_is_translated_post_type( $post_type ) ) {
|
||||
return;
|
||||
}
|
||||
$ids = get_posts(
|
||||
array(
|
||||
'post_type' => $post_type,
|
||||
'post_name__in' => array( $name ),
|
||||
'lang' => pll_current_language(),
|
||||
'post_status' => 'any',
|
||||
'fields' => 'ids',
|
||||
'numberposts' => -1,
|
||||
'suppress_filters' => false,
|
||||
)
|
||||
);
|
||||
if ( ! $ids ) {
|
||||
return;
|
||||
}
|
||||
if ( $is_page ) {
|
||||
$path = trim( (string) $query->get( 'pagename' ), '/' );
|
||||
$page = null;
|
||||
foreach ( $ids as $id ) {
|
||||
if ( get_page_uri( (int) $id ) === $path ) {
|
||||
$page = (int) $id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( null === $page ) {
|
||||
return;
|
||||
}
|
||||
$query->set( 'pagename', '' );
|
||||
$query->set( 'page_id', $page );
|
||||
$query->queried_object = get_post( $page );
|
||||
$query->queried_object_id = $page;
|
||||
return;
|
||||
}
|
||||
$query->set( 'p', (int) $ids[0] );
|
||||
}
|
||||
|
||||
public function rest_locale( string $slug, object $language ): void {
|
||||
if ( defined( 'REST_REQUEST' ) && REST_REQUEST && ! empty( $language->locale ) ) {
|
||||
switch_to_locale( $language->locale );
|
||||
}
|
||||
}
|
||||
|
||||
public function default_hreflang( array $hreflangs ): array {
|
||||
if ( Seo::is_service_page() ) {
|
||||
return array();
|
||||
}
|
||||
$default = function_exists( 'pll_default_language' ) ? pll_default_language( 'locale' ) : '';
|
||||
foreach ( $hreflangs as $code => $url ) {
|
||||
if ( $default && 0 === strpos( str_replace( '_', '-', $default ), $code ) ) {
|
||||
$hreflangs['x-default'] = $url;
|
||||
}
|
||||
}
|
||||
return $hreflangs;
|
||||
}
|
||||
|
||||
public function register_strings(): void {
|
||||
if ( ! function_exists( 'pll_register_string' ) ) {
|
||||
return;
|
||||
}
|
||||
$fields = Settings::fields();
|
||||
foreach ( self::STRINGS as $key => $multiline ) {
|
||||
$value = Settings::raw( $key );
|
||||
if ( '' !== $value ) {
|
||||
pll_register_string( (string) $fields[ $key ][0], $value, self::GROUP, $multiline );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function translate( string $key, string $value ): string {
|
||||
if ( '' === $value || ! isset( self::STRINGS[ $key ] ) || ! function_exists( 'pll__' ) ) {
|
||||
return $value;
|
||||
}
|
||||
return pll__( $value );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
use WP_Post;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Markup {
|
||||
|
||||
public static function text( $value ): string {
|
||||
return esc_html( wp_strip_all_tags( (string) $value ) );
|
||||
}
|
||||
|
||||
public static function link( $value ): string {
|
||||
return esc_url( self::url( (string) $value ) );
|
||||
}
|
||||
|
||||
public static function url( string $value ): string {
|
||||
$value = trim( $value );
|
||||
if ( ! str_starts_with( $value, '/' ) ) {
|
||||
return $value;
|
||||
}
|
||||
$home = function_exists( 'pll_home_url' ) ? pll_home_url() : home_url( '/' );
|
||||
return rtrim( $home, '/' ) . $value;
|
||||
}
|
||||
|
||||
private static function can_edit( int $post_id, array $attributes ): bool {
|
||||
return 'all' !== ( $attributes['part'] ?? 'all' ) && current_user_can( 'edit_post', $post_id );
|
||||
}
|
||||
|
||||
public static function edit_link( int $post_id, array $attributes ): string {
|
||||
if ( ! self::can_edit( $post_id, $attributes ) ) {
|
||||
return '';
|
||||
}
|
||||
return sprintf( ' data-edit-url="%s"', esc_url( (string) get_edit_post_link( $post_id, 'raw' ) ) );
|
||||
}
|
||||
|
||||
public static function edit_button( int $post_id, array $attributes ): void {
|
||||
if ( 'all' === ( $attributes['part'] ?? 'all' ) ) {
|
||||
edit_post_link( __( 'Изменить', 'smilia-core' ), '', '', $post_id, 'edit-badge' );
|
||||
return;
|
||||
}
|
||||
if ( ! self::can_edit( $post_id, $attributes ) ) {
|
||||
return;
|
||||
}
|
||||
printf(
|
||||
'<span class="edit-badge" role="button" title="%s">%s</span>',
|
||||
esc_attr( sprintf( __( 'Изменить: %s', 'smilia-core' ), get_the_title( $post_id ) ) ),
|
||||
esc_html__( 'Изменить', 'smilia-core' )
|
||||
);
|
||||
}
|
||||
|
||||
public static function trail( ?WP_Post $post = null ): array {
|
||||
$post = $post ?? get_post();
|
||||
if ( ! $post instanceof WP_Post ) {
|
||||
return array();
|
||||
}
|
||||
$home = function_exists( 'pll_home_url' ) ? pll_home_url() : home_url( '/' );
|
||||
$trail = array(
|
||||
array(
|
||||
'title' => __( 'Home' ),
|
||||
'url' => $home,
|
||||
),
|
||||
);
|
||||
if ( (int) get_option( 'page_on_front' ) === $post->ID || ( function_exists( 'pll_get_post' ) && (int) pll_get_post( (int) get_option( 'page_on_front' ) ) === $post->ID ) ) {
|
||||
return $trail;
|
||||
}
|
||||
foreach ( array_reverse( get_post_ancestors( $post ) ) as $ancestor ) {
|
||||
$trail[] = array(
|
||||
'title' => get_the_title( $ancestor ),
|
||||
'url' => get_permalink( $ancestor ),
|
||||
);
|
||||
}
|
||||
$trail[] = array(
|
||||
'title' => get_the_title( $post ),
|
||||
'url' => get_permalink( $post ),
|
||||
);
|
||||
return $trail;
|
||||
}
|
||||
|
||||
public static function wave(): void {
|
||||
echo '<svg class="wave" viewBox="0 0 1440 130" preserveAspectRatio="none" aria-hidden="true" focusable="false"><path class="wave__back" d="M0,78 C240,18 480,14 740,52 C1000,90 1220,96 1440,48 L1440,130 L0,130 Z"/><path class="wave__middle" d="M0,96 C260,42 520,40 770,72 C1020,104 1240,108 1440,70 L1440,130 L0,130 Z"/><path class="wave__front" d="M0,114 C280,70 560,68 800,92 C1040,116 1260,118 1440,94 L1440,130 L0,130 Z"/></svg>';
|
||||
}
|
||||
|
||||
public static function asset( string $path ): string {
|
||||
$url = plugins_url( 'assets/img/' . $path, SMILIA_CORE_FILE );
|
||||
$file = SMILIA_CORE_DIR . '/assets/img/' . $path;
|
||||
return is_readable( $file ) ? add_query_arg( 'ver', (string) filemtime( $file ), $url ) : $url;
|
||||
}
|
||||
|
||||
public static function asset_svg( string $path, string $class_name = '', string $alt = '', string $loading = 'lazy' ): void {
|
||||
Picture::svg( SMILIA_CORE_DIR . '/assets/img/' . $path, self::asset( $path ), $class_name, $alt, $loading );
|
||||
}
|
||||
|
||||
public static function image( int $attachment_id, string $size, string $sizes, array $attr = array() ): void {
|
||||
if ( $attachment_id <= 0 ) {
|
||||
return;
|
||||
}
|
||||
echo wp_get_attachment_image(
|
||||
$attachment_id,
|
||||
$size,
|
||||
false,
|
||||
array_merge(
|
||||
array(
|
||||
'sizes' => $sizes,
|
||||
'decoding' => 'async',
|
||||
'loading' => 'lazy',
|
||||
),
|
||||
$attr
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Picture {
|
||||
|
||||
private static array $svg_sizes = array();
|
||||
|
||||
public static function svg( string $path, string $url, string $class_name = '', string $alt = '', string $loading = 'lazy' ): void {
|
||||
$size = self::svg_size( $path );
|
||||
$url = is_readable( $path ) ? add_query_arg( 'ver', (string) filemtime( $path ), $url ) : $url;
|
||||
printf(
|
||||
'<img class="%s" src="%s" alt="%s" width="%d" height="%d" decoding="async" loading="%s">',
|
||||
esc_attr( $class_name ),
|
||||
esc_url( $url ),
|
||||
esc_attr( $alt ),
|
||||
(int) $size[0],
|
||||
(int) $size[1],
|
||||
esc_attr( $loading )
|
||||
);
|
||||
}
|
||||
|
||||
private static function svg_size( string $path ): array {
|
||||
if ( ! isset( self::$svg_sizes[ $path ] ) ) {
|
||||
$head = is_readable( $path ) ? (string) file_get_contents( $path, false, null, 0, 600 ) : '';
|
||||
self::$svg_sizes[ $path ] = preg_match( '/viewBox="[\d.\-]+\s+[\d.\-]+\s+([\d.]+)\s+([\d.]+)"/', $head, $m )
|
||||
? array( (int) round( (float) $m[1] ), (int) round( (float) $m[2] ) )
|
||||
: array( 100, 100 );
|
||||
}
|
||||
return self::$svg_sizes[ $path ];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Plugin {
|
||||
|
||||
private static ?Plugin $instance = null;
|
||||
|
||||
public static function instance(): Plugin {
|
||||
if ( null === self::$instance ) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function register(): void {
|
||||
add_action( 'init', array( $this, 'load_textdomain' ) );
|
||||
( new Post_Types() )->register();
|
||||
( new Meta_Fields() )->register();
|
||||
( new Settings() )->register();
|
||||
( new Content() )->register();
|
||||
( new Blocks() )->register();
|
||||
( new Booking() )->register();
|
||||
( new Captcha() )->register();
|
||||
( new Seo() )->register();
|
||||
( new Sitemap() )->register();
|
||||
( new I18n() )->register();
|
||||
( new Demo_Access() )->register();
|
||||
}
|
||||
|
||||
public function load_textdomain(): void {
|
||||
load_plugin_textdomain( 'smilia-core', false, dirname( plugin_basename( SMILIA_CORE_FILE ) ) . '/languages' );
|
||||
}
|
||||
|
||||
public static function activate(): void {
|
||||
( new Post_Types() )->register_types();
|
||||
flush_rewrite_rules();
|
||||
Booking::schedule_cleanup();
|
||||
}
|
||||
|
||||
public static function deactivate(): void {
|
||||
flush_rewrite_rules();
|
||||
Booking::unschedule_cleanup();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Post_Types {
|
||||
|
||||
public const SERVICE = 'smilia_service';
|
||||
public const DOCTOR = 'smilia_doctor';
|
||||
public const REVIEW = 'smilia_review';
|
||||
public const FAQ = 'smilia_faq';
|
||||
public const BOOKING = 'smilia_booking';
|
||||
|
||||
public const FAQ_GROUP = 'smilia_faq_group';
|
||||
|
||||
public function register(): void {
|
||||
add_action( 'init', array( $this, 'register_types' ) );
|
||||
}
|
||||
|
||||
public function register_types(): void {
|
||||
register_post_type(
|
||||
self::SERVICE,
|
||||
array(
|
||||
'labels' => $this->labels( __( 'Услуги', 'smilia-core' ), __( 'Услуга', 'smilia-core' ) ),
|
||||
'public' => true,
|
||||
'show_in_rest' => true,
|
||||
'has_archive' => false,
|
||||
'menu_icon' => 'dashicons-heart',
|
||||
'menu_position' => 20,
|
||||
'rewrite' => array(
|
||||
'slug' => 'services',
|
||||
'with_front' => false,
|
||||
),
|
||||
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'page-attributes', 'custom-fields' ),
|
||||
)
|
||||
);
|
||||
|
||||
register_post_type(
|
||||
self::DOCTOR,
|
||||
array(
|
||||
'labels' => $this->labels( __( 'Врачи', 'smilia-core' ), __( 'Врач', 'smilia-core' ) ),
|
||||
'public' => true,
|
||||
'show_in_rest' => true,
|
||||
'has_archive' => false,
|
||||
'menu_icon' => 'dashicons-id',
|
||||
'menu_position' => 21,
|
||||
'rewrite' => array(
|
||||
'slug' => 'doctors',
|
||||
'with_front' => false,
|
||||
),
|
||||
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'page-attributes', 'custom-fields' ),
|
||||
)
|
||||
);
|
||||
|
||||
register_post_type(
|
||||
self::REVIEW,
|
||||
array(
|
||||
'labels' => $this->labels( __( 'Отзывы', 'smilia-core' ), __( 'Отзыв', 'smilia-core' ) ),
|
||||
'public' => false,
|
||||
'show_ui' => true,
|
||||
'show_in_rest' => true,
|
||||
'exclude_from_search' => true,
|
||||
'menu_icon' => 'dashicons-format-quote',
|
||||
'menu_position' => 22,
|
||||
'supports' => array( 'title', 'editor', 'page-attributes', 'custom-fields' ),
|
||||
)
|
||||
);
|
||||
|
||||
register_post_type(
|
||||
self::FAQ,
|
||||
array(
|
||||
'labels' => $this->labels( __( 'Вопросы', 'smilia-core' ), __( 'Вопрос', 'smilia-core' ) ),
|
||||
'public' => false,
|
||||
'show_ui' => true,
|
||||
'show_in_rest' => true,
|
||||
'exclude_from_search' => true,
|
||||
'menu_icon' => 'dashicons-editor-help',
|
||||
'menu_position' => 23,
|
||||
'supports' => array( 'title', 'editor', 'page-attributes' ),
|
||||
)
|
||||
);
|
||||
|
||||
register_taxonomy(
|
||||
self::FAQ_GROUP,
|
||||
self::FAQ,
|
||||
array(
|
||||
'labels' => array(
|
||||
'name' => __( 'Группы вопросов', 'smilia-core' ),
|
||||
'singular_name' => __( 'Группа вопросов', 'smilia-core' ),
|
||||
),
|
||||
'public' => false,
|
||||
'show_ui' => true,
|
||||
'show_in_rest' => true,
|
||||
'show_admin_column' => true,
|
||||
'hierarchical' => true,
|
||||
)
|
||||
);
|
||||
|
||||
register_post_type(
|
||||
self::BOOKING,
|
||||
array(
|
||||
'labels' => $this->labels( __( 'Заявки', 'smilia-core' ), __( 'Заявка', 'smilia-core' ) ),
|
||||
'public' => false,
|
||||
'show_ui' => true,
|
||||
'show_in_rest' => false,
|
||||
'exclude_from_search' => true,
|
||||
'menu_icon' => 'dashicons-calendar-alt',
|
||||
'menu_position' => 24,
|
||||
'supports' => array( 'title' ),
|
||||
'capability_type' => 'post',
|
||||
'capabilities' => array(
|
||||
'create_posts' => 'do_not_allow',
|
||||
),
|
||||
'map_meta_cap' => true,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function labels( string $plural, string $singular ): array {
|
||||
return array(
|
||||
'name' => $plural,
|
||||
'singular_name' => $singular,
|
||||
'menu_name' => $plural,
|
||||
'add_new_item' => sprintf( __( 'Добавить: %s', 'smilia-core' ), $singular ),
|
||||
'edit_item' => sprintf( __( 'Редактировать: %s', 'smilia-core' ), $singular ),
|
||||
'all_items' => sprintf( __( 'Все: %s', 'smilia-core' ), mb_strtolower( $plural ) ),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Prices {
|
||||
|
||||
public static function groups( array $groups ): array {
|
||||
$urls = Content::service_urls();
|
||||
$result = array();
|
||||
foreach ( $groups as $group ) {
|
||||
$title = self::plain( (string) ( $group['title'] ?? '' ) );
|
||||
$items = array();
|
||||
foreach ( (array) ( $group['items'] ?? array() ) as $item ) {
|
||||
$name = self::plain( (string) ( $item['name'] ?? '' ) );
|
||||
if ( '' === $name ) {
|
||||
continue;
|
||||
}
|
||||
$raw = self::plain( (string) ( $item['price'] ?? '' ) );
|
||||
$items[] = array(
|
||||
'name' => $name,
|
||||
'price' => self::format( $raw ),
|
||||
'amount' => ctype_digit( preg_replace( '/\s/', '', $raw ) ) && '' !== $raw ? (int) preg_replace( '/\s/', '', $raw ) : null,
|
||||
);
|
||||
}
|
||||
if ( '' === $title || ! $items ) {
|
||||
continue;
|
||||
}
|
||||
$service = sanitize_title( (string) ( $group['service'] ?? '' ) );
|
||||
$result[] = array(
|
||||
'title' => $title,
|
||||
'service' => $service,
|
||||
'url' => $urls[ $service ] ?? '',
|
||||
'items' => $items,
|
||||
);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function plain( string $value ): string {
|
||||
return trim( html_entity_decode( wp_strip_all_tags( $value ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ) );
|
||||
}
|
||||
|
||||
public static function format( string $price ): string {
|
||||
$price = self::plain( $price );
|
||||
$digits = preg_replace( '/[^\d]/', '', $price );
|
||||
if ( '' === $price || $digits !== preg_replace( '/\s/', '', $price ) ) {
|
||||
return $price;
|
||||
}
|
||||
return sprintf( __( 'от %s zł', 'smilia-core' ), number_format_i18n( (int) $digits ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,946 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
use WP_Post;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Schema {
|
||||
|
||||
private const DAYS = array( 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' );
|
||||
|
||||
private const LANGUAGE_NAMES = array(
|
||||
'pl' => 'Polski',
|
||||
'en' => 'English',
|
||||
'ru' => 'Русский',
|
||||
);
|
||||
|
||||
private const SOURCES = array(
|
||||
'google' => 'Google',
|
||||
'booksy' => 'Booksy',
|
||||
'znanylekarz' => 'ZnanyLekarz',
|
||||
);
|
||||
|
||||
private const NEW_PATIENTS = array(
|
||||
'pl' => 'Nowi pacjenci',
|
||||
'en' => 'New patients',
|
||||
'ru' => 'Новые пациенты',
|
||||
);
|
||||
|
||||
private static ?array $price_span = null;
|
||||
|
||||
private static ?array $price_groups = null;
|
||||
|
||||
public static function graph( WP_Post $post ): array {
|
||||
$home = home_url( '/' );
|
||||
$url = (string) wp_get_canonical_url( $post );
|
||||
$trail = Markup::trail( $post );
|
||||
$reviews = self::reviews( $post, $home );
|
||||
$catalog = self::price_catalog( $post, $url );
|
||||
|
||||
$service = self::service( $post, $home, $url );
|
||||
$procedure = self::procedure( $post, $url );
|
||||
$person = self::doctor( $post, $home, $url );
|
||||
|
||||
$nodes = array(
|
||||
self::page( $post, $home, $url, array(
|
||||
'service' => $service,
|
||||
'procedure' => $procedure,
|
||||
'person' => $person,
|
||||
'catalog' => $catalog,
|
||||
'board' => $reviews['board'],
|
||||
) ),
|
||||
);
|
||||
foreach ( array( $person, $service, $procedure ) as $extra ) {
|
||||
if ( $extra ) {
|
||||
$nodes[] = $extra;
|
||||
}
|
||||
}
|
||||
$nodes[] = self::clinic( $home, $reviews['nodes'], $catalog ? array( '@id' => $catalog['@id'] ) : array() );
|
||||
$faq = self::faq( $post, $url );
|
||||
if ( $faq ) {
|
||||
$nodes[] = $faq;
|
||||
}
|
||||
$others = self::other_doctors( $post, $url );
|
||||
if ( $others ) {
|
||||
$nodes[] = $others;
|
||||
}
|
||||
if ( count( $trail ) > 1 ) {
|
||||
$nodes[] = self::breadcrumb( $url, $trail );
|
||||
}
|
||||
$nodes[] = self::website( $home );
|
||||
|
||||
return array(
|
||||
'@context' => 'https://schema.org',
|
||||
'@graph' => $nodes,
|
||||
);
|
||||
}
|
||||
|
||||
private static function website( string $home ): array {
|
||||
$node = array(
|
||||
'@type' => 'WebSite',
|
||||
'@id' => $home . '#website',
|
||||
'url' => $home,
|
||||
'name' => get_bloginfo( 'name' ),
|
||||
'inLanguage' => self::locales(),
|
||||
);
|
||||
$description = get_bloginfo( 'description' );
|
||||
if ( '' !== $description ) {
|
||||
$node['description'] = $description;
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
|
||||
private static function page( WP_Post $post, string $home, string $url, array $parts ): array {
|
||||
$page = array(
|
||||
'@type' => 'WebPage',
|
||||
'@id' => $url . '#webpage',
|
||||
'url' => $url,
|
||||
'name' => wp_get_document_title(),
|
||||
'inLanguage' => get_bloginfo( 'language' ),
|
||||
'datePublished' => get_post_time( DATE_W3C, true, $post ),
|
||||
'dateModified' => get_post_modified_time( DATE_W3C, true, $post ),
|
||||
);
|
||||
$description = Seo::description( $post );
|
||||
if ( '' !== $description ) {
|
||||
$page['description'] = $description;
|
||||
}
|
||||
$image = self::post_image( $post );
|
||||
if ( $image ) {
|
||||
$page['primaryImageOfPage'] = $image;
|
||||
$page['image'] = $image;
|
||||
}
|
||||
$translations = self::translations( $post );
|
||||
if ( $translations ) {
|
||||
$page['workTranslation'] = $translations;
|
||||
}
|
||||
if ( $parts['person'] ) {
|
||||
$page['@type'] = 'ProfilePage';
|
||||
$page['mainEntity'] = array( '@id' => $parts['person']['@id'] );
|
||||
} elseif ( $parts['service'] ) {
|
||||
$page['@type'] = 'MedicalWebPage';
|
||||
$page['specialty'] = array( 'https://schema.org/Dentistry', 'https://schema.org/Pediatric' );
|
||||
$page['mainEntity'] = array( '@id' => $parts['service']['@id'] );
|
||||
if ( $parts['procedure'] ) {
|
||||
$page['about'] = array( '@id' => $parts['procedure']['@id'] );
|
||||
}
|
||||
} elseif ( has_block( 'smilia/contacts-hero', $post ) ) {
|
||||
$page['@type'] = 'ContactPage';
|
||||
$page['mainEntity'] = array( '@id' => $home . '#clinic' );
|
||||
} elseif ( $parts['catalog'] ) {
|
||||
$page['@type'] = 'CollectionPage';
|
||||
$page['mainEntity'] = $parts['catalog'];
|
||||
} elseif ( $parts['board'] ) {
|
||||
$page['@type'] = 'CollectionPage';
|
||||
$page['mainEntity'] = $parts['board'];
|
||||
} elseif ( has_block( 'smilia/prepare', $post ) ) {
|
||||
$page['@type'] = 'MedicalWebPage';
|
||||
$page['audience'] = array(
|
||||
'@type' => 'PeopleAudience',
|
||||
'audienceType' => self::NEW_PATIENTS[ self::language() ] ?? self::NEW_PATIENTS['en'],
|
||||
);
|
||||
} elseif ( (int) $post->ID === self::front_id() ) {
|
||||
$page['mainEntity'] = array( '@id' => $home . '#clinic' );
|
||||
}
|
||||
return $page;
|
||||
}
|
||||
|
||||
private static function language(): string {
|
||||
return function_exists( 'pll_current_language' ) ? (string) pll_current_language() : '';
|
||||
}
|
||||
|
||||
private static function front_id(): int {
|
||||
$front = (int) get_option( 'page_on_front' );
|
||||
if ( $front && function_exists( 'pll_get_post' ) ) {
|
||||
$front = (int) pll_get_post( $front ) ?: $front;
|
||||
}
|
||||
return $front;
|
||||
}
|
||||
|
||||
private static function breadcrumb( string $url, array $trail ): array {
|
||||
return array(
|
||||
'@type' => 'BreadcrumbList',
|
||||
'@id' => $url . '#breadcrumb',
|
||||
'itemListElement' => array_map(
|
||||
static fn( array $crumb, int $index ): array => array(
|
||||
'@type' => 'ListItem',
|
||||
'position' => $index + 1,
|
||||
'name' => $crumb['title'],
|
||||
'item' => $crumb['url'],
|
||||
),
|
||||
$trail,
|
||||
array_keys( $trail )
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
private static function clinic( string $home, array $reviews, array $catalog ): array {
|
||||
$clinic = array(
|
||||
'@type' => 'Dentist',
|
||||
'@id' => $home . '#clinic',
|
||||
'name' => get_bloginfo( 'name' ),
|
||||
'url' => $home,
|
||||
'logo' => self::logo(),
|
||||
);
|
||||
$description = get_bloginfo( 'description' );
|
||||
if ( '' !== $description ) {
|
||||
$clinic['description'] = $description;
|
||||
}
|
||||
$images = self::clinic_images();
|
||||
if ( $images ) {
|
||||
$clinic['image'] = 1 === count( $images ) ? $images[0] : $images;
|
||||
}
|
||||
$phone = Settings::get( 'phone_main' );
|
||||
if ( '' !== $phone ) {
|
||||
$clinic['telephone'] = $phone;
|
||||
}
|
||||
$email = Settings::get( 'email' );
|
||||
if ( '' !== $email ) {
|
||||
$clinic['email'] = $email;
|
||||
}
|
||||
$address = self::address( Settings::get( 'address' ) );
|
||||
if ( $address ) {
|
||||
$clinic['address'] = $address;
|
||||
$clinic['hasMap'] = add_query_arg(
|
||||
array(
|
||||
'api' => 1,
|
||||
'query' => rawurlencode( Settings::get( 'address' ) ),
|
||||
),
|
||||
'https://www.google.com/maps/search/'
|
||||
);
|
||||
if ( isset( $address['addressLocality'] ) ) {
|
||||
$clinic['areaServed'] = array(
|
||||
'@type' => 'City',
|
||||
'name' => $address['addressLocality'],
|
||||
);
|
||||
}
|
||||
}
|
||||
$geo = self::geo();
|
||||
if ( $geo ) {
|
||||
$clinic['geo'] = $geo;
|
||||
}
|
||||
$points = array();
|
||||
if ( '' !== $phone ) {
|
||||
$points[] = array(
|
||||
'@type' => 'ContactPoint',
|
||||
'contactType' => 'customer service',
|
||||
'telephone' => $phone,
|
||||
'availableLanguage' => self::languages(),
|
||||
);
|
||||
}
|
||||
$whatsapp = Settings::get( 'whatsapp' );
|
||||
if ( '' !== $whatsapp ) {
|
||||
$points[] = array(
|
||||
'@type' => 'ContactPoint',
|
||||
'contactType' => 'customer service',
|
||||
'telephone' => $whatsapp,
|
||||
'url' => 'https://wa.me/' . preg_replace( '/\D/', '', $whatsapp ),
|
||||
'availableLanguage' => self::languages(),
|
||||
);
|
||||
}
|
||||
if ( $points ) {
|
||||
$clinic['contactPoint'] = $points;
|
||||
}
|
||||
$hours = self::opening_hours();
|
||||
if ( $hours ) {
|
||||
$clinic['openingHoursSpecification'] = $hours;
|
||||
}
|
||||
$profiles = array_values( Settings::profiles() );
|
||||
if ( $profiles ) {
|
||||
$clinic['sameAs'] = $profiles;
|
||||
}
|
||||
$span = self::price_span();
|
||||
if ( $span ) {
|
||||
$clinic['priceRange'] = $span['min'] . '-' . $span['max'] . ' PLN';
|
||||
$clinic['currenciesAccepted'] = 'PLN';
|
||||
}
|
||||
if ( $reviews ) {
|
||||
$clinic['review'] = $reviews;
|
||||
}
|
||||
$clinic['medicalSpecialty'] = array( 'https://schema.org/Dentistry', 'https://schema.org/Pediatric' );
|
||||
$clinic['isAcceptingNewPatients'] = true;
|
||||
$clinic['knowsLanguage'] = self::languages();
|
||||
|
||||
$services = array();
|
||||
foreach ( Content::services() as $service ) {
|
||||
$services[] = array(
|
||||
'@type' => 'Service',
|
||||
'@id' => $service['url'] . '#service',
|
||||
'name' => $service['title'],
|
||||
'url' => $service['url'],
|
||||
);
|
||||
}
|
||||
if ( $catalog ) {
|
||||
$clinic['hasOfferCatalog'] = $catalog;
|
||||
} elseif ( $services ) {
|
||||
$clinic['hasOfferCatalog'] = array(
|
||||
'@type' => 'OfferCatalog',
|
||||
'name' => get_bloginfo( 'name' ),
|
||||
'itemListElement' => $services,
|
||||
);
|
||||
}
|
||||
$employees = array();
|
||||
foreach ( Content::doctors() as $doctor ) {
|
||||
$employees[] = array(
|
||||
'@type' => self::person_types( (int) $doctor['id'] ),
|
||||
'@id' => $doctor['url'] . '#person',
|
||||
'name' => $doctor['name'],
|
||||
'url' => $doctor['url'],
|
||||
'jobTitle' => $doctor['position'],
|
||||
);
|
||||
}
|
||||
if ( $employees ) {
|
||||
$clinic['employee'] = $employees;
|
||||
}
|
||||
return $clinic;
|
||||
}
|
||||
|
||||
private static function logo(): array|string {
|
||||
$icon = (int) get_option( 'site_icon' );
|
||||
$src = $icon > 0 ? wp_get_attachment_image_src( $icon, 'full' ) : false;
|
||||
if ( ! $src ) {
|
||||
return get_theme_file_uri( 'assets/img/smilia-mark.svg' );
|
||||
}
|
||||
return array(
|
||||
'@type' => 'ImageObject',
|
||||
'url' => $src[0],
|
||||
'width' => (int) $src[1],
|
||||
'height' => (int) $src[2],
|
||||
);
|
||||
}
|
||||
|
||||
private static function clinic_images(): array {
|
||||
$front = self::front_id();
|
||||
if ( $front <= 0 ) {
|
||||
return array();
|
||||
}
|
||||
$ids = array( (int) get_post_thumbnail_id( $front ) );
|
||||
$queue = parse_blocks( (string) get_post_field( 'post_content', $front ) );
|
||||
while ( $queue ) {
|
||||
$block = array_shift( $queue );
|
||||
if ( 'smilia/office-photo' === $block['blockName'] ) {
|
||||
$ids[] = (int) ( $block['attrs']['imageId'] ?? 0 );
|
||||
}
|
||||
$queue = array_merge( $queue, $block['innerBlocks'] );
|
||||
}
|
||||
$images = array();
|
||||
foreach ( array_unique( array_filter( $ids ) ) as $id ) {
|
||||
$image = self::attachment( $id, Seo::IMAGE_SIZE );
|
||||
if ( $image ) {
|
||||
$images[] = $image;
|
||||
}
|
||||
}
|
||||
return $images;
|
||||
}
|
||||
|
||||
private static function geo(): array {
|
||||
if ( ! preg_match( '/^\s*(-?\d{1,2}(?:\.\d+)?)\s*,\s*(-?\d{1,3}(?:\.\d+)?)\s*$/', Settings::raw( 'geo' ), $match ) ) {
|
||||
return array();
|
||||
}
|
||||
return array(
|
||||
'@type' => 'GeoCoordinates',
|
||||
'latitude' => (float) $match[1],
|
||||
'longitude' => (float) $match[2],
|
||||
);
|
||||
}
|
||||
|
||||
private static function post_image( WP_Post $post ): ?array {
|
||||
$id = (int) get_post_thumbnail_id( $post );
|
||||
return $id > 0 ? self::attachment( $id, Seo::IMAGE_SIZE ) : null;
|
||||
}
|
||||
|
||||
private static function attachment( int $id, string $size ): ?array {
|
||||
$src = wp_get_attachment_image_src( $id, $size );
|
||||
if ( ! $src ) {
|
||||
return null;
|
||||
}
|
||||
$node = array(
|
||||
'@type' => 'ImageObject',
|
||||
'url' => $src[0],
|
||||
'contentUrl' => $src[0],
|
||||
'width' => (int) $src[1],
|
||||
'height' => (int) $src[2],
|
||||
);
|
||||
$alt = trim( (string) get_post_meta( $id, '_wp_attachment_image_alt', true ) );
|
||||
if ( '' !== $alt ) {
|
||||
$node['caption'] = $alt;
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
|
||||
private static function address( string $address ): array {
|
||||
if ( '' === $address ) {
|
||||
return array();
|
||||
}
|
||||
$result = array(
|
||||
'@type' => 'PostalAddress',
|
||||
'streetAddress' => $address,
|
||||
'addressCountry' => 'PL',
|
||||
);
|
||||
if ( preg_match( '/^(.+),\s*(\d{2}-\d{3})\s+(.+)$/u', $address, $parts ) ) {
|
||||
$result['streetAddress'] = trim( $parts[1] );
|
||||
$result['postalCode'] = $parts[2];
|
||||
$result['addressLocality'] = trim( $parts[3] );
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function opening_hours(): array {
|
||||
$rows = Settings::hours();
|
||||
if ( count( $rows ) !== count( self::DAYS ) ) {
|
||||
return array();
|
||||
}
|
||||
$result = array();
|
||||
foreach ( $rows as $index => $row ) {
|
||||
if ( ! preg_match( '/(\d{1,2}:\d{2})\s*[–-]\s*(\d{1,2}:\d{2})/u', $row['time'], $time ) ) {
|
||||
continue;
|
||||
}
|
||||
$result[] = array(
|
||||
'@type' => 'OpeningHoursSpecification',
|
||||
'dayOfWeek' => 'https://schema.org/' . self::DAYS[ $index ],
|
||||
'opens' => str_pad( $time[1], 5, '0', STR_PAD_LEFT ),
|
||||
'closes' => str_pad( $time[2], 5, '0', STR_PAD_LEFT ),
|
||||
);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function locales(): array {
|
||||
if ( ! function_exists( 'pll_languages_list' ) ) {
|
||||
return array( get_bloginfo( 'language' ) );
|
||||
}
|
||||
return array_values( array_map( static fn( string $locale ): string => str_replace( '_', '-', $locale ), (array) pll_languages_list( array( 'fields' => 'locale' ) ) ) );
|
||||
}
|
||||
|
||||
private static function languages(): array {
|
||||
$slugs = function_exists( 'pll_languages_list' ) ? (array) pll_languages_list( array( 'fields' => 'slug' ) ) : array();
|
||||
$list = array();
|
||||
foreach ( $slugs as $slug ) {
|
||||
$list[] = array(
|
||||
'@type' => 'Language',
|
||||
'name' => self::LANGUAGE_NAMES[ $slug ] ?? $slug,
|
||||
'alternateName' => $slug,
|
||||
);
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
private static function translations( WP_Post $post ): array {
|
||||
if ( ! function_exists( 'pll_languages_list' ) || ! function_exists( 'pll_get_post' ) ) {
|
||||
return array();
|
||||
}
|
||||
$current = function_exists( 'pll_get_post_language' ) ? (string) pll_get_post_language( $post->ID ) : '';
|
||||
$result = array();
|
||||
foreach ( (array) pll_languages_list( array( 'fields' => 'slug' ) ) as $slug ) {
|
||||
if ( $slug === $current ) {
|
||||
continue;
|
||||
}
|
||||
$other = (int) pll_get_post( $post->ID, $slug );
|
||||
if ( $other <= 0 ) {
|
||||
continue;
|
||||
}
|
||||
$link = get_permalink( $other );
|
||||
$result[] = array(
|
||||
'@type' => 'WebPage',
|
||||
'@id' => $link . '#webpage',
|
||||
'url' => $link,
|
||||
'inLanguage' => str_replace( '_', '-', (string) pll_get_post_language( $other, 'locale' ) ),
|
||||
);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function block_attributes( WP_Post $post, string $name ): array {
|
||||
foreach ( parse_blocks( $post->post_content ) as $block ) {
|
||||
if ( $name === $block['blockName'] ) {
|
||||
return (array) $block['attrs'];
|
||||
}
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
private static function lines( string $value ): array {
|
||||
return array_values( array_filter( array_map( 'trim', (array) preg_split( '/\R/u', wp_strip_all_tags( $value ) ) ) ) );
|
||||
}
|
||||
|
||||
private static function service( WP_Post $post, string $home, string $url ): array {
|
||||
if ( Post_Types::SERVICE !== $post->post_type ) {
|
||||
return array();
|
||||
}
|
||||
$attributes = self::block_attributes( $post, 'smilia/service-page' );
|
||||
$offers = array();
|
||||
$amounts = array();
|
||||
foreach ( (array) ( $attributes['methods'] ?? array() ) as $method ) {
|
||||
$price = (string) preg_replace( '/[\s\x{a0}]+/u', '', (string) ( $method['price'] ?? '' ) );
|
||||
if ( ! preg_match( '/\d+(?:[.,]\d+)?/', $price, $found ) ) {
|
||||
continue;
|
||||
}
|
||||
$amount = (float) str_replace( ',', '.', $found[0] );
|
||||
$amounts[] = $amount;
|
||||
$offers[] = array(
|
||||
'@type' => 'Offer',
|
||||
'name' => wp_strip_all_tags( (string) ( $method['title'] ?? '' ) ),
|
||||
'priceSpecification' => array(
|
||||
'@type' => 'PriceSpecification',
|
||||
'minPrice' => $amount,
|
||||
'priceCurrency' => 'PLN',
|
||||
),
|
||||
);
|
||||
}
|
||||
if ( ! $offers ) {
|
||||
$link = get_permalink( $post );
|
||||
foreach ( self::price_groups() as $group ) {
|
||||
if ( $link !== $group['url'] ) {
|
||||
continue;
|
||||
}
|
||||
foreach ( $group['items'] as $item ) {
|
||||
if ( null === $item['amount'] ) {
|
||||
continue;
|
||||
}
|
||||
$amounts[] = $item['amount'];
|
||||
$offers[] = array(
|
||||
'@type' => 'Offer',
|
||||
'name' => $item['name'],
|
||||
'priceSpecification' => array(
|
||||
'@type' => 'PriceSpecification',
|
||||
'minPrice' => $item['amount'],
|
||||
'priceCurrency' => 'PLN',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
$title = get_the_title( $post );
|
||||
$node = array(
|
||||
'@type' => 'Service',
|
||||
'@id' => $url . '#service',
|
||||
'name' => $title,
|
||||
'serviceType' => $title,
|
||||
'url' => $url,
|
||||
'provider' => array( '@id' => $home . '#clinic' ),
|
||||
'audience' => array(
|
||||
'@type' => 'PeopleAudience',
|
||||
'audienceType' => self::audience_label(),
|
||||
),
|
||||
'availableChannel' => array(
|
||||
'@type' => 'ServiceChannel',
|
||||
'serviceUrl' => $url,
|
||||
'availableLanguage' => self::languages(),
|
||||
),
|
||||
);
|
||||
$phone = Settings::get( 'phone_main' );
|
||||
if ( '' !== $phone ) {
|
||||
$node['availableChannel']['servicePhone'] = array(
|
||||
'@type' => 'ContactPoint',
|
||||
'telephone' => $phone,
|
||||
'contactType' => 'customer service',
|
||||
);
|
||||
}
|
||||
$city = self::address( Settings::get( 'address' ) )['addressLocality'] ?? '';
|
||||
if ( '' !== $city ) {
|
||||
$node['areaServed'] = array(
|
||||
'@type' => 'City',
|
||||
'name' => $city,
|
||||
);
|
||||
}
|
||||
$description = Seo::description( $post );
|
||||
if ( '' !== $description ) {
|
||||
$node['description'] = $description;
|
||||
}
|
||||
$image = self::post_image( $post );
|
||||
if ( $image ) {
|
||||
$node['image'] = $image;
|
||||
}
|
||||
if ( 1 === count( $offers ) ) {
|
||||
$node['offers'] = $offers[0];
|
||||
} elseif ( $offers ) {
|
||||
$node['hasOfferCatalog'] = array(
|
||||
'@type' => 'OfferCatalog',
|
||||
'name' => $title,
|
||||
'itemListElement' => $offers,
|
||||
);
|
||||
$node['offers'] = array(
|
||||
'@type' => 'AggregateOffer',
|
||||
'lowPrice' => min( $amounts ),
|
||||
'highPrice' => max( $amounts ),
|
||||
'priceCurrency' => 'PLN',
|
||||
'offerCount' => count( $offers ),
|
||||
);
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
|
||||
private static function audience_label(): string {
|
||||
return array(
|
||||
'pl' => 'Dzieci',
|
||||
'en' => 'Children',
|
||||
'ru' => 'Дети',
|
||||
)[ self::language() ] ?? 'Children';
|
||||
}
|
||||
|
||||
private static function procedure( WP_Post $post, string $url ): array {
|
||||
if ( Post_Types::SERVICE !== $post->post_type ) {
|
||||
return array();
|
||||
}
|
||||
$attributes = self::block_attributes( $post, 'smilia/service-page' );
|
||||
$steps = array();
|
||||
foreach ( (array) ( $attributes['steps'] ?? array() ) as $index => $step ) {
|
||||
if ( ! is_array( $step ) ) {
|
||||
continue;
|
||||
}
|
||||
$line = trim( wp_strip_all_tags( (string) ( $step['title'] ?? '' ) ) . '. ' . wp_strip_all_tags( (string) ( $step['text'] ?? '' ) ) );
|
||||
if ( '' !== trim( $line, '. ' ) ) {
|
||||
$steps[] = ( $index + 1 ) . '. ' . $line;
|
||||
}
|
||||
}
|
||||
$intro = trim( wp_strip_all_tags( (string) ( $attributes['intro'] ?? '' ) ) );
|
||||
if ( ! $steps && '' === $intro ) {
|
||||
return array();
|
||||
}
|
||||
$node = array(
|
||||
'@type' => 'MedicalProcedure',
|
||||
'@id' => $url . '#procedure',
|
||||
'name' => get_the_title( $post ),
|
||||
'url' => $url,
|
||||
'relevantSpecialty' => array( 'https://schema.org/Dentistry', 'https://schema.org/Pediatric' ),
|
||||
);
|
||||
if ( '' !== $intro ) {
|
||||
$node['description'] = $intro;
|
||||
}
|
||||
if ( $steps ) {
|
||||
$node['howPerformed'] = implode( ' ', $steps );
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
|
||||
private static function doctor( WP_Post $post, string $home, string $url ): array {
|
||||
if ( Post_Types::DOCTOR !== $post->post_type ) {
|
||||
return array();
|
||||
}
|
||||
$name = get_the_title( $post );
|
||||
$position = (string) get_post_meta( $post->ID, Meta_Fields::POSITION, true );
|
||||
$node = array(
|
||||
'@type' => self::person_types( $post->ID ),
|
||||
'@id' => $url . '#person',
|
||||
'name' => $name,
|
||||
'url' => $url,
|
||||
'jobTitle' => $position,
|
||||
'worksFor' => array( '@id' => $home . '#clinic' ),
|
||||
);
|
||||
$parts = preg_split( '/\s+/u', trim( $name ), 2 );
|
||||
if ( 2 === count( $parts ) ) {
|
||||
$node['givenName'] = $parts[0];
|
||||
$node['familyName'] = $parts[1];
|
||||
}
|
||||
$description = Seo::description( $post );
|
||||
if ( '' !== $description ) {
|
||||
$node['description'] = $description;
|
||||
}
|
||||
$thumb = (int) get_post_thumbnail_id( $post );
|
||||
$photo = $thumb > 0 ? self::attachment( $thumb, 'full' ) : null;
|
||||
if ( $photo ) {
|
||||
$node['image'] = $photo;
|
||||
}
|
||||
if ( is_array( $node['@type'] ) ) {
|
||||
$node['practicesAt'] = array( '@id' => $home . '#clinic' );
|
||||
$node['medicalSpecialty'] = array( 'https://schema.org/Dentistry', 'https://schema.org/Pediatric' );
|
||||
$node['isAcceptingNewPatients'] = true;
|
||||
}
|
||||
$education = self::lines( (string) get_post_meta( $post->ID, Meta_Fields::EDUCATION, true ) );
|
||||
if ( $education ) {
|
||||
$node['alumniOf'] = array_map(
|
||||
static fn( string $school ): array => array(
|
||||
'@type' => 'CollegeOrUniversity',
|
||||
'name' => $school,
|
||||
),
|
||||
$education
|
||||
);
|
||||
}
|
||||
$credentials = self::lines( (string) get_post_meta( $post->ID, Meta_Fields::CREDENTIALS, true ) );
|
||||
if ( $credentials ) {
|
||||
$node['hasCredential'] = array_map(
|
||||
static fn( string $credential ): array => array(
|
||||
'@type' => 'EducationalOccupationalCredential',
|
||||
'name' => $credential,
|
||||
),
|
||||
$credentials
|
||||
);
|
||||
}
|
||||
$topics = self::lines( (string) get_post_meta( $post->ID, Meta_Fields::TOPICS, true ) );
|
||||
if ( $topics ) {
|
||||
$node['knowsAbout'] = $topics;
|
||||
}
|
||||
$city = self::address( Settings::get( 'address' ) )['addressLocality'] ?? '';
|
||||
$experience = trim( wp_strip_all_tags( (string) ( self::block_attributes( $post, 'smilia/doctor-page' )['experience'] ?? '' ) ) );
|
||||
if ( '' !== $position ) {
|
||||
$occupation = array(
|
||||
'@type' => 'Occupation',
|
||||
'name' => $position,
|
||||
);
|
||||
if ( '' !== $experience ) {
|
||||
$occupation['description'] = $experience;
|
||||
}
|
||||
if ( '' !== $city ) {
|
||||
$occupation['occupationLocation'] = array(
|
||||
'@type' => 'City',
|
||||
'name' => $city,
|
||||
);
|
||||
}
|
||||
$node['hasOccupation'] = $occupation;
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
|
||||
private static function person_types( int $post_id ): array|string {
|
||||
return 'person' === (string) get_post_meta( $post_id, Meta_Fields::ROLE, true ) ? 'Person' : array( 'Person', 'IndividualPhysician' );
|
||||
}
|
||||
|
||||
private static function other_doctors( WP_Post $post, string $url ): array {
|
||||
if ( Post_Types::DOCTOR !== $post->post_type ) {
|
||||
return array();
|
||||
}
|
||||
$items = array();
|
||||
foreach ( Content::doctors() as $doctor ) {
|
||||
if ( (int) $doctor['id'] === (int) $post->ID ) {
|
||||
continue;
|
||||
}
|
||||
$items[] = array(
|
||||
'@type' => 'ListItem',
|
||||
'position' => count( $items ) + 1,
|
||||
'name' => $doctor['name'],
|
||||
'url' => $doctor['url'],
|
||||
);
|
||||
}
|
||||
if ( ! $items ) {
|
||||
return array();
|
||||
}
|
||||
$title = trim( wp_strip_all_tags( (string) ( self::block_attributes( $post, 'smilia/doctor-page' )['othersTitle'] ?? '' ) ) );
|
||||
$list = array(
|
||||
'@type' => 'ItemList',
|
||||
'@id' => $url . '#doctors',
|
||||
'itemListElement' => $items,
|
||||
);
|
||||
if ( '' !== $title ) {
|
||||
$list['name'] = $title;
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
private static function faq( WP_Post $post, string $url ): array {
|
||||
$questions = array();
|
||||
$title = '';
|
||||
foreach ( parse_blocks( $post->post_content ) as $block ) {
|
||||
if ( 'smilia/faq' !== $block['blockName'] ) {
|
||||
continue;
|
||||
}
|
||||
if ( '' === $title ) {
|
||||
$type = \WP_Block_Type_Registry::get_instance()->get_registered( 'smilia/faq' );
|
||||
$attrs = $type ? $type->prepare_attributes_for_render( (array) $block['attrs'] ) : (array) $block['attrs'];
|
||||
$title = trim( preg_replace( '/\s+/u', ' ', wp_strip_all_tags( str_replace( '<br>', ' ', (string) ( $attrs['title'] ?? '' ) ) ) ) );
|
||||
}
|
||||
$questions = array_merge( $questions, self::questions( $block ) );
|
||||
}
|
||||
if ( ! $questions ) {
|
||||
return array();
|
||||
}
|
||||
$node = array(
|
||||
'@type' => 'FAQPage',
|
||||
'@id' => $url . '#faq',
|
||||
'inLanguage' => get_bloginfo( 'language' ),
|
||||
'mainEntity' => $questions,
|
||||
);
|
||||
if ( '' !== $title ) {
|
||||
$node['name'] = $title;
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
|
||||
private static function questions( array $block ): array {
|
||||
return array_map(
|
||||
static fn( array $item ): array => array(
|
||||
'@type' => 'Question',
|
||||
'name' => $item['question'],
|
||||
'acceptedAnswer' => array(
|
||||
'@type' => 'Answer',
|
||||
'text' => trim( wp_strip_all_tags( preg_replace( '#<a(\s[^>]*)?>.*?</a>#is', '', $item['answer'] ) ) ),
|
||||
),
|
||||
),
|
||||
Content::faq( (string) ( $block['attrs']['group'] ?? 'home' ) )
|
||||
);
|
||||
}
|
||||
|
||||
private static function reviews( WP_Post $post, string $home ): array {
|
||||
$empty = array(
|
||||
'board' => array(),
|
||||
'nodes' => array(),
|
||||
);
|
||||
$board = has_block( 'smilia/reviews-board', $post );
|
||||
$limit = $board ? 100 : ( has_block( 'smilia/reviews', $post ) ? 3 : 0 );
|
||||
if ( $limit <= 0 ) {
|
||||
return $empty;
|
||||
}
|
||||
$nodes = array();
|
||||
foreach ( Content::reviews( $limit ) as $review ) {
|
||||
$node = array(
|
||||
'@type' => 'Review',
|
||||
'@id' => $home . '#review-' . (int) $review['id'],
|
||||
'author' => array(
|
||||
'@type' => 'Person',
|
||||
'name' => $review['author'],
|
||||
),
|
||||
'reviewBody' => $review['text'],
|
||||
'datePublished' => get_post_time( DATE_W3C, true, (int) $review['id'] ),
|
||||
'inLanguage' => get_bloginfo( 'language' ),
|
||||
);
|
||||
if ( '' !== $review['topic'] ) {
|
||||
$node['name'] = $review['topic'];
|
||||
}
|
||||
if ( isset( self::SOURCES[ $review['source'] ] ) ) {
|
||||
$publisher = array(
|
||||
'@type' => 'Organization',
|
||||
'name' => self::SOURCES[ $review['source'] ],
|
||||
);
|
||||
$link = Settings::get( 'review_' . $review['source'] );
|
||||
if ( '' !== $link ) {
|
||||
$publisher['url'] = $link;
|
||||
}
|
||||
$node['publisher'] = $publisher;
|
||||
}
|
||||
$nodes[] = $node;
|
||||
}
|
||||
if ( ! $nodes ) {
|
||||
return $empty;
|
||||
}
|
||||
if ( ! $board ) {
|
||||
return array(
|
||||
'board' => array(),
|
||||
'nodes' => $nodes,
|
||||
);
|
||||
}
|
||||
return array(
|
||||
'board' => array(
|
||||
'@type' => 'ItemList',
|
||||
'name' => get_the_title( $post ),
|
||||
'itemListElement' => array_map(
|
||||
static fn( array $node, int $index ): array => array(
|
||||
'@type' => 'ListItem',
|
||||
'position' => $index + 1,
|
||||
'item' => array( '@id' => $node['@id'] ),
|
||||
),
|
||||
$nodes,
|
||||
array_keys( $nodes )
|
||||
),
|
||||
),
|
||||
'nodes' => $nodes,
|
||||
);
|
||||
}
|
||||
|
||||
private static function price_catalog( WP_Post $post, string $url ): array {
|
||||
$attributes = self::block_attributes( $post, 'smilia/prices' );
|
||||
if ( ! $attributes ) {
|
||||
return array();
|
||||
}
|
||||
$sections = array();
|
||||
foreach ( Prices::groups( (array) ( $attributes['groups'] ?? array() ) ) as $group ) {
|
||||
$offers = array();
|
||||
foreach ( $group['items'] as $item ) {
|
||||
if ( null === $item['amount'] ) {
|
||||
continue;
|
||||
}
|
||||
$offer = array(
|
||||
'@type' => 'Offer',
|
||||
'name' => $item['name'],
|
||||
'priceSpecification' => array(
|
||||
'@type' => 'PriceSpecification',
|
||||
'minPrice' => $item['amount'],
|
||||
'priceCurrency' => 'PLN',
|
||||
),
|
||||
);
|
||||
if ( '' !== $group['url'] ) {
|
||||
$offer['itemOffered'] = array(
|
||||
'@type' => 'Service',
|
||||
'@id' => $group['url'] . '#service',
|
||||
'name' => $group['title'],
|
||||
'url' => $group['url'],
|
||||
);
|
||||
}
|
||||
$offers[] = $offer;
|
||||
}
|
||||
if ( ! $offers ) {
|
||||
continue;
|
||||
}
|
||||
$section = array(
|
||||
'@type' => 'OfferCatalog',
|
||||
'name' => $group['title'],
|
||||
'itemListElement' => $offers,
|
||||
);
|
||||
if ( '' !== $group['url'] ) {
|
||||
$section['url'] = $group['url'];
|
||||
}
|
||||
$sections[] = $section;
|
||||
}
|
||||
if ( ! $sections ) {
|
||||
return array();
|
||||
}
|
||||
return array(
|
||||
'@type' => 'OfferCatalog',
|
||||
'@id' => $url . '#catalog',
|
||||
'name' => get_the_title( $post ),
|
||||
'itemListElement' => $sections,
|
||||
);
|
||||
}
|
||||
|
||||
private static function price_groups(): array {
|
||||
if ( null !== self::$price_groups ) {
|
||||
return self::$price_groups;
|
||||
}
|
||||
$pages = get_posts(
|
||||
array(
|
||||
'post_type' => 'page',
|
||||
'name' => 'prices',
|
||||
'post_status' => 'publish',
|
||||
'numberposts' => 1,
|
||||
'lang' => self::language(),
|
||||
)
|
||||
);
|
||||
self::$price_groups = $pages ? Prices::groups( (array) ( self::block_attributes( $pages[0], 'smilia/prices' )['groups'] ?? array() ) ) : array();
|
||||
return self::$price_groups;
|
||||
}
|
||||
|
||||
private static function price_span(): ?array {
|
||||
if ( null !== self::$price_span ) {
|
||||
return self::$price_span ?: null;
|
||||
}
|
||||
self::$price_span = array();
|
||||
$pages = get_posts(
|
||||
array(
|
||||
'post_type' => 'page',
|
||||
'name' => 'prices',
|
||||
'post_status' => 'publish',
|
||||
'numberposts' => 1,
|
||||
'lang' => '',
|
||||
)
|
||||
);
|
||||
if ( ! $pages ) {
|
||||
return null;
|
||||
}
|
||||
$amounts = array();
|
||||
foreach ( (array) ( self::block_attributes( $pages[0], 'smilia/prices' )['groups'] ?? array() ) as $group ) {
|
||||
foreach ( Prices::groups( array( $group ) ) as $parsed ) {
|
||||
foreach ( $parsed['items'] as $item ) {
|
||||
if ( null !== $item['amount'] ) {
|
||||
$amounts[] = $item['amount'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( $amounts ) {
|
||||
self::$price_span = array(
|
||||
'min' => min( $amounts ),
|
||||
'max' => max( $amounts ),
|
||||
);
|
||||
}
|
||||
return self::$price_span ?: null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Seo {
|
||||
|
||||
public const IMAGE_SIZE = 'smilia-og';
|
||||
|
||||
public const SERVICE_PAGES = array( 'smilia' );
|
||||
|
||||
public function register(): void {
|
||||
add_action( 'init', array( $this, 'supports' ) );
|
||||
add_action( 'after_setup_theme', array( $this, 'image_size' ) );
|
||||
add_filter( 'document_title_separator', array( $this, 'separator' ) );
|
||||
add_filter( 'document_title_parts', array( $this, 'title_parts' ) );
|
||||
add_action( 'wp_head', array( $this, 'meta' ), 2 );
|
||||
add_action( 'wp_head', array( $this, 'schema' ), 99 );
|
||||
}
|
||||
|
||||
public function separator(): string {
|
||||
return '|';
|
||||
}
|
||||
|
||||
public function title_parts( array $parts ): array {
|
||||
$custom = is_singular( array( 'page', Post_Types::SERVICE, Post_Types::DOCTOR ) ) ? trim( (string) get_post_meta( (int) get_queried_object_id(), Meta_Fields::SEO_TITLE, true ) ) : '';
|
||||
if ( '' !== $custom ) {
|
||||
return array(
|
||||
'title' => $custom,
|
||||
'site' => get_bloginfo( 'name' ),
|
||||
);
|
||||
}
|
||||
if ( is_front_page() && ! empty( $parts['tagline'] ) ) {
|
||||
return array(
|
||||
'title' => $parts['tagline'],
|
||||
'site' => get_bloginfo( 'name' ),
|
||||
);
|
||||
}
|
||||
return $parts;
|
||||
}
|
||||
|
||||
public function supports(): void {
|
||||
add_post_type_support( 'page', 'excerpt' );
|
||||
}
|
||||
|
||||
public function image_size(): void {
|
||||
add_image_size( self::IMAGE_SIZE, 1200, 630, true );
|
||||
}
|
||||
|
||||
public static function is_service_page(): bool {
|
||||
return is_page() && in_array( (string) get_post_field( 'post_name', get_queried_object_id() ), self::SERVICE_PAGES, true );
|
||||
}
|
||||
|
||||
public function meta(): void {
|
||||
if ( ! is_singular() || self::is_service_page() ) {
|
||||
return;
|
||||
}
|
||||
$post = get_queried_object();
|
||||
$description = self::description( $post );
|
||||
$image = self::image( $post );
|
||||
$url = (string) wp_get_canonical_url( $post );
|
||||
|
||||
$tags = array(
|
||||
array( 'name', 'description', $description ),
|
||||
array( 'property', 'og:type', is_singular( 'post' ) ? 'article' : 'website' ),
|
||||
array( 'property', 'og:site_name', get_bloginfo( 'name' ) ),
|
||||
array( 'property', 'og:title', wp_get_document_title() ),
|
||||
array( 'property', 'og:description', $description ),
|
||||
array( 'property', 'og:url', $url ),
|
||||
array( 'property', 'og:locale', get_locale() ),
|
||||
);
|
||||
foreach ( self::alternate_locales() as $locale ) {
|
||||
$tags[] = array( 'property', 'og:locale:alternate', $locale );
|
||||
}
|
||||
if ( $image ) {
|
||||
$tags[] = array( 'property', 'og:image', $image[0] );
|
||||
$tags[] = array( 'property', 'og:image:width', (string) $image[1] );
|
||||
$tags[] = array( 'property', 'og:image:height', (string) $image[2] );
|
||||
}
|
||||
$tags[] = array( 'name', 'twitter:card', $image ? 'summary_large_image' : 'summary' );
|
||||
|
||||
foreach ( $tags as list( $attribute, $name, $content ) ) {
|
||||
if ( '' !== $content ) {
|
||||
printf( '<meta %s="%s" content="%s">' . "\n", esc_attr( $attribute ), esc_attr( $name ), esc_attr( $content ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function schema(): void {
|
||||
if ( ! is_singular() || self::is_service_page() ) {
|
||||
return;
|
||||
}
|
||||
printf(
|
||||
'<script type="application/ld+json">%s</script>' . "\n",
|
||||
wp_json_encode( Schema::graph( get_queried_object() ), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG )
|
||||
);
|
||||
}
|
||||
|
||||
public static function description( \WP_Post $post ): string {
|
||||
return trim( wp_strip_all_tags( has_excerpt( $post ) ? $post->post_excerpt : '' ) );
|
||||
}
|
||||
|
||||
private static function image( \WP_Post $post ): ?array {
|
||||
$id = (int) get_post_thumbnail_id( $post );
|
||||
if ( $id <= 0 ) {
|
||||
$front = (int) get_option( 'page_on_front' );
|
||||
if ( $front && function_exists( 'pll_get_post' ) ) {
|
||||
$front = (int) pll_get_post( $front ) ?: $front;
|
||||
}
|
||||
$id = $front > 0 ? (int) get_post_thumbnail_id( $front ) : 0;
|
||||
}
|
||||
if ( $id <= 0 ) {
|
||||
return null;
|
||||
}
|
||||
$image = wp_get_attachment_image_src( $id, self::IMAGE_SIZE );
|
||||
return $image ? array( $image[0], (int) $image[1], (int) $image[2] ) : null;
|
||||
}
|
||||
|
||||
private static function alternate_locales(): array {
|
||||
if ( ! function_exists( 'pll_languages_list' ) ) {
|
||||
return array();
|
||||
}
|
||||
return array_values( array_diff( (array) pll_languages_list( array( 'fields' => 'locale' ) ), array( get_locale() ) ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
<?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>';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
use SimpleXMLElement;
|
||||
use WP_Sitemaps_Renderer;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Sitemap_Renderer extends WP_Sitemaps_Renderer {
|
||||
|
||||
private const XHTML = 'http://www.w3.org/1999/xhtml';
|
||||
|
||||
public function get_sitemap_xml( $url_list ) {
|
||||
$urlset = new SimpleXMLElement(
|
||||
'<?xml version="1.0" encoding="UTF-8" ?>' . $this->stylesheet . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="' . self::XHTML . '" />'
|
||||
);
|
||||
foreach ( $url_list as $item ) {
|
||||
$url = $urlset->addChild( 'url' );
|
||||
$url->addChild( 'loc', esc_url( $item['loc'] ) );
|
||||
foreach ( array( 'lastmod', 'changefreq', 'priority' ) as $name ) {
|
||||
if ( isset( $item[ $name ] ) ) {
|
||||
$url->addChild( $name, esc_xml( (string) $item[ $name ] ) );
|
||||
}
|
||||
}
|
||||
foreach ( (array) ( $item['alternates'] ?? array() ) as $language => $href ) {
|
||||
$link = $url->addChild( 'xhtml:link', null, self::XHTML );
|
||||
$link->addAttribute( 'rel', 'alternate' );
|
||||
$link->addAttribute( 'hreflang', (string) $language );
|
||||
$link->addAttribute( 'href', esc_url( $href ) );
|
||||
}
|
||||
}
|
||||
return $urlset->asXML();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
use WP_Post;
|
||||
use WP_Sitemaps;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Sitemap {
|
||||
|
||||
public function register(): void {
|
||||
add_filter( 'wp_sitemaps_enabled', '__return_true' );
|
||||
add_filter( 'wp_sitemaps_add_provider', array( $this, 'provider' ), 10, 2 );
|
||||
add_filter( 'wp_sitemaps_post_types', array( $this, 'post_types' ) );
|
||||
add_filter( 'wp_sitemaps_posts_query_args', array( $this, 'query_args' ), 10, 2 );
|
||||
add_filter( 'wp_sitemaps_posts_entry', array( $this, 'entry' ), 10, 2 );
|
||||
add_action( 'wp_sitemaps_init', array( $this, 'renderer' ) );
|
||||
}
|
||||
|
||||
public function provider( $provider, string $name ) {
|
||||
return 'posts' === $name ? $provider : false;
|
||||
}
|
||||
|
||||
public function post_types( array $post_types ): array {
|
||||
unset( $post_types['post'] );
|
||||
return $post_types;
|
||||
}
|
||||
|
||||
public function query_args( array $args, string $post_type ): array {
|
||||
if ( 'page' !== $post_type ) {
|
||||
return $args;
|
||||
}
|
||||
$hidden = get_posts(
|
||||
array(
|
||||
'post_type' => 'page',
|
||||
'post_name__in' => Seo::SERVICE_PAGES,
|
||||
'post_status' => 'publish',
|
||||
'fields' => 'ids',
|
||||
'posts_per_page' => -1,
|
||||
'lang' => '',
|
||||
)
|
||||
);
|
||||
if ( $hidden ) {
|
||||
$args['post__not_in'] = array_merge( (array) ( $args['post__not_in'] ?? array() ), $hidden );
|
||||
}
|
||||
return $args;
|
||||
}
|
||||
|
||||
public function entry( array $entry, WP_Post $post ): array {
|
||||
if ( ! function_exists( 'pll_get_post_translations' ) || ! function_exists( 'pll_get_post_language' ) ) {
|
||||
return $entry;
|
||||
}
|
||||
$links = array();
|
||||
foreach ( (array) pll_get_post_translations( $post->ID ) as $id ) {
|
||||
if ( 'publish' !== get_post_status( (int) $id ) ) {
|
||||
continue;
|
||||
}
|
||||
$links[ (string) pll_get_post_language( (int) $id, 'slug' ) ] = (string) get_permalink( (int) $id );
|
||||
}
|
||||
if ( count( $links ) < 2 ) {
|
||||
return $entry;
|
||||
}
|
||||
if ( function_exists( 'pll_languages_list' ) ) {
|
||||
$order = array_flip( (array) pll_languages_list( array( 'fields' => 'slug' ) ) );
|
||||
uksort( $links, static fn( string $a, string $b ): int => ( $order[ $a ] ?? PHP_INT_MAX ) <=> ( $order[ $b ] ?? PHP_INT_MAX ) );
|
||||
}
|
||||
$default = function_exists( 'pll_default_language' ) ? (string) pll_default_language( 'slug' ) : '';
|
||||
if ( isset( $links[ $default ] ) ) {
|
||||
$links['x-default'] = $links[ $default ];
|
||||
}
|
||||
$entry['alternates'] = $links;
|
||||
return $entry;
|
||||
}
|
||||
|
||||
public function renderer( WP_Sitemaps $sitemaps ): void {
|
||||
$sitemaps->renderer = new Sitemap_Renderer();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user