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

Set up help center feature #46

Merged
merged 11 commits into from
May 29, 2024
56 changes: 14 additions & 42 deletions bootstrap.php
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();
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
"newfold-labs/wp-php-standards": "@stable"
},
"require": {
"newfold-labs/wp-module-data": "^2.3.4",
"newfold-labs/wp-module-ai": "^1.1.9"
"newfold-labs/wp-module-ai": "^1.1.9",
"newfold-labs/wp-module-data": "^2.5.0",
"newfold-labs/wp-module-features": "^1.4.1"
},
"autoload": {
"psr-4": {
Expand Down
59 changes: 58 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions includes/HelpCenterFeature.php
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;
}
}
65 changes: 65 additions & 0 deletions includes/HelpCenterFeatureHooks.php
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;
}
}
14 changes: 5 additions & 9 deletions phpcs.xml
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>