web-dev-qa-db-fra.com

Empêcher l'auteur de modifier ses publications si l'administrateur a été modifié

Dans wordpress, par défaut, les auteurs peuvent créer et apporter toutes les modifications à leurs publications.

Je cherche un moyen de les empêcher de modifier une publication si l'administrateur a modifié cette publication (après que l'administrateur a cliqué sur le bouton "mettre à jour").

Pour les publications qui n'ont pas été modifiées par l'administrateur, l'auteur peut toujours apporter les modifications souhaitées.

J'ai essayé plusieurs plugins de rôle utilisateur, mais je ne trouve aucun plugin qui puisse aider.

Merci beaucoup.

5
aye

Vous pouvez essayer ce qui suit:

/**
 * Post Update Locker For Authors
 * If an administrator has updated the post, then lock it for author updates.
 * @see http://wordpress.stackexchange.com/a/168578/26350
 */

add_action( 'pre_post_update', function( $post_ID, $data ) {

    // Target only authors:
    if( ! current_user_can( 'edit_post' ) || current_user_can( 'edit_others_posts' ) )      
        return;

    // Target only 'post' post types:
    if( get_post_type( $post_ID ) !== 'post' )
        return;

    // Fetch all administrators:
    $admins_ids = get_users( 
        array( 
            'role'   => 'administrator', 
            'fields' => 'ID' 
        ) 
    );

    // or hardcoded if needed:
    // $admins_ids = array( 1 );

    // Check if administrators have modified the current post, by checking the revisions:
    $posts = get_posts( 
        array( 
            'no_found_rows'             => true,
            'update_post_meta_cache'    => false,
            'update_post_term_cache'    => false,
            'posts_per_page'            => -1, 
            'fields'                    => 'ids', 
            'post_parent'               => $post_ID, 
            'post_type'                 => 'revision', 
            'post_status'               => 'any', 
            'author__in '               => $admin_ids,  
        ) 
    );

    // Halt if an administrator has modified the post:
    if( count( $posts ) > 0 )
        wp_die( __( "Sorry, you can't modify this post, it's already been modified by an administrator! " ) );

}, 10, 2 );

Vous devrez peut-être ajuster cela et tester plus avant.

Mettre à jour:

Voici un exemple supplémentaire sur la façon de supprimer la métaboxe submitdiv pour les auteurs qui ne peuvent plus mettre à jour la publication actuelle:

/**
 * Hide the publish metabox for authors if an administrator has updated the post.
 * @see http://wordpress.stackexchange.com/a/168578/26350
 */

add_action( 'admin_menu', function() {
    add_action( 'load-post.php', 'wpse_author_update_locking' );
});

function wpse_author_update_locking()
{
    // Setup - Modify this to your needs:
    $admin_ids = array( 1 );
    $cpt       = 'post';

    // User input:
    $_pid    = filter_input( INPUT_GET, 'post',   FILTER_SANITIZE_NUMBER_INT );

    // Target only $cpt post types:
    if( get_post_type( $_pid ) !== $cpt )
        return;

    // Target only authors:
    if( ! current_user_can( 'edit_post' ) || current_user_can( 'edit_others_posts' ) )      
        return;

    if( $_pid > 0 )
    {
        // Check if administrators have modified the current post, 
        // by checking the revisions:
        $posts = get_posts( 
            array( 
                'no_found_rows'             => true,
                'update_post_meta_cache'    => false,
                'update_post_term_cache'    => false,
                'posts_per_page'            => -1, 
                'fields'                    => 'ids', 
                'post_parent'               => $_pid, 
                'post_type'                 => 'revision', 
                'post_status'               => 'any', 
                'author__in '               => $admin_ids,  
            ) 
        );

        // Halt if an administrator has modified the post:
        if( count( $posts ) > 0 )
        {
            remove_meta_box( 'submitdiv', $cpt , 'side' ); 
            add_meta_box( 
                'submitdivmod', 
                __( 'Publish' ), 
                'wpse_post_submit_meta_box', 
                $cpt, 
                'side', 
                'core' 
            ); 
        }
    }
}

function wpse_post_submit_meta_box()
{
    _e("Sorry, you can't modify this post, it's already been modified by an administrator!");
}

et puis à la place de cette metabox:

The publish metabox available

l'auteur voit ceci:

The publish metabox NOT available

5
birgire