Making WordPress.org


Ignore:
Timestamp:
02/24/2016 09:53:47 PM (11 years ago)
Author:
kovshenin
Message:

WordCamp.org: Initial stub for JPM imports.

File:
1 edited

Legend:

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

    r2578 r2597  
    44        public static $list_table;
    55        public static $db_version = 6;
     6        public static $import_results = null;
    67
    78        /**
    … …  
    2021                add_action( 'init', array( __CLASS__, 'upgrade' ) );
    2122                add_action( 'init', array( __CLASS__, 'process_export_request' ) );
     23                add_action( 'init', array( __CLASS__, 'process_import_request' ) );
    2224
    2325                // Diff-based updates to the index.
    … …  
    233235                                if ( 'export' == self::get_current_tab() ) {
    234236                                        self::render_export_tab();
    235                                 } else {
     237                                } elseif ( 'import' == self::get_current_tab() ) {
     238                                        self::render_import_tab();
     239                                }
     240                                else {
    236241                                        self::render_table_tabs();
    237242                                }
    … …  
    10721077
    10731078        /**
     1079         * Renders the import tab.
     1080         */
     1081        public static function render_import_tab() {
     1082                ?>
     1083                <?php if ( isset( self::$import_results ) ) : ?>
     1084                <h2>Import Results</h2>
     1085                <pre><?php echo esc_html( print_r( self::$import_results, true ) ); ?></pre>
     1086                <?php endif; ?>
     1087
     1088                <p>Import payment results from JPM reports CSV.</p>
     1089                <form method="post" enctype="multipart/form-data">
     1090                        <?php wp_nonce_field( 'import', 'wcpn_request_import' ); ?>
     1091                        <label>Import File:</label>
     1092                        <input type="file" name="wcpn_import_file" />
     1093                        <?php submit_button( 'Import' ); ?>
     1094                </form>
     1095                <?php
     1096        }
     1097
     1098        /**
     1099         * Process a payments import, runs during init.
     1100         */
     1101        public static function process_import_request() {
     1102                if ( empty( $_POST['submit'] ) || 'import' != self::get_current_tab() ) {
     1103                        return;
     1104                }
     1105
     1106                if ( ! current_user_can( 'manage_network' ) || ! check_admin_referer( 'import', 'wcpn_request_import' ) ) {
     1107                        return;
     1108                }
     1109
     1110                if ( empty( $_FILES['wcpn_import_file'] ) ) {
     1111                        wp_die( 'Please select a file to import.' );
     1112                }
     1113
     1114                $file = $_FILES['wcpn_import_file'];
     1115                if ( $file['type'] != 'text/csv' ) {
     1116                        wp_die( 'Please upload a text/csv file.' );
     1117                }
     1118
     1119                if ( $file['size'] < 1 ) {
     1120                        wp_die( 'Please upload a file that is not empty.' );
     1121                }
     1122
     1123                if ( $file['error'] ) {
     1124                        wp_die( 'Some other error has occurred. Sorry.' );
     1125                }
     1126
     1127                $handle = fopen( $file['tmp_name'], 'r' );
     1128                $count = 0;
     1129                $header = array();
     1130                $results = array();
     1131
     1132                while ( ( $line = fgetcsv( $handle ) ) !== false ) {
     1133                        // Skip first line.
     1134                        if ( ++$count == 1 ) {
     1135                                continue;
     1136                        }
     1137
     1138                        $entry = array(
     1139                                'type' => strtolower( $line[11] ),
     1140                                'status' => strtolower( $line[7] ),
     1141                                'amount' => round( floatval( $line[13] ), 2 ),
     1142                                'currency' => strtoupper( $line[14] ),
     1143                                'blog_id' => null,
     1144                                'post_id' => null,
     1145                                'processed' => false,
     1146                                'data' => null,
     1147                        );
     1148
     1149                        switch ( $entry['type'] ) {
     1150                                case 'wire':
     1151                                        if ( ! empty( $line[44] ) && preg_match( '#^wcb-([0-9]+)-([0-9]+)$#', $line[44], $matches ) ) {
     1152                                                $entry['blog_id'] = $matches[1];
     1153                                                $entry['post_id'] = $matches[2];
     1154                                        }
     1155                                        break;
     1156                                case 'ach':
     1157                                        if ( ! empty( $line[91] ) && preg_match( '#^([0-9]+)-([0-9]+)$#', $line[91], $matches ) ) {
     1158                                                $entry['blog_id'] = $matches[1];
     1159                                                $entry['post_id'] = $matches[2];
     1160                                        }
     1161                                        break;
     1162                        }
     1163
     1164                        if ( empty( $entry['blog_id'] ) || empty( $entry['post_id'] ) ) {
     1165                                $results[] = $entry;
     1166                                continue;
     1167                        }
     1168
     1169                        // Don't consume memory.
     1170                        wp_suspend_cache_addition( true );
     1171                        switch_to_blog( $entry['blog_id'] );
     1172
     1173                        $results[] = self::_import_process_entry( $entry );
     1174
     1175                        restore_current_blog();
     1176                        wp_suspend_cache_addition( false );
     1177                }
     1178
     1179                fclose( $handle );
     1180                self::$import_results = $results;
     1181        }
     1182
     1183        /**
     1184         * Process a single import entry.
     1185         *
     1186         * Runs in a switch_to_blog() context.
     1187         *
     1188         * @param $entry Array
     1189         * @return Array
     1190         */
     1191        private static function _import_process_entry( $entry ) {
     1192                $post = get_post( $entry['post_id'] );
     1193                if ( ! $post || $post->post_type != 'wcp_payment_request' ) {
     1194                        $entry['data'] = 'Post not found or post type mismatch';
     1195                        return $entry;
     1196                }
     1197
     1198                if ( $entry['currency'] != get_post_meta( $post->ID, '_camppayments_currency', true ) ) {
     1199                        $entry['data'] = 'Currency mismatch';
     1200                        return $entry;
     1201                }
     1202
     1203                $amount_orig = floatval( get_post_meta( $post->ID, '_camppayments_payment_amount', true ) );
     1204                $amount_orig = round( $amount_orig, 2 );
     1205                if ( (string) $entry['amount'] != (string) $amount_orig ) {
     1206                        $entry['data'] = 'Payment amount mismatch';
     1207                        return $entry;
     1208                }
     1209
     1210                // @todo Do some magic here.
     1211
     1212                // All good.
     1213                $entry['processed'] = true;
     1214                return $entry;
     1215        }
     1216
     1217        /**
    10741218         * Loads and initializes the list table object.
    10751219         */
    … …  
    10851229        public static function get_current_tab() {
    10861230                $tab = 'overdue';
    1087 
    1088                 if ( isset( $_REQUEST['wcp-section'] ) && in_array( $_REQUEST['wcp-section'], array( 'pending', 'overdue', 'paid', 'incomplete', 'export' ) ) ) {
     1231                $tabs = array(
     1232                        'pending',
     1233                        'overdue',
     1234                        'paid',
     1235                        'incomplete',
     1236                        'export',
     1237                        'import',
     1238                );
     1239
     1240                if ( isset( $_REQUEST['wcp-section'] ) && in_array( $_REQUEST['wcp-section'], $tabs ) ) {
    10891241                        $tab = $_REQUEST['wcp-section'];
    10901242                }
    … …  
    11041256                        'incomplete' => __( 'Incomplete', 'wordcamporg' ),
    11051257                        'export'     => __( 'Export', 'wordcamporg' ),
     1258                        'import'     => __( 'Import', 'wordcamporg' ),
    11061259                );
    11071260
Note: See TracChangeset for help on using the changeset viewer.