Skip to content

Commit

Permalink
Merge pull request #155 from plausible/show_stats_to_administrator
Browse files Browse the repository at this point in the history
Added disabled/checked Administrator role to "Show stats to..." setting.
  • Loading branch information
Dan0sz authored Aug 4, 2023
2 parents 344ce9d + 26ce978 commit ae59fcc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Admin/Settings/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function render_checkbox_field( array $field ) {
$settings = Helpers::get_settings();
$slug = ! empty( $settings[ $field['slug'] ] ) ? $settings[ $field['slug'] ] : '';
$id = $field['slug'] . '_' . str_replace( '-', '_', sanitize_title( $field['label'] ) );
$checked = is_array( $slug ) ? checked( $value, in_array( $value, $slug, false ) ? $value : false, false ) : checked( $value, $slug, false );
$checked = ! empty( $field['checked'] ) ? 'checked="checked"' : ( is_array( $slug ) ? checked( $value, in_array( $value, $slug, false ) ? $value : false, false ) : checked( $value, $slug, false ) );
$disabled = ! empty( $field['disabled'] ) ? 'disabled' : '';

?>
Expand Down
23 changes: 17 additions & 6 deletions src/Admin/Settings/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function __construct() {
'type' => 'group',
'desc' => esc_html__( 'By default, the stats dashboard is only available to logged in administrators. If you want the dashboard to be available for other logged in users, then please specify them above.', 'plausible-analytics' ),
'toggle' => false,
'fields' => $this->build_user_roles_array( 'expand_dashboard_access', [ 'administrator' ] ),
'fields' => $this->build_user_roles_array( 'expand_dashboard_access', [ 'administrator' => true ] ),
],
[
'label' => esc_html__( 'Disable menu in toolbar', 'plausible-analytics' ),
Expand Down Expand Up @@ -514,6 +514,13 @@ public function render_proxy_warning() {
}
}

/**
* Renders the Self-hosted warning if the Proxy is enabled.
*
* @since 1.3.3
*
* @return void
*/
public function maybe_render_self_hosted_warning() {
if ( Helpers::proxy_enabled() ) {
echo wp_kses( __( 'This option is disabled, because the <strong>Proxy</strong> setting is enabled under <em>General</em> settings.', 'plausible-analytics' ), 'post' );
Expand All @@ -527,20 +534,24 @@ public function maybe_render_self_hosted_warning() {
*
* @return array
*/
private function build_user_roles_array( $slug, $filter_elements = [] ) {
private function build_user_roles_array( $slug, $disable_elements = [] ) {
$wp_roles = wp_roles()->roles ?? [];

foreach ( $wp_roles as $id => $role ) {
if ( in_array( $id, $filter_elements, true ) ) {
continue;
}

$roles_array[ $id ] = [
'label' => $role['name'] ?? '',
'slug' => $slug,
'type' => 'checkbox',
'value' => $id,
];

if ( in_array( $id, array_keys( $disable_elements ), true ) ) {
$roles_array[ $id ]['disabled'] = true;

if ( ! empty( $disable_elements[ $id ] ) ) {
$roles_array[ $id ]['checked'] = $disable_elements[ $id ];
}
}
}

ksort( $roles_array, SORT_STRING );
Expand Down

0 comments on commit ae59fcc

Please sign in to comment.