Beispiel #1
0
/**
 * Generate the reader selector interface.
 */
function cacsp_paper_reader_selector($paper_id)
{
    global $wpdb;
    // Get a list of readers, friends, and co-group-members to prime selectbox.
    // @todo Add AJAX support.
    $paper = new CACSP_Paper($paper_id);
    $paper_reader_ids = $paper->get_reader_ids();
    $groups_of_user = cacsp_get_groups_of_user(bp_loggedin_user_id());
    if (empty($groups_of_user)) {
        $groups_of_user = array(0);
    }
    // So dirty.
    $bp = buddypress();
    $group_member_ids = $wpdb->get_col($wpdb->prepare("SELECT user_id FROM {$bp->groups->table_name_members} WHERE group_id IN (" . implode(',', $groups_of_user) . ") AND user_id != %d AND is_banned = 0 AND is_confirmed = 1", bp_loggedin_user_id()));
    $group_member_ids = array_map('intval', $group_member_ids);
    $friend_member_ids = array();
    if (bp_is_active('friends')) {
        $friend_member_ids = friends_get_friend_user_ids(bp_loggedin_user_id());
    }
    $users = bp_core_get_users(array('include' => array_merge($paper_reader_ids, $group_member_ids, $friend_member_ids), 'type' => 'alphabetical'));
    $user_data = array();
    $selected = array();
    if (!empty($users['users'])) {
        foreach ($users['users'] as $user) {
            $user_id = (int) $user->ID;
            $user_data[] = array('id' => $user_id, 'text' => html_entity_decode($user->display_name, ENT_QUOTES, 'UTF-8'));
            // Collect data about the existing readers.
            if (in_array($user_id, $paper_reader_ids, true)) {
                $selected[] = array('id' => $user_id, 'text' => stripslashes($user->display_name));
            }
        }
    }
    $script = 'var CACSP_Potential_Readers = ' . wp_json_encode($user_data) . ';';
    echo "\n" . '<script type="text/javascript">' . $script . '</script>' . "\n";
    // Select2 only needs an <option> printed for the selected options.
    ?>
	<select name="cacsp-readers[]" multiple="multiple" style="width:100%;" id="cacsp-reader-selector">
		<?php 
    foreach ($selected as $_selected) {
        echo '<option value="' . esc_attr($_selected['id']) . '" selected="selected">' . esc_html($_selected['text']) . '</option>';
    }
    ?>
	</select>
	<?php 
    wp_nonce_field('cacsp-reader-selector', 'cacsp-reader-selector-nonce');
}
/**
 * Format activity actions for papers connected to groups.
 *
 * Disabled for the time being. Not sure if it's valuable information.
 *
 * @param string      $action      Formatted action string.
 * @param obj         $activity    Activity item.
 * @param CACSP_Paper $paper       Paper object.
 * @param string      $paper_title Paper title.
 * @param string      $paper_link  Paper URL.
 * @param string      $user_link   User link.
 * @return string
 */
