PluginProbe
Polylang / 2.1
Polylang v2.1
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / widget-recent-posts.php

widget-recent-posts.php in Polylang 2.1, at include/widget-recent-posts.php

104 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! class_exists( 'WP_Widget_Recent_Posts' ) ) {
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_Posts extends WP_Widget_Recent_Posts {
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 $cache = array();
27 if ( ! $this->is_preview() ) {
28 $cache = wp_cache_get( 'widget_recent_posts', 'widget' );
29 }
30
31 if ( ! is_array( $cache ) ) {
32 $cache = array();
33 }
34
35 if ( ! isset( $args['widget_id'] ) ) {
36 $args['widget_id'] = $this->id;
37 }
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 ob_start();
46
47 $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );
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 $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
56
57 /**
58 * Filter the arguments for the Recent Posts widget.
59 *
60 * @since 3.4.0
61 *
62 * @see WP_Query::get_posts()
63 *
64 * @param array $args An array of arguments used to retrieve the recent posts.
65 */
66 $r = new WP_Query( apply_filters( 'widget_posts_args', array(
67 'posts_per_page' => $number,
68 'no_found_rows' => true,
69 'post_status' => 'publish',
70 'ignore_sticky_posts' => true
71 ) ) );
72
73 if ( $r->have_posts() ) :
74 ?>
75 <?php echo $args['before_widget']; ?>
76 <?php if ( $title ) {
77 echo $args['before_title'] . $title . $args['after_title'];
78 } ?>
79 <ul>
80 <?php while ( $r->have_posts() ) : $r->the_post(); ?>
81 <li>
82 <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
83 <?php if ( $show_date ) : ?>
84 <span class="post-date"><?php echo get_the_date(); ?></span>
85 <?php endif; ?>
86 </li>
87 <?php endwhile; ?>
88 </ul>
89 <?php echo $args['after_widget']; ?>
90 <?php
91 // Reset the global $the_post as this query will have stomped on it
92 wp_reset_postdata();
93
94 endif;
95
96 if ( ! $this->is_preview() ) {
97 $cache[ $args['widget_id'] ][ $lang ] = ob_get_flush(); #modified#
98 wp_cache_set( 'widget_recent_posts', $cache, 'widget' );
99 } else {
100 ob_end_flush();
101 }
102 }
103 }
104