/** * Registers custom meta keys with WordPress and provides callbacks for sanitizing and authorizing * the metadata. * * @since 1.0.0 * @access public * @return void */ function mb_register_meta() { /* General post meta. */ register_meta('post', mb_get_prev_status_meta_key(), 'sanitize_key', '__return_true'); /* Forum meta. */ register_meta('post', mb_get_forum_activity_datetime_meta_key(), 'esc_html', '__return_true'); register_meta('post', mb_get_forum_activity_datetime_epoch_meta_key(), 'esc_html', '__return_true'); register_meta('post', mb_get_forum_last_topic_id_meta_key(), 'absint', '__return_true'); register_meta('post', mb_get_forum_last_reply_id_meta_key(), 'absint', '__return_true'); register_meta('post', mb_get_forum_subforum_count_meta_key(), 'absint', '__return_true'); register_meta('post', mb_get_forum_topic_count_meta_key(), 'absint', '__return_true'); register_meta('post', mb_get_forum_reply_count_meta_key(), 'absint', '__return_true'); register_meta('post', mb_get_forum_type_meta_key(), 'esc_html', '__return_true'); register_meta('post', mb_get_forum_level_meta_key(), 'absint', '__return_true'); /* Topic meta. */ register_meta('post', mb_get_topic_activity_datetime_meta_key(), 'esc_html', '__return_true'); register_meta('post', mb_get_topic_activity_datetime_epoch_meta_key(), 'esc_html', '__return_true'); register_meta('post', mb_get_topic_last_reply_id_meta_key(), 'absint', '__return_true'); register_meta('post', mb_get_topic_voices_meta_key(), 'esc_html', '__return_true'); register_meta('post', mb_get_topic_voice_count_meta_key(), 'absint', '__return_true'); register_meta('post', mb_get_topic_reply_count_meta_key(), 'absint', '__return_true'); register_meta('post', mb_get_topic_type_meta_key(), 'esc_html', '__return_true'); /* User meta. */ register_meta('user', mb_get_user_forum_subscriptions_meta_key(), 'esc_html', '__return_true'); register_meta('user', mb_get_user_topic_subscriptions_meta_key(), 'esc_html', '__return_true'); register_meta('user', mb_get_user_topic_bookmarks_meta_key(), 'esc_html', '__return_true'); register_meta('user', mb_get_user_forum_count_meta_key(), 'absint', '__return_true'); register_meta('user', mb_get_user_topic_count_meta_key(), 'absint', '__return_true'); register_meta('user', mb_get_user_reply_count_meta_key(), 'absint', '__return_true'); }
/** * Filter on the user query to change the users loaded. * * @since 1.0.0 * @access public * @param object $query * @return void */ public function pre_get_users($query) { if ('topic_count' === $query->get('orderby')) { $query->set('orderby', 'meta_value_num'); $query->set('meta_key', mb_get_user_topic_count_meta_key()); } elseif ('reply_count' === $query->get('orderby')) { $query->set('orderby', 'meta_value_num'); $query->set('meta_key', mb_get_user_reply_count_meta_key()); } }
function mb_set_user_topic_count($user_id) { global $wpdb; $open_status = mb_get_open_post_status(); $close_status = mb_get_close_post_status(); $publish_status = mb_get_publish_post_status(); $hidden_status = mb_get_hidden_post_status(); $private_status = mb_get_private_post_status(); $where = $wpdb->prepare("WHERE post_author = %d AND post_type = %s", $user_id, mb_get_topic_post_type()); $status_where = "AND (post_status = '{$open_status}' OR post_status = '{$close_status}' OR post_status = '{$publish_status}' OR post_status = '{$private_status}' OR post_status = '{$hidden_status}')"; $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where} {$status_where}"); update_user_meta($user_id, mb_get_user_topic_count_meta_key(), $count); return $count; }
/** * Function for inserting reply data when it's first published. * * @since 1.0.0 * @access public * @param object $post * @return void */ function mb_insert_reply_data($post) { /* Hook for before inserting reply data. */ do_action('mb_before_insert_reply_data', $post); /* Get the reply ID. */ $reply_id = mb_get_reply_id($post->ID); /* Get the topic ID. */ $topic_id = mb_get_topic_id($post->post_parent); /* Get the forum ID. */ $forum_id = mb_get_topic_forum_id($topic_id); /* Get the user ID. */ $user_id = mb_get_user_id($post->post_author); /* Get the post date. */ $post_date = $post->post_date; $post_epoch = mysql2date('U', $post_date); /* Update user meta. */ $topic_count = mb_get_user_topic_count($user_id); update_user_meta($user_id, mb_get_user_topic_count_meta_key(), $topic_count + 1); /* Update topic position. */ mb_set_topic_position($topic_id, $post_epoch); /* Update topic meta. */ mb_set_topic_activity_datetime($topic_id, $post_date); mb_set_topic_activity_epoch($topic_id, $post_epoch); mb_set_topic_last_reply_id($topic_id, $reply_id); $voices = mb_get_topic_voices($topic_id); if (!in_array($user_id, $voices)) { $voices[] = $user_id; mb_set_topic_voices($topic_id, $voices); mb_set_topic_voice_count($topic_id, count($voices)); } $topic_reply_count = mb_get_topic_reply_count($topic_id); mb_set_topic_reply_count($topic_id, absint($topic_reply_count) + 1); $forum_reply_count = absint(mb_get_forum_reply_count($forum_id)) + 1; /* Update forum meta. */ mb_set_forum_activity_datetime($forum_id, $post_date); mb_set_forum_activity_epoch($forum_id, $post_epoch); mb_set_forum_last_reply_id($forum_id, $reply_id); mb_set_forum_last_topic_id($forum_id, $topic_id); mb_set_forum_reply_count($forum_id, $forum_reply_count); /* Notify subscribers that there's a new reply. */ mb_notify_subscribers($post); /* Hook for after inserting reply data. */ do_action('mb_after_insert_reply_data', $post); }
/** * Function for inserting topic data when it's first published. * * @since 1.0.0 * @access public * @param object $post * @return void */ function mb_insert_topic_data($post) { /* Hook for before inserting topic data. */ do_action('mb_before_insert_topic_data', $post); /* Get the topic ID. */ $topic_id = mb_get_topic_id($post->ID); /* Get the forum ID. */ $forum_id = mb_get_forum_id($post->post_parent); /* Get the User ID. */ $user_id = mb_get_user_id($post->post_author); /* Get the post date. */ $post_date = $post->post_date; $post_epoch = mysql2date('U', $post_date); /* Update user meta. */ $topic_count = mb_get_user_topic_count($user_id); update_user_meta($user_id, mb_get_user_topic_count_meta_key(), $topic_count + 1); /* Add topic meta. */ mb_set_topic_activity_datetime($topic_id, $post_date); mb_set_topic_activity_epoch($topic_id, $post_epoch); mb_set_topic_voices($topic_id, $user_id); mb_set_topic_voice_count($topic_id, 1); mb_set_topic_reply_count($topic_id, 0); /* If we have a forum ID. */ if (0 < $forum_id) { $topic_count = mb_get_forum_topic_count($forum_id); /* Update forum meta. */ mb_set_forum_activity_datetime($forum_id, $post_date); mb_set_forum_activity_epoch($forum_id, $post_epoch); mb_set_forum_last_topic_id($forum_id, $topic_id); mb_set_forum_topic_count($forum_id, absint($topic_count) + 1); } /* Notify subscribers that there's a new topic. */ mb_notify_subscribers($post); /* Hook for after inserting topic data. */ do_action('mb_after_insert_topic_data', $post); }
/** * Returns a user's topic count. * * @since 1.0.0 * @access public * @param int $user_id * @return int */ function mb_get_user_topic_count($user_id = 0) { $user_id = mb_get_user_id($user_id); $count = get_user_meta($user_id, mb_get_user_topic_count_meta_key(), true); if ('' === $count) { $count = mb_set_user_topic_count($user_id); } $count = !empty($count) ? absint($count) : 0; return apply_filters('mb_get_user_topic_count', $count, $user_id); }