Add JReviews tabs to PeepSo profiles
- Thread started
- Last updated
What you’ll achieve
Display a profile owner's JReviews listings, favorites, and reviews inside PeepSo profile tabs.
My site
Add listings, favorites, and reviews tabs to PeepSo profiles on WordPress. Each tab shows content for the profile being viewed.
JReviews 6 Early Access
Add the three profile tabs
This WordPress example embeds the viewed user's latest ten listings and ten favorited listings. The reviews tab shows the latest twenty published reviews, ten per page, with numbered navigation at the bottom. Each list uses one column. Listings include 50-word summaries and cropped thumbnails. Review cards include up to 50 words of comment text.
The tabs follow PeepSo's profile privacy settings. The reviews tab also checks that reviews and their listings are published and that the visitor can view those listings. Its page links stay on the same user's reviews tab, with at most twenty reviews in total.
Disable the PeepSo add-on's existing JReviews links to avoid duplicate navigation. If you need only a listings tab, first check the add-on's built-in jreviews_peepso_profile_listings_as_tab option.
Create wp-content/plugins/community-jreviews-tabs/community-jreviews-tabs.php and add the complete plugin below. Activate it in WordPress. This site-specific plugin registers the PeepSo integration independently of the active theme. Enable JReviews 6 Early Access before using this version.
<?php
use JReviews\App\Models\Comment;
use JReviews\App\Models\Favorite;
use JReviews\App\Models\User;
use JReviews\App\Models\Widget;
use JReviews\App\Settings\CommentsWidgetSettings;
use JReviews\App\Shortcodes\ListingsShortcode;
/**
* Plugin Name: Community JReviews Profile Tabs
*/
defined('ABSPATH') or exit;
function community_jreviews_profile_id(): int
{
if (! class_exists('PeepSoProfile') || ! class_exists('PeepSoUser')) {
return 0;
}
$profileUser = PeepSoProfile::get_instance()->user ?? null;
$id = $profileUser ? (int) $profileUser->get_id() : 0;
return $id > 0 && PeepSoUser::get_instance($id)->is_accessible('profile') ? $id : 0;
}
function community_jreviews_profile_reviews(int $id): string
{
$comments = Comment::query()
->with(['listing.media' => fn ($query) => $query->published(), 'listing_type.rating_criteria', 'ratings', 'user.community'])
->published()->whereOwner($id)->whereRelatedListingPublished()->whereListingViewerAccess()
->orderByDesc('created')->orderByDesc('id')->limit(20)->get();
if ($comments->isEmpty()) {
return '<p>No published reviews.</p>';
}
$pages = (int) ceil($comments->count() / 10);
$requestedPage = filter_input(INPUT_GET, 'jr_reviews_page', FILTER_VALIDATE_INT);
$page = max(1, min($pages, (int) $requestedPage));
$visibleComments = $comments->slice(($page - 1) * 10, 10)->values()->map(function ($comment) {
$display = clone $comment;
$display->comments = esc_html(wp_trim_words(wp_strip_all_tags((string) $comment->comments), 50));
return $display;
});
$widget = new Widget;
$widget->title = '';
$widget->unique_id = 'peepso-reviews-'.$id;
$widget->widget_type = 'comments';
$settings = array_merge(CommentsWidgetSettings::defaults(), [
'show_comment_text' => 1, 'comment_text_lines' => 'full',
]);
$output = fwd_view('jreviews::widget.comments', [
'widget' => $widget, 'title' => '', 'showTitle' => false,
'comments' => $visibleComments, 'widgetSettings' => (object) $settings,
'customLinks' => [], 'useHorizontalSlider' => false,
'cardSize' => 'sm', 'gridRows' => 1, 'columnsCap' => 1,
])->render();
if ($pages > 1) {
$base = PeepSoUser::get_instance($id)->get_profileurl().'myreviews';
$separator = str_contains($base, '?') ? '&' : '?';
$output .= '<nav aria-label="Review pages">';
for ($number = 1; $number <= $pages; $number++) {
if ($number === $page) {
$output .= '<span aria-current="page"> '.$number.' </span>';
} else {
$url = $base.$separator.'jr_reviews_page='.$number;
$output .= '<a href="'.esc_url($url).'"> '.$number.' </a>';
}
}
$output .= '</nav>';
}
return $output;
}
add_filter('peepso_navigation_profile', function (array $links): array {
if (! community_jreviews_profile_id()) {
return $links;
}
foreach (['mylistings' => 'My Listings', 'myfavorites' => 'My Favorites', 'myreviews' => 'My Reviews'] as $key => $label) {
$links[$key] = ['label' => $label, 'href' => $key, 'icon' => 'gcis gci-file-alt'];
}
return $links;
});
foreach (['mylistings', 'myfavorites', 'myreviews'] as $segment) {
add_action('peepso_profile_segment_'.$segment, function () use ($segment): void {
$id = community_jreviews_profile_id();
if (! $id) {
return;
}
require_once WP_PLUGIN_DIR.'/jreviews/jreviews/framework.php';
$user = User::find($id);
if (! $user || $user->block) {
return;
}
$output = null;
if ($segment === 'myreviews') {
$output = community_jreviews_profile_reviews($id);
} else {
$attributes = [
'user' => (string) $id, 'limit' => '10', 'listing_order' => 'latest',
'summary' => '1', 'summary_words' => '50', 'show_category' => '0',
'tn_mode' => 'crop', 'tn_size' => '125x125', 'columns' => '1',
];
if ($segment === 'myfavorites') {
$ids = Favorite::query()->where('user_id', $id)
->pluck('content_id')->map(fn ($value) => (int) $value)->filter()->all();
if (! $ids) {
$output = '<p>No favorite listings.</p>';
} else {
unset($attributes['user']);
$attributes['listing_ids'] = implode(',', $ids);
}
}
if ($output === null) {
$output = (new ListingsShortcode)->render($attributes);
if (trim($output) === '') {
$output = '<p>No published listings.</p>';
}
}
}
echo '<div class="peepso ps-page-profile">';
echo PeepSoTemplate::exec_template('general', 'navbar');
echo PeepSoTemplate::exec_template('profile', 'focus', ['current' => $segment]);
PeepSoTemplate::exec_template('activity', 'dialogs');
echo '<div class="peepso-jreviews">'.$output.'</div></div>';
});
}
Check all three tabs on your own profile and another user's profile. On a profile with at least eleven published reviews, open page 2. It should show the next reviews, with no repeated review IDs, the same profile header, and a working link back to page 1. Unpublished reviews and reviews of unpublished listings must stay out of both pages. Check a profile with no favorites; it must show the empty message rather than unrelated listings. Then sign out and open a restricted profile's tab directly. PeepSo's privacy rules should block both the links and the content. JReviews still applies listing publication and viewing permissions to the embedded results. Review pagination uses ordinary page links, so direct links and the browser Back button reload the complete profile tab. Exclude profile tabs from shared full-page caches if visibility differs between visitors. Adapt the English labels for your site.
JReviews 5 and v6 Legacy
Add My Listings, My Favorites, and My Reviews tabs to a PeepSo profile on WordPress. The JReviews PeepSo Add-on normally links to separate JReviews pages. Some add-on releases also provide an embedded listings-tab option through jreviews_peepso_profile_listings_as_tab. Check that option first if you only need listings. The example below adds listings, favorites, and reviews inside the profile.
Add the profile tabs
Add the code marked with + to your WordPress child theme's functions.php, below the existing PHP opening tag. A site-specific plugin is another supported location if the integration must remain active when you change themes. Do not edit the parent theme, PeepSo, or JReviews plugin files. To replace the add-on's existing links with these tabs, disable those links in the add-on settings.
The example adds the three tabs and shows the viewed user's listings, favorites, or reviews inside them. Each tab keeps the PeepSo profile header and supports its share button.
<?php
function jreviews_peepso_profile_header($active) {
echo PeepSoTemplate::exec_template('general', 'navbar');
echo PeepSoTemplate::exec_template('profile', 'focus', ['current'=> $active]);
PeepSoTemplate::exec_template('activity', 'dialogs'); // Fix share button
}
function jreviews_peepso_profile_userid() {
$profile = PeepSoProfile::get_instance();
return $profile->user->get_id();
}
/**
* Adds the My Listings, My Favorites and My Reviews links to profile navigation
*/
add_filter('peepso_navigation_profile', function($links) {
if (! PeepSoUser::get_instance(jreviews_peepso_profile_userid())->is_accessible('profile')) {
return $links;
}
$links['mylistings'] = [
'label' => __( 'My Listings', 'jreviews' ),
'href' => 'mylistings',
'icon' => 'gcis gci-file-alt'
];
$links['myreviews'] = [
'label' => __( 'My Reviews', 'jreviews' ),
'href' => 'myreviews',
'icon' => 'gcis gci-star'
];
$links['myfavorites'] = [
'label' => __( 'My Favorites', 'jreviews' ),
'href' => 'myfavorites',
'icon' => 'gcis gci-bookmark'
];
return $links;
});
/**
* Registers profile segment mylistings
*/
add_action('peepso_profile_segment_mylistings', function() {
if (! PeepSoUser::get_instance(jreviews_peepso_profile_userid())->is_accessible('profile')) {
return;
}
?>
<div class='peepso ps-page-profile'>
<section id='mainbody' class='ps-page-unstyled'>
<section id='component' role='article' class='ps-clearfix'>
<?php echo jreviews_peepso_profile_header('mylistings'); ?>
<div class="peepso-jreviews">
<?php
require_once( WP_PLUGIN_DIR . '/jreviews/jreviews/framework.php');
$query_vars = array('module'=> [],'data'=> []);
$params = [
'community' => 1,
'show_numbers'=> 0,
'listings_order' => 'latest',
'fields'=>'',
'summary' => 1,
'summary_words' => 50,
'show_category' => 0,
'tn_mode' => 'crop',
'tn_size' => '125x125',
'tn_show' => 1,
'tn_position' => 'left',
'columns' => 1,
'orientation' => 'horizontal',
'slideshow' => 0,
'slideshow_interval' => 6,
'module_limit' => 10,
'module_total' => 10,
'nav_position'=>'bottom',
];
$query_vars['module'] = $params;
$query_vars['user'] = jreviews_peepso_profile_userid();
$query_vars['module_id'] = 'mylistings'.$query_vars['user'];
$query_vars[S2_QVAR_PAGE] = 1;
$query_vars['secret'] = cmsFramework::getConfig('secret');
$query_vars['token'] = cmsFramework::formIntegrityToken($query_vars, ['module','module_id','form','data'], false);
$query_vars['data']['controller'] = 'community_listings';
$query_vars['data']['action'] = 'mylistings';
$Dispatcher = new S2Dispatcher('jreviews');
$content = $Dispatcher->dispatch($query_vars);
echo $content;
?>
</div>
</section>
</section>
</div>
<?php
});
/**
* Registers profile segment myfavorites
*/
add_action('peepso_profile_segment_myfavorites', function() {
if (! PeepSoUser::get_instance(jreviews_peepso_profile_userid())->is_accessible('profile')) {
return;
}
?>
<div class='peepso ps-page-profile'>
<section id='mainbody' class='ps-page-unstyled'>
<section id='component' role='article' class='ps-clearfix'>
<?php echo jreviews_peepso_profile_header('myfavorites'); ?>
<div class="peepso-jreviews">
<?php
require_once( WP_PLUGIN_DIR . '/jreviews/jreviews/framework.php');
$query_vars = array('module'=> [],'data'=> []);
$params = [
'community' => 1,
'show_numbers'=> 0,
'listings_order' => 'latest',
'fields'=>'',
'summary' => 1,
'summary_words' => 50,
'show_category' => 0,
'tn_mode' => 'crop',
'tn_size' => '125x125',
'tn_show' => 1,
'tn_position' => 'left',
'columns' => 1,
'orientation' => 'horizontal',
'slideshow' => 0,
'slideshow_interval' => 6,
'module_limit' => 10,
'module_total' => 10,
'nav_position'=>'bottom',
];
$query_vars['module'] = $params;
$query_vars['user'] = jreviews_peepso_profile_userid();
$query_vars['module_id'] = 'favorites'.$query_vars['user'];
$query_vars[S2_QVAR_PAGE] = 1;
$query_vars['secret'] = cmsFramework::getConfig('secret');
$query_vars['token'] = cmsFramework::formIntegrityToken($query_vars, ['module','module_id','form','data'], false);
$query_vars['data']['controller'] = 'community_listings';
$query_vars['data']['action'] = 'favorites';
$Dispatcher = new S2Dispatcher('jreviews');
$content = $Dispatcher->dispatch($query_vars);
echo $content;
?>
</div>
</section>
</section>
</div>
<?php
});
/**
* Registers profile segment myreviews
*/
add_action('peepso_profile_segment_myreviews', function() {
if (! PeepSoUser::get_instance(jreviews_peepso_profile_userid())->is_accessible('profile')) {
return;
}
?>
<div class='peepso ps-page-profile'>
<section id='mainbody' class='ps-page-unstyled'>
<section id='component' role='article' class='ps-clearfix'>
<?php echo jreviews_peepso_profile_header('myreviews'); ?>
<div class="peepso-jreviews">
<?php
require_once( WP_PLUGIN_DIR . '/jreviews/jreviews/framework.php');
$query_vars = array('module'=> [],'data'=> []);
$params = [
'community' => 1,
'reviews_order' =>'latest',
'show_numbers' => 0,
'fields' => '',
'show_comments' => 1,
'comments_words' => 50,
'tn_mode' => 'crop',
'tn_size' => '125x125',
'tn_show' => 1,
'tn_position' => 'left',
'orientation' => 'horizontal',
'slideshow' => 0,
'slideshow_interval' => 6,
'columns' => 1,
'module_limit' => 10,
'module_total' => 20,
'nav_position' => 'bottom'
];
$query_vars['module'] = $params;
$query_vars['user'] = jreviews_peepso_profile_userid();
$query_vars['module_id'] = 'myreviews'.$query_vars['user'];
$query_vars[S2_QVAR_PAGE] = 1;
$query_vars['secret'] = cmsFramework::getConfig('secret');
$query_vars['token'] = cmsFramework::formIntegrityToken($query_vars, ['module','module_id','form','data'], false);
$query_vars['data']['controller'] = 'community_reviews';
$query_vars['data']['action'] = 'index';
$Dispatcher = new S2Dispatcher('jreviews');
$content = $Dispatcher->dispatch($query_vars);
echo $content;
?>
</div>
</section>
</section>
</div>
<?php
});
Adjust the output
Each tab has a $params = [ section in the code. Change settings in the corresponding section to adjust that tab's output. The listings and favorites examples request the latest listings, 50-word summaries, and 125-by-125 cropped thumbnails. The reviews example requests the latest reviews and 50-word comments. All three use a horizontal layout with one column and navigation at the bottom.
Keep the JReviews loading lines and the secret and token lines unchanged. They load JReviews from WordPress's configured plugin folder and let JReviews verify the requests for profile content.
Compatibility
Use this example with legacy JReviews pages on WordPress. Test the profile tabs, templates, and content loading before use.