Viewing 15 replies - 1 through 15 (of 24 total)
  • I would like this also.

    Thread Starter Maxaud

    (@maxaud)

    I was able to achieve this by hooking into the new blog function and using a modified version of Threewp Broadcasts save_post method.
    I defined a master site and on new blog creation it broadcasts all the master site’s posts to the new blog.

    Thanks @maxaud

    Howsomever, I am a very old geezer who hasn’t a clue about how to do what you did.

    Sigh.

    Maxaud,

    Are you able to post your modifications?

    Thread Starter Maxaud

    (@maxaud)

    This is a version of the function I’m using to broadcast the post

    I have defined in the head of a different file the PHP constant MASTERBLOGID used to pull posts from.

    I then have this hooked into new blog creation

    switch_to_blog( MASTERBLOGID );
    
    					$sql = '
    						SELECT ID
    						FROM ' . $wpdb->base_prefix . MASTERBLOGID . '_posts
    						WHERE post_type IN ("post","page")
    						';
    
    					$post_ids = $wpdb->get_results( $sql );
    
    					foreach ( $post_ids as $post ) {
    						$broadcast = new ThreeWP_Broadcast;
    						$error = $broadcast->CMHL_save_post($post->ID,$new_blog_id);
    					}
    					restore_current_blog();
    public function CMHL_save_post( $post_id, $newblog_id ) {
    
    		$allowed_post_status = array( 'publish', 'pending' );
    		$allowed_post_status[] = 'draft';
    		$allowed_post_status[] = 'future';
    
    		$post = get_post( $post_id, 'ARRAY_A' );
    
    		$post_type = $post->post_type;
    		$post_type_object = get_post_type_object( $post_type);
    		$post_type_supports_thumbnails = post_type_supports( $post_type, 'thumbnail' );
    		$post_type_supports_custom_fields = post_type_supports( $post_type, 'custom-fields' );
    		$post_type_is_hierarchical = $post_type_object->hierarchical;
    
    		// $post_type_supports_categories = $post_type_object->capability_type == 'post';
    		// $post_type_supports_tags  = $post_type_object->capability_type == 'post';
    
    		// Create new post data from the original stuff.
    		$new_post = $post;
    		foreach(array( 'comment_count', 'guid', 'ID', 'menu_order', 'post_parent' ) as $key) {
    			unset( $new_post[$key] );
    		}
    
    		$user_id = $this->user_id();		// Convenience.
    
    		$blogs = array();
    		$blogs[] = $newblog_id;
    
    		$blog_id = MASTERBLOGID;
    
    		// Do we actually need to to anything?
    		if (count( $blogs ) < 1) {
    			return;
    		}
    
    		$link = true;
    
    		if ( $link) {
    
    			// Prepare the broadcast data for linked children.
    			$broadcast_data = $this->get_post_broadcast_data( $blog_id, $post_id );
    
    			// Does this post type have parent support, so that we can link to a parent?
    			if ( $post_type_is_hierarchical && $post->post_parent > 0)
    			{
    				$post_id_parent = $post->post_parent;
    				$parent_broadcast_data = $this->get_post_broadcast_data( $blog_id, $post_id_parent );
    			}
    		}
    
    		$taxonomies_create = true;
    		$taxonomies = true;
    
    		if ( $taxonomies) {
    			$source_blog_taxonomies = get_object_taxonomies( array(
    				'object_type' => $post_type,
    			), 'array' );
    			$source_post_taxonomies = array();
    			foreach( $source_blog_taxonomies as $source_blog_taxonomy => $ignore )
    			{
    				$source_post_taxonomies[ $source_blog_taxonomy ] = get_the_terms( $post_id, $source_blog_taxonomy );
    				if ( $source_post_taxonomies[ $source_blog_taxonomy ] === false )
    					unset( $source_post_taxonomies[ $source_blog_taxonomy ] );
    			}
    		}
    
    		require_once( 'AttachmentData.php' );
    
    		$upload_dir = wp_upload_dir();	// We need to find out where the files are on disk for this blog.
    		$attachment_data = array();
    		$attached_files =& get_children( 'post_parent='.$post_id.'&post_type=attachment' );
    		$has_attached_files = count( $attached_files) > 0;
    		if ( $has_attached_files) {
    			if ( is_array( $attached_files ) ) {
    				foreach( $attached_files as $attached_file) {
    					$attachment_data[ $attached_file->ID ] = AttachmentData::from_attachment_id( $attached_file->ID, $upload_dir );
    				}
    			}
    		}
    
    		$custom_fields = true;
    
    		if ( $custom_fields) {
    
    			$post_custom_fields = get_post_custom( $post_id );
    
    			$has_thumbnail = isset( $post_custom_fields['_thumbnail_id'] );
    
    			if ( $has_thumbnail) {
    				$thumbnail_id = $post_custom_fields['_thumbnail_id'][0];
    				unset( $post_custom_fields['_thumbnail_id'] ); // There is a new thumbnail id for each blog.
    				$attachment_data['thumbnail'] = AttachmentData::from_attachment_id( $thumbnail_id, $upload_dir);
    				// Now that we know what the attachment id the thumbnail has, we must remove it from the attached files to avoid duplicates.
    				unset( $attachment_data[$thumbnail_id] );
    			}
    
    			// Remove all the _internal custom fields.
    			$post_custom_fields = $this->keep_valid_custom_fields( $post_custom_fields);
    		}
    
    		// Sticky isn't a tag, taxonomy or custom_field.
    		$post_is_sticky = false;
    
    		$to_broadcasted_blogs = array();				// Array of blog names that we're broadcasting to. To be used for the activity monitor action.
    		$to_broadcasted_blog_details = array(); 		// Array of blog and post IDs that we're broadcasting to. To be used for the activity monitor action.
    
    		$original_blog = $blog_id;
    
    		switch_to_blog( $newblog_id );
    
    		// Post parent
    		if ( $link && isset( $parent_broadcast_data) ) {
    			if ( $parent_broadcast_data->has_linked_child_on_this_blog() ){
    				$linked_parent = $parent_broadcast_data->get_linked_child_on_this_blog();
    				$new_post['post_parent'] = $linked_parent;
    			}
    		}
    
    		// Insert new? Or update? Depends on whether the parent post was linked before or is newly linked?
    		$need_to_insert_post = true;
    		if ( $link ) {
    
    			if ( $broadcast_data->has_linked_child_on_this_blog() ){
    
    				$child_post_id = $broadcast_data->get_linked_child_on_this_blog();
    
    				// Does this child post still exist?
    				$child_post = get_post( $child_post_id );
    
    				if ( $child_post !== null ) {
    
    					$temp_post_data = $new_post;
    					$temp_post_data['ID'] = $child_post_id;
    					$new_post_id = wp_update_post( $temp_post_data );
    					$need_to_insert_post = false;
    				}
    			}
    		}
    
    		if ( $need_to_insert_post ) {
    
    			$new_post_id = wp_insert_post( $new_post );
    
    			if ( $link ) {
    				$broadcast_data->add_linked_child( $newblog_id, $new_post_id );
    			}
    		}
    
    		if ( $taxonomies ) {
    
    			if ( is_array( $source_post_taxonomies ) ) {
    
    				foreach ( $source_post_taxonomies as $source_post_taxonomy => $source_post_terms ) {
    
    					// If we're updating a linked post, remove all the taxonomies and start from the top.
    					if ( $link ) {
    						if ( $broadcast_data->has_linked_child_on_this_blog() ) {
    							wp_set_object_terms( $new_post_id, array(), $source_post_taxonomy );
    						}
    					}
    
    					// Get a list of cats that the target blog has.
    					$target_blog_terms = $this->get_current_blog_taxonomy_terms( $source_post_taxonomy );
    
    					// Go through the original post's terms and compare each slug with the slug of the target terms.
    					$taxonomies_to_add_to = array();
    					$have_created_taxonomies = false;
    
    					foreach( $source_post_terms as $source_post_term ) {
    
    						$found = false;
    						$source_slug = $source_post_term->slug;
    
    						foreach( $target_blog_terms as $target_blog_term ) {
    
    							if ( $target_blog_term['slug'] == $source_slug) {
    								$found = true;
    								$taxonomies_to_add_to[ $target_blog_term['term_id'] ] = intval( $target_blog_term['term_id'] );
    								break;
    							}
    						}
    
    						// Should we create the taxonomy if it doesn't exist?
    						if ( !$found && $taxonomies_create ) {
    
    							$new_taxonomy = wp_insert_term(
    								$source_post_term->name,
    								$source_post_taxonomy,
    								array(
    									'slug' => $source_post_term->slug,
    									'description' => $source_post_term->description,
    								)
    							);
    
    							$taxonomies_to_add_to[] = $source_post_term->slug;
    							$have_created_taxonomies = true;
    						}
    					}
    
    					if ( $taxonomies_create ) {
    						$this->sync_terms( $source_post_taxonomy, $original_blog, $newblog_id );
    					}
    
    					if ( count( $taxonomies_to_add_to) > 0 ) {
    						wp_set_object_terms( $new_post_id, $taxonomies_to_add_to, $source_post_taxonomy );
    					}
    				}
    			}
    		}
    
    		/**
    			Remove the current attachments.
    		*/
    		$attachments_to_remove =& get_children( 'post_parent='.$new_post_id.'&post_type=attachment' );
    
    		if ( is_array( $attachments_to_remove ) ) {
    			foreach ( $attachments_to_remove as $attachment_to_remove) {
    				wp_delete_attachment( $attachment_to_remove->ID );
    			}
    		}
    
    		if ( is_array( $attachment_data ) ) {
    			foreach ( $attachment_data as $key=>$attached_file) {
    				if ( $key != 'thumbnail' ) {
    					$this->copy_attachment( $attached_file, $new_post_id );
    				}
    			}
    		}
    
    		if ( $custom_fields ) {
    
    			// Remove all old custom fields.
    			$old_custom_fields = get_post_custom( $new_post_id );
    
    			if ( is_array( $old_custom_fields ) ) {
    				foreach ( $old_custom_fields as $key => $value) {
    
    					// This post has a featured image! Remove it from disk!
    					if ( $key == '_thumbnail_id' ) {
    						$thumbnail_post = $value[0];
    						wp_delete_post( $thumbnail_post);
    					}
    
    					delete_post_meta( $new_post_id, $key);
    				}
    			}
    
    			if ( is_array( $post_custom_fields ) ) {
    				foreach ( $post_custom_fields as $meta_key => $meta_value ) {
    					if ( is_array( $meta_value ) ) {
    						foreach ( $meta_value as $single_meta_value ) {
    							$single_meta_value = maybe_unserialize( $single_meta_value );
    							add_post_meta( $new_post_id, $meta_key, $single_meta_value );
    						}
    					} else {
    						$meta_value = maybe_unserialize( $meta_value );
    						add_post_meta( $new_post_id, $meta_key, $meta_value );
    					}
    				}
    			}
    
    			// Attached files are custom fields... but special custom fields. Therefore they need special treatment. Like retards. Retarded files.
    			if ( $has_thumbnail ) {
    				$new_attachment_id = $this->copy_attachment( $attachment_data['thumbnail'], $new_post_id );
    				if ( $new_attachment_id !== false ) {
    					update_post_meta( $new_post_id, '_thumbnail_id', $new_attachment_id );
    				}
    			}
    		}
    
    		if ( $link) {
    
    				$new_post_broadcast_data = $this->get_post_broadcast_data($blog_id, $new_post_id);
    				$new_post_broadcast_data->set_linked_parent( $original_blog, $post_id );
    				$this->set_post_broadcast_data($newblog_id, $new_post_id, $new_post_broadcast_data);
    			}
    
    			restore_current_blog();
    
    		// Finished broadcasting.
    		// Save the post broadcast data.
    		if ($link)
    			$this->set_post_broadcast_data($blog_id, $post_id, $broadcast_data);
    
    		global $wp_rewrite;
    		$wp_rewrite->init(); $wp_rewrite->flush_rules();
    	}

    majority of the settings checks were taken out and some other items as I will always want to broadcast all posts/pages, taxonomies, and custom fields.

    That mod doesn’t happen to backtrack so it populates the new blog with old posts does it?

    Thread Starter Maxaud

    (@maxaud)

    This basically takes all the posts/pages from a “master” blog and broadcasts them to the new blog.

    May I ask where I put in those 2 snippets of code? I thought I could look for it but it’s not in the threewp plugin files.

    The second function looks like it goes into the ThreeWP_Broadcast.php file as part of the class right?

    This basically takes all the posts/pages from a “master” blog and broadcasts them to the new blog.

    Is it only for future posts or all current posts on the master blog?

    Thread Starter Maxaud

    (@maxaud)

    every post that is on the master blog (including past posts).
    Future posts will need to be broadcasted as they normally would.

    The second snippet of code goes at the end of the threewp broadcast class.
    You can add it right below the save_post method in the same file to keep them next to eachother.

    The first snippet needs to execute upon new site creation.
    I have a custom admin panel that I use for creating sites outside of the traditional form that is used by WordPress.

    There is an action/filter that is triggered upon site creation, you’ll want to hook into that to get the site ID that was created so you can plugin that ID into the 1st snippet of code.

    Weird, i’ve put this code in my functions.php in my theme folder

    function pull_content_from_master_blog($blog_id, $user_id, $domain, $path, $site_id, $meta) {
    	switch_to_blog( 1 );
    
    	$sql = '
    		SELECT ID
    		FROM ' . $wpdb->base_prefix . '_posts
    		WHERE post_type IN ("post","page","articles","videos")
    		';
    	$post_ids = $wpdb->get_results( $sql );
    	foreach ( $post_ids as $post ) {
    		$broadcast = new ThreeWP_Broadcast;
    		$error = $broadcast->CMHL_save_post($post->ID,$blog_id);
    	}
    	restore_current_blog();
    }
    add_action( 'wpmu_new_blog', 'pull_content_from_master_blog' );

    adding the other post types and altering the sql query (everything is pulled from the first site which i noticed isn’t formatted like wp1_posts but I get the following error when I try to make a new site

    PHP Fatal error: Call to a member function get_results() on a non-object in /usr/www/virtual/username/www.domain.com/wp-content/themes/v2/functions.php on line 377, referer: http://www.domain.com/wp-admin/network/site-new.php

    Any thoughts? Sorry, I’m a bit new with WP hooks

    Thread Starter Maxaud

    (@maxaud)

    You’re using the WordPress database class in the query so you need to grab it to use in the query before hand.

    add this to the beginning of the function:
    global $wpdb;

    Awesome. No more errors but the posts don’t seem to be broadcasted when I make a new site. Any ideas? The only thing I modified was adding your function next to the save_post() function and adding the edited function above that I pasted into my theme functions.php file.

    I also echoed $blog_id and it outputted the newest blog_id (I was worried I set it up wrong). Did I hook it into the right function?

    Thread Starter Maxaud

    (@maxaud)

    Ok, couple things to check.

    Is $post_ids returning anything?

    Have you defined or replaced all occurrences of MASTERBLOGID in the function?

    $posts_ids is returning an array of post ids. I found an error with the sql statement which I’ve fixed and now it seems to take a bit longer to add a site but it does not seem to add any posts.

    I’ve replaced all occurences of MASTERBLOGID in both functions.

    Thread Starter Maxaud

    (@maxaud)

    It’s hard for me to diagnose what the issue could be.

    What happens if you var_dump() the variables in the function to make sure you have everything you need to broadcast then add a die() after it so you can see what’s being output.

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘[Plugin: ThreeWP Broadcast] Broadcast to newly created blogs?’ is closed to new replies.