Ejemplo n.º 1
0
 /**
  * Adds or removes users to/from groups.
  */
 public static function load_users()
 {
     if (current_user_can(GROUPS_ADMINISTER_GROUPS)) {
         $users = isset($_REQUEST['users']) ? $_REQUEST['users'] : null;
         $action = null;
         if (!empty($_REQUEST['groups'])) {
             if ($_GET['groups-action'] == "add-group") {
                 $action = 'add';
             } else {
                 if ($_GET['groups-action'] == "remove-group") {
                     $action = 'remove';
                 }
             }
         }
         if ($users !== null && $action !== null) {
             if (wp_verify_nonce($_REQUEST['bulk-user-group-nonce'], 'user-group')) {
                 foreach ($users as $user_id) {
                     switch ($action) {
                         case 'add':
                             $group_ids = isset($_GET['group_ids']) ? $_GET['group_ids'] : null;
                             if ($group_ids !== null) {
                                 foreach ($group_ids as $group_id) {
                                     if (!Groups_User_Group::read($user_id, $group_id)) {
                                         Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $group_id));
                                     }
                                 }
                             }
                             break;
                         case 'remove':
                             $group_ids = isset($_GET['group_ids']) ? $_GET['group_ids'] : null;
                             if ($group_ids !== null) {
                                 foreach ($group_ids as $group_id) {
                                     if (Groups_User_Group::read($user_id, $group_id)) {
                                         Groups_User_Group::delete($user_id, $group_id);
                                     }
                                 }
                             }
                             break;
                     }
                 }
                 $referer = wp_get_referer();
                 if ($referer) {
                     $redirect_to = remove_query_arg(array('action', 'action2', 'add-to-group', 'bulk-user-group-nonce', 'group_id', 'new_role', 'remove-from-group', 'users'), $referer);
                     wp_redirect($redirect_to);
                     exit;
                 }
             }
         }
     }
 }
 /**
  * Adds or removes users to/from groups.
  */
 public static function load_users()
 {
     if (current_user_can(GROUPS_ADMINISTER_GROUPS)) {
         $group_id = isset($_REQUEST['group_id']) ? $_REQUEST['group_id'] : null;
         $users = isset($_REQUEST['users']) ? $_REQUEST['users'] : null;
         $action = null;
         if (!empty($_REQUEST['add-to-group'])) {
             $action = 'add';
         } else {
             if (!empty($_REQUEST['remove-from-group'])) {
                 $action = 'remove';
             }
         }
         if ($group_id !== null && $users !== null && $action !== null) {
             if (wp_verify_nonce($_REQUEST['bulk-user-group-nonce'], 'user-group')) {
                 foreach ($users as $user_id) {
                     switch ($action) {
                         case 'add':
                             if (!Groups_User_Group::read($user_id, $group_id)) {
                                 Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $group_id));
                             }
                             break;
                         case 'remove':
                             if (Groups_User_Group::read($user_id, $group_id)) {
                                 Groups_User_Group::delete($user_id, $group_id);
                             }
                             break;
                     }
                 }
             }
         }
     }
 }
 /**
  * Updates the group membership.
  * @param int $user_id
  */
 public static function edit_user_profile_update($user_id)
 {
     global $wpdb;
     if (current_user_can(GROUPS_ADMINISTER_GROUPS)) {
         $groups_table = _groups_get_tablename('group');
         if ($groups = $wpdb->get_results("SELECT * FROM {$groups_table}")) {
             $user_group_ids = isset($_POST['group_ids']) && is_array($_POST['group_ids']) ? $_POST['group_ids'] : array();
             foreach ($groups as $group) {
                 if (in_array($group->group_id, $user_group_ids)) {
                     if (!Groups_User_Group::read($user_id, $group->group_id)) {
                         Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $group->group_id));
                     }
                 } else {
                     if (Groups_User_Group::read($user_id, $group->group_id)) {
                         Groups_User_Group::delete($user_id, $group->group_id);
                     }
                 }
             }
         }
     }
 }
 /**
  * Renders a form that lets a user join a group.
  * * Attributes:
  * - "group" : (required) group name or id
  * 
  * @param array $atts attributes
  * @param string $content not used
  */
 public static function groups_join($atts, $content = null)
 {
     $nonce_action = 'groups_action';
     $nonce = 'nonce_join';
     $output = "";
     $options = shortcode_atts(array('group' => '', 'display_message' => true, 'display_is_member' => false, 'submit_text' => __('Join the %s group', GROUPS_PLUGIN_DOMAIN)), $atts);
     extract($options);
     if ($display_message === 'false') {
         $display_message = false;
     }
     if ($display_is_member === 'true') {
         $display_is_member = true;
     }
     $group = trim($options['group']);
     $current_group = Groups_Group::read($group);
     if (!$current_group) {
         $current_group = Groups_Group::read_by_name($group);
     }
     if ($current_group) {
         if ($user_id = get_current_user_id()) {
             $submitted = false;
             $invalid_nonce = false;
             if (!empty($_POST['groups_action']) && $_POST['groups_action'] == 'join') {
                 $submitted = true;
                 if (!wp_verify_nonce($_POST[$nonce], $nonce_action)) {
                     $invalid_nonce = true;
                 }
             }
             if ($submitted && !$invalid_nonce) {
                 // add user to group
                 if (isset($_POST['group_id'])) {
                     $join_group = Groups_Group::read($_POST['group_id']);
                     Groups_User_Group::create(array('group_id' => $join_group->group_id, 'user_id' => $user_id));
                 }
             }
             if (!Groups_User_Group::read($user_id, $current_group->group_id)) {
                 $submit_text = sprintf($options['submit_text'], wp_filter_nohtml_kses($current_group->name));
                 $output .= '<div class="groups-join">';
                 $output .= '<form action="#" method="post">';
                 $output .= '<input type="hidden" name="groups_action" value="join" />';
                 $output .= '<input type="hidden" name="group_id" value="' . esc_attr($current_group->group_id) . '" />';
                 $output .= '<input type="submit" value="' . $submit_text . '" />';
                 $output .= wp_nonce_field($nonce_action, $nonce, true, false);
                 $output .= '</form>';
                 $output .= '</div>';
             } else {
                 if ($display_message) {
                     if ($submitted && !$invalid_nonce && isset($join_group) && $join_group->group_id === $current_group->group_id) {
                         $output .= '<div class="groups-join joined">';
                         $output .= sprintf(__('You have joined the %s group.', GROUPS_PLUGIN_DOMAIN), wp_filter_nohtml_kses($join_group->name));
                         $output .= '</div>';
                     } else {
                         if ($display_is_member && isset($current_group) && $current_group !== false) {
                             $output .= '<div class="groups-join member">';
                             $output .= sprintf(__('You are a member of the %s group.', GROUPS_PLUGIN_DOMAIN), wp_filter_nohtml_kses($current_group->name));
                             $output .= '</div>';
                         }
                     }
                 }
             }
         }
     }
     return $output;
 }
 /**
  * Save capability options.
  * 
  * @param int $post_id
  * @param mixed $post post data (not used here)
  */
 public static function save_post($post_id = null, $post = null)
 {
     if (defined("DOING_AUTOSAVE") && DOING_AUTOSAVE) {
     } else {
         $post_type = get_post_type($post_id);
         $post_type_object = get_post_type_object($post_type);
         if ($post_type_object && $post_type != 'attachment') {
             $post_types_option = Groups_Options::get_option(Groups_Post_Access::POST_TYPES, array());
             if (!isset($post_types_option[$post_type]['add_meta_box']) || $post_types_option[$post_type]['add_meta_box']) {
                 if (isset($_POST[self::NONCE]) && wp_verify_nonce($_POST[self::NONCE], self::SET_CAPABILITY)) {
                     $post_type = isset($_POST["post_type"]) ? $_POST["post_type"] : null;
                     if ($post_type !== null) {
                         // See http://codex.wordpress.org/Function_Reference/current_user_can 20130119 WP 3.5
                         // "... Some capability checks (like 'edit_post' or 'delete_page') require this [the post ID] be provided."
                         // If the post ID is not provided, it will throw:
                         // PHP Notice:  Undefined offset: 0 in /var/www/groups-forums/wp-includes/capabilities.php on line 1067
                         $edit_post_type = 'edit_' . $post_type;
                         if ($post_type_object = get_post_type_object($post_type)) {
                             if (!isset($post_type_object->capabilities)) {
                                 // get_post_type_capabilities() (WP 3.8) will throw a warning
                                 // when trying to merge the missing property otherwise. It's either a
                                 // bug or the function's documentation should make it clear that you
                                 // have to provide that.
                                 $post_type_object->capabilities = array();
                             }
                             $caps_object = get_post_type_capabilities($post_type_object);
                             if (isset($caps_object->edit_post)) {
                                 $edit_post_type = $caps_object->edit_post;
                             }
                         }
                         if (current_user_can($edit_post_type, $post_id)) {
                             // quick-create ?
                             if (current_user_can(GROUPS_ADMINISTER_GROUPS)) {
                                 if (!empty($_POST['quick-group-capability'])) {
                                     $creator_id = get_current_user_id();
                                     $datetime = date('Y-m-d H:i:s', time());
                                     $name = ucfirst(strtolower(trim($_POST['quick-group-capability'])));
                                     if (strlen($name) > 0) {
                                         // create or obtain the group
                                         if ($group = Groups_Group::read_by_name($name)) {
                                         } else {
                                             if ($group_id = Groups_Group::create(compact('creator_id', 'datetime', 'name'))) {
                                                 $group = Groups_Group::read($group_id);
                                             }
                                         }
                                         // create or obtain the capability
                                         $name = strtolower($name);
                                         if ($capability = Groups_Capability::read_by_capability($name)) {
                                         } else {
                                             if ($capability_id = Groups_Capability::create(array('capability' => $name))) {
                                                 $capability = Groups_Capability::read($capability_id);
                                             }
                                         }
                                         if ($group && $capability) {
                                             // add the capability to the group
                                             if (!Groups_Group_Capability::read($group->group_id, $capability->capability_id)) {
                                                 Groups_Group_Capability::create(array('group_id' => $group->group_id, 'capability_id' => $capability->capability_id));
                                             }
                                             // enable the capability for access restriction
                                             $valid_read_caps = Groups_Options::get_option(Groups_Post_Access::READ_POST_CAPABILITIES, array(Groups_Post_Access::READ_POST_CAPABILITY));
                                             if (!in_array($capability->capability, $valid_read_caps)) {
                                                 $valid_read_caps[] = $capability->capability;
                                             }
                                             Groups_Options::update_option(Groups_Post_Access::READ_POST_CAPABILITIES, $valid_read_caps);
                                             // add the current user to the group
                                             Groups_User_Group::create(array('user_id' => get_current_user_id(), 'group_id' => $group->group_id));
                                             // put the capability ID in $_POST[self::CAPABILITY] so it is treated below
                                             if (empty($_POST[self::CAPABILITY])) {
                                                 $_POST[self::CAPABILITY] = array();
                                             }
                                             if (!in_array($capability->capability_id, $_POST[self::CAPABILITY])) {
                                                 $_POST[self::CAPABILITY][] = $capability->capability_id;
                                             }
                                         }
                                     }
                                 }
                             }
                             // set
                             if (self::user_can_restrict()) {
                                 $valid_read_caps = self::get_valid_read_caps_for_user();
                                 foreach ($valid_read_caps as $valid_read_cap) {
                                     if ($capability = Groups_Capability::read_by_capability($valid_read_cap)) {
                                         if (!empty($_POST[self::CAPABILITY]) && is_array($_POST[self::CAPABILITY]) && in_array($capability->capability_id, $_POST[self::CAPABILITY])) {
                                             Groups_Post_Access::create(array('post_id' => $post_id, 'capability' => $capability->capability));
                                         } else {
                                             Groups_Post_Access::delete($post_id, $capability->capability);
                                         }
                                     }
                                 }
                             }
                             // show groups
                             Groups_Options::update_user_option(self::SHOW_GROUPS, !empty($_POST[self::SHOW_GROUPS]));
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * Assign a user to its "Registered" group for the given blog.
  * 
  * @param int $user_id
  * @param WP_string $role
  */
 function add_user_to_blog($user_id, $role, $blog_id)
 {
     if (is_multisite()) {
         Groups_Controller::switch_to_blog($blog_id);
     }
     global $wpdb;
     // Check if the group table exists, if it does not exist, we are
     // probably here because the action has been triggered in the middle
     // of wpmu_create_blog() before the wpmu_new_blog action has been
     // triggered. In that case, just skip this as the user will be added
     // later when wpmu_new_blog is triggered, the activation sequence has
     // created the tables and all users of the new blog are added to
     // that blog's "Registered" group.
     $group_table = _groups_get_tablename('group');
     if ($wpdb->get_var("SHOW TABLES LIKE '" . $group_table . "'") == $group_table) {
         $registered_group = Groups_Group::read_by_name(self::REGISTERED_GROUP_NAME);
         if (!$registered_group) {
             $registered_group_id = Groups_Group::create(array("name" => self::REGISTERED_GROUP_NAME));
         } else {
             $registered_group_id = $registered_group->group_id;
         }
         if ($registered_group_id) {
             Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $registered_group_id));
         }
     }
     if (is_multisite()) {
         Groups_Controller::restore_current_blog();
     }
 }
 /**
  * Handle group assignment : assign the user to the groups related to the subscription's product.
  * @param int $user_id
  * @param string $subscription_key
  */
 public static function activated_subscription($user_id, $subscription_key)
 {
     $subscription = WC_Subscriptions_Manager::get_users_subscription($user_id, $subscription_key);
     if (isset($subscription['product_id']) && isset($subscription['order_id'])) {
         $product_id = $subscription['product_id'];
         $order_id = $subscription['order_id'];
         // Leasving this here for reference, it can be assumed that normally,
         // if the product's groups are modified, a reactivation should take its
         // data from the current product, not from its previous state.
         // See if the subscription was activated before and try to get subscription's groups.
         // If there are any, use these instead of those from the product.
         // This is necessary when a subscription has been cancelled and re-activated and the
         // original product groups were modified since and we do NOT want to make group
         // assignments based on the current state of the product.
         $done = false;
         //$groups_product_groups = get_user_meta( $user_id, '_groups_product_groups', true );
         //if ( isset( $groups_product_groups[$order_id] ) && isset( $groups_product_groups[$order_id][$product_id] ) &&
         //	 isset( $groups_product_groups[$order_id][$product_id]['groups'] ) &&
         //	 isset( $groups_product_groups[$order_id][$product_id]['subscription_key'] ) &&
         //	( $groups_product_groups[$order_id][$product_id]['subscription_key'] === $subscription_key )
         //) {
         //	foreach( $groups_product_groups[$order_id][$product_id]['groups'] as $group_id ) {
         //		Groups_User_Group::create( $user_id, $group_id );
         //	}
         //	$done = true;
         //}
         // maybe unschedule pending expiration
         wp_clear_scheduled_hook('groups_ws_subscription_expired', array('user_id' => $user_id, 'subscription_key' => $subscription_key));
         if (!$done) {
             // get the product from the subscription
             $product = new WC_Product($product_id);
             if ($product->exists()) {
                 // get the groups related to the product
                 if ($product_groups = get_post_meta($product_id, '_groups_groups', false)) {
                     if (count($product_groups) > 0) {
                         // add the groups to the subscription (in case the product is changed later on, the subscription is still valid)
                         $groups_product_groups = get_user_meta($user_id, '_groups_product_groups', true);
                         if (empty($groups_product_groups)) {
                             $groups_product_groups = array();
                         }
                         $groups_product_groups[$order_id][$product_id]['groups'] = $product_groups;
                         $groups_product_groups[$order_id][$product_id]['subscription_key'] = $subscription_key;
                         update_user_meta($user_id, '_groups_product_groups', $groups_product_groups);
                         // add the user to the groups
                         foreach ($product_groups as $group_id) {
                             $result = Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $group_id));
                         }
                         Groups_WS_Terminator::mark_as_eternal($user_id, $group_id);
                     }
                 }
                 // remove from groups
                 if ($product_groups_remove = get_post_meta($product_id, '_groups_groups_remove', false)) {
                     if (count($product_groups_remove) > 0) {
                         $groups_product_groups_remove = get_user_meta($user_id, '_groups_product_groups_remove', true);
                         if (empty($groups_product_groups_remove)) {
                             $groups_product_groups_remove = array();
                         }
                         $groups_product_groups_remove[$order_id][$product_id]['groups'] = $product_groups_remove;
                         update_user_meta($user_id, '_groups_product_groups_remove', $groups_product_groups_remove);
                         // remove the user from the groups
                         foreach ($product_groups_remove as $group_id) {
                             $result = Groups_User_Group::delete($user_id, $group_id);
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * Add to groups after a subscription on hold has been reactivated.
  * 
  * This must NOT replicate the full action taken when a subscription is
  * activated initially but reinstate group membership that was previously
  * revoked.
  * 
  * @param int $user_id
  * @param string $subscription_key
  */
 public static function reactivated_subscription($user_id, $subscription_key)
 {
     $subscription = self::get_subscription_by_subscription_key($subscription_key);
     if (isset($subscription['product_id']) && isset($subscription['order_id'])) {
         $product_id = $subscription['product_id'];
         $order_id = $subscription['order_id'];
         $groups_product_groups = get_user_meta($user_id, '_groups_product_groups', true);
         if (isset($groups_product_groups[$order_id]) && isset($groups_product_groups[$order_id][$product_id]) && isset($groups_product_groups[$order_id][$product_id]['groups'])) {
             foreach ($groups_product_groups[$order_id][$product_id]['groups'] as $group_id) {
                 Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $group_id));
             }
         }
     }
 }
 public static function create_registration_group($group_name, $users)
 {
     // Check if Groups plugin is active
     if (is_plugin_active('groups/groups.php')) {
         Groups_Group::create(array('name' => $group_name));
         if ($group = Groups_Group::read_by_name($group_name)) {
             $group_id = $group->group_id;
         }
         if (!empty($group_id)) {
             foreach ($users as $user_id) {
                 Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $group_id));
             }
         }
     }
 }