/
simple-microblogging.php
256 lines (230 loc) · 7.3 KB
/
simple-microblogging.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/*
* Plugin Name: Simple microblogging
* Description: Use your wordpress site as a microblog; display the microposts in a widget or using a shortcode.
* Version: 0.1
* Author: Samuel Coskey, Victoria Gitman
* Author URI: http://boolesrings.org
*/
/*
* Create the new post type
*/
add_action( 'init', 'create_micropost_type' );
function create_micropost_type() {
register_post_type( 'micropost',
array(
'labels' => array(
'name' => __( 'Microposts' ),
'singular_name' => __( 'Micropost' ),
),
'has_archive' => true,
'menu_icon' => plugins_url( 'microblogging-icon.png', __FILE__ ),
'menu_position' => 5,
'public' => true,
'rewrite' => array( 'slug' => 'microposts' ),
'supports' => array( 'title', 'editor', 'author', 'comments' ),
// uncomment to support categories and tags:
// 'taxonomies' => array ( 'category', 'post_tag' ),
)
);
}
/*
* Tells wordpress to reset its permalink structure, to accommodate the new post type
*/
register_activation_hook( __FILE__, 'my_rewrite_flush' );
function my_rewrite_flush()
{
create_micropost_type();
flush_rewrite_rules();
}
/*
* Microblog widget code
*/
add_action('widgets_init', 'ahspfc_load_widgets');
function ahspfc_load_widgets() {
register_widget('microblog_widget');
}
class microblog_widget extends WP_Widget {
function microblog_widget() {
$widget_ops = array(
'classname' => 'microblog_widget',
'description' => 'Allows you to display a list of microblog entries while excluding them from posts.',
);
$control_ops = array(
'id_base' => 'microblog-widget',
);
$this->WP_Widget('microblog-widget', 'Microblog', $widget_ops, $control_ops );
}
function form ($instance) {
$defaults = array(
'numberposts' => '5',
'title' => '',
'rss' => '',
);
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
<input type="text" name="<?php echo $this->get_field_name('title') ?>" id="<?php echo $this->get_field_id('title') ?> " value="<?php echo $instance['title'] ?>" size="20">
</p>
<p>
<label for="<?php echo $this->get_field_id('numberposts'); ?>">Number of posts:</label>
<input type="text" name="<?php echo $this->get_field_name('numberposts'); ?>" id="<?php echo $this->get_field_id('numberposts'); ?>" value="<?php echo $instance['numberposts']; ?>">
</p>
<p>
<input type="checkbox" id="<?php echo $this->get_field_id('use_excerpt'); ?>" name="<?php echo $this->get_field_name('use_excerpt'); ?>" <?php if ($instance['use_excerpt']) echo 'checked="checked"' ?> />
<label for="<?php echo $this->get_field_id('use_excerpt'); ?>">Show excerpts only?</label>
</p>
<p>
<input type="checkbox" id="<?php echo $this->get_field_id('rss'); ?>" name="<?php echo $this->get_field_name('rss'); ?>" <?php if ($instance['rss']) echo 'checked="checked"' ?> />
<label for="<?php echo $this->get_field_id('rss'); ?>">Show RSS feed link?</label>
</p>
<?php
}
function update ($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['numberposts'] = $new_instance['numberposts'];
$instance['use_excerpt'] = $new_instance['use_excerpt'];
$instance['rss'] = $new_instance['rss'];
return $instance;
}
function widget ($args,$instance) {
extract($args);
$title = $instance['title'];
$numberposts = $instance['numberposts'];
$use_excerpt = $instance['use_excerpt'];
$rss = $instance['rss'];
// retrieve posts information from database
global $post;
$query = "post_type=micropost&posts_per_page=" . $numberposts;
$query_results = new WP_Query($query);
// build the widget contents!
$out = "<ul>";
while ( $query_results->have_posts() ) {
$query_results->the_post();
$out .= "<li>";
$post_title = the_title( '', '', false );
if ( $post_title ) {
$out .= "<span class='microblog-widget-post-title'>"
. $post_title
. " </span>";
}
$out .= "<span class='microblog-widget-post-content'>";
if ( $use_excerpt ) {
add_filter('excerpt_more', 'micropost_excerpt_more');
$out .= get_the_excerpt();
remove_filter('excerpt_more', 'micropost_excerpt_more');
} else {
$out .= $post->post_content;
}
$out .= "</span>";
$out .= "<span lass='microblog-widget-commentlink'>";
$out .= " <a href='" . get_permalink() . "'>";
$out .= "<img width='14px' src='"
. site_url() . "/wp-includes/images/wlw/wp-comments.png'>";
$out .= "×" . get_comments_number();
$out .= '</a>';
$out .= "</span>\n";
$out .= "</li>\n";
}
$out .= "</ul>";
//print the widget for the sidebar
echo $before_widget;
echo $before_title;
echo $title;
if ($rss) {
echo ' <a href="' . get_site_url() . '/feed/?post_type=micropost" class="rss">';
echo '<img src="' . site_url() . '/wp-includes/images/rss.png"/>';
echo '</a>';
}
echo $after_title;
echo $out;
echo $after_widget;
// clean up
wp_reset_postdata();
}
}
/*
* Microblog shortcode code
*/
add_shortcode( 'microblog', 'microblog_shortcode' );
function microblog_shortcode($atts) {
global $post;
// Arguments to the shortcode
extract( shortcode_atts( array(
'num' => '5',
'null_text' => '(none)',
'show_date' => '',
'date_format' => get_option('date_format'), // I recommend 'F j'
'use_excerpt' => '',
'q' => '',
), $atts ) );
if ( $q ) {
$q = str_replace ( "&", "&", $q );
}
/*
* query the database for tweets!
* query syntax:
* http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
*/
$query .= "post_type=micropost&posts_per_page=" . $num;
if ( $q ) {
$query .= "&" . $q;
}
$query_results = new WP_Query($query);
if ( $query_results->post_count == 0 ) {
return "<p>" . wp_kses($null_text,array()) . "</p>\n";
}
$out = "<ul class='microblog-shortcode'>\n";
while ( $query_results->have_posts() ) {
$query_results->the_post();
$out .= "<li>";
if ( $show_date) {
$out .= "<span class='microblog-shortcode-date'>"
. get_the_date($date_format)
. "</span>";
$out .= "<span class='microblog-shortcode-date-sep'>: </span>\n";
}
$post_title = the_title( '', '', false );
if ( $post_title ) {
$out .= "<span class='microblog-shortcode-post-title'>"
. $post_title
. " </span>";
}
$out .= "<span class='microblog-shortcode-post-content'>";
if ( $use_excerpt ) {
add_filter('excerpt_more', 'micropost_excerpt_more');
$out .= get_the_excerpt();
remove_filter('excerpt_more', 'micropost_excerpt_more');
} else {
$out .= $post->post_content;
}
$out .= "</span>";
$out .= "<span class='microblog-shortcode-commentlink'>";
$out .= " <a href='" . get_permalink() . "'>";
$out .= "<img width='14px' src='"
. site_url() . "/wp-includes/images/wlw/wp-comments.png'>";
$out .= "×" . get_comments_number();
$out .= "</a>";
$out .= "</span>\n";
$out .= "</li>\n";
}
$out .= "</ul>";
// clean up
wp_reset_postdata();
return $out;
}
function micropost_excerpt_more($more) {
return ' ...';
}
/*
* Load our default style sheet
*/
add_action( 'wp_print_styles', 'microblog_enqueue_styles' );
function microblog_enqueue_styles() {
wp_register_style( 'simple-microblogging',
plugins_url('simple-microblogging.css', __FILE__) );
wp_enqueue_style( 'simple-microblogging' );
}
?>