/**
  * Save the post to the blogs
  *
  * @param int     $post_id
  * @param WP_Post $post
  *
  * @return void
  */
 public function save($post_id, WP_Post $post)
 {
     if (!$this->is_valid_save_request($post)) {
         return;
     }
     $available_blogs = get_site_option('inpsyde_multilingual');
     if (empty($available_blogs)) {
         return;
     }
     // auto-drafts
     $post_id = $this->basic_data->get_real_post_id($post_id);
     $post_type = $this->basic_data->get_real_post_type($post);
     $source_blog_id = get_current_blog_id();
     $thumb_data = $this->get_source_thumb_data($post_id);
     $related_blogs = $this->relations->get_related_sites($source_blog_id);
     if (empty($related_blogs)) {
         return;
     }
     // Check Post Type
     if (!in_array($post_type, $this->allowed_post_types)) {
         return;
     }
     $this->save_context = array('source_blog' => get_current_blog_id(), 'source_post' => $post, 'real_post_type' => $post_type, 'real_post_id' => $post_id);
     $this->basic_data->set_save_context($this->save_context);
     $post_meta = $this->basic_data->get_post_meta_to_transfer();
     $this->basic_data->find_post_parents($post_type, $post->post_parent);
     /**
      * Runs before the first save_post action is called for the remote blogs.
      *
      * @param array $save_context Context of the to-be-saved post.
      */
     do_action('mlp_before_post_synchronization', $this->save_context);
     foreach ($this->post_request_data[$this->name_base] as $remote_blog_id => $post_data) {
         if (!blog_exists($remote_blog_id) || !in_array($remote_blog_id, $related_blogs)) {
             continue;
         }
         switch_to_blog($remote_blog_id);
         $this->save_context['target_blog_id'] = $remote_blog_id;
         $new_post = $this->create_post_to_send($post_data, $post_type, $remote_blog_id);
         if (array() !== $new_post) {
             $sync_thumb = !empty($post_data['thumbnail']);
             $update = !empty($post_data['remote_post_id']) && 0 < $post_data['remote_post_id'];
             $new_id = $this->sync_post($new_post, $post_id, $remote_blog_id, $update);
             $this->basic_data->set_save_context($this->save_context);
             $this->basic_data->update_remote_post_meta($new_id, $post_meta);
             if ($sync_thumb && $thumb_data->has_thumb) {
                 $this->copy_thumb($new_id, $thumb_data);
             }
             $tax_data = empty($post_data['tax']) ? array() : (array) $post_data['tax'];
             $this->set_remote_tax_terms($new_id, $tax_data);
         }
         restore_current_blog();
     }
     /**
      * Runs after all save_post actions have been called for the remote blogs.
      *
      * @param array $save_context
      */
     do_action('mlp_after_post_synchronization', $this->save_context);
 }
 /**
  * @param $site_id
  * @return array
  */
 private function get_related_sites($site_id)
 {
     if (empty($site_id)) {
         $site_id = get_current_blog_id();
     }
     return $this->site_relations->get_related_sites($site_id, !is_user_logged_in());
 }
Esempio n. 3
0
 /**
  * @param      $site_id
  * @param bool $include_base
  * @return array
  */
 private function get_related_sites($site_id, $include_base)
 {
     if (empty($site_id)) {
         $site_id = get_current_blog_id();
     }
     $sites = $this->site_relations->get_related_sites($site_id);
     if (empty($sites)) {
         return array();
     }
     if ($include_base) {
         $sites[] = $site_id;
     }
     return $sites;
 }
    /**
     * @param array $site_option
     * @return void
     */
    private function show_blog_relationships($site_option)
    {
        if (!is_array($site_option)) {
            return;
        }
        unset($site_option[$this->blog_id]);
        if (empty($site_option)) {
            return;
        }
        ?>
		<tr>
			<td><?php 
        esc_html_e('Relationships', 'multilingualpress');
        ?>
</td>
			<td>
		<?php 
        foreach ($site_option as $blog_id => $meta) {
            $blog_id = (int) $blog_id;
            // Get blog display name
            switch_to_blog($blog_id);
            $blog_name = esc_html(get_bloginfo('Name'));
            restore_current_blog();
            // Get current settings
            $related_blogs = $this->relations->get_related_sites($this->blog_id, FALSE);
            $checked = checked(TRUE, in_array($blog_id, $related_blogs), FALSE);
            $id = "related_blog_{$blog_id}";
            ?>
			<p>
				<label for="<?php 
            echo $id;
            ?>
">
					<input id="<?php 
            echo $id;
            ?>
" <?php 
            echo $checked;
            ?>
 type="checkbox" name="related_blogs[]" value="<?php 
            echo $blog_id;
            ?>
" />
					<?php 
            echo $blog_name;
            ?>
 - <?php 
            echo Mlp_Helpers::get_blog_language($blog_id);
            ?>
				</label>
			</p>
			<?php 
        }
        ?>
		<p class="description">
		<?php 
        esc_html_e('You can connect this site only to sites with an assigned language. Other sites will not show up here.', 'multilingualpress');
        ?>
		</p>
			</td>
		</tr>
		<?php 
    }
 /**
  * @param $blog_id
  * @param $old_related
  * @param $new_related
  * @param $relations
  * @param $changed
  * @return int
  */
 private function delete_unset_relations($blog_id, $old_related, $new_related, Mlp_Site_Relations_Interface $relations, $changed)
 {
     // Delete removed relations.
     foreach ($old_related as $old_blog_id) {
         if (!in_array($old_blog_id, $new_related)) {
             $relations->delete_relation($blog_id, $old_blog_id);
             $changed += 1;
         }
     }
     return $changed;
 }