Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/05/2016 03:54:56 AM (10 years ago)
Author:
boonebgorges
Message:

Move bp-activity classes to their own files.

See #6870.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/classes/class-bp-activity-list-table.php

    r10515 r10516  
    11<?php
    22/**
    3  * BuddyPress Activity component admin screen.
     3 * BuddyPress Activity component admin list table.
    44 *
    55 * Props to WordPress core for the Comments admin screen, and its contextual
     
    1313// Exit if accessed directly.
    1414defined( 'ABSPATH' ) || exit;
    15 
    16 // Include WP's list table class.
    17 if ( !class_exists( 'WP_List_Table' ) ) require( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
    18 
    19 // Per_page screen option. Has to be hooked in extremely early.
    20 if ( is_admin() && ! empty( $_REQUEST['page'] ) && 'bp-activity' == $_REQUEST['page'] )
    21     add_filter( 'set-screen-option', 'bp_activity_admin_screen_options', 10, 3 );
    22 
    23 /**
    24  * Register the Activity component admin screen.
    25  *
    26  * @since 1.6.0
    27  */
    28 function bp_activity_add_admin_menu() {
    29 
    30     // Add our screen.
    31     $hook = add_menu_page(
    32         _x( 'Activity', 'Admin Dashbord SWA page title', 'buddypress' ),
    33         _x( 'Activity', 'Admin Dashbord SWA menu', 'buddypress' ),
    34         'bp_moderate',
    35         'bp-activity',
    36         'bp_activity_admin',
    37         'div'
    38     );
    39 
    40     // Hook into early actions to load custom CSS and our init handler.
    41     add_action( "load-$hook", 'bp_activity_admin_load' );
    42 }
    43 add_action( bp_core_admin_hook(), 'bp_activity_add_admin_menu' );
    44 
    45 /**
    46  * Add activity component to custom menus array.
    47  *
    48  * Several BuddyPress components have top-level menu items in the Dashboard,
    49  * which all appear together in the middle of the Dashboard menu. This function
    50  * adds the Activity page to the array of these menu items.
    51  *
    52  * @since 1.7.0
    53  *
    54  * @param array $custom_menus The list of top-level BP menu items.
    55  * @return array $custom_menus List of top-level BP menu items, with Activity added.
    56  */
    57 function bp_activity_admin_menu_order( $custom_menus = array() ) {
    58     array_push( $custom_menus, 'bp-activity' );
    59     return $custom_menus;
    60 }
    61 add_filter( 'bp_admin_menu_order', 'bp_activity_admin_menu_order' );
    62 
    63 /**
    64  * AJAX receiver for Activity replies via the admin screen.
    65  *
    66  * Processes requests to add new activity comments, and echoes HTML for a new
    67  * table row.
    68  *
    69  * @since 1.6.0
    70  */
    71 function bp_activity_admin_reply() {
    72     // Check nonce.
    73     check_ajax_referer( 'bp-activity-admin-reply', '_ajax_nonce-bp-activity-admin-reply' );
    74 
    75     $parent_id = ! empty( $_REQUEST['parent_id'] ) ? (int) $_REQUEST['parent_id'] : 0;
    76     $root_id   = ! empty( $_REQUEST['root_id'] )   ? (int) $_REQUEST['root_id']   : 0;
    77 
    78     // $parent_id is required
    79     if ( empty( $parent_id ) )
    80         die( '-1' );
    81 
    82     // If $root_id not set (e.g. for root items), use $parent_id.
    83     if ( empty( $root_id ) )
    84         $root_id = $parent_id;
    85 
    86     // Check that a reply has been entered.
    87     if ( empty( $_REQUEST['content'] ) )
    88         die( __( 'ERROR: Please type a reply.', 'buddypress' ) );
    89 
    90     // Check parent activity exists.
    91     $parent_activity = new BP_Activity_Activity( $parent_id );
    92     if ( empty( $parent_activity->component ) )
    93         die( __( 'ERROR: The item you are trying to reply to cannot be found, or it has been deleted.', 'buddypress' ) );
    94 
    95     // @todo: Check if user is allowed to create new activity items
    96     // if ( ! current_user_can( 'bp_new_activity' ) )
    97     if ( ! current_user_can( 'bp_moderate' ) )
    98         die( '-1' );
    99 
    100     // Add new activity comment.
    101     $new_activity_id = bp_activity_new_comment( array(
    102         'activity_id' => $root_id,              // ID of the root activity item.
    103         'content'     => $_REQUEST['content'],
    104         'parent_id'   => $parent_id,            // ID of a parent comment.
    105     ) );
    106 
    107     // Fetch the new activity item, as we need it to create table markup to return.
    108     $new_activity = new BP_Activity_Activity( $new_activity_id );
    109 
    110     // This needs to be set for the BP_Activity_List_Table constructor to work.
    111     set_current_screen( 'toplevel_page_bp-activity' );
    112 
    113     // Set up an output buffer.
    114     ob_start();
    115     $list_table = new BP_Activity_List_Table();
    116     $list_table->single_row( (array) $new_activity );
    117 
    118     // Get table markup.
    119     $response =  array(
    120         'data'     => ob_get_contents(),
    121         'id'       => $new_activity_id,
    122         'position' => -1,
    123         'what'     => 'bp_activity',
    124     );
    125     ob_end_clean();
    126 
    127     // Send response.
    128     $r = new WP_Ajax_Response();
    129     $r->add( $response );
    130     $r->send();
    131 
    132     exit();
    133 }
    134 add_action( 'wp_ajax_bp-activity-admin-reply', 'bp_activity_admin_reply' );
    135 
    136 /**
    137  * Handle save/update of screen options for the Activity component admin screen.
    138  *
    139  * @since 1.6.0
    140  *
    141  * @param string $value     Will always be false unless another plugin filters it first.
    142  * @param string $option    Screen option name.
    143  * @param string $new_value Screen option form value.
    144  * @return string Option value. False to abandon update.
    145  */
    146 function bp_activity_admin_screen_options( $value, $option, $new_value ) {
    147     if ( 'toplevel_page_bp_activity_per_page' != $option && 'toplevel_page_bp_activity_network_per_page' != $option )
    148         return $value;
    149 
    150     // Per page.
    151     $new_value = (int) $new_value;
    152     if ( $new_value < 1 || $new_value > 999 )
    153         return $value;
    154 
    155     return $new_value;
    156 }
    157 
    158 /**
    159  * Hide the advanced edit meta boxes by default, so we don't clutter the screen.
    160  *
    161  * @since 1.6.0
    162  *
    163  * @param array     $hidden Array of items to hide.
    164  * @param WP_Screen $screen Screen identifier.
    165  * @return array Hidden Meta Boxes.
    166  */
    167 function bp_activity_admin_edit_hidden_metaboxes( $hidden, $screen ) {
    168     if ( empty( $screen->id ) || 'toplevel_page_bp-activity' != $screen->id && 'toplevel_page_bp-activity_network' != $screen->id )
    169         return $hidden;
    170 
    171     // Hide the primary link meta box by default.
    172     $hidden  = array_merge( (array) $hidden, array( 'bp_activity_itemids', 'bp_activity_link', 'bp_activity_type', 'bp_activity_userid', ) );
    173 
    174     /**
    175      * Filters default hidden metaboxes so plugins can alter list.
    176      *
    177      * @since 1.6.0
    178      *
    179      * @param array     $hidden Default metaboxes to hide.
    180      * @param WP_Screen $screen Screen identifier.
    181      */
    182     return apply_filters( 'bp_hide_meta_boxes', array_unique( $hidden ), $screen );
    183 }
    184 add_filter( 'default_hidden_meta_boxes', 'bp_activity_admin_edit_hidden_metaboxes', 10, 2 );
    185 
    186 /**
    187  * Set up the Activity admin page.
    188  *
    189  * Does the following:
    190  *   - Register contextual help and screen options for this admin page.
    191  *   - Enqueues scripts and styles.
    192  *   - Catches POST and GET requests related to Activity.
    193  *
    194  * @since 1.6.0
    195  *
    196  * @global object                 $bp                     BuddyPress global settings.
    197  * @global BP_Activity_List_Table $bp_activity_list_table Activity screen list table.
    198  */
    199 function bp_activity_admin_load() {
    200     global $bp_activity_list_table;
    201 
    202     $bp = buddypress();
    203 
    204     // Decide whether to load the dev version of the CSS and JavaScript.
    205     $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : 'min.';
    206 
    207     $doaction = bp_admin_list_table_current_bulk_action();
    208 
    209     /**
    210      * Fires at top of Activity admin page.
    211      *
    212      * @since 1.6.0
    213      *
    214      * @param string $doaction Current $_GET action being performed in admin screen.
    215      */
    216     do_action( 'bp_activity_admin_load', $doaction );
    217 
    218     // Edit screen.
    219     if ( 'edit' == $doaction && ! empty( $_GET['aid'] ) ) {
    220         // Columns screen option.
    221         add_screen_option( 'layout_columns', array( 'default' => 2, 'max' => 2, ) );
    222 
    223         get_current_screen()->add_help_tab( array(
    224             'id'      => 'bp-activity-edit-overview',
    225             'title'   => __( 'Overview', 'buddypress' ),
    226             'content' =>
    227                 '<p>' . __( 'You edit activities made on your site similar to the way you edit a comment. This is useful if you need to change which page the activity links to, or when you notice that the author has made a typographical error.', 'buddypress' ) . '</p>' .
    228                 '<p>' . __( 'The two big editing areas for the activity title and content are fixed in place, but you can reposition all the other boxes using drag and drop, and can minimize or expand them by clicking the title bar of each box. Use the Screen Options tab to unhide more boxes (Primary Item/Secondary Item, Link, Type, Author ID) or to choose a 1- or 2-column layout for this screen.', 'buddypress' ) . '</p>' .
    229                 '<p>' . __( 'You can also moderate the activity from this screen using the Status box, where you can also change the timestamp of the activity.', 'buddypress' ) . '</p>'
    230         ) );
    231 
    232         get_current_screen()->add_help_tab( array(
    233             'id'      => 'bp-activity-edit-advanced',
    234             'title'   => __( 'Item, Link, Type', 'buddypress' ),
    235             'content' =>
    236                 '<p>' . __( '<strong>Primary Item/Secondary Item</strong> - These identify the object that created the activity. For example, the fields could reference a comment left on a specific site. Some types of activity may only use one, or none, of these fields.', 'buddypress' ) . '</p>' .
    237                 '<p>' . __( '<strong>Link</strong> - Used by some types of activity (e.g blog posts and comments, and forum topics and replies) to store a link back to the original content.', 'buddypress' ) . '</p>' .
    238                 '<p>' . __( '<strong>Type</strong> - Each distinct kind of activity has its own type. For example, <code>created_group</code> is used when a group is created and <code>joined_group</code> is used when a user joins a group.', 'buddypress' ) . '</p>' .
    239                 '<p>' . __( 'For information about when and how BuddyPress uses all of these settings, see the Managing Activity link in the panel to the side.', 'buddypress' ) . '</p>'
    240         ) );
    241 
    242         // Help panel - sidebar links.
    243         get_current_screen()->set_help_sidebar(
    244             '<p><strong>' . __( 'For more information:', 'buddypress' ) . '</strong></p>' .
    245             '<p>' . __( '<a href="https://codex.buddypress.org/administrator-guide/activity-stream-management-panels/">Managing Activity</a>', 'buddypress' ) . '</p>' .
    246             '<p>' . __( '<a href="https://buddypress.org/support/">Support Forums</a>', 'buddypress' ) . '</p>'
    247         );
    248 
    249         // Register metaboxes for the edit screen.
    250         add_meta_box( 'submitdiv',           _x( 'Status', 'activity admin edit screen', 'buddypress' ), 'bp_activity_admin_edit_metabox_status', get_current_screen()->id, 'side', 'core' );
    251         add_meta_box( 'bp_activity_itemids', _x( 'Primary Item/Secondary Item', 'activity admin edit screen', 'buddypress' ), 'bp_activity_admin_edit_metabox_itemids', get_current_screen()->id, 'normal', 'core' );
    252         add_meta_box( 'bp_activity_link',    _x( 'Link', 'activity admin edit screen', 'buddypress' ), 'bp_activity_admin_edit_metabox_link', get_current_screen()->id, 'normal', 'core' );
    253         add_meta_box( 'bp_activity_type',    _x( 'Type', 'activity admin edit screen', 'buddypress' ), 'bp_activity_admin_edit_metabox_type', get_current_screen()->id, 'normal', 'core' );
    254         add_meta_box( 'bp_activity_userid',  _x( 'Author ID', 'activity admin edit screen', 'buddypress' ), 'bp_activity_admin_edit_metabox_userid', get_current_screen()->id, 'normal', 'core' );
    255 
    256         /**
    257          * Fires after the registration of all of the default activity meta boxes.
    258          *
    259          * @since 2.4.0
    260          */
    261         do_action( 'bp_activity_admin_meta_boxes' );
    262 
    263         // Enqueue JavaScript files.
    264         wp_enqueue_script( 'postbox' );
    265         wp_enqueue_script( 'dashboard' );
    266         wp_enqueue_script( 'comment' );
    267 
    268     // Index screen.
    269     } else {
    270         // Create the Activity screen list table.
    271         $bp_activity_list_table = new BP_Activity_List_Table();
    272 
    273         // The per_page screen option.
    274         add_screen_option( 'per_page', array( 'label' => _x( 'Activity', 'Activity items per page (screen options)', 'buddypress' )) );
    275 
    276         // Help panel - overview text.
    277         get_current_screen()->add_help_tab( array(
    278             'id'      => 'bp-activity-overview',
    279             'title'   => __( 'Overview', 'buddypress' ),
    280             'content' =>
    281                 '<p>' . __( 'You can manage activities made on your site similar to the way you manage comments and other content. This screen is customizable in the same ways as other management screens, and you can act on activities using the on-hover action links or the Bulk Actions.', 'buddypress' ) . '</p>' .
    282                 '<p>' . __( 'There are many different types of activities. Some are generated automatically by BuddyPress and other plugins, and some are entered directly by a user in the form of status update. To help manage the different activity types, use the filter dropdown box to switch between them.', 'buddypress' ) . '</p>'
    283         ) );
    284 
    285         // Help panel - moderation text.
    286         get_current_screen()->add_help_tab( array(
    287             'id'        => 'bp-activity-moderating',
    288             'title'     => __( 'Moderating Activity', 'buddypress' ),
    289             'content'   =>
    290                 '<p>' . __( 'In the <strong>Activity</strong> column, above each activity it says &#8220;Submitted on,&#8221; followed by the date and time the activity item was generated on your site. Clicking on the date/time link will take you to that activity on your live site. Hovering over any activity gives you options to reply, edit, spam mark, or delete that activity.', 'buddypress' ) . '</p>' .
    291                 '<p>' . __( "In the <strong>In Response To</strong> column, if the activity was in reply to another activity, it shows that activity's author's picture and name, and a link to that activity on your live site. If there is a small bubble, the number in it shows how many other activities are related to this one; these are usually comments. Clicking the bubble will filter the activity screen to show only related activity items.", 'buddypress' ) . '</p>'
    292         ) );
    293 
    294         // Help panel - sidebar links.
    295         get_current_screen()->set_help_sidebar(
    296             '<p><strong>' . __( 'For more information:', 'buddypress' ) . '</strong></p>' .
    297             '<p>' . __( '<a href="https://buddypress.org/support/">Support Forums</a>', 'buddypress' ) . '</p>'
    298         );
    299 
    300         // Add accessible hidden heading and text for Activity screen pagination.
    301         if ( bp_get_major_wp_version() >= 4.4 ) {
    302             get_current_screen()->set_screen_reader_content( array(
    303                 'heading_pagination' => __( 'Activity list navigation', 'buddypress' ),
    304             ) );
    305         }
    306     }
    307 
    308     // Enqueue CSS and JavaScript.
    309     wp_enqueue_script( 'bp_activity_admin_js', $bp->plugin_url . "bp-activity/admin/js/admin.{$min}js",   array( 'jquery', 'wp-ajax-response' ), bp_get_version(), true );
    310     wp_localize_script( 'bp_activity_admin_js', 'bp_activity_admin_vars', array(
    311         'page' => get_current_screen()->id
    312     ) );
    313     wp_enqueue_style( 'bp_activity_admin_css', $bp->plugin_url . "bp-activity/admin/css/admin.{$min}css", array(),                               bp_get_version()       );
    314 
    315     wp_style_add_data( 'bp_activity_admin_css', 'rtl', true );
    316     if ( $min ) {
    317         wp_style_add_data( 'bp_activity_admin_css', 'suffix', $min );
    318     }
    319 
    320     /**
    321      * Fires after the activity js and style has been enqueued.
    322      *
    323      * @since 2.4.0
    324      */
    325     do_action( 'bp_activity_admin_enqueue_scripts' );
    326 
    327     // Handle spam/un-spam/delete of activities.
    328     if ( !empty( $doaction ) && ! in_array( $doaction, array( '-1', 'edit', 'save', ) ) ) {
    329 
    330         // Build redirection URL.
    331         $redirect_to = remove_query_arg( array( 'aid', 'deleted', 'error', 'spammed', 'unspammed', ), wp_get_referer() );
    332         $redirect_to = add_query_arg( 'paged', $bp_activity_list_table->get_pagenum(), $redirect_to );
    333 
    334         // Get activity IDs.
    335         $activity_ids = array_map( 'absint', (array) $_REQUEST['aid'] );
    336 
    337         /**
    338          * Filters list of IDs being spammed/un-spammed/deleted.
    339          *
    340          * @since 1.6.0
    341          *
    342          * @param array $activity_ids Activity IDs to spam/un-spam/delete.
    343          */
    344         $activity_ids = apply_filters( 'bp_activity_admin_action_activity_ids', $activity_ids );
    345 
    346         // Is this a bulk request?
    347         if ( 'bulk_' == substr( $doaction, 0, 5 ) && ! empty( $_REQUEST['aid'] ) ) {
    348             // Check this is a valid form submission.
    349             check_admin_referer( 'bulk-activities' );
    350 
    351             // Trim 'bulk_' off the action name to avoid duplicating a ton of code.
    352             $doaction = substr( $doaction, 5 );
    353 
    354         // This is a request to delete, spam, or un-spam, a single item.
    355         } elseif ( !empty( $_REQUEST['aid'] ) ) {
    356 
    357             // Check this is a valid form submission.
    358             check_admin_referer( 'spam-activity_' . $activity_ids[0] );
    359         }
    360 
    361         // Initialise counters for how many of each type of item we perform an action on.
    362         $deleted = $spammed = $unspammed = 0;
    363 
    364         // Store any errors that occurs when updating the database items.
    365         $errors = array();
    366 
    367         // "We'd like to shoot the monster, could you move, please?"
    368         foreach ( $activity_ids as $activity_id ) {
    369             // @todo: Check the permissions on each
    370             // if ( ! current_user_can( 'bp_edit_activity', $activity_id ) )
    371             // continue;
    372             // Get the activity from the database.
    373             $activity = new BP_Activity_Activity( $activity_id );
    374             if ( empty( $activity->component ) ) {
    375                 $errors[] = $activity_id;
    376                 continue;
    377             }
    378 
    379             switch ( $doaction ) {
    380                 case 'delete' :
    381                     if ( 'activity_comment' == $activity->type )
    382                         bp_activity_delete_comment( $activity->item_id, $activity->id );
    383                     else
    384                         bp_activity_delete( array( 'id' => $activity->id ) );
    385 
    386                     $deleted++;
    387                     break;
    388 
    389                 case 'ham' :
    390                     /**
    391                      * Remove moderation and blacklist checks in case we want to ham an activity
    392                      * which contains one of these listed keys.
    393                      */
    394                     remove_action( 'bp_activity_before_save', 'bp_activity_check_moderation_keys', 2, 1 );
    395                     remove_action( 'bp_activity_before_save', 'bp_activity_check_blacklist_keys',  2, 1 );
    396 
    397                     bp_activity_mark_as_ham( $activity );
    398                     $result = $activity->save();
    399 
    400                     // Check for any error during activity save.
    401                     if ( ! $result )
    402                         $errors[] = $activity->id;
    403                     else
    404                         $unspammed++;
    405                     break;
    406 
    407                 case 'spam' :
    408                     bp_activity_mark_as_spam( $activity );
    409                     $result = $activity->save();
    410 
    411                     // Check for any error during activity save.
    412                     if ( ! $result )
    413                         $errors[] = $activity->id;
    414                     else
    415                         $spammed++;
    416                     break;
    417 
    418                 default:
    419                     break;
    420             }
    421 
    422             // Release memory.
    423             unset( $activity );
    424         }
    425 
    426         /**
    427          * Fires before redirect for plugins to do something with activity.
    428          *
    429          * Passes an activity array counts how many were spam, not spam, deleted, and IDs that were errors.
    430          *
    431          * @since 1.6.0
    432          *
    433          * @param array  $value        Array holding spam, not spam, deleted counts, error IDs.
    434          * @param string $redirect_to  URL to redirect to.
    435          * @param array  $activity_ids Original array of activity IDs.
    436          */
    437         do_action( 'bp_activity_admin_action_after', array( $spammed, $unspammed, $deleted, $errors ), $redirect_to, $activity_ids );
    438 
    439         // Add arguments to the redirect URL so that on page reload, we can easily display what we've just done.
    440         if ( $spammed )
    441             $redirect_to = add_query_arg( 'spammed', $spammed, $redirect_to );
    442 
    443         if ( $unspammed )
    444             $redirect_to = add_query_arg( 'unspammed', $unspammed, $redirect_to );
    445 
    446         if ( $deleted )
    447             $redirect_to = add_query_arg( 'deleted', $deleted, $redirect_to );
    448 
    449         // If an error occurred, pass back the activity ID that failed.
    450         if ( ! empty( $errors ) )
    451             $redirect_to = add_query_arg( 'error', implode ( ',', array_map( 'absint', $errors ) ), $redirect_to );
    452 
    453         /**
    454          * Filters redirect URL after activity spamming/un-spamming/deletion occurs.
    455          *
    456          * @since 1.6.0
    457          *
    458          * @param string $redirect_to URL to redirect to.
    459          */
    460         wp_redirect( apply_filters( 'bp_activity_admin_action_redirect', $redirect_to ) );
    461         exit;
    462 
    463 
    464     // Save the edit.
    465     } elseif ( $doaction && 'save' == $doaction ) {
    466         // Build redirection URL.
    467         $redirect_to = remove_query_arg( array( 'action', 'aid', 'deleted', 'error', 'spammed', 'unspammed', ), $_SERVER['REQUEST_URI'] );
    468 
    469         // Get activity ID.
    470         $activity_id = (int) $_REQUEST['aid'];
    471 
    472         // Check this is a valid form submission.
    473         check_admin_referer( 'edit-activity_' . $activity_id );
    474 
    475         // Get the activity from the database.
    476         $activity = new BP_Activity_Activity( $activity_id );
    477 
    478         // If the activity doesn't exist, just redirect back to the index.
    479         if ( empty( $activity->component ) ) {
    480             wp_redirect( $redirect_to );
    481             exit;
    482         }
    483 
    484         // Check the form for the updated properties.
    485         // Store any error that occurs when updating the database item.
    486         $error = 0;
    487 
    488         // Activity spam status.
    489         $prev_spam_status = $new_spam_status = false;
    490         if ( ! empty( $_POST['activity_status'] ) ) {
    491             $prev_spam_status = $activity->is_spam;
    492             $new_spam_status  = ( 'spam' == $_POST['activity_status'] ) ? true : false;
    493         }
    494 
    495         // Activity action.
    496         if ( isset( $_POST['bp-activities-action'] ) )
    497             $activity->action = $_POST['bp-activities-action'];
    498 
    499         // Activity content.
    500         if ( isset( $_POST['bp-activities-content'] ) )
    501             $activity->content = $_POST['bp-activities-content'];
    502 
    503         // Activity primary link.
    504         if ( ! empty( $_POST['bp-activities-link'] ) )
    505             $activity->primary_link = $_POST['bp-activities-link'];
    506 
    507         // Activity user ID.
    508         if ( ! empty( $_POST['bp-activities-userid'] ) )
    509             $activity->user_id = (int) $_POST['bp-activities-userid'];
    510 
    511         // Activity item primary ID.
    512         if ( isset( $_POST['bp-activities-primaryid'] ) )
    513             $activity->item_id = (int) $_POST['bp-activities-primaryid'];
    514 
    515         // Activity item secondary ID.
    516         if ( isset( $_POST['bp-activities-secondaryid'] ) )
    517             $activity->secondary_item_id = (int) $_POST['bp-activities-secondaryid'];
    518 
    519         // Activity type.
    520         if ( ! empty( $_POST['bp-activities-type'] ) ) {
    521             $actions = bp_activity_admin_get_activity_actions();
    522 
    523             // Check that the new type is a registered activity type.
    524             if ( in_array( $_POST['bp-activities-type'], $actions ) ) {
    525                 $activity->type = $_POST['bp-activities-type'];
    526             }
    527         }
    528 
    529         // Activity timestamp.
    530         if ( ! empty( $_POST['aa'] ) && ! empty( $_POST['mm'] ) && ! empty( $_POST['jj'] ) && ! empty( $_POST['hh'] ) && ! empty( $_POST['mn'] ) && ! empty( $_POST['ss'] ) ) {
    531             $aa = $_POST['aa'];
    532             $mm = $_POST['mm'];
    533             $jj = $_POST['jj'];
    534             $hh = $_POST['hh'];
    535             $mn = $_POST['mn'];
    536             $ss = $_POST['ss'];
    537             $aa = ( $aa <= 0 ) ? date( 'Y' ) : $aa;
    538             $mm = ( $mm <= 0 ) ? date( 'n' ) : $mm;
    539             $jj = ( $jj > 31 ) ? 31 : $jj;
    540             $jj = ( $jj <= 0 ) ? date( 'j' ) : $jj;
    541             $hh = ( $hh > 23 ) ? $hh -24 : $hh;
    542             $mn = ( $mn > 59 ) ? $mn -60 : $mn;
    543             $ss = ( $ss > 59 ) ? $ss -60 : $ss;
    544 
    545             // Reconstruct the date into a timestamp.
    546             $gmt_date = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
    547 
    548             $activity->date_recorded = $gmt_date;
    549         }
    550 
    551         // Has the spam status has changed?
    552         if ( $new_spam_status != $prev_spam_status ) {
    553             if ( $new_spam_status )
    554                 bp_activity_mark_as_spam( $activity );
    555             else
    556                 bp_activity_mark_as_ham( $activity );
    557         }
    558 
    559         // Save.
    560         $result = $activity->save();
    561 
    562         // Clear the activity stream first page cache, in case this activity's timestamp was changed.
    563         wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
    564 
    565         // Check for any error during activity save.
    566         if ( false === $result )
    567             $error = $activity->id;
    568 
    569         /**
    570          * Fires before redirect so plugins can do something first on save action.
    571          *
    572          * @since 1.6.0
    573          *
    574          * @param array $value Array holding activity object and ID that holds error.
    575          */
    576         do_action_ref_array( 'bp_activity_admin_edit_after', array( &$activity, $error ) );
    577 
    578         // If an error occurred, pass back the activity ID that failed.
    579         if ( $error )
    580             $redirect_to = add_query_arg( 'error', (int) $error, $redirect_to );
    581         else
    582             $redirect_to = add_query_arg( 'updated', (int) $activity->id, $redirect_to );
    583 
    584         /**
    585          * Filters URL to redirect to after saving.
    586          *
    587          * @since 1.6.0
    588          *
    589          * @param string $redirect_to URL to redirect to.
    590          */
    591         wp_redirect( apply_filters( 'bp_activity_admin_edit_redirect', $redirect_to ) );
    592         exit;
    593 
    594 
    595     // If a referrer and a nonce is supplied, but no action, redirect back.
    596     } elseif ( ! empty( $_GET['_wp_http_referer'] ) ) {
    597         wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), stripslashes( $_SERVER['REQUEST_URI'] ) ) );
    598         exit;
    599     }
    600 }
    601 
    602 /**
    603  * Output the Activity component admin screens.
    604  *
    605  * @since 1.6.0
    606  */
    607 function bp_activity_admin() {
    608     // Decide whether to load the index or edit screen.
    609     $doaction = ! empty( $_REQUEST['action'] ) ? $_REQUEST['action'] : '';
    610 
    611     // Display the single activity edit screen.
    612     if ( 'edit' == $doaction && ! empty( $_GET['aid'] ) )
    613         bp_activity_admin_edit();
    614 
    615     // Otherwise, display the Activity index screen.
    616     else
    617         bp_activity_admin_index();
    618 }
    619 
    620 /**
    621  * Display the single activity edit screen.
    622  *
    623  * @since 1.6.0
    624  */
    625 function bp_activity_admin_edit() {
    626 
    627     // @todo: Check if user is allowed to edit activity items
    628     // if ( ! current_user_can( 'bp_edit_activity' ) )
    629     if ( ! is_super_admin() )
    630         die( '-1' );
    631 
    632     // Get the activity from the database.
    633     $activity = bp_activity_get( array(
    634         'in'               => ! empty( $_REQUEST['aid'] ) ? (int) $_REQUEST['aid'] : 0,
    635         'max'              => 1,
    636         'show_hidden'      => true,
    637         'spam'             => 'all',
    638         'display_comments' => 0
    639     ) );
    640 
    641     if ( ! empty( $activity['activities'][0] ) ) {
    642         $activity = $activity['activities'][0];
    643 
    644         // Workaround to use WP's touch_time() without duplicating that function.
    645         $GLOBALS['comment'] = new stdClass;
    646         $GLOBALS['comment']->comment_date = $activity->date_recorded;
    647     } else {
    648         $activity = '';
    649     }
    650 
    651     // Construct URL for form.
    652     $form_url = remove_query_arg( array( 'action', 'deleted', 'error', 'spammed', 'unspammed', ), $_SERVER['REQUEST_URI'] );
    653     $form_url = add_query_arg( 'action', 'save', $form_url );
    654 
    655     /**
    656      * Fires before activity edit form is displays so plugins can modify the activity.
    657      *
    658      * @since 1.6.0
    659      *
    660      * @param array $value Array holding single activity object that was passed by reference.
    661      */
    662     do_action_ref_array( 'bp_activity_admin_edit', array( &$activity ) ); ?>
    663 
    664     <div class="wrap">
    665         <h1><?php printf( __( 'Editing Activity (ID #%s)', 'buddypress' ), number_format_i18n( (int) $_REQUEST['aid'] ) ); ?></h1>
    666 
    667         <?php if ( ! empty( $activity ) ) : ?>
    668 
    669             <form action="<?php echo esc_url( $form_url ); ?>" id="bp-activities-edit-form" method="post">
    670                 <div id="poststuff">
    671 
    672                     <div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? '1' : '2'; ?>">
    673                         <div id="post-body-content">
    674                             <div id="postdiv">
    675                                 <div id="bp_activity_action" class="postbox">
    676                                     <h2><?php _e( 'Action', 'buddypress' ); ?></h2>
    677                                     <div class="inside">
    678                                         <?php wp_editor( stripslashes( $activity->action ), 'bp-activities-action', array( 'media_buttons' => false, 'textarea_rows' => 7, 'teeny' => true, 'quicktags' => array( 'buttons' => 'strong,em,link,block,del,ins,img,code,spell,close' ) ) ); ?>
    679                                     </div>
    680                                 </div>
    681 
    682                                 <div id="bp_activity_content" class="postbox">
    683                                     <h2><?php _e( 'Content', 'buddypress' ); ?></h2>
    684                                     <div class="inside">
    685                                         <?php wp_editor( stripslashes( $activity->content ), 'bp-activities-content', array( 'media_buttons' => false, 'teeny' => true, 'quicktags' => array( 'buttons' => 'strong,em,link,block,del,ins,img,code,spell,close' ) ) ); ?>
    686                                     </div>
    687                                 </div>
    688                             </div>
    689                         </div><!-- #post-body-content -->
    690 
    691                         <div id="postbox-container-1" class="postbox-container">
    692                             <?php do_meta_boxes( get_current_screen()->id, 'side', $activity ); ?>
    693                         </div>
    694 
    695                         <div id="postbox-container-2" class="postbox-container">
    696                             <?php do_meta_boxes( get_current_screen()->id, 'normal', $activity ); ?>
    697                             <?php do_meta_boxes( get_current_screen()->id, 'advanced', $activity ); ?>
    698                         </div>
    699                     </div><!-- #post-body -->
    700 
    701                 </div><!-- #poststuff -->
    702                 <?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
    703                 <?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
    704                 <?php wp_nonce_field( 'edit-activity_' . $activity->id ); ?>
    705             </form>
    706 
    707         <?php else : ?>
    708             <p>
    709                 <?php _e( 'No activity found with this ID.', 'buddypress' ); ?>
    710                 <a href="<?php echo esc_url( bp_get_admin_url( 'admin.php?page=bp-activity' ) ); ?>"><?php _e( 'Go back and try again.', 'buddypress' ); ?></a>
    711             </p>
    712         <?php endif; ?>
    713 
    714     </div><!-- .wrap -->
    715 
    716 <?php
    717 }
    718 
    719 /**
    720  * Status metabox for the Activity admin edit screen.
    721  *
    722  * @since 1.6.0
    723  *
    724  * @param object $item Activity item.
    725  */
    726 function bp_activity_admin_edit_metabox_status( $item ) {
    727 ?>
    728 
    729     <div class="submitbox" id="submitcomment">
    730 
    731         <div id="minor-publishing">
    732             <div id="minor-publishing-actions">
    733                 <div id="preview-action">
    734                     <a class="button preview" href="<?php echo esc_attr( bp_activity_get_permalink( $item->id, $item ) ); ?>" target="_blank"><?php _e( 'View Activity', 'buddypress' ); ?></a>
    735                 </div>
    736 
    737                 <div class="clear"></div>
    738             </div><!-- #minor-publishing-actions -->
    739 
    740             <div id="misc-publishing-actions">
    741                 <div class="misc-pub-section" id="comment-status-radio">
    742                     <label class="approved" for="activity-status-approved"><input type="radio" name="activity_status" id="activity-status-approved" value="ham" <?php checked( $item->is_spam, 0 ); ?>><?php _e( 'Approved', 'buddypress' ); ?></label><br />
    743                     <label class="spam" for="activity-status-spam"><input type="radio" name="activity_status" id="activity-status-spam" value="spam" <?php checked( $item->is_spam, 1 ); ?>><?php _e( 'Spam', 'buddypress' ); ?></label>
    744                 </div>
    745 
    746                 <div class="misc-pub-section curtime misc-pub-section-last">
    747                     <?php
    748                     // Translators: Publish box date format, see http://php.net/date.
    749                     $datef = __( 'M j, Y @ G:i', 'buddypress' );
    750                     $date  = date_i18n( $datef, strtotime( $item->date_recorded ) );
    751                     ?>
    752                     <span id="timestamp"><?php printf( __( 'Submitted on: %s', 'buddypress' ), '<strong>' . $date . '</strong>' ); ?></span>&nbsp;<a href="#edit_timestamp" class="edit-timestamp hide-if-no-js" tabindex='4'><?php _e( 'Edit', 'buddypress' ); ?></a>
    753 
    754                     <div id='timestampdiv' class='hide-if-js'>
    755                         <?php touch_time( 1, 0, 5 ); ?>
    756                     </div><!-- #timestampdiv -->
    757                 </div>
    758             </div> <!-- #misc-publishing-actions -->
    759 
    760             <div class="clear"></div>
    761         </div><!-- #minor-publishing -->
    762 
    763         <div id="major-publishing-actions">
    764             <div id="publishing-action">
    765                 <?php submit_button( __( 'Update', 'buddypress' ), 'primary', 'save', false ); ?>
    766             </div>
    767             <div class="clear"></div>
    768         </div><!-- #major-publishing-actions -->
    769 
    770     </div><!-- #submitcomment -->
    771 
    772 <?php
    773 }
    774 
    775 /**
    776  * Primary link metabox for the Activity admin edit screen.
    777  *
    778  * @since 1.6.0
    779  *
    780  * @param object $item Activity item.
    781  */
    782 function bp_activity_admin_edit_metabox_link( $item ) {
    783 ?>
    784 
    785     <label class="screen-reader-text" for="bp-activities-link"><?php _e( 'Link', 'buddypress' ); ?></label>
    786     <input type="url" name="bp-activities-link" id="bp-activities-link" value="<?php echo esc_url( $item->primary_link ); ?>" aria-describedby="bp-activities-link-description" />
    787     <p id="bp-activities-link-description"><?php _e( 'Activity generated by posts and comments, forum topics and replies, and some plugins, uses the link field for a permalink back to the content item.', 'buddypress' ); ?></p>
    788 
    789 <?php
    790 }
    791 
    792 /**
    793  * User ID metabox for the Activity admin edit screen.
    794  *
    795  * @since 1.6.0
    796  *
    797  * @param object $item Activity item.
    798  */
    799 function bp_activity_admin_edit_metabox_userid( $item ) {
    800 ?>
    801 
    802     <label class="screen-reader-text" for="bp-activities-userid"><?php _e( 'Author ID', 'buddypress' ); ?></label>
    803     <input type="number" name="bp-activities-userid" id="bp-activities-userid" value="<?php echo esc_attr( $item->user_id ); ?>" min="1" />
    804 
    805 <?php
    806 }
    807 
    808 /**
    809  * Get flattened array of all registered activity actions.
    810  *
    811  * Format is [activity_type] => Pretty name for activity type.
    812  *
    813  * @since 2.0.0
    814  *
    815  * @return array $actions
    816  */
    817 function bp_activity_admin_get_activity_actions() {
    818     $actions  = array();
    819 
    820     // Walk through the registered actions, and build an array of actions/values.
    821     foreach ( bp_activity_get_actions() as $action ) {
    822         $action = array_values( (array) $action );
    823 
    824         for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ ) {
    825             $actions[ $action[$i]['key'] ] = $action[$i]['value'];
    826         }
    827     }
    828 
    829     // This was a mis-named activity type from before BP 1.6.
    830     unset( $actions['friends_register_activity_action'] );
    831 
    832     // Sort array by the human-readable value.
    833     natsort( $actions );
    834 
    835     return $actions;
    836 }
    837 
    838 /**
    839  * Activity type metabox for the Activity admin edit screen.
    840  *
    841  * @since 1.6.0
    842  *
    843  * @param object $item Activity item.
    844  */
    845 function bp_activity_admin_edit_metabox_type( $item ) {
    846     $bp = buddypress();
    847 
    848     $actions  = array();
    849     $selected = $item->type;
    850 
    851     // Walk through the registered actions, and build an array of actions/values.
    852     foreach ( bp_activity_get_actions() as $action ) {
    853         $action = array_values( (array) $action );
    854 
    855         for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ )
    856             $actions[ $action[$i]['key'] ] = $action[$i]['value'];
    857     }
    858 
    859     // This was a mis-named activity type from before BP 1.6.
    860     unset( $actions['friends_register_activity_action'] );
    861 
    862     // Sort array by the human-readable value.
    863     natsort( $actions );
    864 
    865     /*
    866      * If the activity type is not registered properly (eg, a plugin has
    867      * not called bp_activity_set_action()), add the raw type to the end
    868      * of the list.
    869      */
    870     if ( ! isset( $actions[ $selected ] ) ) {
    871         _doing_it_wrong( __FUNCTION__, sprintf( __( 'This activity item has a type (%s) that is not registered using bp_activity_set_action(), so no label is available.', 'buddypress' ), $selected ), '2.0.0' );
    872         $actions[ $selected ] = $selected;
    873     }
    874 
    875     ?>
    876 
    877     <label for="bp-activities-type" class="screen-reader-text"><?php esc_html_e( 'Select activity type', 'buddypress' ); ?></label>
    878     <select name="bp-activities-type" id="bp-activities-type">
    879         <?php foreach ( $actions as $k => $v ) : ?>
    880             <option value="<?php echo esc_attr( $k ); ?>" <?php selected( $k,  $selected ); ?>><?php echo esc_html( $v ); ?></option>
    881         <?php endforeach; ?>
    882     </select>
    883 
    884 <?php
    885 }
    886 
    887 /**
    888  * Primary item ID/Secondary item ID metabox for the Activity admin edit screen.
    889  *
    890  * @since 1.6.0
    891  *
    892  * @param object $item Activity item.
    893  */
    894 function bp_activity_admin_edit_metabox_itemids( $item ) {
    895 ?>
    896 
    897     <label for="bp-activities-primaryid"><?php _e( 'Primary Item ID', 'buddypress' ); ?></label>
    898     <input type="number" name="bp-activities-primaryid" id="bp-activities-primaryid" value="<?php echo esc_attr( $item->item_id ); ?>" min="0" />
    899     <br />
    900 
    901     <label for="bp-activities-secondaryid"><?php _e( 'Secondary Item ID', 'buddypress' ); ?></label>
    902     <input type="number" name="bp-activities-secondaryid" id="bp-activities-secondaryid" value="<?php echo esc_attr( $item->secondary_item_id ); ?>" min="0" />
    903 
    904     <p><?php _e( 'These identify the object that created this activity. For example, the fields could reference a pair of site and comment IDs.', 'buddypress' ); ?></p>
    905 
    906 <?php
    907 }
    908 
    909 /**
    910  * Display the Activity admin index screen, which contains a list of all the activities.
    911  *
    912  * @since 1.6.0
    913  *
    914  * @global BP_Activity_List_Table $bp_activity_list_table Activity screen list table.
    915  * @global string                 $plugin_page            The current plugin page.
    916  */
    917 function bp_activity_admin_index() {
    918     global $bp_activity_list_table, $plugin_page;
    919 
    920     $messages = array();
    921 
    922     // If the user has just made a change to an activity item, build status messages.
    923     if ( ! empty( $_REQUEST['deleted'] ) || ! empty( $_REQUEST['spammed'] ) || ! empty( $_REQUEST['unspammed'] ) || ! empty( $_REQUEST['error'] ) || ! empty( $_REQUEST['updated'] ) ) {
    924         $deleted   = ! empty( $_REQUEST['deleted']   ) ? (int) $_REQUEST['deleted']   : 0;
    925         $errors    = ! empty( $_REQUEST['error']     ) ? $_REQUEST['error']           : '';
    926         $spammed   = ! empty( $_REQUEST['spammed']   ) ? (int) $_REQUEST['spammed']   : 0;
    927         $unspammed = ! empty( $_REQUEST['unspammed'] ) ? (int) $_REQUEST['unspammed'] : 0;
    928         $updated   = ! empty( $_REQUEST['updated']   ) ? (int) $_REQUEST['updated']   : 0;
    929 
    930         $errors = array_map( 'absint', explode( ',', $errors ) );
    931 
    932         // Make sure we don't get any empty values in $errors.
    933         for ( $i = 0, $errors_count = count( $errors ); $i < $errors_count; $i++ ) {
    934             if ( 0 === $errors[$i] ) {
    935                 unset( $errors[$i] );
    936             }
    937         }
    938 
    939         // Reindex array.
    940         $errors = array_values( $errors );
    941 
    942         if ( $deleted > 0 )
    943             $messages[] = sprintf( _n( '%s activity item has been permanently deleted.', '%s activity items have been permanently deleted.', $deleted, 'buddypress' ), number_format_i18n( $deleted ) );
    944 
    945         if ( ! empty( $errors ) ) {
    946             if ( 1 == count( $errors ) ) {
    947                 $messages[] = sprintf( __( 'An error occurred when trying to update activity ID #%s.', 'buddypress' ), number_format_i18n( $errors[0] ) );
    948 
    949             } else {
    950                 $error_msg  = __( 'Errors occurred when trying to update these activity items:', 'buddypress' );
    951                 $error_msg .= '<ul class="activity-errors">';
    952 
    953                 // Display each error as a list item.
    954                 foreach ( $errors as $error ) {
    955                     // Translators: This is a bulleted list of item IDs.
    956                     $error_msg .= '<li>' . sprintf( __( '#%s', 'buddypress' ), number_format_i18n( $error ) ) . '</li>';
    957                 }
    958 
    959                 $error_msg  .= '</ul>';
    960                 $messages[] = $error_msg;
    961             }
    962         }
    963 
    964         if ( $spammed > 0 )
    965             $messages[] = sprintf( _n( '%s activity item has been successfully spammed.', '%s activity items have been successfully spammed.', $spammed, 'buddypress' ), number_format_i18n( $spammed ) );
    966 
    967         if ( $unspammed > 0 )
    968             $messages[] = sprintf( _n( '%s activity item has been successfully unspammed.', '%s activity items have been successfully unspammed.', $unspammed, 'buddypress' ), number_format_i18n( $unspammed ) );
    969 
    970         if ( $updated > 0 )
    971             $messages[] = __( 'The activity item has been updated successfully.', 'buddypress' );
    972     }
    973 
    974     // Prepare the activity items for display.
    975     $bp_activity_list_table->prepare_items();
    976 
    977     /**
    978      * Fires before edit form is displayed so plugins can modify the activity messages.
    979      *
    980      * @since 1.6.0
    981      *
    982      * @param array $messages Array of messages to display at top of page.
    983      */
    984     do_action( 'bp_activity_admin_index', $messages ); ?>
    985 
    986     <div class="wrap">
    987         <h1>
    988             <?php if ( !empty( $_REQUEST['aid'] ) ) : ?>
    989                 <?php printf( __( 'Activity related to ID #%s', 'buddypress' ), number_format_i18n( (int) $_REQUEST['aid'] ) ); ?>
    990             <?php else : ?>
    991                 <?php _ex( 'Activity', 'Admin SWA page', 'buddypress' ); ?>
    992             <?php endif; ?>
    993 
    994             <?php if ( !empty( $_REQUEST['s'] ) ) : ?>
    995                 <span class="subtitle"><?php printf( __( 'Search results for &#8220;%s&#8221;', 'buddypress' ), wp_html_excerpt( esc_html( stripslashes( $_REQUEST['s'] ) ), 50 ) ); ?></span>
    996             <?php endif; ?>
    997         </h1>
    998 
    999         <?php // If the user has just made a change to an activity item, display the status messages. ?>
    1000         <?php if ( !empty( $messages ) ) : ?>
    1001             <div id="moderated" class="<?php echo ( ! empty( $_REQUEST['error'] ) ) ? 'error' : 'updated'; ?>"><p><?php echo implode( "<br/>\n", $messages ); ?></p></div>
    1002         <?php endif; ?>
    1003 
    1004         <?php // Display each activity on its own row. ?>
    1005         <?php $bp_activity_list_table->views(); ?>
    1006 
    1007         <form id="bp-activities-form" action="" method="get">
    1008             <?php $bp_activity_list_table->search_box( __( 'Search all Activity', 'buddypress' ), 'bp-activity' ); ?>
    1009             <input type="hidden" name="page" value="<?php echo esc_attr( $plugin_page ); ?>" />
    1010             <?php $bp_activity_list_table->display(); ?>
    1011         </form>
    1012 
    1013         <?php // This markup is used for the reply form. ?>
    1014         <table style="display: none;">
    1015             <tr id="bp-activities-container" style="display: none;">
    1016                 <td colspan="4">
    1017                     <form method="get" action="">
    1018 
    1019                         <h3 id="bp-replyhead"><?php _e( 'Reply to Activity', 'buddypress' ); ?></h3>
    1020                         <?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, ) ); ?>
    1021 
    1022                         <p id="bp-replysubmit" class="submit">
    1023                             <a href="#" class="cancel button-secondary alignleft"><?php _e( 'Cancel', 'buddypress' ); ?></a>
    1024                             <a href="#" class="save button-primary alignright"><?php _e( 'Reply', 'buddypress' ); ?></a>
    1025 
    1026                             <img class="waiting" style="display:none;" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
    1027                             <span class="error" style="display:none;"></span>
    1028                             <br class="clear" />
    1029                         </p>
    1030 
    1031                         <?php wp_nonce_field( 'bp-activity-admin-reply', '_ajax_nonce-bp-activity-admin-reply', false ); ?>
    1032 
    1033                     </form>
    1034                 </td>
    1035             </tr>
    1036         </table>
    1037     </div>
    1038 
    1039 <?php
    1040 }
    104115
    104216/**
Note: See TracChangeset for help on using the changeset viewer.