Пример #1
0
 /**
  * Persist a group.
  * 
  * Parameters:
  * - name (required) - the group's name
  * - creator_id (optional) - defaults to the current user's id
  * - datetime (optional) - defaults to now
  * - description (optional)
  * - parent_id (optional)
  * 
  * @param array $map attributes
  * @return group_id on success, otherwise false
  */
 public static function create($map)
 {
     global $wpdb;
     extract($map);
     $result = false;
     $error = false;
     if (!empty($name)) {
         $group_table = _groups_get_tablename("group");
         $data = array('name' => $name);
         $formats = array('%s');
         if (!isset($creator_id)) {
             $creator_id = get_current_user_id();
         }
         if (isset($creator_id)) {
             $data['creator_id'] = Groups_Utility::id($creator_id);
             $formats[] = '%d';
         }
         if (!isset($datetime)) {
             $datetime = date('Y-m-d H:i:s', time());
         }
         if (isset($datetime)) {
             $data['datetime'] = $datetime;
             $formats[] = '%s';
         }
         if (!empty($description)) {
             $data['description'] = $description;
             $formats[] = '%s';
         }
         if (!empty($parent_id)) {
             // only allow to set an existing parent group (that is from the same blog)
             $parent_group_id = $wpdb->get_var($wpdb->prepare("SELECT group_id FROM {$group_table} WHERE group_id = %d", Groups_Utility::id($parent_id)));
             if ($parent_group_id === $parent_id) {
                 $data['parent_id'] = Groups_Utility::id($parent_id);
                 $formats[] = '%d';
             } else {
                 $error = true;
             }
         }
         // no duplicate names
         $duplicate = Groups_Group::read_by_name($name);
         if ($duplicate) {
             $error = true;
         }
         if (!$error) {
             if ($wpdb->insert($group_table, $data, $formats)) {
                 if ($result = $wpdb->get_var("SELECT LAST_INSERT_ID()")) {
                     do_action("groups_created_group", $result);
                 }
             }
         }
     }
     return $result;
 }
/**
 * Handle add group form submission.
 * @return int new group's id or false if unsuccessful
 */
function groups_admin_groups_add_submit()
{
    global $wpdb;
    if (!current_user_can(GROUPS_ADMINISTER_GROUPS)) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    if (!wp_verify_nonce($_POST[GROUPS_ADMIN_GROUPS_NONCE], 'groups-add')) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    $creator_id = get_current_user_id();
    $datetime = date('Y-m-d H:i:s', time());
    $parent_id = isset($_POST['parent-id-field']) ? $_POST['parent-id-field'] : null;
    $description = isset($_POST['description-field']) ? $_POST['description-field'] : '';
    $name = isset($_POST['name-field']) ? $_POST['name-field'] : null;
    $group_id = Groups_Group::create(compact("creator_id", "datetime", "parent_id", "description", "name"));
    if ($group_id) {
        if (!empty($_POST['capability_ids'])) {
            $caps = $_POST['capability_ids'];
            foreach ($caps as $cap) {
                Groups_Group_Capability::create(array('group_id' => $group_id, 'capability_id' => $cap));
            }
        }
        do_action('groups_admin_groups_add_submit_success', $group_id);
    } else {
        if (!$name) {
            Groups_Admin::add_message(__('The name must not be empty.', GROUPS_PLUGIN_DOMAIN), 'error');
        } else {
            if (Groups_Group::read_by_name($name)) {
                Groups_Admin::add_message(sprintf(__('The <em>%s</em> group already exists.', GROUPS_PLUGIN_DOMAIN), stripslashes(wp_filter_nohtml_kses($name))), 'error');
            }
        }
    }
    return $group_id;
}
function gpbbp_new_post_notification($post_id, $post, $post_type)
{
    $post_is_reply = $post_type == bbp_get_reply_post_type() ? true : false;
    $post_topic = $post_is_reply ? get_post(bbp_get_topic_id())->post_title : $post->post_title;
    $post_author = get_user_by('id', $post->post_author);
    $post_forum_title = bbp_get_forum_title($forum_id);
    $post_info = array('topic' => htmlspecialchars_decode($post_topic, ENT_QUOTES), 'topic_id' => bbp_get_topic_id(), 'category' => $post_forum_title, 'category_id' => $forum_id, 'category_slug' => str_replace(' ', '', $post_forum_title), 'is_reply' => $post_is_reply, 'author' => "{$post_author->first_name} {$post_author->last_name}", 'author_brand' => $post_author->brand, 'author_username' => $post_author->display_name, 'body' => $post->post_content, 'permalink' => $post_is_reply ? get_permalink(bbp_get_topic_id()) . "#post-{$post_id}" : get_permalink($post_id), 'user_slug' => home_url() . '/directory/user/' . $post->post_author);
    $group = Groups_Group::read_by_name($post_forum_title);
    $group = new Groups_Group($group->group_id);
    $mandrill_endpoint = 'https://mandrillapp.com/api/1.0/messages/send-template.json';
    $mandrill_key = 'MANDRILL KEY';
    $mandrill_template = 'new-post-notification-backup-mc-version-1';
    $mandrill_merge_vars = array();
    $mandrill_recipients[] = array();
    foreach ($group->users as $group_member) {
        if ($group_member->user->ID != $post->post_author) {
            $mandrill_recipients[] = array('email' => $group_member->user->user_email, 'name' => $group_member->user->display_name);
        }
    }
    // Set up merge vars
    foreach ($post_info as $key => $value) {
        $mandrill_merge_vars[] = array('name' => $key, 'content' => $value);
    }
    // Prepare request
    $mandrill_request = array('key' => $mandrill_key, 'template_name' => $mandrill_template, 'template_content' => array(), 'message' => array('to' => $mandrill_recipients, 'global_merge_vars' => $mandrill_merge_vars, 'merge' => true, 'merge_language' => 'handlebars'));
    // Send request
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $mandrill_endpoint);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($mandrill_request));
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mandrill-Curl/1.0');
    $result = curl_exec($ch);
    curl_close($ch);
}
/**
 * Handle edit form submission.
 */
