/** * Hooks the WP admin_init action to redirect any requests accessing * content which is not in the current language. * * @return void **/ function bbl_admin_init() { global $pagenow; $taxonomy = isset($_GET['taxonomy']) ? sanitize_text_field($_GET['taxonomy']) : false; $post_type = isset($_GET['post_type']) ? sanitize_text_field($_GET['post_type']) : false; // Deal with the special URL case of the listing screens for vanilla posts if (!$post_type && 'edit.php' == $pagenow) { $post_type = 'post'; } $cur_lang_code = bbl_get_current_lang_code(); if ($taxonomy) { $new_taxonomy = bbl_get_taxonomy_in_lang($taxonomy, $cur_lang_code); if ($taxonomy != $new_taxonomy) { $url = add_query_arg(array('taxonomy' => rawurlencode($new_taxonomy))); wp_safe_redirect($url); exit; } } if ($post_type) { $new_post_type = bbl_get_post_type_in_lang($post_type, $cur_lang_code); if ($post_type != $new_post_type) { $url = add_query_arg(array('post_type' => rawurlencode($new_post_type))); wp_safe_redirect($url); exit; } } }
/** * Add an admin list terms screen link to the parent link provided (by reference). * * @param object $lang A Babble language object for this link * @return void **/ protected function add_admin_list_terms_link($lang) { $classes = array(); $args = array('lang' => $lang->code, 'taxonomy' => bbl_get_taxonomy_in_lang($this->screen->taxonomy, $lang->code)); $href = add_query_arg($args); $href = apply_filters('bbl_switch_admin_list_terms_link', $href, $lang); $title = sprintf(__('Switch to %s', 'babble'), $lang->display_name); $classes[] = "bbl-lang-{$lang->code} bbl-lang-{$lang->url_prefix}"; $classes[] = 'bbl-admin'; $classes[] = 'bbl-admin-taxonomy'; $classes[] = 'bbl-admin-list-terms'; $classes[] = 'bbl-lang'; if ($lang->code == 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); }
/** * Checks for the relevant POSTed field, then * resyncs the terms. * * @param int $post_id The ID of the WP post * @param object $post The WP Post object * @return void **/ protected function maybe_resync_terms($post_id, $post) { // Check that the fields were included on the screen, we // can do this by checking for the presence of the nonce. $nonce = isset($_POST['_bbl_metabox_resync']) ? $_POST['_bbl_metabox_resync'] : false; if (!in_array($post->post_status, array('draft', 'publish'))) { return; } if (!$nonce) { return; } $posted_id = isset($_POST['post_ID']) ? $_POST['post_ID'] : 0; if ($posted_id != $post_id) { return; } // While we're at it, let's check the nonce check_admin_referer("bbl_resync_translation-{$post_id}", '_bbl_metabox_resync'); if ($this->no_recursion) { return; } $this->no_recursion = true; $taxonomies = get_object_taxonomies($post->post_type); $origin_post = bbl_get_post_in_lang($post_id, bbl_get_default_lang_code()); // First dissociate all the terms from synced taxonomies from this post wp_delete_object_term_relationships($post_id, $taxonomies); // Now associate terms from synced taxonomies in from the origin post foreach ($taxonomies as $taxonomy) { $origin_taxonomy = $taxonomy; if ($this->is_taxonomy_translated($taxonomy)) { $origin_taxonomy = bbl_get_taxonomy_in_lang($taxonomy, bbl_get_default_lang_code()); } $term_ids = wp_get_object_terms($origin_post->ID, $origin_taxonomy, array('fields' => 'ids')); $term_ids = array_map('absint', $term_ids); $result = wp_set_object_terms($post_id, $term_ids, $taxonomy); if (is_wp_error($result, true)) { throw new exception("Problem syncing terms: " . print_r($terms, true), " Error: " . print_r($result, true)); } } }
/** * Hooks the WP set_object_terms action to sync any untranslated * taxonomies across to the translations. * * @param int $object_id The object to relate to * @param array $terms The slugs or ids of the terms * @param array $tt_ids The term_taxonomy_ids * @param string $taxonomy The name of the taxonomy for which terms are being set * @param bool $append If false will delete difference of terms * @return void **/ public function set_object_terms($object_id, $terms, $tt_ids, $taxonomy, $append) { if ($this->no_recursion) { return; } $this->no_recursion = true; // DO NOT SYNC THE TRANSID TAXONOMIES!! if (in_array($taxonomy, $this->ignored_taxonomies())) { $this->no_recursion = false; return; } if ($this->is_taxonomy_translated($taxonomy)) { // Here we assume that this taxonomy is on a post type $translations = bbl_get_post_translations($object_id); foreach ($translations as $lang_code => &$translation) { if (bbl_get_post_lang_code($object_id) == $lang_code) { continue; } $translated_taxonomy = bbl_get_taxonomy_in_lang($taxonomy, $lang_code); $translated_terms = array(); foreach ($terms as $term) { if (is_int($term)) { $_term = get_term($term, $taxonomy); } else { $_term = get_term_by('name', $term, $taxonomy); } if (is_wp_error($_term) or empty($_term)) { continue; } if ($translated_term = $this->get_term_in_lang($_term->term_id, $taxonomy, $lang_code, false)) { $translated_terms[] = (int) $translated_term->term_id; } } if ($translated_terms) { $result = wp_set_object_terms($translation->ID, $translated_terms, $translated_taxonomy, $append); } } } else { // Here we assume that this taxonomy is on a post type $translations = bbl_get_post_translations($object_id); foreach ($translations as $lang_code => &$translation) { if (bbl_get_post_lang_code($object_id) == $lang_code) { continue; } wp_set_object_terms($translation->ID, $terms, $taxonomy, $append); } } $this->no_recursion = false; }