-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdraft-feed.php
287 lines (232 loc) · 7.58 KB
/
draft-feed.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
<?php
/**
* Plugin Name: Drafts Feed
* Plugin URI: http://bueltge.de/wordpress-feed-fuer-entwuerfe/829/
* Description: Add a new Feed for drafts: <code>/?feed=drafts</code> or with active permalinks <code>/feed/drafts</code>
* Version: 1.1.0
* Author: Frank Bültge
* Author URI: http://bueltge.de/
* Licence: GPLv2
* Last Change: 03/05/2014
*/
//avoid direct calls to this file, because now WP core and framework has been used
if ( ! function_exists( 'add_filter' ) ) {
header('Status: 403 Forbidden');
header('HTTP/1.1 403 Forbidden');
exit();
}
if ( ! class_exists( 'Draft_Feed' ) ) {
add_action( 'plugins_loaded', array( 'Draft_Feed', 'init' ) );
class Draft_Feed {
protected static $classobj = NULL;
public static $feed_slug = 'drafts';
public static $widget_slug = 'dashboard_recent_drafts_all_authors';
public static $options_slug = 'draft_feed_options';
/**
* Handler for the action 'init'. Instantiates this class.
*
* @access public
* @return $classobj
*/
public static function init() {
NULL === self::$classobj && self::$classobj = new self();
return self::$classobj;
}
/**
* Constructor, init in WP
*
* @return void
*/
public function __construct() {
$options = $this->get_options();
// if options allow the draft feed
if ( 1 === $options[ 'feed' ] ) {
// add custom feed
add_action( 'init', array( $this, 'add_draft_feed' ) );
// change query for custom feed
add_action( 'pre_get_posts', array( $this, 'feed_content' ) );
}
// add dashboard widget
add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widget') );
// add multilingual possibility, load lang file
add_action( 'admin_init', array( $this, 'textdomain') );
}
/**
* Load language file for translations
*
* @return void
*/
public function textdomain() {
$locale = get_locale();
if ( 'en_US' === $locale )
return;
load_plugin_textdomain( 'draft_feed', FALSE, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Return the drafts
*
* @param Integer $post_per_page for count of drafts
* @return Array
*/
public function get_drafts() {
$options = $this->get_options();
$args = array(
'post_type' => 'post',
'post_status' => 'draft',
'posts_per_page' => $options[ 'posts_per_page' ],
'orderby' => 'modified',
'order' => 'DESC'
);
$drafts_query = new WP_Query( $args );
return $drafts_query->posts;
}
/**
* Customizes the query.
* It will bail if $query is not an object, filters are suppressed
* and it's not our feed query.
*
* @param WP_Query $query The current query
* @return void
*/
public function feed_content( $query ) {
// Bail if $query is not an object or of incorrect class
if ( ! $query instanceof WP_Query )
return;
// Bail if filters are suppressed on this query
if ( $query->get( 'suppress_filters' ) )
return;
// Bail if it's not our feed
if ( ! $query->is_feed( self::$feed_slug ) )
return;
$query->set( 'post_status', array( 'draft' ) );
$query->set( 'orderby', 'modified' );
}
/**
* Get dashbaord content
*
* @param Array $drafts
* @return void
*/
public function dashboard_recent_drafts( $drafts = FALSE ) {
if ( ! $drafts )
$drafts = $this->get_drafts();
if ( $drafts && is_array( $drafts ) ) {
// Get options
$options = $this->get_options();
// Count all draft posts
$count = (int) wp_count_posts()->draft;
$list = array();
foreach ( $drafts as $draft ) {
$url = get_edit_post_link( $draft->ID );
$title = _draft_or_post_title( $draft->ID );
$user = get_userdata( $draft->post_author );
$author = $user->display_name;
$item = "<h4><a href='$url' title='" . sprintf( __( 'Edit “%s”' ), esc_attr( $title ) ) . "'>"
. esc_html( $title ) . "</a> <small>" . sprintf( __( 'by %s, ', 'draft_feed' ), esc_attr( $author ) ) . "<abbr title='" . get_the_time( __( 'Y/m/d g:i:s A' ), $draft ) . "'>"
. get_the_time( get_option( 'date_format' ), $draft ) . '</abbr></small></h4>';
if ( 0 === $options[ 'only_title' ]
&& $the_content = preg_split( '#[\r\n\t ]#', strip_tags( $draft->post_content ), 11, PREG_SPLIT_NO_EMPTY ) )
$item .= '<p>' . join( ' ', array_slice( $the_content, 0, 10 ) ) . ( 10 < count( $the_content ) ? '…' : '' ) . '</p>';
$list[] = $item;
}
?>
<ul>
<li><?php echo join( "</li>\n<li>", $list ); ?></li>
</ul>
<p class="textright"><a href="edit.php?post_status=draft" class="button"><?php printf( __( 'View all %d', 'draft_feed' ), $count ); ?></a></p>
<?php
} else {
_e( 'There are no drafts at the moment', 'draft_feed' );
}
}
/**
* Control for settings
*
* @return String
*/
public function widget_settings() {
$options = $this->get_options();
// validate and update options
if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] )
&& isset( $_POST[ 'widget_id' ] ) && self::$widget_slug == $_POST[ 'widget_id' ]
) {
// reset
$options[ 'feed' ] = $options[ 'only_title' ] = 0;
if ( $_POST[ 'feed' ] )
$options[ 'feed' ] = (int) $_POST[ 'feed' ];
if ( $_POST[ 'only_title' ] )
$options[ 'only_title' ] = (int) $_POST[ 'only_title' ];
if ( $_POST[ 'posts_per_page' ] )
$options[ 'posts_per_page' ] = (int) $_POST[ 'posts_per_page' ];
update_option( self::$options_slug, $options );
}
?>
<p>
<label for="feed">
<input type="checkbox" id="feed" name="feed" value="1" <?php checked( 1, $options[ 'feed' ] ); ?> /> <?php _e( 'Create Draft Feed?', 'draft_feed' ); ?>
</label>
</p>
<p>
<label for="only_title">
<input type="checkbox" id="only_title" name="only_title" value="1" <?php checked( 1, $options[ 'only_title' ] ); ?> /> <?php _e( 'Show only the title inside the dashboard widget?', 'draft_feed' ); ?>
</label>
</p>
<p>
<label for="posts_per_page">
<input type="text" id="posts_per_page" name="posts_per_page" value="<?php esc_attr_e( $options[ 'posts_per_page' ] ); ?>" size="2" /> <?php _e( 'How many items show inside the dashboard widget?', 'draft_feed' ); ?>
</label>
</p>
<?php
}
/**
* Get options
*
* @return Array
*/
public function get_options() {
$defaults = array(
'feed' => 1,
'only_title' => 1,
'posts_per_page' => 5
);
$args = wp_parse_args(
get_option( self::$options_slug ),
apply_filters( 'draft_feed_options', $defaults )
);
return $args;
}
/**
* Add Dashbaord widget
*
* @return void
*/
public function add_dashboard_widget() {
wp_add_dashboard_widget(
self::$widget_slug,
__( 'Recents Drafts', 'draft_feed' ) .
' <small>' . __( 'of all authors', 'draft_feed' ) . '</small>',
array( $this, 'dashboard_recent_drafts' ), // content
array( $this, 'widget_settings' ) // control
);
}
/**
* Add feed with key
* Use as key the var $feed_strings
*
* @return void
*/
public function add_draft_feed() {
// set name for the feed
// http://examble.com/?feed=drafts
add_feed( self::$feed_slug, array( $this, 'get_feed_template' ) );
}
/**
* Loads the feed template
*
* @return void
*/
public function get_feed_template() {
load_template( ABSPATH . WPINC . '/feed-rss2.php' );
}
} // end class
} // end if class exists