/** * Hooks the various dynamic actions fired when the edit post and * edit new post screens are loaded. Determines if the post to be * edited has become disconnected from it's translation group, * and shows the Reconnect metabox if it has. * * @return void **/ public function load_post() { if (!($post_id = isset($_GET['post']) ? absint($_GET['post']) : false)) { return; } $post = get_post($post_id); if (!in_array($post->post_status, array('draft', 'pending', 'publish'))) { return; } if (bbl_get_post_lang_code($post) == bbl_get_default_lang_code()) { return; } if ($default_lang_post = bbl_get_post_in_lang($post, bbl_get_default_lang_code())) { return; } add_meta_box('bbl_reconnect', 'Reconnect Translation', array($this, 'metabox_reconnect'), $post->post_type, 'side'); }
/** * Returns the permalink of a post, in the requested language (if available). * * @param int|object $post Either a WP Post object, or a post ID * @param string $lang_code The code for the language the title is requested in * @param bool $fallback Whether to provide a fallback title in the default language if the requested language is unavailable (defaults to false) * @return void **/ function bbl_get_the_permalink_in_lang($post = null, $lang_code = null, $fallback = false) { $post = get_post($post); if (is_null($lang_code)) { $lang_code = bbl_get_current_lang_code(); } // Hopefully we find the post in the right language if ($lang_post = bbl_get_post_in_lang($post, $lang_code, $fallback)) { return apply_filters('bbl_permalink_in_lang', get_permalink($lang_post->ID), $lang_code); } // We have failed… return ''; }
/** * Resync all (synced) post meta data from the post in * the default language to this post. * * @param $int The post ID to sync TO * @return void **/ function sync_post_meta($post_id) { if ($this->no_meta_recursion) { return; } $this->no_meta_recursion = 'updated_post_meta'; // First delete all the synced meta from this post $current_metas = (array) get_post_meta($post_id); foreach ($current_metas as $current_meta_key => &$current_meta_values) { // Some metadata shouldn't be synced, this filter allows a dev to return // false if the particular meta_key is one which shouldn't be synced. // If you find a core meta_key which is currently synced and should NOT be, // please submit a patch to the sync_meta_key method on this class. Thanks. if (apply_filters('bbl_sync_meta_key', true, $current_meta_key)) { delete_post_meta($post_id, $current_meta_key); } } // Now add meta in again from the origin post $origin_post = bbl_get_post_in_lang($post_id, bbl_get_default_lang_code()); $metas = get_post_meta($origin_post->ID); if (!$metas) { return; } foreach ($metas as $meta_key => &$meta_value) { // Some metadata shouldn't be synced if (!apply_filters('bbl_sync_meta_key', true, $meta_key)) { continue; } // The meta could be an array stored in a single postmeta row or an // array of values from multiple rows; work out which we have. $val_multi = get_post_meta($origin_post->ID, $meta_key); foreach ($val_multi as &$val_single) { add_post_meta($post_id, $meta_key, $val_single); } } $this->no_meta_recursion = false; }
/** * 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)); } } }