function groups_admin_groups_edit_submit()
{
    global $wpdb;
    if (!current_user_can(GROUPS_ADMINISTER_GROUPS)) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    if (!wp_verify_nonce($_POST[GROUPS_ADMIN_GROUPS_NONCE], 'groups-edit')) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    $group_id = isset($_POST['group-id-field']) ? $_POST['group-id-field'] : null;
    $group = Groups_Group::read($group_id);
    if ($group) {
        $group_id = $group->group_id;
        if ($group->name !== Groups_Registered::REGISTERED_GROUP_NAME) {
            $name = isset($_POST['name-field']) ? $_POST['name-field'] : null;
        } else {
            $name = Groups_Registered::REGISTERED_GROUP_NAME;
        }
        $parent_id = isset($_POST['parent-id-field']) ? $_POST['parent-id-field'] : null;
        $description = isset($_POST['description-field']) ? $_POST['description-field'] : '';
        if (empty($name)) {
            Groups_Admin::add_message(__('The <em>Name</em> must not be empty.', GROUPS_PLUGIN_DOMAIN), 'error');
            return false;
        }
        if ($other_group = Groups_Group::read_by_name($name)) {
            if ($other_group->group_id != $group_id) {
                Groups_Admin::add_message(sprintf(__('The <em>%s</em> group already exists and cannot be used to name this one.', GROUPS_PLUGIN_DOMAIN), stripslashes(wp_filter_nohtml_kses($other_group->name))), 'error');
                return false;
            }
        }
        $group_id = Groups_Group::update(compact("group_id", "name", "parent_id", "description"));
        if ($group_id) {
            $capability_table = _groups_get_tablename("capability");
            $group_capability_table = _groups_get_tablename("group_capability");
            $group_capabilities = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$capability_table} WHERE capability_id IN ( SELECT capability_id FROM {$group_capability_table} WHERE group_id = %d )", Groups_Utility::id($group_id)));
            $group_capabilities_array = array();
            foreach ($group_capabilities as $group_capability) {
                $group_capabilities_array[] = $group_capability->capability_id;
            }
            $caps = array();
            if (isset($_POST['capability_ids'])) {
                $caps = $_POST['capability_ids'];
            }
            // delete
            foreach ($group_capabilities_array as $group_cap) {
                if (!in_array($group_cap, $caps)) {
                    Groups_Group_Capability::delete($group_id, $group_cap);
                }
            }
            // add
            foreach ($caps as $cap) {
                if (!in_array($cap, $group_capabilities_array)) {
                    Groups_Group_Capability::create(array('group_id' => $group_id, 'capability_id' => $cap));
                }
            }
        }
        return $group_id;
    } else {
        return false;
    }
}
 /**
  * Renders a form that lets a user leave a group.
  * * Attributes:
  * - "group" : (required) group name or id
  *
  * @param array $atts attributes
  * @param string $content not used
  */
 public static function groups_leave($atts, $content = null)
 {
     $nonce_action = 'groups_action';
     $nonce = 'nonce_leave';
     $output = "";
     $options = shortcode_atts(array('group' => '', 'display_message' => true, 'submit_text' => __('Leave the %s group', GROUPS_PLUGIN_DOMAIN)), $atts);
     extract($options);
     if ($display_message === 'false') {
         $display_message = false;
     }
     $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'] == 'leave') {
                 $submitted = true;
                 if (!wp_verify_nonce($_POST[$nonce], $nonce_action)) {
                     $invalid_nonce = true;
                 }
             }
             if ($submitted && !$invalid_nonce) {
                 // remove user from group
                 if (isset($_POST['group_id'])) {
                     $leave_group = Groups_Group::read($_POST['group_id']);
                     Groups_User_Group::delete($user_id, $leave_group->group_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="leave" />';
                 $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($leave_group) && $leave_group->group_id === $current_group->group_id) {
                         $output .= '<div class="groups-join left">';
                         $output .= sprintf(__('You have left the %s group.', GROUPS_PLUGIN_DOMAIN), wp_filter_nohtml_kses($leave_group->name));
                         $output .= '</div>';
                     }
                 }
             }
         }
     }
     return $output;
 }
 /**
  * Takes one attribute "group" which is a comma-separated list of group
  * names or ids (can be mixed).
  * The content is shown if the current user does NOT belong to the group(s).
  *
  * @param array $atts attributes
  * @param string $content content to render
  */
 public static function groups_non_member($atts, $content = null)
 {
     $output = "";
     $options = shortcode_atts(array("group" => ""), $atts);
     $show_content = true;
     if ($content !== null) {
         $groups_user = new Groups_User(get_current_user_id());
         $groups = explode(",", $options['group']);
         foreach ($groups as $group) {
             $group = trim($group);
             $current_group = Groups_Group::read($group);
             if (!$current_group) {
                 $current_group = Groups_Group::read_by_name($group);
             }
             if ($current_group) {
                 if (Groups_User_Group::read($groups_user->user->ID, $current_group->group_id)) {
                     $show_content = false;
                     break;
                 }
             }
         }
         if ($show_content) {
             remove_shortcode('groups_non_member');
             $content = do_shortcode($content);
             add_shortcode('groups_non_member', array(__CLASS__, 'groups_non_member'));
             $output = $content;
         }
     }
     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();
     }
 }
 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));
             }
         }
     }
 }