Making WordPress.org


Ignore:
Timestamp:
08/28/2016 01:35:13 PM (9 years ago)
Author:
kovshenin
Message:

WordCamp Budgets: Add the Log metabox/functionality to reimbursement requests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/includes/reimbursement-request.php

    r3800 r3868  
    2121add_action( 'save_post',              __NAMESPACE__ . '\save_request',                     10, 2 );
    2222add_action( 'transition_post_status', __NAMESPACE__ . '\notify_organizer_request_updated', 10, 3 );
     23add_action( 'transition_post_status', __NAMESPACE__ . '\transition_post_status',           10, 3 );
    2324
    2425// Miscellaneous
     
    201202        'high'
    202203    );
     204
     205    add_meta_box(
     206        'wcp_log',
     207        __( 'Log', 'wordcamporg' ),
     208        __NAMESPACE__ . '\render_log_metabox',
     209        POST_TYPE,
     210        'normal',
     211        'high'
     212    );
    203213}
    204214
     
    474484        add_action( 'save_post', __NAMESPACE__ . '\save_request', 10, 2 );
    475485    }
     486
     487    $user = get_user_by( 'id', get_current_user_id() );
     488
     489    // Look at post status transitions.
     490    foreach ( _transition_post_status() as $data ) {
     491        list( $new, $old, $transition_post ) = $data;
     492
     493        // Transitioning a different post.
     494        if ( $transition_post->ID != $post->ID )
     495            continue;
     496
     497        if ( $new == 'incomplete' || $new == 'wcb-incomplete' ) {
     498            $incomplete_text = get_post_meta( $post->ID, '_wcp_incomplete_notes', true );
     499            $incomplete_text = preg_replace( '#\.$#', '', $incomplete_text ); // trailing-undot-it.
     500            \WordCamp_Budgets::log( $post->ID, $user->ID, sprintf( 'Marked as incomplete: %s', $incomplete_text ), array(
     501                'action' => 'marked-incomplete',
     502                'reason' => 'maybe notes',
     503            ) );
     504
     505            \WordCamp_Budgets::log( $post->ID, $user->ID, 'Incomplete notification e-mail sent.', array(
     506                'action' => 'incomplete-notification-sent',
     507            ) );
     508
     509        } elseif ( $new == 'paid' || $new == 'wcb-paid' ) {
     510            \WordCamp_Budgets::log( $post->ID, $user->ID, 'Marked as paid', array(
     511                'action' => 'marked-paid',
     512            ) );
     513
     514            \WordCamp_Budgets::log( $post->ID, $user->ID, 'Paid notification e-mail sent.', array(
     515                'action' => 'paid-notification-sent',
     516            ) );
     517
     518        } elseif ( $old == 'auto-draft' ) {
     519            \WordCamp_Budgets::log( $post->ID, $user->ID, 'Request created', array(
     520                'action' => 'updated',
     521            ) );
     522        }
     523    }
     524
     525    \WordCamp_Budgets::log( $post->ID, $user->ID, 'Request updated', array(
     526        'action' => 'updated',
     527    ) );
     528}
     529
     530/**
     531 * Add log entries when the post status changes
     532 *
     533 * @param string  $new
     534 * @param string  $old
     535 * @param WP_Post $post
     536 */
     537function transition_post_status( $new, $old, $post ) {
     538    if ( $post->post_type != POST_TYPE )
     539        return;
     540
     541    if ( $new == 'auto-draft' || $new == $old )
     542        return;
     543
     544    // Move logging to save_post because transitions are fired before save_post.
     545    _transition_post_status( array( $new, $old, $post ) );
     546}
     547
     548/**
     549 * A wrapper around a static variable to hold caught transitions.
     550 *
     551 * @param null|array $set Pass null to retrieve transitions, or an array of transition data to append them.
     552 *
     553 * @return array Transitions.
     554 */
     555function _transition_post_status( $set = null ) {
     556    static $transitions;
     557
     558    if ( ! isset( $transitions ) )
     559        $transitions = array();
     560
     561    if ( is_null( $set ) )
     562        return $transitions;
     563
     564    $transitions[] = $set;
     565    return $transitions;
     566}
     567
     568/**
     569 * Render the Log metabox
     570 *
     571 * @param WP_Post $post
     572 */
     573function render_log_metabox( $post ) {
     574    $log = get_post_meta( $post->ID, '_wcp_log', true );
     575    if ( empty( $log ) )
     576        $log = '[]';
     577
     578    $log = json_decode( $log, true );
     579
     580    // I wish I had a spaceship.
     581    uasort( $log, function( $a, $b ) {
     582        if ( $b['timestamp'] == $a )
     583            return 0;
     584
     585        return ( $a['timestamp'] > $b['timestamp'] ) ? -1 : 1;
     586    });
     587
     588    require_once( dirname( __DIR__ ) . '/views/reimbursement-request/metabox-log.php' );
    476589}
    477590
     
    588701    update_post_meta( $post->ID, '_wcbrr_notes', $notes );
    589702    notify_parties_of_new_note( $post, $new_note );
     703
     704    \WordCamp_Budgets::log( $post->ID, get_current_user_id(), sprintf( 'Note: %s', $new_note_message ), array(
     705        'action' => 'note-added',
     706    ) );
    590707}
    591708
Note: See TracChangeset for help on using the changeset viewer.