diff --git a/plugins/woocommerce-admin/src/Loader.php b/plugins/woocommerce-admin/src/Loader.php index 64e6a3491d2..5fd28bd6151 100644 --- a/plugins/woocommerce-admin/src/Loader.php +++ b/plugins/woocommerce-admin/src/Loader.php @@ -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; + } ?> @@ -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 ); ?>