Permitiendo al usuario editar solo ciertas páginas

13

Me gustaría permitir que cierto usuario edite solo una página y sus subpáginas. ¿Cómo sería esto posible? Probé el antiguo Role Scoper, pero parece tener muchos problemas y errores.

    
pregunta naf 16.06.2015 - 16:15

4 respuestas

11

Lo primero que debe hacer para implementar dicha tarea es poder reconocer qué página puede editar un usuario.

Hay diferentes maneras de hacerlo. Podría ser un meta del usuario, algún valor de configuración ... Por el bien de esta respuesta, asumiré que existe una función así:

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...

}

Ahora que tenemos una manera de determinar si un usuario puede editar una página, solo necesitamos decirle a WordPress que use esta función para verificar la capacidad del usuario para editar una página.

Se puede hacer a través del filtro 'map_meta_cap' .

Algo como:

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 );

En este punto, solo necesitamos una manera de conectar un usuario a una o más páginas.

Puede haber diferentes soluciones según el caso de uso.

Una solución flexible podría ser agregar un menú desplegable de páginas "raíz" (consulte wp_dropdown_pages ) a la pantalla de edición del administrador del usuario y guarde las páginas seleccionadas como meta del usuario.

Podríamos aprovechar 'edit_user_profile' para agregar el campo desplegable de páginas y 'edit_user_profile_update' para almacenar el valor seleccionado como meta del usuario.

Estoy seguro de que en este sitio web hay suficiente orientación sobre cómo hacerlo en detalle.

Cuando las páginas se almacenan como meta del usuario, la función wpse_user_can_edit() de arriba se puede finalizar al verificar si la identificación de la página es parte del valor meta del usuario.

Al eliminar la capacidad de editar la página, WordPress se encargará del resto: eliminará cualquier enlace de edición del backend y del frontend, impedirá el acceso directo ... y así sucesivamente.

    
respondido por el gmazzap 14.03.2017 - 11:07
7

Se necesita una pequeña cantidad de código para implementar esta función, incluso si utiliza una clase de PHP para evitar las variables globales. Tampoco quise ocultar las páginas prohibidas para el usuario en el Panel. ¿Qué sucede si agregaron contenido que ya estaba en el sitio?

$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;
    }
}

Lo que hace el código anterior es evitar que lo siguiente funcione o aparezca como es necesario:

  1. get_edit_post_link
  2. Enlace Edit Page en la barra de administración de WP que aparece para las páginas
  3. Edit , Quick Edit y Trash enlaces rápidos que aparecen debajo de las Páginas en /wp-admin/edit.php?post_type=page

Esto funcionó en mi instalación local de WordPress 4.7. Suponiendo que las páginas en el sitio no cambien con frecuencia, podría ser mejor codificar las ID de la página y sus subpáginas y eliminar el WP_Query dentro del método __construct . Esto ahorrará mucho en las llamadas a la base de datos.

    
respondido por el ricotheque 14.03.2017 - 10:26
5

Si desea mantenerse alejado de los complementos, puede realizar una variación del código a continuación en un archivo functions.php o un complemento personalizado.

Hay 2 partes separadas en este código, solo necesitarías usar una de ellas, pero cuál depende de la complejidad de los requisitos.

La Parte 1 está especificando un solo usuario y restringiéndolos a una publicación específica.

La Parte 2 le permite crear un mapa de usuarios y publicar ID y permite múltiples publicaciones

El código a continuación es solo para una página, pero si desea cambiarlo a una publicación, o un tipo de publicación personalizada, deberá cambiar la cadena en $screen->id == 'page' a otra cosa.

Puede encontrar una referencia a las ID de pantalla alrededor de wp-admin aquí

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' );
    
respondido por el Ben Casey 14.03.2017 - 10:26
-4

He usado User Role Editor un par de veces y es bastante bueno. Tal vez te pueda ayudar también. Aquí está el enlace Editor de roles de usuario

    
respondido por el user2319361 16.06.2015 - 16:31

Lea otras preguntas en las etiquetas