/**
 * Overwrites capabilities in certain scenarios.
 *
 * @since  1.0.0
 * @access public
 * @param  array   $caps
 * @param  string  $cap
 * @param  int     $user_id
 * @param  array   $args
 * @return array
 */
function mb_topic_map_meta_cap($caps, $cap, $user_id, $args)
{
    /* Checks if a user can read a specific topic. */
    if ('read_post' === $cap && mb_is_topic($args[0])) {
        $post = get_post($args[0]);
        /* Only run our code if the user isn't the post author. */
        if ($user_id != $post->post_author) {
            $forum_id = $post->post_parent;
            /* If we have a forum and the user can't read it, don't allow reading the topic. */
            if (0 < $forum_id && !mb_user_can($user_id, 'read_forum', $forum_id)) {
                $caps = array('do_not_allow');
                /* If the user can read the forum, check if they can read the topic. */
            } else {
                $post_type = get_post_type_object($post->post_type);
                $post_status = mb_get_topic_status($post->ID);
                $status_obj = get_post_status_object($post_status);
                if (mb_get_hidden_post_status() === $status_obj->name) {
                    $caps[] = $post_type->cap->read_hidden_topics;
                } elseif (mb_get_private_post_status() === $status_obj->name) {
                    $caps[] = $post_type->cap->read_private_posts;
                } elseif ($post_type->cap->read !== $post_type->cap->read_others_topics) {
                    $caps[] = $post_type->cap->read_others_topics;
                } else {
                    $caps = array();
                }
                //$caps[] = $post_type->cap->read;
            }
        } else {
            $caps = array();
        }
        /* Meta cap for editing a single topic. */
    } elseif ('edit_post' === $cap && mb_is_topic($args[0])) {
        $post = get_post($args[0]);
        $topic_obj = get_post_type_object(mb_get_topic_post_type());
        if ($user_id != $post->post_author) {
            // Open topics.
            if (mb_is_topic_open($args[0])) {
                $caps[] = $topic_obj->cap->edit_open_topics;
            } elseif (mb_is_topic_closed($args[0])) {
                $caps[] = $topic_obj->cap->edit_closed_topics;
            } elseif (mb_is_topic_hidden($args[0])) {
                $caps[] = $topic_obj->cap->edit_hidden_topics;
            }
        }
        // Spam topics
        if (mb_is_topic_spam($args[0])) {
            $caps[] = $topic_obj->cap->edit_spam_topics;
        } elseif (mb_is_topic_orphan($args[0])) {
            $caps[] = $topic_obj->cap->edit_orphan_topics;
        }
        /* Meta cap for opening a single topic. */
    } elseif ('open_topic' === $cap) {
        $caps = array();
        $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'open_topics' : 'do_not_allow';
        /* Meta cap for closing a single topic. */
    } elseif ('close_topic' === $cap) {
        $caps = array();
        $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'close_topics' : 'do_not_allow';
        /* Meta cap for privatizing a single topic. */
    } elseif ('privatize_topic' === $cap) {
        $caps = array();
        $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'privatize_topics' : 'do_not_allow';
        /* Meta cap for hiding a single topic. */
    } elseif ('hide_topic' === $cap) {
        $caps = array();
        $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'hide_topics' : 'do_not_allow';
        /* Meta cap for spamming a single topic. */
    } elseif ('spam_topic' === $cap) {
        $caps = array();
        $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'spam_topics' : 'do_not_allow';
        /* Meta cap for spamming a single topic. */
    } elseif ('super_topic' === $cap) {
        $caps = array();
        $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'super_topics' : 'do_not_allow';
        /* Meta cap for spamming a single topic. */
    } elseif ('stick_topic' === $cap) {
        $caps = array();
        $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'stick_topics' : 'do_not_allow';
        /* Meta cap check for accessing the topic form. */
    } elseif ('access_topic_form' === $cap) {
        $caps = array('create_topics');
        if (mb_is_single_forum()) {
            $forum_id = mb_get_forum_id();
            if (!current_user_can('read_forum', $forum_id)) {
                $caps[] = 'do_not_allow';
            } elseif (!mb_forum_allows_topics($forum_id)) {
                $caps[] = 'do_not_allow';
            }
        } elseif (mb_is_topic_edit() && !user_can($user_id, 'edit_post', mb_get_topic_id())) {
            $caps[] = 'do_not_allow';
        }
    }
    return $caps;
}
Esempio n. 2
0
function mb_handler_topic_toggle_open()
{
    $actions = array('mb_toggle_open', 'mb_toggle_close');
    if (!isset($_GET['action']) || !in_array($_GET['action'], $actions) || !isset($_GET['topic_id'])) {
        return;
    }
    $topic_id = mb_get_topic_id($_GET['topic_id']);
    if ('mb_toggle_open' === $_GET['action']) {
        /* Verify nonce. */
        if (!isset($_GET['mb_nonce']) || !wp_verify_nonce($_GET['mb_nonce'], "open_topic_{$topic_id}")) {
            return;
        }
        if (mb_is_topic_open($topic_id) || !current_user_can('open_topic', $topic_id)) {
            return;
        }
        $updated = mb_open_topic($topic_id);
    } elseif ('mb_toggle_close' === $_GET['action']) {
        /* Verify nonce. */
        if (!isset($_GET['mb_nonce']) || !wp_verify_nonce($_GET['mb_nonce'], "close_topic_{$topic_id}")) {
            return;
        }
        if (mb_is_topic_closed($topic_id) || !current_user_can('close_topic', $topic_id)) {
            return;
        }
        $updated = mb_close_topic($topic_id);
    }
    $redirect = remove_query_arg(array('action', 'topic_id', 'mb_nonce'));
    wp_safe_redirect(esc_url($redirect));
}
Esempio n. 3
0
 /**
  * Callback function for handling post status changes.
  *
  * @since  1.0.0
  * @access public
  * @return void
  */
 public function handler()
 {
     /* Checks if the open/close toggle link was clicked. */
     if (isset($_GET['mb_toggle_status']) && isset($_GET['topic_id'])) {
         $topic_id = absint(mb_get_topic_id($_GET['topic_id']));
         /* Assume the changed failed. */
         $notice = 'failure';
         if ('spam' === $_GET['mb_toggle_status']) {
             /* Verify the nonce. */
             check_admin_referer("spam_topic_{$topic_id}");
             /* Check if the topic is open. */
             $is_spam = mb_is_topic_spam($topic_id);
             /* Update the post status. */
             $updated = $is_spam ? mb_unspam_topic($topic_id) : mb_spam_topic($topic_id);
             /* If the status was updated, add notice slug. */
             if ($updated && !is_wp_error($updated)) {
                 $notice = $is_spam ? 'restore' : mb_get_spam_post_status();
             }
         } elseif ('open' === $_GET['mb_toggle_status'] && !mb_is_topic_open($topic_id)) {
             /* Verify the nonce. */
             check_admin_referer("open_topic_{$topic_id}");
             /* Update the post status. */
             $updated = mb_open_topic($topic_id);
             /* If the status was updated, add notice slug. */
             if ($updated && !is_wp_error($updated)) {
                 $notice = mb_get_open_post_status();
             }
         } elseif ('close' === $_GET['mb_toggle_status'] && !mb_is_topic_closed($topic_id)) {
             /* Verify the nonce. */
             check_admin_referer("close_topic_{$topic_id}");
             /* Update the post status. */
             $updated = mb_close_topic($topic_id);
             /* If the status was updated, add notice slug. */
             if ($updated && !is_wp_error($updated)) {
                 $notice = mb_get_close_post_status();
             }
         }
         /* Redirect to correct admin page. */
         $redirect = add_query_arg(array('topic_id' => $topic_id, 'mb_topic_notice' => $notice), remove_query_arg(array('action', 'mb_toggle_status', 'topic_id', '_wpnonce')));
         wp_safe_redirect($redirect);
         /* Always exit for good measure. */
         exit;
     } elseif (isset($_GET['action']) && 'mb_toggle_spam' === $_GET['action'] && isset($_GET['topic_id'])) {
         $topic_id = absint(mb_get_topic_id($_GET['topic_id']));
         /* Verify the nonce. */
         check_admin_referer("spam_topic_{$topic_id}");
         /* Assume the changed failed. */
         $notice = 'failure';
         /* Check if the topic is open. */
         $is_spam = mb_is_topic_spam($topic_id);
         /* Update the post status. */
         $updated = $is_spam ? mb_unspam_topic($topic_id) : mb_spam_topic($topic_id);
         /* If the status was updated, add notice slug. */
         if ($updated && !is_wp_error($updated)) {
             $notice = $is_spam ? 'restore' : mb_get_spam_post_status();
         }
         /* Redirect to correct admin page. */
         $redirect = add_query_arg(array('topic_id' => $topic_id, 'mb_topic_notice' => $notice), remove_query_arg(array('action', 'topic_id', '_wpnonce')));
         wp_safe_redirect($redirect);
         /* Always exit for good measure. */
         exit;
     } elseif (isset($_GET['action']) && 'mb_toggle_sticky' === $_GET['action'] && isset($_GET['topic_id'])) {
         $topic_id = absint(mb_get_topic_id($_GET['topic_id']));
         /* Verify the nonce. */
         check_admin_referer("sticky_topic_{$topic_id}");
         /* Assume the changed failed. */
         $notice = 'failure';
         /* Check if the topic is sticky. */
         $is_sticky = mb_is_topic_sticky($topic_id);
         /* Update the topic type. */
         if ($is_sticky) {
             $updated = mb_remove_sticky_topic($topic_id);
             mb_set_topic_type($topic_id, 'normal');
         } else {
             $updated = mb_add_sticky_topic($topic_id);
             mb_set_topic_type($topic_id, 'sticky');
         }
         /* If the status was updated, add notice slug. */
         if ($updated && !is_wp_error($updated)) {
             $notice = $is_sticky ? 'unsticky' : 'sticky';
         }
         /* Redirect to correct admin page. */
         $redirect = add_query_arg(array('topic_id' => $topic_id, 'mb_topic_notice' => $notice), remove_query_arg(array('action', 'topic_id', '_wpnonce')));
         wp_safe_redirect($redirect);
         /* Always exit for good measure. */
         exit;
     } elseif (isset($_GET['action']) && 'mb_toggle_super' === $_GET['action'] && isset($_GET['topic_id'])) {
         $topic_id = absint(mb_get_topic_id($_GET['topic_id']));
         /* Verify the nonce. */
         check_admin_referer("super_topic_{$topic_id}");
         /* Assume the changed failed. */
         $notice = 'failure';
         /* Check if the topic is sticky. */
         $is_super = mb_is_topic_super($topic_id);
         /* Update the topic type. */
         if ($is_super) {
             $updated = mb_remove_super_topic($topic_id);
             mb_set_topic_type($topic_id, 'normal');
         } else {
             $updated = mb_add_super_topic($topic_id);
             mb_set_topic_type($topic_id, 'super');
         }
         /* If the status was updated, add notice slug. */
         if ($updated && !is_wp_error($updated)) {
             $notice = $is_sticky ? 'unsuper' : 'super';
         }
         /* Redirect to correct admin page. */
         $redirect = add_query_arg(array('topic_id' => $topic_id, 'mb_topic_notice' => $notice), remove_query_arg(array('action', 'topic_id', '_wpnonce')));
         wp_safe_redirect($redirect);
         /* Always exit for good measure. */
         exit;
     }
 }
Esempio n. 4
0
function mb_get_topic_toggle_open_url($topic_id = 0)
{
    $topic_id = mb_get_topic_id($topic_id);
    if (mb_is_topic_open($topic_id) || !current_user_can('open_topic', $topic_id)) {
        return '';
    }
    $url = add_query_arg(array('topic_id' => $topic_id, 'action' => 'mb_toggle_open'));
    $url = wp_nonce_url($url, "open_topic_{$topic_id}", 'mb_nonce');
    return $url;
}