/**
  * @param string $post_type
  * @param int $post_parent
  * @return void
  */
 public function find_post_parents($post_type, $post_parent)
 {
     if (!is_post_type_hierarchical($post_type)) {
         return;
     }
     if (0 < $post_parent) {
         $this->parent_elements = mlp_get_linked_elements($post_parent);
     }
 }
    /**
     * show the metabox
     *
     * @access  public
     * @since   0.1
     * @uses	get_site_option, get_current_blog_id
     * @param   $post post object
     * @return  void
     */
    public function display_cpt_meta_box($post)
    {
        $linked = mlp_get_linked_elements($post->ID);
        if (0 < count($linked)) {
            // post is a linked post
            $languages = mlp_get_available_languages();
            if (0 < count($languages)) {
                ?>
				<select name="multilingual_press" id="multilingual_press">
					<option><?php 
                _e('choose preview language', 'multilingualpress');
                ?>
</option>
				<?php 
                foreach ($languages as $language_blogid => $language_name) {
                    if ($language_blogid != get_current_blog_id()) {
                        ?>
						<option value="<?php 
                        echo $language_blogid;
                        ?>
"><?php 
                        echo $language_name;
                        ?>
</option>
						<?php 
                    }
                }
                ?>
				</select>
				<div id="multilingual_press_content"></div>

				<script type="text/javascript">
					//<![CDATA[
					jQuery( document ).ready( function( $ ) {
						$( '#multilingual_press' ).change( function() {

							blogid = '';
							$( '#multilingual_press option:selected' ).each( function () {
								blogid += $( this ).attr( 'value' );
							} );

							$.post( ajaxurl,
								{
									action: 'get_metabox_content',
									blogid: blogid,
									post: <?php 
                echo $post->ID;
                ?>
								},
								function( returned_data ) {
									if ( '' != returned_data ) {
										$( '#multilingual_press_content' ).html( returned_data );
										$( '#multilingual_press_content #headline' ).remove();
										$( '#multilingual_press_content #content' ).remove();
									}
								}
							);
						} );
					} );
					//]]>
				</script>
				<?php 
            }
        }
    }
 /**
  * Get the selected blog's post permalink
  *
  * @since	0.1
  * @access	private
  * @param	int $blog_id
  * @param	int $post_id
  * @uses	mlp_get_linked_elements, get_current_blog_id, get_blog_post, get_blog_permalink
  * @return	string $permalink | the post permalink
  */
 private function get_element_permalink($blog_id, $post_id)
 {
     // Get blog id of desired blog
     $remote_blog_id = intval($blog_id);
     // Get all elements linked to the current one
     $elements = mlp_get_linked_elements(intval($post_id), '', get_current_blog_id());
     // No linked elements found
     if (array() == $elements || empty($elements[$remote_blog_id])) {
         return '';
     }
     $remote_post_id = intval($elements[$remote_blog_id]);
     $post = get_blog_post($remote_blog_id, $remote_post_id);
     if (is_object($post) && 'publish' == $post->post_status) {
         $permalink = get_blog_permalink($remote_blog_id, $remote_post_id);
     } else {
         return '';
     }
     if (1 < strlen($permalink)) {
         return $permalink;
     }
     return '';
 }
 /**
  * Check for elements we can redirect to
  * and do the redirect.
  *
  * @since 0.2
  * @param int $found | Blog ID
  * @param string $lang | Language code of current blog
  * @return FALSE | If no related element found
  */
 private function do_redirect($found, $lang)
 {
     // Get currently queried object
     $object = get_queried_object();
     if (!$object) {
         if (is_home()) {
             wp_redirect(get_site_url($found));
             exit;
         }
         return FALSE;
     }
     $url = '';
     // Can we redirect to a specific element?
     // @TODO: make calling mlp_get_linked_elements easier, i.e. no parameters necessary
     $linked_elements = mlp_get_linked_elements($object->ID, '', get_current_blog_id());
     // Redirect to specific element within blog. Above
     // function returns array() when nothing found,
     // so ! is_array won't work here.
     if (array() !== $linked_elements) {
         $post = get_blog_post($found, $linked_elements[$found]);
         // Is the post status 'publish'?
         if ('publish' == $post->post_status) {
             $url = get_blog_permalink($found, $linked_elements[$found]);
         }
     }
     // No related elements found
     if ('' == $url) {
         return FALSE;
     }
     // Otherwise do the redirect
     wp_redirect($url);
     exit;
 }
 /**
  * update post meta
  *
  * @param   int $post_id ID of the post
  * @return  void
  */
 public function save_post($post_id)
 {
     // leave function if box was not available
     if (!isset($_POST['trasher_box'])) {
         return;
     }
     // We're only interested in published posts at this time
     $post_status = get_post_status($post_id);
     if ('publish' !== $post_status && 'draft' !== $post_status) {
         return;
     }
     // Avoid recursion:
     // wp_insert_post() invokes the save_post hook, so we have to make sure
     // the loop below is only entered once per save action. Therefore we save
     // the source_blog in a static class variable. If it is already set we
     // know the loop has already been entered and we can exit the save action.
     if (NULL === self::$source_blog) {
         self::$source_blog = get_current_blog_id();
     } else {
         return;
     }
     // old key
     delete_post_meta($post_id, 'trash_the_other_posts');
     $trash_the_other_posts = FALSE;
     // Should the other post also been trashed?
     if (isset($_POST['_trash_the_other_posts']) && 'on' == $_POST['_trash_the_other_posts']) {
         update_post_meta($post_id, '_trash_the_other_posts', '1');
         $trash_the_other_posts = TRUE;
     } else {
         update_post_meta($post_id, '_trash_the_other_posts', '0');
     }
     // Get linked posts
     $linked_posts = mlp_get_linked_elements($post_id);
     foreach ($linked_posts as $linked_blog => $linked_post) {
         switch_to_blog($linked_blog);
         delete_post_meta($linked_post, 'trash_the_other_posts');
         // Should the other post also been trashed?
         if ($trash_the_other_posts) {
             update_post_meta($linked_post, '_trash_the_other_posts', '1');
         } else {
             update_post_meta($linked_post, '_trash_the_other_posts', '0');
         }
         restore_current_blog();
     }
 }
 /**
  * Get the linked elements and display them as a list
  * flag from a blogid
  *
  * @since	0.1
  * @access	public
  * @param	int $blog_id ID of a blog
  * @uses	mlp_get_available_languages, mlp_get_available_languages_titles, is_single,
  * 			is_page, mlp_get_linked_elements, mlp_get_language_flag, get_current_blog_id,
  * 			get_blog_post, get_site_url
  * @return	string output of the bloglist
  */
 public static function show_linked_elements($args)
 {
     global $wp_query;
     $output = '';
     $languages = mlp_get_available_languages();
     $language_titles = mlp_get_available_languages_titles();
     if (!(0 < count($languages))) {
         return $output;
     }
     // returns NULL if there is no post, get_the_ID() throws a notice,
     // if we don' check this before.
     $default_post = get_post();
     if ($default_post) {
         $current_element_id = get_the_ID();
     } elseif (!empty($wp_query->queried_object) && !empty($wp_query->queried_object->ID)) {
         $current_element_id = $wp_query->queried_object->ID;
     } else {
         $current_element_id = 0;
     }
     $linked_elements = array();
     // double check to avoid issues with a static front page.
     if (!is_front_page() && !is_home() && is_singular()) {
         $linked_elements = mlp_get_linked_elements($current_element_id);
     }
     $defaults = array('link_text' => 'text', 'echo' => TRUE, 'sort' => 'blogid', 'show_current_blog' => FALSE);
     $params = wp_parse_args($args, $defaults);
     if ('blogid' == $params['sort']) {
         ksort($languages);
     } else {
         asort($languages);
     }
     $output .= '<div class="mlp_language_box"><ul>';
     foreach ($languages as $language_blog => $language_string) {
         $current_language = mlp_get_current_blog_language(2);
         if ($current_language == $language_string && $params['show_current_blog'] == FALSE) {
             continue;
         }
         // Get params
         $flag = mlp_get_language_flag($language_blog);
         $title = mlp_get_available_languages_titles(TRUE);
         // Display type
         if ('flag' == $params['link_text'] && '' != $flag) {
             $display = '<img src="' . $flag . '" alt="' . $languages[$language_blog] . '" title="' . $title[$language_blog] . '" />';
         } else {
             if ('text' == $params['link_text'] && !empty($language_titles[$language_blog])) {
                 $display = $language_titles[$language_blog];
             } else {
                 if ('text_flag' == $params['link_text']) {
                     $display = '<img src="' . $flag . '" alt="' . $languages[$language_blog] . '" title="' . $title[$language_blog] . '" />';
                     if (!empty($language_titles[$language_blog])) {
                         $display .= ' ' . $language_titles[$language_blog];
                     }
                 } else {
                     $display = $languages[$language_blog];
                 }
             }
         }
         $class = get_current_blog_id() == $language_blog ? 'id="mlp_current_locale"' : '';
         // set element to 0 to avoid empty element
         if (!isset($linked_elements[$language_blog])) {
             $linked_elements[$language_blog] = 0;
         }
         // Check post status
         $post = $linked_elements[$language_blog] > 0 ? get_blog_post($language_blog, $linked_elements[$language_blog]) : '';
         do_action('mlp_before_link');
         $link = (is_single() || is_page() || is_home()) && isset($post->post_status) && ('publish' === $post->post_status || 'private' === $post->post_status && is_super_admin()) ? get_blog_permalink($language_blog, $linked_elements[$language_blog]) : get_site_url($language_blog);
         // apply filter to help others to change the link
         $link = apply_filters('mlp_linked_element_link', $link, $language_blog, $linked_elements[$language_blog]);
         do_action('mlp_after_link');
         // Output link elements
         $output .= '<li ' . ($current_language == $language_string ? 'class="current"' : '') . '><a rel="alternate" hreflang="' . self::get_blog_language($language_blog) . '" ' . $class . ' href="' . $link . '">' . $display . '</a></li>';
     }
     $output .= '</ul></div>';
     return $output;
 }
 /**
  * Load the Content for the Metabox
  *
  * @access  public
  * @since   0.1
  * @uses	switch_to_blog, get_post, get_the_title, get_post_status, admin_url, apply_filters, restore_current_blog
  * @return  void
  */
 public function ajax_get_metabox_content()
 {
     $has_linked = FALSE;
     // Get elements linked to this item
     $linked = mlp_get_linked_elements(esc_attr($_POST['post']));
     // No elements available? Au revoir.
     if (!$linked) {
         die(__('No post available', 'multilingualpress'));
     }
     // Walk through elements
     foreach ($linked as $linked_blog => $linked_post) {
         // Not concerning this blog?
         if (intval($_POST['blogid']) !== $linked_blog) {
             continue;
         }
         // Switch to appropriate blog to
         // have the helper functions at hand
         switch_to_blog($linked_blog);
         // Get post
         $remote_post = get_post($linked_post);
         // Create output
         if (NULL != $remote_post) {
             $has_linked = TRUE;
             echo '<p>' . __('Status:', 'multilingualpress') . '&nbsp;<b>' . ucfirst(get_post_status($linked_post)) . '</b>&nbsp;|&nbsp;' . __('Published on:', 'multilingualpress') . '<b>&nbsp;' . get_post_time(get_option('date_format'), FALSE, $linked_post) . '</b></p>';
             echo '<h2 id="headline">' . get_the_title($linked_post) . '</h2>';
             echo '<textarea id="content" class="large-text cols="80" rows="10" readonly="readonly">' . apply_filters('the_content', $remote_post->post_content) . '</textarea>';
             echo '<p><a href="' . admin_url('post.php?post=' . $linked_post . '&action=edit') . '">' . __('Edit', 'multilingualpress') . '</a></p>';
         }
         restore_current_blog();
     }
     // No posts available?
     if (FALSE === $has_linked) {
         die('<p>' . __('No post available', 'multilingualpress') . '</p>');
     }
     die;
 }