127 lines
4.1 KiB
PHP
127 lines
4.1 KiB
PHP
<?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() ) ) );
|
|
}
|
|
}
|