7

My client is using an old classipress version, here's a github repo I found but what he's using is much older. Running the latest Wordpress version. It comes with plupload, some old version, couldn't find the version in the theme. Here's Functions.php, here's plupload. Here's the html of my page, no need to see it, but i'm putting it there because the page is protected so that's the only way to inspect the whole code if you want to.

I want to add the ability to upload multiple pictures at the same time, to do that, I add this to functions.php

add_filter('appthemes_plupload_config', 'enable_plupload_multisel', 10 ,1);
function enable_plupload_multisel($app_plupload_config){
$app_plupload_config['plupload']['multi_selection'] = true;
return $app_plupload_config; }

But I don't know how to stop the user from uploading more than 8 pictures? I tried adding max_files and max_files_count and max_file_count and nothing worked, I even modified the source code of the plugin itself and the js and nothing worked. I want to stop the user from being able to upload more than 8 images.

After I gave up on plupload, I tried doing it using Jquery, again didn't work

 /* prevent form submission if user selects more than 8 pics */
 jQuery('#app-attachment-upload-pickfiles').change(function() {
     if (this.files.length > 8) {
         alert('Uploading more than 8 images is not allowed');
         this.value = '';
     }
 });
 // Prevent submission if limit is exceeded.
 jQuery('#mainform').submit(function() {
     if (this.files.length > 8) {
         jQuery('#app-attachment-upload-pickfiles').hide();
         jQuery('#step1').hide();
         return false;
     } else {
         jQuery('#app-attachment-upload-pickfiles').show();
         jQuery('#step1').show();
     }
 });

Edit

My pluploadjs here. FilesAdded

    attachUploader.bind('FilesAdded', function(up, files) {
        jQuery.each(files, function(i, file) {
            jQuery('#app-attachment-upload-filelist').append(
                '<div id="' + file.id + '" class="app-attachment-upload-progress">' +
                file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
                '</div>');

            window.appFileCount += 1;
            APP_Attachment.hideUploadBtn();
        });

        up.refresh();
        attachUploader.start();
    });

I modified it to look like so

    attachUploader.bind('FilesAdded', function(up, files) {
        var maxfiles = 8;
            if(up.files.length > maxfiles )
             {
                up.splice(maxfiles);
                alert('no more than '+maxfiles + ' file(s)');
             }
            if (up.files.length === maxfiles) {
                $('#app-attachment-upload-filelist').hide("slow"); // provided there is only one #uploader_browse on page
            }
        jQuery.each(files, function(i, file) {
            jQuery('#app-attachment-upload-filelist').append(
                '<div id="' + file.id + '" class="app-attachment-upload-progress">' +
                file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
                '</div>');

            window.appFileCount += 1;
            APP_Attachment.hideUploadBtn();
        });

        up.refresh();

        attachUploader.start();
    });

Is that all? Will it work now? I haven't tested it because it will give errors

Lynob
  • 5,059
  • 15
  • 64
  • 114
  • I tried asking on plupload forum http://www.plupload.com/punbb/register.php it says `This forum is not accepting new registrations. Post your questions to StackOverflow instead (with a tag "plupload").` I tried asking on github too, I hope to get an answer – Lynob Oct 24 '17 at 19:47
  • this may help : https://stackoverflow.com/a/15520225/1236044 – jbl Oct 25 '17 at 16:26
  • @jbl please read my edit and tell me if it would work now – Lynob Oct 27 '17 at 12:33
  • well, perfoming a jQuery.each on a collection which may just have been spliced does not seem correct. A way to test : http://www.plupload.com/docs/v2/Create-a-Fiddle – jbl Oct 27 '17 at 12:43
  • @jbl tried it, nothing is working, maybe there's some function in wordpress. I don't know I give up – Lynob Oct 27 '17 at 19:38
  • your JQuery.each should take `up.files` and not `files` as first parameter. Also `window.appFileCount += 1;` may produce erratic behaviour – jbl Oct 31 '17 at 11:06
  • @jbl I guess so because the answer given almost worked – Lynob Oct 31 '17 at 12:58

1 Answers1

1

I'm not sure but your code should almost work. I think you should manually remove the files from the queue by calling the removeFile method.

Maybe try this code:

    attachUploader.bind('FilesAdded', function(up, files) {
        var maxfiles = 8;

        // remove all new files after the max of files
        jQuery.each(up.files, function(i, file) {
            if(i > maxfiles){
                up.removeFile(file);
            }
        });      
});
Ferry Kranenburg
  • 2,625
  • 1
  • 17
  • 23
  • almost there, worked once however i still have this https://pastebin.com/04UDCBxY – Lynob Oct 31 '17 at 12:58
  • That's an stacktrace, so you should be able to trace it back to the exact line of code where this happens. Probably where you use and 'id' of an undefnined object . Make sure you don't use the file object that was already removed. – Ferry Kranenburg Nov 02 '17 at 18:28
  • will try it later, I give you the bounty because it was going to expire and I don't have time to test it – Lynob Nov 04 '17 at 19:28