/** * 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_forum_map_meta_cap($caps, $cap, $user_id, $args) { /* Checks if a user can read a specific forum. */ if ('read_post' === $cap && mb_is_forum($args[0])) { $post = get_post($args[0]); if ($user_id != $post->post_author) { $parent_id = $post->post_parent; /* If we have a parent forum and the user can't read it, don't allow reading this forum. */ if (0 < $parent_id && !mb_user_can($user_id, 'read_forum', $parent_id)) { $caps = array('do_not_allow'); /* If the user can read the parent forum, check if they can read this one. */ } else { $post_type = get_post_type_object($post->post_type); $post_status = mb_get_forum_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_forums; } 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_forums) { $caps[] = $post_type->cap->read_others_forums; } else { $caps = array(); } } } else { $caps = array(); } /* Meta cap for editing a single forum. */ } elseif ('edit_post' === $cap && mb_is_forum($args[0])) { $post = get_post($args[0]); $forum_obj = get_post_type_object(mb_get_forum_post_type()); if ($user_id != $post->post_author) { // Open forums. if (mb_is_forum_open($args[0])) { $caps[] = $forum_obj->cap->edit_open_forums; } elseif (mb_is_forum_closed($args[0])) { $caps[] = $forum_obj->cap->edit_closed_forums; } elseif (mb_is_forum_hidden($args[0])) { $caps[] = $forum_obj->cap->edit_hidden_forums; } } /* Meta cap for opening a single forum. */ } elseif ('open_forum' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_forum', $args[0]) ? 'open_forums' : 'do_not_allow'; /* Meta cap for closing a single forum. */ } elseif ('close_forum' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_forum', $args[0]) ? 'close_forums' : 'do_not_allow'; /* Meta cap for privatizing a single forum. */ } elseif ('privatize_forum' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_forum', $args[0]) ? 'privatize_forums' : 'do_not_allow'; /* Meta cap for hiding a single forum. */ } elseif ('hide_forum' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_forum', $args[0]) ? 'hide_forums' : 'do_not_allow'; /* Meta cap for spamming a single forum. */ } elseif ('archive_forum' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_forum', $args[0]) ? 'archive_forums' : 'do_not_allow'; /* Meta cap for deleting a specific forum. */ } elseif ('delete_post' === $cap && mb_is_forum($args[0])) { $forum_id = mb_get_forum_id($args[0]); if (mb_get_default_forum_id() === $forum_id) { $caps = array('do_not_allow'); } /* Meta cap check for accessing the forum form. */ } elseif ('access_forum_form' === $cap) { $caps = array('create_forums'); /* If this is a single forum page, check if user can create sub-forums. */ 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_subforums($forum_id)) { $caps[] = 'do_not_allow'; } } elseif (mb_is_forum_edit() && !user_can($user_id, 'edit_post', mb_get_forum_id())) { $caps[] = 'do_not_allow'; } } return $caps; }
/** * Filter for the `post_states` hook. We're going to replace any defaults and roll our own. * * @since 1.0.0 * @access public * @param array $post_states * @param object $post */ public function display_post_states($post_states, $post) { $states = array(); $forum_id = mb_get_forum_id($post->ID); if (mb_get_default_forum_id() === $forum_id) { $states['default-forum'] = __('Default Forum', 'message-board'); } return $states; }
/** * Attempt to always make sure that topics have a post parent. * * @since 1.0.0 * @access public * @param int $post_parent * @param int $post_id * @param array $new_postarr * @return int */ function mb_insert_topic_post_parent($post_parent, $post_id, $new_postarr) { if (mb_get_topic_post_type() === $new_postarr['post_type'] && 0 >= $post_parent) { $post_parent = mb_get_default_forum_id(); } return $post_parent; }
/** * Callback function on the `before_delete_post` hook for when a post is deleted. This sets up some * specific actions based on our post types. It also saves the deleted post object for later use in * those actions. * * @since 1.0.0 * @access public * @param int $post_id * @return void */ function mb_before_delete_post($post_id) { $forum_type = mb_get_forum_post_type(); $topic_type = mb_get_topic_post_type(); $reply_type = mb_get_reply_post_type(); $post_type = get_post_type($post_id); /* WP doesn't pass the post object after a post has been deleted, so we need to save it temporarily. */ if (in_array($post_type, array($forum_type, $topic_type, $reply_type))) { message_board()->deleted_post = get_post($post_id); } /* If a forum is being deleted. */ if ($forum_type === $post_type) { /* If this is the default forum, stop everything. */ if (mb_get_default_forum_id() === $post_id) { wp_die('Whoah there! This is the default forum and cannot be deleted. Visit the settings page to change the default forum.', 'message-board'); } add_action('after_delete_post', 'mb_after_delete_forum'); /* If a topic is being deleted. */ } elseif ($topic_type === $post_type) { add_action('after_delete_post', 'mb_after_delete_topic'); /* If a reply is being deleted. */ } elseif ($reply_type === $post_type) { add_action('after_delete_post', 'mb_after_delete_reply'); } }
/** * Front end edit topic handler. * * @since 1.0.0 * @access public * @return void */ function mb_handler_edit_topic() { /* Verify the nonce. */ if (!mb_check_post_nonce('mb_edit_topic_nonce', 'mb_edit_topic_action')) { return; } /* Make sure we have a topic ID. */ if (!isset($_POST['mb_topic_id'])) { mb_bring_the_doom('what-edit'); } /* Get the topic ID. */ $topic_id = mb_get_topic_id($_POST['mb_topic_id']); /* Make sure the current user can create forum topics. */ if (!current_user_can('edit_topic', $topic_id)) { mb_bring_the_doom('no-permission'); } /* Make sure we have a topic title. */ if (empty($_POST['mb_topic_title'])) { mb_bring_the_doom('no-title'); } /* Make sure we have topic content. */ if (empty($_POST['mb_topic_content'])) { mb_bring_the_doom('no-content'); } /* Post title. */ $post_title = apply_filters('mb_pre_insert_topic_title', $_POST['mb_topic_title']); /* Post content. */ $post_content = apply_filters('mb_pre_insert_topic_content', $_POST['mb_topic_content']); /* Forum ID. */ $forum_id = isset($_POST['mb_topic_forum']) ? mb_get_forum_id($_POST['mb_topic_forum']) : 0; $forum_id = 0 < $forum_id ? $forum_id : mb_get_default_forum_id(); /* Update the topic. */ $published = wp_update_post(array('ID' => $topic_id, 'post_title' => $post_title, 'post_content' => $post_content, 'post_parent' => $forum_id)); /* If the post was published. */ if ($published && !is_wp_error($published)) { /* If the user chose to subscribe to the topic. */ if (isset($_POST['mb_topic_subscribe']) && 1 === absint($_POST['mb_topic_subscribe'])) { mb_add_user_topic_subscription(absint($user_id), $published); } else { mb_remove_user_topic_subscription(absint($user_id), $published); } /* Redirect to the published topic page. */ wp_safe_redirect(get_permalink($published)); } }
/** * Topic attributes meta box. This handles whether the topic is sticky and the parent forum. It also * has the hidden input to save the proper `menu_order` field for the post. * * @since 1.0.0 * @access public * @param object $post * @return void */ function mb_topic_attributes_meta_box($post) { wp_nonce_field('_mb_topic_attr_nonce', 'mb_topic_attr_nonce'); $topic_type_object = get_post_type_object(mb_get_topic_post_type()); ?> <p> <label for="mb_topic_type"> <strong><?php _e('Topic Type:', 'message-board'); ?> </strong> </label> </p> <p> <?php mb_dropdown_topic_type(array('selected' => mb_get_topic_type($post->ID))); ?> </p> <p> <label for="mb_parent_forum"> <strong><?php echo $topic_type_object->labels->parent_item_colon; ?> </strong> </label> </p> <p> <?php mb_dropdown_forums(array('child_type' => mb_get_topic_post_type(), 'name' => 'parent_id', 'id' => 'mb_parent_forum', 'selected' => !empty($post->post_parent) ? $post->post_parent : mb_get_default_forum_id())); ?> </p> <?php $order = 0 != $post->ID ? $post->post_date : current_time('mysql'); ?> <input type="hidden" name="menu_order" value="<?php echo esc_attr(mysql2date('U', $order)); ?> " /> <?php }