Making WordPress.org

Changeset 2342


Ignore:
Timestamp:
01/20/2016 09:49:51 PM (11 years ago)
Author:
iandunn
Message:

WordCamp Budgets: Centralize file uploads for all modules.

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

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/css/wordcamp-budgets.css

    r2309 r2342  
    1515}
    1616
    17 .wcb-form li label {
     17.wcb-form li > label {
    1818        display: table-cell;
    1919        margin-bottom: 1em;
     
    3737}
    3838
     39/* Files */
     40.wcb_no_files_uploaded.active {
     41        display: block;
     42}
     43
     44#wcb_files .description {
     45        margin-bottom: 1em;
     46}
     47
     48.wcb_files_list li {
     49        list-style-type: disc;
     50        margin-left: 15px;
     51}
     52
     53.form-table td p.wcb_no_files_uploaded {
     54        margin-bottom: 1em;
     55}
     56
    3957
    4058/*
     
    4361
    4462/* Payment methods */
    45 .payment_method_fields.active,
    46 .wcp_no_files_uploaded.active {
     63.payment_method_fields.active {
    4764        display: block;
    4865}
     
    5067tr#row-payment-method label {
    5168        margin-right: 20px;
    52 }
    53 
    54 /* Files */
    55 #wcp_files .description {
    56         margin-bottom: 1em;
    57 }
    58 
    59 .wcp_files_list li {
    60         list-style-type: disc;
    61         margin-left: 15px;
    62 }
    63 
    64 .form-table td p.wcp_no_files_uploaded {
    65         margin-bottom: 1em;
    6669}
    6770
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/includes/payment-request.php

    r2331 r2342  
    387387         */
    388388        protected function render_files_input( $post, $label, $name, $description = '' ) {
    389                 $files = get_posts( array(
    390                         'post_parent'    => $post->ID,
    391                         'post_type'      => 'attachment',
    392                         'posts_per_page' => -1,
    393                         'orderby'        => 'title',
    394                         'order'          => 'ASC',
    395                 ) );
    396 
    397                 foreach ( $files as &$file ) {
    398                         $file->filename = wp_basename( $file->guid );
    399                         $file->url      = wp_get_attachment_url( $file->ID );
    400                 }
     389                $files = WordCamp_Budgets::get_attached_files( $post );
     390
     391                wp_localize_script( 'wcb-attached-files', 'wcbAttachedFiles', $files ); // todo merge into wordcampBudgets var
    401392
    402393                require( dirname( __DIR__ ) . '/views/payment-request/input-files.php' );
     
    765756
    766757                // Attach existing files
    767                 $this->attach_existing_files( $post_id, $_POST );
    768         }
    769 
     758                remove_action( 'save_post', array( $this, 'save_payment' ), 10 ); // avoid infinite recursion
     759                WordCamp_Budgets::attach_existing_files( $post_id, $_POST );
     760                add_action( 'save_post', array( $this, 'save_payment' ), 10, 2 );
     761        }
     762
     763        /**
     764         * Add log entries when the post status changes
     765         *
     766         * @param string  $new
     767         * @param string  $old
     768         * @param WP_Post $post
     769         */
    770770        public function transition_post_status( $new, $old, $post ) {
    771771                if ( $post->post_type != self::POST_TYPE )
     
    803803
    804804        /**
    805          * Attach unattached files to the payment request post
    806          *
    807          * Sometimes users will upload the files manually to the Media Library, instead of using the Add Files button,
    808          * and we need to attach them to the request so that they show up in the metabox.
    809          *
    810          * @param int   $post_id
    811          * @param array $request
    812          */
    813         protected function attach_existing_files( $post_id, $request ) {
    814                 if ( empty( $request['wcp_existing_files_to_attach'] ) ) {
    815                         return;
    816                 }
    817 
    818                 if ( ! $files = json_decode( $request['wcp_existing_files_to_attach'] ) ) {
    819                         return;
    820                 }
    821 
    822                 remove_action( 'save_post', array( $this, 'save_payment' ), 10 ); // avoid infinite recursion
    823 
    824                 foreach( $files as $file_id ) {
    825                         wp_update_post( array(
    826                                 'ID'          => $file_id,
    827                                 'post_parent' => $post_id,
    828                         ) );
    829                 }
    830 
    831                 add_action( 'save_post', array( $this, 'save_payment' ), 10, 2 );
    832         }
    833 
    834         /**
    835805         * Define columns for the Payment Requests screen.
    836806         *
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/includes/wordcamp-budgets.php

    r2331 r2342  
    275275
    276276        /**
     277         * Get the files attached to a post
     278         *
     279         * @param WP_Post $post
     280         *
     281         * @return array
     282         */
     283        public static function get_attached_files( $post ) {
     284                $files = get_posts( array(
     285                        'post_parent'    => $post->ID,
     286                        'post_type'      => 'attachment',
     287                        'posts_per_page' => 100,
     288                        'orderby'        => 'title',
     289                        'order'          => 'ASC',
     290                ) );
     291
     292                foreach ( $files as &$file ) {
     293                        $file->filename = wp_basename( $file->guid );
     294                        $file->url      = wp_get_attachment_url( $file->ID );
     295                }
     296
     297                return $files;
     298        }
     299
     300        /**
     301         * Attach unattached files to the payment request post
     302         *
     303         * Sometimes users will upload the files manually to the Media Library, instead of using the Add Files button,
     304         * and we need to attach them to the request so that they show up in the metabox.
     305         *
     306         * NOTE: The calling function must remove any of its save_post callbacks before calling this, in order to
     307         * avoid infinite recursion
     308         *
     309         * @param int   $post_id
     310         * @param array $request
     311         */
     312        public static function attach_existing_files( $post_id, $request ) {
     313                if ( empty( $request['wcb_existing_files_to_attach'] ) ) {
     314                        return;
     315                }
     316
     317                if ( ! $files = json_decode( $request['wcb_existing_files_to_attach'] ) ) {
     318                        return;
     319                }
     320
     321                foreach( $files as $file_id ) {
     322                        wp_update_post( array(
     323                                'ID'          => $file_id,
     324                                'post_parent' => $post_id,
     325                        ) );
     326                }
     327        }
     328
     329        /**
    277330         * Insert an entry into a log for one of the custom post types
    278331         *
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/javascript/attached-files.js

    r2328 r2342  
    2828        app.AttachedFileView = Backbone.View.extend( {
    2929                tagName: 'li',
    30                 template: wp.template( 'wcp-attached-file' ),
     30                template: wp.template( 'wcb-attached-file' ),
    3131
    3232                initialize: function() {
     
    4444         */
    4545        app.AttachedFilesView = Backbone.View.extend( {
    46                 el: $( '#wcp_files' ),
    47 
    4846                initialize: function() {
    4947                        _.bindAll( this, 'render', 'appendFile' );
     
    6462
    6563                appendFile: function( file ) {
    66                         var noFilesUploaded  = $( '.wcp_no_files_uploaded' );
     64                        var noFilesUploaded  = $( '.wcb_no_files_uploaded' );
    6765                        var attachedFileView = new app.AttachedFileView( { model: file } );
    6866
    69                         $( '.wcp_files_list' ).append( attachedFileView.render().el );
     67                        $( '.wcb_files_list' ).append( attachedFileView.render().el );
    7068                        noFilesUploaded.removeClass( 'active' );
    7169                        noFilesUploaded.addClass( 'hidden' );
     
    8684                attachExistingFile: function( file ) {
    8785                        var fileIDsToAttach,
    88                                 existingFilesToAttach = $( '#wcp_existing_files_to_attach' );
     86                                existingFilesToAttach = $( '#wcb_existing_files_to_attach' );
    8987
    9088                        try {
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/javascript/payment-requests.js

    r2331 r2342  
    1111                        try {
    1212                                app.registerEventHandlers();
    13                                 wcb.attachedFilesView = new wcb.AttachedFilesView();
     13                                wcb.attachedFilesView = new wcb.AttachedFilesView( { el: $( '#wcp_files' ) } );
    1414                                wcb.setupDatePicker( '#wcp_general_info' );
    1515                        } catch ( exception ) {
     
    2727                        paymentCategory.change( app.toggleOtherCategoryDescription );
    2828                        paymentCategory.trigger( 'change' );   // Set the initial state
    29                         $( '#wcp_files' ).find( 'a.wcp-insert-media' ).click( wcb.showUploadModal );
     29                        $( '#wcp_files' ).find( 'a.wcb-insert-media' ).click( wcb.showUploadModal );
    3030                        $( '#wcp_mark_incomplete_checkbox' ).click( app.requireNotes );
    3131                },
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/javascript/wordcamp-budgets.js

    r2331 r2342  
    4949                 */
    5050                addSelectedFilesToCollection : function() {
    51                         // app var needs to point to caller?
    52 
    5351                        var attachments = app.fileUploadFrame.state().get( 'selection' ).toJSON();
    5452
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/views/payment-request/input-files.php

    r2327 r2342  
    99                <?php endif; ?>
    1010
    11                 <?php // todo move to centralized view file ?>
    12                 <p>
    13                         <a class="button wcp-insert-media" role="button">
    14                                 <?php _e( 'Add files', 'wordcamporg' ); ?>
    15                         </a>
    16                 </p>
    17 
    18                 <h4><?php _e( 'Attached files:', 'wordcamporg' ); ?></h4>
    19 
    20                 <ul class="wcp_files_list"></ul>
    21 
    22                 <p class="wcp_no_files_uploaded <?php echo $files ? 'hidden' : 'active'; ?>">
    23                         <?php _e( "You haven't uploaded any files yet.", 'wordcamporg' ); ?>
    24                 </p>
    25 
    26                 <script type="text/html" id="tmpl-wcp-attached-file">
    27                         <a href="{{ data.url }}">{{ data.filename }}</a>
    28                 </script>
    29 
    30                 <?php wp_localize_script( 'wcb-attached-files', 'wcbAttachedFiles', $files ); // todo merge into wordcampBudgets var ?>
     11                <?php require_once( dirname( __DIR__ ) . '/wordcamp-budgets/field-attached-files.php' ); ?>
    3112        </td>
    3213</tr>
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/views/payment-request/metabox-files.php

    r1939 r2342  
    11<table class="form-table">
    2         <input id="wcp_existing_files_to_attach" name="wcp_existing_files_to_attach" type="hidden" value="" />
    3 
    42        <?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' ) ); ?>
    53        <?php $this->render_textarea_input( $post, 'Notes', 'file_notes' ); ?>
Note: See TracChangeset for help on using the changeset viewer.