Skip to content

Commit d620831

Browse files
committed
New setting "Also show dashboard to"
1 parent 53e3c06 commit d620831

File tree

3 files changed

+86
-20
lines changed

3 files changed

+86
-20
lines changed

includes/admin/class-dashboard.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace WebberZone\Top_Ten\Admin;
1313

14+
use WebberZone\Top_Ten\Admin\Settings\Settings_API;
15+
1416
if ( ! defined( 'WPINC' ) ) {
1517
die;
1618
}
@@ -176,10 +178,12 @@ public function render_page() {
176178
* @since 3.0.0
177179
*/
178180
public function admin_menu() {
181+
$roles = wp_parse_list( \tptn_get_option( 'show_dashboard_to_roles' ) );
182+
179183
$this->parent_id = add_menu_page(
180184
esc_html__( 'Top 10 Dashboard', 'top-10' ),
181185
esc_html__( 'Top 10', 'top-10' ),
182-
'manage_options',
186+
Settings_API::get_capability_for_menu( $roles ),
183187
'tptn_dashboard',
184188
array( $this, 'render_page' ),
185189
'dashicons-editor-ol'
@@ -189,7 +193,7 @@ public function admin_menu() {
189193
'tptn_dashboard',
190194
esc_html__( 'Top 10 Dashboard', 'top-10' ),
191195
esc_html__( 'Dashboard', 'top-10' ),
192-
'manage_options',
196+
Settings_API::get_capability_for_menu( $roles ),
193197
'tptn_dashboard',
194198
array( $this, 'render_page' )
195199
);
@@ -234,7 +238,9 @@ public function admin_enqueue_scripts( $hook ) {
234238
public function get_chart_data() {
235239
global $wpdb;
236240

237-
if ( ! current_user_can( 'manage_options' ) ) {
241+
$roles = wp_parse_list( \tptn_get_option( 'show_dashboard_to_roles' ) );
242+
243+
if ( ! current_user_can( Settings_API::get_capability_for_menu( $roles ) ) ) {
238244
wp_die();
239245
}
240246
check_ajax_referer( 'tptn-dashboard', 'security' );

includes/admin/settings/class-settings-api.php

+38-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Settings_API {
3131
*
3232
* @var string
3333
*/
34-
const VERSION = '2.5.1';
34+
const VERSION = '2.5.2';
3535

3636
/**
3737
* Settings Key.
@@ -333,7 +333,7 @@ public function add_custom_menu_page( $menu ) {
333333
'parent_slug' => 'options-general.php',
334334
'page_title' => '',
335335
'menu_title' => '',
336-
'capability' => 'manage_options',
336+
'capability' => $this->get_capability_for_menu(),
337337
'menu_slug' => '',
338338
'function' => array( $this, 'plugin_settings' ),
339339

@@ -416,6 +416,42 @@ public function admin_menu() {
416416
add_action( 'load-' . $this->settings_page, array( $this, 'settings_help' ) );
417417
}
418418

419+
/**
420+
* Get the appropriate capability for the menu based on the user's roles and settings.
421+
*
422+
* @param array $roles Array of roles to check.
423+
* @param string $base_capability The default capability.
424+
* @param \WP_User $current_user The current user object.
425+
* @param array $role_capabilities Array of role capabilities.
426+
* @return string The capability to use for the menu.
427+
*/
428+
public static function get_capability_for_menu( $roles = array(), $base_capability = 'manage_options', $current_user = null, $role_capabilities = array() ) {
429+
if ( ! $current_user ) {
430+
$current_user = wp_get_current_user();
431+
}
432+
433+
if ( empty( $roles ) || in_array( 'administrator', $current_user->roles, true ) ) {
434+
return $base_capability;
435+
}
436+
437+
if ( empty( $role_capabilities ) ) {
438+
$role_capabilities = array(
439+
'editor' => 'edit_others_posts',
440+
'author' => 'publish_posts',
441+
'contributor' => 'edit_posts',
442+
'subscriber' => 'read',
443+
);
444+
}
445+
446+
foreach ( $current_user->roles as $role ) {
447+
if ( in_array( $role, $roles, true ) && isset( $role_capabilities[ $role ] ) ) {
448+
return $role_capabilities[ $role ];
449+
}
450+
}
451+
452+
return $base_capability;
453+
}
454+
419455
/**
420456
* Enqueue scripts and styles.
421457
*

includes/admin/settings/class-settings.php

+39-15
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public static function settings_general() {
281281
),
282282
'show_metabox_admins' => array(
283283
'id' => 'show_metabox_admins',
284-
'name' => esc_html__( 'Limit meta box to Admins only', 'top-10' ),
284+
'name' => esc_html__( 'Limit meta box to Admins', 'top-10' ),
285285
'desc' => esc_html__( 'If selected, the meta box will be hidden from anyone who is not an Admin. By default, Contributors and above will be able to see the meta box. Applies only if the above option is selected.', 'top-10' ),
286286
'type' => 'checkbox',
287287
'options' => false,
@@ -308,6 +308,15 @@ public static function settings_general() {
308308
'type' => 'checkbox',
309309
'options' => true,
310310
),
311+
'show_dashboard_to_roles' => array(
312+
'id' => 'show_dashboard_to_roles',
313+
'name' => esc_html__( 'Also show dashboard to', 'top-10' ),
314+
'desc' => esc_html__( 'Choose the user roles that should have access to the Top 10 dashboard, which showcases popular posts over time. These roles are linked to specific capabilities, and selecting a lower role will automatically grant access to higher roles.', 'top-10' ),
315+
'type' => 'multicheck',
316+
'default' => 'administrator',
317+
'options' => self::get_user_roles( array( 'administrator' ) ),
318+
'pro' => true,
319+
),
311320
);
312321

313322
/**
@@ -342,10 +351,7 @@ public static function settings_counter() {
342351
/* translators: 1: Code. */
343352
'desc' => sprintf( esc_html__( 'If you choose to disable this, please add the following code to your template file where you want it displayed: %1$s', 'top-10' ), "<code>&lt;?php if ( function_exists( 'echo_tptn_post_count' ) ) { echo_tptn_post_count(); } ?&gt;</code>" ),
344353
'type' => 'multicheck',
345-
'default' => array(
346-
'single' => 'single',
347-
'page' => 'page',
348-
),
354+
'default' => 'single,page',
349355
'options' => array(
350356
'single' => esc_html__( 'Posts', 'top-10' ),
351357
'page' => esc_html__( 'Pages', 'top-10' ),
@@ -434,12 +440,9 @@ public static function settings_counter() {
434440
'id' => 'trackers',
435441
'name' => esc_html__( 'Enable trackers', 'top-10' ),
436442
/* translators: 1: Code. */
437-
'desc' => '',
443+
'desc' => esc_html__( 'Top 10 tracks hits in two tables in the database. The overall table only tracks the total hits per post. The daily table tracks hits per post on an hourly basis.', 'top-10' ),
438444
'type' => 'multicheck',
439-
'default' => array(
440-
'overall' => 'overall',
441-
'daily' => 'daily',
442-
),
445+
'default' => 'overall,daily',
443446
'options' => array(
444447
'overall' => esc_html__( 'Overall', 'top-10' ),
445448
'daily' => esc_html__( 'Daily range', 'top-10' ),
@@ -465,11 +468,7 @@ public static function settings_counter() {
465468
'name' => esc_html__( 'Track user groups', 'top-10' ) . ':',
466469
'desc' => esc_html__( 'Uncheck above to disable tracking if the current user falls into any one of these groups.', 'top-10' ),
467470
'type' => 'multicheck',
468-
'default' => array(
469-
'authors' => 'authors',
470-
'editors' => 'editors',
471-
'admins' => 'admins',
472-
),
471+
'default' => 'authors,editors,admins',
473472
'options' => array(
474473
'authors' => esc_html__( 'Authors', 'top-10' ),
475474
'editors' => esc_html__( 'Editors', 'top-10' ),
@@ -1095,6 +1094,31 @@ public static function get_styles() {
10951094
return apply_filters( self::$prefix . '_get_styles', $styles );
10961095
}
10971096

1097+
/**
1098+
* Get User Roles.
1099+
*
1100+
* @since 3.4.0
1101+
*
1102+
* @param array $remove_roles Roles to remove.
1103+
* @return array User roles in the format 'role' => 'name'.
1104+
*/
1105+
public static function get_user_roles( $remove_roles = array() ) {
1106+
global $wp_roles;
1107+
1108+
// Initialize the array to store roles in the desired format.
1109+
$roles_array = array();
1110+
1111+
// Loop through all roles and store them in 'role' => 'name' format.
1112+
foreach ( $wp_roles->roles as $role_key => $role_details ) {
1113+
if ( in_array( $role_key, $remove_roles, true ) ) {
1114+
continue;
1115+
}
1116+
$roles_array[ $role_key ] = esc_html( $role_details['name'] );
1117+
}
1118+
1119+
return $roles_array;
1120+
}
1121+
10981122
/**
10991123
* Adding WordPress plugin action links.
11001124
*

0 commit comments

Comments
 (0)