From 5c508757448bbfd465a32232cfdb721e1f675299 Mon Sep 17 00:00:00 2001 From: iniznet Date: Wed, 25 Sep 2024 22:00:32 +0700 Subject: [PATCH] Refactor fictioneer_verify_unpublish_access to allow customization --- FILTERS.md | 10 ++++++++++ includes/functions/_utility.php | 21 +++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/FILTERS.md b/FILTERS.md index 02db79c5..5c4324b0 100644 --- a/FILTERS.md +++ b/FILTERS.md @@ -1605,6 +1605,16 @@ Filters the arguments for the story chapter posts query, an utility function cal --- +### `apply_filters( 'fictioneer_filter_verify_unpublish_access', $authorized, $post_id, $post )` +Filters the boolean return value of the `fictioneer_verify_unpublish_access( $post )` function. By default, this verifies current user has access to unpublished posts (not drafts). + +**Parameters:** +* $authorized (boolean) – Whether the current user can unpublish the post. +* $post_id (int) – The post ID. +* $post (WP_Post) – The post object. + +--- + ### `apply_filters( 'fictioneer_filter_story_footer_meta', $meta_output, $args, $post )` Filters the intermediate output array of story meta data in the `_story-footer.php` partial before it is imploded and rendered. Contains the status, publish date, word count, age rating, and checkmark (if enabled). diff --git a/includes/functions/_utility.php b/includes/functions/_utility.php index e28e1c9f..520767f8 100644 --- a/includes/functions/_utility.php +++ b/includes/functions/_utility.php @@ -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 ); } }