/** * Sets the topic type for a specific topic. * * @since 1.0.0 * @access public * @param int $topic_id * @param string $topic_type * @return bool */ function mb_set_topic_type($topic_id, $type) { $type = mb_topic_type_exists($type) ? $type : mb_get_normal_topic_type(); return update_post_meta($topic_id, mb_get_topic_type_meta_key(), $type); }
/** * Front end new topic handler. * * @since 1.0.0 * @access public * @return void */ function mb_handler_new_topic() { /* Verify the nonce. */ if (!mb_check_post_nonce('mb_new_topic_nonce', 'mb_new_topic_action')) { return; } /* Make sure the current user can create forum topics. */ if (!current_user_can('create_topics')) { 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_forum_id']) ? mb_get_forum_id($_POST['mb_forum_id']) : 0; $forum_id = 0 < $forum_id ? $forum_id : mb_get_default_forum_id(); /* Post Status. */ $post_status = isset($_POST['mb_post_status']) && in_array($_POST['mb_post_status'], mb_get_topic_post_statuses()) ? $_POST['mb_post_status'] : mb_get_open_post_status(); /* Publish a new forum topic. */ $published = mb_insert_topic(array('post_title' => $post_title, 'post_content' => $post_content, 'post_parent' => $forum_id, 'post_status' => $post_status)); /* If the post was published. */ if ($published && !is_wp_error($published)) { /* Topic Type. */ $topic_type = isset($_POST['mb_topic_type']) && mb_topic_type_exists($_POST['mb_topic_type']) ? $_POST['mb_topic_type'] : 'normal'; mb_set_topic_type($published, $topic_type); /* If the user chose to subscribe to the topic. */ if (isset($_POST['mb_topic_subscribe']) && 1 == $_POST['mb_topic_subscribe']) { mb_add_user_topic_subscription(get_current_user_id(), $published); } /* Redirect to the published topic page. */ wp_safe_redirect(get_permalink($published)); } }
/** * Filter on the `request` hook to change what posts are loaded. * * @since 1.0.0 * @access public * @param array $vars * @return array */ public function request($vars) { $new_vars = array(); /* Load topics of a specific forum. */ if (isset($_GET['post_parent'])) { $new_vars['post_parent'] = mb_get_forum_id($_GET['post_parent']); } elseif (isset($_GET['topic_type']) && mb_topic_type_exists($_GET['topic_type'])) { $new_vars['meta_key'] = mb_get_topic_type_meta_key(); $new_vars['meta_value'] = sanitize_key($_GET['topic_type']); } elseif (isset($vars['orderby']) && 'post_parent' === $vars['orderby']) { $new_vars['orderby'] = 'post_parent'; } elseif (isset($vars['orderby']) && 'reply_count' === $vars['orderby']) { $new_vars['orderby'] = 'meta_value_num'; $new_vars['meta_key'] = mb_get_topic_reply_count_meta_key(); } elseif (isset($vars['orderby']) && 'voice_count' === $vars['orderby']) { $new_vars['orderby'] = 'meta_value_num'; $new_vars['meta_key'] = mb_get_topic_voice_count_meta_key(); } elseif (isset($vars['orderby']) && 'post_author' === $vars['orderby']) { $new_vars['orderby'] = 'post_author'; } /* Return the vars, merging with the new ones. */ return array_merge($vars, $new_vars); }
/** * Callback for the `save_post` hook to handle meta boxes. * * @since 1.0.0 * @access public * @param int $post_id * @param object $post * @return void */ function save_post($post_id, $post) { /* Fix for attachment save issue in WordPress 3.5. @link http://core.trac.wordpress.org/ticket/21963 */ if (!is_object($post)) { $post = get_post(); } if (mb_get_topic_post_type() !== $post->post_type) { return; } if (!isset($_POST['mb_topic_attr_nonce']) || !wp_verify_nonce($_POST['mb_topic_attr_nonce'], '_mb_topic_attr_nonce')) { return; } /* Get the new topic type. */ $topic_type = sanitize_key($_POST['mb_topic_type']); if (mb_get_topic_type($post_id) !== $topic_type && mb_topic_type_exists($topic_type)) { if ('super' === $topic_type) { mb_add_super_topic($post_id); } elseif ('sticky' === $topic_type) { mb_add_sticky_topic($post_id); } elseif ('normal' === $topic_type && mb_is_topic_super($post_id)) { mb_remove_super_topic($post_id); } elseif ('normal' === $topic_type && mb_is_topic_sticky($post_id)) { mb_remove_sticky_topic($post_id); } /* Set the new topic type. */ mb_set_topic_type($post_id, $topic_type); } }