Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/20/2011 11:39:56 PM (15 years ago)
Author:
djpaul
Message:

See #3660. Activity admin iteration.

  • "Reply" button now works.
  • Fixed incorrect HTML markup.
  • Improve error handling when an update on an activity fails.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-admin.php

    r5330 r5339  
    4040}
    4141add_action( bp_core_admin_hook(), 'bp_activity_add_admin_menu' );
     42
     43/**
     44 * AJAX receiver for Activity replies via the admin screen. Adds a new activity
     45 * comment, and returns HTML for a new table row.
     46 *
     47 * @since 1.6
     48 */
     49function bp_activity_admin_reply() {
     50        // Check nonce
     51        check_ajax_referer( 'bp-activity-admin-reply', '_ajax_nonce-bp-activity-admin-reply' );
     52
     53        $parent_id = ! empty( $_REQUEST['parent_id'] ) ? (int) $_REQUEST['parent_id'] : 0;
     54        $root_id   = ! empty( $_REQUEST['root_id'] )   ? (int) $_REQUEST['root_id']   : 0;
     55
     56        // $parent_id is required
     57        if ( empty( $parent_id ) )
     58                die( '-1' );
     59
     60        // If $root_id not set (e.g. for root items), use $parent_id
     61        if ( empty( $root_id ) )
     62                $root_id = $parent_id;
     63
     64        // Check that a reply has been entered
     65        if ( empty( $_REQUEST['content'] ) )
     66                die( __( 'ERROR: Please type a reply.', 'buddypress' ) );
     67
     68        // Check parent activity exists
     69        $parent_activity = new BP_Activity_Activity( $parent_id );
     70        if ( empty( $parent_activity->component ) )
     71                die( __( 'ERROR: The item you are trying to reply to cannot be found, or it has been deleted.', 'buddypress' ) );
     72
     73        // @todo: Check if user is allowed to create new activity items
     74        // if ( ! current_user_can( 'bp_new_activity' ) )
     75        if ( ! is_super_admin() )
     76                die( '-1' );
     77
     78        // Add new activity comment
     79        $new_activity_id = bp_activity_new_comment( array(
     80                'activity_id' => $root_id,              // ID of the root activity item
     81                'content'     => $_REQUEST['content'],
     82                'parent_id'   => $parent_id,            // ID of a parent comment
     83        ) );
     84
     85        // Fetch the new activity item, as we need it to create table markup to return
     86        $new_activity = new BP_Activity_Activity( $new_activity_id );
     87
     88        // This needs to be set for the BP_Activity_List_Table constructor to work
     89        set_current_screen( 'toplevel_page_bp-activity' );
     90
     91        // Set up an output buffer
     92        ob_start();
     93        $list_table = new BP_Activity_List_Table();
     94        $list_table->single_row( (array) $new_activity );
     95
     96        // Get table markup
     97        $response =  array(
     98                'data'     => ob_get_contents(),
     99                'id'       => $new_activity_id,
     100                'position' => -1,
     101                'what'     => 'bp_activity',
     102        );
     103        ob_end_clean();
     104
     105        // Send response
     106        $r = new WP_Ajax_Response();
     107        $r->add( $response );
     108        $r->send();
     109
     110        exit();
     111}
     112add_action( 'wp_ajax_bp-activity-admin-reply', 'bp_activity_admin_reply' );
    42113
    43114/**
     
    88159        );
    89160
     161        // Enqueue CSS and JavaScript
     162        if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
     163                wp_enqueue_script( 'bp_activity_admin_js', BP_PLUGIN_URL . 'bp-activity/admin/js/admin.dev.js', array( 'jquery', 'wp-ajax-response' ), '20111120' );
     164                wp_enqueue_style( 'bp_activity_admin_css', BP_PLUGIN_URL . 'bp-activity/admin/css/admin.dev.css', array(), '20111120' );
     165
     166        } else {
     167                wp_enqueue_script( 'bp_activity_admin_js', BP_PLUGIN_URL . 'bp-activity/admin/js/admin.js', array( 'jquery', 'wp-ajax-response' ), '20111120' );
     168                wp_enqueue_style( 'bp_activity_admin_css', BP_PLUGIN_URL . 'bp-activity/admin/css/admin.css', array(), '20111120' );
     169        }
     170
    90171        // Create the Activity screen list table
    91172        $bp_activity_list_table = new BP_Activity_List_Table();
     
    96177
    97178                // Build redirection URL
    98                 $redirect_to = remove_query_arg( array( 'aid', 'deleted', 'spammed', 'unspammed', ), wp_get_referer() );
     179                $redirect_to = remove_query_arg( array( 'aid', 'deleted', 'error', 'spammed', 'unspammed', ), wp_get_referer() );
    99180                $redirect_to = add_query_arg( 'paged', $bp_activity_list_table->get_pagenum(), $redirect_to );
    100181
     
    119200                // Initialise counters for how many of each type of item we perform an action on
    120201                $deleted = $spammed = $unspammed = 0;
     202
     203                // Store any error that occurs when updating the database item
     204                $error = 0;
    121205
    122206                // "We'd like to shoot the monster, could you move, please?"
     
    143227                                case 'ham' :
    144228                                        bp_activity_mark_as_ham( $activity );
    145                                         $activity->save();
    146 
     229                                        $result = $activity->save();
     230
     231                                        // Check for any error during activity save
     232                                        if ( ! $result ) {
     233                                                $error = $activity->id;
     234                                                break;
     235                                        }
     236
     237                                        $unspammed++;
    147238                                        break;
    148239
    149240                                case 'spam' :
    150241                                        bp_activity_mark_as_spam( $activity );
    151                                         $activity->save();
     242                                        $result = $activity->save();
     243
     244                                        // Check for any error during activity save
     245                                        if ( ! $result ) {
     246                                                $error = $activity->id;
     247                                                break;
     248                                        }
    152249
    153250                                        $spammed++;
     
    158255                        }
    159256
     257                        // If an error occured, don't bother looking at the other activities. Bail out of the foreach.
     258                        if ( $error )
     259                                break;
     260
    160261                        // Release memory
    161262                        unset( $activity );
     
    171272                if ( $deleted )
    172273                        $redirect_to = add_query_arg( 'deleted', $deleted, $redirect_to );
     274
     275                // If an error occured, pass back the activity ID that failed
     276                if ( $error )
     277                        $redirect_to = add_query_arg( 'error', (int) $error, $redirect_to );
    173278
    174279                // Redirect
     
    195300
    196301        // If the user has just made a change to an activity item, build status messages
    197         if ( !empty( $_REQUEST['deleted'] ) || !empty( $_REQUEST['spammed'] ) || !empty( $_REQUEST['unspammed'] ) ) {
     302        if ( !empty( $_REQUEST['deleted'] ) || !empty( $_REQUEST['spammed'] ) || !empty( $_REQUEST['unspammed'] ) || !empty( $_REQUEST['error'] ) ) {
    198303                $deleted   = !empty( $_REQUEST['deleted']   ) ? (int) $_REQUEST['deleted']   : 0;
     304                $error     = !empty( $_REQUEST['error']     ) ? (int) $_REQUEST['error']     : 0;
    199305                $spammed   = !empty( $_REQUEST['spammed']   ) ? (int) $_REQUEST['spammed']   : 0;
    200306                $unspammed = !empty( $_REQUEST['unspammed'] ) ? (int) $_REQUEST['unspammed'] : 0;
     
    202308                if ( $deleted > 0 )
    203309                        $messages[] = sprintf( _n( '%s activity was permanently deleted.', '%s activities were permanently deleted.', $deleted, 'buddypress' ), $deleted );
     310
     311                if ( $error > 0 )
     312                        $messages[] = sprintf( __( 'An error occured when updating Activity ID #%d.', 'buddypress' ), $error );
    204313
    205314                if ( $spammed > 0 )
     
    235344                <?php // If the user has just made a change to an activity item, display the status messages ?>
    236345                <?php if ( !empty( $messages ) ) : ?>
    237                         <div id="moderated" class="updated"><p><?php echo implode( "<br/>\n", $messages ); ?></p></div>
     346                        <div id="moderated" class="<?php echo ( ! empty( $_REQUEST['error'] ) ) ? 'error' : 'updated'; ?>"><p><?php echo implode( "<br/>\n", $messages ); ?></p></div>
    238347                <?php endif; ?>
    239348
     
    246355                        <?php $bp_activity_list_table->display(); ?>
    247356                </form>
     357
     358                <table style="display: none;">
     359                        <tr id="bp-activities-container" style="display: none;">
     360                                <td colspan="4">
     361                                        <form method="get" action="">
     362
     363                                                <h5 id="bp-replyhead"><?php _e( 'Reply to Activity', 'buddypress' ); ?></h5>
     364                                                <?php wp_editor( '', 'bp-activities', array( 'dfw' => false, 'media_buttons' => false, 'quicktags' => array( 'buttons' => 'strong,em,link,block,del,ins,img,code,spell,close' ), 'tinymce' => false, ) ); ?>
     365
     366                                                <p id="bp-replysubmit" class="submit">
     367                                                        <a href="#" class="cancel button-secondary alignleft"><?php _e( 'Cancel', 'tmggc' ); ?></a>
     368                                                        <a href="#" class="save button-primary alignright"><?php _e( 'Reply', 'tmggc' ); ?></a>
     369
     370                                                        <img class="waiting" style="display:none;" src="<?php echo esc_url( network_admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
     371                                                        <span class="error" style="display:none;"></span>
     372                                                        <br class="clear" />
     373                                                </p>
     374
     375                                                <?php wp_nonce_field( 'bp-activity-admin-reply', '_ajax_nonce-bp-activity-admin-reply', false ); ?>
     376
     377                                        </form>
     378                                </td>
     379                        </tr>
     380                </table>
    248381        </div>
    249382
     
    437570
    438571        /**
     572         * Generates content for a single row of the table
     573         *
     574         * @param object $item The current item
     575         * @since 1.6
     576         */
     577        function single_row( $item ) {
     578                static $row_class = '';
     579                $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
     580
     581                echo '<tr' . $row_class . ' id="activity-' . esc_attr( $item['id'] ) . '" data-parent_id="' . esc_attr( $item['id'] ) . '" data-root_id="' . esc_attr( $item['item_id'] ) . '">';
     582                echo $this->single_row_columns( $item );
     583                echo '</tr>';
     584        }
     585
     586        /**
    439587         * Get the list of views available on this table (e.g. "all", "spam").
    440588         *
     
    451599        }
    452600
    453         /**
     601                /**
    454602         * Get bulk actions
    455603         *
     
    599747                $spam_nonce = esc_html( '_wpnonce=' . wp_create_nonce( 'spam-activity_' . $item['id'] ) );
    600748
    601                 $delete_url = $base_url . "&amp;action=delete&$spam_nonce";
     749                $delete_url = $base_url . "&amp;action=delete&amp;$spam_nonce";
    602750                $edit_url   = $base_url . '&amp;action=edit';
    603                 $ham_url    = $base_url . "&amp;action=ham&$spam_nonce";
    604                 $spam_url   = $base_url . "&amp;action=spam&$spam_nonce";
     751                $ham_url    = $base_url . "&amp;action=ham&amp;$spam_nonce";
     752                $spam_url   = $base_url . "&amp;action=spam&amp;$spam_nonce";
    605753
    606754                // Rollover actions
    607755
    608756                // Reply - javascript only; implemented by AJAX.
    609                 if ( 'spam' != $item_status )
    610                         $actions['reply'] = sprintf( '<a href="%s" class="reply hide-if-no-js">%s</a>', $base_url, __( 'Reply', 'buddypress' ) );
    611 
    612                 // Edit
    613                 $actions['edit'] = sprintf( '<a href="%s">%s</a>', $edit_url, __( 'Edit', 'buddypress' ) );
     757                if ( 'spam' != $item_status ) {
     758                        $actions['reply'] = sprintf( '<a href="#" class="reply hide-if-no-js">%s</a>', __( 'Reply', 'buddypress' ) );
     759
     760                        // Edit
     761                        $actions['edit'] = sprintf( '<a href="%s">%s</a>', $edit_url, __( 'Edit', 'buddypress' ) );
     762                }
    614763
    615764                // Spam/unspam
Note: See TracChangeset for help on using the changeset viewer.