Making WordPress.org

Changeset 2770


Ignore:
Timestamp:
03/21/2016 02:09:51 PM (10 years ago)
Author:
kovshenin
Message:

WordCamp Budgets: A couple of improvements to payment requests and reimbursements.

  • Add an updated timestamp and index column for reimbursements.
  • Move import form and handler to the Import/Export network admin menu.
Location:
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/payment-requests-dashboard.php

    r2748 r2770  
    44        public static $list_table;
    55        public static $db_version = 7;
    6         public static $import_results = null;
    76
    87        /**
     
    1918                add_action( 'network_admin_menu', array( __CLASS__, 'network_admin_menu' ) );
    2019                add_action( 'init', array( __CLASS__, 'upgrade' ) );
    21                 add_action( 'init', array( __CLASS__, 'process_import_request' ) );
    2220
    2321                // Diff-based updates to the index.
     
    248246                        <h3 class="nav-tab-wrapper"><?php self::render_dashboard_tabs(); ?></h3>
    249247
    250                         <?php
    251                                 if ( 'import' == self::get_current_tab() ) {
    252                                         self::render_import_tab();
    253                                 }
    254                                 else {
    255                                         self::render_table_tabs();
    256                                 }
    257                         ?>
     248                        <?php self::render_table_tabs(); ?>
    258249
    259250                </div> <!-- /wrap -->
     
    285276
    286277        /**
    287          * Renders the import tab.
    288          */
    289         public static function render_import_tab() {
    290                 ?>
    291                 <?php if ( isset( self::$import_results ) ) : ?>
    292                 <h2>Import Results</h2>
    293                 <pre><?php echo esc_html( print_r( self::$import_results, true ) ); ?></pre>
    294                 <?php endif; ?>
    295 
    296                 <p>Import payment results from JPM reports CSV.</p>
    297                 <form method="post" enctype="multipart/form-data">
    298                         <?php wp_nonce_field( 'import', 'wcpn_request_import' ); ?>
    299                         <label>Import File:</label>
    300                         <input type="file" name="wcpn_import_file" />
    301                         <?php submit_button( 'Import' ); ?>
    302                 </form>
    303                 <?php
    304         }
    305 
    306         /**
    307          * Process a payments import, runs during init.
    308          */
    309         public static function process_import_request() {
    310                 if ( empty( $_POST['submit'] ) || 'import' != self::get_current_tab() ) {
    311                         return;
    312                 }
    313 
    314                 if ( ! current_user_can( 'manage_network' ) || ! check_admin_referer( 'import', 'wcpn_request_import' ) ) {
    315                         return;
    316                 }
    317 
    318                 if ( empty( $_FILES['wcpn_import_file'] ) ) {
    319                         wp_die( 'Please select a file to import.' );
    320                 }
    321 
    322                 $file = $_FILES['wcpn_import_file'];
    323                 if ( $file['type'] != 'text/csv' ) {
    324                         wp_die( 'Please upload a text/csv file.' );
    325                 }
    326 
    327                 if ( $file['size'] < 1 ) {
    328                         wp_die( 'Please upload a file that is not empty.' );
    329                 }
    330 
    331                 if ( $file['error'] ) {
    332                         wp_die( 'Some other error has occurred. Sorry.' );
    333                 }
    334 
    335                 $handle = fopen( $file['tmp_name'], 'r' );
    336                 $count = 0;
    337                 $header = array();
    338                 $results = array();
    339 
    340                 while ( ( $line = fgetcsv( $handle ) ) !== false ) {
    341                         // Skip first line.
    342                         if ( ++$count == 1 ) {
    343                                 continue;
    344                         }
    345 
    346                         $entry = array(
    347                                 'type' => strtolower( $line[11] ),
    348                                 'status' => strtolower( $line[7] ),
    349                                 'amount' => round( floatval( $line[13] ), 2 ),
    350                                 'currency' => strtoupper( $line[14] ),
    351                                 'blog_id' => null,
    352                                 'post_id' => null,
    353                                 'processed' => false,
    354                                 'data' => null,
    355                         );
    356 
    357                         switch ( $entry['type'] ) {
    358                                 case 'wire':
    359                                         if ( ! empty( $line[44] ) && preg_match( '#^wcb-([0-9]+)-([0-9]+)$#', $line[44], $matches ) ) {
    360                                                 $entry['blog_id'] = $matches[1];
    361                                                 $entry['post_id'] = $matches[2];
    362                                         }
    363                                         break;
    364                                 case 'ach':
    365                                         if ( ! empty( $line[91] ) && preg_match( '#^([0-9]+)-([0-9]+)$#', $line[91], $matches ) ) {
    366                                                 $entry['blog_id'] = $matches[1];
    367                                                 $entry['post_id'] = $matches[2];
    368                                         }
    369                                         break;
    370                         }
    371 
    372                         if ( empty( $entry['blog_id'] ) || empty( $entry['post_id'] ) ) {
    373                                 $results[] = $entry;
    374                                 continue;
    375                         }
    376 
    377                         // Don't consume memory.
    378                         wp_suspend_cache_addition( true );
    379                         switch_to_blog( $entry['blog_id'] );
    380 
    381                         $results[] = self::_import_process_entry( $entry );
    382 
    383                         restore_current_blog();
    384                         wp_suspend_cache_addition( false );
    385                 }
    386 
    387                 fclose( $handle );
    388                 self::$import_results = $results;
    389         }
    390 
    391         /**
    392          * Process a single import entry.
    393          *
    394          * Runs in a switch_to_blog() context.
    395          *
    396          * @param $entry Array
    397          * @return Array
    398          */
    399         private static function _import_process_entry( $entry ) {
    400                 $post = get_post( $entry['post_id'] );
    401                 if ( ! $post || $post->post_type != 'wcp_payment_request' ) {
    402                         $entry['data'] = 'Post not found or post type mismatch';
    403                         return $entry;
    404                 }
    405 
    406                 if ( $entry['currency'] != get_post_meta( $post->ID, '_camppayments_currency', true ) ) {
    407                         $entry['data'] = 'Currency mismatch';
    408                         return $entry;
    409                 }
    410 
    411                 $amount_orig = floatval( get_post_meta( $post->ID, '_camppayments_payment_amount', true ) );
    412                 $amount_orig = round( $amount_orig, 2 );
    413                 if ( (string) $entry['amount'] != (string) $amount_orig ) {
    414                         $entry['data'] = 'Payment amount mismatch';
    415                         return $entry;
    416                 }
    417 
    418                 // @todo Do some magic here.
    419 
    420                 // All good.
    421                 $entry['processed'] = true;
    422                 return $entry;
    423         }
    424 
    425         /**
    426278         * Loads and initializes the list table object.
    427279         */
     
    447299                        'cancelled-failed',
    448300                        'incomplete',
    449 
    450                         'import',
    451301                );
    452302
     
    472322                        'cancelled-failed' => __( 'Cancelled/Failed', 'wordcamporg' ),
    473323                        'incomplete'       => __( 'Incomplete', 'wordcamporg' ),
    474                         'import'           => __( 'Import', 'wordcamporg' ),
    475324                );
    476325
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/reimbursement-requests-dashboard.php

    r2748 r2770  
    55defined( 'WPINC' ) or die();
    66
    7 const LATEST_DATABASE_VERSION = 2;
     7const LATEST_DATABASE_VERSION = 3;
    88
    99if ( is_network_admin() ) {
     
    154154                        date_requested int( 11 )        unsigned NOT NULL default '0',
    155155                        date_paid      int( 11 )        unsigned NOT NULL default '0',
     156                        date_updated   int( 11 )        unsigned NOT NULL default '0',
    156157                        request_title  varchar( 75 )             NOT NULL default '',
    157158                        status         varchar( 30 )             NOT NULL default '',
     
    198199
    199200        // Update the timestamp and logs.
    200         update_post_meta( $request_id, '_wcb_updated_timestamp', time() );
     201        update_post_meta( $request->ID, '_wcb_updated_timestamp', time() );
    201202
    202203        // Back-compat.
     
    227228                'date_requested' => strtotime( $request->post_date_gmt ),
    228229                'date_paid'      => get_post_meta( $request_id, '_wcbrr_date_paid', true ),
     230                'date_updated'   => absint( get_post_meta( $request->ID, '_wcb_updated_timestamp', time() ) ),
    229231                'request_title'  => $request->post_title,
    230232                'status'         => $request->post_status,
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/wordcamp-budgets-dashboard.php

    r2747 r2770  
    1616add_action( 'admin_init', __NAMESPACE__ . '\process_action_approve', 11 );
    1717add_action( 'admin_init', __NAMESPACE__ . '\process_action_set_pending_payment', 11 );
     18
     19add_action( 'admin_init', __NAMESPACE__ . '\process_import_request', 11 );
    1820
    1921/**
     
    129131 */
    130132function render_import_tab() {
    131         echo '<p>Move along, nothing to see here.</p>';
     133        ?>
     134        <?php if ( isset( WCB_Import_Results::$data ) ) : ?>
     135        <h2>Import Results</h2>
     136        <pre><?php echo esc_html( print_r( WCB_Import_Results::$data, true ) ); ?></pre>
     137        <?php endif; ?>
     138
     139        <p>Import payment results from JPM reports CSV.</p>
     140        <form method="post" enctype="multipart/form-data">
     141                <input type="hidden" name="wcpn-request-import" value="1" />
     142                <?php wp_nonce_field( 'import', 'wcpn-request-import' ); ?>
     143                <label>Import File:</label>
     144                <input type="file" name="wcpn-import-file" />
     145                <?php submit_button( 'Import' ); ?>
     146        </form>
     147        <?php
    132148}
    133149
     
    613629        add_settings_error( 'wcb-dashboard', 'success', 'Success! Request has been marked as Pending Payment.', 'updated' );
    614630}
     631
     632/**
     633 * Process a payments import, runs during init.
     634 */
     635function process_import_request() {
     636        if ( empty( $_POST['wcpn-request-import'] ) ) {
     637                return;
     638        }
     639
     640        if ( ! current_user_can( 'manage_network' ) || ! check_admin_referer( 'import', 'wcpn-request-import' ) ) {
     641                return;
     642        }
     643
     644        if ( empty( $_FILES['wcpn-import-file'] ) ) {
     645                wp_die( 'Please select a file to import.' );
     646        }
     647
     648        $file = $_FILES['wcpn-import-file'];
     649        if ( $file['type'] != 'text/csv' ) {
     650                wp_die( 'Please upload a text/csv file.' );
     651        }
     652
     653        if ( $file['size'] < 1 ) {
     654                wp_die( 'Please upload a file that is not empty.' );
     655        }
     656
     657        if ( $file['error'] ) {
     658                wp_die( 'Some other error has occurred. Sorry.' );
     659        }
     660
     661        $handle = fopen( $file['tmp_name'], 'r' );
     662        $count = 0;
     663        $header = array();
     664        $results = array();
     665
     666        while ( ( $line = fgetcsv( $handle ) ) !== false ) {
     667                // Skip first line.
     668                if ( ++$count == 1 ) {
     669                        continue;
     670                }
     671
     672                $entry = array(
     673                        'type' => strtolower( $line[11] ),
     674                        'status' => strtolower( $line[7] ),
     675                        'amount' => round( floatval( $line[13] ), 2 ),
     676                        'currency' => strtoupper( $line[14] ),
     677                        'blog_id' => null,
     678                        'post_id' => null,
     679                        'processed' => false,
     680                        'data' => null,
     681                );
     682
     683                switch ( $entry['type'] ) {
     684                        case 'wire':
     685                                if ( ! empty( $line[44] ) && preg_match( '#^wcb-([0-9]+)-([0-9]+)$#', $line[44], $matches ) ) {
     686                                        $entry['blog_id'] = $matches[1];
     687                                        $entry['post_id'] = $matches[2];
     688                                }
     689                                break;
     690                        case 'ach':
     691                                if ( ! empty( $line[91] ) && preg_match( '#^([0-9]+)-([0-9]+)$#', $line[91], $matches ) ) {
     692                                        $entry['blog_id'] = $matches[1];
     693                                        $entry['post_id'] = $matches[2];
     694                                }
     695                                break;
     696                }
     697
     698                if ( empty( $entry['blog_id'] ) || empty( $entry['post_id'] ) ) {
     699                        $results[] = $entry;
     700                        continue;
     701                }
     702
     703                // Don't consume memory.
     704                wp_suspend_cache_addition( true );
     705                switch_to_blog( $entry['blog_id'] );
     706
     707                $results[] = _import_process_entry( $entry );
     708
     709                restore_current_blog();
     710                wp_suspend_cache_addition( false );
     711        }
     712
     713        fclose( $handle );
     714
     715        WCB_Import_Results::$data = $results;
     716}
     717
     718/**
     719 * Process a single import entry.
     720 *
     721 * Runs in a switch_to_blog() context.
     722 *
     723 * @param $entry Array
     724 * @return Array
     725 */
     726function _import_process_entry( $entry ) {
     727        $post = get_post( $entry['post_id'] );
     728        if ( ! $post || ! in_array( $post->post_type, array( 'wcp_payment_request', 'wcb_reimbursement' ) ) ) {
     729                $entry['data'] = 'Post not found or post type mismatch';
     730                return $entry;
     731        }
     732
     733        $currency = false;
     734        $amout = false;
     735
     736        if ( $post->post_type == 'wcb_reimbursement' ) {
     737                $currency = get_post_meta( $post->ID, '_wcbrr_currency', true );
     738                $expenses = get_post_meta( $post->ID, '_wcbrr_expenses', true );
     739                foreach ( $expenses as $expense ) {
     740                        if ( ! empty( $expense['_wcbrr_amount'] ) ) {
     741                                $amount += floatval( $expense['_wcbrr_amount'] );
     742                        }
     743                }
     744        } elseif ( $post->post_type == 'wcp_payment_request' ) {
     745                $currency = get_post_meta( $post->ID, '_camppayments_currency', true );
     746                $amount = floatval( get_post_meta( $post->ID, '_camppayments_payment_amount', true ) );
     747        }
     748        $amount = round( $amount, 2 );
     749
     750        if ( empty( $entry['currency'] ) || $entry['currency'] != $currency ) {
     751                $entry['data'] = 'Currency mismatch';
     752                return $entry;
     753        }
     754
     755        $entry['amount'] = floatval( $entry['amount'] );
     756        $entry['amount'] = round( $entry['amount'], 2 );
     757
     758        if ( $entry['amount'] !== $amount ) {
     759                $entry['data'] = 'Payment amount mismatch';
     760                return $entry;
     761        }
     762
     763        // @todo Do some magic here.
     764
     765        // All good.
     766        $entry['processed'] = true;
     767        return $entry;
     768}
     769
     770class WCB_Import_Results {
     771        public static $data;
     772}
Note: See TracChangeset for help on using the changeset viewer.