PluginProbe
Edit Flow / trunk
Edit Flow vtrunk
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 / widgets / dashboard-notepad.php

dashboard-notepad.php in Edit Flow trunk, at modules/dashboard/widgets/dashboard-notepad.php

132 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Dashboard Notepad Widget for Edit Flow.
4 *
5 * @package EditFlow
6 */
7
8 /**
9 * A notepad for the dashboard.
10 */
11 class EF_Dashboard_Notepad_Widget {
12
13 // phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase
14 const notepad_post_type = 'dashboard-note';
15
16 /**
17 * Capability required to edit the notepad.
18 *
19 * @var string
20 */
21 public $edit_cap = 'edit_others_posts';
22
23 /**
24 * Constructor.
25 */
26 public function __construct() {
27 // Silence is golden.
28 }
29
30 /**
31 * Initialize the widget.
32 *
33 * @return void
34 */
35 public function init() {
36
37 register_post_type( self::notepad_post_type, array(
38 'rewrite' => false,
39 'label' => __( 'Dashboard Note', 'edit-flow' ),
40 ));
41
42 $this->edit_cap = apply_filters( 'ef_dashboard_notepad_edit_cap', $this->edit_cap );
43
44 add_action( 'admin_init', array( $this, 'handle_notepad_update' ) );
45 }
46
47 /**
48 * Handle a dashboard note being created or updated
49 *
50 * @since 0.8
51 */
52 public function handle_notepad_update() {
53 global $pagenow;
54
55 if ( 'index.php' !== $pagenow || empty( $_POST['action'] ) || 'dashboard-notepad' !== $_POST['action'] ) {
56 return;
57 }
58
59 check_admin_referer( 'dashboard-notepad' );
60
61 if ( ! current_user_can( $this->edit_cap ) ) {
62 wp_die( esc_html( EditFlow()->dashboard->messages['invalid-permissions'] ) );
63 }
64
65 $note_data = array(
66 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- wp_filter_nohtml_kses is a valid sanitization function.
67 'post_content' => isset( $_POST['note'] ) ? wp_filter_nohtml_kses( wp_unslash( $_POST['note'] ) ) : '',
68 'post_type' => self::notepad_post_type,
69 'post_status' => 'draft',
70 'post_author' => get_current_user_id(),
71 );
72
73 $existing_notepad = isset( $_POST['notepad-id'] ) ? get_post( absint( $_POST['notepad-id'] ) ) : null;
74 if ( isset( $existing_notepad->post_type ) && self::notepad_post_type === $existing_notepad->post_type ) {
75 $note_data['ID'] = $existing_notepad->ID;
76 }
77
78 wp_insert_post( $note_data );
79 }
80
81 /**
82 * Notepad Widget
83 * Editors can leave notes in the dashboard for authors and contributors
84 *
85 * @since 0.8
86 */
87 public function notepad_widget() {
88
89 $args = array(
90 'posts_per_page' => 1,
91 'post_status' => 'draft',
92 'post_type' => self::notepad_post_type,
93 );
94
95 $posts = get_posts( $args );
96 $current_note = ( ! empty( $posts[0]->post_content ) ) ? $posts[0]->post_content : '';
97 $current_id = ( ! empty( $posts[0]->ID ) ) ? $posts[0]->ID : 0;
98 $current_post = ( ! empty( $posts[0] ) ) ? $posts[0] : false;
99
100 if ( $current_post ) {
101 // translators: %1$s is the author name, %2$s is the date the note was last updated.
102 $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_modified_time( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $current_post ) ) . '</span>';
103 } else {
104 $last_updated = '';
105 }
106
107 if ( current_user_can( $this->edit_cap ) ) {
108 echo '<form method="post" id="dashboard-notepad">';
109 echo '<input type="hidden" name="action" value="dashboard-notepad" />';
110 echo '<input type="hidden" name="notepad-id" value="' . esc_attr( $current_id ) . '" />';
111 echo '<textarea style="width:100%" rows="10" name="note">';
112 echo esc_textarea( trim( $current_note ) );
113 echo '</textarea>';
114 echo '<p class="submit">';
115 echo wp_kses_post( $last_updated );
116 echo '<span id="dashboard-notepad-submit-buttons">';
117 submit_button( __( 'Update Note', 'edit-flow' ), 'primary', 'update-note', false );
118 echo '</span>';
119 echo '<div style="clear:both;"></div>';
120 wp_nonce_field( 'dashboard-notepad' );
121 echo '</form>';
122 } else {
123 echo '<form id="dashboard-notepad">';
124 echo '<textarea style="width:100%" rows="10" name="note" disabled="disabled">';
125 echo esc_textarea( trim( $current_note ) );
126 echo '</textarea>';
127 echo wp_kses_post( $last_updated );
128 echo '</form>';
129 }
130 }
131 }
132