/** * Adds a topic to the list of sticky topics. * * @since 1.0.0 * @access public * @param int $topic_id * @return bool */ function mb_add_sticky_topic($topic_id) { $topic_id = mb_get_topic_id($topic_id); if (mb_is_topic_super($topic_id)) { mb_remove_super_topic($topic_id); } if (!mb_is_topic_sticky($topic_id)) { return update_option('mb_sticky_topics', array_unique(array_merge(mb_get_sticky_topics(), array($topic_id)))); } return false; }
/** * Returns a topic's labels. * * @since 1.0.0 * @access public * @return string */ function mb_get_topic_states($topic_id = 0) { $topic_id = mb_get_topic_id($topic_id); $labels = array(); if (mb_is_topic_super($topic_id) && (mb_is_topic_archive() || mb_is_single_forum())) { $labels['super'] = __('[Sticky]', 'message-board'); } elseif (mb_is_topic_sticky($topic_id) && mb_is_single_forum()) { $labels['sticky'] = __('[Sticky]', 'message-board'); } if (mb_is_topic_closed($topic_id)) { $labels['closed'] = __('[Closed]', 'message-board'); } $labels = apply_filters('mb_topic_labels', $labels, $topic_id); if (!empty($labels)) { $formatted = ''; foreach ($labels as $key => $value) { $formatted .= sprintf('<span class="topic-label %s">%s</span> ', sanitize_html_class("topic-label-{$key}"), $value); } return sprintf('<span class="topic-labels">%s</span>', $formatted); } return ''; }
/** * 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); } }