public function test_home_url() { $switched = bbl_switch_to_lang('fr_FR'); $this->assertTrue($switched); // The `query_vars` filter is only triggered once a query has been made, so we need to trigger one. $this->go_to(home_url('/test/')); $home_url = trailingslashit(home_url()); $this->assertContains('/fr/', $home_url); $switched = bbl_switch_to_lang('en_GB'); $this->assertTrue($switched); $home_url = trailingslashit(home_url()); $this->assertContains('/uk/', $home_url); // switch back to fr_FR bbl_restore_lang(); $home_url = trailingslashit(home_url()); $this->assertContains('/fr/', $home_url); // switch back to en_US bbl_restore_lang(); }
/** * Hooks the WP manage_posts_custom_column action to add our “link” content. * * @param string $column_name The name of this column * @param int $post_id The ID for the post for the row which parents this column * @return void **/ public function manage_posts_custom_column($column_name, $post_id) { if ('bbl_link' != $column_name) { return; } $default_post = bbl_get_post_in_lang($post_id, bbl_get_default_lang_code()); if (!$default_post) { echo '<em style="color: #bc0b0b">' . __('No link', 'babble') . '</em>'; return; } $edit_link = get_edit_post_link($default_post->ID); $edit_link = add_query_arg(array('lang' => bbl_get_default_lang_code()), $edit_link); bbl_switch_to_lang(bbl_get_default_lang_code()); $view_link = get_permalink($default_post->ID); bbl_restore_lang(); $edit_title = esc_attr(sprintf(__('Edit the originating post: “%s”', 'babble'), get_the_title($default_post->ID))); $view_title = esc_attr(sprintf(__('View the originating post: “%s”', 'babble'), get_the_title($default_post->ID))); echo "<a href='{$view_link}' title='{$view_title}'>" . __('View', 'babble') . "</a> | <a href='{$edit_link}' title='{$edit_title}'>" . __('Edit', 'babble') . "</a>"; }
/** * Add a link to an arbitrary link, e.g. 404, within the site. * * @param object $lang A Babble language object for this link * @return void **/ protected function add_arbitrary_link($lang) { $classes = array(); if (!preg_match('|^/[^/]+/(.*)?|', $_SERVER['REQUEST_URI'], $matches)) { return; } bbl_switch_to_lang($lang->code); $href = home_url($matches[1]); bbl_restore_lang(); $href = apply_filters('bbl_switch_arbitrary_link', $href, $lang); $title = sprintf(__('Switch to %s', 'babble'), $lang->display_name); $classes[] = 'bbl-existing'; $classes[] = 'bbl-existing-term'; $classes[] = "bbl-lang-{$lang->code} bbl-lang-{$lang->url_prefix}"; $classes[] = 'bbl-lang'; $classes[] = 'bbl-term'; if ($lang == bbl_get_current_lang_code()) { $classes[] = 'bbl-active'; } $this->links[$lang->code] = array('classes' => $classes, 'href' => $href, 'id' => $lang->url_prefix, 'meta' => array('class' => strtolower(join(' ', array_unique($classes)))), 'title' => $title, 'lang' => $lang); }
/** * Returns the link to a post type in a particular language. * * @param string $post_type A post type for which you want a translated archive link * @param string $lang_code The code for the language the link is requested in * @return void **/ function bbl_get_post_type_archive_link_in_lang($post_type, $lang_code = null) { if (is_null($lang_code)) { $lang_code = bbl_get_current_lang_code(); } bbl_switch_to_lang($lang_code); $lang_post_type = bbl_get_post_type_in_lang($post_type, $lang_code); $link = get_post_type_archive_link($lang_post_type); bbl_restore_lang(); return apply_filters('bbl_post_type_archive_link_in_lang', $link); }
/** * Hooks the WordPress term_link filter to provide functions to provide * appropriate links for the shadow taxonomies. * * @see get_term_link from whence much of this was copied * * @param string $termlink The currently generated term URL * @param object $term The WordPress term object we're generating a link for * @param string $taxonomy The * @return string The term link **/ public function term_link($termlink, $term, $taxonomy) { $taxonomy = strtolower($taxonomy); // No need to worry about the built in taxonomies if ('post_tag' == $taxonomy || 'category' == $taxonomy || !isset($this->taxonomies[$taxonomy])) { return $termlink; } // Deal with our shadow taxonomies if (!($base_taxonomy = $this->get_base_taxonomy($taxonomy))) { return $termlink; } // START copying from get_term_link, replacing $taxonomy with $base_taxonomy global $wp_rewrite; if (!is_object($term)) { if (is_int($term)) { $term =& get_term($term, $base_taxonomy); } else { $term =& get_term_by('slug', $term, $base_taxonomy); } } if (!is_object($term)) { $term = new WP_Error('invalid_term', esc_html__('Empty Term', 'babble')); } if (is_wp_error($term)) { return $term; } $termlink = $wp_rewrite->get_extra_permastruct($base_taxonomy); $slug = $term->slug; $t = get_taxonomy($base_taxonomy); $lang = $this->get_taxonomy_lang_code($taxonomy); bbl_switch_to_lang($lang); if (empty($termlink)) { if ('category' == $base_taxonomy) { $termlink = '?cat=' . $term->term_id; } elseif ($t->query_var) { $termlink = "?{$t->query_var}={$slug}"; } else { $termlink = "?taxonomy={$base_taxonomy}&term={$slug}"; } $termlink = home_url($termlink); } else { if ($t->rewrite['hierarchical']) { $hierarchical_slugs = array(); $ancestors = get_ancestors($term->term_id, $base_taxonomy); foreach ((array) $ancestors as $ancestor) { $ancestor_term = get_term($ancestor, $base_taxonomy); $hierarchical_slugs[] = $ancestor_term->slug; } $hierarchical_slugs = array_reverse($hierarchical_slugs); $hierarchical_slugs[] = $slug; $termlink = str_replace("%{$base_taxonomy}%", implode('/', $hierarchical_slugs), $termlink); } else { $termlink = str_replace("%{$base_taxonomy}%", $slug, $termlink); } $termlink = home_url(user_trailingslashit($termlink, 'category')); } bbl_restore_lang(); // STOP copying from get_term_link return $termlink; }