Making WordPress.org

Changeset 2083


Ignore:
Timestamp:
11/16/2015 01:16:20 PM (9 years ago)
Author:
kovshenin
Message:

WordCamp.org: Add some basic logging to our payment requests plugin.

Location:
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments
Files:
1 added
3 edited

Legend:

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

    r2060 r2083  
    2020        add_action( 'save_post',              array( $this, 'save_payment' ), 10, 2 );
    2121        add_filter( 'map_meta_cap',           array( $this, 'modify_capabilities' ), 10, 4 );
     22        add_action( 'transition_post_status', array( $this, 'transition_post_status' ), 10, 3 );
    2223
    2324        // Columns
     
    165166            'high'
    166167        );
     168
     169        add_meta_box( 'wcp_log', __( 'Log', 'wordcamporg' ), array( $this, 'render_log_metabox' ),
     170            self::POST_TYPE, 'normal', 'high' );
    167171    }
    168172
     
    248252
    249253        require_once( dirname( __DIR__ ) . '/views/payment-request/metabox-files.php' );
     254    }
     255
     256    /**
     257     * Render the Log metabox
     258     *
     259     * @param WP_Post $post
     260     */
     261    public function render_log_metabox( $post ) {
     262        $log = get_post_meta( $post->ID, '_wcp_log', true );
     263        if ( empty( $log ) )
     264            $log = '[]';
     265
     266        $log = json_decode( $log, true );
     267
     268        // I wish I had a spaceship.
     269        uasort( $log, function( $a, $b ) {
     270            if ( $b['timestamp'] == $a )
     271                return 0;
     272
     273            return ( $a['timestamp'] < $b['timestamp'] ) ? -1 : 1;
     274        });
     275
     276        require_once( dirname( __DIR__ ) . '/views/payment-request/metabox-log.php' );
    250277    }
    251278
     
    658685                $post_data['post_status'] = 'incomplete';
    659686                $this->notify_requester_request_incomplete( $post_data_raw['ID'], $post_data, $post_data_raw );
     687
     688                update_post_meta( $post_data_raw['ID'], '_wcp_incomplete_notes', sanitize_text_field( $post_data_raw['wcp_mark_incomplete_notes'] ) );
    660689            } else {
    661690                $previous_status          = $post_data['post_status'];
     
    901930    }
    902931
     932    public function transition_post_status( $new, $old, $post ) {
     933        if ( $post->post_type != self::POST_TYPE )
     934            return;
     935
     936        $user = get_user_by( 'id', get_current_user_id() );
     937        if ( $new == 'auto-draft' )
     938            return;
     939
     940        if ( $new == 'incomplete' && $old != 'incomplete' ) {
     941            $incomplete_text = get_post_meta( $post->ID, '_wcp_incomplete_notes', true );
     942            $incomplete_text = preg_replace( '#\.$#', '', $incomplete_text ); // trailing-undot-it.
     943            WordCamp_Payments::log( $post->ID, sprintf( 'Marked as incomplete by %s: %s.', $user->display_name, $incomplete_text ), array(
     944                'user_id' => $user->ID,
     945                'action' => 'marked-incomplete',
     946                'reason' => 'maybe notes',
     947            ) );
     948        } elseif ( $new == 'paid' && $old != 'paid' ) {
     949            WordCamp_Payments::log( $post->ID, sprintf( 'Marked as paid by %s.', $user->display_name ), array(
     950                'user_id' => $user->ID,
     951                'action' => 'marked-paid',
     952            ) );
     953        } elseif ( $old == 'auto-draft' && $new != 'auto-draft' ) {
     954            WordCamp_Payments::log( $post->ID, sprintf( 'Request created by %s.', $user->display_name ), array(
     955                'user_id' => $user->ID,
     956                'action' => 'updated',
     957            ) );
     958        } else {
     959            WordCamp_Payments::log( $post->ID, sprintf( 'Request updated by %s.', $user->display_name ), array(
     960                'user_id' => $user->ID,
     961                'action' => 'updated',
     962            ) );
     963        }
     964    }
     965
    903966    /**
    904967     * Attach unattached files to the payment request post
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/classes/wordcamp-payments.php

    r1939 r2083  
    1111     */
    1212    public function __construct() {
    13         add_action( 'admin_enqueue_scripts',  array( $this, 'enqueue_assets' ) );
     13        add_action( 'admin_enqueue_scripts',  array( $this, 'enqueue_assets' ), 11 );
    1414    }
    1515
     
    3939        );
    4040
     41        // Let's still include our .css file even if these are unavailable.
     42        $soft_deps = array( 'jquery-ui', 'wp-datepicker-skins' );
     43        foreach ( $soft_deps as $key => $handle )
     44            if ( ! wp_style_is( $handle, 'registered' ) )
     45                unset( $soft_deps[ $key ] );
     46
    4147        wp_register_style(
    4248            'wordcamp-payments',
    4349            plugins_url( 'css/wordcamp-payments.css', __DIR__ ),
    44             array( 'jquery-ui', 'wp-datepicker-skins' ),
     50            $soft_deps,
    4551            self::VERSION
    4652        );
     
    6874        }
    6975    }
     76
     77    /**
     78     * Log something with a payment request.
     79     *
     80     * @param int $post_id The payment requset ID.
     81     * @param string $message A log message.
     82     * @param array $data Optional data.
     83     */
     84    public static function log( $post_id, $message, $data = array() ) {
     85        global $wpdb;
     86
     87        $entry = array(
     88            'timestamp' => time(),
     89            'message' => $message,
     90            'data' => $data,
     91        );
     92
     93        $log = get_post_meta( $post_id, '_wcp_log', true );
     94        if ( empty( $log ) )
     95            $log = '[]';
     96
     97        $log = json_decode( $log, true );
     98        $log[] = $entry;
     99        $log = json_encode( $log );
     100
     101        update_post_meta( $post_id, '_wcp_log', wp_slash( $log ) );
     102    }
    70103}
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/css/wordcamp-payments.css

    r995 r2083  
    2727    margin-bottom: 1em;
    2828}
     29
     30#wcp_log .inside {
     31    margin: 0;
     32    padding: 0;
     33}
     34
     35#wcp_log .wcp-request-log {
     36    border-collapse: collapse;
     37    width: 100%;
     38}
     39
     40#wcp_log .wcp-request-log td {
     41    padding: 8px;
     42}
     43
     44#wcp_log td.timestamp {
     45    width: 130px;
     46}
Note: See TracChangeset for help on using the changeset viewer.