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