| 1 |
<?php |
| 2 |
/** |
| 3 |
* A notepad for the dashboard |
| 4 |
*/ |
| 5 |
|
| 6 |
class EF_Dashboard_Notepad_Widget { |
| 7 |
|
| 8 |
const notepad_post_type = 'dashboard-note'; |
| 9 |
|
| 10 |
public $edit_cap = 'edit_others_posts'; |
| 11 |
|
| 12 |
function __construct() { |
| 13 |
// Silence is golden |
| 14 |
} |
| 15 |
|
| 16 |
public function init() { |
| 17 |
|
| 18 |
register_post_type( self::notepad_post_type ); |
| 19 |
|
| 20 |
$this->edit_cap = apply_filters( 'ef_dashboard_notepad_edit_cap', $this->edit_cap ); |
| 21 |
|
| 22 |
add_action( 'admin_init', array( $this, 'handle_notepad_update' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Handle a dashboard note being created or updated |
| 27 |
* |
| 28 |
* @since 0.8 |
| 29 |
*/ |
| 30 |
public function handle_notepad_update() { |
| 31 |
global $pagenow; |
| 32 |
|
| 33 |
if ( 'index.php' != $pagenow |
| 34 |
|| ( empty( $_REQUEST['action'] ) || 'dashboard-notepad' != $_REQUEST['action'] ) ) |
| 35 |
return; |
| 36 |
|
| 37 |
check_admin_referer( 'dashboard-notepad' ); |
| 38 |
|
| 39 |
if ( ! current_user_can( $this->edit_cap ) ) |
| 40 |
wp_die( EditFlow()->dashboard->messages['invalid-permissions'] ); |
| 41 |
|
| 42 |
$current_id = (int)$_REQUEST['notepad-id']; |
| 43 |
$current_notepad = get_post( $current_id ); |
| 44 |
$new_note = array( |
| 45 |
'post_content' => wp_filter_nohtml_kses( $_REQUEST['note'] ), |
| 46 |
'post_type' => self::notepad_post_type, |
| 47 |
'post_status' => 'draft', |
| 48 |
'post_author' => get_current_user_id(), |
| 49 |
); |
| 50 |
if ( $current_notepad |
| 51 |
&& self::notepad_post_type == $current_notepad->post_type |
| 52 |
&& ! isset ( $_REQUEST['create-note'] ) ) { |
| 53 |
$new_note['ID'] = $current_id; |
| 54 |
wp_update_post( $new_note ); |
| 55 |
} else { |
| 56 |
wp_insert_post( $new_note ); |
| 57 |
} |
| 58 |
|
| 59 |
wp_safe_redirect( wp_get_referer() ); |
| 60 |
exit; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Notepad Widget |
| 65 |
* Editors can leave notes in the dashboard for authors and contributors |
| 66 |
* |
| 67 |
* @since 0.8 |
| 68 |
*/ |
| 69 |
public function notepad_widget() { |
| 70 |
|
| 71 |
$args = array( |
| 72 |
'posts_per_page' => 1, |
| 73 |
'post_status' => 'draft', |
| 74 |
'post_type' => self::notepad_post_type, |
| 75 |
); |
| 76 |
$posts = get_posts( $args ); |
| 77 |
$current_note = ( ! empty( $posts[0]->post_content ) ) ? $posts[0]->post_content : ''; |
| 78 |
$current_id = ( ! empty( $posts[0]->ID ) ) ? $posts[0]->ID : 0; |
| 79 |
$current_post = ( ! empty( $posts[0] ) ) ? $posts[0] : false; |
| 80 |
|
| 81 |
if ( $current_post ) |
| 82 |
$last_updated = '<span id="dashboard-notepad-last-updated">' . sprintf( __( '%1$s last updated on %2$s', 'edit-flow' ), get_user_by( 'id', $current_post->post_author )->display_name, get_the_time( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $current_post ) ) . '</span>'; |
| 83 |
else |
| 84 |
$last_updated = ''; |
| 85 |
|
| 86 |
if ( current_user_can( $this->edit_cap ) ) { |
| 87 |
echo '<form id="dashboard-notepad">'; |
| 88 |
echo '<input type="hidden" name="action" value="dashboard-notepad" />'; |
| 89 |
echo '<input type="hidden" name="notepad-id" value="' . esc_attr( $current_id ) . '" />'; |
| 90 |
echo '<textarea style="width:100%" rows="10" name="note">'; |
| 91 |
echo esc_textarea( trim( $current_note ) ); |
| 92 |
echo '</textarea>'; |
| 93 |
echo '<p class="submit">'; |
| 94 |
echo $last_updated; |
| 95 |
echo '<span id="dashboard-notepad-submit-buttons">'; |
| 96 |
submit_button( __( 'Update Note', 'edit-flow' ), 'primary', 'update-note', false ); |
| 97 |
echo '</span>'; |
| 98 |
echo '<div style="clear:both;"></div>'; |
| 99 |
wp_nonce_field( 'dashboard-notepad' ); |
| 100 |
echo '</form>'; |
| 101 |
} else { |
| 102 |
echo '<form id="dashboard-notepad">'; |
| 103 |
echo '<textarea style="width:100%" rows="10" name="note" disabled="disabled">'; |
| 104 |
echo esc_textarea( trim( $current_note ) ); |
| 105 |
echo '</textarea>'; |
| 106 |
echo $last_updated; |
| 107 |
echo '</form>'; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
} |