function cacsp_format_activity_action_for_group($action, $activity, CACSP_Paper $paper, $paper_title, $paper_link, $user_link)
{
    // Don't bother doing this on a group page.
    if (bp_is_group()) {
        return $action;
    }
    $paper_group_ids = $paper->get_group_ids();
    if (empty($paper_group_ids)) {
        return $action;
    }
    // @todo roll our own cache support here too? Le sigh.
    $_paper_groups = groups_get_groups(array('populate_extras' => false, 'update_meta_cache' => false, 'show_hidden' => true, 'include' => $paper_group_ids));
    $paper_groups = $_paper_groups['groups'];
    // Only include groups that a user has access to. Groups a user is a member of come first.
    $groups_to_include = array('is_member' => array(), 'is_not_member' => array());
    $user_groups = array();
    if (is_user_logged_in()) {
        $user_groups = cacsp_get_groups_of_user(bp_loggedin_user_id());
    }
    foreach ($paper_groups as $pg) {
        $pg_id = (int) $pg->id;
        $k = null;
        if (in_array($pg_id, $user_groups, true)) {
            $k = 'is_member';
        } elseif ('public' === $pg->status) {
            $k = 'is_not_member';
        }
        if ($k) {
            $groups_to_include[$k][$pg->name] = bp_get_group_permalink($pg) . 'papers/';
        }
    }
    $links = array();
    foreach ($groups_to_include as $gg) {
        ksort($gg);
        foreach ($gg as $group_name => $group_link) {
            $links[] = sprintf('<a href="%s">%s</a>', esc_url($group_link), esc_html($group_name));
        }
    }
    // @todo Other activity types.
    // 1, 2, 3 groups: show all. 4+ groups: show first two + "and x more groups".
    if ('new_cacsp_paper' === $activity->type) {
        if (count($links) === 1) {
            $action = sprintf(__('%1$s created a new paper %2$s in the group %3$s', 'social-paper'), $user_link, sprintf('<a href="%s">%s</a>', esc_url($paper_link), esc_html($paper_title)), implode('', $links));
        } elseif (count($links) <= 3) {
            $action = sprintf(_n('%1$s created a new paper %2$s in the group %3$s', '%1$s created a new paper in the groups %3$s', count($links), 'social-paper'), $user_link, sprintf('<a href="%s">%s</a>', esc_url($paper_link), esc_html($paper_title)), implode(', ', $links));
        } else {
            $remainder = count($links) - 2;
            $action = sprintf(_n('%1$s created a new paper %2$s in %3$s and %4$s more group', '%1$s created a new paper in %3$s and %4$s more groups', $remainder, 'social-paper'), $user_link, sprintf('<a href="%s">%s</a>', esc_url($paper_link), esc_html($paper_title)), implode(', ', array_slice($links, 0, 2)), number_format_i18n($remainder));
        }
    }
    return $action;
}
Beispiel #3
0
/**
 * Add capabilities for subscribers and contributors.
 *
 * By default, subscribers and contributors do not have caps to post, edit or
 * delete papers. This function injects these caps for users with these roles.
 *
 * @param  array  $caps    The mapped caps
 * @param  string $cap     The cap being mapped
 * @param  int    $user_id The user id in question
 * @param  array  $args    Optional parameters passed to has_cap(). For us, this means the post ID.
 * @return array
 */
function cacsp_map_extra_meta_caps($caps, $cap, $user_id, $args)
{
    switch ($cap) {
        // give user these caps
        case 'publish_papers':
        case 'edit_papers':
            // handles adding tags/categories
            break;
            // meta caps
        // meta caps
        case 'edit_paper':
        case 'delete_paper':
            // bail if someone else's event
            if (!empty($caps[0]) && false !== strpos($caps[0], 'others')) {
                return $caps;
            }
            break;
        case 'read_paper':
            // Make sure authors can view their own post
            if ((int) get_post($args[0])->post_author === $user_id) {
                return array('exist');
            }
            $post_id = $args[0];
            if (!cacsp_paper_is_protected($post_id)) {
                return array('exist');
            } else {
                $paper = new CACSP_Paper($post_id);
                $paper_reader_ids = $paper->get_reader_ids();
                if (in_array(get_current_user_id(), $paper_reader_ids, true)) {
                    return array('exist');
                } elseif (function_exists('bp_is_active') && bp_is_active('groups')) {
                    $paper_group_ids = $paper->get_group_ids();
                    $user_group_ids = cacsp_get_groups_of_user(get_current_user_id());
                    if (array_intersect($paper_group_ids, $user_group_ids)) {
                        return array('exist');
                    }
                }
            }
            return array('do_not_allow');
            // allow files to be uploaded via AJAX
        // allow files to be uploaded via AJAX
        case 'upload_files':
            if (defined('DOING_AJAX') && true === constant('DOING_AJAX')) {
                if (false === isset($_REQUEST['post_id'])) {
                    return $caps;
                }
                if (false === current_user_can('edit_paper', $_REQUEST['post_id'])) {
                    return $caps;
                }
                $user_id = get_current_user_id();
            } else {
                return $caps;
            }
            break;
        default:
            return $caps;
            break;
    }
    // make sure user is valid
    $user = new WP_User($user_id);
    if (!is_a($user, 'WP_User') || empty($user->ID)) {
        return $caps;
    }
    /**
     * Filters Social Paper basic meta caps.
     *
     * @param array   Pass 'exist' cap so users are able to manage papers.
     * @param array   $caps The mapped caps
     * @param string  $cap The cap being mapped
     * @param WP_User The user being tested for the cap.
     */
    return apply_filters('cacsp_map_extra_meta_caps', array('exist'), $caps, $cap, $user);
}