/** * create the element on other blogs and link them * * @access public * @since 0.1 * @uses get_post_status, get_post, get_post_thumbnail_id, wp_upload_dir, get_post_meta, * pathinfo, get_blog_list, get_current_blog_id, switch_to_blog, wp_insert_post, * wp_unique_filename, wp_check_filetype, is_wp_error, wp_update_attachment_metadata, * wp_generate_attachment_metadata, update_post_meta, restore_current_blog * @param $post_id ID of the post * @param $post Post object * @return void */ public function save_post($post_id, $post = NULL) { // We're only interested in published posts at this time if (!in_array($post->post_status, array('publish', 'draft', 'private'))) { return; } // Avoid recursion: // wp_insert_post() invokes the save_post hook, so we have to make sure // the loop below is only entered once per save action. Therefore we save // the source_blog in a static class variable. If it is already set we // know the loop has already been entered and we can exit the save action. if (NULL === self::$source_blog) { self::$source_blog = get_current_blog_id(); } else { return; } // If checkbox is not checked, return if (!isset($_POST['translate_this_post'])) { return; } // Get the post $postdata = get_post($post_id, ARRAY_A); $post_meta = $this->get_post_meta_to_transfer($post_id); // Apply a filter here so modules can play around // with the postdata before it is processed. $postdata = apply_filters('mlp_pre_save_postdata', $postdata); $post_meta = apply_filters('mlp_pre_save_post_meta', $post_meta); // If there is no filter hooked into this saving method, then we // will exclude all post types other that "post" and "page". // @TODO: improve this logic :/ // @TODO: create a whitelist for allowed post types, incl. apply_filters() ? if (!has_filter('mlp_pre_save_postdata')) { if ('post' != $postdata['post_type'] && 'page' != $postdata['post_type']) { return; } } // When the filter returns FALSE, we'll stop here if (FALSE == $postdata || !is_array($postdata)) { return; } $linked = mlp_get_linked_elements($post_id); // We already linked this element? if (0 !== count($linked)) { return; } $this->set_source_id($post_id); $file = ''; // 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); } } // Create the post array $newpost = array('post_title' => $postdata['post_title'], 'post_content' => $postdata['post_content'], 'post_status' => 'draft', 'post_author' => $postdata['post_author'], 'post_excerpt' => $postdata['post_excerpt'], 'post_date' => $postdata['post_date'], 'post_type' => $postdata['post_type']); $blogs = mlp_get_available_languages(); if (empty($blogs)) { return; } // Load Page Parents $parent_elements = array(); if ('page' == $postdata['post_type'] && 0 < $postdata['post_parent']) { $parent_elements = mlp_get_linked_elements($postdata['post_parent']); } // Create a copy of the item for every related blog foreach ($blogs as $blogid => $blogname) { if ($blogid != self::$source_blog) { switch_to_blog($blogid); // Set the linked parent page if (0 < count($parent_elements) && 0 < $parent_elements[$blogid]) { $newpost['post_parent'] = $parent_elements[$blogid]; } // use filter to change postdata for every blog $newpost = apply_filters('mlp_pre_insert_post', $newpost); // Insert remote blog post $remote_post_id = wp_insert_post($newpost); if (!empty($post_meta)) { $this->update_remote_post_meta($remote_post_id, $post_meta); } if ('' != $file) { // thumbfile exists include_once ABSPATH . 'wp-admin/includes/image.php'; //including the attachment function 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) { unset($postdata['ID']); $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' => $postdata['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($remote_post_id); restore_current_blog(); } } }