Permalink
Browse files

Add roles capabilitiy to allow moving tree for admins and editors onl…

…y (by default)
  • Loading branch information...
1 parent 0b7e49f commit db33756539b8099b9679166f9605b9b087fe87e2 MD committed Sep 30, 2013
Showing with 61 additions and 1 deletion.
  1. +59 −1 functions.php
  2. +2 −0 index.php
View
@@ -1583,7 +1583,11 @@ function cms_tpv_move_page() {
*/
global $wpdb;
-
+
+ if ( !current_user_can( CMS_TPV_MOVE_PERMISSION ) )
+ die("Error: you dont have permission");
+
+
$node_id = $_POST["node_id"]; // the node that was moved
$ref_node_id = $_POST["ref_node_id"];
$type = $_POST["type"];
@@ -1752,6 +1756,60 @@ function cms_tpv_install() {
// set to current version
update_option('cms_tpv_version', CMS_TPV_VERSION);
+
+ // Add necessary capabilities to allow moving tree of cms_tpv
+ $roles = array(
+ 'administrator' => array(CMS_TPV_MOVE_PERMISSION),
+ 'editor' => array(CMS_TPV_MOVE_PERMISSION),
+// 'author' => array(CMS_TPV_MOVE_PERMISSION),
+// 'contributor' => array(CMS_TPV_MOVE_PERMISSION)
+ );
+
+ foreach ( $roles as $role => $caps ) {
+ add_caps_to_role( $role, $caps );
+ }
+}
+
+function cms_tpv_remove() {
+ // Remove capabilities to disallow moving tree of cms_tpv
+ $roles = array(
+ 'administrator' => array(CMS_TPV_MOVE_PERMISSION),
+ 'editor' => array(CMS_TPV_MOVE_PERMISSION),
+// 'author' => array(CMS_TPV_MOVE_PERMISSION),
+// 'contributor' => array(CMS_TPV_MOVE_PERMISSION)
+ );
+
+ foreach ( $roles as $role => $caps ) {
+ remove_caps_from_role( $role, $caps );
+ }
+}
+
+/**
+* Adds an array of capabilities to a role.
+*/
+function add_caps_to_role( $role, $caps ) {
+
+ global $wp_roles;
+
+ if ( $wp_roles->is_role( $role ) ) {
+ $role =& get_role( $role );
+ foreach ( $caps as $cap )
+ $role->add_cap( $cap );
+ }
+}
+
+/**
+* Remove an array of capabilities from role.
+*/
+function remove_caps_from_role( $role, $caps ) {
+
+ global $wp_roles;
+
+ if ( $wp_roles->is_role( $role ) ) {
+ $role =& get_role( $role );
+ foreach ( $caps as $cap )
+ $role->remove_cap( $cap );
+ }
}
// cms_tpv_install();
View
@@ -68,7 +68,9 @@
add_action('wp_ajax_cms_tpv_add_pages', 'cms_tpv_add_pages');
// activation
+define( "CMS_TPV_MOVE_PERMISSION", "move_cms_tree_view_page");
register_activation_hook( WP_PLUGIN_DIR . "/cms-tree-page-view/index.php" , 'cms_tpv_install' );
+register_deactivation_hook( WP_PLUGIN_DIR . "/cms-tree-page-view/index.php" , 'cms_tpv_remove' );
// To test activation hook, uncomment function below
// cms_tpv_install();

0 comments on commit db33756

Please sign in to comment.