問題描述

我想允許某些使用者只編輯一個頁面,它是子頁面。這怎麼可能?我嘗試過舊的角色詭計,但似乎有很多問題和錯誤。

最佳解決方案

實現此類任務的第一件事是能夠識別使用者可以編輯的頁面。

有不同的方式來做。它可以是一個使用者元,一些配置值… 為了這個答案,我將假設一個函式 lile 存在:

function wpse_user_can_edit( $user_id, $page_id ) {

   $page = get_post( $page_id );

   // let's find the topmost page in the hierarchy
   while( $page && (int) $page->parent ) {
     $page = get_post( $page->parent );
   }

   if ( ! $page ) {
     return false;
   }

   // now $page is the top page in the hierarchy
   // how to know if an user can edit it, it's up to you...

}

現在我們有一種方法來確定使用者是否可以編輯一個頁面,我們只需要告訴 WordPress 使用此功能來檢查使用者編輯頁面的能力。

這可以透過'map_meta_cap'過濾器完成。

就像是:

add_filter( 'map_meta_cap', function ( $caps, $cap, $user_id, $args ) {

    $to_filter = [ 'edit_post', 'delete_post', 'edit_page', 'delete_page' ];

    // If the capability being filtered isn't of our interest, just return current value
    if ( ! in_array( $cap, $to_filter, true ) ) {
        return $caps;
    }

    // First item in $args array should be page ID
    if ( ! $args || empty( $args[0] ) || ! wpse_user_can_edit( $user_id, $args[0] ) ) {
        // User is not allowed, let's tell that to WP
        return [ 'do_not_allow' ];
    }

    // Every user is allowed to exist.
    // Return this array, the check for capability will be true
    return [ 'exist' ];

}, 10, 4 );

在這一點上,我們只需要一種將使用者連線到一個或多個頁面的方法。

根據用例可能有不同的解決方案。

靈活的解決方案可能是將”root” 頁面 (參見 wp_dropdown_pages) 的下拉選單新增到編輯使用者管理螢幕,並將所選頁面儲存為使用者後設資料。

我們可以利用'edit_user_profile'新增頁面下拉欄位和'edit_user_profile_update'來儲存所選值作為使用者元。

我相信在這個網站上有足夠的指導如何詳細。

當頁面儲存為使用者元時,可以透過檢查頁面 ID 是否是使用者元值的一部分來完成上述的 wpse_user_can_edit()函式。

刪除編輯頁面的功能,WordPress 將執行其餘操作:將從後端和前端刪除任何編輯連結,將阻止直接訪問… 等等。

次佳解決方案

即使您使用 PHP 類來避免全域性變數,它也需要少量程式碼實現此功能。我也不想在儀錶板中隱藏使用者的禁止頁面。如果他們新增了已經在網站上的內容怎麼辦?

$user_edit_limit = new NS_User_Edit_Limit(
    15,       // User ID we want to limit
    [2, 17]   // Array of parent page IDs user is allowed to edit
                 (also accepts sub-page IDs)
);

class NS_User_Edit_Limit {

    /**
     * Store the ID of the user we want to control, and the
     * posts we will let the user edit.
     */
    private $user_id = 0;
    private $allowed = array();

    public function __construct( $user_id, $allowed ) {

        // Save the ID of the user we want to limit.
        $this->user_id = $user_id;

        // Expand the list of allowed pages to include sub pages
        $all_pages = new WP_Query( array(
            'post_type' => 'page',
            'posts_per_page' => -1,
        ) );            
        foreach ( $allowed as $page ) {
            $this->allowed[] = $page;
            $sub_pages = get_page_children( $page, $all_pages );
            foreach ( $sub_pages as $sub_page ) {
                $this->allowed[] = $sub_page->ID;
            }
        }

        // For the prohibited user...
        // Remove the edit link from the front-end as needed
        add_filter( 'get_edit_post_link', array( $this, 'remove_edit_link' ), 10, 3 );
        add_action( 'admin_bar_menu', array( $this, 'remove_wp_admin_edit_link' ), 10, 1 );
        // Remove the edit link from wp-admin as needed
        add_action( 'page_row_actions', array( $this, 'remove_page_list_edit_link' ), 10, 2 );
    }

    /**
     * Helper functions that check if the current user is the one
     * we want to limit, and check if a specific post is in our
     * list of posts that we allow the user to edit.
     */
    private function is_user_limited() {
        $current_user = wp_get_current_user();
        return ( $current_user->ID == $this->user_id );
    }
    private function is_page_allowed( $post_id ) {
        return in_array( $post_id, $this->allowed );
    }

