/** * Check if the current request should be processed by save(). * * @param WP_Post $post * * @return bool */ private function is_valid_save_request(WP_Post $post) { static $called = 0; // for auto-drafts, 'save_post' is called twice, resulting in doubled // drafts for translations. $called += 1; if (!empty($this->post_request_data['original_post_status']) && 'auto-draft' === $this->post_request_data['original_post_status'] && 1 < $called) { return FALSE; } if (!$this->request_validator->is_valid($post)) { return FALSE; } if (empty($this->post_request_data)) { return FALSE; } if (empty($this->post_request_data[$this->name_base])) { return FALSE; } if (!is_array($this->post_request_data[$this->name_base])) { return FALSE; } // We only need this when the post is published oder drafted if (!$this->is_connectable_status($post)) { return FALSE; } return TRUE; }
/** * @param int $post_id * @param WP_Post $post * @return void */ public function save($post_id, WP_Post $post) { if (!$this->request_validator->is_valid($post)) { return; } $post_type = $this->get_real_post_type($post); $post_id = $this->get_real_post_id($post_id); if (!in_array($post_type, $this->allowed_post_types)) { return; } if (empty($this->post_request_data['mlp_to_translate'])) { return; } $to_translate = $this->post_request_data['mlp_to_translate']; $this->save_context = array('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($post_id); /** * Pre-Filter before Saving the Post * @param Array $post_data * @param Array $save_context */ $post_data = apply_filters('mlp_pre_save_post', $post_data, $this->save_context); // When the filter returns FALSE, we'll stop here if (FALSE == $post_data || !is_array($post_data)) { return; } $file = $path = ''; $fileinfo = array(); // 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 = array('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); /** * Run before the first save_post action is called in other blogs. * * @param Array $save_context */ 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 !blog_exists($blog_id)) { 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; /** * Filter post data before it is saved to the database. * * @param array $new_post * @param array $context */ $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); //echo $remote_post_id . '<br>'; 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 = array('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)); set_post_thumbnail($remote_post_id, $attach_id); } // update the image data } } } $this->set_linked_element($post_id, $blog_id, $remote_post_id); restore_current_blog(); } /** * Run after all save_post actions are called in other blogs. * * @param Array $save_context */ do_action('mlp_after_post_synchronization', $this->save_context); }