/** * Return the numeric position of a reply within a topic * * @since bbPress (r2984) * * @param int $reply_id Optional. Reply id * @param int $topic_id Optional. Topic id * @uses bbp_get_reply_id() To get the reply id * @uses bbp_get_reply_topic_id() Get the topic id of the reply id * @uses bbp_get_topic_reply_count() To get the topic reply count * @uses bbp_get_reply_post_type() To get the reply post type * @uses bbp_get_reply_position_raw() To get calculate the reply position * @uses bbp_update_reply_position() To update the reply position * @uses bbp_show_lead_topic() Bump the count if lead topic is included * @uses apply_filters() Calls 'bbp_get_reply_position' with the reply * position, reply id and topic id * @return int Reply position */ function bbp_get_reply_position($reply_id = 0, $topic_id = 0) { // Get required data $reply_id = bbp_get_reply_id($reply_id); $reply_position = get_post_field('menu_order', $reply_id); // Reply doesn't have a position so get the raw value if (empty($reply_position)) { $topic_id = !empty($topic_id) ? bbp_get_topic_id($topic_id) : bbp_get_reply_topic_id($reply_id); // Post is not the topic if ($reply_id !== $topic_id) { $reply_position = bbp_get_reply_position_raw($reply_id, $topic_id); // Update the reply position in the posts table so we'll never have // to hit the DB again. if (!empty($reply_position)) { bbp_update_reply_position($reply_id, $reply_position); } // Topic's position is always 0 } else { $reply_position = 0; } } // Bump the position by one if the lead topic is in the replies loop if (!bbp_show_lead_topic()) { $reply_position++; } return (int) apply_filters('bbp_get_reply_position', $reply_position, $reply_id, $topic_id); }
/** * @covers ::bbp_reply_position * @covers ::bbp_get_reply_position * * @ticket BBP2845 */ public function test_bbp_get_reply_position() { $f = $this->factory->forum->create(); $t = $this->factory->topic->create(array('post_parent' => $f, 'topic_meta' => array('forum_id' => $f))); $r = $this->factory->reply->create_many(7, array('post_parent' => $t, 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t))); // Reply menu position is unaltered when bbp_show_lead_topic() true. add_filter('bbp_show_lead_topic', '__return_true'); $position = get_post_field('menu_order', $r[3]); $this->assertSame(4, $position); $position = bbp_get_reply_position_raw($r[3]); $this->assertSame(4, $position); $position = bbp_get_reply_position($r[3]); $this->assertSame(4, $position); // Force a reply's 'menu_order' to 0. wp_update_post(array('ID' => $r[3], 'menu_order' => 0)); $position = get_post_field('menu_order', $r[3]); $this->assertSame(0, $position); $position = bbp_get_reply_position_raw($r[3]); $this->assertSame(4, $position); $position = bbp_get_reply_position($r[3]); $this->assertSame(4, $position); // Remove the filter for WordPress < 4.0 compatibility. remove_filter('bbp_show_lead_topic', '__return_true'); // Reply menu position is bumped by 1 when bbp_show_lead_topic() false. add_filter('bbp_show_lead_topic', '__return_false'); $position = get_post_field('menu_order', $r[3]); $this->assertSame(4, $position); $position = bbp_get_reply_position_raw($r[3]); $this->assertSame(4, $position); $position = bbp_get_reply_position($r[3]); $this->assertSame(5, $position); // Force a reply's 'menu_order' to 0. wp_update_post(array('ID' => $r[3], 'menu_order' => 0)); $position = get_post_field('menu_order', $r[3]); $this->assertSame(0, $position); $position = bbp_get_reply_position_raw($r[3]); $this->assertSame(4, $position); $position = bbp_get_reply_position($r[3]); $this->assertSame(5, $position); // Remove the filter for WordPress < 4.0 compatibility. remove_filter('bbp_show_lead_topic', '__return_false'); }
/** * Update the position of the reply. * * The reply position is stored in the menu_order column of the posts table. * This is done to prevent using a meta_query to retrieve posts in the proper * freshness order. By updating the menu_order accordingly, we're able to * leverage core WordPress query ordering much more effectively. * * @since bbPress (r3933) * * @global type $wpdb * @param type $reply_id * @param type $reply_position * @return mixed */ function bbp_update_reply_position($reply_id = 0, $reply_position = 0) { // Bail if reply_id is empty $reply_id = bbp_get_reply_id($reply_id); if (empty($reply_id)) { return false; } // If no position was passed, get it from the db and update the menu_order if (empty($reply_position)) { $reply_position = bbp_get_reply_position_raw($reply_id, bbp_get_reply_topic_id($reply_id)); } // Update the replies' 'menp_order' with the reply position wp_update_post(array('ID' => $reply_id, 'menu_order' => $reply_position)); return (int) $reply_position; }
/** * Update the position of the reply. * * The reply position is stored in the menu_order column of the posts table. * This is done to prevent using a meta_query to retrieve posts in the proper * freshness order. By updating the menu_order accordingly, we're able to * leverage core WordPress query ordering much more effectively. * * @since 2.1.0 bbPress (r3933) * * @param int $reply_id * @param int $reply_position * * @return mixed */ function bbp_update_reply_position($reply_id = 0, $reply_position = 0) { // Bail if reply_id is empty $reply_id = bbp_get_reply_id($reply_id); if (empty($reply_id)) { return false; } // If no position was passed, get it from the db and update the menu_order if (empty($reply_position)) { $reply_position = bbp_get_reply_position_raw($reply_id, bbp_get_reply_topic_id($reply_id)); } // Toggle revisions off as we are not altering content if (post_type_supports(bbp_get_reply_post_type(), 'revisions')) { $revisions_removed = true; remove_post_type_support(bbp_get_reply_post_type(), 'revisions'); } // Update the replies' 'menu_order' with the reply position wp_update_post(array('ID' => $reply_id, 'menu_order' => $reply_position)); // Toggle revisions back on if (true === $revisions_removed) { $revisions_removed = false; add_post_type_support(bbp_get_reply_post_type(), 'revisions'); } return (int) $reply_position; }