PluginProbe
Edit Flow / 0.9.1
Edit Flow v0.9.1
0.11.1 0.11.0 0.7.2 0.7.3 0.7.4 0.7.5 0.7.6 0.8 0.8.1 0.8.2 0.9 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5 0.9.6 0.9.7 0.9.8 0.9.9 trunk 0.1.5 0.10.0 0.10.1 0.10.2 All 44 releases
edit-flow / modules / dashboard / dashboard.php

dashboard.php in Edit Flow 0.9.1, at modules/dashboard/dashboard.php

365 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class EF_Dashboard
4 * All of the code for the dashboard widgets from Edit Flow
5 *
6 * Dashboard widgets currently:
7 * - Post Status Widget - Shows numbers for all (custom|post) statuses
8 * - Posts I'm Following Widget - Show the headlines with edit links for posts I'm following
9 *
10 * @todo for 0.7
11 * - Update the Posts I'm Following widget to use new activity class
12 */
13
14 if ( !class_exists('EF_Dashboard') ) {
15
16 class EF_Dashboard extends EF_Module {
17
18 public $widgets;
19
20 /**
21 * Load the EF_Dashboard class as an Edit Flow module
22 */
23 function __construct() {
24
25 // Register the module with Edit Flow
26 $this->module_url = $this->get_module_url( __FILE__ );
27 $args = array(
28 'title' => __( 'Dashboard Widgets', 'edit-flow' ),
29 'short_description' => __( 'Track your content from the WordPress dashboard.', 'edit-flow' ),
30 'extended_description' => __( 'Enable dashboard widgets to quickly get an overview of what state your content is in.', 'edit-flow' ),
31 'module_url' => $this->module_url,
32 'img_url' => $this->module_url . 'lib/dashboard_s128.png',
33 'slug' => 'dashboard',
34 'post_type_support' => 'ef_dashboard',
35 'default_options' => array(
36 'enabled' => 'on',
37 'post_status_widget' => 'on',
38 'my_posts_widget' => 'on',
39 'notepad_widget' => 'on',
40 ),
41 'configure_page_cb' => 'print_configure_view',
42 'configure_link_text' => __( 'Widget Options', 'edit-flow' ),
43 );
44 $this->module = EditFlow()->register_module( 'dashboard', $args );
45 }
46
47 /**
48 * Initialize all of the class' functionality if its enabled
49 */
50 function init() {
51
52 $this->widgets = new stdClass;
53
54 if ( 'on' == $this->module->options->notepad_widget ) {
55 require_once dirname( __FILE__ ) . '/widgets/dashboard-notepad.php';
56 $this->widgets->notepad_widget = new EF_Dashboard_Notepad_Widget;
57 $this->widgets->notepad_widget->init();
58 }
59
60 // Add the widgets to the dashboard
61 add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widgets') );
62
63 // Register our settings
64 add_action( 'admin_init', array( $this, 'register_settings' ) );
65 }
66
67 /**
68 * Upgrade our data in case we need to
69 *
70 * @since 0.7
71 */
72 function upgrade( $previous_version ) {
73 global $edit_flow;
74
75 // Upgrade path to v0.7
76 if ( version_compare( $previous_version, '0.7' , '<' ) ) {
77 // Migrate whether dashboard widgets were enabled or not
78 if ( $enabled = get_option( 'edit_flow_dashboard_widgets_enabled' ) )
79 $enabled = 'on';
80 else
81 $enabled = 'off';
82 $edit_flow->update_module_option( $this->module->name, 'enabled', $enabled );
83 delete_option( 'edit_flow_dashboard_widgets_enabled' );
84 // Migrate whether the post status widget was on
85 if ( $post_status_widget = get_option( 'edit_flow_post_status_widget_enabled' ) )
86 $post_status_widget = 'on';
87 else
88 $post_status_widget = 'off';
89 $edit_flow->update_module_option( $this->module->name, 'post_status_widget', $post_status_widget );
90 delete_option( 'edit_flow_post_status_widget_enabled' );
91 // Migrate whether the my posts widget was on
92 if ( $my_posts_widget = get_option( 'edit_flow_myposts_widget_enabled' ) )
93 $my_posts_widget = 'on';
94 else
95 $my_posts_widget = 'off';
96 $edit_flow->update_module_option( $this->module->name, 'post_status_widget', $my_posts_widget );
97 delete_option( 'edit_flow_myposts_widget_enabled' );
98 // Delete legacy option
99 delete_option( 'edit_flow_quickpitch_widget_enabled' );
100
101 // Technically we've run this code before so we don't want to auto-install new data
102 $edit_flow->update_module_option( $this->module->name, 'loaded_once', true );
103 }
104
105 }
106
107 /**
108 * Add Edit Flow dashboard widgets to the WordPress admin dashboard
109 */
110 function add_dashboard_widgets() {
111
112 // Only show dashboard widgets for Contributor or higher
113 if ( !current_user_can('edit_posts') )
114 return;
115
116 wp_enqueue_style( 'edit-flow-dashboard-css', $this->module_url . 'lib/dashboard.css', false, EDIT_FLOW_VERSION, 'all' );
117
118 // Set up Post Status widget but, first, check to see if it's enabled
119 if ( $this->module->options->post_status_widget == 'on' ) {
120
121 $status_widget_post_types = apply_filters( 'ef_dashboard_psw_post_types', $this->get_all_post_types() );
122
123 foreach ( $status_widget_post_types as $post_type => $label ) {
124 $this->add_dashboard_status_widget( $post_type );
125 }
126 }
127
128 // Set up the Notepad widget if it's enabled
129 if ( 'on' == $this->module->options->notepad_widget )
130 wp_add_dashboard_widget( 'notepad_widget', __( 'Notepad', 'edit-flow' ), array( $this->widgets->notepad_widget, 'notepad_widget' ) );
131
132 // Add the MyPosts widget, if enabled
133 if ( $this->module->options->my_posts_widget == 'on' && $this->module_enabled( 'notifications' ) )
134 wp_add_dashboard_widget( 'myposts_widget', __( 'Posts I\'m Following', 'edit-flow' ), array( $this, 'myposts_widget' ) );
135
136 }
137
138 /**
139 * Dynamically add a "Unpublished Content" post status widget for any post type
140 *
141 * @param $post_type_slug
142 */
143 public function add_dashboard_status_widget( $post_type_slug ) {
144
145 $post_type_labels = get_post_type_labels( get_post_type_object( $post_type_slug ) );
146
147 if ( $post_type_slug !== 'post' ) {
148 $widget_id = 'post_status_widget_' . $post_type_slug;
149 $widget_title = sprintf( esc_html__( 'Unpublished Content: %s', 'edit-flow' ), $post_type_labels->name );
150 } else {
151 $widget_id = 'post_status_widget';
152 $widget_title = __( 'Unpublished Content', 'edit-flow' );
153 }
154
155 $widget_title = apply_filters( 'ef_dashboard_psw_title', $widget_title, $post_type_slug );
156 $args = array(
157 'post_type' => $post_type_slug,
158 'labels' => $post_type_labels,
159 );
160
161 wp_add_dashboard_widget( $widget_id, $widget_title, array( $this, 'post_status_widget' ), NULL, $args );
162 }
163
164 /**
165 * Creates Post Status widget
166 * Display an at-a-glance view of post counts for all (post|custom) statuses in the system
167 */
168 function post_status_widget( $unused, $args ) {
169
170 $labels = $args['args']['labels'];
171 $post_type = $args['args']['post_type'];
172
173 $statuses = $this->get_post_statuses();
174 $statuses[] = (object)array(
175 'name' => __( 'Scheduled', 'edit-flow' ),
176 'description' => '',
177 'slug' => 'future',
178 );
179 $statuses = apply_filters( 'ef_dashboard_post_status_widget_statuses', $statuses );
180 // If custom statuses are enabled, we'll output a link to edit the terms just below the post counts
181 if ( $this->module_enabled( 'custom_status' ) )
182 $edit_custom_status_url = add_query_arg( 'page', 'ef-custom-status-settings', get_admin_url( null, 'admin.php' ) );
183
184 ?>
185 <p class="sub ef-psw-title"><?php printf( esc_html__('%s at a Glance', 'edit-flow'), $labels->name ) ?></p>
186
187 <div class="table ef-psw-content">
188 <table>
189 <tbody>
190 <?php $post_count = wp_count_posts( $post_type ); ?>
191 <?php foreach($statuses as $status) : ?>
192 <?php $filter_link = $this->filter_posts_link( $status->slug, $post_type ); ?>
193 <tr>
194 <td class="b">
195 <a href="<?php echo esc_url( $filter_link ); ?>">
196 <?php
197 $slug = $status->slug;
198 echo esc_html( $post_count->$slug ); ?>
199 </a>
200 </td>
201 <td>
202 <a href="<?php echo esc_url( $filter_link ); ?>"><?php echo esc_html( $status->name ); ?></a>
203 </td>
204 </tr>
205
206 <?php endforeach; ?>
207 </tbody>
208 </table>
209 <?php if ( isset( $edit_custom_status_url ) ) : ?>
210 <span class="small"><a href="<?php echo esc_url( $edit_custom_status_url ); ?>"><?php _e( 'Edit Custom Statuses', 'edit-flow' ); ?></a></span>
211 <?php endif; ?>
212 </div>
213 <?php
214 }
215
216 /**
217 * Creates My Posts widget
218 * Shows a list of the "posts you're following" sorted by most recent activity.
219 */
220 function myposts_widget() {
221 global $edit_flow;
222
223 $myposts = $edit_flow->notifications->get_user_following_posts();
224
225 ?>
226 <div class="ef-myposts">
227 <?php if( !empty($myposts) ) : ?>
228
229 <?php foreach( $myposts as $post ) : ?>
230 <?php
231 $url = esc_url(get_edit_post_link( $post->ID ));
232 $title = esc_html($post->post_title);
233 ?>
234 <li>
235 <h4><a href="<?php echo $url ?>" title="<?php _e('Edit this post', 'edit-flow') ?>"><?php echo $title; ?></a></h4>
236 <span class="ef-myposts-timestamp"><?php _e('This post was last updated on', 'edit-flow') ?> <?php echo get_the_time('F j, Y \\a\\t g:i a', $post) ?></span>
237 </li>
238 <?php endforeach; ?>
239 <?php else : ?>
240 <p><?php _e('Sorry! You\'re not subscribed to any posts!', 'edit-flow') ?></p>
241 <?php endif; ?>
242 </div>
243 <?php
244 }
245
246 /**
247 * Register settings for notifications so we can partially use the Settings API
248 * (We use the Settings API for form generation, but not saving)
249 *
250 * @since 0.7
251 */
252 function register_settings() {
253
254 add_settings_section( $this->module->options_group_name . '_general', false, '__return_false', $this->module->options_group_name );
255 add_settings_field( 'post_status_widget', __( 'Post Status Widget', 'edit-flow' ), array( $this, 'settings_post_status_widget_option' ), $this->module->options_group_name, $this->module->options_group_name . '_general' );
256 add_settings_field( 'my_posts_widget',__( 'Posts I\'m Following', 'edit-flow' ), array( $this, 'settings_my_posts_widget_option' ), $this->module->options_group_name, $this->module->options_group_name . '_general' );
257 add_settings_field( 'notepad_widget',__( 'Notepad', 'edit-flow' ), array( $this, 'settings_notepad_widget_option' ), $this->module->options_group_name, $this->module->options_group_name . '_general' );
258
259 }
260
261 /**
262 * Enable or disable the Post Status Widget for the WP dashboard
263 *
264 * @since 0.7
265 */
266 function settings_post_status_widget_option() {
267 $options = array(
268 'off' => __( 'Disabled', 'edit-flow' ),
269 'on' => __( 'Enabled', 'edit-flow' ),
270 );
271 echo '<select id="post_status_widget" name="' . $this->module->options_group_name . '[post_status_widget]">';
272 foreach ( $options as $value => $label ) {
273 echo '<option value="' . esc_attr( $value ) . '"';
274 echo selected( $this->module->options->post_status_widget, $value );
275 echo '>' . esc_html( $label ) . '</option>';
276 }
277 echo '</select>';
278 }
279
280 /**
281 * Enable or disable the Posts I'm Following Widget for the WP dashboard
282 *
283 * @since 0.7
284 */
285 function settings_my_posts_widget_option() {
286 global $edit_flow;
287 $options = array(
288 'off' => __( 'Disabled', 'edit-flow' ),
289 'on' => __( 'Enabled', 'edit-flow' ),
290 );
291 echo '<select id="my_posts_widget" name="' . $this->module->options_group_name . '[my_posts_widget]"';
292 // Notifications module has to be enabled for the My Posts widget to work
293 if ( !$this->module_enabled('notifications') ) {
294 echo ' disabled="disabled"';
295 $this->module->options->my_posts_widget = 'off';
296 }
297 echo '>';
298 foreach ( $options as $value => $label ) {
299 echo '<option value="' . esc_attr( $value ) . '"';
300 echo selected( $this->module->options->my_posts_widget, $value );
301 echo '>' . esc_html( $label ) . '</option>';
302 }
303 echo '</select>';
304 if ( !$this->module_enabled('notifications') ) {
305 echo '&nbsp;&nbsp;&nbsp;<span class="description">' . __( 'The notifications module will need to be enabled for this widget to display.', 'edit-flow' );
306 }
307 }
308
309 /**
310 * Enable or disable the Notepad widget for the dashboard
311 *
312 * @since 0.8
313 */
314 function settings_notepad_widget_option() {
315 $options = array(
316 'off' => __( 'Disabled', 'edit-flow' ),
317 'on' => __( 'Enabled', 'edit-flow' ),
318 );
319 echo '<select id="notepad_widget" name="' . $this->module->options_group_name . '[notepad_widget]">';
320 foreach ( $options as $value => $label ) {
321 echo '<option value="' . esc_attr( $value ) . '"';
322 echo selected( $this->module->options->notepad_widget, $value );
323 echo '>' . esc_html( $label ) . '</option>';
324 }
325 echo '</select>';
326 }
327
328 /**
329 * Validate the field submitted by the user
330 *
331 * @since 0.7
332 */
333 function settings_validate( $new_options ) {
334
335 // Follow whitelist validation for modules
336 if ( array_key_exists( 'post_status_widget', $new_options ) && $new_options['post_status_widget'] != 'on' )
337 $new_options['post_status_widget'] = 'off';
338
339 if ( array_key_exists( 'my_posts_widget', $new_options ) && $new_options['my_posts_widget'] != 'on' )
340 $new_options['my_posts_widget'] = 'off';
341
342 return $new_options;
343 }
344
345 /**
346 * Settings page for the dashboard
347 *
348 * @since 0.7
349 */
350 function print_configure_view() {
351 ?>
352 <form class="basic-settings" action="<?php echo esc_url( menu_page_url( $this->module->settings_slug, false ) ); ?>" method="post">
353 <?php settings_fields( $this->module->options_group_name ); ?>
354 <?php do_settings_sections( $this->module->options_group_name ); ?>
355 <?php
356 echo '<input id="edit_flow_module_name" name="edit_flow_module_name" type="hidden" value="' . esc_attr( $this->module->name ) . '" />';
357 ?>
358 <p class="submit"><?php submit_button( null, 'primary', 'submit', false ); ?><a class="cancel-settings-link" href="<?php echo EDIT_FLOW_SETTINGS_PAGE; ?>"><?php _e( 'Back to Edit Flow', 'edit-flow' ); ?></a></p>
359 </form>
360 <?php
361 }
362 }
363
364 } // END - !class_exists('EF_Dashboard')
365