don't load analytics dashboard for users without needed capability (https://github.com/woocommerce/woocommerce-admin/pull/3386)

* don't load analytics dashboard for users without needed capability

* add helper function
This commit is contained in:
Ron Rennick 2019-12-10 10:04:39 -04:00 committed by GitHub
parent 47ca4f1032
commit d1b356919c
1 changed files with 45 additions and 2 deletions

View File

@ -33,6 +33,13 @@ class Loader {
*/
protected static $classes = array();
/**
* WordPress capability required to use analytics features.
*
* @var string
*/
protected static $required_capability = null;
/**
* Get class instance.
*/
@ -116,6 +123,32 @@ class Loader {
return apply_filters( 'woocommerce_admin_features', array() );
}
/**
* Gets WordPress capability required to use analytics features.
*
* @return string
*/
public static function get_analytics_capability() {
if ( null === static::$required_capability ) {
/**
* Filters the required capability to use the analytics features.
*
* @param string $capability WordPress capability.
*/
static::$required_capability = apply_filters( 'woocommerce_analytics_menu_capability', 'view_woocommerce_reports' );
}
return static::$required_capability;
}
/**
* Helper function indicating whether the current user has the required analytics capability.
*
* @return bool
*/
public static function user_can_analytics() {
return current_user_can( static::get_analytics_capability() );
}
/**
* Returns if a specific wc-admin feature is enabled.
*
@ -204,14 +237,13 @@ class Loader {
* @todo The entry point for the embed needs moved to this class as well.
*/
public static function register_page_handler() {
$analytics_cap = apply_filters( 'woocommerce_analytics_menu_capability', 'view_woocommerce_reports' );
wc_admin_register_page(
array(
'id' => 'woocommerce-dashboard', // Expected to be overridden if dashboard is enabled.
'parent' => 'woocommerce',
'title' => null,
'path' => self::APP_ENTRY_POINT,
'capability' => $analytics_cap,
'capability' => static::get_analytics_capability(),
)
);
@ -378,6 +410,10 @@ class Loader {
return;
}
if ( ! static::user_can_analytics() ) {
return;
}
wp_enqueue_script( WC_ADMIN_APP );
wp_enqueue_style( WC_ADMIN_APP );
wp_enqueue_style( 'wc-material-icons' );
@ -424,6 +460,9 @@ class Loader {
* @param array $section Section to create breadcrumb from.
*/
private static function output_breadcrumbs( $section ) {
if ( ! static::user_can_analytics() ) {
return;
}
?>
<span>
<?php if ( is_array( $section ) ) : ?>
@ -444,6 +483,10 @@ class Loader {
return;
}
if ( ! static::user_can_analytics() ) {
return;
}
$sections = self::get_embed_breadcrumbs();
$sections = is_array( $sections ) ? $sections : array( $sections );
?>