| 1 |
<?php |
| 2 |
|
| 3 |
if(!class_exists('WP_Widget_Recent_Posts')){ |
| 4 |
require_once( ABSPATH . '/wp-includes/default-widgets.php' ); |
| 5 |
} |
| 6 |
|
| 7 |
/* |
| 8 |
* obliged to rewrite the whole functionnality to have a language dependant cache key |
| 9 |
* code base is WP 3.9.1 |
| 10 |
* |
| 11 |
* @since 1.5 |
| 12 |
*/ |
| 13 |
class PLL_Widget_Recent_Posts extends WP_Widget_Recent_Posts { |
| 14 |
|
| 15 |
/* |
| 16 |
* displays the widget |
| 17 |
* modified version of the parent function to call to use a language dependant cache key |
| 18 |
* |
| 19 |
* @since 1.5 |
| 20 |
* |
| 21 |
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 22 |
* @param array $instance The settings for the particular instance of the widget |
| 23 |
*/ |
| 24 |
function widget($args, $instance) { |
| 25 |
$cache = array(); |
| 26 |
if ( ! $this->is_preview() ) { |
| 27 |
$cache = wp_cache_get( 'widget_recent_posts', 'widget' ); |
| 28 |
} |
| 29 |
|
| 30 |
if ( ! is_array( $cache ) ) { |
| 31 |
$cache = array(); |
| 32 |
} |
| 33 |
|
| 34 |
if ( ! isset( $args['widget_id'] ) ) { |
| 35 |
$args['widget_id'] = $this->id; |
| 36 |
} |
| 37 |
|
| 38 |
$lang = pll_current_language(); #added |
| 39 |
if ( isset( $cache[ $args['widget_id'] ] [$lang] ) ) { #modified# |
| 40 |
echo $cache[ $args['widget_id'] ] [$lang]; #modified# |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
ob_start(); |
| 45 |
extract($args); |
| 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 $before_widget; ?> |
| 76 |
<?php if ( $title ) echo $before_title . $title . $after_title; ?> |
| 77 |
<ul> |
| 78 |
<?php while ( $r->have_posts() ) : $r->the_post(); ?> |
| 79 |
<li> |
| 80 |
<a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a> |
| 81 |
<?php if ( $show_date ) : ?> |
| 82 |
<span class="post-date"><?php echo get_the_date(); ?></span> |
| 83 |
<?php endif; ?> |
| 84 |
</li> |
| 85 |
<?php endwhile; ?> |
| 86 |
</ul> |
| 87 |
<?php echo $after_widget; ?> |
| 88 |
<?php |
| 89 |
// Reset the global $the_post as this query will have stomped on it |
| 90 |
wp_reset_postdata(); |
| 91 |
|
| 92 |
endif; |
| 93 |
|
| 94 |
if ( ! $this->is_preview() ) { |
| 95 |
$cache[ $args['widget_id'] ] [$lang] = ob_get_flush(); #modified# |
| 96 |
wp_cache_set( 'widget_recent_posts', $cache, 'widget' ); |
| 97 |
} else { |
| 98 |
ob_end_flush(); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/* |
| 103 |
* backward compatibility with WP < 3.9 |
| 104 |
* |
| 105 |
* @since 1.5 |
| 106 |
* |
| 107 |
* @return bool |
| 108 |
*/ |
| 109 |
function is_preview() { |
| 110 |
return version_compare($GLOBALS['wp_version'], '3.9', '<') ? false : parent::is_preview(); |
| 111 |
} |
| 112 |
} |
| 113 |
|