Changeset 4311
- Timestamp:
- 10/31/2016 02:47:42 AM (8 years ago)
- Location:
- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-plugin.php
r4310 r4311 3 3 use WordPressdotorg\Plugin_Directory\Plugin_Directory; 4 4 use WordPressdotorg\Plugin_Directory\Template; 5 use WordPressdotorg\Plugin_Directory\Tools; 5 6 use WordPressdotorg\Plugin_Directory\API\Base; 6 7 use WP_REST_Server; … … 105 106 $_pages = preg_split( "#<!--section=(.+?)-->#", $post->post_content, - 1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); 106 107 for ( $i = 0; $i < count( $_pages ); $i += 2 ) { 107 $result['sections'][ $_pages[ $i ] ] = apply_filters( 'the_content', $_pages[ $i + 1 ] );108 $result['sections'][ $_pages[ $i ] ] = apply_filters( 'the_content', $_pages[ $i + 1 ], $_pages[ $i ] ); 108 109 } 109 110 $result['sections']['reviews'] = $this->get_plugin_reviews_markup( $post->post_name ); … … 204 205 * Returns a HTML formatted representation of the latest 10 reviews for a plugin. 205 206 * 207 * This intentionally uses different markup than what the theme uses, as it's for display within the WordPress Administration area. 208 * 206 209 * @param string $plugin_slug The plugin slug. 207 210 * @return string HTML blob of data. … … 209 212 protected function get_plugin_reviews_markup( $plugin_slug ) { 210 213 $output = ''; 211 foreach ( $this->get_plugin_reviews_data( $plugin_slug) as $review ) {214 foreach ( Tools::get_plugin_reviews( $plugin_slug, 10 ) as $review ) { 212 215 $output .= $this->get_plugin_reviews_markup_singular( $review ); 213 216 } … … 218 221 * Generates a HTML blob for a single review. 219 222 * 220 * @param object $review Single row of data from `self::get_plugin_reviews_data()`223 * @param object $review The review data. 221 224 * @return string Blob of HTML representing the review. 222 225 */ 223 226 protected function get_plugin_reviews_markup_singular( $review ) { 224 $reviewer = get_user_by( 'id', $review-> topic_poster );227 $reviewer = get_user_by( 'id', $review->post_author ); 225 228 ob_start(); 226 227 // Copied from bb-theme/wporg/_reviews.php, with quite a few things stripped out.228 229 ?> 229 230 <div class="review"> … … 231 232 <div class="reviewer-info"> 232 233 <div class="review-title-section"> 233 <h4 class="review-title"><?php echo $review->topic_title; ?></h4>234 <h4 class="review-title"><?php echo esc_html( $review->post_title ); ?></h4> 234 235 <div class="star-rating"><?php 235 236 /* Core has .star-rating .star colour styling, which is why we use a custom wrapper and template */ 236 237 echo Template::dashicons_stars( array( 237 'rating' => wporg_get_rating( $review->topic_id ),238 'rating' => $review->post_rating, 238 239 'template' => '<span class="star %1$s"></span>', 239 240 ) ); … … 241 242 </div> 242 243 <p class="reviewer"> 243 By <a href="https://profiles.wordpress.org/<?php echo $reviewer->user_nicename; ?>"><?php echo get_avatar( $review->topic_poster, 16, 'monsterid' ); ?></a> 244 <a href="https://profiles.wordpress.org/<?php echo $reviewer->user_nicename; ?>" class="reviewer-name"><?php 245 echo $reviewer->display_name; 246 244 <?php 245 $review_author_markup_profile = esc_url( 'https://profiles.wordpress.org/' . $reviewer->user_nicename ); 246 $review_author_markup = '<a href="' . $review_author_markup_profile . '">'; 247 $review_author_markup .= get_avatar( $reviewer->ID, 16, 'monsterid' ) . '</a>'; 248 $review_author_markup .= '<a href="' . $review_author_markup_profile . '" class="reviewer-name">'; 249 $review_author_markup .= $reviewer->display_name; 247 250 if ( $reviewer->display_name != $reviewer->user_login ) { 248 echo" <small>({$reviewer->user_login})</small>";251 $review_author_markup .= " <small>({$reviewer->user_login})</small>"; 249 252 } 250 ?></a><?php 251 /* // Display author badge next to the person's name if they're reviewing their own thing 252 if ( class_exists( '\WPORG_Extend_Author_Badge' ) ) { 253 echo \WPORG_Extend_Author_Badge::get_instance()->show_author_badge( '', $post->post_id ); 254 } */ 255 ?>, 256 <span class="review-date"><?php echo gmdate( 'F j, Y', strtotime( $review->topic_start_time ) ); ?></span> 257 <?php if ( $review->wp_version ) : ?> 258 <span class="review-wp-version">for WordPress <?php echo $review->wp_version; ?></span> 259 <?php endif; ?> 253 $review_author_markup .= '</a>'; 254 255 printf( __( 'By %1$s on %2$s', 'wporg-plugins' ), 256 $review_author_markup, 257 '<span class="review-date">' . gmdate( 'F j, Y', strtotime( $review->post_modified ) ) . '</span>' 258 ); 259 ?> 260 260 </p> 261 261 </div> 262 262 </div> 263 <div class="review-body"><?php echo $review->post_ text; ?></div>263 <div class="review-body"><?php echo $review->post_content; ?></div> 264 264 </div> 265 265 <?php … … 268 268 } 269 269 270 /**271 * Fetch the latest 10 reviews for a given plugin from the database.272 *273 * This uses raw SQL to query the bbPress tables to fetch reviews.274 *275 * @param string $plugin_slug The slug of the plugin.276 * @return array An array of review details.277 */278 protected function get_plugin_reviews_data( $plugin_slug ) {279 global $wpdb;280 if ( ! defined( 'WPORGPATH' ) || ! defined( 'CUSTOM_USER_TABLE' ) ) {281 // Reviews are stored in the main supoport forum, which isn't open source yet.282 return array();283 }284 285 if ( $reviews = wp_cache_get( $plugin_slug, 'reviews' ) ) {286 return $reviews;287 }288 289 // The forums are the source for users, and also where reviews live.290 $table_prefix = str_replace( 'users', '', CUSTOM_USER_TABLE );291 $forum_id = 18; // The Review Forums ID292 293 $reviews = $wpdb->get_results( $wpdb->prepare( "294 SELECT295 t.topic_id, t.topic_title, t.topic_poster, t.topic_start_time,296 p.post_text,297 tm_wp.meta_value as wp_version298 FROM {$table_prefix}topics AS t299 JOIN {$table_prefix}meta AS tm ON ( tm.object_type = 'bb_topic' AND t.topic_id = tm.object_id AND tm.meta_key = 'is_plugin' )300 JOIN {$table_prefix}posts as p ON ( t.topic_id = p.topic_id AND post_status = 0 AND post_position = 1 )301 LEFT JOIN {$table_prefix}meta AS tm_wp ON ( tm_wp.object_type = 'bb_topic' AND t.topic_id = tm_wp.object_id AND tm_wp.meta_key = 'wp_version' )302 WHERE t.forum_id = %d AND t.topic_status = 0 AND t.topic_sticky = 0 AND tm.meta_value = %s303 ORDER BY t.topic_start_time DESC304 LIMIT 10",305 $forum_id,306 $plugin_slug307 ) );308 309 wp_cache_set( $plugin_slug, $reviews, 'reviews' );310 return $reviews;311 }312 270 } 313 271 -
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-tools.php
r4214 r4311 45 45 * @return array|false 46 46 */ 47 public static function get_plugin_reviews( $plugin_slug ) { 48 if ( false === ( $reviews = wp_cache_get( "{$plugin_slug}_reviews", 'wporg-plugins' ) ) ) { 47 public static function get_plugin_reviews( $plugin_slug, $number = 2 ) { 48 $number = absint( $number ); 49 if ( $number < 1 || $number > 100 ) { 50 $number = 2; 51 } 52 if ( false === ( $reviews = wp_cache_get( "{$plugin_slug}_last{$number}_reviews", 'wporg-plugins' ) ) ) { 49 53 global $wpdb; 50 54 51 $reviews = $wpdb->get_results( $wpdb->prepare( " 52 SELECT posts.post_text AS post_content, topics.topic_title AS post_title, ratings.rating AS post_rating, ratings.user_id AS post_author 53 FROM ratings 54 INNER JOIN minibb_topics AS topics ON ( ratings.review_id = topics.topic_id ) 55 INNER JOIN minibb_posts AS posts ON ( ratings.review_id = posts.topic_id ) 56 WHERE 57 ratings.object_type = 'plugin' AND 58 ratings.object_slug = %s AND 59 posts.post_position = 1 AND 60 topics.topic_status = 0 AND 61 topics.topic_sticky = 0 62 ORDER BY ratings.review_id DESC LIMIT 2", $plugin_slug ) ); 63 64 wp_cache_set( "{$plugin_slug}_reviews", $reviews, 'wporg-plugins', HOUR_IN_SECONDS ); 55 $reviews = $wpdb->get_results( $wpdb->prepare( 56 "SELECT 57 post_content, post_title, post_author, post_modified, 58 r.rating as post_rating 59 FROM ratings r 60 LEFT JOIN wporg_419_posts p ON r.post_id = p.ID 61 WHERE r.object_type = 'plugin' AND r.object_slug = %s AND p.post_status = 'publish' 62 ORDER BY r.review_id DESC 63 LIMIT %d", $plugin_slug, $number ) ); 64 65 wp_cache_set( "{$plugin_slug}_last{$number}_reviews", $reviews, 'wporg-plugins', HOUR_IN_SECONDS ); 65 66 } 66 67
Note: See TracChangeset
for help on using the changeset viewer.