110 lines
2.8 KiB
PHP
110 lines
2.8 KiB
PHP
<?php
|
|
/**
|
|
* Outputs <title> and <meta name="description"> tags in the <head>.
|
|
*
|
|
* @package S1_SEO_Meta
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
class S1_SEO_Meta_Output {
|
|
|
|
public function __construct() {
|
|
// Output meta tags as early as possible in wp_head.
|
|
add_action( 'wp_head', array( $this, 'output_meta_tags' ), 1 );
|
|
|
|
// Override the document <title> tag via wp_title (classic themes).
|
|
add_filter( 'wp_title', array( $this, 'filter_wp_title' ), 20, 1 );
|
|
|
|
// Override the document <title> tag via document_title_parts (block / modern themes).
|
|
add_filter( 'document_title_parts', array( $this, 'filter_document_title_parts' ), 20 );
|
|
}
|
|
|
|
/**
|
|
* Determine the SEO title and description for the current page and echo them.
|
|
*/
|
|
public function output_meta_tags() {
|
|
$seo_title = $this->get_current_title();
|
|
$seo_desc = $this->get_current_description();
|
|
|
|
if ( $seo_desc ) {
|
|
echo '<meta name="description" content="' . esc_attr( $seo_desc ) . '" />' . "\n";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Filter wp_title() for classic themes that use it.
|
|
*
|
|
* @param string $title Current title string.
|
|
* @return string
|
|
*/
|
|
public function filter_wp_title( $title ) {
|
|
$seo_title = $this->get_current_title();
|
|
return $seo_title ? $seo_title : $title;
|
|
}
|
|
|
|
/**
|
|
* Filter document_title_parts for modern themes that use add_theme_support('title-tag').
|
|
*
|
|
* @param array $parts Title parts array.
|
|
* @return array
|
|
*/
|
|
public function filter_document_title_parts( $parts ) {
|
|
$seo_title = $this->get_current_title();
|
|
|
|
if ( $seo_title ) {
|
|
// Replace the 'title' part; remove 'site' to avoid duplication.
|
|
$parts['title'] = $seo_title;
|
|
unset( $parts['tagline'], $parts['site'] );
|
|
}
|
|
|
|
return $parts;
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Helpers
|
|
// -------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Return the SEO title for whatever is currently being displayed.
|
|
*
|
|
* @return string Empty string when no custom title is set.
|
|
*/
|
|
private function get_current_title() {
|
|
if ( is_singular() ) {
|
|
return S1_SEO_Meta_Post::get_title( get_the_ID() );
|
|
}
|
|
|
|
if ( is_category() || is_tag() || is_tax() ) {
|
|
$term = get_queried_object();
|
|
if ( $term instanceof WP_Term ) {
|
|
return S1_SEO_Meta_Term::get_title( $term->term_id );
|
|
}
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Return the SEO description for whatever is currently being displayed.
|
|
*
|
|
* @return string Empty string when no custom description is set.
|
|
*/
|
|
private function get_current_description() {
|
|
if ( is_singular() ) {
|
|
return S1_SEO_Meta_Post::get_description( get_the_ID() );
|
|
}
|
|
|
|
if ( is_category() || is_tag() || is_tax() ) {
|
|
$term = get_queried_object();
|
|
if ( $term instanceof WP_Term ) {
|
|
return S1_SEO_Meta_Term::get_description( $term->term_id );
|
|
}
|
|
}
|
|
|
|
return '';
|
|
}
|
|
}
|