/** * Register one box for each connected site. * * @param int $blog_id * @param WP_Post $post * * @return void */ private function register_metabox_per_language($blog_id, WP_Post $post) { $remote_post = $this->data->get_remote_post($post, $blog_id); $lang = $this->data->get_remote_language($blog_id); $title = $this->get_metabox_title($blog_id, $remote_post, $lang); $metabox_data = ['remote_blog_id' => $blog_id, 'remote_post' => $remote_post, 'language' => $lang]; $view = new Mlp_Translation_Metabox_View($this->nonce_factory->create(["save_translation_of_post_{$post->ID}_for_site_{$blog_id}"])); add_meta_box("inpsyde_multilingual_{$blog_id}", $title, [$view, 'render'], null, 'advanced', 'default', $metabox_data); if (empty($remote_post->dummy)) { $this->register_metabox_view_details($view, $post, $blog_id); } else { $callback = [$view, 'show_translation_checkbox']; /** * Filter the post translator activation checkbox callback. * * @param array|string $callback Callback name or class-method array. */ $checkbox_callback = apply_filters('mlp_post_translator_activation_checkbox', $callback); if ($checkbox_callback) { add_action("mlp_translation_meta_box_top_{$blog_id}", $checkbox_callback, 10, 3); } } /** * Runs after registration of the meta box for the given blog's language. * * @param WP_Post $post Post object. * @param int $blog_id Blog ID. */ do_action('mlp_translation_meta_box_registered', $post, $blog_id); }
/** * 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(); $featured_image_path = $this->get_featured_image_path($post_id); $related_blogs = $this->relations->get_related_site_ids($source_blog_id); if (empty($related_blogs)) { return; } // Check Post Type if (!in_array($post_type, $this->allowed_post_types, true)) { return; } $this->save_context = ['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 (!in_array($remote_blog_id, $related_blogs) || !\Inpsyde\MultilingualPress\site_exists($remote_blog_id)) { continue; } $nonce = $this->nonce_factory->create(["save_translation_of_post_{$post_id}_for_site_{$remote_blog_id}"]); $request_validator = Mlp_Save_Post_Request_Validator_Factory::create($nonce); 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 ([] !== $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 && $featured_image_path) { $this->copy_thumb($new_id, $featured_image_path); } $tax_data = empty($post_data['tax']) ? [] : (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); }