| 1 |
<?php |
| 2 |
|
| 3 |
add_filter( 'post_link', 'bogo_post_link', 10, 3 ); |
| 4 |
|
| 5 |
function bogo_post_link( $permalink, $post, $leavename ) { |
| 6 |
if ( ! bogo_is_localizable_post_type( $post->post_type ) ) |
| 7 |
return $permalink; |
| 8 |
|
| 9 |
$locale = bogo_get_post_locale( $post->ID ); |
| 10 |
$sample = ( isset( $post->filter ) && 'sample' == $post->filter ); |
| 11 |
$permalink_structure = get_option( 'permalink_structure' ); |
| 12 |
|
| 13 |
$using_permalinks = $permalink_structure && |
| 14 |
( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ); |
| 15 |
|
| 16 |
$permalink = bogo_get_url_with_lang( $permalink, $locale, |
| 17 |
array( 'using_permalinks' => $using_permalinks ) ); |
| 18 |
|
| 19 |
return $permalink; |
| 20 |
} |
| 21 |
|
| 22 |
add_filter( 'page_link', 'bogo_page_link', 10, 3 ); |
| 23 |
|
| 24 |
function bogo_page_link( $permalink, $id, $sample ) { |
| 25 |
if ( ! bogo_is_localizable_post_type( 'page' ) ) |
| 26 |
return $permalink; |
| 27 |
|
| 28 |
$locale = bogo_get_post_locale( $id ); |
| 29 |
$post = get_post( $id ); |
| 30 |
|
| 31 |
if ( 'page' == get_option( 'show_on_front' ) ) { |
| 32 |
$front_page_id = get_option( 'page_on_front' ); |
| 33 |
|
| 34 |
if ( $id == $front_page_id ) |
| 35 |
return $permalink; |
| 36 |
|
| 37 |
$translations = bogo_get_post_translations( $front_page_id ); |
| 38 |
|
| 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 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
$permalink_structure = get_option( 'permalink_structure' ); |
| 49 |
|
| 50 |
$using_permalinks = $permalink_structure && |
| 51 |
( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ); |
| 52 |
|
| 53 |
$permalink = bogo_get_url_with_lang( $permalink, $locale, |
| 54 |
array( 'using_permalinks' => $using_permalinks ) ); |
| 55 |
|
| 56 |
return $permalink; |
| 57 |
} |
| 58 |
|
| 59 |
add_filter( 'post_type_link', 'bogo_post_type_link', 10, 4 ); |
| 60 |
|
| 61 |
function bogo_post_type_link( $permalink, $post, $leavename, $sample ) { |
| 62 |
if ( ! bogo_is_localizable_post_type( $post->post_type ) ) |
| 63 |
return $permalink; |
| 64 |
|
| 65 |
$locale = bogo_get_post_locale( $post->ID ); |
| 66 |
$permalink_structure = get_option( 'permalink_structure' ); |
| 67 |
|
| 68 |
$using_permalinks = $permalink_structure && |
| 69 |
( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ); |
| 70 |
|
| 71 |
$permalink = bogo_get_url_with_lang( $permalink, $locale, |
| 72 |
array( 'using_permalinks' => $using_permalinks ) ); |
| 73 |
|
| 74 |
return $permalink; |
| 75 |
} |
| 76 |
|
| 77 |
add_filter( 'year_link', 'bogo_year_link', 10, 2 ); |
| 78 |
|
| 79 |
function bogo_year_link( $link, $year ) { |
| 80 |
return bogo_url( $link ); |
| 81 |
} |
| 82 |
|
| 83 |
add_filter( 'month_link', 'bogo_month_link', 10, 3 ); |
| 84 |
|
| 85 |
function bogo_month_link( $link, $year, $month ) { |
| 86 |
return bogo_url( $link ); |
| 87 |
} |
| 88 |
|
| 89 |
add_filter( 'day_link', 'bogo_day_link', 10, 4 ); |
| 90 |
|
| 91 |
function bogo_day_link( $link, $year, $month, $day ) { |
| 92 |
return bogo_url( $link ); |
| 93 |
} |
| 94 |
|
| 95 |
add_filter( 'feed_link', 'bogo_feed_link', 10, 2 ); |
| 96 |
|
| 97 |
function bogo_feed_link( $link, $feed ) { |
| 98 |
return bogo_url( $link ); |
| 99 |
} |
| 100 |
|
| 101 |
add_filter( 'author_feed_link', 'bogo_author_feed_link', 10, 2 ); |
| 102 |
|
| 103 |
function bogo_author_feed_link( $link, $feed ) { |
| 104 |
return bogo_url( $link ); |
| 105 |
} |
| 106 |
|
| 107 |
add_filter( 'category_feed_link', 'bogo_category_feed_link', 10, 2 ); |
| 108 |
|
| 109 |
function bogo_category_feed_link( $link, $feed ) { |
| 110 |
return bogo_url( $link ); |
| 111 |
} |
| 112 |
|
| 113 |
add_filter( 'taxonomy_feed_link', 'bogo_taxonomy_feed_link', 10, 3 ); |
| 114 |
|
| 115 |
function bogo_taxonomy_feed_link( $link, $feed, $taxonomy ) { |
| 116 |
return bogo_url( $link ); |
| 117 |
} |
| 118 |
|
| 119 |
add_filter( 'post_type_archive_link', 'bogo_post_type_archive_link', 10, 2 ); |
| 120 |
|
| 121 |
function bogo_post_type_archive_link( $link, $post_type ) { |
| 122 |
return bogo_url( $link ); |
| 123 |
} |
| 124 |
|
| 125 |
add_filter( 'post_type_archive_feed_link', 'bogo_post_type_archive_feed_link', 10, 2 ); |
| 126 |
|
| 127 |
function bogo_post_type_archive_feed_link( $link, $feed ) { |
| 128 |
return bogo_url( $link ); |
| 129 |
} |
| 130 |
|
| 131 |
add_filter( 'term_link', 'bogo_term_link', 10, 3 ); |
| 132 |
|
| 133 |
function bogo_term_link( $link, $term, $taxonomy ) { |
| 134 |
return bogo_url( $link ); |
| 135 |
} |
| 136 |
|
| 137 |
add_filter( 'home_url', 'bogo_home_url' ); |
| 138 |
|
| 139 |
function bogo_home_url( $url ) { |
| 140 |
if ( is_admin() || ! did_action( 'template_redirect' ) ) |
| 141 |
return $url; |
| 142 |
|
| 143 |
return bogo_url( $url ); |
| 144 |
} |
| 145 |
|
| 146 |
add_action( 'wp_head', 'bogo_m17n_headers' ); |
| 147 |
|
| 148 |
function bogo_m17n_headers() { |
| 149 |
$languages = array(); |
| 150 |
|
| 151 |
if ( is_singular() ) { |
| 152 |
$post_id = get_queried_object_id(); |
| 153 |
|
| 154 |
if ( $post_id && $translations = bogo_get_post_translations( $post_id ) ) { |
| 155 |
$locale = get_locale(); |
| 156 |
$translations[$locale] = get_post( $post_id ); |
| 157 |
|
| 158 |
foreach ( $translations as $lang => $translation ) { |
| 159 |
$languages[] = array( |
| 160 |
'hreflang' => bogo_language_tag( $lang ), |
| 161 |
'href' => get_permalink( $translation ) ); |
| 162 |
} |
| 163 |
} |
| 164 |
} else { |
| 165 |
$available_locales = bogo_available_locales( array( |
| 166 |
'exclude_enus_if_inactive' => true ) ); |
| 167 |
|
| 168 |
foreach ( $available_locales as $locale ) { |
| 169 |
$languages[] = array( |
| 170 |
'hreflang' => bogo_language_tag( $locale ), |
| 171 |
'href' => bogo_url( null, $locale ) ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
$languages = apply_filters( 'bogo_rel_alternate_hreflang', $languages ); |
| 176 |
|
| 177 |
foreach ( (array) $languages as $language ) { |
| 178 |
$hreflang = isset( $language['hreflang'] ) ? $language['hreflang'] : ''; |
| 179 |
$href = isset( $language['href'] ) ? $language['href'] : ''; |
| 180 |
|
| 181 |
if ( $hreflang && $href ) { |
| 182 |
$link = sprintf( '<link rel="alternate" hreflang="%1$s" href="%2$s" />', |
| 183 |
esc_attr( $hreflang ), esc_url( $href ) ); |
| 184 |
|
| 185 |
echo $link . "\n"; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
add_filter( 'get_previous_post_join', 'bogo_adjacent_post_join', 10, 3 ); |
| 191 |
add_filter( 'get_next_post_join', 'bogo_adjacent_post_join', 10, 3 ); |
| 192 |
|
| 193 |
function bogo_adjacent_post_join( $join, $in_same_term, $excluded_terms ) { |
| 194 |
global $wpdb; |
| 195 |
|
| 196 |
$post = get_post(); |
| 197 |
|
| 198 |
if ( $post && bogo_is_localizable_post_type( get_post_type( $post ) ) ) { |
| 199 |
$join .= " LEFT JOIN $wpdb->postmeta AS postmeta_bogo ON (p.ID = postmeta_bogo.post_id AND postmeta_bogo.meta_key = '_locale')"; |
| 200 |
} |
| 201 |
|
| 202 |
return $join; |
| 203 |
} |
| 204 |
|
| 205 |
add_filter( 'get_previous_post_where', 'bogo_adjacent_post_where', 10, 3 ); |
| 206 |
add_filter( 'get_next_post_where', 'bogo_adjacent_post_where', 10, 3 ); |
| 207 |
|
| 208 |
function bogo_adjacent_post_where( $where, $in_same_term, $excluded_terms ) { |
| 209 |
global $wpdb; |
| 210 |
|
| 211 |
$post = get_post(); |
| 212 |
|
| 213 |
if ( $post && bogo_is_localizable_post_type( get_post_type( $post ) ) ) { |
| 214 |
$locale = bogo_get_post_locale( $post->ID ); |
| 215 |
|
| 216 |
$where .= " AND (1=0"; |
| 217 |
$where .= $wpdb->prepare( " OR postmeta_bogo.meta_value LIKE %s", $locale ); |
| 218 |
|
| 219 |
if ( bogo_is_default_locale( $locale ) ) { |
| 220 |
$where .= " OR postmeta_bogo.meta_id IS NULL"; |
| 221 |
} |
| 222 |
|
| 223 |
$where .= ")"; |
| 224 |
} |
| 225 |
|
| 226 |
return $where; |
| 227 |
} |
| 228 |
|