Advertisement
Guest User

scripts.js

a guest
Jul 12th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var hasChanged = false;
  2.  
  3. function confirmExit()
  4. {
  5.     var mce = typeof(tinyMCE) != 'undefined' ? tinyMCE.activeEditor : false;
  6.     if(hasChanged || (mce && !mce.isHidden() && mce.isDirty() ))
  7.         return "You have unsaved changes. Proceed anyway?";
  8. }
  9. window.onbeforeunload = confirmExit;
  10.  
  11. function substr_count(mainString, subString){
  12.     var re = new RegExp(subString, 'g');
  13.     if(!mainString.match(re) || !mainString || !subString)
  14.     return 0;
  15.     var count = mainString.match(re);
  16.     return count.length;
  17. }
  18.  
  19. function str_word_count(s){
  20.     if(!s.length)
  21.     return 0;
  22.     s = s.replace(/(^\s*)|(\s*$)/gi,"");
  23.     s = s.replace(/[ ]{2,}/gi," ");
  24.     s = s.replace(/\n /,"\n");
  25.     return s.split(' ').length;
  26. }
  27.  
  28. function countTags(s){
  29.     if(!s.length)
  30.         return 0;
  31.     return s.split(',').length;
  32. }
  33.  
  34. function post_has_errors(title,content,bio,category,tags, fimg){
  35.     var error_string = '';
  36.     if(fep_rules.check_required == false)
  37.         return false;
  38.  
  39.     if((fep_rules.min_words_title !=0 && title==='') || (fep_rules.min_words_content !=0 && content==='') || (fep_rules.min_words_bio !=0 && bio==='') || category==-1 || (fep_rules.min_tags !=0 &&  tags==='') )
  40.         error_string = 'You missed one or more required fields</br>';
  41.    
  42.     var stripped_content = content.replace(/(<([^>]+)>)/ig,"");
  43.     var stripped_bio = bio.replace(/(<([^>]+)>)/ig,"");
  44.  
  45.     if ( title != '' && str_word_count(title) < fep_rules.min_words_title )
  46.         error_string += 'The title is too short<br/>';
  47.     if ( content != '' && str_word_count( title ) > fep_rules.max_words_title )
  48.         error_string += 'The title is too long<br/>';
  49.     if ( content != '' && str_word_count( stripped_content ) < fep_rules.min_words_content )
  50.         error_string += 'The article is too short<br/>';
  51.     if ( str_word_count( stripped_content ) > fep_rules.max_words_content )
  52.         error_string += 'The article is too long<br/>';
  53.     if ( bio != -1 && bio != '' && str_word_count( stripped_bio ) < fep_rules.min_words_bio )
  54.         error_string += 'The bio is too short<br/>';
  55.     if ( bio != -1 && str_word_count( stripped_bio ) > fep_rules.max_words_bio )
  56.         error_string += 'The bio is too long<br/>';
  57.     if ( substr_count( content, '</a>' ) > fep_rules.max_links )
  58.         error_string += 'There are too many links in the article body<br/>';
  59.     if ( substr_count( bio, '</a>' ) > fep_rules.max_links_bio )
  60.         error_string += 'There are too many links in the bio<br/>';
  61.     if ( tags != '' && countTags(tags) < fep_rules.min_tags )
  62.         error_string += 'You haven\'t added the required number of tags<br/>';
  63.     if ( countTags(tags) > fep_rules.max_tags )
  64.         error_string += 'There are too many tags<br/>';
  65.     if(fep_rules.thumbnail_required && fep_rules.thumbnail_required == 'true' && fimg == -1)
  66.         error_string += 'You need to choose a featured image<br/>';
  67.  
  68.     if ( error_string == '' )
  69.         return false;
  70.     else
  71.         return '<h2>Your submission has errors. Please try again!</h2>'+error_string;
  72. }
  73.  
  74. jQuery(document).ready(function($){
  75.     $("input, textarea, #fep-post-content").keydown(function () { hasChanged = true; });
  76.     $("select").change(function(){ hasChanged = true; });
  77.     $("td.post-delete a").click(function(event) {
  78.         var id              = $(this).siblings('.post-id').first().val();
  79.         var nonce           = $('#fepnonce_delete').val();
  80.         var loadimg         = $(this).siblings('.fep-loading-img').first();
  81.         var row             = $(this).closest('.fep-row');
  82.         var message_box     = $('#fep-message');
  83.         var post_count      = $('#fep-posts .count');
  84.         var confirmation    = confirm('Are you sure?');
  85.         if(!confirmation)
  86.             return;
  87.         $(this).hide();
  88.         loadimg.show().css({'float':'none','box-shadow':'none'});
  89.         $.ajax({
  90.             type: 'POST',
  91.             url: fepajaxhandler.ajaxurl,
  92.             data: {
  93.                 action: 'fep_delete_posts',
  94.                 post_id: id,
  95.                 delete_nonce: nonce
  96.             },
  97.             success:function(data, textStatus, XMLHttpRequest){
  98.                 var arr = $.parseJSON(data);
  99.                 message_box.html('');
  100.                 if(arr.success){
  101.                     row.hide();
  102.                     message_box.show().addClass('success').append(arr.message);
  103.                     post_count.html(Number(post_count.html())-1);
  104.                 }
  105.                 else{
  106.                     message_box.show().addClass('warning').append(arr.message);
  107.                 }
  108.                 if( message_box.offset().top < $(window).scrollTop() ){
  109.                     $('html, body').animate({ scrollTop: message_box.offset().top-10 }, 'slow');
  110.                 }
  111.             },
  112.             error: function(MLHttpRequest, textStatus, errorThrown){
  113.                 alert(errorThrown);
  114.             }
  115.         });
  116.         event.preventDefault();
  117.     });
  118.     $("#fep-submit-post.active-btn").on('click', function() {
  119.         tinyMCE.triggerSave();
  120.         var title           = $("#fep-post-title").val();
  121.         var content         = $("#fep-post-content").val();
  122.         var bio             = $("#fep-about").val();
  123.         var category        = $("#fep-category").val();
  124.         var tags            = $("#fep-tags").val();
  125.         var pid             = $("#fep-post-id").val();
  126.         var fimg            = $("#fep-featured-image-id").val();
  127.         var nonce           = $("#fepnonce").val();
  128.         var message_box     = $('#fep-message');
  129.         var form_container  = $('#fep-new-post');
  130.         var submit_btn      = $('#fep-submit-post');
  131.         var load_img        = $("img.fep-loading-img");
  132.         var submission_form = $('#fep-submission-form');
  133.         var post_id_input   = $("#fep-post-id");
  134.         var errors          = post_has_errors(title, content, bio, category, tags, fimg);
  135.         if( errors ){
  136.             if( form_container.offset().top < $(window).scrollTop() ){
  137.             $('html, body').animate({ scrollTop: form_container.offset().top-10 }, 'slow'); }
  138.             message_box.removeClass('success').addClass('warning').html('').show().append(errors);
  139.             return;
  140.         }
  141.         load_img.show();
  142.         submit_btn.attr("disabled", true).removeClass('active-btn').addClass('passive-btn');
  143.         $.ajaxSetup({cache:false});
  144.         $.ajax({
  145.             type: 'POST',
  146.             url: fepajaxhandler.ajaxurl,
  147.             data: {
  148.                 action:             'fep_process_form_input',
  149.                 post_title:         title,
  150.                 post_content:       content,
  151.                 about_the_author:   bio,
  152.                 post_category:      category,
  153.                 post_tags:          tags,
  154.                 post_id:            pid,
  155.                 featured_img:       fimg,
  156.                 post_nonce:         nonce
  157.             },
  158.             success:function(data, textStatus, XMLHttpRequest){
  159.                 hasChanged = false;
  160.                 var arr = $.parseJSON(data);
  161.                 if(arr.success){
  162.                     submission_form.hide();
  163.                     post_id_input.val(arr.post_id);
  164.                     message_box.removeClass('warning').addClass('success');
  165.                 }
  166.                 else
  167.                     message_box.removeClass('success').addClass('warning');
  168.                 message_box.html('').append(arr.message).show();
  169.                 if( form_container.offset().top < $(window).scrollTop() ){
  170.                     $('html, body').animate({ scrollTop: form_container.offset().top-10 }, 'slow');
  171.                 }
  172.                 load_img.hide();
  173.                 submit_btn.attr("disabled", false).removeClass('passive-btn').addClass('active-btn');
  174.             },
  175.             error: function(MLHttpRequest, textStatus, errorThrown){
  176.                 alert(errorThrown);
  177.             }
  178.         });
  179.     });
  180.     $('body').on('click', '#fep-continue-editing', function(e){
  181.         $('#fep-message').hide();
  182.         $('#fep-submission-form').show();
  183.         e.preventDefault();
  184.     });
  185.     $('#fep-featured-image a#fep-featured-image-link').click(function(e){
  186.         e.preventDefault();
  187.         custom_uploader = wp.media.frames.file_frame = wp.media({
  188.             title: 'Choose Image',
  189.             button: {
  190.                 text: 'Choose Image'
  191.             },
  192.             multiple: false
  193.         });
  194.         custom_uploader.on('select', function() {
  195.             attachment = custom_uploader.state().get('selection').first().toJSON();
  196.             jQuery('#fep-featured-image input#fep-featured-image-id').val(attachment.id);
  197.             $.ajax({
  198.                 type: 'POST',
  199.                 url: fepajaxhandler.ajaxurl,
  200.                 data: {
  201.                     action: 'fep_fetch_featured_image',
  202.                     img: attachment.id
  203.                 },
  204.                 success:function(data, textStatus, XMLHttpRequest){
  205.                     $('#fep-featured-image-container').html(data);
  206.                     hasChanged = true;
  207.                 },
  208.                 error: function(MLHttpRequest, textStatus, errorThrown){
  209.                     alert(errorThrown);
  210.                 }
  211.             });
  212.         });
  213.         custom_uploader.open();
  214.     });
  215. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement