Skip to content

Commit

Permalink
Merge pull request humanmade#6 from humanmade/roles
Browse files Browse the repository at this point in the history
do roles
  • Loading branch information
mattheu committed Jul 9, 2015
2 parents 3dae158 + cad9c4a commit 3b953e8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ Further (optional) settings
// Number of results to fetch for each page. Default is 20.
define( 'PJ_RESOURCE_SPACE_RESULTS_PER_PAGE', 20 );
````

**Grant access by user role**

The plugin uses the `insert_from_resourcespace` capability. This is granted to administrators and editors by default.
2 changes: 1 addition & 1 deletion inc/class-resource-space-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function admin_enqueue_scripts() {

$deps = array( 'jquery', 'backbone', 'media' );
wp_enqueue_script( 'resource-space-admin', PJ_RESOURCE_SPACE_PLUGIN_URL . '/js/admin.js', $deps, PJ_RESOURCESPACE_PLUGIN_VERSION, true );

}

}
Expand Down
34 changes: 33 additions & 1 deletion resourcespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@
Resource_Space_Admin::get_instance();

add_filter( 'mexp_services', function( array $services ) {
$services['resource-space'] = new MEXP_Resource_Space_Service;

if ( current_user_can( 'insert_from_resourcespace' ) ) {
$services['resource-space'] = new MEXP_Resource_Space_Service;
}

return $services;

} );

/**
Expand All @@ -43,6 +48,10 @@ function resource_space_vc_script() {
return;
}

if ( ! current_user_can( 'insert_from_resourcespace' ) ) {
return;
}

wp_enqueue_script( 'resource-space-vc', plugins_url( 'js/resource-space-vc.js', __FILE__ ), array( 'wpb_jscomposer_media_editor_js' ), null, true );

wp_localize_script( 'resource-space-vc', 'resourceSpaceVC', array(
Expand All @@ -54,3 +63,26 @@ function resource_space_vc_script() {

add_action( 'admin_print_scripts-post.php', 'resource_space_vc_script' );
add_action( 'admin_print_scripts-post-new.php', 'resource_space_vc_script' );

/**
* Add the resourcespace MEXP capability for required roles.
* Note - version check to ensure this isn't fired always as it writes to the database.d
*/
add_action( 'admin_init', function() {

if ( version_compare( PJ_RESOURCESPACE_PLUGIN_VERSION, get_option( 'pj_resourcespace_version'), '<=' ) ) {
return;
}

update_option( 'pj_resourcespace_version', PJ_RESOURCESPACE_PLUGIN_VERSION );

$roles = get_editable_roles();
$default_roles = array( 'editor', 'administrator' );

foreach ( $GLOBALS['wp_roles']->role_objects as $key => $role ) {
if ( in_array( $key, $default_roles ) && isset( $roles[ $key ] ) ) {
$role->add_cap( 'insert_from_resourcespace' );
}
}

} );

0 comments on commit 3b953e8

Please sign in to comment.