index.php
163 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Blocks; |
| 4 | |
| 5 | use Kubio\Core\Blocks\BlockBase; |
| 6 | use Kubio\Core\Registry; |
| 7 | |
| 8 | class PageTitleBlock extends BlockBase { |
| 9 | |
| 10 | |
| 11 | const CONTAINER = 'container'; |
| 12 | |
| 13 | |
| 14 | static function getTitle( $json = false, $titles = array() ) { |
| 15 | // translators: %s - year month or day |
| 16 | $posts_from = __( 'Posts from %s', 'kubio' ); |
| 17 | $titles = array_merge( |
| 18 | array( |
| 19 | 'normalPage' => '{TITLE}', |
| 20 | // translators: %s - search term |
| 21 | 'normalResultsPage' => sprintf( __( 'Search results for: %s', 'kubio' ), '{TITLE}' ), |
| 22 | 'errorPage' => __( 'Sorry! Page Not Found!', 'kubio' ), |
| 23 | 'singlePost' => '{TITLE}', |
| 24 | // translators: %s - category |
| 25 | 'categoryArchive' => sprintf( __( 'Posts in %s', 'kubio' ), '{TITLE}' ), |
| 26 | // translators: %s - author |
| 27 | 'authorArchive' => sprintf( __( 'Posts by %s', 'kubio' ), '{TITLE}' ), |
| 28 | // translators: %s - tag |
| 29 | 'tagArchive' => sprintf( __( 'Posts about %s', 'kubio' ), '{TITLE}' ), |
| 30 | |
| 31 | 'yearArchive' => sprintf( $posts_from, '{TITLE}' ), |
| 32 | 'monthArchive' => sprintf( $posts_from, '{TITLE}' ), |
| 33 | 'dayArchive' => sprintf( $posts_from, '{TITLE}' ), |
| 34 | ), |
| 35 | $titles |
| 36 | ); |
| 37 | |
| 38 | ob_start(); |
| 39 | $final_title = ''; |
| 40 | $title_type = ''; |
| 41 | $title = ''; |
| 42 | |
| 43 | if ( is_404() ) { |
| 44 | $title_type = 'errorPage'; |
| 45 | $final_title = $titles['errorPage']; |
| 46 | } elseif ( is_search() ) { |
| 47 | $title = get_search_query(); |
| 48 | $title_type = 'normalResultsPage'; |
| 49 | } elseif ( is_home() ) { |
| 50 | $title = __('Blog', 'kubio'); |
| 51 | $title_type = 'normalPage'; |
| 52 | |
| 53 | } elseif(is_front_page()) { |
| 54 | $title = get_bloginfo( 'name' ); |
| 55 | $title_type = 'normalPage'; |
| 56 | } elseif ( is_archive() ) { |
| 57 | if ( is_post_type_archive() ) { |
| 58 | $title = post_type_archive_title( '', false ); |
| 59 | } else { |
| 60 | if ( is_category() ) { |
| 61 | /* translators: Category archive title. 1: Category name */ |
| 62 | $title = single_cat_title( '', false ); |
| 63 | $title_type = 'categoryArchive'; |
| 64 | } elseif ( is_tag() ) { |
| 65 | /* translators: Tag archive title. 1: Tag name */ |
| 66 | $title = single_tag_title( '', false ); |
| 67 | $title_type = 'tagArchive'; |
| 68 | } elseif ( is_author() ) { |
| 69 | /* translators: Author archive title. 1: Author name */ |
| 70 | $title = '<span class="vcard">' . get_the_author() . '</span>'; |
| 71 | $title_type = 'authorArchive'; |
| 72 | } elseif ( is_year() ) { |
| 73 | /* translators: Yearly archive title. 1: Year */ |
| 74 | $title = get_the_date( _x( 'Y', 'yearly archives date format', 'kubio' ) ); |
| 75 | $title_type = 'yearArchive'; |
| 76 | } elseif ( is_month() ) { |
| 77 | /* translators: Monthly archive title. 1: Month name and year */ |
| 78 | $title = get_the_date( _x( 'F Y', 'monthly archives date format', 'kubio' ) ); |
| 79 | $title_type = 'monthArchive'; |
| 80 | } elseif ( is_day() ) { |
| 81 | /* translators: Daily archive title. 1: Date */ |
| 82 | $title = get_the_date( _x( 'F j, Y', 'daily archives date format', 'kubio' ) ); |
| 83 | $title_type = 'dayArchive'; |
| 84 | } elseif ( is_tax( 'post_format' ) ) { |
| 85 | if ( is_tax( 'post_format', 'post-format-aside' ) ) { |
| 86 | $title = _x( 'Asides', 'post format archive title', 'kubio' ); |
| 87 | } elseif ( is_tax( 'post_format', 'post-format-gallery', 'kubio' ) ) { |
| 88 | $title = _x( 'Galleries', 'post format archive title', 'kubio' ); |
| 89 | } elseif ( is_tax( 'post_format', 'post-format-image', 'kubio' ) ) { |
| 90 | $title = _x( 'Images', 'post format archive title', 'kubio' ); |
| 91 | } elseif ( is_tax( 'post_format', 'post-format-video', 'kubio' ) ) { |
| 92 | $title = _x( 'Videos', 'post format archive title', 'kubio' ); |
| 93 | } elseif ( is_tax( 'post_format', 'post-format-quote', 'kubio' ) ) { |
| 94 | $title = _x( 'Quotes', 'post format archive title', 'kubio' ); |
| 95 | } elseif ( is_tax( 'post_format', 'post-format-link', 'kubio' ) ) { |
| 96 | $title = _x( 'Links', 'post format archive title', 'kubio' ); |
| 97 | } elseif ( is_tax( 'post_format', 'post-format-status', 'kubio' ) ) { |
| 98 | $title = _x( 'Statuses', 'post format archive title', 'kubio' ); |
| 99 | } elseif ( is_tax( 'post_format', 'post-format-audio', 'kubio' ) ) { |
| 100 | $title = _x( 'Audio', 'post format archive title', 'kubio' ); |
| 101 | } elseif ( is_tax( 'post_format', 'post-format-chat' ) ) { |
| 102 | $title = _x( 'Chats', 'post format archive title', 'kubio' ); |
| 103 | } |
| 104 | } elseif ( is_post_type_archive() ) { |
| 105 | /* translators: Post type archive title. 1: Post type name */ |
| 106 | $title = sprintf( __( 'Archives: %s', 'kubio' ), post_type_archive_title( '', false ) ); |
| 107 | } elseif ( is_tax() ) { |
| 108 | $tax = get_taxonomy( get_queried_object()->taxonomy ); |
| 109 | /* translators: Taxonomy term archive title. 1: Taxonomy singular name, 2: Current taxonomy term */ |
| 110 | $title = sprintf( __( '%1$s: %2$s', 'kubio' ), $tax->labels->singular_name, single_term_title( '', false ) ); |
| 111 | } else { |
| 112 | $title = __( 'Archives', 'kubio' ); |
| 113 | } |
| 114 | } |
| 115 | } elseif ( is_single() ) { |
| 116 | $title = get_bloginfo( 'name' ); |
| 117 | |
| 118 | global $post; |
| 119 | if ( $post ) { |
| 120 | // apply core filter |
| 121 | $title = apply_filters( 'single_post_title', $post->post_title, $post ); |
| 122 | } |
| 123 | $title_type = 'singlePost'; |
| 124 | } else { |
| 125 | $title = get_the_title(); |
| 126 | $title_type = 'normalPage'; |
| 127 | } |
| 128 | |
| 129 | $final_title = $title_type ? str_replace( '{TITLE}', $title, kubio_wpml_get_translated_string( $titles[ $title_type ] ) ) : $title; |
| 130 | |
| 131 | $content = ob_get_clean(); |
| 132 | |
| 133 | if ( $json ) { |
| 134 | return array( |
| 135 | 'type' => $title_type, |
| 136 | 'title' => $title, |
| 137 | 'computed' => $final_title, |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | $result = $content ? $content : $final_title; |
| 142 | return wp_kses_post( $result ); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | public function mapPropsToElements() { |
| 147 | $headingType = esc_attr( |
| 148 | $this->getProp( 'level' ) ? |
| 149 | $this->getProp( 'level' ) : |
| 150 | $this->getAttribute( 'tag' ) |
| 151 | ); |
| 152 | |
| 153 | return array( |
| 154 | self::CONTAINER => array( |
| 155 | 'innerHTML' => $this->getTitle( false, $this->getAttribute( 'templates' ) ), |
| 156 | 'tag' => $headingType, |
| 157 | ), |
| 158 | ); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | Registry::registerBlock( __DIR__, PageTitleBlock::class ); |
| 163 |