Make WordPress Core

Ticket #28053: media-models.patch

File media-models.patch, 2.3 KB (added by drosendo, 9 years ago)

Allow multiple values in Media Attachment fields

  • wp-includes/js/media-models.js

     
    358358                        return $.Deferred().rejectWith( this ).promise();
    359359                }
    360360
     361                //Add test for multiple field values and returns value for storage.
     362                data = model.multipleFieldsValidator(data);
     363                   
    361364                return wp.media.post( 'save-attachment-compat', _.defaults({
    362365                        id:      this.id,
    363366                        nonce:   this.get('nonces').update,
     
    365368                }, data ) ).done( function( resp, status, xhr ) {
    366369                        model.set( model.parse( resp, xhr ), options );
    367370                });
    368         }
     371        },
     372        /**
     373         * Test for multiple field values and returns value for storage.
     374         *
     375         * @static
     376         * @param {Object} data The properties to be checked for multiple fields.
     377         * @returns {Object} data The properties to be saved.
     378         */
     379        multipleFieldsValidator: function (data) {
     380            var result = true;
     381            var values;
     382            $.each(data, function (el, i) {
     383                var arrChkBox = document.getElementsByName(el);
     384                var elementType = $(arrChkBox).get(0).tagName;
     385
     386                switch (elementType) {
     387                    case 'INPUT':
     388                        if ($(arrChkBox).attr('type') == 'checkbox' && $(arrChkBox).size() > 1)
     389                            result = false;
     390                        values = $(arrChkBox).map(function () {
     391                            if ($(this).is(':checked'))
     392                                return $(this).val();
     393                        }).get();
     394                        break;
     395                    case 'SELECT':
     396                        if ($(arrChkBox).attr('multiple')) {
     397                            result = false;
     398                            values = $(arrChkBox).find('option').map(function () {
     399                                if ($(this).is(':selected'))
     400                                    return $(this).val();
     401                            }).get();
     402                        }
     403                        break;
     404                }
     405                if (!result) {
     406                    data[el] = values;
     407                }
     408            });
     409            return data;
     410        }
    369411}, {
    370412        /**
    371413         * Create a new model on the static 'all' attachments collection and return it.