/** * @param WP_Post $source_post * @param int $blog_id * @return WP_Post */ public function get_remote_post(WP_Post $source_post, $blog_id) { $post = NULL; $linked = Mlp_Helpers::load_linked_elements($source_post->ID, '', get_current_blog_id()); if (!empty($linked[$blog_id]) && blog_exists($blog_id)) { $post = get_blog_post($blog_id, $linked[$blog_id]); } if ($post) { return $post; } return $this->get_dummy_post($source_post->post_type); }
/** * wrapper of Mlp_Helpers function to get the element ID in other blogs for the selected element * * @since 0.1 * @param int $element_id ID of the selected element * @param string $type type of the selected element * @param int $blog_id ID of the selected blog * @return array linked elements */ function mlp_get_linked_elements($element_id = FALSE, $type = '', $blog_id = 0) { return Mlp_Helpers::load_linked_elements($element_id, $type, $blog_id); }
/** * Save the post to the blogs * * @access public * @since 0.1 * @uses DOING_AUTOSAVE, get_option, current_user_can, get_post_status, get_site_option, get_current_blog_id, * wp_verify_nonce, plugin_basename, switch_to_blog, restore_current_blog, wp_update_post, wp_insert_post, * has_post_thumbnail, ABSPATH, get_post_thumbnail_id, wp_get_attachment_metadata, wp_upload_dir, * wp_unique_filename, wp_check_filetype, get_current_user_id, wp_insert_attachment, is_wp_error, * wp_update_attachment_metadata, wp_generate_attachment_metadata, update_post_meta * @global $wpdb WordPress Database Wrapper * @return void */ public function save_post($post_id, $post = NULL) { // Preventing Autosave, we don't want that if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } // We don't need to save because there is no Post array if (empty($_POST)) { return; } $related_blogs = get_option('inpsyde_multilingual_blog_relationship'); // We don't need to save because there is no related blog if (empty($related_blogs)) { return; } // We need an array, if it is not, there are no related blogs if (!is_array($related_blogs)) { return; } // Make me unique $related_blogs = array_unique($related_blogs); // Check permissions if ('page' === $post->post_type && !current_user_can('edit_page', $post_id)) { return; } else { if (!current_user_can('edit_post', $post_id)) { return; } } // We only need this, when the post is published oder drafted if (!in_array($post->post_status, array('publish', 'draft', 'private'))) { return; } // Check Post Type if (!in_array($post->post_type, array('post', 'page'))) { return; } global $wpdb; $available_blogs = get_site_option('inpsyde_multilingual'); // 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; } // Setting up relationship data $ml_type = ''; if ('post' !== $post->post_type) { $ml_type = $post->post_type; } $post_meta = $this->get_post_meta_to_transfer($post_id); $post_meta = apply_filters('mlp_pre_save_post_meta', $post_meta); // Set source $this->set_linked_element($_POST['ID'], $_POST['ID'], self::$source_blog, $ml_type, self::$source_blog); foreach ($related_blogs as $blog_to_translate) { if ($blog_to_translate != self::$source_blog) { $lang = $available_blogs[$blog_to_translate]['lang']; // verify this came from the our screen and with proper authorization, // because save_post can be triggered at other times if (isset($_POST['multilingualpress' . '-' . $lang]) && !wp_verify_nonce($_POST['multilingualpress' . '-' . $lang], plugin_basename(__FILE__))) { continue; } // If we don't want to save the post for this language, go to the next one if (!isset($_POST['do_translate_' . $lang])) { continue; } // We need to switch the blog to save the post switch_to_blog($blog_to_translate); // Setup Post Data if ('' != $_POST['post_title_' . $lang]) { $mlp_post_title = $_POST['post_title_' . $lang]; } else { $mlp_post_title = $_POST['post_title']; } if ('' != $_POST['content_' . $lang]) { $mlp_post_content = $_POST['content_' . $lang]; } else { $mlp_post_content = $_POST['content']; } if (!isset($_POST['category_' . $lang])) { $mlp_post_categories = array(); } else { $mlp_post_categories = $_POST['category_' . $lang]; } $mlp_post_data = array('post_author' => get_current_user_id(), 'post_type' => $post->post_type, 'post_format' => isset($_POST['post_format']) ? $_POST['post_format'] : '', 'post_title' => $mlp_post_title, 'post_content' => $mlp_post_content, 'post_category' => $mlp_post_categories, 'tags_input' => isset($_POST['tags_' . $lang]) ? $_POST['tags_' . $lang] : ''); // Check for Update $update_post_id = $_POST['old_post_id_' . $lang]; // Double Check $update_post_id $has_link = Mlp_Helpers::load_linked_elements($post_id, '', self::$source_blog); if (0 < $update_post_id && !$linked) { // Update Post $mlp_post_data['ID'] = $update_post_id; // use filter to change postdata for every blog $mlp_post_data = apply_filters('mlp_pre_update_post', $mlp_post_data); $translated_post_id = wp_update_post($mlp_post_data); } else { $mlp_post_data['post_status'] = 'draft'; // use filter to change postdata for every blog $mlp_post_data = apply_filters('mlp_pre_insert_post', $mlp_post_data); // Insert Post $translated_post_id = wp_insert_post($mlp_post_data); // Insert Relationship $this->set_linked_element($translated_post_id, $post_id, self::$source_blog, $ml_type, $blog_to_translate); // Insert postmeta $this->update_remote_post_meta($translated_post_id, $post_meta); } // We have to get back to the original blog to load the thumbnail restore_current_blog(); // Post Thumbnail if (isset($_POST['copy_thumbnail_' . $lang]) && 'on' == $_POST['copy_thumbnail_' . $lang]) { // Stop if there is no thumbnail if (!has_post_thumbnail($post_id)) { continue; } // We need some image and media functions in here include_once ABSPATH . 'wp-admin/includes/image.php'; include_once ABSPATH . 'wp-includes/media.php'; // Load Original Image $original_attachment_id = get_post_thumbnail_id($post_id); $original_attachment = wp_get_attachment_metadata($original_attachment_id); // Build Path to Originial Image $original_filedir = wp_upload_dir(); $original_file_path = $original_filedir['basedir'] . '/' . $original_attachment['file']; // Now switch to the new blog and post the image switch_to_blog($blog_to_translate); // Prepare and Copy the image $filedir = wp_upload_dir(); if (!is_dir($filedir['path'])) { wp_mkdir_p($filedir['path']); } $filename = wp_unique_filename($filedir['path'], $original_attachment['file']); $copy = copy($original_file_path, $filedir['path'] . '/' . $filename); // Now insert it into the posts if ($copy) { $wp_filetype = wp_check_filetype($filedir['url'] . '/' . $filename); $attachment = array('post_mime_type' => $wp_filetype['type'], 'guid' => $filedir['url'] . '/' . $filename, 'post_parent' => $translated_post_id, 'post_title' => '', 'post_excerpt' => '', 'post_author' => get_current_user_id(), 'post_content' => ''); $attach_id = wp_insert_attachment($attachment, $filedir['path'] . '/' . $filename); // Everything went well? if (!is_wp_error($attach_id)) { wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $filedir['path'] . '/' . $filename)); update_post_meta($translated_post_id, '_thumbnail_id', $attach_id); } } // Aaaaand finish! restore_current_blog(); } } } }