$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( '' . "\n", esc_attr( $attribute ), esc_attr( $name ), esc_attr( $content ) );
}
}
}
public function schema(): void {
if ( ! is_singular() || self::is_service_page() ) {
return;
}
printf(
'' . "\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() ) ) );
}
}