public function save($post_id, WP_Post $post)
{
$post_type = $this->get_real_post_type($post);
if (!in_array($post_type, $this->allowed_post_types, true)) {
return;
}
if (empty($this->post_request_data['mlp_to_translate'])) {
return;
}
$to_translate = $this->post_request_data['mlp_to_translate'];
$post_id = $this->get_real_post_id($post_id);
$this->save_context = ['source_blog' => get_current_blog_id(), 'source_post' => $post, 'real_post_type' => $post_type, 'real_post_id' => $post_id];
// Get the post
$post_data = get_post($post_id, ARRAY_A);
$post_meta = $this->get_post_meta_to_transfer();
/** This filter is documented in inc/advanced-translator/Mlp_Advanced_Translator_Data.php */
$post_data = apply_filters('mlp_pre_save_post', $post_data, $this->save_context);
if (!$post_data || !is_array($post_data)) {
return;
}
$file = $path = '';
$fileinfo = [];
// Check for thumbnail
if (current_theme_supports('post-thumbnails')) {
$thumb_id = get_post_thumbnail_id($post_id);
if (0 < $thumb_id) {
$path = wp_upload_dir();
$file = get_post_meta($thumb_id, '_wp_attached_file', true);
$fileinfo = pathinfo($file);
include_once ABSPATH . 'wp-admin/includes/image.php';
//including the attachment function
}
}
// Create the post array
$new_post = ['post_title' => $post_data['post_title'], 'post_content' => $post_data['post_content'], 'post_status' => 'draft', 'post_author' => $post_data['post_author'], 'post_excerpt' => $post_data['post_excerpt'], 'post_date' => $post_data['post_date'], 'post_type' => $post_data['post_type']];
$this->find_post_parents($post_data['post_type'], $post->post_parent);
/** This action is documented in inc/advanced-translator/Mlp_Advanced_Translator_Data.php */
do_action('mlp_before_post_synchronization', $this->save_context);
// Create a copy of the item for every related blog
foreach ($to_translate as $blog_id) {
if ($blog_id == get_current_blog_id() or !\Inpsyde\MultilingualPress\site_exists($blog_id)) {
continue;
}
$request_validator = Mlp_Save_Post_Request_Validator_Factory::create($this->nonce_factory->create(["save_translation_of_post_{$post_id}_for_site_{$blog_id}"]));
if (!$request_validator->is_valid($post)) {
continue;
}
switch_to_blog($blog_id);
// Set the linked parent post
$new_post['post_parent'] = $this->get_post_parent($blog_id);
$this->save_context['target_blog_id'] = $blog_id;
/** This filter is documented in inc/advanced-translator/Mlp_Advanced_Translator_Data.php */
$new_post = apply_filters('mlp_pre_insert_post', $new_post, $this->save_context);
// Insert remote blog post
$remote_post_id = wp_insert_post($new_post);
if (!empty($post_meta)) {
$this->update_remote_post_meta($remote_post_id, $post_meta);
}
if ('' != $file) {
// thumbfile exists
if (0 < count($fileinfo)) {
$filedir = wp_upload_dir();
$filename = wp_unique_filename($filedir['path'], $fileinfo['basename']);
$copy = copy($path['basedir'] . '/' . $file, $filedir['path'] . '/' . $filename);
if ($copy) {
$wp_filetype = wp_check_filetype($filedir['url'] . '/' . $filename);
//get the file type
$attachment = ['post_mime_type' => $wp_filetype['type'], 'guid' => $filedir['url'] . '/' . $filename, 'post_parent' => $remote_post_id, 'post_title' => '', 'post_excerpt' => '', 'post_author' => $post_data['post_author'], 'post_content' => ''];
//insert the image
$attach_id = wp_insert_attachment($attachment, $filedir['path'] . '/' . $filename);
if (!is_wp_error($attach_id)) {
wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $filedir['path'] . '/' . $filename));
// update the image data
set_post_thumbnail($remote_post_id, $attach_id);
}
}
}
}
$this->set_linked_element($post_id, $blog_id, $remote_post_id);
restore_current_blog();
}
/** This action is documented in inc/advanced-translator/Mlp_Advanced_Translator_Data.php */
do_action('mlp_after_post_synchronization', $this->save_context);
}