| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! class_exists( 'WP_Widget_Recent_Comments' ) ) { |
| 4 |
require_once( ABSPATH . '/wp-includes/default-widgets.php' ); |
| 5 |
} |
| 6 |
|
| 7 |
/* |
| 8 |
* backward compatibility with WP < 4.4 |
| 9 |
* obliged to rewrite the whole functionnality to have a language dependant cache key |
| 10 |
* code base is WP 4.2 |
| 11 |
* |
| 12 |
* @since 1.5 |
| 13 |
*/ |
| 14 |
class PLL_Widget_Recent_Comments extends WP_Widget_Recent_Comments { |
| 15 |
|
| 16 |
/* |
| 17 |
* displays the widget |
| 18 |
* modified version of the parent function to call to use a language dependant cache key |
| 19 |
* |
| 20 |
* @since 1.5 |
| 21 |
* |
| 22 |
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 23 |
* @param array $instance The settings for the particular instance of the widget |
| 24 |
*/ |
| 25 |
public function widget( $args, $instance ) { |
| 26 |
global $comments, $comment; |
| 27 |
|
| 28 |
$cache = array(); |
| 29 |
if ( ! $this->is_preview() ) { |
| 30 |
$cache = wp_cache_get( 'widget_recent_comments', 'widget' ); |
| 31 |
} |
| 32 |
if ( ! is_array( $cache ) ) { |
| 33 |
$cache = array(); |
| 34 |
} |
| 35 |
|
| 36 |
if ( ! isset( $args['widget_id'] ) ) |
| 37 |
$args['widget_id'] = $this->id; |
| 38 |
|
| 39 |
$lang = pll_current_language(); #added |
| 40 |
if ( isset( $cache[ $args['widget_id'] ][ $lang ] ) ) { #modified# |
| 41 |
echo $cache[ $args['widget_id'] ][ $lang ]; #modified# |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$output = ''; |
| 46 |
|
| 47 |
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Comments' ); |
| 48 |
|
| 49 |
/** This filter is documented in wp-includes/default-widgets.php */ |
| 50 |
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
| 51 |
|
| 52 |
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; |
| 53 |
if ( ! $number ) |
| 54 |
$number = 5; |
| 55 |
|
| 56 |
/** |
| 57 |
* Filter the arguments for the Recent Comments widget. |
| 58 |
* |
| 59 |
* @since 3.4.0 |
| 60 |
* |
| 61 |
* @see WP_Comment_Query::query() for information on accepted arguments. |
| 62 |
* |
| 63 |
* @param array $comment_args An array of arguments used to retrieve the recent comments. |
| 64 |
*/ |
| 65 |
$comments = get_comments( apply_filters( 'widget_comments_args', array( |
| 66 |
'number' => $number, |
| 67 |
'status' => 'approve', |
| 68 |
'post_status' => 'publish' |
| 69 |
) ) ); |
| 70 |
|
| 71 |
$output .= $args['before_widget']; |
| 72 |
if ( $title ) { |
| 73 |
$output .= $args['before_title'] . $title . $args['after_title']; |
| 74 |
} |
| 75 |
|
| 76 |
$output .= '<ul id="recentcomments">'; |
| 77 |
if ( $comments ) { |
| 78 |
// Prime cache for associated posts. (Prime post term cache if we need it for permalinks.) |
| 79 |
$post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) ); |
| 80 |
_prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false ); |
| 81 |
|
| 82 |
foreach ( (array) $comments as $comment ) { |
| 83 |
$output .= '<li class="recentcomments">'; |
| 84 |
/* translators: comments widget: 1: comment author, 2: post link */ |
| 85 |
$output .= sprintf( _x( '%1$s on %2$s', 'widgets' ), |
| 86 |
'<span class="comment-author-link">' . get_comment_author_link() . '</span>', |
| 87 |
'<a href="' . esc_url( get_comment_link( $comment->comment_ID ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>' |
| 88 |
); |
| 89 |
$output .= '</li>'; |
| 90 |
} |
| 91 |
} |
| 92 |
$output .= '</ul>'; |
| 93 |
$output .= $args['after_widget']; |
| 94 |
|
| 95 |
echo $output; |
| 96 |
|
| 97 |
if ( ! $this->is_preview() ) { |
| 98 |
$cache[ $args['widget_id'] ][ $lang ] = $output; #modified# |
| 99 |
wp_cache_set( 'widget_recent_comments', $cache, 'widget' ); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|