Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace support review component with shared one. #408

Closed
Original file line number Diff line number Diff line change
Expand Up @@ -212,65 +212,9 @@ public function do_view_header() {
add_filter( 'bbp_get_topic_last_topic_title', array( $this, 'undo_topic_title' ), 10, 1 );
?>
<div class="review-ratings">
<div class="col-3">
<div class="reviews-about" style="display:none;"><?php echo esc_html( $this->object->post_title ); ?></div>
<div class="reviews-total-count"><?php
printf(
/* translators: %s: number of reviews */
_n( '%s review', '%s reviews', $this->reviews_count, 'wporg-forums' ),
'<span>' . number_format_i18n( $this->reviews_count ) . '</span>'
);
?></div>
<?php
foreach ( array( 5, 4, 3, 2, 1 ) as $rating ) {
$ratings_count = isset( $this->ratings_counts[ $rating ] ) ? $this->ratings_counts[ $rating ] : 0;
$ratings_count_total = isset( $this->ratings_counts ) ? array_sum( $this->ratings_counts) : 0;
$stars_title = sprintf(
/* translators: %s: number of stars */
_n(
'Click to see reviews that provided a rating of %d star',
'Click to see reviews that provided a rating of %d stars',
$rating,
'wporg-forums'
),
$rating
);
/* translators: %d: number of stars */
$stars_text = sprintf(
/* translators: %d: number of stars */
_n( '%d star', '%d stars', $rating, 'wporg-forums' ),
$rating
);
$width = 0;
if ( $ratings_count && $ratings_count_total ) {
$width = 100 * ( $ratings_count / $ratings_count_total );
}
?>
<div class="counter-container">
<a href="<?php echo esc_url( sprintf( home_url( '/%s/%s/reviews/?filter=%s' ), $this->compat, $this->slug, $rating ) ); ?>"
title="<?php echo esc_attr( $stars_title ); ?>">
<span class="counter-label" style="float:left;margin-right:5px;min-width:58px;"><?php echo esc_html( $stars_text ); ?></span>
<span class="counter-back" style="height:17px;width:100px;background-color:#ececec;float:left;">
<span class="counter-bar" style="width:<?php echo esc_attr( $width ); ?>px;height:17px;background-color:#ffc733;float:left;"></span>
</span>
</a>
<span class="counter-count" style="margin-left:5px;"><?php echo esc_html( number_format_i18n( $ratings_count ) ); ?></span>
</div>
<?php
}
?>
</div>
<div class="col-5">
<div>
<div style="font-weight:bold;"><?php _e( 'Average Rating', 'wporg-forums' ); ?></div>
<?php
echo \WPORG_Ratings::get_dashicons_stars( $this->avg_rating );
printf(
/* translators: 1: number of stars in rating, 2: total number of stars (5) */
__( '%1$s out of %2$s stars', 'wporg-forums' ),
round( isset( $this->avg_rating ) ? $this->avg_rating : 0, 1 ),
'<span>5</span>'
);
?>
<?php echo do_blocks( '<!-- wp:wporg/ratings-stars /-->' ); ?>
StevenDufresne marked this conversation as resolved.
Show resolved Hide resolved
<div class="reviews-submit-link">
<?php
if ( is_user_logged_in() ) {
Expand All @@ -297,6 +241,17 @@ public function do_view_header() {
?>
</div>
</div>
<div>
<div class="reviews-about" style="display:none;"><?php echo esc_html( $this->object->post_title ); ?></div>
<div class="reviews-total-count"><?php
printf(
/* translators: %s: number of reviews */
_n( '%s review', '%s reviews', $this->reviews_count, 'wporg-forums' ),
'<span>' . number_format_i18n( $this->reviews_count ) . '</span>'
);
?></div>
<?php echo do_blocks( '<!-- wp:wporg/ratings-bars /-->' ); ?>
</div>
</div>
<?php
// If current listing is filtered by rating, display message indicating this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Include locale specific styles.
*/
require_once get_theme_root( 'wporg-parent-2021' ) . '/wporg-parent-2021/inc/rosetta-styles.php';
require_once( __DIR__ . '/inc/block-config.php' );

/**
* Use the ‘Lead Topic’ uses the single topic part
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Set up configuration for dynamic blocks.
*/

StevenDufresne marked this conversation as resolved.
Show resolved Hide resolved
namespace WordPressdotorg\Forums\Block_Config;

/**
* Actions and filters.
*/
add_filter( 'render_block_context', __NAMESPACE__ . '\wporg_render_block_context', 10, 3 );
add_filter( 'wporg_ratings_data', __NAMESPACE__ . '\wporg_set_rating_data', 10, 2 );

/**
* Update ratings blocks with real rating data.
*
* @param array $data Rating data.
* @param int $post_id Current post.
*
* @return array
*/
function wporg_set_rating_data( $data, $post_id ) {
$post = wporg_support_get_compat_object();

if ( ! class_exists( '\WPORG_Ratings' ) ) {
return $data;
}
StevenDufresne marked this conversation as resolved.
Show resolved Hide resolved

$rating = \WPORG_Ratings::get_avg_rating( $post->type, $post->post_name ) ?: 0;
$ratings = \WPORG_Ratings::get_rating_counts( $post->type, $post->post_name ) ?: array();

/**
* Why do we multiply the average rating by 20?
* The themes API does it this way, and the rating plugin was built to fit that.
* Instead of redoing everything, multiplying here keeps things simple and works well.
*
* @see theme-directory/class-themes-api.php for more info.
*/
$adjusted_rating = $rating * 20;

return array(
'rating' => $adjusted_rating,
'ratingsCount' => array_sum( $ratings ),
'ratings' => $ratings,
'supportUrl' => esc_url( sprintf( home_url( '/%s/%s/reviews/' ), $post->type, $post->post_name ) )
);
}

/**
* Modifies the block context to include a `postId` for specific blocks.
*
* The `wporg/ratings-stars` and `wporg/ratings-bars` require a postId. Due to context, it's unavailable.
*
* @param array $context The current block context.
* @param array $parsed_block The block being rendered.
* @param WP_Block|null $parent_block Optional. The parent block, if any.
*
* @return array The modified block context.
*/
function wporg_render_block_context( $context, $parsed_block, $parent_block ) {
if ( isset( $parsed_block['blockName'] ) &&
in_array( $parsed_block['blockName'], [ 'wporg/ratings-stars', 'wporg/ratings-bars' ], true ) ) {

$compat_object = wporg_support_get_compat_object();

if ( $compat_object ) {
$context = array_merge( $context, [ 'postId' => $compat_object->ID ] );
}
}

return $context;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -1343,40 +1343,31 @@ div.bbp-breadcrumb {
// Review page
.review-ratings {

@extend .clear;
margin-bottom: ms(0);
padding-bottom: 10px;
margin-bottom: var(--wp--preset--spacing--20);
padding-bottom: var(--wp--preset--spacing--20);
border-bottom: 1px solid var(--wp--custom--color--border);
display: flex;
flex-direction: row-reverse;

.col-3 {
> div:first-child {
font-size: ms(-2);
margin: 0;
min-width: 40%;
border-top: 1px solid var(--wp--custom--color--border);
width: 60%;
min-width: 54%;
flex-shrink: 0;
}

> div:last-child {
margin: 0 0 10px 0;
padding-right: 10px;
min-width: 40%;
font-size: ms(-2);

.reviews-total-count {
font-weight: 700;
padding-bottom: 5px;
padding-top: 5px;
}

.counter-bar {
background-color: var(--wp--preset--color--pomegrade-1) !important;
}
}

.col-5 {
margin: 0 0 10px 0;
padding-right: 10px;
width: 60%;
min-width: 54%;
font-size: ms(-2);

> div:first-child {

@extend h4;
margin-top: 0;
}
Expand All @@ -1392,11 +1383,16 @@ div.bbp-breadcrumb {
}

@media (max-width: 499px) {
flex-direction: column-reverse;
flex-direction: column;

.col-3, .col-5 {
> div:first-child {
width: 100%;
margin-bottom: 20px;
}
}
}

.reviews-submit-link {
margin-top: 10px;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ h1,
h2,
h3,
h4,
.bbp-view .review-ratings .col-5 > div:first-child,
.bbp-view .review-ratings > div:last-child > div:first-child,
h5,
h6 {
margin: 0 0 var(--wp--style--block-gap);
Expand Down Expand Up @@ -1980,9 +1980,8 @@ fieldset label {
/*--------------------------------------------------------------
# Clearings
--------------------------------------------------------------*/
.clear::before, .bbp-view .review-ratings::before, .bbpress main#main::before,
.clear::before, .bbpress main#main::before,
.clear::after,
.bbp-view .review-ratings::after,
.bbpress main#main::after,
.entry-content::before,
.entry-content::after,
Expand All @@ -1999,7 +1998,7 @@ fieldset label {
table-layout: fixed;
}

.clear::after, .bbp-view .review-ratings::after, .bbpress main#main::after,
.clear::after, .bbpress main#main::after,
.entry-content::after,
.comment-content::after,
.site-header::after,
Expand Down Expand Up @@ -3557,52 +3556,50 @@ div.bbp-breadcrumb .bbp-breadcrumb-sep {
# Plugin / Theme specific support pages
--------------------------------------------------------------*/
.bbp-view .review-ratings {
margin-bottom: 1rem;
padding-bottom: 10px;
margin-bottom: var(--wp--preset--spacing--20);
padding-bottom: var(--wp--preset--spacing--20);
border-bottom: 1px solid var(--wp--custom--color--border);
display: flex;
flex-direction: row-reverse;
}
.bbp-view .review-ratings .col-3 {
.bbp-view .review-ratings > div:first-child {
font-size: 0.8rem;
margin: 0;
min-width: 40%;
border-top: 1px solid var(--wp--custom--color--border);
width: 60%;
min-width: 54%;
flex-shrink: 0;
}
.bbp-view .review-ratings .col-3 .reviews-total-count {
font-weight: 700;
padding-bottom: 5px;
padding-top: 5px;
}
.bbp-view .review-ratings .col-3 .counter-bar {
background-color: var(--wp--preset--color--pomegrade-1) !important;
}
.bbp-view .review-ratings .col-5 {
.bbp-view .review-ratings > div:last-child {
margin: 0 0 10px 0;
padding-left: 10px;
width: 60%;
min-width: 54%;
min-width: 40%;
font-size: 0.8rem;
}
.bbp-view .review-ratings .col-5 > div:first-child {
.bbp-view .review-ratings > div:last-child .reviews-total-count {
font-weight: 700;
padding-bottom: 5px;
padding-top: 5px;
}
.bbp-view .review-ratings > div:last-child > div:first-child {
margin-top: 0;
}
.bbp-view .review-ratings .col-5 .wporg-ratings {
.bbp-view .review-ratings > div:last-child .wporg-ratings {
display: inline-block;
margin-left: 1rem;
}
.bbp-view .review-ratings .col-5 .reviews-submit-link {
.bbp-view .review-ratings > div:last-child .reviews-submit-link {
margin-top: 1rem;
}
@media (max-width: 499px) {
.bbp-view .review-ratings {
flex-direction: column-reverse;
flex-direction: column;
}
.bbp-view .review-ratings .col-3, .bbp-view .review-ratings .col-5 {
.bbp-view .review-ratings > div:first-child {
width: 100%;
margin-bottom: 20px;
}
}
.bbp-view .reviews-submit-link {
margin-top: 10px;
}

/*--------------------------------------------------------------
# HelpHub Specific
Expand Down
Loading