Making WordPress.org


Ignore:
Timestamp:
03/15/2016 09:11:27 PM (9 years ago)
Author:
obenland
Message:

Plugin Directory: After [2752], improve error handling and docs.

See #1571.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/metabox/class-committers.php

    r2752 r2753  
    2222    }
    2323
     24    /**
     25     * Displays a list of committers for the current plugin.
     26     */
    2427    public static function display() {
    2528        $list = new Committers_List_Table();
     
    2831    }
    2932
     33    /**
     34     * Ajax handler for adding a new committer.
     35     */
    3036    public static function add_committer() {
    3137        check_ajax_referer( 'add-committer' );
    3238
    33         $login   = isset( $_POST['add_committer'] ) ? sanitize_user( $_POST['add_committer'] ) : '';
    34         $post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;
     39        $login    = isset( $_POST['add_committer'] ) ? sanitize_user( $_POST['add_committer'] ) : '';
     40        $post_id  = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;
     41        $response = new \WP_Ajax_Response();
    3542
    3643        if ( ! $committer = get_user_by( 'login', $login ) ) {
    37             wp_die( time() );
     44            $response->add( array(
     45                'what' => 'committer',
     46                'data' => new \WP_Error( 'error', sprintf( __( 'The user %s does not exist.', 'wporg-plugins' ), '<code>' . $login . '</code>' ) ),
     47            ) );
     48            $response->send();
    3849        }
    3950
    40         if ( ! current_user_can( 'manage_committers', $post_id ) ) {
    41         //  wp_die( -1 );
     51        // @todo: Capabilities.
     52        if ( ! current_user_can( 'add_committers', $post_id ) ) {
     53            //  wp_die( -1 );
    4254        }
    4355        global $post, $wpdb;
    4456
    45         $response = new \WP_Ajax_Response();
    46         $post     = get_post( $post_id );
    47 
     57        $post   = get_post( $post_id );
    4858        $result = $wpdb->insert( PLUGINS_TABLE_PREFIX . 'svn_access', array(
    4959            'path'   => "/{$post->post_name}",
     
    5161            'access' => 'rw',
    5262        ) );
    53 
    5463        if ( ! $result ) {
    55             $message = __( 'An error has occurred. Please reload the page and try again.' );
    56             if ( is_wp_error( $result ) && $result->get_error_message() ) {
    57                 $message = $result->get_error_message();
     64            if ( 'Duplicate entry' === substr( $wpdb->last_error, 0, 15 ) ) {
     65                $message = __( 'Duplicate committer detected.', 'wporg-plugins' );
     66            } else {
     67                $message = __( 'An error has occurred. Please reload the page and try again.', 'wporg-plugins' );
    5868            }
    5969
     
    7686    }
    7787
     88    /**
     89     * Ajax handler for removing a committer.
     90     */
    7891    public static function remove_committer() {
    7992        $id      = isset( $_POST['id'] )      ? (int) $_POST['id']      : 0;
     
    8295        check_ajax_referer( "remove-committer-$id" );
    8396
     97        $response = new \WP_Ajax_Response();
     98
    8499        if ( ! $committer = get_user_by( 'id', $id ) ) {
    85             wp_die( time() );
     100            $response->add( array(
     101                'what' => 'committer',
     102                'data' => new \WP_Error( 'error', sprintf( __( 'The user %s does not exist.', 'wporg-plugins' ), '<code>' . $login . '</code>' ) ),
     103            ) );
     104            $response->send();
    86105        }
    87106
    88         if ( ! current_user_can( 'manage_committers', $post_id ) ) {
    89         //  wp_die( -1 );
     107        // @todo: Capabilities.
     108        if ( ! current_user_can( 'remove_committers', $post_id ) ) {
     109            //  wp_die( -1 );
    90110        }
    91111
     
    93113
    94114        $result = $GLOBALS['wpdb']->delete( PLUGINS_TABLE_PREFIX . 'svn_access', array(
    95             'path'   => "/{$plugin_slug}",
    96             'user'   => $committer->user_login,
     115            'path' => "/{$plugin_slug}",
     116            'user' => $committer->user_login,
    97117        ) );
    98118
Note: See TracChangeset for help on using the changeset viewer.