/** * Hooks the WP the_posts filter on WP_Query. * * Check the post_title, post_excerpt, post_content and substitute from * the default language where appropriate. * * @param array $posts The posts retrieved by WP_Query, passed by reference * @param WP_Query $wp_query The WP_Query, passed by reference * @return array The posts **/ public function the_posts(array $posts, WP_Query &$wp_query) { if (is_admin()) { return $posts; } // Get fallback content in the default language $subs_index = array(); foreach ($posts as &$post) { if (empty($post->post_title) || empty($post->post_excerpt) || empty($post->post_content)) { if ($default_post = bbl_get_default_lang_post($post->ID)) { $subs_index[$post->ID] = $default_post->ID; } } if (!$this->get_transid($post) && bbl_get_default_lang_code() == bbl_get_post_lang_code($post)) { $this->set_transid($post); } } if (!$subs_index) { return $posts; } $subs_posts = get_posts(array('include' => array_values($subs_index), 'post_status' => 'publish')); // @FIXME: Check the above get_posts call results are cached somewhere… I think they are // @FIXME: Alternative approach: hook on save_post to save the current value to the translation, BUT content could get out of date – in post_content_filtered foreach ($posts as &$post) { // @TODO why does this only override the title/excerpt/content? Why not override the post object entirely? // @FIXME: I'm assuming this get_post call is cached, which it seems to be if (isset($subs_index[$post->ID])) { $default_post = get_post($subs_index[$post->ID]); if (empty($post->post_title)) { $post->post_title = $default_post->post_title; } if (empty($post->post_excerpt)) { $post->post_excerpt = $default_post->post_excerpt; } if (empty($post->post_content)) { $post->post_content = $default_post->post_content; } } } return $posts; }
/** * Hooks the WP filter get_edit_post_link * * @filter get_edit_post_link * @param string $url The edit post link URL * @param int $post_ID The ID of the post to edit * @param string $context The link context. * * @return string The edit post link URL * @author Simon Wheatley **/ public function get_edit_post_link($url, $post_ID, $context) { if ($this->no_recursion) { return $url; } if (bbl_get_default_lang_code() == bbl_get_post_lang_code($post_ID)) { return $url; } $completed_jobs = $this->get_completed_post_jobs(bbl_get_default_lang_post($post_ID)); if (!isset($completed_jobs[bbl_get_current_lang_code()])) { return $url; } $job = $completed_jobs[bbl_get_current_lang_code()]; if (!current_user_can('publish_post', $job->ID)) { return $url; } $this->no_recursion = true; $url = get_edit_post_link($completed_jobs[bbl_get_current_lang_code()]->ID); $this->no_recursion = false; return $url; }