| @@ -2,14 +2,13 @@ | ||
| 2 | 2 | |
| 3 | 3 | add_filter( 'post_link', 'bogo_post_link', 10, 3 ); |
| 4 | 4 | |
| 5 | 5 | function bogo_post_link( $permalink, $post, $leavename ) { |
| 6 | - if ( ! bogo_is_localizable_post_type( $post->post_type ) ) { | |
| 6 | + if ( ! bogo_is_localizable_post_type( $post->post_type ) ) | |
| 7 | 7 | return $permalink; |
| 8 | - } | |
| 9 | 8 | |
| 10 | 9 | $locale = bogo_get_post_locale( $post->ID ); |
| 11 | - $sample = ( isset( $post->filter ) && 'sample' === $post->filter ); | |
| 10 | + $sample = ( isset( $post->filter ) && 'sample' == $post->filter ); | |
| 12 | 11 | $permalink_structure = get_option( 'permalink_structure' ); |
| 13 | 12 | |
| 14 | 13 | $using_permalinks = $permalink_structure && |
| 15 | 14 | ( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ); |
| @@ -14,38 +13,36 @@ | ||
| 14 | 13 | $using_permalinks = $permalink_structure && |
| 15 | 14 | ( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ); |
| 16 | 15 | |
| 17 | 16 | $permalink = bogo_get_url_with_lang( $permalink, $locale, |
| 18 | - array( 'using_permalinks' => $using_permalinks ) | |
| 19 | - ); | |
| 17 | + array( 'using_permalinks' => $using_permalinks ) ); | |
| 20 | 18 | |
| 21 | 19 | return $permalink; |
| 22 | 20 | } |
| 23 | 21 | |
| 24 | - | |
| 25 | 22 | add_filter( 'page_link', 'bogo_page_link', 10, 3 ); |
| 26 | 23 | |
| 27 | -function bogo_page_link( $permalink, $post_id, $sample ) { | |
| 28 | - if ( ! bogo_is_localizable_post_type( 'page' ) ) { | |
| 24 | +function bogo_page_link( $permalink, $id, $sample ) { | |
| 25 | + if ( ! bogo_is_localizable_post_type( 'page' ) ) | |
| 29 | 26 | return $permalink; |
| 30 | - } | |
| 31 | 27 | |
| 32 | - $locale = bogo_get_post_locale( $post_id ); | |
| 33 | - $post = get_post( $post_id ); | |
| 28 | + $locale = bogo_get_post_locale( $id ); | |
| 29 | + $post = get_post( $id ); | |
| 34 | 30 | |
| 35 | - if ( 'page' === get_option( 'show_on_front' ) ) { | |
| 31 | + if ( 'page' == get_option( 'show_on_front' ) ) { | |
| 36 | 32 | $front_page_id = get_option( 'page_on_front' ); |
| 37 | 33 | |
| 38 | - if ( $post_id == $front_page_id ) { | |
| 34 | + if ( $id == $front_page_id ) | |
| 39 | 35 | return $permalink; |
| 40 | - } | |
| 41 | 36 | |
| 42 | - $translation = bogo_get_post_translation( $front_page_id, $locale ); | |
| 37 | + $translations = bogo_get_post_translations( $front_page_id ); | |
| 43 | 38 | |
| 44 | - if ( $translation and $translation->ID === $post_id ) { | |
| 45 | - $home = set_url_scheme( get_option( 'home' ) ); | |
| 46 | - $home = trailingslashit( $home ); | |
| 47 | - return bogo_url( $home, $locale ); | |
| 39 | + if ( ! empty( $translations[$locale] ) ) { | |
| 40 | + if ( $translations[$locale]->ID == $id ) { | |
| 41 | + $home = set_url_scheme( get_option( 'home' ) ); | |
| 42 | + $home = trailingslashit( $home ); | |
| 43 | + return bogo_url( $home, $locale ); | |
| 44 | + } | |
| 48 | 45 | } |
| 49 | 46 | } |
| 50 | 47 | |
| 51 | 48 | $permalink_structure = get_option( 'permalink_structure' ); |
| @@ -53,21 +50,18 @@ | ||
| 53 | 50 | $using_permalinks = $permalink_structure && |
| 54 | 51 | ( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ); |
| 55 | 52 | |
| 56 | 53 | $permalink = bogo_get_url_with_lang( $permalink, $locale, |
| 57 | - array( 'using_permalinks' => $using_permalinks ) | |
| 58 | - ); | |
| 54 | + array( 'using_permalinks' => $using_permalinks ) ); | |
| 59 | 55 | |
| 60 | 56 | return $permalink; |
| 61 | 57 | } |
| 62 | 58 | |
| 63 | - | |
| 64 | 59 | add_filter( 'post_type_link', 'bogo_post_type_link', 10, 4 ); |
| 65 | 60 | |
| 66 | 61 | function bogo_post_type_link( $permalink, $post, $leavename, $sample ) { |
| 67 | - if ( ! bogo_is_localizable_post_type( $post->post_type ) ) { | |
| 62 | + if ( ! bogo_is_localizable_post_type( $post->post_type ) ) | |
| 68 | 63 | return $permalink; |
| 69 | - } | |
| 70 | 64 | |
| 71 | 65 | $locale = bogo_get_post_locale( $post->ID ); |
| 72 | 66 | $permalink_structure = get_option( 'permalink_structure' ); |
| 73 | 67 | |
| @@ -74,15 +68,13 @@ | ||
| 74 | 68 | $using_permalinks = $permalink_structure && |
| 75 | 69 | ( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ); |
| 76 | 70 | |
| 77 | 71 | $permalink = bogo_get_url_with_lang( $permalink, $locale, |
| 78 | - array( 'using_permalinks' => $using_permalinks ) | |
| 79 | - ); | |
| 72 | + array( 'using_permalinks' => $using_permalinks ) ); | |
| 80 | 73 | |
| 81 | 74 | return $permalink; |
| 82 | 75 | } |
| 83 | 76 | |
| 84 | - | |
| 85 | 77 | add_filter( 'year_link', 'bogo_year_link', 10, 2 ); |
| 86 | 78 | |
| 87 | 79 | function bogo_year_link( $link, $year ) { |
| 88 | 80 | return bogo_url( $link ); |
| @@ -87,9 +79,8 @@ | ||
| 87 | 79 | function bogo_year_link( $link, $year ) { |
| 88 | 80 | return bogo_url( $link ); |
| 89 | 81 | } |
| 90 | 82 | |
| 91 | - | |
| 92 | 83 | add_filter( 'month_link', 'bogo_month_link', 10, 3 ); |
| 93 | 84 | |
| 94 | 85 | function bogo_month_link( $link, $year, $month ) { |
| 95 | 86 | return bogo_url( $link ); |
| @@ -94,9 +85,8 @@ | ||
| 94 | 85 | function bogo_month_link( $link, $year, $month ) { |
| 95 | 86 | return bogo_url( $link ); |
| 96 | 87 | } |
| 97 | 88 | |
| 98 | - | |
| 99 | 89 | add_filter( 'day_link', 'bogo_day_link', 10, 4 ); |
| 100 | 90 | |
| 101 | 91 | function bogo_day_link( $link, $year, $month, $day ) { |
| 102 | 92 | return bogo_url( $link ); |
| @@ -101,9 +91,8 @@ | ||
| 101 | 91 | function bogo_day_link( $link, $year, $month, $day ) { |
| 102 | 92 | return bogo_url( $link ); |
| 103 | 93 | } |
| 104 | 94 | |
| 105 | - | |
| 106 | 95 | add_filter( 'feed_link', 'bogo_feed_link', 10, 2 ); |
| 107 | 96 | |
| 108 | 97 | function bogo_feed_link( $link, $feed ) { |
| 109 | 98 | return bogo_url( $link ); |
| @@ -108,9 +97,8 @@ | ||
| 108 | 97 | function bogo_feed_link( $link, $feed ) { |
| 109 | 98 | return bogo_url( $link ); |
| 110 | 99 | } |
| 111 | 100 | |
| 112 | - | |
| 113 | 101 | add_filter( 'author_feed_link', 'bogo_author_feed_link', 10, 2 ); |
| 114 | 102 | |
| 115 | 103 | function bogo_author_feed_link( $link, $feed ) { |
| 116 | 104 | return bogo_url( $link ); |
| @@ -115,9 +103,8 @@ | ||
| 115 | 103 | function bogo_author_feed_link( $link, $feed ) { |
| 116 | 104 | return bogo_url( $link ); |
| 117 | 105 | } |
| 118 | 106 | |
| 119 | - | |
| 120 | 107 | add_filter( 'category_feed_link', 'bogo_category_feed_link', 10, 2 ); |
| 121 | 108 | |
| 122 | 109 | function bogo_category_feed_link( $link, $feed ) { |
| 123 | 110 | return bogo_url( $link ); |
| @@ -122,9 +109,8 @@ | ||
| 122 | 109 | function bogo_category_feed_link( $link, $feed ) { |
| 123 | 110 | return bogo_url( $link ); |
| 124 | 111 | } |
| 125 | 112 | |
| 126 | - | |
| 127 | 113 | add_filter( 'taxonomy_feed_link', 'bogo_taxonomy_feed_link', 10, 3 ); |
| 128 | 114 | |
| 129 | 115 | function bogo_taxonomy_feed_link( $link, $feed, $taxonomy ) { |
| 130 | 116 | return bogo_url( $link ); |
| @@ -129,9 +115,8 @@ | ||
| 129 | 115 | function bogo_taxonomy_feed_link( $link, $feed, $taxonomy ) { |
| 130 | 116 | return bogo_url( $link ); |
| 131 | 117 | } |
| 132 | 118 | |
| 133 | - | |
| 134 | 119 | add_filter( 'post_type_archive_link', 'bogo_post_type_archive_link', 10, 2 ); |
| 135 | 120 | |
| 136 | 121 | function bogo_post_type_archive_link( $link, $post_type ) { |
| 137 | 122 | return bogo_url( $link ); |
| @@ -136,19 +121,14 @@ | ||
| 136 | 121 | function bogo_post_type_archive_link( $link, $post_type ) { |
| 137 | 122 | return bogo_url( $link ); |
| 138 | 123 | } |
| 139 | 124 | |
| 125 | +add_filter( 'post_type_archive_feed_link', 'bogo_post_type_archive_feed_link', 10, 2 ); | |
| 140 | 126 | |
| 141 | -add_filter( 'post_type_archive_feed_link', | |
| 142 | - 'bogo_post_type_archive_feed_link', | |
| 143 | - 10, 2 | |
| 144 | -); | |
| 145 | - | |
| 146 | 127 | function bogo_post_type_archive_feed_link( $link, $feed ) { |
| 147 | 128 | return bogo_url( $link ); |
| 148 | 129 | } |
| 149 | 130 | |
| 150 | - | |
| 151 | 131 | add_filter( 'term_link', 'bogo_term_link', 10, 3 ); |
| 152 | 132 | |
| 153 | 133 | function bogo_term_link( $link, $term, $taxonomy ) { |
| 154 | 134 | return bogo_url( $link ); |
| @@ -153,22 +133,19 @@ | ||
| 153 | 133 | function bogo_term_link( $link, $term, $taxonomy ) { |
| 154 | 134 | return bogo_url( $link ); |
| 155 | 135 | } |
| 156 | 136 | |
| 137 | +add_filter( 'home_url', 'bogo_home_url' ); | |
| 157 | 138 | |
| 158 | -add_filter( 'home_url', 'bogo_home_url', 10, 1 ); | |
| 159 | - | |
| 160 | 139 | function bogo_home_url( $url ) { |
| 161 | - if ( is_admin() or ! did_action( 'template_redirect' ) ) { | |
| 140 | + if ( is_admin() || ! did_action( 'template_redirect' ) ) | |
| 162 | 141 | return $url; |
| 163 | - } | |
| 164 | 142 | |
| 165 | 143 | return bogo_url( $url ); |
| 166 | 144 | } |
| 167 | 145 | |
| 146 | +add_action( 'wp_head', 'bogo_m17n_headers' ); | |
| 168 | 147 | |
| 169 | -add_action( 'wp_head', 'bogo_m17n_headers', 10, 0 ); | |
| 170 | - | |
| 171 | 148 | function bogo_m17n_headers() { |
| 172 | 149 | $languages = array(); |
| 173 | 150 | |
| 174 | 151 | if ( is_singular() ) { |
| @@ -173,9 +150,9 @@ | ||
| 173 | 150 | |
| 174 | 151 | if ( is_singular() ) { |
| 175 | 152 | $post_id = get_queried_object_id(); |
| 176 | 153 | |
| 177 | - if ( $post_id and $translations = bogo_get_post_translations( $post_id ) ) { | |
| 154 | + if ( $post_id && $translations = bogo_get_post_translations( $post_id ) ) { | |
| 178 | 155 | $locale = get_locale(); |
| 179 | 156 | $translations[$locale] = get_post( $post_id ); |
| 180 | 157 | |
| 181 | 158 | foreach ( $translations as $lang => $translation ) { |
| @@ -180,20 +157,19 @@ | ||
| 180 | 157 | |
| 181 | 158 | foreach ( $translations as $lang => $translation ) { |
| 182 | 159 | $languages[] = array( |
| 183 | 160 | 'hreflang' => bogo_language_tag( $lang ), |
| 184 | - 'href' => get_permalink( $translation ), | |
| 185 | - ); | |
| 161 | + 'href' => get_permalink( $translation ) ); | |
| 186 | 162 | } |
| 187 | 163 | } |
| 188 | 164 | } else { |
| 189 | - $available_locales = bogo_available_locales(); | |
| 165 | + $available_locales = bogo_available_locales( array( | |
| 166 | + 'exclude_enus_if_inactive' => true ) ); | |
| 190 | 167 | |
| 191 | 168 | foreach ( $available_locales as $locale ) { |
| 192 | 169 | $languages[] = array( |
| 193 | 170 | 'hreflang' => bogo_language_tag( $locale ), |
| 194 | - 'href' => bogo_url( null, $locale ), | |
| 195 | - ); | |
| 171 | + 'href' => bogo_url( null, $locale ) ); | |
| 196 | 172 | } |
| 197 | 173 | } |
| 198 | 174 | |
| 199 | 175 | $languages = apply_filters( 'bogo_rel_alternate_hreflang', $languages ); |
| @@ -198,36 +174,123 @@ | ||
| 198 | 174 | |
| 199 | 175 | $languages = apply_filters( 'bogo_rel_alternate_hreflang', $languages ); |
| 200 | 176 | |
| 201 | 177 | foreach ( (array) $languages as $language ) { |
| 202 | - $hreflang = $language['hreflang'] ?? ''; | |
| 203 | - $href = $language['href'] ?? ''; | |
| 178 | + $hreflang = isset( $language['hreflang'] ) ? $language['hreflang'] : ''; | |
| 179 | + $href = isset( $language['href'] ) ? $language['href'] : ''; | |
| 204 | 180 | |
| 205 | - if ( $hreflang and $href ) { | |
| 206 | - $link = sprintf( | |
| 207 | - '<link %s />', | |
| 208 | - bogo_format_atts( array( | |
| 209 | - 'rel' => 'alternate', | |
| 210 | - 'href' => esc_url( $href ), | |
| 211 | - 'hreflang' => $hreflang, | |
| 212 | - ) ) | |
| 213 | - ); | |
| 181 | + if ( $hreflang && $href ) { | |
| 182 | + $link = sprintf( '<link rel="alternate" hreflang="%1$s" href="%2$s" />', | |
| 183 | + esc_attr( $hreflang ), esc_url( $href ) ); | |
| 214 | 184 | |
| 215 | - echo wp_kses( | |
| 216 | - $link, | |
| 217 | - array( | |
| 218 | - 'link' => array( | |
| 219 | - 'rel' => true, | |
| 220 | - 'href' => true, | |
| 221 | - 'hreflang' => true, | |
| 222 | - ), | |
| 223 | - ) | |
| 224 | - ) . "\n"; | |
| 185 | + echo $link . "\n"; | |
| 225 | 186 | } |
| 226 | 187 | } |
| 227 | 188 | } |
| 228 | 189 | |
| 190 | +function bogo_language_switcher( $args = '' ) { | |
| 191 | + $args = wp_parse_args( $args, array( | |
| 192 | + 'echo' => false ) ); | |
| 229 | 193 | |
| 194 | + $links = bogo_language_switcher_links( $args ); | |
| 195 | + $total = count( $links ); | |
| 196 | + $count = 0; | |
| 197 | + | |
| 198 | + $output = ''; | |
| 199 | + | |
| 200 | + foreach ( $links as $link ) { | |
| 201 | + $count += 1; | |
| 202 | + $class = array(); | |
| 203 | + $class[] = bogo_language_tag( $link['locale'] ); | |
| 204 | + $class[] = bogo_lang_slug( $link['locale'] ); | |
| 205 | + | |
| 206 | + if ( get_locale() == $link['locale'] ) { | |
| 207 | + $class[] = 'current'; | |
| 208 | + } | |
| 209 | + | |
| 210 | + if ( 1 == $count ) { | |
| 211 | + $class[] = 'first'; | |
| 212 | + } | |
| 213 | + | |
| 214 | + if ( $total == $count ) { | |
| 215 | + $class[] = 'last'; | |
| 216 | + } | |
| 217 | + | |
| 218 | + $class = implode( ' ', array_unique( $class ) ); | |
| 219 | + | |
| 220 | + $label = $link['native_name'] ? $link['native_name'] : $link['title']; | |
| 221 | + $title = $link['title']; | |
| 222 | + | |
| 223 | + if ( empty( $link['href'] ) ) { | |
| 224 | + $li = esc_html( $label ); | |
| 225 | + } else { | |
| 226 | + $li = sprintf( | |
| 227 | + '<a rel="alternate" hreflang="%1$s" href="%2$s" title="%3$s">%4$s</a>', | |
| 228 | + $link['lang'], | |
| 229 | + esc_url( $link['href'] ), | |
| 230 | + esc_attr( $title ), | |
| 231 | + esc_html( $label ) ); | |
| 232 | + } | |
| 233 | + | |
| 234 | + $li = sprintf( '<li class="%1$s">%2$s</li>', $class, $li ); | |
| 235 | + | |
| 236 | + $output .= $li . "\n"; | |
| 237 | + } | |
| 238 | + | |
| 239 | + $output = '<ul class="bogo-language-switcher">' . $output . '</ul>' . "\n"; | |
| 240 | + | |
| 241 | + $output = apply_filters( 'bogo_language_switcher', $output, $args ); | |
| 242 | + | |
| 243 | + if ( $args['echo'] ) { | |
| 244 | + echo $output; | |
| 245 | + } else { | |
| 246 | + return $output; | |
| 247 | + } | |
| 248 | +} | |
| 249 | + | |
| 250 | +function bogo_language_switcher_links( $args = '' ) { | |
| 251 | + global $wp_query; | |
| 252 | + | |
| 253 | + $args = wp_parse_args( $args, array() ); | |
| 254 | + | |
| 255 | + $locale = get_locale(); | |
| 256 | + $available_languages = bogo_available_languages( array( | |
| 257 | + 'exclude_enus_if_inactive' => true ) ); | |
| 258 | + | |
| 259 | + $translations = array(); | |
| 260 | + $is_singular = false; | |
| 261 | + | |
| 262 | + if ( is_singular() || ! empty( $wp_query->is_posts_page ) ) { | |
| 263 | + $translations = bogo_get_post_translations( get_queried_object_id() ); | |
| 264 | + $is_singular = true; | |
| 265 | + } | |
| 266 | + | |
| 267 | + $links = array(); | |
| 268 | + | |
| 269 | + foreach ( $available_languages as $code => $name ) { | |
| 270 | + $link = array( | |
| 271 | + 'locale' => $code, | |
| 272 | + 'lang' => bogo_language_tag( $code ), | |
| 273 | + 'title' => $name, | |
| 274 | + 'native_name' => bogo_get_language_native_name( $code ), | |
| 275 | + 'href' => '' ); | |
| 276 | + | |
| 277 | + if ( $is_singular ) { | |
| 278 | + if ( $locale != $code && ! empty( $translations[$code] ) ) { | |
| 279 | + $link['href'] = get_permalink( $translations[$code] ); | |
| 280 | + } | |
| 281 | + } else { | |
| 282 | + if ( $locale != $code ) { | |
| 283 | + $link['href'] = bogo_url( null, $code ); | |
| 284 | + } | |
| 285 | + } | |
| 286 | + | |
| 287 | + $links[] = $link; | |
| 288 | + } | |
| 289 | + | |
| 290 | + return apply_filters( 'bogo_language_switcher_links', $links, $args ); | |
| 291 | +} | |
| 292 | + | |
| 230 | 293 | add_filter( 'get_previous_post_join', 'bogo_adjacent_post_join', 10, 3 ); |
| 231 | 294 | add_filter( 'get_next_post_join', 'bogo_adjacent_post_join', 10, 3 ); |
| 232 | 295 | |
| 233 | 296 | function bogo_adjacent_post_join( $join, $in_same_term, $excluded_terms ) { |
| @@ -234,9 +297,9 @@ | ||
| 234 | 297 | global $wpdb; |
| 235 | 298 | |
| 236 | 299 | $post = get_post(); |
| 237 | 300 | |
| 238 | - if ( $post and bogo_is_localizable_post_type( get_post_type( $post ) ) ) { | |
| 301 | + if ( $post && bogo_is_localizable_post_type( get_post_type( $post ) ) ) { | |
| 239 | 302 | $join .= " LEFT JOIN $wpdb->postmeta AS postmeta_bogo ON (p.ID = postmeta_bogo.post_id AND postmeta_bogo.meta_key = '_locale')"; |
| 240 | 303 | } |
| 241 | 304 | |
| 242 | 305 | return $join; |
| @@ -241,9 +304,8 @@ | ||
| 241 | 304 | |
| 242 | 305 | return $join; |
| 243 | 306 | } |
| 244 | 307 | |
| 245 | - | |
| 246 | 308 | add_filter( 'get_previous_post_where', 'bogo_adjacent_post_where', 10, 3 ); |
| 247 | 309 | add_filter( 'get_next_post_where', 'bogo_adjacent_post_where', 10, 3 ); |
| 248 | 310 | |
| 249 | 311 | function bogo_adjacent_post_where( $where, $in_same_term, $excluded_terms ) { |
| @@ -250,9 +312,9 @@ | ||
| 250 | 312 | global $wpdb; |
| 251 | 313 | |
| 252 | 314 | $post = get_post(); |
| 253 | 315 | |
| 254 | - if ( $post and bogo_is_localizable_post_type( get_post_type( $post ) ) ) { | |
| 316 | + if ( $post && bogo_is_localizable_post_type( get_post_type( $post ) ) ) { | |
| 255 | 317 | $locale = bogo_get_post_locale( $post->ID ); |
| 256 | 318 | |
| 257 | 319 | $where .= " AND (1=0"; |
| 258 | 320 | $where .= $wpdb->prepare( " OR postmeta_bogo.meta_value LIKE %s", $locale ); |