/** * Adds a custom filter on 'request' when viewing the edit menu items screen in the admin. * * @since 1.0.0 * @access public * @return void */ public function load_edit() { /* Get the current screen object. */ $screen = get_current_screen(); /* Get the reply post type name. */ $reply_type = mb_get_reply_post_type(); /* Bail if we're not on the edit topic screen. */ if (!empty($screen->post_type) && $screen->post_type !== $reply_type) { return; } /* Custom action for loading the edit screen. */ do_action('mb_load_edit_reply'); /* Filter the `request` vars. */ add_filter('request', array($this, 'request')); /* Enqueue custom styles. */ add_action('admin_enqueue_scripts', array($this, 'print_styles')); /* Add custom admin notices. */ add_action('admin_notices', array($this, 'admin_notices')); /* Filter the bulk actions. */ add_filter("bulk_actions-{$screen->id}", array($this, 'bulk_actions')); /* Handle custom columns. */ add_filter("manage_edit-{$reply_type}_columns", array($this, 'edit_columns')); add_filter("manage_edit-{$reply_type}_sortable_columns", array($this, 'manage_sortable_columns')); add_action("manage_{$reply_type}_posts_custom_column", array($this, 'manage_columns'), 10, 2); /* Filter the row actions. */ add_filter('post_row_actions', array($this, 'row_actions'), 10, 2); /* Filter post states (shown next to post title). */ add_filter('display_post_states', array($this, 'display_post_states'), 0, 2); }
function mb_set_user_reply_count($user_id) { global $wpdb; // @todo check all public reply statuses $where = $wpdb->prepare("WHERE post_author = %d AND post_type = %s AND post_status = %s", $user_id, mb_get_reply_post_type(), mb_get_publish_post_status()); $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}"); update_user_meta($user_id, mb_get_user_reply_count_meta_key(), $count); return $count; }
/** * Overwrites capabilities in certain scenarios. * * @since 1.0.0 * @access public * @param array $caps * @param string $cap * @param int $user_id * @param array $args * @return array */ function mb_reply_map_meta_cap($caps, $cap, $user_id, $args) { /* Checks if a user can read a specific reply. */ if ('read_post' === $cap && mb_is_reply($args[0])) { $post = get_post($args[0]); /* Only run our code if the user isn't the post author. */ if ($user_id != $post->post_author) { $topic_id = $post->post_parent; /* If we have a topic and the user can't read it, don't allow reading the reply. */ if (0 < $topic_id && !user_can($user_id, 'read_post', $topic_id)) { $caps = array('do_not_allow'); /* If the user can read the topic, check if they can read the reply. */ } else { $post_type = get_post_type_object($post->post_type); if ($post_type->cap->read !== $post_type->cap->read_others_replies) { $caps[] = $post_type->cap->read_others_replies; } else { $caps = array(); } } } else { $caps = array(); } /* Meta cap for editing a single reply. */ } elseif ('edit_post' === $cap && mb_is_reply($args[0])) { $post = get_post($args[0]); $reply_obj = get_post_type_object(mb_get_reply_post_type()); // Spam topics if (mb_is_reply_spam($args[0])) { $caps[] = $reply_obj->cap->edit_spam_replies; } /* Meta cap for spamming a single reply. */ } elseif ('spam_reply' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_reply', $args[0]) ? 'spam_replies' : 'do_not_allow'; /* Meta cap check for accessing the reply form. */ } elseif ('access_reply_form' === $cap) { $caps = array('create_replies'); if (mb_is_single_topic()) { $topic_id = mb_get_topic_id(); $topic_status = mb_get_topic_status($topic_id); $topic_type = mb_get_topic_type($topic_id); if (!current_user_can('read_topic', $topic_id)) { $caps[] = 'do_not_allow'; } elseif (!mb_topic_allows_replies($topic_id)) { $caps[] = 'do_not_allow'; } } elseif (mb_is_reply_edit() && !user_can($user_id, 'edit_post', mb_get_reply_id())) { $caps[] = 'do_not_allow'; } } return $caps; }
/** * Callback function for the `load-post.php` or `load-post-new.php` screen. * * @since 1.0.0 * @access public * @return void */ function load_post() { $screen = get_current_screen(); if (empty($screen->post_type) || $screen->post_type !== mb_get_reply_post_type()) { return; } if ('load-post-new.php' === current_filter()) { wp_safe_redirect(esc_url(add_query_arg('post_type', $screen->post_type, admin_url('edit.php')))); exit; } add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts')); add_action("add_meta_boxes_{$screen->post_type}", array($this, 'add_meta_boxes')); }
/** * Sets up needed actions/filters for the admin to initialize. * * @since 1.0.0 * @access public * @return void */ public function __construct() { /* Get post type names. */ $this->forum_type = mb_get_forum_post_type(); $this->topic_type = mb_get_topic_post_type(); $this->reply_type = mb_get_reply_post_type(); /* Add admin menu items. */ add_action('admin_menu', array($this, 'admin_menu')); /* Correct parent file. */ add_filter('parent_file', array($this, 'parent_file')); /* Admin notices. */ add_action('admin_notices', array($this, 'admin_notices')); /* Register scripts and styles. */ add_action('admin_enqueue_scripts', array($this, 'register_scripts')); /* Add custom body class. */ add_filter('admin_body_class', array($this, 'admin_body_class')); /* Overwrite the nav menu meta box object query. */ add_filter('nav_menu_meta_box_object', array($this, 'nav_menu_meta_box_object')); /* Edit screen views. */ foreach (mb_get_post_types() as $post_type) { add_filter("views_edit-{$post_type}", array($this, 'views_edit'), 5); } }
/** * Overwrites the main query depending on the situation. * * @since 1.0.0 * @access public * @param object $query * @return void */ function mb_pre_get_posts($query) { /* If viewing an admin page or this isn't the main query, bail. */ if (is_admin() || !$query->is_main_query()) { return; } /* If viewing the forum archive page. */ if (mb_is_forum_archive()) { $statuses = array(mb_get_open_post_status(), mb_get_close_post_status(), mb_get_publish_post_status(), mb_get_private_post_status(), mb_get_archive_post_status()); if (current_user_can('read_hidden_forums')) { $statuses[] = mb_get_hidden_post_status(); } $query->set('post_type', mb_get_forum_post_type()); $query->set('post_status', $statuses); $query->set('posts_per_page', mb_get_forums_per_page()); $query->set('orderby', array('menu_order' => 'ASC', 'title' => 'ASC')); $query->set('post_parent', 0); add_filter('the_posts', 'mb_posts_hierarchy_filter', 10, 2); } elseif (mb_is_topic_archive()) { $statuses = array(mb_get_open_post_status(), mb_get_close_post_status(), mb_get_publish_post_status(), mb_get_private_post_status()); if (current_user_can('read_hidden_topics')) { $statuses[] = mb_get_hidden_post_status(); } $query->set('post_type', mb_get_topic_post_type()); $query->set('post_status', $statuses); $query->set('posts_per_page', mb_get_topics_per_page()); $query->set('order', 'DESC'); $query->set('orderby', 'menu_order'); add_filter('the_posts', 'mb_posts_super_filter', 10, 2); } elseif (mb_is_user_page()) { /* Single user forums created page. */ if (mb_is_user_page('forums')) { $query->set('post_type', mb_get_forum_post_type()); $query->set('posts_per_page', mb_get_forums_per_page()); $query->set('order', 'ASC'); $query->set('orderby', 'title'); /* Single user topics created page. */ } elseif (mb_is_user_page('topics')) { $query->set('post_type', mb_get_topic_post_type()); $query->set('posts_per_page', mb_get_topics_per_page()); $query->set('order', 'DESC'); $query->set('orderby', 'menu_order'); /* Single user replies created page. */ } elseif (mb_is_user_page('replies')) { $query->set('post_type', mb_get_reply_post_type()); $query->set('posts_per_page', mb_get_replies_per_page()); $query->set('order', 'DESC'); $query->set('orderby', 'date'); /* Single user bookmarks saved page. */ } elseif (mb_is_user_page('bookmarks')) { $user = get_user_by('slug', get_query_var('author_name')); $favs = get_user_meta($user->ID, mb_get_user_topic_bookmarks_meta_key(), true); $favs = wp_parse_id_list($favs); /* Empty array with `post_in` hack. @link https://core.trac.wordpress.org/ticket/28099 */ if (empty($favs)) { $favs = array(0); } $query->set('post__in', $favs); $query->set('post_type', mb_get_topic_post_type()); $query->set('posts_per_page', mb_get_topics_per_page()); $query->set('order', 'DESC'); $query->set('orderby', 'menu_order'); add_filter('posts_where', 'mb_auth_posts_where', 10, 2); /* Single user topic subscriptions page. */ } elseif (mb_is_user_page('topic-subscriptions')) { $user = get_user_by('slug', get_query_var('author_name')); $subs = mb_get_user_topic_subscriptions($user->ID); /* Empty array with `post_in` hack. @link https://core.trac.wordpress.org/ticket/28099 */ if (empty($subs)) { $subs = array(0); } $query->set('post__in', $subs); $query->set('post_type', mb_get_topic_post_type()); $query->set('posts_per_page', mb_get_topics_per_page()); $query->set('order', 'DESC'); $query->set('orderby', 'menu_order'); add_filter('posts_where', 'mb_auth_posts_where', 10, 2); /* Single user forum subscriptions page. */ } elseif (mb_is_user_page('forum-subscriptions')) { $user = get_user_by('slug', get_query_var('author_name')); $subs = mb_get_user_forum_subscriptions($user->ID); /* Empty array with `post_in` hack. @link https://core.trac.wordpress.org/ticket/28099 */ if (empty($subs)) { $subs = array(0); } $query->set('post__in', $subs); $query->set('post_type', mb_get_forum_post_type()); $query->set('posts_per_page', mb_get_forums_per_page()); $query->set('order', 'DESC'); $query->set('orderby', 'menu_order'); add_filter('posts_where', 'mb_auth_posts_where', 10, 2); } } elseif (mb_is_search_results()) { $post_type = $query->get('post_type'); /* If not searching a specific post type, make sure to search all forum-related post types. */ if (empty($post_type) || 'any' === $post_type || !isset($_GET['mb_search_mode'])) { $query->set('post_type', array(mb_get_forum_post_type(), mb_get_topic_post_type(), mb_get_reply_post_type())); } $query->set('post_status', array(mb_get_open_post_status(), mb_get_close_post_status(), mb_get_publish_post_status())); $query->set('posts_per_page', mb_get_topics_per_page()); } elseif (mb_is_user_archive() && get_query_var('mb_role')) { $role = get_query_var('mb_role'); if ($role && in_array("mb_{$role}", array_keys(mb_get_dynamic_roles()))) { $query->set('mb_role', "mb_{$role}"); } } elseif ($query->is_single && isset($query->query_vars['post_type']) && in_array($query->query_vars['post_type'], array(mb_get_forum_post_type(), mb_get_topic_post_type(), mb_get_reply_post_type()))) { add_filter('the_posts', 'mb_posts_can_read_parent'); } }
/** * Callback function on the `before_delete_post` hook for when a post is deleted. This sets up some * specific actions based on our post types. It also saves the deleted post object for later use in * those actions. * * @since 1.0.0 * @access public * @param int $post_id * @return void */ function mb_before_delete_post($post_id) { $forum_type = mb_get_forum_post_type(); $topic_type = mb_get_topic_post_type(); $reply_type = mb_get_reply_post_type(); $post_type = get_post_type($post_id); /* WP doesn't pass the post object after a post has been deleted, so we need to save it temporarily. */ if (in_array($post_type, array($forum_type, $topic_type, $reply_type))) { message_board()->deleted_post = get_post($post_id); } /* If a forum is being deleted. */ if ($forum_type === $post_type) { /* If this is the default forum, stop everything. */ if (mb_get_default_forum_id() === $post_id) { wp_die('Whoah there! This is the default forum and cannot be deleted. Visit the settings page to change the default forum.', 'message-board'); } add_action('after_delete_post', 'mb_after_delete_forum'); /* If a topic is being deleted. */ } elseif ($topic_type === $post_type) { add_action('after_delete_post', 'mb_after_delete_topic'); /* If a reply is being deleted. */ } elseif ($reply_type === $post_type) { add_action('after_delete_post', 'mb_after_delete_reply'); } }
/** * Handles the output for custom columns. * * @since 1.0.0 * @access public * @param string $column * @param string $column_name * @param int $post_id */ public function custom_column($column, $column_name, $user_id) { /* Post status column. */ if ('topics' === $column_name) { $user_id = mb_get_user_id($user_id); $topic_count = mb_get_user_topic_count($user_id); /* If the current user can create topics, link the topic count back to the edit topics screen. */ if (!empty($topic_count) && current_user_can('create_topics')) { $url = add_query_arg(array('post_type' => mb_get_topic_post_type(), 'author' => $user_id), admin_url('edit.php')); $column = sprintf('<a href="%s" title="%s">%s</a>', esc_url($url), __('View topics by this user', 'message-board'), $topic_count); /* Else, display the count. */ } else { $column = !empty($topic_count) ? $topic_count : number_format_i18n(0); } /* Replies column. */ } elseif ('replies' === $column_name) { $user_id = mb_get_user_id($user_id); $reply_count = mb_get_user_reply_count($user_id); /* If the current user can create replies, link the topic count back to the edit replies screen. */ if (!empty($reply_count) && current_user_can('create_replies')) { $url = add_query_arg(array('post_type' => mb_get_reply_post_type(), 'author' => $user_id), admin_url('edit.php')); $column = sprintf('<a href="%s" title="%s">%s</a>', esc_url($url), __('View replies by this user', 'message-board'), $reply_count); /* Else, display the count. */ } else { $column = !empty($reply_count) ? $reply_count : number_format_i18n(0); } } /* Return the filtered column. */ return $column; }
function mb_get_edit_form() { $edit = get_query_var('edit'); if (empty($edit)) { return ''; } $post = get_post($edit); if (empty($post) || mb_get_topic_post_type() !== $post->post_type && mb_get_reply_post_type() !== $post->post_type) { return; } $edit = $post->ID; if (!current_user_can('edit_post', $edit)) { return; } $pt_object = get_post_type_object($post->post_type); $form = sprintf('<form id="edit-post-form" method="post" action="%s">', mb_get_edit_form_action_url()); $form .= '<fieldset>'; $form .= sprintf('<legend>%s</legend>', $pt_object->labels->edit_item); // title field if (mb_get_topic_post_type() === $post->post_type) { $default_fields['title'] = '<p>'; $default_fields['title'] .= sprintf('<label for="mb_post_title">%s</label>', __('Topic title: (be brief and descriptive)', 'message-board')); $default_fields['title'] .= sprintf('<input type="text" id="mb_post_title" name="mb_post_title" value="%s" />', esc_attr($post->post_title)); $default_fields['title'] .= '</p>'; $terms = get_the_terms($edit, 'forum'); $forum = array_shift($terms); $default_fields['forum'] = '<p>'; $default_fields['forum'] .= sprintf('<label for="mb_post_forum">%s</label>', __('Select a forum:', 'message-board')); $default_fields['forum'] .= wp_dropdown_categories(array('name' => 'mb_post_forum', 'id' => 'mb_post_forum', 'selected' => absint($forum->term_id), 'hierarchical' => true, 'orderby' => 'name', 'hide_empty' => false, 'hide_if_empty' => true, 'taxonomy' => 'forum', 'echo' => false)); $default_fields['forum'] .= '</p>'; } // content field $default_fields['content'] = '<p>'; $default_fields['content'] .= sprintf('<label for="mb_post_content" name="mb_post_content">%s</label>', __('Please put code in between <code>`backtick`</code> characters.', 'message-board')); $default_fields['content'] .= sprintf('<textarea id="mb_post_content" name="mb_post_content">%s</textarea>', format_to_edit(mb_code_trick_reverse($post->post_content))); $default_fields['content'] .= '</p>'; $default_fields = apply_filters('mb_edit_form_fields', $default_fields); foreach ($default_fields as $key => $field) { $form .= $field; } $form .= sprintf('<p><input type="submit" value="%s" /></p>', esc_attr__('Submit', 'message-board')); $form .= sprintf('<input type="hidden" name="mb_post_id" value="%s" />', absint($edit)); $form .= wp_nonce_field('mb_edit_post_action', 'mb_edit_post_nonce', false, false); $form .= '</fieldset>'; $form .= '</form>'; return apply_filters('mb_get_edit_form', $form); }
/** * Filter on `body_class` to add custom classes for the plugin's pages on the front end. * * @todo Remove `bbpress` class. * @todo Decide on class naming system. * * @since 1.0.0 * @access public * @param array $classes * @return array */ function mb_body_class($classes) { global $wp; if (mb_is_message_board()) { $classes[] = 'mb'; $classes[] = 'bbpress'; // temporary class for compat $forum_type = mb_get_forum_post_type(); $topic_type = mb_get_topic_post_type(); $reply_type = mb_get_reply_post_type(); $_classes = $classes; $remove = array("single-{$forum_type}", "single-{$topic_type}", "single-{$reply_type}", "singular-{$forum_type}", "singular-{$topic_type}", "singular-{$reply_type}", "archive-{$forum_type}", "archive-{$topic_type}", "archive-{$reply_type}"); foreach ($_classes as $class_key => $class_value) { if (in_array($class_value, $remove)) { unset($classes[$class_key]); } } if (mb_is_forum_front()) { $classes[] = 'forum-front'; } elseif (mb_is_single_forum()) { $classes[] = 'single-forum'; } elseif (mb_is_single_topic()) { $classes[] = 'single-topic'; } elseif (mb_is_single_reply()) { $classes[] = 'single-reply'; } elseif (mb_is_single_role()) { $classes[] = 'single-role'; } elseif (mb_is_forum_archive()) { $classes[] = 'archive-forum'; } elseif (mb_is_topic_archive()) { $classes[] = 'archive-topic'; } elseif (mb_is_reply_archive()) { $classes[] = 'archive-reply'; } elseif (mb_is_role_archive()) { $classes[] = 'archive-role'; } elseif (mb_is_user_archive()) { $classes[] = 'archive-user'; } } return $classes; }
/** * Handles the output for custom columns. * * @since 1.0.0 * @access public * @param string $column * @param int $post_id */ public function manage_columns($column, $post_id) { switch ($column) { /* Post status column. */ case 'status': $post_type = mb_get_forum_post_type(); $status = get_post_status_object(mb_get_forum_status($post_id)); /* If the forum has the "publish" post status, change it to "open". */ if (mb_get_publish_post_status() === $status->name) { wp_update_post(array('ID' => $post_id, 'post_status' => mb_get_open_post_status())); } $url = add_query_arg(array('post_status' => $status->name, 'post_type' => $post_type), admin_url('edit.php')); printf('<a href="%s">%s</a>', $url, $status->label); break; /* Forum type column. */ /* Forum type column. */ case 'type': $post_type = mb_get_forum_post_type(); $forum_type = mb_get_forum_type_object(mb_get_forum_type($post_id)); $url = add_query_arg(array('post_type' => $post_type, 'forum_type' => $forum_type->name), admin_url('edit.php')); printf('<a href="%s">%s</a>', $url, $forum_type->label); break; /* Topic count column. */ /* Topic count column. */ case 'subforums': $subforum_count = mb_get_forum_subforum_count($post_id); $subforum_count = !empty($subforum_count) ? absint($subforum_count) : number_format_i18n(0); if (0 < $subforum_count) { printf('<a href="%s">%s</a>', add_query_arg(array('post_type' => mb_get_forum_post_type(), 'post_parent' => $post_id), admin_url('edit.php')), $subforum_count); } else { echo $subforum_count; } break; /* Topic count column. */ /* Topic count column. */ case 'topics': $topic_count = mb_get_forum_topic_count($post_id); $topic_count = !empty($topic_count) ? absint($topic_count) : number_format_i18n(0); if (0 < $topic_count && current_user_can('edit_topics')) { printf('<a href="%s">%s</a>', add_query_arg(array('post_type' => mb_get_topic_post_type(), 'post_parent' => $post_id), admin_url('edit.php')), $topic_count); } else { echo $topic_count; } break; /* Reply count column. */ /* Reply count column. */ case 'replies': $reply_count = mb_get_forum_reply_count($post_id); $reply_count = !empty($reply_count) ? absint($reply_count) : number_format_i18n(0); if (0 < $reply_count && current_user_can('edit_replies')) { printf('<a href="%s">%s</a>', add_query_arg(array('post_type' => mb_get_reply_post_type(), 'mb_forum' => $post_id), admin_url('edit.php')), $reply_count); } else { echo $reply_count; } break; /* Datetime column. */ /* Datetime column. */ case 'datetime': the_time(get_option('date_format')); echo '<br />'; the_time(get_option('time_format')); break; /* Just break out of the switch statement for everything else. */ /* Just break out of the switch statement for everything else. */ default: break; } }
function mb_get_reply_label($label) { $labels = get_post_type_object(mb_get_reply_post_type())->labels; return $labels->{$label}; }
/** * Removes the ability to add a new forum topic from the admin bar. * * @since 1.0.0 * @access public * @global object $wp_admin_bar * @return void */ function mb_admin_bar() { global $wp_admin_bar; $wp_admin_bar->remove_menu('new-' . mb_get_reply_post_type()); }
/** * Forum activity dashboard widget. * * @since 1.0.0 * @access public * @return void */ function mb_dashboard_activity_meta_box() { $statuses = mb_get_published_post_statuses(); $post_types = array(mb_get_forum_post_type(), mb_get_topic_post_type(), mb_get_reply_post_type()); ?> <div class="count-block"> <span class="dashicons dashicons-format-chat"></span> <ul> <?php foreach ($post_types as $type) { $count = 0; $num_posts = wp_count_posts($type); foreach ((array) $num_posts as $status => $num) { if (in_array($status, $statuses)) { $count = $count + absint($num); } } $post_type_object = get_post_type_object($type); $text = translate_nooped_plural($post_type_object->labels->mb_dashboard_count, $count, 'message-board'); $text = sprintf($text, number_format_i18n($count)); $class = sanitize_html_class('mb-' . mb_translate_post_type($type) . '-count'); if ($post_type_object && current_user_can($post_type_object->cap->edit_posts)) { printf('<li><a class="%s" href="%s">%s</a></li>', $class, add_query_arg('post_type', $type, admin_url('edit.php')), $text); } else { printf('<li><span class="%s">%s</span></li>', $class, $text); } } ?> </ul> </div><!-- .count-block --> <div id="mb-activity-widget"> <?php $args = array('posts_per_page' => 7, 'post_type' => $post_types, 'post_status' => $statuses, 'order' => 'DESC', 'orderby' => 'date', 'no_found_rows' => true, 'cache_results' => false, 'perm' => 'readable'); $loop = new WP_Query($args); if ($loop->have_posts()) { ?> <div class="activity-block"> <h4><?php _e('Recently Published', 'message-board'); ?> </h4> <ul> <?php $today = date('Y-m-d', current_time('timestamp')); $tomorrow = date('Y-m-d', strtotime('+1 day', current_time('timestamp'))); ?> <?php while ($loop->have_posts()) { ?> <?php $loop->the_post(); $time = get_the_time('U'); if (date('Y-m-d', $time) == $today) { $relative = __('Today'); } else { /* translators: date and time format for recent posts on the dashboard, see http://php.net/date */ $relative = date_i18n(__('M jS'), $time); } $url = current_user_can('edit_post', get_the_ID()) ? get_edit_post_link() : get_permalink(); $format = __('<span>%1$s, %2$s</span> %3$s', 'message-board'); $link = sprintf('<a href="%s">%s</a>', $url, get_the_title()); printf("<li>{$format}</li>", $relative, get_the_time(), $link); } ?> </ul> </div><!-- .activity-block --> <?php } ?> <?php wp_reset_postdata(); ?> </div> <?php }
/** * Post updated messages in the admin. * * @since 1.0.0 * @access public * @return void */ function mb_post_updated_messages($messages) { global $post, $post_ID; $forum_type = mb_get_forum_post_type(); $topic_type = mb_get_topic_post_type(); $reply_type = mb_get_reply_post_type(); $messages[$forum_type] = array(0 => '', 1 => sprintf(__('Forum updated. <a href="%s">View forum</a>', 'message-board'), esc_url(get_permalink($post_ID))), 2 => '', 3 => '', 4 => __('Forum updated.', 'message-board'), 5 => isset($_GET['revision']) ? sprintf(__('Forum restored to revision from %s', 'message-board'), wp_post_revision_title((int) $_GET['revision'], false)) : false, 6 => sprintf(__('Forum published. <a href="%s">View forum</a>', 'message-board'), esc_url(get_permalink($post_ID))), 7 => __('Forum saved.', 'message-board'), 8 => '', 9 => '', 10 => ''); $messages[$topic_type] = array(0 => '', 1 => sprintf(__('Topic updated. <a href="%s">View topic</a>', 'message-board'), esc_url(get_permalink($post_ID))), 2 => '', 3 => '', 4 => __('Topic updated.', 'message-board'), 5 => isset($_GET['revision']) ? sprintf(__('Topic restored to revision from %s', 'message-board'), wp_post_revision_title((int) $_GET['revision'], false)) : false, 6 => sprintf(__('Topic published. <a href="%s">View topic</a>', 'message-board'), esc_url(get_permalink($post_ID))), 7 => __('Topic saved.', 'message-board'), 8 => '', 9 => '', 10 => ''); $messages[$reply_type] = array(0 => '', 1 => sprintf(__('Reply updated. <a href="%s">View reply</a>', 'message-board'), esc_url(get_permalink($post_ID))), 2 => '', 3 => '', 4 => __('Reply updated.', 'message-board'), 5 => isset($_GET['revision']) ? sprintf(__('Reply restored to revision from %s', 'message-board'), wp_post_revision_title((int) $_GET['revision'], false)) : false, 6 => sprintf(__('Reply published. <a href="%s">View reply</a>', 'message-board'), esc_url(get_permalink($post_ID))), 7 => __('Reply saved.', 'message-board'), 8 => '', 9 => '', 10 => ''); return $messages; }
/** * Resets the topic voices. * * @since 1.0.0 * @access public * @param int $topic_id * @return array */ function mb_reset_topic_voices($topic_id) { global $wpdb; $voices = $wpdb->get_col($wpdb->prepare("SELECT post_author FROM {$wpdb->posts} WHERE post_parent = %d AND post_type = %s AND post_status = %s", absint($topic_id), mb_get_reply_post_type(), mb_get_publish_post_status())); $topic_author = mb_get_topic_author_id($topic_id); $voices = array_merge(array($topic_author), (array) $voices); $voices = array_unique($voices); mb_set_topic_voices($topic_id, $voices); mb_set_topic_voice_count($topic_id, count($voices)); return $voices; }
/** * Handles the output for custom columns. * * @since 1.0.0 * @access public * @param string $column * @param int $post_id */ public function manage_columns($column, $post_id) { /* Post status column. */ if ('status' === $column) { $post_type = mb_get_topic_post_type(); $status = get_post_status_object(mb_get_topic_status($post_id)); if (mb_get_publish_post_status() === $status->name) { wp_update_post(array('ID' => $post_id, 'post_status' => mb_get_open_post_status())); } $url = add_query_arg(array('post_status' => $status->name, 'post_type' => $post_type), admin_url('edit.php')); printf('<a href="%s">%s</a>', $url, $status->label); /* Topic forum column. */ } elseif ('forum' === $column) { $post_type = mb_get_topic_post_type(); $forum_id = mb_get_topic_forum_id($post_id); $url = add_query_arg(array('post_type' => $post_type, 'post_parent' => $forum_id), admin_url('edit.php')); printf('<a href="%s">%s</a>', $url, mb_get_forum_title($forum_id)); /* Replies column. */ } elseif ('replies' === $column) { $reply_count = mb_get_topic_reply_count($post_id); $reply_count = !empty($reply_count) ? absint($reply_count) : number_format_i18n(0); if (0 < $reply_count && current_user_can('edit_replies')) { printf('<a href="%s">%s</a>', add_query_arg(array('post_type' => mb_get_reply_post_type(), 'post_parent' => $post_id), admin_url('edit.php')), $reply_count); } else { echo $reply_count; } /* Voices column. */ } elseif ('voices' === $column) { $voice_count = mb_get_topic_voice_count($post_id); echo !empty($voice_count) ? absint($voice_count) : number_format_i18n(0); /* Datetime column. */ } elseif ('datetime' === $column) { the_time(get_option('date_format')); echo '<br />'; the_time(get_option('time_format')); } }
/** * Notifies users by email when a new post is published. By default, notifications are sent to users * who are subscribed to a particular forum or a particular topic. * * @since 1.0.0 * @access public * @param object|int $post * @return void */ function mb_notify_subscribers($post) { $post = !is_object($post) ? get_post($post) : $post; $forum_type = mb_get_forum_post_type(); $topic_type = mb_get_topic_post_type(); $reply_type = mb_get_reply_post_type(); if (!in_array($post->post_type, array($forum_type, $topic_type, $reply_type))) { return; } do_action('mb_before_notify_subscribers', $post); if ($topic_type === $post->post_type) { //mb_notify_forum_subscribers( $post ); } elseif ($reply_type === $post->post_type) { //mb_notify_forum_subscribers( $post ); mb_notify_topic_subscribers($post->post_parent, $post); } do_action('mb_after_notify_subscribers', $post); }
function mb_reset_reply_positions($topic_id) { global $wpdb; $topic_id = mb_get_topic_id($topic_id); $replies = $wpdb->get_results($wpdb->prepare("SELECT ID, menu_order FROM {$wpdb->posts} WHERE post_type = %s AND post_status = %s AND post_parent = %d ORDER BY post_date ASC", mb_get_reply_post_type(), mb_get_publish_post_status(), $topic_id)); if (empty($replies)) { return false; } $reply_ids = array(); $i = 0; $update_sql = "UPDATE {$wpdb->posts} SET menu_order = CASE ID"; foreach ($replies as $reply) { $i++; $reply_ids[] = $reply->ID; $update_sql .= sprintf(" WHEN %d THEN %d", $reply->ID, $i); } $update_sql .= " END WHERE ID IN (" . implode(',', $reply_ids) . ")"; $wpdb->query($update_sql); }