Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roles & Capabilities start AND Improved testing factory #534

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gravityview.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
require_once( GRAVITYVIEW_DIR . 'includes/class-common.php');
require_once( GRAVITYVIEW_DIR . 'includes/connector-functions.php');
require_once( GRAVITYVIEW_DIR . 'includes/class-gravityview-compatibility.php' );
require_once( GRAVITYVIEW_DIR . 'includes/class-gravityview-roles-capabilities.php' );

/** Register Post Types and Rewrite Rules */
require_once( GRAVITYVIEW_DIR . 'includes/class-post-types.php');
Expand Down Expand Up @@ -218,6 +219,8 @@ public static function activate( $network_wide = false ) {

// Clear settings transient
delete_transient( 'redux_edd_license_license_valid' );

GravityView_Roles_Capabilities::get_instance()->add_caps();
}


Expand Down
119 changes: 0 additions & 119 deletions includes/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -874,125 +874,6 @@ function gravityview_get_the_term_list( $post_id, $link = true, $taxonomy = 'pos

}

/**
* Do a _very_ basic match for second-level TLD domains, like `.co.uk`
*
* Ideally, we'd use https://github.com/jeremykendall/php-domain-parser to check for this, but it's too much work for such a basic functionality. Maybe if it's needed more in the future. So instead, we use [Basic matching regex](http://stackoverflow.com/a/12372310).
* @param string $domain Domain to check if it's a TLD or subdomain
* @return string Extracted domain if it has a subdomain
*/
function _gravityview_strip_subdomain( $string_maybe_has_subdomain ) {

if( preg_match("/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.(?:com\.|co\.|net\.|org\.|firm\.|me\.|school\.|law\.|gov\.|mod\.|msk\.|irkutsks\.|sa\.|act\.|police\.|plc\.|ac\.|tm\.|asso\.|biz\.|pro\.|cg\.|telememo\.)?[a-z\.]{2,6})$/i", $string_maybe_has_subdomain, $matches ) ) {
return $matches['domain'];
} else {
return $string_maybe_has_subdomain;
}
}


/**
* Convert a whole link into a shorter link for display
*
* @since 1.1
*
* @param string $value Existing URL
* @return string If parse_url doesn't find a 'host', returns original value. Otherwise, returns formatted link.
*/
function gravityview_format_link( $value = null ) {


$parts = parse_url( $value );

// No domain? Strange...show the original text.
if( empty( $parts['host'] ) ) {
return $value;
}

// Start with empty value for the return URL
$return = '';

/**
* @filter `gravityview_anchor_text_striphttp` Strip scheme from the displayed URL?
* @since 1.5.1
* @param boolean $enable Whether to strip the scheme. Return false to show scheme. (default: true)\n
* If true: `http://example.com => example.com`
*/
if( false === apply_filters('gravityview_anchor_text_striphttp', true) ) {

if( isset( $parts['scheme'] ) ) {
$return .= $parts['scheme'];
}

}

// The domain, which may contain a subdomain
$domain = $parts['host'];

/**
* @filter `gravityview_anchor_text_stripwww` Strip www from the domain?
* @since 1.5.1
* @param boolean $enable Whether to strip www. Return false to show www. (default: true)\n
* If true: `www.example.com => example.com`
*/
$strip_www = apply_filters('gravityview_anchor_text_stripwww', true );

if( $strip_www ) {
$domain = str_replace('www.', '', $domain );
}

/**
* @filter `gravityview_anchor_text_nosubdomain` Strip subdomains from the domain?
* @since 1.5.1
* @param boolean $enable Whether to strip subdomains. Return false to show subdomains. (default: true)\n
* If true: `http://demo.example.com => example.com` \n
* If false: `http://demo.example.com => demo.example.com`
*/
$strip_subdomains = apply_filters('gravityview_anchor_text_nosubdomain', true);

if( $strip_subdomains ) {

$domain = _gravityview_strip_subdomain( $parts['host'] );

}

// Add the domain
$return .= $domain;

/**
* @filter `gravityview_anchor_text_rootonly` Display link path going only to the base directory, not a sub-directory or file?
* @since 1.5.1
* @param boolean $enable Whether to enable "root only". Return false to show full path. (default: true)\n
* If true: `http://example.com/sub/directory/page.html => example.com` \n
* If false: `http://example.com/sub/directory/page.html => example.com/sub/directory/page.html`
*/
$root_only = apply_filters('gravityview_anchor_text_rootonly', true);

if( empty( $root_only ) ) {

if( isset( $parts['path'] ) ) {
$return .= $parts['path'];
}
}

/**
* @filter `gravityview_anchor_text_noquerystring` Strip the query string from the end of the URL?
* @since 1.5.1
* @param boolean $enable Whether to enable "root only". Return false to show full path. (default: true)\n
* If true: `http://example.com/?query=example => example.com`
*/
$strip_query_string = apply_filters('gravityview_anchor_text_noquerystring', true );

if( empty( $strip_query_string ) ) {

if( isset( $parts['query'] ) ) {
$return .= '?'.$parts['query'];
}

}

return $return;
}

/**
* Get all views processed so far for the current page load
Expand Down
228 changes: 228 additions & 0 deletions includes/class-gravityview-roles-capabilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
<?php
/**
* Roles and Capabilities
*
* @package GravityView
* @license GPL2+
* @since 1.14
* @author Katz Web Services, Inc.
* @link http://gravityview.co
* @copyright Copyright 2015, Katz Web Services, Inc.
*/

