Skip to content

Commit

Permalink
Merge pull request #1469 from shadyvb/fix/1468-dynamic-callback-format
Browse files Browse the repository at this point in the history
Fix dynamic callback method detection
  • Loading branch information
delawski authored Jul 22, 2024
2 parents 159e61f + d1bd648 commit c6e12ba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion classes/class-connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function unregister() {
*/
public function callback() {
$action = current_filter();
$callback = array( $this, 'callback_' . preg_replace( '/[^a-z0-9_\-]/', '_', $action ) );
$callback = array( $this, 'callback_' . preg_replace( '/[^a-z0-9_]/', '_', $action ) );

// For the sake of testing, trigger an action with the name of the callback.
if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) {
Expand Down
23 changes: 23 additions & 0 deletions tests/tests/test-class-connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function setUp(): void {
*/
public $actions = array(
'simulate_fault',
'hyphenated-action',
);

/**
Expand Down Expand Up @@ -69,6 +70,17 @@ public function callback_simulate_fault() {
// This is used to check if this callback method actually ran
do_action( 'wp_stream_test_child_callback_simulate_fault' );
}

/**
* Log the hyphenated action callback.
*
* @action hyphenated-action
*
* @return void
*/
public function callback_hyphenated_action() {
do_action( 'wp_stream_test_child_callback_hyphenated_action' );
}
};

$this->assertNotEmpty( $this->connector );
Expand Down Expand Up @@ -111,6 +123,17 @@ public function test_callback() {
$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'child_callback_' . $action ) );
}

public function test_callback_hyphenated() {
global $wp_current_filter;
$action = $this->connector->actions[1];
$wp_current_filter[] = $action; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

$this->connector->callback();

$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'callback_hyphenated_action' ) );
$this->assertGreaterThan( 0, did_action( $this->action_prefix . 'child_callback_hyphenated_action' ) );
}

public function test_action_links() {
$current_links = array(
'IMDB' => '',
Expand Down

0 comments on commit c6e12ba

Please sign in to comment.