| 1 |
<?php |
| 2 |
|
| 3 |
namespace AgeGate\Presentation; |
| 4 |
|
| 5 |
use AgeGate\Common\Content; |
| 6 |
use AgeGate\Common\Settings; |
| 7 |
|
| 8 |
class Title |
| 9 |
{ |
| 10 |
private $settings; |
| 11 |
|
| 12 |
private $content; |
| 13 |
|
| 14 |
public function __construct(Content $content, Settings $settings) |
| 15 |
{ |
| 16 |
$this->settings = $settings; |
| 17 |
$this->content = $content; |
| 18 |
|
| 19 |
if ($settings->method !== 'js' && $settings->switchTitle) { |
| 20 |
add_filter('wpseo_title', [$this, 'returnPageTitle'], 1000, 1); |
| 21 |
add_filter('rank_math/frontend/title', [$this, 'returnPageTitle'], 1000, 1); |
| 22 |
add_filter('document_title_parts', [$this, 'changePageTitle'], 1000, 1); |
| 23 |
add_filter('wp_title', [$this, 'changeDefaultTitle'], 10, 3); |
| 24 |
} |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
public function getTitle() |
| 29 |
{ |
| 30 |
return $this->settings->customTitle . ' - ' . get_bloginfo('name'); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Change the page title for themes with theme_support |
| 35 |
* for title-tag |
| 36 |
* |
| 37 |
* @return array |
| 38 |
*/ |
| 39 |
public function changePageTitle($title = []) |
| 40 |
{ |
| 41 |
if ($this->content->isRestricted()) { |
| 42 |
return [ |
| 43 |
'title' => $this->settings->customTitle, |
| 44 |
'site' => get_bloginfo('name'), |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
return $title; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Change the title for themes that |
| 53 |
* do not support title tag |
| 54 |
* |
| 55 |
* @param string $title |
| 56 |
* @param string $sep |
| 57 |
* @param string $location |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public function changeDefaultTitle($title, $sep, $location) |
| 61 |
{ |
| 62 |
|
| 63 |
if ($this->content->isRestricted()) { |
| 64 |
return $this->getTitle(); |
| 65 |
} |
| 66 |
|
| 67 |
return $title; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Change the parts sent to Yoast or Think Math |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
public function returnPageTitle($title) |
| 76 |
{ |
| 77 |
if ($this->content->isRestricted()) { |
| 78 |
return $this->getTitle(); |
| 79 |
} |
| 80 |
|
| 81 |
return $title; |
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
|