Skip to content

Commit

Permalink
Merge pull request #8 from x-team/issue-3
Browse files Browse the repository at this point in the history
Add settings tab with custom roles
  • Loading branch information
frankiejarrett committed Feb 10, 2014
2 parents 1710d30 + f186298 commit 4d07b29
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 7 deletions.
112 changes: 112 additions & 0 deletions includes/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Settings class for Stream Notifications
*
* @author X-Team <x-team.com>
* @author Shady Sharaf <shady@x-team.com>, Jaroslav Polakovič <dero@x-team.com>
*/
class WP_Stream_Reports_Settings {

public static $fields = array();

/**
* Public constructor
*/
public static function load() {
// User and role caps
add_filter( 'user_has_cap', array( __CLASS__, '_filter_user_caps' ), 10, 4 );
add_filter( 'role_has_cap', array( __CLASS__, '_filter_role_caps' ), 10, 3 );

// Add Notifications settings tab to Stream settings
add_filter( 'wp_stream_options_fields', array( __CLASS__, '_register_settings' ) );
}

public static function get_fields() {
if ( empty( self::$fields ) ) {
$fields = array(
'reports' => array(
'title' => __( 'Reports', 'stream-reports' ),
'fields' => array(
array(
'name' => 'role_access',
'title' => __( 'Role Access', 'stream-reports' ),
'type' => 'multi_checkbox',
'desc' => __( 'Users from the selected roles above will have permission to view and edit Stream Reports. However, only site Administrators can access Stream Reports Settings.', 'stream-reports' ),
'choices' => WP_Stream_Settings::get_roles(),
'default' => array( 'administrator' ),
),
),
),
);

self::$fields = apply_filters( 'wp_stream_reports_options_fields', $fields );
}
return self::$fields;
}

/**
* Appends Notifications settings to Stream settings
*
* @filter wp_stream_options_fields
*/
public static function _register_settings( $stream_fields ) {
return array_merge( $stream_fields, self::get_fields() );
}

/**
* Filter user caps to dynamically grant our view cap based on allowed roles
*
* @filter user_has_cap
*
* @param $allcaps
* @param $caps
* @param $args
* @param $user
*
* @return array
*/
public static function _filter_user_caps( $allcaps, $caps, $args, $user = null ) {
$user = is_a( $user, 'WP_User' ) ? $user : wp_get_current_user();

foreach ( $caps as $cap ) {
if ( WP_Stream_Reports::VIEW_CAP === $cap ) {
foreach ( $user->roles as $role ) {
if ( self::_role_can_access_notifications( $role ) ) {
$allcaps[ $cap ] = true;
break 2;
}
}
}
}

return $allcaps;
}

/**
* Filter role caps to dynamically grant our view cap based on allowed roles
*
* @filter role_has_cap
*
* @param $allcaps
* @param $cap
* @param $role
*
* @return array
*/
public static function _filter_role_caps( $allcaps, $cap, $role ) {
if ( WP_Stream_Reports::VIEW_CAP === $cap && self::_role_can_access_notifications( $role ) ) {
$allcaps[ $cap ] = true;
}

return $allcaps;
}

private static function _role_can_access_notifications( $role ) {
if ( in_array( $role, WP_Stream_Settings::$options['reports_role_access'] ) ) {
return true;
}

return false;
}

}
1 change: 0 additions & 1 deletion phpcs.ruleset.xml

This file was deleted.

11 changes: 11 additions & 0 deletions phpcs.ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards for Plugins">
<description>Generally-applicable sniffs for WordPress plugins</description>

<rule ref="WordPress" />

<rule ref="WordPress.NamingConventions.ValidFunctionName">
<exclude-pattern>/tests/*</exclude-pattern><!-- because of PHPUnit method names -->
</rule>

</ruleset>
17 changes: 11 additions & 6 deletions stream-reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Author: X-Team
* Author URI: http://x-team.com/wordpress/
* License: GPLv2+
* Text Domain: stream-notifications
* Text Domain: stream-reports
* Domain Path: /languages
*/

Expand Down Expand Up @@ -72,16 +72,16 @@ class WP_Stream_Reports {
*
* @const string
*/
const VIEW_CAP = 'manage_options';
const VIEW_CAP = 'view_stream_reports';

/**
* Class constructor
*/
private function __construct() {
define( 'WP_STREAM_REPORTS_DIR', plugin_dir_path( __FILE__ ) );
define( 'WP_STREAM_REPORTS_URL', plugin_dir_url( __FILE__ ) );
define( 'WP_STREAM_REPORTS_INC_DIR', WP_STREAM_NOTIFICATIONS_DIR . 'includes/' );
define( 'WP_STREAM_REPORTS_CLASS_DIR', WP_STREAM_NOTIFICATIONS_DIR . 'classes/' );
define( 'WP_STREAM_REPORTS_INC_DIR', WP_STREAM_REPORTS_DIR . 'includes/' );
define( 'WP_STREAM_REPORTS_CLASS_DIR', WP_STREAM_REPORTS_DIR . 'classes/' );

add_action( 'plugins_loaded', array( $this, 'load' ) );
}
Expand All @@ -99,6 +99,10 @@ public function load() {
return;
}

// Load settings, enabling extensions to hook in
require_once WP_STREAM_REPORTS_INC_DIR . 'settings.php';
WP_Stream_Reports_Settings::load();

// Register new submenu
add_action( 'admin_menu', array( $this, 'register_menu' ), 11 );
}
Expand All @@ -119,8 +123,8 @@ public function register_menu() {
array( $this, 'page' )
);

// add_action( 'load-' . self::$screen_id, array( $this, 'page_form_save' ) );
// add_action( 'load-' . self::$screen_id, array( $this->form, 'load' ) );
// add_action( 'load-' . self::$screen_id, array( $this, 'page_form_save' ) );
// add_action( 'load-' . self::$screen_id, array( $this->form, 'load' ) );
}

/**
Expand All @@ -133,6 +137,7 @@ public function page() {
$view = filter_input( INPUT_GET, 'view', FILTER_DEFAULT, array( 'options' => array( 'default' => 'list' ) ) );
$id = filter_input( INPUT_GET, 'id' );

return 'This is the page';
/*switch ( $view ) {
case 'rule':
$this->page_form( $id );
Expand Down

0 comments on commit 4d07b29

Please sign in to comment.