Skip to content

Commit

Permalink
Merge pull request #1148 from xwp/feature/connector-unregister-function
Browse files Browse the repository at this point in the history
Adds connector unregister functionality
  • Loading branch information
kidunot89 authored Aug 6, 2020
2 parents 0223f7e + 1369b68 commit 80477f7
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
39 changes: 39 additions & 0 deletions classes/class-connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,54 @@ abstract class Connector {
*/
public $register_frontend = true;

/**
* Holds connector registration status flag.
*
* @var bool
*/
private $is_registered = false;

/**
* Is the connector currently registered?
*
* @return boolean
*/
public function is_registered() {
return $this->is_registered;
}

/**
* Register all context hooks
*/
public function register() {
if ( $this->is_registered ) {
return;
}

foreach ( $this->actions as $action ) {
add_action( $action, array( $this, 'callback' ), 10, 99 );
}

add_filter( 'wp_stream_action_links_' . $this->name, array( $this, 'action_links' ), 10, 2 );

$this->is_registered = true;
}

/**
* Unregister all context hooks
*/
public function unregister() {
if ( ! $this->is_registered ) {
return;
}

foreach ( $this->actions as $action ) {
remove_action( $action, array( $this, 'callback' ), 10, 99 );
}

remove_filter( 'wp_stream_action_links_' . $this->name, array( $this, 'action_links' ), 10, 2 );

$this->is_registered = false;
}

/**
Expand Down
40 changes: 40 additions & 0 deletions classes/class-connectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,44 @@ public function load_connectors() {
*/
do_action( 'wp_stream_after_connectors_registration', $labels, $this );
}

/**
* Unregisters the context hooks for all connectors.
*/
public function unload_connectors() {
foreach ( $this->connectors as $connector ) {
$connector->unregister();
}
}

/**
* Reregisters the context hooks for all connectors.
*/
public function reload_connectors() {
foreach ( $this->connectors as $connector ) {
$connector->register();
}
}

/**
* Unregisters the context hooks for a connectors.
*
* @param string $name Name of the connector.
*/
public function unload_connector( $name ) {
if ( ! empty( $this->connectors[ $name ] ) ) {
$this->connectors[ $name ]->unregister();
}
}

/**
* Reregisters the context hooks for a connector.
*
* @param string $name Name of the connector.
*/
public function reload_connector( $name ) {
if ( ! empty( $this->connectors[ $name ] ) ) {
$this->connectors[ $name ]->register();
}
}
}
16 changes: 15 additions & 1 deletion tests/tests/test-class-connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ public function test_register() {
}
}

public function test_unregister() {
$this->connector->register();

foreach ( $this->connector->actions as $tag ) {
$this->assertGreaterThan( 0, has_action( $tag ) );
}

$this->connector->unregister();

foreach ( $this->connector->actions as $tag ) {
$this->assertFalse( has_action( $tag ) );
}
}

public function test_callback() {
global $wp_current_filter;
$action = $this->connector->actions[0];
Expand Down Expand Up @@ -222,4 +236,4 @@ 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' );
}
}
}
47 changes: 47 additions & 0 deletions tests/tests/test-class-connectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,51 @@ public function test_load_connectors() {

$this->assertEmpty( $notices );
}

public function test_unload_connectors() {
$this->connectors->load_connectors();
$this->assertNotEmpty( $this->connectors->connectors );

foreach( $this->connectors->connectors as $connector ) {
$this->assertTrue( $connector->is_registered() );
}

$this->connectors->unload_connectors();
foreach( $this->connectors->connectors as $connector ) {
$this->assertFalse( $connector->is_registered() );
}
}

public function test_reload_connectors() {
$this->connectors->load_connectors();
$this->assertNotEmpty( $this->connectors->connectors );
$this->connectors->unload_connectors();
foreach( $this->connectors->connectors as $connector ) {
$this->assertFalse( $connector->is_registered() );
}

$this->connectors->reload_connectors();
foreach( $this->connectors->connectors as $connector ) {
$this->assertTrue( $connector->is_registered() );
}
}

public function test_unload_connector() {
$this->connectors->load_connectors();
$this->assertNotEmpty( $this->connectors->connectors['posts'] );
$this->assertTrue( $this->connectors->connectors['posts']->is_registered() );

$this->connectors->unload_connector( 'posts' );
$this->assertFalse( $this->connectors->connectors['posts']->is_registered() );
}

public function test_reload_connector() {
$this->connectors->load_connectors();
$this->assertNotEmpty( $this->connectors->connectors['posts'] );
$this->connectors->unload_connector( 'posts' );
$this->assertFalse( $this->connectors->connectors['posts']->is_registered() );

$this->connectors->reload_connector( 'posts' );
$this->assertTrue( $this->connectors->connectors['posts']->is_registered() );
}
}

0 comments on commit 80477f7

Please sign in to comment.