Core.php
3 weeks ago
Debug.php
3 weeks ago
FixDates.php
3 weeks ago
FixPermalink.php
3 weeks ago
FixTitle.php
3 weeks ago
ShamsiDate.php
3 weeks ago
FixTitle.php
71 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Fix title settings |
| 4 | * |
| 5 | * Fix page title |
| 6 | */ |
| 7 | |
| 8 | namespace WPParsidate\App\Core; |
| 9 | |
| 10 | use WPParsidate\Core\Names; |
| 11 | use WPParsidate\Helper\Number; |
| 12 | use WPParsidate\Settings\Settings; |
| 13 | |
| 14 | class FixTitle { |
| 15 | public function __construct() { |
| 16 | add_filter( 'wp_title', [ $this, 'fixWpTitle' ], PHP_INT_MAX, 3 ); |
| 17 | add_filter( 'pre_get_document_title', [ $this, 'fixWpTitle' ], PHP_INT_MAX ); // WP 4.4+ |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Fixes titles for archives |
| 22 | * |
| 23 | * @param string|null $title Archive title. |
| 24 | * @param string $sep Separator. |
| 25 | * @param string $seplocation Separator location. |
| 26 | * |
| 27 | * @return string |
| 28 | */ |
| 29 | public function fixWpTitle( ?string $title, $sep = '-', $seplocation = 'right' ): string { |
| 30 | global $wp_query; |
| 31 | |
| 32 | // pre_get_document_title may return null. |
| 33 | $title ??= ''; |
| 34 | |
| 35 | /** |
| 36 | * Filters the separator for the document title. |
| 37 | * |
| 38 | * @since WP 4.4.0 |
| 39 | * |
| 40 | * @param string $sep Document title separator. Default '-'. |
| 41 | */ |
| 42 | $sep = apply_filters( 'document_title_separator', $sep ); |
| 43 | |
| 44 | $query = $wp_query->query ?? []; |
| 45 | |
| 46 | if ( ! is_archive() || ! Settings::get( 'persian_date', false ) ) { |
| 47 | return $title; |
| 48 | } |
| 49 | |
| 50 | if ( $seplocation === 'right' ) { |
| 51 | $query = array_reverse( $query ); |
| 52 | } |
| 53 | |
| 54 | if ( isset( $query['monthnum'] ) ) { |
| 55 | $monthsName = Names::getMonths(); |
| 56 | |
| 57 | if ( isset( $monthsName[ (int) $query['monthnum'] ] ) ) { |
| 58 | $query['monthnum'] = $monthsName[ (int) $query['monthnum'] ]; |
| 59 | } |
| 60 | |
| 61 | $title = implode( ' ', $query ) . " $sep " . get_bloginfo( 'name' ); |
| 62 | } |
| 63 | |
| 64 | if ( Settings::get( 'conv_page_title', false ) ) { |
| 65 | $title = Number::fixNumber( $title ); |
| 66 | } |
| 67 | |
| 68 | return (string) $title; |
| 69 | } |
| 70 | } |
| 71 |