Skip to content

Commit

Permalink
Settings tab for Notifications, resolves #8, also fixes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
dero committed Feb 6, 2014
1 parent 5fa28cb commit a8b4658
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 2 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_Notification_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(
'notifications' => array(
'title' => __( 'Notifications', 'stream-notifications' ),
'fields' => array(
array(
'name' => 'role_access',
'title' => __( 'Role Access', 'stream-notifications' ),
'type' => 'multi_checkbox',
'desc' => __( 'Users from the selected roles above will have permission to view, create and edit Stream Notifications. However, only site Administrators can access Stream Notifications Settings.', 'stream-notifications' ),
'choices' => WP_Stream_Settings::get_roles(),
'default' => array( 'administrator' ),
),
),
),
);

self::$fields = apply_filters( 'wp_stream_notifications_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_Notifications::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_Notifications::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['notifications_role_access'] ) ) {
return true;
}

return false;
}

}
15 changes: 13 additions & 2 deletions stream-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ class WP_Stream_Notifications {
*/
public $form;

/**
* Capability for the Notifications to be viewed
*
* @const string
*/
const VIEW_CAP = 'view_stream_notifications';

/**
* Class constructor
*/
Expand Down Expand Up @@ -129,6 +136,10 @@ public function load() {
include $class;
}

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

add_action( 'admin_menu', array( $this, 'register_menu' ), 11 );

// Default list actions handlers
Expand All @@ -140,7 +151,7 @@ public function load() {
$this->matcher = new WP_Stream_Notification_Rule_Matcher();

// Load form class

if ( is_admin() ) {
include WP_STREAM_NOTIFICATIONS_INC_DIR . '/form.php';
$this->form = new WP_Stream_Notifications_Form;
Expand All @@ -158,7 +169,7 @@ public function register_menu() {
'wp_stream',
__( 'Notifications', 'stream-notifications' ),
__( 'Notifications', 'stream-notifications' ),
'manage_options',
self::VIEW_CAP,
self::NOTIFICATIONS_PAGE_SLUG,
array( $this, 'page' )
);
Expand Down

0 comments on commit a8b4658

Please sign in to comment.