Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a note type for logging post events, that displays inline with user-input internal notes #9

Merged
merged 19 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions env/0-sandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
add_filter( 'wp_is_application_passwords_available', '__return_true' );

add_post_type_support( 'post', 'wporg-internal-notes' );
add_post_type_support( 'post', 'wporg-log-notes' );
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ To enable internal notes for a particular post type, add this line to an mu-plug

`add_post_type_support( '[post type slug here]', 'wporg-internal-notes' );`

To enable log entries for changes to posts, which will appear in the same stream with internal notes, add this line:

`add_post_type_support( '[post type slug here]', 'wporg-log-notes' );`

You can also specify only certain change events to log. See `logging_enabled()` for a list of available events.

`add_post_type_support( '[post type slug here]', 'wporg-log-notes', array( 'status-change' => true ) );`

## Development

### Prerequisites
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/
function map_meta_caps( $required_caps, $current_cap, $user_id, $args ) {
switch ( $current_cap ) {
case 'read-internal-notes':
case 'create-internal-note':
case 'read-notes':
case 'create-note':
$required_caps = array();

$parent = get_post( $args[0] );
Expand All @@ -44,7 +44,7 @@ function map_meta_caps( $required_caps, $current_cap, $user_id, $args ) {

$required_caps[] = $post_type->cap->edit_others_posts;
break;
case 'delete-internal-note':
case 'delete-note':
$required_caps = array();

$note = get_post( $args[0] );
Expand All @@ -53,6 +53,13 @@ function map_meta_caps( $required_caps, $current_cap, $user_id, $args ) {
break;
}

$note_type = get_post_type( $note );
if ( NOTE_POST_TYPE !== $note_type ) {
// Log notes should not be deleted.
$required_caps[] = 'do_not_allow';
break;
}

$parent = get_post( $note->post_parent );
if ( ! $parent ) {
$required_caps[] = 'do_not_allow';
Expand All @@ -66,7 +73,6 @@ function map_meta_caps( $required_caps, $current_cap, $user_id, $args ) {
}

// TODO maybe only allow deleting your own notes?

$required_caps[] = $post_type->cap->edit_others_posts;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,19 @@ public function permissions_check( $request ) {

switch ( $request->get_method() ) {
case \WP_REST_Server::READABLE:
$cap = 'read-internal-notes';
$cap = 'read-notes';
$arg = $parent->ID;
$msg = __( 'Sorry, you are not allowed to access internal notes on this post.', 'wporg' );
break;
case \WP_REST_Server::CREATABLE:
$cap = 'create-internal-note';
$cap = 'create-note';
$arg = $parent->ID;
$msg = __( 'Sorry, you are not allowed to create internal notes on this post.', 'wporg' );
break;
case \WP_REST_Server::DELETABLE:
$cap = 'delete-internal-note';
$cap = 'delete-note';
$arg = $request['id'];
$msg = __( 'Sorry, you are not allowed to delete this note.', 'wporg' );
break;
default:
$cap = 'do_not_allow';
Expand All @@ -144,7 +147,7 @@ public function permissions_check( $request ) {
if ( ! current_user_can( $cap, $arg ) ) {
return new \WP_Error(
'rest_cannot_access',
__( 'Sorry, you are not allowed to access internal notes on this post.', 'wporg' ),
$msg,
array( 'status' => rest_authorization_required_code() )
);
}
Expand All @@ -168,17 +171,26 @@ public function get_items( $request ) {
$args = array();

$collection_params = array(
'offset' => 'offset',
'order' => 'order',
'page' => 'paged',
'per_page' => 'posts_per_page',
'offset' => 'offset',
);
foreach ( $collection_params as $request_param => $query_param ) {
if ( isset( $request[ $request_param ] ) ) {
$args[ $query_param ] = $request[ $request_param ];
}
}

if ( isset( $request['after'] ) ) {
$args['date_query'] = array(
array(
'after' => $request['after'],
'column' => 'post_date',
),
);
}

$notes_query = get_notes( $parent->ID, $args, true );
$notes = $notes_query->get_posts();

Expand Down Expand Up @@ -328,15 +340,16 @@ public function prepare_item_for_response( $note, $request ) {
human_time_diff( strtotime( $note->post_date_gmt ), time() )
);

$data['id'] = (int) $note->ID;
$data['parent'] = (int) $note->post_parent;
$data['author'] = (int) $note->post_author;
$data['type'] = $note->post_type;

$data['excerpt'] = array(
'raw' => $note->post_excerpt,
'rendered' => wpautop( $note->post_excerpt ),
);

$data['id'] = (int) $note->ID;
$data['author'] = (int) $note->post_author;
$data['parent'] = (int) $note->post_parent;

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->filter_response_by_context( $data, $context );
$response = rest_ensure_response( $data );
Expand Down Expand Up @@ -430,6 +443,13 @@ public function get_item_schema() {
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'type' => array(
'description' => __( 'Type of note.', 'wporg' ),
'type' => 'string',
'enum' => get_note_post_types(),
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'excerpt' => array(
'description' => __( 'The excerpt for the post.', 'wporg' ),
'type' => 'object',
Expand Down Expand Up @@ -474,6 +494,12 @@ public function get_collection_params() {
'type' => 'integer',
);

$params['after'] = array(
'description' => __( 'Limit response to posts published after a given ISO8601 compliant date.', 'wporg' ),
'type' => 'string',
'format' => 'date-time',
);

$params['order'] = array(
'description' => __( 'Order sort attribute ascending or descending.', 'wporg' ),
'type' => 'string',
Expand Down Expand Up @@ -530,7 +556,7 @@ protected function get_note( $note_id ) {
}

$post = get_post( (int) $note_id );
if ( empty( $post ) || empty( $post->ID ) || POST_TYPE !== $post->post_type ) {
if ( empty( $post ) || empty( $post->ID ) || ! in_array( $post->post_type, get_note_post_types(), true ) ) {
return $error;
}

Expand Down
Loading