-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from newfold-labs/feature/help-center-feature
Set up help center feature
- Loading branch information
Showing
6 changed files
with
209 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,19 @@ | ||
<?php | ||
|
||
use NewfoldLabs\WP\Module\HelpCenter\HelpCenter; | ||
use NewfoldLabs\WP\ModuleLoader\Container; | ||
use NewfoldLabs\WP\Module\HelpCenter\Data\Brands; | ||
use function NewfoldLabs\WP\ModuleLoader\register; | ||
|
||
if ( function_exists( 'add_action' ) ) { | ||
|
||
add_action( | ||
'plugins_loaded', | ||
function () { | ||
if ( ! defined( 'USER_INTERACTION_SERVICE_BASE' ) ) { | ||
define( 'USER_INTERACTION_SERVICE_BASE', 'https://hiive.cloud/workers/ai-proxy/' ); | ||
} | ||
|
||
register( | ||
array( | ||
'name' => 'help-center', | ||
'label' => __( 'Help Center', 'wp-module-help-center' ), | ||
'callback' => function ( Container $container ) { | ||
define( 'NFD_HELPCENTER_PLUGIN_DIRNAME', dirname( $container->plugin()->basename ) ); | ||
define( 'NFD_HELPCENTER_DIR', __DIR__ ); | ||
define( 'NFD_HELPCENTER_BUILD_DIR', __DIR__ . '/build/' ); | ||
define( 'NFD_HELPCENTER_PLUGIN_URL', $container->plugin()->url ); | ||
/* | ||
Do not load Help Center when in Onboarding (Onboarding has no admin bar to toggle this). | ||
[TODO] Find a cleaner way to handle this. | ||
*/ | ||
if ( isset( $_GET['page'] ) && 'nfd-onboarding' === sanitize_text_field( $_GET['page'] ) ) { | ||
return; | ||
} | ||
|
||
new HelpCenter( $container ); | ||
|
||
// Define the brand | ||
Brands::set_current_brand( $container ); | ||
}, | ||
'isActive' => true, | ||
'isHidden' => true, | ||
) | ||
); | ||
|
||
namespace NewfoldLabs\WP\Module\HelpCenter; | ||
|
||
// Define global constants | ||
define( 'NFD_HELPCENTER_DIR', __DIR__ ); | ||
define( 'NFD_HELPCENTER_BUILD_DIR', __DIR__ . '/build/' ); | ||
|
||
// Register the HelpCenterFeature class in the features filter | ||
if ( function_exists( 'add_filter' ) ) { | ||
add_filter( | ||
'newfold/features/filter/register', | ||
function ( $features ) { | ||
return array_merge( $features, array( HelpCenterFeature::class ) ); | ||
} | ||
); | ||
|
||
} | ||
|
||
new HelpCenterFeatureHooks(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace NewfoldLabs\WP\Module\HelpCenter; | ||
|
||
use NewfoldLabs\WP\Module\HelpCenter\HelpCenter; | ||
use NewfoldLabs\WP\Module\HelpCenter\Data\Brands; | ||
use NewfoldLabs\WP\Module\Data\SiteCapabilities; | ||
|
||
use function NewfoldLabs\WP\ModuleLoader\container as getContainer; | ||
|
||
/** | ||
* Child class for a feature | ||
* | ||
* Child classes should define a name property as the feature name for all API calls. This name will be used in the registry. | ||
* Child class naming convention is {FeatureName}Feature. | ||
*/ | ||
class HelpCenterFeature extends \NewfoldLabs\WP\Module\Features\Feature { | ||
/** | ||
* The feature name. | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'helpCenter'; | ||
|
||
/** | ||
* The feature value. Defaults to on. | ||
* | ||
* @var boolean | ||
*/ | ||
protected $value = true; | ||
|
||
/** | ||
* Initialize staging feature. | ||
*/ | ||
public function initialize() { | ||
if ( function_exists( 'add_action' ) ) { | ||
|
||
// Register module | ||
add_action( | ||
'plugins_loaded', | ||
function () { | ||
$container = getContainer(); | ||
define( 'NFD_HELPCENTER_PLUGIN_DIRNAME', dirname( $container->plugin()->basename ) ); | ||
define( 'NFD_HELPCENTER_PLUGIN_URL', $container->plugin()->url ); | ||
new HelpCenter( $container ); | ||
// Define the brand | ||
Brands::set_current_brand( $container ); | ||
} | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Checks for capability and if user has permissions to toggle. | ||
* | ||
* @return bool True if the feature toggle is allowed, false otherwise. | ||
*/ | ||
public function canToggle() { | ||
$capabilies = new SiteCapabilities(); | ||
$hasCapability = $capabilies->get( 'canAccessHelpCenter' ); | ||
$canManageOptions = current_user_can( 'manage_options' ); | ||
return (bool) $hasCapability && $canManageOptions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
namespace NewfoldLabs\WP\Module\HelpCenter; | ||
|
||
use NewfoldLabs\WP\Module\Data\SiteCapabilities; | ||
|
||
/** | ||
* This class adds helpCenter feature hooks. | ||
**/ | ||
class HelpCenterFeatureHooks { | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct() { | ||
// set constant | ||
if ( ! defined( 'USER_INTERACTION_SERVICE_BASE' ) ) { | ||
define( 'USER_INTERACTION_SERVICE_BASE', 'https://hiive.cloud/workers/ai-proxy/' ); | ||
} | ||
|
||
if ( function_exists( 'add_action' ) ) { | ||
add_action( 'plugins_loaded', array( $this, 'hooks' ) ); | ||
} | ||
} | ||
|
||
/** | ||
* Add hooks. | ||
*/ | ||
public function hooks() { | ||
// add filter so we don't show/load help during onboarding | ||
add_filter( 'newfold/features/filter/isEnabled:helpCenter', array( $this, 'filterValue' ) ); | ||
} | ||
|
||
/** | ||
* Feature filter based on capabilities. | ||
* | ||
* @param boolean $value the value | ||
* @return boolean the filtered value | ||
*/ | ||
public function filterValue( $value ) { | ||
if ( $this->shouldDisable() ) { | ||
$value = false; | ||
} | ||
return $value; | ||
} | ||
|
||
/** | ||
* Context condition for disabling feature. | ||
* | ||
* @return boolean whether the feature should be disabled | ||
*/ | ||
public function shouldDisable() { | ||
// Do not load Help Center when in Onboarding (Onboarding has no admin bar to toggle this). | ||
$isOnboarding = isset( $_GET['page'] ) && 'nfd-onboarding' === sanitize_text_field( $_GET['page'] ); | ||
if ( $isOnboarding ) { | ||
return true; | ||
} | ||
// Do not load if `canAccessHelpCenter` capability is not true | ||
$capabilities = new SiteCapabilities(); | ||
$hasCapability = $capabilities->get( 'canAccessHelpCenter' ); | ||
if ( ! $hasCapability ) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,23 @@ | ||
<?xml version="1.0"?> | ||
<ruleset name="Project Rules"> | ||
<ruleset name="Help-Center-Module"> | ||
<rule ref="Newfold" /> | ||
<config name="testVersion" value="7.0-" /> | ||
<config name="minimum_supported_wp_version" value="6.0" /> | ||
<config name="testVersion" value="7.3-" /> | ||
<config name="minimum_supported_wp_version" value="6.3" /> | ||
<rule ref="WordPress-Extra"> | ||
<exclude name="WordPress.Arrays.ArrayDeclarationSpacing" /> | ||
|
||
<!-- Allow short array declaration --> | ||
<exclude name="Generic.Arrays.DisallowShortArraySyntax" /> | ||
|
||
<!-- Forget about file names --> | ||
<exclude name="WordPress.Files.FileName" /> | ||
|
||
<!-- Allow same line control structures e.g. if ( true ) { echo 1; } --> | ||
<exclude name="Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace" /> | ||
|
||
<!-- Don't require punctuation after inline comments --> | ||
<exclude name="Squiz.Commenting.InlineComment.InvalidEndChar" /> | ||
|
||
<!-- Allow empty catch statements --> | ||
<exclude name="Generic.CodeAnalysis.EmptyStatement.DetectedCatch" /> | ||
|
||
<!-- Comment punctuation doesn't matter --> | ||
<exclude name="Squiz.Commenting.FunctionComment.ParamCommentFullStop" /> | ||
<exclude name="WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid"/> | ||
<exclude name="WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase"/> | ||
</rule> | ||
</ruleset> |