Skip to content

Commit

Permalink
Language specific homepages and remove sil_get_translation_permalink
Browse files Browse the repository at this point in the history
List (shadow) posts on language specific home pages. Closes #16.

Remove deprecated `sil_get_translation_permalink` (closes #13), and add
new API function `sil_get_post_lang` which returns the language that a
given post or post ID is written for.
  • Loading branch information
Simon Wheatley committed Sep 27, 2011
1 parent d3d7398 commit 8ee1286
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
22 changes: 8 additions & 14 deletions api.php
Expand Up @@ -72,26 +72,20 @@ function sil_get_post_translations( $post_id ) {
}

/**
* Get the language specific permalink for a particular post.
* undocumented function
*
* DEPRECATED.
*
* @param int|object $post Either the WP Post object or a post ID
* @param string $lang The language code to get the translation for
* @return object|string The permalink for the translated post, or a WP_Error if it doesn't exist
* @param int|object $post Either a WP Post object, or a post ID
* @return string|object Either a language code, or a WP_Error object
* @author Simon Wheatley
**/
function sil_get_translation_permalink( $post, $lang ) {
_doing_it_wrong( __FUNCTION__, 'We should be working this out transparently for the theme author, so they can just call get_permalink and have it work.', 1 );
function sil_get_post_lang( $post ) {
global $sil_lang_map;
$post = get_post( $post );
if ( ! $post )
return new WP_Error( 'invalid_post', __( 'Invalid Post' ) );
// @FIXME: Check the language is valid for this site
$sequestered_lang = get_query_var( 'lang' );
set_query_var( 'lang', $lang );
$permalink = get_permalink( $post->ID );
set_query_var( 'lang', $sequestered_lang );
return $permalink;
if ( isset( $sil_lang_map[ $post->post_type ] ) )
return $sil_lang_map[ $post->post_type ];
return SIL_DEFAULT_LANGUAGE;
}

?>
36 changes: 29 additions & 7 deletions translation-poc.php
Expand Up @@ -317,6 +317,8 @@ function sil_registered_taxonomy( $taxonomy, $object_type, $args ) {
/**
* Hooks the WP parse_request action
*
* FIXME: Should I be extending and replacing the WP class?
*
* @param object $wp WP object, passed by reference (so no need to return)
* @return void
**/
Expand All @@ -329,19 +331,34 @@ function sil_parse_request( $wp ) {
} elseif ( $lang = @ $_GET[ 'lang' ] ) {
$wp->query_vars[ 'lang' ] = $lang;
} else {
return; // Bail. Not sure what could trigger this though (site root URL?)
$wp->query_vars[ 'lang' ] = SIL_DEFAULT_LANGUAGE;
}
error_log( "Request: " . print_r( $wp->request, true ) );
error_log( "Original Query: " . print_r( $wp->query_vars, true ) );

// Sequester the original query, in case we need it to get the default content later
$wp->query_vars[ 'sil_original_query' ] = $wp->query_vars;

// Detect language specific homepages
if ( $wp->request == $wp->query_vars[ 'lang' ] ) {
unset( $wp->query_vars[ 'error' ] );
// @FIXME: Cater for front pages which don't list the posts
// Trigger the archive listing for the relevant shadow post type
// for this language.
if ( 'en' != $wp->query_vars[ 'lang' ] ) {
$wp->query_vars[ 'post_type' ] = 'post_' . $wp->query_vars[ 'lang' ];
}
return;
}

// If we're asking for the default content, it's fine
// error_log( "Original query: " . print_r( $wp->query_vars, true ) );
if ( 'en' == $wp->query_vars[ 'lang' ] ) {
// error_log( "Default content" );
error_log( "New Query 0: " . print_r( $wp->query_vars, true ) );
return;
}

// Sequester the original query, in case we need it to get the default content later
$wp->query_vars[ 'sil_original_query' ] = $wp->query_vars;

// Now swap the query vars so we get the content in the right language post_type

// @FIXME: Do I need to change $wp->matched query? I think $wp->matched_rule is fine?
Expand All @@ -359,6 +376,7 @@ function sil_parse_request( $wp ) {
} elseif ( isset( $wp->query_vars[ 'post_type' ] ) ) {
$wp->query_vars[ 'post_type' ] = $wp->query_vars[ 'post_type' ] . '_' . $wp->query_vars[ 'lang' ];
}
error_log( "New Query 1: " . print_r( $wp->query_vars, true ) );
}
add_action( 'parse_request', 'sil_parse_request' );

Expand Down Expand Up @@ -443,14 +461,14 @@ function sil_admin_bar_menu( $wp_admin_bar ) {
foreach ( $langs as $i => & $lang )
if ( $lang == sil_get_current_lang_code() )
unset( $langs[ $i ] );

$translations = sil_get_post_translations( get_the_ID() );

foreach ( $langs as $i => & $lang ) {
if ( is_admin() ) {
$href = add_query_arg( array( 'lang' => $lang ) );
} else {
$href = sil_get_translation_permalink( $translations[ $lang ]->ID, $lang );
$href = get_permalink( $translations[ $lang ]->ID );
}
$args = array(
'id' => "sil_languages_$lang",
Expand Down Expand Up @@ -519,7 +537,7 @@ function sil_add_menu_classes( $menu ) {
* @return string The permalink
**/
function sil_post_type_link( $post_link, $post, $leavename, $sample ) {
global $sil_post_types, $wp_rewrite;
global $sil_post_types, $sil_lang_map, $wp_rewrite;
// var_dump( "Post type link ($post->post_type): $post_link" );
// var_dump( $sil_post_types );
// exit;
Expand Down Expand Up @@ -593,7 +611,11 @@ function sil_post_type_link( $post_link, $post, $leavename, $sample ) {
$author,
$post->post_name,
);
$sequestered_lang = get_query_var( 'lang' );
$lang = sil_get_post_lang( $post );
set_query_var( 'lang', $lang );
$post_link = home_url( str_replace( $rewritecode, $rewritereplace, $post_link ) );
set_query_var( 'lang', $sequestered_lang );
$post_link = user_trailingslashit($post_link, 'single');
// END copying from get_permalink function
} else { // if they're not using the fancy permalink option
Expand Down

0 comments on commit 8ee1286

Please sign in to comment.