bbp_admin_repair_topic_voice_count()

Recount topic voices


Description Description


Return Return

(array) An array of the status code and the message


Top ↑

Source Source

File: includes/admin/tools/repair.php

function bbp_admin_repair_topic_voice_count() {

	// Define variables
	$bbp_db    = bbp_db();
	$statement = esc_html__( 'Counting the number of voices in each topic… %s', 'bbpress' );
	$result    = esc_html__( 'Failed!', 'bbpress' );

	$sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ('_bbp_voice_count', '_bbp_engagement')";
	if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
		return array( 1, sprintf( $statement, $result ) );
	}

	// Post types and status
	$tpt = bbp_get_topic_post_type();
	$rpt = bbp_get_reply_post_type();
	$pps = bbp_get_public_status_id();
	$cps = bbp_get_closed_status_id();

	$engagements_sql = $bbp_db->prepare( "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) (
		SELECT postmeta.meta_value, '_bbp_engagement', posts.post_author
			FROM {$bbp_db->posts} AS posts
			LEFT JOIN {$bbp_db->postmeta} AS postmeta
				ON posts.ID = postmeta.post_id
				AND postmeta.meta_key = '_bbp_topic_id'
			WHERE posts.post_type IN (%s, %s)
				AND posts.post_status IN (%s, %s)
			GROUP BY postmeta.meta_value, posts.post_author)", $tpt, $rpt, $pps, $cps );

	if ( is_wp_error( $bbp_db->query( $engagements_sql ) ) ) {
		return array( 2, sprintf( $statement, $result ) );
	}

	$voice_count_sql = "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) (
		SELECT post_id, '_bbp_voice_count', COUNT(DISTINCT meta_value)
			FROM {$bbp_db->postmeta}
			WHERE meta_key = '_bbp_engagement'
			GROUP BY post_id)";

	if ( is_wp_error( $bbp_db->query( $voice_count_sql ) ) ) {
		return array( 3, sprintf( $statement, $result ) );
	}

	return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.