/** * Get an array of user IDs for users who are bookmarkd to the topic. * * @since 1.0.0 * @access public * @param int $topic_id * @return array */ function mb_get_topic_bookmarkers($topic_id = 0) { $topic_id = mb_get_topic_id($topic_id); $users = wp_cache_get('mb_get_topic_bookmarkers_' . $topic_id, 'message-board-users'); if (false === $users) { global $wpdb; $users = $wpdb->get_col($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND FIND_IN_SET( '{$topic_id}', meta_value ) > 0", mb_get_user_topic_bookmarks_meta_key())); wp_cache_set('mb_get_topic_bookmarkers_' . $topic_id, $users, 'message-board-users'); } return apply_filters('mb_get_topic_bookmarkers', $users); }
/** * 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; }
/** * Filter on the `request` hook to change what posts are loaded. * * @since 1.0.0 * @access public * @param array $vars * @return array */ public function request($vars) { $new_vars = array(); /* Load replies of a specific topic. */ if (isset($_GET['post_parent'])) { $new_vars['post_parent'] = mb_get_topic_id($_GET['post_parent']); } elseif (isset($_GET['mb_forum'])) { $topic_ids = mb_get_forum_topic_ids(mb_get_forum_id($_GET['mb_forum'])); $new_vars['post_parent__in'] = (array) $topic_ids; } elseif (isset($vars['orderby']) && 'forum' === $vars['orderby']) { // @todo - Fix or remove //$new_vars['orderby'] = 'meta_value_num'; //$new_vars['meta_key'] = mb_get_reply_forum_id_meta_key(); } elseif (isset($vars['orderby']) && 'post_parent' === $vars['orderby']) { $new_vars['orderby'] = 'post_parent'; } elseif (isset($vars['orderby']) && 'post_author' === $vars['orderby']) { $new_vars['orderby'] = 'post_author'; } /* Return the vars, merging with the new ones. */ return array_merge($vars, $new_vars); }
/** * Creates a new reply query and checks if there are any replies found. * * @since 1.0.0 * @access public * @return bool */ function mb_reply_query() { $mb = message_board(); if (!is_null($mb->reply_query->query)) { $have_posts = $mb->reply_query->have_posts(); if (empty($have_posts)) { wp_reset_postdata(); } return $have_posts; } if (mb_is_reply_archive() || mb_is_single_reply() || mb_is_user_page('replies')) { global $wp_query; $mb->reply_query = $wp_query; } else { $per_page = mb_get_replies_per_page(); $defaults = array('post_type' => mb_get_reply_post_type(), 'post_status' => mb_get_publish_post_status(), 'posts_per_page' => $per_page, 'paged' => get_query_var('paged'), 'orderby' => 'menu_order', 'order' => 'ASC', 'hierarchical' => false, 'ignore_sticky_posts' => true); if ($mb->topic_query->in_the_loop || mb_is_single_topic()) { $defaults['post_parent'] = mb_get_topic_id(); } $mb->reply_query = new WP_Query($defaults); } return $mb->reply_query->have_posts(); }
function mb_notify_topic_subscribers($topic_id, $post) { $reply_id = mb_get_reply_id($post->ID); $topic_id = mb_get_topic_id($topic_id); $forum_id = mb_get_topic_forum_id($topic_id); $forum_subscribers = $forum_id ? mb_get_forum_subscribers($forum_id) : array(); $topic_subscribers = $topic_id ? mb_get_topic_subscribers($topic_id) : array(); /* Remove users who are already subscribed to the topic's forum or who wrote the post. */ $subscribers = array_diff($topic_subscribers, $forum_subscribers, array($post->post_author)); /* If there are no subscribers, bail. */ if (empty($subscribers)) { return false; } /* Get needed topic data. */ $topic_title = strip_tags(mb_get_topic_title($topic_id)); /* Get needed reply data. */ $reply_url = mb_get_reply_url($reply_id); $reply_author = mb_get_reply_author($reply_id); $reply_author_id = mb_get_reply_author_id($reply_id); $reply_content = mb_get_reply_content($reply_id, 'raw'); /* Filter the reply content for email. */ $reply_content = apply_filters('mb_pre_email_reply_content', $reply_content, $reply_id); /* Build the email message. */ $message = sprintf(__('%1$s replied: %4$s%2$s %4$sPost Link: %3$s %4$sYou are receiving this email because you subscribed to a forum topic. Log in and visit the topic to unsubscribe from these emails.', 'message-board'), $reply_author, $reply_content, $reply_url, "\n\n"); /* Get the site name and domain. */ $site_name = esc_html(strip_tags(get_option('blogname'))); $site_domain = untrailingslashit(str_replace(array('http://', 'https://'), '', home_url())); /* Who's the message from? */ $from = '<noreply@' . $site_domain . '>'; /* Translators: Email subject. 1 is the blog name. 2 is the topic title. */ $subject = sprintf(esc_attr__('[$1%s] $2%s', 'message-board'), $site_name, $topic_title); /* Build the email headers. */ $headers = array(); $headers[] = sprintf('From: %s %s', $site_name, $from); foreach ($subscribers as $user_id) { $headers[] = 'Bcc: ' . get_userdata($user_id)->user_email; } /* Send the email. */ return wp_mail($from, $subject, $message, $headers); }
/** * 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_topic_map_meta_cap($caps, $cap, $user_id, $args) { /* Checks if a user can read a specific topic. */ if ('read_post' === $cap && mb_is_topic($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) { $forum_id = $post->post_parent; /* If we have a forum and the user can't read it, don't allow reading the topic. */ if (0 < $forum_id && !mb_user_can($user_id, 'read_forum', $forum_id)) { $caps = array('do_not_allow'); /* If the user can read the forum, check if they can read the topic. */ } else { $post_type = get_post_type_object($post->post_type); $post_status = mb_get_topic_status($post->ID); $status_obj = get_post_status_object($post_status); if (mb_get_hidden_post_status() === $status_obj->name) { $caps[] = $post_type->cap->read_hidden_topics; } elseif (mb_get_private_post_status() === $status_obj->name) { $caps[] = $post_type->cap->read_private_posts; } elseif ($post_type->cap->read !== $post_type->cap->read_others_topics) { $caps[] = $post_type->cap->read_others_topics; } else { $caps = array(); } //$caps[] = $post_type->cap->read; } } else { $caps = array(); } /* Meta cap for editing a single topic. */ } elseif ('edit_post' === $cap && mb_is_topic($args[0])) { $post = get_post($args[0]); $topic_obj = get_post_type_object(mb_get_topic_post_type()); if ($user_id != $post->post_author) { // Open topics. if (mb_is_topic_open($args[0])) { $caps[] = $topic_obj->cap->edit_open_topics; } elseif (mb_is_topic_closed($args[0])) { $caps[] = $topic_obj->cap->edit_closed_topics; } elseif (mb_is_topic_hidden($args[0])) { $caps[] = $topic_obj->cap->edit_hidden_topics; } } // Spam topics if (mb_is_topic_spam($args[0])) { $caps[] = $topic_obj->cap->edit_spam_topics; } elseif (mb_is_topic_orphan($args[0])) { $caps[] = $topic_obj->cap->edit_orphan_topics; } /* Meta cap for opening a single topic. */ } elseif ('open_topic' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'open_topics' : 'do_not_allow'; /* Meta cap for closing a single topic. */ } elseif ('close_topic' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'close_topics' : 'do_not_allow'; /* Meta cap for privatizing a single topic. */ } elseif ('privatize_topic' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'privatize_topics' : 'do_not_allow'; /* Meta cap for hiding a single topic. */ } elseif ('hide_topic' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'hide_topics' : 'do_not_allow'; /* Meta cap for spamming a single topic. */ } elseif ('spam_topic' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'spam_topics' : 'do_not_allow'; /* Meta cap for spamming a single topic. */ } elseif ('super_topic' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'super_topics' : 'do_not_allow'; /* Meta cap for spamming a single topic. */ } elseif ('stick_topic' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'stick_topics' : 'do_not_allow'; /* Meta cap check for accessing the topic form. */ } elseif ('access_topic_form' === $cap) { $caps = array('create_topics'); if (mb_is_single_forum()) { $forum_id = mb_get_forum_id(); if (!current_user_can('read_forum', $forum_id)) { $caps[] = 'do_not_allow'; } elseif (!mb_forum_allows_topics($forum_id)) { $caps[] = 'do_not_allow'; } } elseif (mb_is_topic_edit() && !user_can($user_id, 'edit_post', mb_get_topic_id())) { $caps[] = 'do_not_allow'; } } return $caps; }
/** * Resets the topic reply count. * * @since 1.0.0 * @access public * @param int $topic_id * @global object $wpdb * @return array */ function mb_reset_topic_reply_count($topic_id) { global $wpdb; $topic_id = mb_get_topic_id($topic_id); $publish_status = mb_get_publish_post_status(); $where = $wpdb->prepare("WHERE post_parent = %d AND post_type = %s", $topic_id, mb_get_reply_post_type()); $status_where = "AND post_status = '{$publish_status}'"; $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where} {$status_where}"); mb_set_topic_reply_count($topic_id, $count); }
<p> <input type="submit" value="<?php esc_attr_e('Submit', 'message-board'); ?> " /> </p> <p> <label> <input type="checkbox" name="mb_topic_subscribe" value="<?php echo mb_is_user_subscribed_topic(mb_get_topic_author_id(), mb_get_topic_id()) ? 1 : 0; ?> " /> <?php _e('Notify me of follow-up posts via email', 'message-board'); ?> </label> </p> <input type="hidden" name="mb_topic_id" value="<?php echo absint(mb_get_topic_id()); ?> " /> <?php wp_nonce_field('mb_edit_topic_action', 'mb_edit_topic_nonce', false); ?> </fieldset> </form>
/** * Removes a topic from the list of sticky topics. * * @since 1.0.0 * @access public * @param int $topic_id * @return bool */ function mb_remove_sticky_topic($topic_id) { $topic_id = mb_get_topic_id($topic_id); if (mb_is_topic_sticky($topic_id)) { $stickies = mb_get_sticky_topics(); $key = array_search($topic_id, $stickies); if (isset($stickies[$key])) { unset($stickies[$key]); return update_option('mb_sticky_topics', array_unique($stickies)); } } return false; }
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); }
/** * 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); } } }
function mb_handler_topic_toggle_trash() { if (!isset($_GET['action']) || 'mb_toggle_trash' !== $_GET['action'] || !isset($_GET['topic_id'])) { return; } $topic_id = mb_get_topic_id($_GET['topic_id']); /* Verify nonce. */ if (!isset($_GET['mb_nonce']) || !wp_verify_nonce($_GET['mb_nonce'], "trash_topic_{$topic_id}")) { return; } if (!current_user_can('delete_topic', $topic_id)) { return; } $updated = mb_is_topic_trash($topic_id) ? wp_untrash_post($topic_id) : wp_trash_post($topic_id); $redirect = remove_query_arg(array('action', 'topic_id', 'mb_nonce')); wp_safe_redirect(esc_url($redirect)); }
function mb_get_forum_toggle_trash_link($forum_id = 0) { $forum_id = mb_get_topic_id($forum_id); if (!current_user_can('delete_post', $forum_id)) { return ''; } $text = mb_is_forum_trash($forum_id) ? __('Restore', 'message-board') : get_post_status_object(mb_get_trash_post_status())->label; $link = sprintf('<a class="toggle-trash-link" href="%s">%s</a>', mb_get_forum_toggle_trash_url($forum_id), $text); return $link; }
/** * Returns the topic un/bookmark link. * * @since 1.0.0 * @access public * @param int $topic_id * @return string */ function mb_get_topic_bookmark_link($topic_id = 0) { if (!mb_is_bookmarks_active()) { return ''; } $topic_id = mb_get_topic_id($topic_id); $link = ''; if (is_user_logged_in()) { $user_id = get_current_user_id(); $url = mb_get_topic_bookmark_url($topic_id); $text = mb_is_topic_user_bookmark($user_id, $topic_id) ? __('Unbookmark', 'message-board') : __('Bookmark', 'message-board'); if (!empty($url)) { $link = sprintf('<a class="mb-bookmark-link" href="%s">%s</a>', $url, $text); } } return apply_filters('mb_get_topic_bookmark_link', $link, $topic_id); }
<span class="mb-topic-voice-count"><?php printf(__('Participating: %s', 'message-board'), mb_get_topic_voice_count()); ?> </span> <?php mb_topic_subscribe_link(); ?> <?php mb_topic_bookmark_link(); ?> </p> </header><!-- .mb-page-header --> <?php if (current_user_can('read_topic', mb_get_topic_id())) { ?> <ol id="mb-thread" class="mb-thread"> <?php if (mb_show_lead_topic() && mb_topic_query()) { ?> <?php while (mb_topic_query()) { ?> <?php mb_the_topic(); ?>
?> </label> <?php mb_reply_editor(); ?> </div><!-- .mb-form-content --> <p> <input type="submit" value="<?php esc_attr_e('Post Reply', 'message-board'); ?> " /> </p> <?php if (mb_is_subscriptions_active() && !mb_is_user_subscribed_topic(mb_get_topic_id())) { ?> <p> <label> <input type="checkbox" name="mb_reply_subscribe" value="1" /> <?php _e('Notify me of follow-up posts via email', 'message-board'); ?> </label> </p> <?php } // End check if subscriptions enabled. ?>