Making WordPress.org

Changeset 1939


Ignore:
Timestamp:
10/06/2015 12:11:17 AM (11 years ago)
Author:
iandunn
Message:

WordCamp Payments: Attach existing files to payment request.

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

Legend:

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

    r1820 r1939  
    748748
    749749                // Verify nonces
    750                 $nonces = array( 'status_nonce', 'general_info_nonce', 'payment_details_nonce', 'vendor_details_nonce' );    // todo add prefix to all of these
     750                $nonces = array( 'status_nonce', 'general_info_nonce', 'payment_details_nonce', 'vendor_details_nonce', 'wcp_files_nonce' );    // todo add prefix to all of these
    751751
    752752                foreach ( $nonces as $nonce ) {
    … …  
    854854                        }
    855855                }
     856
     857                // Attach existing files
     858                $this->attach_existing_files( $post_id, $_POST );
     859        }
     860
     861        /**
     862         * Attach unattached files to the payment request post
     863         *
     864         * Sometimes users will upload the files manually to the Media Library, instead of using the Add Files button,
     865         * and we need to attach them to the request so that they show up in the metabox.
     866         *
     867         * @param int   $post_id
     868         * @param array $request
     869         */
     870        protected function attach_existing_files( $post_id, $request ) {
     871                if ( empty( $request['wcp_existing_files_to_attach'] ) ) {
     872                        return;
     873                }
     874
     875                if ( ! $files = json_decode( $request['wcp_existing_files_to_attach'] ) ) {
     876                        return;
     877                }
     878
     879                remove_action( 'save_post', array( $this, 'save_payment' ), 10, 2 ); // avoid infinite recursion
     880
     881                foreach( $files as $file_id ) {
     882                        wp_update_post( array(
     883                                'ID'          => $file_id,
     884                                'post_parent' => $post_id,
     885                        ) );
     886                }
     887
     888                add_action( 'save_post', array( $this, 'save_payment' ), 10, 2 );
    856889        }
    857890
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/classes/wordcamp-payments.php

    r995 r1939  
    55 */
    66class WordCamp_Payments {
    7         const VERSION = '0.1.0';
     7        const VERSION = '0.1.1';
    88
    99        /**
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/javascript/attached-files.js

    r995 r1939  
    77                defaults: {
    88                        'ID':       0,
     9                        'post_parent': 0,
    910                        'filename': '',
    1011                        'url':      ''
    1112                }
     13
     14                // todo realign
    1215        } );
    1316
    … …  
    6669                        noFilesUploaded.removeClass( 'active' );
    6770                        noFilesUploaded.addClass( 'hidden' );
     71
     72                        this.attachExistingFile( file );
     73                },
     74
     75                /**
     76                 * Keep track of existing files that should be attached to the request
     77                 *
     78                 * Sometimes users add existing files to the request, rather than uploading new ones. We need to keep track
     79                 * of those so that they can be attached to the request when the form is submitted.
     80                 *
     81                 * Files that are already attached to other posts are ignored.
     82                 *
     83                 * @param {wordcampPayments.AttachedFile} file
     84                 */
     85                attachExistingFile: function( file ) {
     86                        var fileIDsToAttach,
     87                                existingFilesToAttach = $( '#wcp_existing_files_to_attach' );
     88
     89                        try {
     90                                fileIDsToAttach = JSON.parse( existingFilesToAttach.val() );
     91                        } catch ( exception ) {
     92                                fileIDsToAttach = [];
     93                        }
     94
     95                        if ( 0 === file.get( 'post_parent' ) && -1 === $.inArray( file.get( 'ID' ), fileIDsToAttach ) ) {
     96                                fileIDsToAttach.push( file.get( 'ID' ) );
     97                                existingFilesToAttach.val( JSON.stringify( fileIDsToAttach ) );
     98                        }
    6899                }
    69100        } )
     101        // todo add semicolon
    70102
    71103        $.wordcampPayments.attachedFilesView = new $.wordcampPayments.AttachedFilesView();
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/javascript/wordcamp-payments.js

    r1816 r1939  
    8585                        var attachments = $.wordcampPayments.fileUploadFrame.state().get( 'selection' ).toJSON();
    8686
    87                         $.each( attachments, function( index, attachment ) {                                                                                            // todo if selected an existing file, it isn't attached, so after post is saved it wont be in the list
     87                        $.each( attachments, function( index, attachment ) {
    8888                                var newFile = new $.wordcampPayments.AttachedFile( {
    8989                                        'ID':       attachment.id,
     90                                        'post_parent': attachment.uploadedTo,
    9091                                        'filename': attachment.filename,
    9192                                        'url':      attachment.url
    9293                                } );
     94
     95                                // todo realign
    9396
    9497                                $.wordcampPayments.attachedFilesView.collection.add( newFile );
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/views/payment-request/metabox-files.php

    r995 r1939  
    11<table class="form-table">
     2        <input id="wcp_existing_files_to_attach" name="wcp_existing_files_to_attach" type="hidden" value="" />
     3
    24        <?php $this->render_files_input( $post, 'Files', 'files', __( 'Attach supporting documentation including invoices, contracts, or other vendor correspondence. If no supporting documentation is available, please indicate the reason in the notes below.', 'wordcamporg' ) ); ?>
    35        <?php $this->render_textarea_input( $post, 'Notes', 'file_notes' ); ?>
Note: See TracChangeset for help on using the changeset viewer.