-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
2 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters