-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathseries.php
500 lines (417 loc) · 19.5 KB
/
series.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<?php
/*
Plugin Name: Simple Post Series
Plugin URI: http://www.chaozh.com/simple-post-series-plugin-officially-publish/
Description: Better organize your posts by grouping them into series and display them within the series dynamically in your blog. This version of Post Series Plugin requires at least WordPress 3.1 and PHP 5.0+ to work.
Version: 2.5
Author: chaozh
Author URI: http://chaozh.com/
GitHub Plugin URI: https://github.com/chaozh/Post-Series
*/
### INSTALLATION/USAGE INSTRUCTIONS ###
// Installation and/or usage instructions for the Post Series Plugin
// can be found at http://www.chaozh.com/simple-post-series-plugin-officially-publish/
define('SERIES','series');
define('SERIES_BASE', 'simple-post-series');
define('VERSION', 2.5);
/* Copyright 2009-2012 CHAO ZHENG (email: [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
define('SERIES_URL', plugins_url( '', __FILE__ ));
define('SERIES_ROOT', dirname(__FILE__) );
define('SERIES_REL', dirname( plugin_basename( __FILE__ ) ));
define('SERIES_BASENAME', plugin_basename( __FILE__ ));
define('SERIES_FILE', __FILE__);
// aboud debug
define('SERIES_DEBUG',false);
// Adds translation support for language files
function series_localization() {
load_plugin_textdomain( SERIES_BASE, false, SERIES_REL. '/languages' );
}
add_action( 'plugins_loaded', 'series_localization' );
if ( ! function_exists('series_log')) {
function series_log ( $log ) {
if(WP_DEBUG === true && SERIES_DEBUG == true){
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
}
}
function series_plugin_uninstall(){
// Delete settings page options from options table
series_log("uninstall plugin and delete options: " . SERIES . "_options");
delete_option( SERIES . '_options' );
// drop a custom database table
// DELETE FROM wp_posts WHERE post_type = '要删除的post_type';
// DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT id FROM wp_posts);
// SELECT * FROM `wp_term_taxonomy` where taxonomy = 'series'; 82
// SELECT * FROM `wp_terms` WHERE `term_id` = 82;
// SELECT * FROM `wp_term_relationships` WHERE `term_taxonomy_id` = 82; object_id
global $wpdb;
$wpdb->query("DELETE FROM wp_terms WHERE term_id IN (SELECT term_id FROM wp_term_taxonomy where taxonomy = '". SERIES . "'");
$wpdb->query("DELETE FROM wp_term_relationships WHERE term_taxonomy_id IN (SELECT term_id FROM wp_term_taxonomy where taxonomy = '". SERIES . "'");
$wpdb->query("DELETE FROM wp_term_taxonomy WHERE taxonomy = '". SERIES . "'");
}
// register_deactivation_hook( __FILE__, 'series_plugin_uninstall' );
register_uninstall_hook( __FILE__, 'series_plugin_uninstall' );
function series_posttype_support() {
$args = array(
'public' => true,
'_builtin' => false
);
return apply_filters('series_posttype_support', array_merge(array('post', 'page'), get_post_types( $args )));
//return apply_filters('series_posttype_support', array('post', 'page') );
}
function series_get_default_options() {
$series_defaults_args = array(
'title_format' => __('This entry is part %current of %count in the series: %link', SERIES_BASE),
'class_prefix' => 'post-series',
'series_wrap' => 'section',
'title_wrap' => 'h3',
'show_future' => true,
'auto_display' => 0,
'custom_styles' => false,
'show_thumbnail' => false,
'show_excerpt' => false,
'show_nav' => false,
'loop_display' => false,
'auto_hide' => false
);
return apply_filters( SERIES . '_default_options', $series_defaults_args );
}
function series_register_taxonomy() {
$posttypes = series_posttype_support();
$labels = array(
'name' => _x('Series', 'taxonomy general name',SERIES_BASE),
'singular_name' => _x('Series', 'taxonomy singular name',SERIES_BASE),
'all_items' => __('All Series',SERIES_BASE),
'edit_item' => __('Edit Series',SERIES_BASE),
'update_item' => __('Update Series',SERIES_BASE),
'add_new_item' => __('Add New Series',SERIES_BASE),
'new_item_name' => __('New Series Name',SERIES_BASE),
'menu_name' => __('Series',SERIES_BASE)
);
$series_tax_args = array(
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => SERIES)
);
register_taxonomy( SERIES, $posttypes, $series_tax_args );
$options = get_option( SERIES.'_options', series_get_default_options());
if( $options['auto_display'] ){
add_filter('the_content', 'series_auto_content_display',0);
}
if( $options['loop_display'] ){
add_filter('the_content', 'series_auto_loop_display',0);
}
if( !$options['custom_styles'] ){
add_action( 'wp_print_styles', 'series_css' );
}
// deal with auto hide style and script
wp_enqueue_script( SERIES, SERIES_URL . '/autohide.js', array('jquery'), VERSION );
wp_enqueue_style( 'series_font', SERIES_URL . '/inc/icomoon/style.css', null, VERSION );
/*
* we will not include sereis archives template by default
* and this setting section is removed from plugin settings
if( $options['custom_archives'] ){
add_filter('template_include', 'series_set_template');
}
*/
}
add_action('init', 'series_register_taxonomy', 0);
// Adds CSS for the series
function series_css() {
if ( file_exists( get_stylesheet_directory()."/series.css" ) ) {
wp_enqueue_style( SERIES, get_stylesheet_directory_uri() . '/series.css', array(), VERSION );
}
elseif ( file_exists( get_template_directory()."/series.css" ) ) {
wp_enqueue_style( SERIES, get_template_directory_uri() . '/series.css', array(), VERSION );
}
else {
wp_enqueue_style( SERIES, SERIES_URL . '/series.css', array(), VERSION );
}
}
function series_is_template( $template_path ){
//Get template name
$template = basename($template_path);
//Check if template is taxonomy-series.php
//Check if template is taxonomy-series-{term-slug}.php
if( 1 == preg_match('/^taxonomy-series((-(\S*))?).php/',$template) )
return true;
return false;
}
function series_attrs($attrs, $options){
//TODO: try wp_parse_args instead
if( isset($options) && is_array($options)){
$attrs = $attrs + $options;
} else{
series_log("FAIL in ".__FUNCTION__.'for $options'.($options));
}
return $attrs;
}
function series_set_template( $template )
{
if( is_tax(SERIES) && !series_is_template($template) ){
$template = SERIES_ROOT."/template/taxonomy-".SERIES.".php";
}
return $template;
}
// Load admin functions if in the backend
if ( is_admin() ){
require_once(SERIES_ROOT. '/series-admin.php');
}
require_once(SERIES_ROOT. '/series-display.php');
// The shortcode function of Post Series
function series_sc($atts) {
$options = get_option( SERIES.'_options', series_get_default_options());
//fetch class_prefix options first
//this option is the only option that **must** sync with settings option
$class_prefix = 'post-series';
if( isset($options) && is_array($options))
$class_prefix = $options["class_prefix"];
//merge options
$series_arg = shortcode_atts( array (
"slug" => '',
"id" => '',
"title" => '',
"limit" => -1,
"show_future" => true,
"class_prefix" => $class_prefix
), $atts );
if( isset($atts['show_future']) && $atts['show_future'] == 'off'){
$series_arg['show_future'] = false;
}else{
$series_arg['show_future'] = true;
}
//new sc args overrides the option settings
$series_arg = series_attrs($series_arg, $options);
return series_display($series_arg);
}
add_shortcode('series','series_sc');
function series_auto_content_display($content) {
global $post;
if(is_single() || is_page() || is_feed()){
$options = get_option( SERIES.'_options', series_get_default_options());
$series_arg = array(
"limit" => -1
);
$series_arg =series_attrs($series_arg, $options);
$series_display = series_display($series_arg);
switch($options['auto_display']){
case 2:// At the end of post
$content = $content."\n".$series_display."\n";
break;
case 3:// At the begining of post
$content = $series_display."\n".$content."\n";
break;
case 4:
// Case of teaser
if(strpos($content, 'span id="more-')) {
$parts = preg_split('/(<span id="more-[0-9]*"><\/span>)/', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
$content = $parts[0].$parts[1].$series_display.$parts[2];
} // End of detect tag "more"
break;
}
}
return $content;
}
function series_auto_loop_display($content){
if( is_home() || is_front_page() || is_archive() ){
$options = get_option( SERIES.'_options', series_get_default_options());
$series_arg = array(
"limit" => -1,
'auto_hide'=>true
);
$series_arg = series_attrs($series_arg, $options);
$series_display = series_display($series_arg);
$content .= $series_display;
$pos1 = strpos($content, '<span id=”more-');
$pos2 = strpos($content, '</span>', $pos1);
$text1 = substr($content, 0, $pos2);
$text2 = substr($content, $pos2);
$text = $text1 . $series_display . $text2;
}
return $content;
}
class Post_Series_Widget extends WP_Widget {
/** constructor */
function __construct() {
$widget_ops = array(
'classname' => 'widget_' . SERIES,
'description' => __( "A simple widget to display post series.", SERIES_BASE )
);
parent::__construct( 'widget_' . SERIES, __('Post Series', SERIES_BASE), $widget_ops );
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') );
}
/** @use WP_Widget::widget */
function widget($args, $instance) {
$cache = wp_cache_get('widget_'.SERIES, 'widget');
if ( !is_array($cache) )
$cache = array();
if ( ! isset( $args['widget_id'] ) )
$args['widget_id'] = null;
if ( isset($cache[$args['widget_id']]) ) {
echo $cache[$args['widget_id']];
return;
}
$options = get_option( SERIES.'_options', series_get_default_options());
$series_arg = array(
"id" => intval($instance["id"]),
"limit" => intval($instance["limit"]),
"show_future" => isset($instance["show_future"])?$instance["show_future"]:false,
"class_prefix" => $instance["class_prefix"],
"show_nav" => false,
"title_format" => '',
"title_wrap" => ''
);
$series_arg = series_attrs($series_arg, $options);
$title = apply_filters( 'widget_title', empty( $instance['widget_title'] ) ? __( 'Post Series List', SERIES_BASE) : $instance['widget_title'], $instance, $this->id_base );
ob_start();
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
echo series_display($series_arg);
echo $args['after_widget'];
$cache[$args['widget_id']] = ob_get_flush();
wp_cache_set('widget_'.SERIES, $cache, 'widget');
}
function flush_widget_cache() {
wp_cache_delete( 'widget_'.SERIES, 'widget' );
}
/** @use WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['widget_title'] = sanitize_text_field($new_instance['widget_title']);
$instance['id'] = absint($new_instance['id']);
if( empty($new_instance['limit']) )
$instance['limit'] = -1;
$instance['class_prefix'] = sanitize_text_field($new_instance['class_prefix']);
$instance['show_future'] = $new_instance['show_future'] == "on" ? true : false;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['widget_'.SERIES]) )
delete_option('widget_'.SERIES);
return $instance;
}
/** @use WP_Widget::form */
function form($instance) {
$options = get_option( SERIES.'_options', series_get_default_options());
$instance = wp_parse_args( (array) $instance, array(
'id' => -1,
'widget_title' => '',
'class_prefix' => $options['class_prefix'],
'show_future' => $options['show_future']
) );
if( empty($instance['limit']) || $instance['limit'] == -1)
$limit = '';
else
$limit = $instance['limit'];
$id = $instance['id'];
$class_prefix = sanitize_text_field($instance['class_prefix']);
$show_future = $instance['show_future'];
$widget_title = sanitize_text_field($instance['widget_title']);
?>
<p>
<label for="<?php echo $this->get_field_id('widget_title'); ?>"><?php _e('Title:'); ?> </label>
<input id="<?php echo $this->get_field_id('widget_title'); ?>" name="<?php echo $this->get_field_name('widget_title'); ?>" class="widefat" type="text" value="<?php echo esc_attr($widget_title); ?>" />
</p>
<?php
$series = get_terms( SERIES );
if( count($series) > 0 ): ?>
<p>
<label for="<?php echo $this->get_field_id('id'); ?>"><?php _e('Choose post series to display:', SERIES_BASE); ?> </label>
<select id="<?php echo $this->get_field_id('id'); ?>" name="<?php echo $this->get_field_name('id'); ?>" class="widefat">
<?php foreach( $series as $term ): ?>
<option value="<?php echo $term->term_id; ?>" <?php echo ($id == $term->term_id) ? 'selected="selected"':""; ?>><?php echo $term->name . "(" . $term->count . ")"; ?></option>
<?php endforeach; ?>
</select>
</p>
<?php endif; ?>
<p>
<label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e('Number of posts to show:', SERIES_BASE); ?> </label>
<input id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" class="widefat" type="text" value="<?php echo $limit; ?>" size="3" />
</p>
<p>
<label for="<?php echo $this->get_field_id('class_prefix'); ?>"><?php _e('Special class prefix:', SERIES_BASE); ?> </label>
<input id="<?php echo $this->get_field_id('class_prefix'); ?>" name="<?php echo $this->get_field_name('class_prefix'); ?>" class="widefat" type="text" value="<?php echo $class_prefix; ?>" />
</p>
<p>
<input id="show_future" type="checkbox" name="<?php echo $this->get_field_name('show_future'); ?>" value="on" <?php checked( $show_future ); ?> />
<label for="show_future" class="future-on-label"><?php _e('Show future',SERIES_BASE);?></label>
</p>
<?php
}
}
// for displaying list
class Series_List_Widget extends WP_Widget {
/** constructor */
function __construct() {
$widget_ops = array(
'classname' => 'widget_list_' . SERIES,
'description' => __( "A simple widget to display all post series in list.", SERIES_BASE )
);
parent::__construct( 'widget_list_' . SERIES, __('Post Series List', SERIES_BASE), $widget_ops );
}
function widget( $args, $instance ) {
// Widget output
$title = apply_filters( 'widget_title', empty( $instance['widget_title'] ) ? __( 'Post Series List', SERIES_BASE) : $instance['widget_title'], $instance, $this->id_base );
$list_args = array(
"count" => ! empty( $instance['count'] ) ? '1' : '0',
"hierarchical" => !empty( $instance['hierarchical'] ) ? '1' : '0'
);
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
echo series_list_display(apply_filters( 'widget_series_list_args', $list_args ));
echo $args['after_widget'];
}
function update( $new_instance, $old_instance ) {
// Save widget options
$instance = $old_instance;
$instance['widget_title'] = sanitize_text_field( $new_instance['widget_title'] );
$instance['count'] = !empty($new_instance['count']) ? 1 : 0;
$instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
return $instance;
}
function form( $instance ) {
// Output admin widget options form
// title | number | category | hirechy
$instance = wp_parse_args( (array) $instance, array( 'widget_title' => '') );
$widget_title = sanitize_text_field( $instance['widget_title'] );
$count = isset($instance['count']) ? (bool) $instance['count'] :false;
$hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
?>
<p><label for="<?php echo $this->get_field_id('widget_title'); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('widget_title'); ?>" name="<?php echo $this->get_field_name('widget_title'); ?>" type="text" value="<?php echo esc_attr( $widget_title ); ?>" /></p>
<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br />
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> />
<label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>
<?php
}
}
function series_register_widgets() {
register_widget( 'Post_Series_Widget' );
register_widget( 'Series_List_Widget' );
}
add_action( 'widgets_init', 'series_register_widgets' );
?>