Allow overriding the fictioneer_verify_unpublish_access value #52

Merged
iniznet merged 3 commits from refactor/fictioneer_verify_unpublish_access into main 2024-09-26 17:57:01 +08:00
2 changed files with 21 additions and 10 deletions

View File

@ -1745,6 +1745,16 @@ Filters the check result of whether the users Patreon data is still valid. Be
---
### `apply_filters( 'fictioneer_filter_verify_unpublish_access', $authorized, $post_id, $post )`
Filters the boolean return value of the `fictioneer_verify_unpublish_access()` function. By default, this verifies whether the current user has access to unpublished posts (not drafts).
**Parameters:**
* $authorized (boolean) Whether the current user can access unpublished posts.
* $post_id (int) The post ID.
* $post (WP_Post) The post object.
---
### `apply_filters( 'fictioneer_filter_word_count', $word_count, $post_id )`
Filters the word count of a post after sanitization (greater than or equal to 0) and before `fictioneer_multiply_word_count()` is applied, returning a positive integer. The word count is only calculated and updated when a post is saved.

View File

@ -2769,45 +2769,46 @@ if ( ! function_exists( 'fictioneer_verify_unpublish_access' ) ) {
function fictioneer_verify_unpublish_access( $post_id ) {
// Setup
$post = get_post( $post_id );
$authorized = false;
// Always let owner to pass
if ( get_current_user_id() === absint( $post->post_author ) ) {
return true;
$authorized = true;
}
// Always let administrators pass
if ( current_user_can( 'manage_options' ) ) {
return true;
$authorized = true;
}
// Check capability for post type
if ( $post->post_status === 'private' ) {
switch ( $post->post_type ) {
case 'post':
return current_user_can( 'edit_private_posts' );
$authorized = current_user_can( 'edit_private_posts' );
break;
case 'page':
return current_user_can( 'edit_private_pages' );
$authorized = current_user_can( 'edit_private_pages' );
break;
case 'fcn_chapter':
return current_user_can( 'read_private_fcn_chapters' );
$authorized = current_user_can( 'read_private_fcn_chapters' );
break;
case 'fcn_story':
return current_user_can( 'read_private_fcn_stories' );
$authorized = current_user_can( 'read_private_fcn_stories' );
break;
case 'fcn_recommendation':
return current_user_can( 'read_private_fcn_recommendations' );
$authorized = current_user_can( 'read_private_fcn_recommendations' );
break;
case 'fcn_collection':
return current_user_can( 'read_private_fcn_collections' );
$authorized = current_user_can( 'read_private_fcn_collections' );
break;
default:
return current_user_can( 'edit_others_posts' );
$authorized = current_user_can( 'edit_others_posts' );
}
}
// Drafts are handled by WordPress
return false;
return apply_filters( 'fictioneer_filter_verify_unpublish_access', $authorized, $post_id, $post );
}
}