#StackBounty: #php #javascript #comments How does this WordPress Plugin (Thrive Comments) apply their custom comment sort?

Bounty: 50

I am building a site that uses the WordPress Plugin Thrive Comments.

It allows people to sort the comment by Top Rated, Newest, or Oldest.

Digging into the code I can see that Top Rated is sorting based on a default WordPress comment_karma field.

I want to change it so that Top Rated sorts based on a new comment metadata field I created called confidence_rank_cached.

The Thrive Comments plugin has a function named tcm_get_localization_parameters which I think is sending PHP data to the front end so it can be used by the JavaScript sort script.

Here is a full copy of that function:

/**
 * Get params to be used in javascript
 *
 * @return array
 */
public function tcm_get_localization_parameters() {
    $post    = $this->post_for_localization();
    $post_id = empty( $post['ID'] ) ? 0 : $post['ID'];

    $localization = array(
        'current_user'               => tcmh()->tcm_get_current_user(),
        'translations'               => include tcm()->plugin_path( 'includes/i18n.php' ),
        'nonce'                      => $this->create_nonce(),
        'routes'                     => array(
            'comments'               => tcm()->tcm_get_route_url( 'comments' ),
            'gravatar'               => tcm()->tcm_get_route_url( 'comments' ) . '/gravatar',
            'live_update'            => tcm()->tcm_get_route_url( 'comments' ) . '/live_update',
            'update_post_subscriber' => tcm()->tcm_get_route_url( 'comments' ) . '/update_post_subscriber',
            'generate_nonce'         => admin_url( 'admin-ajax.php' ),
        ),
        'post'                       => $post,
        'related_posts'              => tcmc()->get_related_posts( Thrive_Comments_Constants::TCM_NO_RELATED_POSTS, $args = array() ),
        'const'                      => array(
            'toast_timeout' => Thrive_Comments_Constants::TCM_TOAST_TIMEOUT, // Not sure if we really need this.
            'wp_content'    => rtrim( WP_CONTENT_URL, '/' ) . '/',
            'ajax_dash'     => array( Thrive_Comments_Constants::TCM_AJAX_DASH ),
            'site_url'      => get_site_url(),
            'post_url'      => apply_filters( 'tcm_post_url', get_permalink() ),
            'moderation'    => array(
                'approve'              => Thrive_Comments_Constants::TCM_APPROVE,
                'unapprove'            => Thrive_Comments_Constants::TCM_UNAPPROVE,
                'spam'                 => Thrive_Comments_Constants::TCM_SPAM,
                'unspam'               => Thrive_Comments_Constants::TCM_UNSPAM,
                'trash'                => Thrive_Comments_Constants::TCM_TRASH,
                'untrash'              => Thrive_Comments_Constants::TCM_UNTRASH,
                'unreplied'            => Thrive_Comments_Constants::TCM_UNREPLIED,
                'tcm_delegate'         => Thrive_Comments_Constants::TCM_DELEGATE,
                'tcm_featured'         => Thrive_Comments_Constants::TCM_FEATURED,
                'tcm_keyboard_tooltip' => Thrive_Comments_Constants::TCM_KEYBOARD_TOOLTIP,
                'featured'             => Thrive_Comments_Constants::TCM_FEATURE_VALUE,
                'not_featured'         => Thrive_Comments_Constants::TCM_NOT_FEATURE_VALUE,
            ),
        ),
        'settings'                   => tcms()->tcm_get_settings(),
        'close_comments'             => tcms()->close_comments( $post_id ) || ! $this->tcm_show_comments(),
        'sorting'                    => tcms()->get_comment_sorting(),
        'tcm_customize_labels'       => tcms()->tcm_get_setting_by_name( Thrive_Comments_Constants::TCM_LABELS_KEY ),
        'tcm_social_apis'            => array(
            'facebook' => Thrive_Dash_List_Manager::credentials( 'facebook' ),
            'google'   => Thrive_Dash_List_Manager::credentials( 'google' ),
        ),
        'email_services'             => tcamh()->get_email_services(),
        'tcm_accent_color'           => tcms()->tcm_get_setting_by_name( Thrive_Comments_Constants::TCM_ACCENT_COLOR ),
        'has_plugin_cache'           => tve_dash_detect_cache_plugin(),
        'default_author_picture_url' => tcmh()->get_picture_url(),
    );

    /**
     * Filter for adding extra params for comments localization in fronted
     *
     * @param array $localization the already built localization by TC
     */
    return apply_filters( 'tcm_comments_localization', $localization );
}

Looking at the function, you will see it is returning apply_filters( 'tcm_comments_localization', $localization );.

So I thought I could apply a filter inside functions.php of my child theme to make it sort by my confidence_rank_cached comment metadata field instead of the default WordPress comment_karma field.

Here is the code I’m using inside functions.php:

function custom_change_top_ranking_comment_sort( $localization ) {
    $localization['sorting']['sort_field'] = 'confidence_rank_cached';
    return $localization;
}
add_filter( 'tcm_comments_localization', 'custom_change_top_ranking_comment_sort', 10, 1 );

After adding that code and inspecting the source code of the loaded post I can see that the JavaScript sort_field is now using confidence_rank_cached instead of comment_karma.

This is good, but the sorting does not work correctly. When Top Rated is selected as the sort option it doesn’t sort the comments by my confidence_rank_cached metadata value.

I’m sure there is a step I’m missing here somewhere.

I have a suspicion that it isn’t working because the original JavaScript sort is based on comment_karma which is from the main comment database table, and now I’m trying to make it use confidence_rank_cached which is from the comment metadata table… but I don’t have a good enough understanding of JavaScript and AJAX etc to figure it out.

Could someone please take a look and help me get this working?

Here is a live staging site here so you can see it in action. I have disabled my custom code in functions.php so you can see how it is intended to work: https://wholesale-discussion.flywheelsites.com/new-post-test/

Since it’s not live yet you will need these login details to view the post…

  • Username: explode
  • Password: boomboom

And incase it helps, here is a copy of the Thrive Comments frontend JavaScript file. I un-minified it to make it easy to read: https://pastebin.com/6NznyLKV


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.