/** * 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)); } }
/** * 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; } }
/** * 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); } }