/**
* 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);
}