/**
  * Constructor.
  *
  * @param Inpsyde_Property_List_Interface $plugin_data
  */
 public function __construct(Inpsyde_Property_List_Interface $plugin_data)
 {
     if (!$this->is_post_editor()) {
         return;
     }
     $this->plugin_data = $plugin_data;
     /**
      * Filter the allowed post types.
      *
      * @param string[]                $allowed_post_types Allowed post type names.
      * @param Mlp_Translation_Metabox $meta_box           Translation meta box object.
      *
      * @return string[]
      */
     $this->allowed_post_types = (array) apply_filters('mlp_allowed_post_types', $this->allowed_post_types, $this);
     $this->data = new Mlp_Translatable_Post_Data(null, $this->allowed_post_types, $this->plugin_data->get('link_table'), $this->plugin_data->get('content_relations'));
     add_action('add_meta_boxes', array($this, 'register_meta_boxes'), 10, 2);
     /**
      * Filter whether to use an external save method instead of the built-in method.
      *
      * @param bool $external_save_method Use an external save method?
      *
      * @return bool
      */
     $mlp_external_save_method = (bool) apply_filters('mlp_external_save_method', false);
     if ('POST' === $_SERVER['REQUEST_METHOD'] && !$mlp_external_save_method) {
         add_action('save_post', array($this->data, 'save'), 10, 2);
     }
     // Both the generic nonce and request validator are kept for backwards compatibility reasons only.
     $nonce_validator = Mlp_Nonce_Validator_Factory::create('mlp_post_translator_bc', get_current_blog_id());
     $request_validator = Mlp_Save_Post_Request_Validator_Factory::create($nonce_validator);
     $translator_init_args = array('nonce' => $nonce_validator, 'request_validator' => $request_validator, 'allowed_post_types' => $this->allowed_post_types, 'basic_data' => $this->data, 'instance' => $this);
     /**
      * Runs before internal actions are registered.
      *
      * @param array $translator_init_args Translator arguments {
      *                                    'nonce'              => Inpsyde_Nonce_Validator
      *                                    'request_validator'  => Mlp_Save_Post_Request_Validator
      *                                    'allowed_post_types' => string[]
      *                                    'basic_data'         => Mlp_Translatable_Post_Data
      *                                    'instance'           => Mlp_Translation_Metabox
      *                                    }
      */
     do_action('mlp_post_translator_init', $translator_init_args);
 }
 /**
  * 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;
         }
         $nonce_validator = Mlp_Nonce_Validator_Factory::create("save_translation_of_post_{$post->ID}_for_site_{$remote_blog_id}", $source_blog_id);
         $request_validator = Mlp_Save_Post_Request_Validator_Factory::create($nonce_validator);
         if (!$request_validator->is_valid($post)) {
             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 int     $post_id
  * @param WP_Post $post
  *
  * @return void
  */
 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);
 }