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
49 changes: 8 additions & 41 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,14 @@
<?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;

if ( function_exists( 'add_filter' ) ) {
add_filter(
'newfold/features/filter/register',
function ( $features ) {
return array_merge( $features, array( HelpCenterFeature::class ) );
}
);

}

new HelpCenterFeatureHooks();
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
"newfold-labs/wp-php-standards": "@stable"
},
"require": {
"newfold-labs/wp-module-data": "^2.3.4",
"newfold-labs/wp-module-ai": "^1.1.7"
"newfold-labs/wp-module-data": "^2.4.8",
"newfold-labs/wp-module-ai": "^1.1.7",
"newfold-labs/wp-module-features": "^1.3"
},
"autoload": {
"psr-4": {
"NewfoldLabs\\WP\\Module\\HelpCenter\\": "includes"
},
"files": [
"includes/HelpCenterFeature.php",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@circlecube This line is unnecessary since the class will be autoloaded using PSR-4 from the line above.

Suggested change
"includes/HelpCenterFeature.php",

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed, thanks

"bootstrap.php"
]
},
Expand Down
96 changes: 95 additions & 1 deletion composer.lock

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

66 changes: 66 additions & 0 deletions includes/HelpCenterFeature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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_DIR', __DIR__ );
define( 'NFD_HELPCENTER_BUILD_DIR', __DIR__ . '/build/' );
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() {
$capability = new SiteCapabilities();
$hasCapability = $capability->get( 'canAccessHelpCenter' );
$canManageOptions = current_user_can( 'manage_options' );
return (bool) $hasCapability && $canManageOptions;
}
}
69 changes: 69 additions & 0 deletions includes/HelpCenterFeatureHooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
namespace NewfoldLabs\WP\Module\HelpCenter;

use NewfoldLabs\WP\Module\Data\SiteCapabilities;

use function NewfoldLabs\WP\Module\Features\disable as disableFeature;
abhijitb marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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
$capability = new SiteCapabilities();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code formatting

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching, should be fixed now.

$hasCapability = $capability->get( 'canAccessHelpCenter' );
if ( ! $hasCapability ) {
return true;
}

return false;
}
}
12 changes: 3 additions & 9 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
<?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" />
</rule>
Expand Down
Loading