function mb_dropdown_post_status($args = array()) { $defaults = array('post_type' => mb_get_forum_post_type(), 'exclude' => '', 'name' => 'post_status', 'id' => 'post_status', 'selected' => '', 'echo' => true); $args = wp_parse_args($args, $defaults); $post_type_object = get_post_type_object($args['post_type']); if (mb_get_forum_post_type() === $args['post_type']) { $stati = apply_filters('mb_select_forum_post_statuses', mb_get_forum_post_statuses()); } elseif (mb_get_topic_post_type() === $args['post_type']) { $stati = apply_filters('mb_select_topic_post_statuses', mb_get_topic_post_statuses()); } elseif (mb_get_reply_post_type() === $args['post_type']) { $stati = apply_filters('mb_select_reply_post_statuses', mb_get_reply_post_statuses()); } if (is_array($args['exclude'])) { $stati = array_diff($stati, $args['exclude']); } $out = sprintf('<select name="%s" id="%s">', sanitize_html_class($args['name']), sanitize_html_class($args['id'])); $current_status_object = get_post_status_object($args['selected']); $current_status_cap = isset($current_status_object->mb_capability) ? $current_status_object->mb_capability : 'edit_posts'; $current_status_cap = $post_type_object->cap->{$current_status_cap}; if (!empty($args['selected']) && !current_user_can($current_status_cap)) { $status_obj = $current_status_object; $out .= sprintf('<option value="%s"%s>%s</option>', esc_attr($status_obj->name), selected($status_obj->name, $args['selected'], false), $status_obj->label); } else { foreach ($stati as $status) { $status_obj = get_post_status_object($status); $status_cap = isset($status_obj->mb_capability) ? $status_obj->mb_capability : 'edit_posts'; $status_cap = $post_type_object->cap->{$status_cap}; if (!current_user_can($status_cap) || false === $status_obj->mb_show_in_status_select) { continue; } $out .= sprintf('<option value="%s"%s>%s</option>', esc_attr($status_obj->name), selected($status_obj->name, $args['selected'], false), $status_obj->label); } } $out .= '</select>'; if (!$args['echo']) { return $out; } echo $out; }
/** * Callback function for the `transition_post_status` hook. This function saves the previous post status * as metadata. It also adds actions for more specific status changes. * * @since 1.0.0 * @access public * @param string $new_status * @param string $old_status * @param object $post * @return void */ function mb_transition_post_status($new_status, $old_status, $post) { /* Get post types. */ $forum_type = mb_get_forum_post_type(); $topic_type = mb_get_topic_post_type(); $reply_type = mb_get_reply_post_type(); /* If not one of our post types, bail. */ if (!in_array($post->post_type, array($forum_type, $topic_type, $reply_type))) { return; } /* Keep track of the old post status by saving it as post meta. */ update_post_meta($post->ID, mb_get_prev_status_meta_key(), $old_status); /* Get post type statuses. */ $forum_statuses = mb_get_forum_post_statuses(); $topic_statuses = mb_get_topic_post_statuses(); $reply_statuses = mb_get_reply_post_statuses(); /* Get the post statuses we need to work with. */ $publish_status = mb_get_publish_post_status(); $open_status = mb_get_open_post_status(); $close_status = mb_get_close_post_status(); $private_status = mb_get_private_post_status(); $hidden_status = mb_get_hidden_post_status(); $spam_status = mb_get_spam_post_status(); $trash_status = mb_get_trash_post_status(); /* If old status is not one of our statuses but the new is, assume we're publishing for the first time. */ if ($forum_type === $post->post_type && !in_array($old_status, $forum_statuses) && in_array($new_status, $forum_statuses)) { mb_insert_forum_data($post); } elseif ($topic_type === $post->post_type && !in_array($old_status, $topic_statuses) && in_array($new_status, $topic_statuses)) { mb_insert_topic_data($post); } elseif ($reply_type === $post->post_type && !in_array($old_status, $reply_statuses) && in_array($new_status, $reply_statuses)) { mb_insert_reply_data($post); } /* Publish status change. */ add_action("{$publish_status}_to_{$spam_status}", 'mb_publish_to_spam'); add_action("{$publish_status}_to_{$trash_status}", 'mb_publish_to_trash'); /* Open status change. */ add_action("{$open_status}_to_{$spam_status}", 'mb_publish_to_spam'); add_action("{$open_status}_to_{$trash_status}", 'mb_publish_to_trash'); /* Close status change. */ add_action("{$close_status}_to_{$spam_status}", 'mb_close_to_spam'); add_action("{$close_status}_to_{$trash_status}", 'mb_close_to_trash'); /* Private status change. */ add_action("{$private_status}_to_{$spam_status}", 'mb_publish_to_spam'); add_action("{$private_status}_to_{$trash_status}", 'mb_publish_to_trash'); /* Hidden status change. */ add_action("{$hidden_status}_to_{$spam_status}", 'mb_publish_to_spam'); add_action("{$hidden_status}_to_{$trash_status}", 'mb_publish_to_trash'); /* Spam status change. */ add_action("{$spam_status}_to_{$publish_status}", 'mb_spam_to_publish'); add_action("{$spam_status}_to_{$open_status}", 'mb_spam_to_publish'); add_action("{$spam_status}_to_{$close_status}", 'mb_spam_to_close'); add_action("{$spam_status}_to_{$private_status}", 'mb_spam_to_publish'); add_action("{$spam_status}_to_{$hidden_status}", 'mb_spam_to_publish'); /* Trash status change. */ add_action("{$trash_status}_to_{$publish_status}", 'mb_trash_to_publish'); add_action("{$trash_status}_to_{$open_status}", 'mb_trash_to_publish'); add_action("{$trash_status}_to_{$close_status}", 'mb_trash_to_close'); add_action("{$trash_status}_to_{$private_status}", 'mb_trash_to_publish'); add_action("{$trash_status}_to_{$hidden_status}", 'mb_trash_to_publish'); }
/** * 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 }