Developer: Actions

Broadcast offers several actions which can be hooked into. All actions are based off of the Plainview SDK’s WordPress Action class, which means that the parameter is an object that is passed around.

All of the actions can be found in the src/actions directory. The name of the actions are prefixed with threewp_broadcast_. For example, the file src/actions/broadcasting_started.php provides the threewp_broadcast_broadcasting_started action.

The source code of the actions themselves should be documented enough to be self-explanatory.

Finish

The Action class offers two methods that could / should be used to signal to hooks that the action has been fully applied / completed / finished.

$action->finish();

and

$action->is_finished();

Generally, if an executed action modifies something, after which no other hook should do anything, call the finish() method. Logically, when receiving the action yourself, check is_finished().

 

Comments

  1. Love the plugin and appreciate all the opportunities to hook into things and muck around.

    Question: how would you recommend modifying the post status of broadcast posts only during their initial broadcasting? Our goal is to make all child posts ‘pending’ when they’re broadcast so that child site admins can choose which posts they want to appear on their site. They would then need to be able to publish posts or leave as pending without impacting the parent posts.

    We’re currently playing around with the ‘threewp_broadcast_broadcasting_after_update_post’ hook, but having issues determining when a post is being broadcast for the first time or if it’s an update.

    1. I’d have a look at the broadcasting_after_switch_to_blog action, together with the $bcd->new_post_created attribute. :)

      See broadcast / src / traits / broadcasting, at about line 322.

      If a new post was created, modify the $bcd->new_post->post_status.

    2. I’m digging a little deeper into this, and I think we need to stick with `broadcasting_after_update_post` because `$bcd->new_child_created = true;` doesn’t get set for the current blog until well after `broadcasting_after_switch_to_blog` has fired (around line 443).

      The thing is, we can’t seem to get ‘new_child_created’ to ever be true. Any thoughts on what’s up? Here’s the code we’re using. $bcd->new_post_created always seems to be false, no matter how we create/broadcast our posts in the admin.

      function broadcast_change_status( $action ) {
      $bcd = $action->broadcasting_data;
      if( $bcd->new_post_created ):
      $bcd->new_post->post_status = ‘pending’;
      $action->set_post_status( $bcd->new_post( ‘ID’ ), $bcd->new_post->post_status );
      endif;
      }

      add_action( ‘threewp_broadcast_broadcasting_after_update_post’, ‘broadcast_change_status’ );

    3. Ah geez, I found the issue: you’d mentioned $bcd->new_post_created, but it’s actually $bcd->new_child_created, which I’d even realized after digging around the code, but failed to update in my own code. So for anyone that stumbles on this, you can set new child post status like so:

      function broadcast_change_status( $action ) {
      $bcd = $action->broadcasting_data;
      if( $bcd->new_child_created ):
      $bcd->new_post->post_status = ‘pending’;
      wp_update_post($bcd->new_post);
      endif;
      }
      add_action( ‘threewp_broadcast_broadcasting_after_update_post’, ‘broadcast_change_status’ );

  2. Do you have an example on how to programmatically delete all linked child posts?
    I am unsure which action is better to use each_linked_post or trash_untrash_delete_post? and more importantly how to even use these methods.
    1. Try this simpler code on the parent post:

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

    2. that worked and was a lot cleaner solution than i had been trying to implement. thank you.

Leave a Reply

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