// Exit if accessed directly
defined( 'ABSPATH' ) || exit;

/**
* GravityView Roles Class
*
* This class handles the role creation and assignment of capabilities for those roles.
*
* @since 1.14
*/
class GravityView_Roles_Capabilities {

/**
* @var GravityView_Roles_Capabilities|null
*/
static $instance = null;

/**
* @since 1.14
* @return GravityView_Roles_Capabilities
*/
public static function get_instance() {

if( ! self::$instance ) {
self::$instance = new self;
}

return self::$instance;
}

/**
* Get things going
*
* @since 1.14
*/
public function __construct() {
$this->add_hooks();
}

/**
* Add Members plugin hook
* @since 1.14
*/
private function add_hooks() {
add_filter( 'members_get_capabilities', array( $this, 'members_get_capabilities' ) );
}

/**
* Add GravityView capabilities to the Members plugin
*
* @since 1.14
* @param array $caps Existing capabilities registered with Members
* @return array Modified capabilities array
*/
public function members_get_capabilities( $caps ) {
return array_merge( $caps, $this->all_caps('all') );
}

/**
* Retrieves the global WP_Roles instance and instantiates it if necessary.
*
* @see wp_roles() This method uses the exact same code as wp_roles(), here for backward compatibility
*
* @global WP_Roles $wp_roles WP_Roles global instance.
*
* @return WP_Roles WP_Roles global instance if not already instantiated.
*/
function wp_roles() {
global $wp_roles;

if ( ! isset( $wp_roles ) ) {
$wp_roles = new WP_Roles();
}
return $wp_roles;
}

/**
* Add capabilities to their respective roles
*
* @since 1.14
* @return void
*/
public function add_caps() {

$wp_roles = $this->wp_roles();

if ( is_object( $wp_roles ) ) {

foreach( $wp_roles->get_names() as $role_slug => $role_label ) {

$capabilities = $this->all_caps( $role_slug );

foreach( $capabilities as $cap ) {
$wp_roles->add_cap( $role_slug, $cap );
}
}
}
}

/**
* Get an array of GravityView capabilities
*
* @see get_post_type_capabilities()
*
* @since 1.14
*
* @param string $role If set, get the caps for a specific role. Pass 'all' to get all caps in a flat array. Default: ''
*
* @return array If $role is set, flat array of caps. Otherwise, a multi-dimensional array of roles and their caps with the following keys: 'administrator', 'editor', 'author', 'contributor', 'subscriber'
*/
public function all_caps( $role = '' ) {

$administrator = array(
// Settings
'gravityview_view_settings',
'gravityview_edit_settings',
);

// Edit, publish, delete own and others' stuff
$editor = array(
'edit_others_gravityviews',
'read_private_gravityviews',
'delete_private_gravityviews',
'delete_others_gravityviews',
'edit_private_gravityviews',
'publish_gravityviews',
'delete_published_gravityviews',
'edit_published_gravityviews',

// GF caps
'gravityview_edit_others_entries',

// GF caps
'gravityview_view_others_entry_notes',
'gravityview_moderate_entries',
'gravityview_delete_others_entries',
);

// Edit, publish and delete own stuff
$author = array(

// GF caps
'gravityview_edit_entries',
'gravityview_view_entry_notes',
'gravityview_delete_entries',

);

// Edit and delete drafts but not publish
$contributor = array(
'edit_gravityview',
'edit_gravityviews',
'delete_gravityview',
'delete_gravityviews',
);

// Read only
$subscriber = array(
'read_gravityview',
);

$capabilities = array();

switch( $role ) {
case 'subscriber':
$capabilities = $subscriber;
break;
case 'contributor':
$capabilities = array_merge( $contributor, $subscriber );
break;
case 'author':
$capabilities = array_merge( $author, $contributor, $subscriber );
break;
case 'editor':
$capabilities = array_merge( $editor, $author, $contributor, $subscriber );
break;
case 'administrator':
case 'all':
$capabilities = array_merge( $administrator, $editor, $author, $contributor, $subscriber );
break;
}

// If role is set, return empty array if not exists
if( $role ) {
return isset( $capabilities[ $role ] ) ? $capabilities[ $role ] : array();
}

// By default, return multi-dimensional array of all caps
return compact( 'administrator', 'editor', 'author', 'contributor', 'subscriber' );
}


/**
* Remove all GravityView caps from all roles
*
* @since 1.14
* @return void
*/
public function remove_caps() {

$wp_roles = $this->wp_roles();

if ( is_object( $wp_roles ) ) {

/** Remove all GravityView caps from all roles */
$capabilities = $this->all_caps('all');

// Loop through each role and remove GV caps
foreach( $wp_roles->get_names() as $role_slug => $role_name ) {
foreach ( $capabilities as $cap ) {
$wp_roles->remove_cap( $role_slug, $cap );
}
}
}
}
}
1 change: 1 addition & 0 deletions includes/class-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public static function init_post_types() {
'slug' => apply_filters( 'gravityview_slug', 'view' )
),
'capability_type' => 'page',
'map_meta_cap' => true,
);

register_post_type( 'gravityview', $args );
Expand Down
Loading