Support » Plugin: Site Reviews » unapproved user review showing up for the user?

  • Resolved enigma2k

    (@enigma2k)


    Hi,

    Is it possible that the user who writes the review also sees the his own review once he posts it? Only other users shouldn’t be able to see it once it is approved.

    It is often that users want direct feedback and see that their review gets public otherwise they think it is fake if it isn’t immediately approved like it is the case on Amazon.

    This is something that was possible with my old plugin handled over cookies and/or if the user is logged in so I hope there is a way here too! 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Gemini Labs

    (@geminilabs)

    Site Reviews uses the WP_Query, so in order to do this, you will need to edit the “where” clause of the database query using the WordPress posts_where filter hook. You can do it like this:

    /**
     * Allow users to see their own pending reviews
     * @param string $where
     * @param \WP_Query $query
     * @return string
     */
    add_filter('posts_where', function ($where, $query) {
        if ('site-review' !== $query->get('post_type') || !is_user_logged_in() || is_admin()) {
            return $where;
        }
        global $wpdb;
        $userId = get_current_user_id();
        $table = $wpdb->prefix.'posts';
        $search = "($table.post_status = 'publish')";
        $replace = "($table.post_status = 'publish' OR ($table.post_author = '$userId' AND $table.post_status = 'pending'))";
        return str_replace($search, $replace, $where);
    }, 10, 2);

    If you do this, you may also wish to visually display the review as a pending review. You can do this by using a custom template in your theme (see the Help page of the plugin to learn how to do this) in order to add a “pending” class which you can style yourself with some custom CSS.

    Here is an example where we add the .glsr-status-publish (approved) or .glsr-status-pending (unapproved) class to the “review.php” template:

    <?php defined('WPINC') || die; ?>
    
    <div class="glsr-review glsr-status-<?= $review->status; ?>">
        {{ title }}
        {{ rating }}
        {{ date }}
        {{ assigned_to }}
        {{ content }}
        {{ avatar }}
        {{ author }}
        {{ response }}
    </div>
    Thread Starter enigma2k

    (@enigma2k)

    Thank you for the code! Really appreciate your support!

    I’ve put it into functions.php of the Theme Child and it is working (which I hope it is the right way to do this).

    It is working for logged in users. But users will actually need an account for this which is not often the case since most review sites are public.

    Is there a way to make it instead show up for users who just have written a review using a cookie or maybe using their IP address? or maybe there is an even simpler way?

    Plugin Author Gemini Labs

    (@geminilabs)

    This adds an INNER JOIN to the database query in order to use the IP Address in the WHERE clause of the query later on.

    /**
     * INNER JOIN the postmeta table in order to add the WHERE query for the _ip_address
     * @param string $join
     * @param \WP_Query $query
     * @return string
     */
    add_filter('posts_join', function ($join, $query) {
        if ('site-review' !== $query->get('post_type') || is_admin()) {
            return $join;
        }
        global $wpdb;
        return $join." INNER JOIN {$wpdb->prefix}postmeta AS glsr_mt ON ({$wpdb->prefix}posts.ID = glsr_mt.post_id)";
    }, 10, 2);

    This filters the WHERE clause of the database query to include a check for the IP address.

    /**
     * Allow anyone to see their submitted pending reviews
     * @param string $where
     * @param \WP_Query $query
     * @return string
     */
    add_filter('posts_where', function ($where, $query) {
        if ('site-review' !== $query->get('post_type') || is_admin()) {
            return $where;
        }
        $ipAddress = function_exists('glsr')
            ? glsr('Helper')->getIpAddress()
            : filter_input(INPUT_SERVER, 'REMOTE_ADDR');
        if (empty($ipAddress)) {
            return $where;
        }
        global $wpdb;
        $table = $wpdb->prefix.'posts';
        $search = "($table.post_status = 'publish')";
        $replace = "($table.post_status = 'publish' OR (glsr_mt.meta_key = '_ip_address' AND glsr_mt.meta_value = '$ipAddress' AND $table.post_status = 'pending'))";
        return str_replace($search, $replace, $where);
    }, 10, 2);
    Thread Starter enigma2k

    (@enigma2k)

    Thank you very much for this extensive support! 🙂

    Zee

    (@laserjobs)

    Did the reviews used to show the “pending” review to the user in the earlier versions?
    I am not seeing the pending either after entering a review.

    Plugin Author Gemini Labs

    (@geminilabs)

    @laserjobs

    No.

    Pending (unapproved) reviews are not visible on your website until approved.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘unapproved user review showing up for the user?’ is closed to new replies.