Skip to content

Commit

Permalink
use feature hooks class for managing the hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
circlecube committed May 13, 2024
1 parent bbd4374 commit a1921af
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
29 changes: 2 additions & 27 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
<?php

use function NewfoldLabs\WP\Context\getContext;
use function NewfoldLabs\WP\Module\Features\getFeature;
namespace NewfoldLabs\WP\Module\Staging;

if ( function_exists( 'add_action' ) ) {

// update as needed based on context
add_filter(
'newfold/features/filter/isEnabled/staging',
function($value) {
if ( 'atomic' === getContext( 'platform' ) ) {
$value = false;
}
return $value;
}
);

add_action(
'after_setup_theme',
function () {
if ( 'atomic' === getContext( 'platform' ) ) {
$stagingFeature = getFeature('staging');
if ( $stagingFeature ) {
$stagingFeature->disable();
}
}
}
);
}
new StagingFeatureHooks();
52 changes: 52 additions & 0 deletions includes/StagingFeatureHooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace NewfoldLabs\WP\Module\Staging;

use function NewfoldLabs\WP\Context\getContext;
use function NewfoldLabs\WP\Module\Features\disable as disableFeature;

class StagingFeatureHooks {

/**
* Constructor.
*/
public function __construct() {
$this->hooks();
}

/**
* Add hooks.
*/
public function hooks() {

// Filter vale based on context
add_filter( 'newfold/features/filter/isEnabled:staging', array( $this, 'filterValue' ) );

// Force disable based on context
add_action( 'newfold/features/action/onEnable:staging', array( $this, 'maybeDisable' ) );

// Check if should disable on setup
add_action( 'after_setup_theme', array( $this, 'maybeDisable' ) );

}

// Feature filter based on context
function filterValue( $value ) {
if ( $this->shouldDisable() ) {
$value = false;
}
return $value;
}

// Maybe disable
function maybeDisable() {
if ( $this->shouldDisable() ) {
disableFeature('staging');
}
}

// Context condition for disabling feature
function shouldDisable() {
// check for atomic context
return 'atomic' === getContext( 'platform' );
}
}

0 comments on commit a1921af

Please sign in to comment.