PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 2.5.1
Forumax – AI Powered Advanced Community Forum Plugin v2.5.1
2.5.1 2.5.0 2.4.4 2.4.3 2.4.2 2.4.1 2.4.0 trunk 1.0.8 1.1.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 All 31 releases
← All changes | includes/features/bbp_voting/helpers.php +99 -0 2.4.3 → 2.5.1 View file →
@@ -170,4 +170,103 @@
170 170 }
171 171 add_action( 'bbp_new_topic', 'forumax_init_voting_scores' );
172 172 add_action( 'bbp_new_reply', 'forumax_init_voting_scores' );
173 173 }
174 +
175 +if ( ! function_exists( 'forumax_ensure_voting_score_meta' ) ) {
176 + /**
177 + * Give every saved topic/reply a vote score, including ones created in
178 + * wp-admin or by importers, so "sort by votes" never drops them.
179 + *
180 + * @param int $post_id Post ID.
181 + */
182 + function forumax_ensure_voting_score_meta( $post_id ) {
183 + if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
184 + return;
185 + }
186 +
187 + add_post_meta( $post_id, 'bbp_voting_score', '0', true );
188 + }
189 + add_action( 'save_post_topic', 'forumax_ensure_voting_score_meta' );
190 + add_action( 'save_post_reply', 'forumax_ensure_voting_score_meta' );
191 +}
192 +
193 +if ( ! function_exists( 'forumax_backfill_voting_score_meta_once' ) ) {
194 + /**
195 + * One-time bulk backfill of bbp_voting_score for existing topics/replies.
196 + *
197 + * Replaces the old per-page-view backfill, which ran an extra query and
198 + * stored a new transient for every distinct topic list.
199 + */
200 + function forumax_backfill_voting_score_meta_once() {
201 + if ( get_option( 'forumax_vote_score_backfilled' ) ) {
202 + return;
203 + }
204 +
205 + global $wpdb;
206 +
207 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
208 + $wpdb->query(
209 + "INSERT INTO {$wpdb->postmeta} ( post_id, meta_key, meta_value )
210 + SELECT p.ID, 'bbp_voting_score', '0'
211 + FROM {$wpdb->posts} p
212 + LEFT JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID AND pm.meta_key = 'bbp_voting_score'
213 + WHERE p.post_type IN ( 'topic', 'reply' ) AND pm.meta_id IS NULL"
214 + );
215 +
216 + update_option( 'forumax_vote_score_backfilled', 1 );
217 +
218 + // Drop the transients the old per-page backfill left behind.
219 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
220 + $wpdb->query(
221 + $wpdb->prepare(
222 + "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
223 + $wpdb->esc_like( '_transient_frmx_vote_backfill_' ) . '%',
224 + $wpdb->esc_like( '_transient_timeout_frmx_vote_backfill_' ) . '%'
225 + )
226 + );
227 + }
228 + add_action( 'admin_init', 'forumax_backfill_voting_score_meta_once' );
229 +}
230 +
231 +if ( ! function_exists( 'forumax_backfill_voting_sort_meta' ) ) {
232 + /**
233 + * Backfill a custom (filtered) sort meta key for the posts of one query.
234 + *
235 + * Only used when bbp_voting_sort_meta_key points at a non-default key.
236 + * Results are remembered in the object cache for the request/day.
237 + *
238 + * @param string $sort_meta_key Meta key used for sorting.
239 + * @param array $args bbPress query args.
240 + */
241 + function forumax_backfill_voting_sort_meta( $sort_meta_key, $args ) {
242 + $cache_key = 'vote_backfill_' . md5( $sort_meta_key . wp_json_encode( $args ) );
243 + if ( false !== wp_cache_get( $cache_key, 'forumax' ) ) {
244 + return;
245 + }
246 +
247 + $query = new WP_Query(
248 + array_merge(
249 + $args,
250 + [
251 + 'meta_query' => [
252 + [
253 + 'key' => $sort_meta_key,
254 + 'compare' => 'NOT EXISTS',
255 + ],
256 + ],
257 + 'fields' => 'ids',
258 + 'no_found_rows' => true,
259 + 'update_post_meta_cache' => false,
260 + 'update_post_term_cache' => false,
261 + ]
262 + )
263 + );
264 +
265 + foreach ( $query->posts as $post_id ) {
266 + $default_value = apply_filters( 'bbp_voting_sort_meta_key_default_value', '0', $post_id );
267 + update_post_meta( $post_id, $sort_meta_key, $default_value );
268 + }
269 +
270 + wp_cache_set( $cache_key, 1, 'forumax', DAY_IN_SECONDS );
271 + }
272 +}