diff --git a/includes/settings.php b/includes/settings.php new file mode 100644 index 000000000..be4e659d6 --- /dev/null +++ b/includes/settings.php @@ -0,0 +1,112 @@ + + * @author Shady Sharaf , Jaroslav Polakovič + */ +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; + } + +} diff --git a/phpcs.ruleset.xml b/phpcs.ruleset.xml deleted file mode 120000 index 8f964bf70..000000000 --- a/phpcs.ruleset.xml +++ /dev/null @@ -1 +0,0 @@ -bin/phpcs.ruleset.xml \ No newline at end of file diff --git a/phpcs.ruleset.xml b/phpcs.ruleset.xml new file mode 100644 index 000000000..fbb2829b1 --- /dev/null +++ b/phpcs.ruleset.xml @@ -0,0 +1,11 @@ + + + Generally-applicable sniffs for WordPress plugins + + + + + /tests/* + + + diff --git a/stream-reports.php b/stream-reports.php index ae4432b6d..44b1e7ce3 100644 --- a/stream-reports.php +++ b/stream-reports.php @@ -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 */ @@ -72,7 +72,7 @@ class WP_Stream_Reports { * * @const string */ - const VIEW_CAP = 'manage_options'; + const VIEW_CAP = 'view_stream_reports'; /** * Class constructor @@ -80,8 +80,8 @@ class WP_Stream_Reports { 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' ) ); } @@ -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 ); } @@ -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' ) ); } /** @@ -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 );