public function personal_options(WP_User $user) { $langs = bbl_get_active_langs(); $current = bbl_get_current_interface_lang_code(); if (empty($langs)) { return; } $vars = compact('langs', 'current'); $this->render_admin('switcher-interface.php', $vars); }
/** * Returns an array of all the shadow post types associated with * this post type. * * @param string $base_post_type The post type to look up shadow post types for * @return array The names of all the related shadow post types **/ public function get_shadow_post_types($base_post_type) { $post_types = array(); $langs = bbl_get_active_langs(); foreach ($langs as $lang) { if (isset($this->lang_map2[$lang->code][$base_post_type])) { $post_types[] = $this->lang_map2[$lang->code][$base_post_type]; } } return $post_types; }
/** * undocumented function * * @return void **/ protected function populate_links() { if (is_array($this->links) && !empty($this->links)) { return; } // Already done $this->links = array(); // @FIXME: Not sure this is the best way to specify languages $alt_langs = bbl_get_active_langs(); $this->screen = is_admin() ? get_current_screen() : false; // Create a handy flag for whether we're editing a post or listing posts $editing_post = false; $listing_posts = false; if (is_admin()) { $editing_post = is_admin() && 'post' == $this->screen->base && isset($_GET['post']); $listing_posts = is_admin() && 'edit' == $this->screen->base && !isset($_GET['post']); } // Create a handy flag for whether we're editing a term or listing terms $editing_term = false; $listing_terms = false; if (is_admin()) { $editing_term = is_admin() && 'edit-tags' == $this->screen->base && isset($_GET['tag_ID']); $listing_terms = is_admin() && 'edit-tags' == $this->screen->base && !isset($_GET['tag_ID']); } if (is_singular() || is_single() || $editing_post) { $this->translations = bbl_get_post_translations(get_the_ID()); $this->jobs = bbl_get_incomplete_post_jobs(get_the_ID()); } else { if ('page' == get_option('show_on_front') && is_home()) { $this->translations = bbl_get_post_translations(get_option('page_for_posts')); $this->jobs = bbl_get_incomplete_post_jobs(get_option('page_for_posts')); } else { if ((!is_admin() and is_tax() || is_category()) || $editing_term) { if (isset($_REQUEST['tag_ID'])) { $term = get_term((int) @$_REQUEST['tag_ID'], $this->screen->taxonomy); } else { $term = get_queried_object(); } $this->translations = bbl_get_term_translations($term->term_id, $term->taxonomy); $this->jobs = bbl_get_term_jobs($term->term_id, $term->taxonomy); } } } foreach ($alt_langs as $i => &$alt_lang) { // @TODO: Convert to a switch statement, convert all the vars to a single property on the class if (is_admin()) { if ($editing_post) { // Admin: Editing post link $this->add_admin_post_link($alt_lang); } else { if ($editing_term) { // Admin: Editing term link $this->add_admin_term_link($alt_lang); } else { if ($listing_posts) { // Admin: Listing posts link $this->add_admin_list_posts_link($alt_lang); } else { if ($listing_terms) { // Admin: Listing terms link $this->add_admin_list_terms_link($alt_lang); } else { // Admin: Generic link link $this->add_admin_generic_link($alt_lang); } } } } continue; } if (is_singular() || is_single() || 'page' == get_option('show_on_front') && is_home()) { // Single posts, pages, blog homepage $this->add_post_link($alt_lang); continue; } // Don't add a switcher link if the language is not public and // the user cannot edit any posts (as a rough guide to whether // they are more than just a subscriber). // @TODO this cap check should move into each add_*_link() method: if (!bbl_is_public_lang($alt_lang->code) && !current_user_can('edit_posts')) { continue; } if (is_front_page()) { // Language homepage // is_front_page works for language homepages, phew $this->add_front_page_link($alt_lang); } else { if (is_post_type_archive()) { // Post type archives $this->add_post_type_archive_link($alt_lang); } else { if (is_tax() || is_category()) { // Category or taxonomy archive $this->add_taxonomy_archive_link($alt_lang); } else { // 404's, amongst other things $this->add_arbitrary_link($alt_lang); } } } } // Make up the class attribute on all links foreach ($this->links as $lang_code => &$link) { $link['class'] = implode(' ', $link['classes']); $link['active'] = $lang_code == bbl_get_current_lang_code(); } }
/** * Create empty translations of a post for all languages. Called via WP-Cron on the `babble_create_empty_translation` hook. * * @param array $args Args array containing a `post_id` element. */ public function create_empty_translation(array $args) { if (!($post = get_post($args['post_id']))) { return; } $bbl_post_public = Babble::get('post_public'); foreach (bbl_get_active_langs() as $lang) { if (!($trans = $bbl_post_public->get_post_in_lang($post, $lang->code, false))) { $trans = $bbl_post_public->initialise_translation($post, $lang->code); } $post_data = array('ID' => $trans->ID, 'post_status' => $post->post_status, 'post_name' => $post->post_name); $this->no_recursion = true; wp_update_post($post_data, true); $this->no_recursion = false; } }
/** * Hooks the WP locale filter to switch locales whenever we gosh darned want. * * @param string $locale The locale * @return string The locale **/ public function set_locale($locale) { // Deal with the special case of wp-comments-post.php if (false !== stristr($_SERVER['REQUEST_URI'], 'wp-comments-post.php')) { // @TODO we should be able to hook into an action here (pre_comment_post) rather than looking at the URL. if ($comment_post_ID = isset($_POST['comment_post_ID']) ? (int) $_POST['comment_post_ID'] : false) { if (!isset($this->content_lang)) { $this->set_content_lang(bbl_get_post_lang_code($comment_post_ID)); } return $this->content_lang; } } if (is_admin()) { if (isset($this->interface_lang)) { return $this->interface_lang; } } else { if (isset($this->content_lang)) { return $this->content_lang; } } // $current_user = wp_get_current_user(); if ($lang = $this->get_cookie_interface_lang()) { $this->set_interface_lang($lang); } // $current_user = wp_get_current_user(); if ($lang = $this->get_cookie_content_lang()) { $this->set_content_lang($lang); } $active_langs = bbl_get_active_langs(); $active_lang_codes = wp_list_pluck($active_langs, 'code'); $active_lang_prefixes = wp_list_pluck($active_langs, 'url_prefix'); if (is_admin()) { if (isset($_POST['interface_lang'])) { $lang = $_POST['interface_lang']; if (!in_array($lang, $active_lang_codes, true)) { $lang = bbl_get_default_lang_code(); } $this->set_interface_lang($lang); } if (isset($_GET['lang'])) { $lang = $_GET['lang']; if (!in_array($lang, $active_lang_codes, true)) { $lang = bbl_get_default_lang_code(); } $this->set_content_lang($lang); } } else { // Front end if (preg_match($this->lang_regex, $this->get_request_string(), $matches)) { if (in_array($matches[0], $active_lang_prefixes, true)) { $this->set_content_lang_from_prefix($matches[0]); } } } if (!isset($this->content_lang) || !$this->content_lang) { $this->set_content_lang(bbl_get_default_lang_code()); } if (!isset($this->interface_lang) || !$this->interface_lang) { $this->set_interface_lang(bbl_get_default_lang_code()); } if (is_admin()) { return $this->interface_lang; } else { return $this->content_lang; } }
/** * Get the terms which are the translations for the provided * term ID. N.B. The returned array of term objects (and false * values) will include the term for the term ID passed. * * @FIXME: We should cache the translation groups, as we do for posts * * @param int|object $term Either a WP Term object, or a term_id * @return array Either an array keyed by the site languages, each key containing false (if no translation) or a WP Term object **/ public function get_term_translations($term, $taxonomy) { $term = get_term($term, $taxonomy); $langs = bbl_get_active_langs(); $translations = array(); foreach ($langs as $lang) { $translations[$lang->code] = false; } $transid = $this->get_transid($term->term_id); // I thought the fracking bug where the get_objects_in_term function returned integers // as strings was fixed. Seems not. See #17646 for details. Argh. $term_ids = array_map('absint', get_objects_in_term($transid, 'term_translation')); // We're dealing with terms across multiple taxonomies $base_taxonomy = isset($this->taxonomies[$taxonomy]) ? $this->taxonomies[$taxonomy] : $taxonomy; $taxonomies = array(); $taxonomies[] = $base_taxonomy; foreach ($this->lang_map as $lang_taxes) { if ($lang_taxes[$base_taxonomy]) { $taxonomies[] = $lang_taxes[$base_taxonomy]; } } // Get all the translations in one cached DB query $existing_terms = get_terms($taxonomies, array('include' => $term_ids, 'hide_empty' => false)); // Finally, we're ready to return the terms in this // translation group. $terms = array(); foreach ($existing_terms as $t) { $terms[$this->get_taxonomy_lang_code($t->taxonomy)] = $t; } return $terms; }
/** * Get the terms which are the translations for the provided * term ID. N.B. The returned array of term objects (and false * values) will include the term for the term ID passed. * * @FIXME: We should cache the translation groups, as we do for posts * * @param int|object $term Either a WP Term object, or a term_id * @return array Either an array keyed by the site languages, each key containing false (if no translation) or a WP Term object **/ public function get_term_translations($term, $taxonomy) { $term = get_term($term, $taxonomy); $transid = $this->get_transid($term->term_id); $group = 'bbl_term_translation_ids'; $term_ids = wp_cache_get($transid, $group); if (false === $term_ids) { $langs = bbl_get_active_langs(); $translations = array(); foreach ($langs as $lang) { $translations[$lang->code] = false; } $term_ids = array_map('absint', get_objects_in_term($transid, 'term_translation')); wp_cache_set($transid, $term_ids, $group); } // We're dealing with terms across multiple taxonomies $base_taxonomy = isset($this->taxonomies[$taxonomy]) ? $this->taxonomies[$taxonomy] : $taxonomy; $taxonomies = array(); $taxonomies[] = $base_taxonomy; foreach ($this->lang_map as $lang_taxes) { if ($lang_taxes[$base_taxonomy]) { $taxonomies[] = $lang_taxes[$base_taxonomy]; } } // Get all the translations in one cached DB query $existing_terms = get_terms($taxonomies, array('include' => $term_ids, 'hide_empty' => false)); // Finally, we're ready to return the terms in this // translation group. $terms = array(); foreach ($existing_terms as $t) { $terms[$this->get_taxonomy_lang_code($t->taxonomy)] = get_term($t, $t->taxonomy); } return $terms; }