| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Presenters; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Presentations\Indexable_Presentation; |
| 6 |
|
| 7 |
/** |
| 8 |
* Presenter class for the document title. |
| 9 |
*/ |
| 10 |
class Title_Presenter extends Abstract_Indexable_Tag_Presenter { |
| 11 |
|
| 12 |
/** |
| 13 |
* The tag key name. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
protected $key = 'title'; |
| 18 |
|
| 19 |
/** |
| 20 |
* The tag format including placeholders. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $tag_format = '<title>%s</title>'; |
| 25 |
|
| 26 |
/** |
| 27 |
* The method of escaping to use. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $escaping = 'html'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Gets the raw value of a presentation. |
| 35 |
* |
| 36 |
* @return string The raw value. |
| 37 |
*/ |
| 38 |
public function get() { |
| 39 |
// This ensures backwards compatibility with other plugins using this filter as well. |
| 40 |
\add_filter( 'pre_get_document_title', [ $this, 'get_title' ], 15 ); |
| 41 |
$title = \wp_get_document_title(); |
| 42 |
\remove_filter( 'pre_get_document_title', [ $this, 'get_title' ], 15 ); |
| 43 |
return $title; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Returns a tag in the head. |
| 48 |
* |
| 49 |
* @return string The tag. |
| 50 |
*/ |
| 51 |
public function present() { |
| 52 |
$value = $this->get(); |
| 53 |
|
| 54 |
if ( \is_string( $value ) && $value !== '' ) { |
| 55 |
return \sprintf( $this->tag_format, $this->escape_value( $value ) ); |
| 56 |
} |
| 57 |
|
| 58 |
return ''; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns the presentation title. |
| 63 |
* |
| 64 |
* @return string The title. |
| 65 |
*/ |
| 66 |
public function get_title() { |
| 67 |
$title = $this->replace_vars( $this->presentation->title ); |
| 68 |
|
| 69 |
/** |
| 70 |
* Filter: 'wpseo_title' - Allow changing the Yoast SEO generated title. |
| 71 |
* |
| 72 |
* @param string $title The title. |
| 73 |
* @param Indexable_Presentation $presentation The presentation of an indexable. |
| 74 |
*/ |
| 75 |
$title = \apply_filters( 'wpseo_title', $title, $this->presentation ); |
| 76 |
$title = $this->helpers->string->strip_all_tags( $title ); |
| 77 |
return \trim( $title ); |
| 78 |
} |
| 79 |
} |
| 80 |
|