function mb_set_user_topic_count($user_id) { global $wpdb; $open_status = mb_get_open_post_status(); $close_status = mb_get_close_post_status(); $publish_status = mb_get_publish_post_status(); $hidden_status = mb_get_hidden_post_status(); $private_status = mb_get_private_post_status(); $where = $wpdb->prepare("WHERE post_author = %d AND post_type = %s", $user_id, mb_get_topic_post_type()); $status_where = "AND (post_status = '{$open_status}' OR post_status = '{$close_status}' OR post_status = '{$publish_status}' OR post_status = '{$private_status}' OR post_status = '{$hidden_status}')"; $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where} {$status_where}"); update_user_meta($user_id, mb_get_user_topic_count_meta_key(), $count); return $count; }
/** * Inserts a new topic. This is a wrapper for the `wp_insert_post()` function and should be used in * its place where possible. * * @since 1.0.0 * @access public * @param array $args * @return int|WP_Error */ function mb_insert_topic($args = array()) { /* Convert date. */ $post_date = current_time('mysql'); $post_epoch = mysql2date('U', $post_date); /* Set up the defaults. */ $defaults = array('menu_order' => $post_epoch, 'post_date' => $post_date, 'post_author' => get_current_user_id(), 'post_status' => mb_get_open_post_status(), 'post_parent' => mb_get_default_forum_id()); /* Allow devs to filter the defaults. */ $defaults = apply_filters('mb_insert_topic_defaults', $defaults); /* Parse the args/defaults and apply filters. */ $args = apply_filters('mb_insert_topic_args', wp_parse_args($args, $defaults)); /* Always make sure it's the correct post type. */ $args['post_type'] = mb_get_topic_post_type(); /* Insert the topic. */ return wp_insert_post($args); }
/** * Displays admin notices for the edit forum screen. * * @since 1.0.0 * @access public * @return void */ public function admin_notices() { $allowed_notices = array(mb_get_open_post_status(), mb_get_close_post_status(), mb_get_archive_post_status()); /* If we have an allowed notice. */ if (isset($_GET['mb_forum_notice']) && in_array($_GET['mb_forum_notice'], $allowed_notices) && isset($_GET['forum_id'])) { $notice = $_GET['mb_forum_notice']; $forum_id = mb_get_forum_id(absint($_GET['forum_id'])); if (mb_get_close_post_status() === $notice) { $text = sprintf(__('The forum "%s" was successfully closed.', 'message-board'), mb_get_forum_title($forum_id)); } elseif (mb_get_open_post_status() === $notice) { $text = sprintf(__('The forum "%s" was successfully opened.', 'message-board'), mb_get_forum_title($forum_id)); } elseif (mb_get_archive_post_status() === $notice) { $text = sprintf(__('The forum "%s" was successfully archived.', 'message-board'), mb_get_forum_title($forum_id)); } if (!empty($text)) { printf('<div class="updated"><p>%s</p></div>', $text); } } }
/** * Makes sure the correct post status is used when loading forums on the nav menus screen. By * default, WordPress will only load them if they have the "publish" post status. * * @since 1.0.0 * @access public * @param object $object * @return object */ public function nav_menu_meta_box_object($object) { if (isset($object->name) && mb_get_forum_post_type() === $object->name) { $statuses = array(mb_get_open_post_status(), mb_get_close_post_status(), mb_get_publish_post_status(), mb_get_private_post_status(), mb_get_hidden_post_status(), mb_get_archive_post_status()); $object->_default_query = wp_parse_args(array('post_status' => $statuses), $object->_default_query); } return $object; }
/** * Adds sticky posts to the front of the line with any given set of posts and stickies. * * @since 1.0.0 * @access public * @param array $posts Array of post objects. * @param array $sticky_posts Array of post IDs. * @param int $forum_id Limit to specific forum. * @return array */ function mb_add_stickies($posts, $sticky_posts, $forum_id = 0) { /* Only do this if on the first page and we indeed have stickies. */ if (!is_paged() && !empty($sticky_posts)) { $num_posts = count($posts); $sticky_offset = 0; /* Loop over posts and relocate stickies to the front. */ for ($i = 0; $i < $num_posts; $i++) { if (in_array($posts[$i]->ID, $sticky_posts)) { $sticky_post = $posts[$i]; /* Remove sticky from current position. */ array_splice($posts, $i, 1); /* Move to front, after other stickies. */ array_splice($posts, $sticky_offset, 0, array($sticky_post)); /* Increment the sticky offset. The next sticky will be placed at this offset. */ $sticky_offset++; /* Remove post from sticky posts array. */ $offset = array_search($sticky_post->ID, $sticky_posts); unset($sticky_posts[$offset]); } } /* Fetch sticky posts that weren't in the query results. */ if (!empty($sticky_posts)) { $args = array('post__in' => $sticky_posts, 'post_type' => mb_get_topic_post_type(), 'post_status' => array(mb_get_open_post_status(), mb_get_close_post_status(), mb_get_publish_post_status()), 'nopaging' => true); if (0 < $forum_id) { $args['post_parent'] = $forum_id; } $stickies = get_posts($args); foreach ($stickies as $sticky_post) { array_splice($posts, $sticky_offset, 0, array($sticky_post)); $sticky_offset++; } } } return $posts; }
/** * Gets a post's previous post status. * * @since 1.0.0 * @access public * @param int $post_id * @return string */ function mb_get_prev_post_status($post_id) { $status = get_post_meta($post_id, mb_get_prev_status_meta_key(), true); if (empty($status)) { $status = mb_get_publish_post_status(); if (in_array(get_post_type($post_id), array(mb_get_forum_post_type(), mb_get_topic_post_type()))) { $status = mb_get_open_post_status(); } } return $status; }
function mb_dropdown_topic_status($args = array()) { $args['post_type'] = mb_get_topic_post_type(); $args['selected'] = !empty($args['selected']) ? $args['selected'] : mb_get_open_post_status(); return mb_dropdown_post_status($args); }
/** * Displays admin notices for the edit forum screen. * * @since 1.0.0 * @access public * @return void */ public function admin_notices() { $allowed_notices = array('restore', mb_get_spam_post_status(), mb_get_open_post_status(), mb_get_close_post_status(), 'sticky', 'unsticky', 'super', 'unsuper'); if (isset($_GET['mb_topic_notice']) && in_array($_GET['mb_topic_notice'], $allowed_notices) && isset($_GET['topic_id'])) { $notice = $_GET['mb_topic_notice']; $topic_id = mb_get_topic_id(absint($_GET['topic_id'])); if (mb_get_spam_post_status() === $notice) { $text = sprintf(__('The topic "%s" was successfully marked as spam.', 'message-board'), mb_get_topic_title($topic_id)); } elseif ('restore' === $notice) { $text = sprintf(__('The topic "%s" was successfully removed from spam.', 'message-board'), mb_get_topic_title($topic_id)); } elseif (mb_get_close_post_status() === $notice) { $text = sprintf(__('The topic "%s" was successfully closed.', 'message-board'), mb_get_topic_title($topic_id)); } elseif (mb_get_open_post_status() === $notice) { $text = sprintf(__('The topic "%s" was successfully opened.', 'message-board'), mb_get_topic_title($topic_id)); } elseif ('sticky' === $notice) { $text = sprintf(__('The topic "%s" was successfully added as a sticky topic.', 'message-board'), mb_get_topic_title($topic_id)); } elseif ('unsticky' === $notice) { $text = sprintf(__('The topic "%s" was successfully removed from sticky topics.', 'message-board'), mb_get_topic_title($topic_id)); } elseif ('super' === $notice) { $text = sprintf(__('The topic "%s" was successfully added as a super sticky topic.', 'message-board'), mb_get_topic_title($topic_id)); } elseif ('unsuper' === $notice) { $text = sprintf(__('The topic "%s" was successfully removed from super sticky topics.', 'message-board'), mb_get_topic_title($topic_id)); } if (!empty($text)) { printf('<div class="updated"><p>%s</p></div>', $text); } } }
/** * Resets the forum's "latest" data. * * @since 1.0.0 * @access public * @param int $forum_id * @return int */ function mb_reset_forum_latest($forum_id) { global $wpdb; $forum_id = mb_get_forum_id($forum_id); $open_status = mb_get_open_post_status(); $close_status = mb_get_close_post_status(); $publish_status = mb_get_publish_post_status(); $private_status = mb_get_private_post_status(); $hidden_status = mb_get_hidden_post_status(); $status_where = "AND (post_status = '{$open_status}' OR post_status = '{$close_status}' OR post_status = '{$publish_status}' OR post_status = '{$private_status}' OR post_status = '{$hidden_status}')"; $topic_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = %s {$status_where} AND post_parent = %d ORDER BY menu_order DESC", mb_get_topic_post_type(), $forum_id)); if (!empty($topic_id)) { $t_status_where = "AND (topic.post_status = '{$open_status}' OR topic.post_status = '{$close_status}' OR topic.post_status = '{$publish_status}' OR topic.post_status = '{$private_status}' OR topic.post_status = '{$hidden_status}')"; $r_status_where = "AND reply.post_status = '{$publish_status}'"; $status_where = $t_status_where . $r_status_where; $reply_id = $wpdb->get_var($wpdb->prepare("SELECT reply.ID FROM {$wpdb->posts} AS reply INNER JOIN {$wpdb->posts} AS topic ON reply.post_parent = topic.ID WHERE topic.post_parent = %d {$status_where} ORDER BY reply.post_date DESC", $forum_id)); if ($reply_id) { mb_set_forum_last_reply_id($forum_id, $reply_id); mb_set_forum_last_topic_id($forum_id, $topic_id); $last_date = get_post($reply_id)->post_date; $epoch_date = mysql2date('U', $last_date); mb_set_forum_activity_datetime($forum_id, $last_date); mb_set_forum_activity_epoch($forum_id, $epoch_date); } else { delete_post_meta($forum_id, mb_get_forum_last_reply_id_meta_key()); mb_set_forum_last_topic_id($forum_id, $topic_id); $last_date = get_post($topic_id)->post_date; $epoch_date = mysql2date('U', $last_date); mb_set_forum_activity_datetime($forum_id, $last_date); mb_set_forum_activity_epoch($forum_id, $epoch_date); } } else { delete_post_meta($forum_id, mb_get_forum_last_reply_id_meta_key()); delete_post_meta($forum_id, mb_get_forum_last_topic_id_meta_key()); delete_post_meta($forum_id, mb_get_forum_activity_datetime_meta_key()); delete_post_meta($forum_id, mb_get_forum_activity_datetime_epoch_meta_key()); } }
/** * 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)); } }
function mb_get_forum_toggle_open_link($forum_id = 0) { $forum_id = mb_get_forum_id($forum_id); $url = mb_get_forum_toggle_open_url($forum_id); if (empty($url)) { return ''; } $status = get_post_status_object(mb_get_open_post_status()); $link = sprintf('<a class="mb-forum-open-link" href="%s">%s</a>', $url, $status->mb_label_verb); return $link; }
/** * Custom `submitdiv` meta box. This replaces the WordPress default because it has too many things * hardcoded that we cannot overwrite, particularly dealing with post statuses. * * @since 1.0.0 * @access public * @param object $post * @param array $args * @return void */ function mb_submit_meta_box($post, $args = array()) { $forum_type = mb_get_forum_post_type(); $topic_type = mb_get_topic_post_type(); $reply_type = mb_get_reply_post_type(); $post_type = $post->post_type; $post_status = $post->post_status; $hidden_status = 'auto-draft' === $post_status ? 'draft' : $post_status; /* If the post is a forum. */ if ($forum_type === $post_type) { $allowed_stati = mb_get_forum_post_statuses(); $status_obj = in_array($post_status, $allowed_stati) ? get_post_status_object($post_status) : get_post_status_object(mb_get_open_post_status()); /* If the post is a topic. */ } elseif ($topic_type === $post_type) { $allowed_stati = mb_get_topic_post_statuses(); $status_obj = in_array($post_status, $allowed_stati) ? get_post_status_object($post_status) : get_post_status_object(mb_get_open_post_status()); /* If the post is a reply. */ } elseif ($reply_type === $post_type) { $allowed_stati = mb_get_reply_post_statuses(); $status_obj = in_array($post_status, $allowed_stati) ? get_post_status_object($post_status) : get_post_status_object(mb_get_publish_post_status()); } ?> <div class="submitbox" id="submitpost"> <div id="minor-publishing"> <div id="misc-publishing-actions"> <div class="misc-pub-section misc-pub-post-status"> <label for="post_status"> <?php printf(__('Status: %s', 'message-board'), "<strong class='mb-current-status'>{$status_obj->label}</strong>"); ?> </label> <a href="#post-status-select" class="edit-post-status hide-if-no-js"> <span aria-hidden="true"><?php _e('Edit', 'message-board'); ?> </span> <span class="screen-reader-text"><?php _e('Edit status', 'message-board'); ?> </span> </a> <div id="post-status-select" class="hide-if-js"> <input type="hidden" name="hidden_post_status" id="hidden_post_status" value="<?php echo esc_attr($hidden_status); ?> " /> <div id="post_status"> <?php mb_dropdown_forum_status(array('selected' => $post_status, 'id' => 'post_status', 'name' => 'post_status')); ?> <a href="#post_status" class="save-post-status hide-if-no-js button"><?php _e('OK', 'message-board'); ?> </a> <a href="#post_status" class="cancel-post-status hide-if-no-js button-cancel"><?php _e('Cancel', 'message-board'); ?> </a> </div><!-- #post_status --> </div><!-- #post-status-select --> </div><!-- .misc-pub-section --> <?php /* Get the post date. */ $date_mysql = 0 != $post->ID ? $post->post_date : current_time('mysql'); /* Translators: Publish box date format. */ $date_i18n = date_i18n(__('M j, Y @ G:i', 'message-board'), strtotime($date_mysql)); ?> <div class="misc-pub-section curtime misc-pub-curtime"> <span id="timestamp"><?php printf(__('Date: %s', 'message-board'), "<strong>{$date_i18n}</strong>"); ?> </span> </div><!-- .misc-pub-curtime --> <div class="misc-pub-section"> <i class="dashicons dashicons-admin-users"></i> <?php printf(__('Author: %s', 'message-board'), '<strong>' . get_the_author_meta('display_name', $post->post_author) . '</strong>'); ?> </div><!-- .misc-pub-section --> <?php do_action('post_submitbox_misc_actions'); ?> </div><!-- #misc-publishing-actions --> <div class="clear"></div> </div><!-- #minor-publishing --> <div id="major-publishing-actions"> <?php do_action('post_submitbox_start'); ?> <div id="delete-action"> <?php if (current_user_can('delete_post', $post->ID)) { ?> <a class="submitdelete deletion" href="<?php echo get_delete_post_link($post->ID); ?> "> <?php !EMPTY_TRASH_DAYS ? _e('Delete Permanently', 'message-board') : _e('Move to Trash', 'message-board'); ?> </a> <?php } ?> </div><!-- #delete-action --> <div id="publishing-action"> <span class="spinner"></span> <?php if (0 == $post->ID || !in_array($post_status, $allowed_stati)) { ?> <input name="original_publish" type="hidden" id="original_publish" value="<?php esc_attr_e('Publish', 'message-board'); ?> " /> <?php submit_button(__('Publish', 'message-board'), 'primary button-large', 'mb-publish', false, array('accesskey' => 'p')); ?> <?php } else { ?> <input name="original_publish" type="hidden" id="original_publish" value="<?php esc_attr_e('Update', 'message-board'); ?> " /> <input name="save" type="submit" class="button button-primary button-large" id="publish" accesskey="p" value="<?php esc_attr_e('Update', 'message-board'); ?> " /> <?php } ?> </div><!-- #publishing-action --> <div class="clear"></div> </div><!-- #major-publishing-actions --> </div><!-- #submitpost --> <?php }