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