169 lines
5.1 KiB
PHP
169 lines
5.1 KiB
PHP
<?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 );
|
|
}
|
|
}
|