    /**
     * Removes the edit link from the front-end as needed.
     */
    public function remove_edit_link( $link, $post_id, $test ) {
        /**
         * If...
         * - The limited user is logged in
         * - The page the edit link is being created for is not in the allowed list
         * ...return an empty $link. This also causes edit_post_link() to show nothing.
         *
         * Otherwise, return link as normal.
         */
        if ( $this->is_user_limited() && !$this->is_page_allowed( $post_id ) ) {
            return '';
        }
        return $link;
    }

    /**
     * Removes the edit link from WP Admin Bar
     */
    public function remove_wp_admin_edit_link( $wp_admin_bar ) {
        /**
         *  If:
         *  - We're on a single page
         *  - The limited user is logged in
         *  - The page is not in the allowed list
         *  ...Remove the edit link from the WP Admin Bar
         */
        if ( 
            is_page() &&
            $this->is_user_limited() &&
            !$this->is_page_allowed( get_post()->ID )
        ) {
            $wp_admin_bar->remove_node( 'edit' );
        }
    }

    /**
     * Removes the edit link from WP Admin's edit.php
     */
    public function remove_page_list_edit_link( $actions, $post ) {
        /**
         * If:
         * -The limited user is logged in
         * -The page is not in the allowed list
         * ...Remove the "Edit", "Quick Edit", and "Trash" quick links.
         */
        if ( 
            $this->is_user_limited() &&
            !$this->is_page_allowed( $post->ID )
        ) {
            unset( $actions['edit'] );
            unset( $actions['inline hide-if-no-js']);
            unset( $actions['trash'] );
        }
        return $actions;
    }
}

上面的程式碼是防止以下內容根據需要工作或出現:

  1. get_edit_post_link

  2. 在頁面上顯示的 WP 管理欄上的 Edit Page 連結

  3. EditQuick EditTrash 快速連結,出現在/wp-admin/edit.php?post_type=page 頁面下方

這適用於我本地的 WordPress 4.7 安裝。假設站點上的頁面不會頻繁更改,可能會更好地編碼頁面及其 sub-pages 的 ID,並刪除__construct 方法中的 WP_Query 。這將節省很多資料庫呼叫。

第三種解決方案

如果你想遠離外掛,你可以在一個 functions.php 檔案或一個自定義外掛的下面的程式碼的變體。

該程式碼有兩個獨立的部分,您只需要使用其中的 1 個,但哪一個取決於要求的複雜性。

第 1 部分是指定一個使用者並將其限制在特定的帖子中。

第 2 部分允許您建立使用者的 Map 和帖子 ID,並允許多個帖子

下面的程式碼僅用於頁面,但是如果要將其更改為帖子或自定義帖子型別,則需要將 $screen->id == 'page'中的字串更改為其他內容。

您可以找到 wp-admin here 附近螢幕 ID 的參考

function my_pre_get_posts( $query ){

    $screen = get_current_screen();
    $current_user = wp_get_current_user();

    /**
     * Specify a single user and restrict to a single page
     */
    $restricted_user_id = 10; //User ID of the restricted user
    $allowed_post_id = 1234; //Post ID of the allowed post

    $current_post_id = isset( $_GET['post'] ) ? (int)$_GET['post'] : false ;

    //Only affecting a specific user
    if( $current_user->ID !== $restricted_user_id ){
        return;
    }

    //Only Affecting EDIT page.
    if( ! $current_post_id ){
        return;
    }

    if( $screen->id == 'page' && $current_post_id !== $allowed_post_id ){
        wp_redirect( admin_url( ) );
        exit;
    }

    /**
     * Specify a map of user_id => $allowed_posts
     */
    $restrictions_map = [
        10 => [ 123 ], //Allow user ID to edit Page ID 123
        11 => [ 152, 186 ] //Allow user ID to edit Page ID 123 and 186
    ];

    if( array_key_exists( $current_user->ID, $restrictions_map ) ){

        $allowed_posts = $restrictions_map[$current_user->ID];

        if( $screen->id == 'page' && ! in_array( $current_user->ID, $allowed_posts ) ){
            wp_redirect( admin_url( ) );
            exit;
        }

    }

}
add_action( 'pre_get_posts', 'my_pre_get_posts' );

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。