forked from matthiassiegel/Related
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelated-widget.php
85 lines (63 loc) · 2.47 KB
/
related-widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
class Related_Widget extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'related_widget', 'description' => __('Related Posts Widget','related') );
parent::__construct('related_widget', __('Related Posts Widget','related'), $widget_ops);
$this->alt_option_name = 'related_widget';
add_action( 'save_post', array(&$this, 'flush_widget_cache') );
add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
}
function widget($args, $instance) {
$cache = wp_cache_get('related_widget', 'widget');
if ( !is_array($cache) )
$cache = array();
if ( ! isset( $args['widget_id'] ) )
$args['widget_id'] = $this->id;
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? __('Related Posts','related') : $instance['title'], $instance, $this->id_base);
if ( is_singular() ) {
global $related;
$related_str = $related->show( get_the_ID() );
if ( ! empty( $related_str ) ) {
echo $before_widget;
if ( $title ) echo $before_title . $title . $after_title;
echo $related_str;
echo $after_widget;
}
}
$cache[$args['widget_id']] = ob_get_flush();
wp_cache_set('related_widget', $cache, 'widget');
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['related_widget']) )
delete_option('related_widget');
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('related_widget', 'widget');
}
function form( $instance ) {
/*
* Set Default Value for widget form
*/
$default_value = array( "title"=> __('Related Posts','related') );
$instance = wp_parse_args( (array) $instance, $default_value );
$title = isset($instance['title']) ? esc_attr($instance['title']) : ''; ?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'related'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p><?php
}
}
function related_widget() {
register_widget('Related_Widget');
}
add_action('widgets_init', 'related_widget' );