Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(data-events): current_event() method #3383

Draft
wants to merge 4 commits into
base: trunk
Choose a base branch
from

Conversation

miguelpeixe
Copy link
Member

@miguelpeixe miguelpeixe commented Aug 30, 2024

Make the current event name available during the handler execution.

All Submissions:

Changes proposed in this Pull Request:

Allows for a handler to always be aware of which data event action it's under.

Take this example:

Data_Events::register_handler( 'create_contact', 'reader_registered' );
Data_Events::register_handler( 'update_lists', 'reader_subscribed' );

function create_contact( $timestamp, $data, $client_id ) {
  // Some data manipulation.
  sync( $contact );
}
function update_lists( $timestamp, $data, $client_id ) {
  // Some data manipulation.
  sync( $contact );
}

function sync( $contact ) {
  $contact = apply_filters( 'sync_contact', $contact );
  do_sync( $contact );
}

add_filter(
  'sync_contact',
  function( $contact ) {
    if ( 'reader_registered' === Data_Events::current_event() ) {
      // Some data manipulation only if it's called under "reader_registered"
    }
  }
);

How to test the changes in this Pull Request:

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant