Making WordPress.org

Ticket #1199: 1199.diff

File 1199.diff, 7.7 KB (added by DrewAPicture, 9 years ago)
  • inc/parsed-content.php

     
    122122                                                        <a href="#detach-ticket" class="button secondary <?php echo $ticket ? '' : 'hidden'; ?>" id="wporg_ticket_detach" name="wporg_ticket_detach" aria-label="<?php esc_attr_e( 'Detach the Trac ticket', 'wporg' ); ?>" data-nonce="<?php echo wp_create_nonce( 'wporg-detach-ticket' ); ?>" data-id="<?php the_ID(); ?>">
    123123                                                                <?php esc_attr_e( 'Detach Ticket', 'wporg' ); ?>
    124124                                                        </a>
     125                                                        <span class="spinner"></span>
    125126                                                </span>
    126127                                                <div id="ticket_status">
    127                                                         <span class="spinner"></span>
    128128                                                        <span class="ticket_info_icon <?php echo $ticket ? 'dashicons dashicons-external' : ''; ?>"></span>
    129129                                                        <span id="wporg_ticket_info"><em><?php echo $ticket_message; ?></em></span>
    130130                                                </div>
     
    166166                        wp_enqueue_style( 'wporg-admin', get_template_directory_uri() . '/stylesheets/admin.css', array(), '20140826' );
    167167                        wp_enqueue_script( 'wporg-parsed-content', get_template_directory_uri() . '/js/parsed-content.js', array( 'jquery', 'utils' ), '20140826', true );
    168168
    169                         wp_localize_script( 'wporg-parsed-content', 'wporg', array(
     169                        wp_localize_script( 'wporg-parsed-content', 'wporgParsedContent', array(
    170170                                'ajaxURL'    => admin_url( 'admin-ajax.php' ),
    171171                                'searchText' => __( 'Searching ...', 'wporg' ),
     172                                'retryText'  => __( 'Invalid ticket number, please try again.', 'wporg' )
    172173                        ) );
    173174                }
    174175        }
     
    215216                        $link = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $ticket_url ),  apply_filters( 'the_title', $title ) );
    216217
    217218                        // Can haz success.
    218                         $message = array(
    219                                 'type'    => 'success',
    220                                 'message' => $link,
    221                         );
     219                        wp_send_json_success( array(
     220                                'message'   => $link,
     221                                'new_nonce' => wp_create_nonce( 'wporg-attach-ticket' )
     222                        ) );
    222223
    223224                } else {
    224225                        // Ticket number is invalid.
    225                         $message = array(
    226                                 'type'    => 'invalid',
    227                                 'message' => __( 'Invalid ticket number.', 'wporg' ),
    228                         );
     226                        wp_send_json_error( array(
     227                                'message'   => __( 'Invalid ticket number.', 'wporg' ),
     228                                'new_nonce' =>  wp_create_nonce( 'wporg-attach-ticket' )
     229                        ) );
    229230                }
    230231
    231                 // Slap on a new nonce for repeat offenders.
    232                 $message['new_nonce'] = wp_create_nonce( 'wporg-attach-ticket' );
    233 
    234                 die( json_encode( $message ) );
     232                die( 0 );
    235233        }
    236234
    237235        /**
     
    248246                if ( delete_post_meta( $post_id, 'wporg_ticket_number' )
    249247                        && delete_post_meta( $post_id, 'wporg_ticket_title' )
    250248                ) {
    251                         $message = array(
    252                                 'type'    => 'success',
    253                                 'message' => __( 'Ticket detached.', 'wporg' )
    254                         );
     249                        // Success!
     250                        wp_send_json_success( array(
     251                                'message'   => __( 'Ticket detached.', 'wporg' ),
     252                                'new_nonce' => wp_create_nonce( 'wporg-detach-ticket' )
     253                        ) );
     254
    255255                } else {
    256256                        // Still attached.
    257                         $message = array(
    258                                 'type'    => 'failure',
    259                                 'message' => __( 'Ticket still attached.', 'wporg' )
    260                         );
     257                        wp_send_json_error( array(
     258                                'message'   => __( 'Ticket still attached.', 'wporg' ),
     259                                'new_nonce' => wp_create_nonce( 'wporg-detach-ticket' )
     260                        ) );
    261261                }
    262262
    263                 // Slap on a new nonce for repeat offenders.
    264                 $message['new_nonce'] = wp_create_nonce( 'wporg-detach-ticket' );
    265 
    266                 die( json_encode( $message ) );
     263                die( 0 );
    267264        }
    268265
    269266} // WPORG_Edit_Parsed_Content
  • js/parsed-content.js

     
    77                attachButton   = $( '#wporg_ticket_attach' ),
    88                detachButton   = $( '#wporg_ticket_detach' ),
    99                ticketInfo     = $( '#wporg_ticket_info' ),
    10                 spinner        = $( '#ticket_status .spinner' );
     10                spinner        = $( '.attachment_controls .spinner' );
    1111
    1212        var handleTicket = function( event ) {
    1313                event.preventDefault();
     
    1515                var $this        = $(this),
    1616                        attachAction = 'attach' == event.data.action;
    1717
    18                 spinner.css( 'display', 'inline-block' );
     18                spinner.addClass( 'is-active' );
    1919
     20                // Searching ... text.
    2021                if ( attachAction ) {
    21                         ticketInfo.text( wporg.searchText );
     22                        ticketInfo.text( wporgParsedContent.searchText );
    2223                }
    2324
    24                 var data = {
    25                         action:  attachAction ? 'wporg_attach_ticket' : 'wporg_detach_ticket',
     25                var request = wp.ajax.post( attachAction ? 'wporg_attach_ticket' : 'wporg_detach_ticket', {
    2626                        ticket:  ticketNumber.val(),
    2727                        nonce:   $this.data( 'nonce' ),
    2828                        post_id: $this.data( 'id' )
    29                 };
     29                } );
    3030
    31                 $.post( wporg.ajaxURL, data, function( resp ) {
     31                // Success.
     32                request.done( function( response ) {
    3233                        // Refresh the nonce.
    33                         $this.data( 'nonce', resp.new_nonce );
     34                        $this.data( 'nonce', response.new_nonce );
    3435
    35                         spinner.hide();
     36                        // Hide or show the parsed content boxes.
     37                        $( '.wporg_parsed_content' ).each( function() {
     38                                attachAction ? $(this).show() : $(this).hide();
     39                        });
    3640
    37                         // Update the ticket info text
    38                         ticketInfo.html( resp.message ).show();
     41                        $( '.wporg_parsed_readonly' ).each( function() {
     42                                attachAction ? $(this).hide() : $(this).show();
     43                        });
    3944
    40                         // Handle the response.
    41                         if ( resp.type && 'success' == resp.type ) {
    42                                 // Hide or show the parsed content boxes.
    43                                 $( '.wporg_parsed_content' ).each( function() {
    44                                         attachAction ? $(this).show() : $(this).hide();
    45                                 });
     45                        var otherButton = attachAction ? detachButton : attachButton;
    4646
    47                                 $( '.wporg_parsed_readonly' ).each( function() {
    48                                         attachAction ? $(this).hide() : $(this).show();
    49                                 });
     47                        // Toggle the buttons.
     48                        $this.hide();
     49                        otherButton.css( 'display', 'inline-block' );
    5050
    51                                 var otherButton = attachAction ? detachButton : attachButton;
     51                        // Update the ticket info text.
     52                        ticketInfo.html( response.message ).show();
    5253
    53                                 // Toggle the buttons.
    54                                 $this.hide();
    55                                 otherButton.css( 'display', 'inline-block' );
     54                        // Clear the ticket number when detaching.
     55                        if ( ! attachAction ) {
     56                                ticketNumber.val( '' );
     57                        }
    5658
    57                                 // Clear the ticket number when detaching.
    58                                 if ( ! attachAction ) {
    59                                         ticketNumber.val( '' );
    60                                 }
     59                        spinner.removeClass( 'is-active' );
    6160
    62                                 // Set or unset the ticket link icon.
    63                                 $( '.ticket_info_icon' ).toggleClass( 'dashicons dashicons-external', attachAction );
     61                        // Set or unset the ticket link icon.
     62                        $( '.ticket_info_icon' ).toggleClass( 'dashicons dashicons-external', attachAction );
    6463
    65                                 // Set the ticket number to readonly when a ticket is attached.
    66                                 attachAction ? ticketNumber.prop( 'readonly', 'readonly' ) : ticketNumber.removeAttr( 'readonly' );
    67                         } else {
    68                                 ticketInfo.text( wporg.retryText );
    69                         }
     64                        // Set the ticket number to readonly when a ticket is attached.
     65                        attachAction ? ticketNumber.prop( 'readonly', 'readonly' ) : ticketNumber.removeAttr( 'readonly' );
     66                } );
    7067
    71                 }, 'json' );
     68                // Error.
     69                request.fail( function( response ) {
     70                        // Refresh the nonce.
     71                        $this.data( 'nonce', response.new_nonce );
     72
     73                        // Retry text.
     74                        ticketInfo.text( wporgParsedContent.retryText );
     75
     76                        spinner.removeClass( 'is-active' );
     77                } );
    7278        };
    7379
    7480        attachButton.on( 'click', { action: 'attach' }, handleTicket );
  • scss/admin.scss

     
    1010        color: #a00;
    1111}
    1212
    13 #ticket_status .spinner {
     13.attachment_controls .spinner {
    1414        position: relative;
     15        margin-top: 3px;
    1516        bottom: 4px;
    1617        float: none;
    1718}
  • stylesheets/admin.css

     
    99  color: #a00;
    1010}
    1111
    12 #ticket_status .spinner {
     12.attachment_controls .spinner {
    1313  position: relative;
     14  margin-top: 3px;
    1415  bottom: 4px;
    1516  float: none;
    1617}