Developer: API

This is taken from the src/api.php file. Use doxygen to create the apidoc directory.

Using the API

Retrieve the API object by asking Broadcast for it.

$api = ThreeWP_Broadcast()->api();

Or call an API method directly.

ThreeWP_Broadcast()->api()->broadcast_children( 123, [ 10, 11, 12 ] );

Broadcasting

The methods below refer to the post ID on the current blog.

To broadcast a post to one or more blogs, see broadcast_children(). The posts will be linked as children.

To rebroadcast (update) the post, see update_children(). You can also, optionally, specified an array of new blog IDs which to add the post. Linking is parent <-> child.

Broadcasting is done like so:

  • A broadcasting_data (bcd) object is created with the specified post_id as the source.
  • The bcd will examine the post and prepare itself, setting its internal properties.
  • The bcd constructor will create and prepare the meta_box_data object, which allows plugins to do things like force broadcast to blogs or similar.
  • Optionally, the prepare_broadcasting_data action is executed in order to parse the meta box, to see which options and blogs are selected. This is only done when input from the user needs to be parsed. This is not run when using the API.
  • The threewp_broadcast_broadcast_post action is executed, which will broadcast the post. The queue plugin overrides this hook and stores the bcd for later.
  • The bcd object is returned.

If you wish to not use the default broadcasting_data values (perhaps you don’t want taxonomies broadcasted), then you should create the bcd object yourself, modify it, and then run action.

Linking

2015-oct-12 The current linking scheme, broadcast_data, should be replaced by a Link class driven from the API. It will first use the current broadcast_data to store the linking, switching to a new database scheme later on after developers have been given the chance to upgrade to Link. There is no timetable for this, but for the sake of data integrity and ease of use, it should be done sometime.

Useful functions

The below isn’t really part of the API, but could be useful when coding together custom solutions.

The action each_linked_post executes a callback on each linked post.

The function ThreeWP_Broadcast()->switch_broadcast_data( $blog_or_post_id, $post_id = null ) can switch the broadcast data of two posts, making the parent the child and vice versa.

Comments

  1. I’m trying to broadcast a post created with wp_insert_post to another two blogs by using the following code.

    ThreeWP_Broadcast()->api()->broadcast_children($post_id, [2,3]);

    Child blogs ids, post_id are correct. But the post doesn’t appearing in blog 2 and 3.
    Please kindly suggest. I’m a paid subscriber.

    1. The best way to solve this would be to enable Broadcast debug to browser (or file) mode and then run the above command. It should tell you what broadcast is or isn’t doing. If you have trouble deciphering the debug dump, just e-mail me it and I’ll help you take a look.

  2. Im trying to asign a category to each post depending where was broadcasted.
    Is there a function that returns the id of the post where was broadcasted?

    1. Sure is. During broadcasting, I suppose you’re hooking into before_restore_current_blog, then use $action->new_post( ‘ID’ );

Leave a Reply

Your email address will not be published. Required fields are marked *