/**
  * Hooks the WP get_comments_number filter to get the number of comments
  * across all posts in the translation group.
  *
  * @param int $count The number of comments on the single translation
  * @param int $post_id The post ID of the single translation
  * @return int The count of all comments on published posts in this translation group
  **/
 public function get_comments_number($count, $post_id)
 {
     $translations = bbl_get_post_translations($post_id);
     $count = 0;
     foreach ($translations as &$translation) {
         $post_status = get_post_status_object($translation->post_status);
         // FIXME: I'm not entirely sure about using publicly_queryable here… what I want to avoid is draft, private, etc statii.
         if ($post_status->publicly_queryable) {
             $count += $translation->comment_count;
         }
     }
     return $count;
 }
Esempio n. 2
0
 /**
  * Returns the post ID for the post in the default language from which
  * this post was translated.
  *
  * @param int|WP_Post $post Either a WP Post object, or a post ID
  * @return int The ID of the default language equivalent post
  **/
 public function get_default_lang_post($post)
 {
     $post = get_post($post);
     $translations = bbl_get_post_translations($post->ID);
     if (isset($translations[bbl_get_default_lang_code()])) {
         return $translations[bbl_get_default_lang_code()];
     }
     return false;
 }
 /**
  * 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();
     }
 }
    public function metabox_post_translations(WP_Post $post, array $metabox)
    {
        $trans = bbl_get_post_translations($post);
        $incomplete_jobs = $this->get_incomplete_post_jobs($post);
        $completed_jobs = $this->get_completed_post_jobs($post);
        $default = bbl_get_default_lang_code();
        # The ability to create a translation of a post directly
        # maps to the ability to publish the canonical post.
        $capable = current_user_can('publish_post', $post->ID);
        unset($trans[$default]);
        if (!empty($trans)) {
            if (!empty($completed_jobs) and $capable) {
                ?>
<h4><?php 
                esc_html_e('Complete:', 'babble');
                ?>
</h4><?php 
            }
            foreach ($completed_jobs as $lang_code => $job) {
                $lang = bbl_get_lang($lang_code);
                ?>
				<p><?php 
                printf('%s: <a href="%s">%s</a>', esc_html($lang->display_name), esc_url(get_edit_post_link($job->ID)), esc_html__('View', 'babble'));
                ?>
				<?php 
            }
        }
        if (!empty($incomplete_jobs) and $capable) {
            ?>
<h4><?php 
            esc_html_e('Pending:', 'babble');
            ?>
</h4><?php 
            foreach ($incomplete_jobs as $job) {
                $lang = $this->get_job_language($job);
                $status = get_post_status_object($job->post_status);
                ?>
				<p><?php 
                printf('%s (%s)', esc_html($lang->display_name), esc_html($status->label));
                ?>
				<?php 
            }
            $args = array('post_type' => 'bbl_job', 'bbl_job_post' => "{$post->post_type}|{$post->ID}");
            ?>
			<p><a href="<?php 
            echo esc_url(add_query_arg($args, admin_url('edit.php')));
            ?>
"><?php 
            esc_html_e('View pending translation jobs &raquo;', 'babble');
            ?>
</a></p>
			<?php 
        } else {
            if ($capable) {
                wp_nonce_field("bbl_ready_for_translation-{$post->ID}", '_bbl_ready_for_translation');
                ?>
			<p><label><input type="checkbox" name="babble_ready_for_translation" value="<?php 
                echo absint($post->ID);
                ?>
" /> <?php 
                esc_html_e('Ready for translation', 'babble');
                ?>
</label></p>
			<?php 
            } else {
                ?>
			<p><?php 
                esc_html_e(_x('None', 'No translations', 'babble'));
                ?>
</p>
			<?php 
            }
        }
    }
Esempio n. 5
0
 /**
  * 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;
                 }
                 $translated_term = $this->get_term_in_lang($_term->term_id, $taxonomy, $lang_code, false);
                 $translated_terms[] = (int) $translated_term->term_id;
             }
             $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;
 }