-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 4 commits
4f536fb
b392db1
7131315
c985438
f5e3a0e
8b7a315
4b0955a
bd8a4c9
8486ea2
e077db8
82904ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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; | ||
} | ||
} |
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code formatting There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed, thanks