Plugin Directory

Changeset 2789926


Ignore:
Timestamp:
09/25/2022 05:56:56 PM (4 years ago)
Author:
jweathe
Message:

Native WP error messages

Location:
ajax-comments-refueled
Files:
4 edited
6 copied

Legend:

Unmodified
Added
Removed
  • ajax-comments-refueled/tags/1.2.0/plugin.php

    r2610646 r2789926  
    55Plugin URI: https://planetjon.ca/projects/ajax-comments/
    66Description: Make your comment system fly with a full AJAX implementation of your existing template.
    7 Version: 1.1.1
     7Version: 1.2.0
    88Requires at least: 4.6
    9 Tested up to: 5.8
     9Tested up to: 6.0.2
    1010Requires PHP: 5.4
    1111Author: Jonathan Weatherhead
     
    1919use \WP_Query;
    2020
    21 add_action( 'wp_enqueue_scripts', function() {
    22     if( is_singular() ) {
    23         wp_dequeue_script( 'reply-comments' );
    24         wp_enqueue_script( 'wac-script', plugins_url( 'scripts.min.js', __FILE__ ), [], false, true );
    25         wp_localize_script( 'wac-script', 'wac_env', [
    26             'ajaxurl' => admin_url( 'admin-ajax.php' ),
    27             'pending_msg' => __( '⧖ Thanks! Your comment will be available shortly. If you dont see it immediately, it is pending moderation.', 'wp-ajax-comments' ),
    28             'error_msg' => __( '❌ Something went wrong. Please try again later.', 'wp-ajax-comments' )
     21add_action('wp_enqueue_scripts', function () {
     22    if (is_singular()) {
     23        wp_dequeue_script('reply-comments');
     24        wp_enqueue_script('wac-script', plugins_url('scripts.min.js', __FILE__), [], false, true);
     25        wp_localize_script('wac-script', 'wac_env', [
     26            'ajaxurl' => admin_url('admin-ajax.php'),
     27            'pending_msg' => __('Your comment is awaiting moderation.'), // WP string
     28            'error_msg' => __('Something went wrong. Please try again later.', 'wp-ajax-comments')
    2929        ]);
    3030    }
    31 }, 11 );
     31}, 11);
    3232
    33 add_filter( 'comments_template', function( $theme_template ) {
    34     return !wp_doing_ajax() ? plugin_dir_path( __FILE__ ) . 'comments.php' : $theme_template;
    35 } );
     33add_filter('comments_template', function ($theme_template) {
     34    return !wp_doing_ajax() ? plugin_dir_path(__FILE__) . 'comments.php' : $theme_template;
     35});
    3636
    37 add_action( 'wp_ajax_wac_load_comments', __NAMESPACE__ . '\wac_load_comments' );
    38 add_action( 'wp_ajax_nopriv_wac_load_comments', __NAMESPACE__ . '\wac_load_comments' );
     37add_action('wp_ajax_wac_load_comments', __NAMESPACE__ . '\wac_load_comments');
     38add_action('wp_ajax_nopriv_wac_load_comments', __NAMESPACE__ . '\wac_load_comments');
    3939
    40 function wac_load_comments() {
     40function wac_load_comments()
     41{
    4142    global $post;
    4243
    43     if( !headers_sent() ) {
    44         header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
    45         status_header( 200 );
     44    if (!headers_sent()) {
     45        header('Content-Type: text/html; charset=' . get_option('blog_charset'));
     46        status_header(200);
    4647    }
    4748
    48     $postID = filter_input( INPUT_GET, 'postID', FILTER_VALIDATE_INT );
    49     $posts = new WP_Query( [ 'p' => $postID ] );
     49    $postID = filter_input(INPUT_GET, 'postID', FILTER_VALIDATE_INT);
     50    $posts = new WP_Query(['p' => $postID]);
    5051    $GLOBALS['wp_query'] = $posts;
    5152
    52     if( have_posts() ) {
    53         while( have_posts() ) {
     53    if (have_posts()) {
     54        while (have_posts()) {
    5455            the_post();
    5556            comments_template();
  • ajax-comments-refueled/tags/1.2.0/readme.txt

    r2610645 r2789926  
    33Tags: comments, ajax
    44Donate link: https://planetjon.ca
     5Stable tag: 1.2.0
    56Requires at least: 4.6
    6 Tested up to: 5.8
     7Tested up to: 6.0.2
    78Requires PHP: 5.4
    8 Stable tag: 1.1.1
    99License: GPL2
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1313
    1414== Description ==
    15 AJAX Comments Refueled allows for pages to be cached without needing to invalidate on new comments,
    16 and is useful for upgrading the default comment system to not rely on page refreshes for replying to comments.
     15AJAX Comments Refueled allows for pages to be cached without needing to invalidate on new comments, and is useful for upgrading the default comment system to not rely on page refreshes for replying to comments.
    1716
    1817This plugin is a zero-dependency implementation. This means that it does not require jQuery.
     18
     19Image by [Gerd Altmann](https://pixabay.com/users/geralt-9301/?utm_source=link-attribution) from Pixabay.
    1920
    2021== How To Use ==
     
    2324== Installation ==
    2425Simply upload the zip using the Add New Plugin feature within WordPress admin as per usual.
     26
     27== Changelog ==
     28
     29= 1.2 =
     30* WordPress error messages are now shown to the user instead of the generic error message.
  • ajax-comments-refueled/tags/1.2.0/scripts.js

    r2610642 r2789926  
    1 ;(function() {
    2 var commentsBlock
     1(function () {
     2  var commentsBlock;
    33
    4 if(!(commentsBlock = document.getElementById('wac-stub'))) {
    5     return
    6 }
     4  if (!(commentsBlock = document.getElementById("wac-stub"))) {
     5    return;
     6  }
    77
    8 var postID = commentsBlock.dataset.postid
     8  var postID = commentsBlock.dataset.postid;
    99
    10 delegateEvent(commentsBlock, 'click', '.comment-reply-link', function(e) {
    11     e.preventDefault()
    12     replyToComment(e.target)
    13 })
     10  delegateEvent(commentsBlock, "click", ".comment-reply-link", function (e) {
     11    e.preventDefault();
     12    replyToComment(e.target);
     13  });
    1414
    15 delegateEvent(commentsBlock, 'submit', '#commentform', function(e) {
    16     e.preventDefault()
    17     submitComment(e.target)
    18 })
     15  delegateEvent(commentsBlock, "submit", "#commentform", function (e) {
     16    e.preventDefault();
     17    submitComment(e.target);
     18  });
    1919
    20 delegateEvent(commentsBlock, 'reset', '#commentform', function(e) {
    21     resetResponder()
    22 })
     20  delegateEvent(commentsBlock, "reset", "#commentform", function (e) {
     21    resetResponder();
     22  });
    2323
    24 if('IntersectionObserver' in window) {
    25     var commentsObserver = new IntersectionObserver(function(changes) {
    26         changes.forEach(change => {
    27             if(change.isIntersecting) {
    28                 loadComments()
    29                 commentsObserver.unobserve(change.target)
    30             }
    31         })
    32     })
     24  if ("IntersectionObserver" in window) {
     25    var commentsObserver = new IntersectionObserver(function (changes) {
     26      changes.forEach((change) => {
     27        if (change.isIntersecting) {
     28          loadComments();
     29          commentsObserver.unobserve(change.target);
     30        }
     31      });
     32    });
    3333
    34     commentsObserver.observe(commentsBlock)
    35 }
    36 else {
    37     loadComments()
    38 }
     34    commentsObserver.observe(commentsBlock);
     35  } else {
     36    loadComments();
     37  }
    3938
    40 function replyToComment(target) {
    41     var link = target
    42     var commentID = link.dataset.commentid
    43     var commentBlockID = link.dataset.belowelement
    44     var respond = document.getElementById('respond')
     39  function replyToComment(target) {
     40    var link = target;
     41    var commentID = link.dataset.commentid;
     42    var commentBlockID = link.dataset.belowelement;
     43    var respond = document.getElementById("respond");
    4544
    46     document.getElementById('comment_parent').value = commentID
    47     document.getElementById(commentBlockID).after(respond)
    48     document.getElementById(commentBlockID).scrollIntoView({behavior: 'smooth'})
    49 }
     45    document.getElementById("comment_parent").value = commentID;
     46    document.getElementById(commentBlockID).after(respond);
     47    document
     48      .getElementById(commentBlockID)
     49      .scrollIntoView({ behavior: "smooth" });
     50  }
    5051
    51 function submitComment(target) {
    52     var form = target
    53     var payload = new FormData(form)
    54     var placeholder = document.createElement('div')
    55     placeholder.style = 'margin:1em'
     52  function submitComment(target) {
     53    var form = target;
     54    var payload = new FormData(form);
     55    var placeholder = document.createElement("div");
     56    placeholder.style = "margin:1em";
    5657
    57     fetch(form.action, {
    58         method: form.method,
    59         redirect: 'manual',
    60         body: payload
    61     })
    62     .then(function(response) {
    63         if(!response.status) {
    64             placeholder.innerHTML = '<p><small>' + wac_env.pending_msg + '</small></p>'
    65             placeholder.innerHTML += payload.get('comment')
    66         }
    67         else {
    68             return Promise.reject()
    69         }
    70     })
    71     .catch(function() {
    72         placeholder.innerHTML = wac_env.error_msg
    73     })
    74     .finally(function() {
    75         var responder = document.getElementById('respond')
    76         responder.before(placeholder)
    77         document.getElementById('commentform').reset()
    78     })
    79 }
     58    fetch(form.action, {
     59      method: form.method,
     60      redirect: "manual",
     61      body: payload,
     62    })
     63      .then(function (response) {
     64        if (!response.status) {
     65          return Promise.resolve({
     66            message: wac_env.pending_msg,
     67            comment: payload.get("comment"),
     68          });
     69        } else if (response.OK) {
     70          return response.text().then(function (html) {
     71            var message = new DOMParser()
     72              .parseFromString(html, "text/html")
     73              .querySelector(".wp-die-message").innerText;
     74            return Promise.resolve({ message: message, comment: "" });
     75          });
     76        } else {
     77          return Promise.reject();
     78        }
     79      })
     80      .catch(function (e) {
     81        return Promise.resolve({ message: wac_env.error_msg, comment: "" });
     82      })
     83      .then(function (bundle) {
     84        placeholder.innerHTML =
     85          "<p><small>" + bundle.message + "</small></p>" + bundle.comment;
     86      })
     87      .finally(function () {
     88        var responder = document.getElementById("respond");
     89        responder.before(placeholder);
     90        document.getElementById("commentform").reset();
     91      });
     92  }
    8093
    81 function resetResponder() {
    82     var respond = document.getElementById('respond')
    83     document.getElementById('comment_parent').value = ''
    84     document.getElementById('respond-anchor').before(respond)
    85 }
     94  function resetResponder() {
     95    var respond = document.getElementById("respond");
     96    document.getElementById("comment_parent").value = "";
     97    document.getElementById("respond-anchor").before(respond);
     98  }
    8699
    87 function loadComments() {
    88     fetch(wac_env.ajaxurl + '?action=wac_load_comments&postID=' + postID)
    89         .then(function(response) {
    90             return response.ok && response.text();
    91         })
    92         .then(function(comments) {
    93             commentsBlock.innerHTML = comments
     100  function loadComments() {
     101    fetch(wac_env.ajaxurl + "?action=wac_load_comments&postID=" + postID)
     102      .then(function (response) {
     103        return response.ok && response.text();
     104      })
     105      .then(function (comments) {
     106        commentsBlock.innerHTML = comments;
    94107
    95             var respond = document.getElementById('respond')
    96             var commentForm = document.getElementById('commentform')
    97             var formActions = commentForm.querySelector('.form-submit')
     108        var respond = document.getElementById("respond");
     109        var commentForm = document.getElementById("commentform");
     110        var formActions = commentForm.querySelector(".form-submit");
    98111
    99             var resetButton = document.createElement('input')
    100             resetButton.type = 'reset'
    101             resetButton.value = 'Cancel'
     112        var resetButton = document.createElement("input");
     113        resetButton.type = "reset";
     114        resetButton.value = "Cancel";
    102115
    103             var respondAnchor = document.createElement('div')
    104             respondAnchor.id = 'respond-anchor'
    105             respond.after(respondAnchor)
    106             formActions.append(resetButton)
    107         })
    108 }
     116        var respondAnchor = document.createElement("div");
     117        respondAnchor.id = "respond-anchor";
     118        respond.after(respondAnchor);
     119        formActions.append(resetButton);
     120      });
     121  }
    109122
    110 function delegateEvent(delegate, event, target, handler) {
    111     delegate.addEventListener(event, function(e) {
    112         e.target.matches(target) && handler(e)
    113     })
    114 }
    115 })()
     123  function delegateEvent(delegate, event, target, handler) {
     124    delegate.addEventListener(event, function (e) {
     125      e.target.matches(target) && handler(e);
     126    });
     127  }
     128})();
  • ajax-comments-refueled/tags/1.2.0/scripts.min.js

    r2610642 r2789926  
    1 !function(){var m,e,t;function n(){fetch(wac_env.ajaxurl+"?action=wac_load_comments&postID="+e).then(function(e){return e.ok&&e.text()}).then(function(e){m.innerHTML=e;var t=document.getElementById("respond"),n=document.getElementById("commentform").querySelector(".form-submit"),o=document.createElement("input");o.type="reset",o.value="Cancel";e=document.createElement("div");e.id="respond-anchor",t.after(e),n.append(o)})}function o(e,t,n,o){e.addEventListener(t,function(e){e.target.matches(n)&&o(e)})}(m=document.getElementById("wac-stub"))&&(e=m.dataset.postid,o(m,"click",".comment-reply-link",function(e){var t,n;e.preventDefault(),t=e.target,n=t.dataset.commentid,e=t.dataset.belowelement,t=document.getElementById("respond"),document.getElementById("comment_parent").value=n,document.getElementById(e).after(t),document.getElementById(e).scrollIntoView({behavior:"smooth"})}),o(m,"submit","#commentform",function(e){var t,n;e.preventDefault(),e=e.target,t=new FormData(e),(n=document.createElement("div")).style="margin:1em",fetch(e.action,{method:e.method,redirect:"manual",body:t}).then(function(e){return e.status?Promise.reject():(n.innerHTML="<p><small>"+wac_env.pending_msg+"</small></p>",void(n.innerHTML+=t.get("comment")))}).catch(function(){n.innerHTML=wac_env.error_msg}).finally(function(){document.getElementById("respond").before(n),document.getElementById("commentform").reset()})}),o(m,"reset","#commentform",function(e){var t;t=document.getElementById("respond"),document.getElementById("comment_parent").value="",document.getElementById("respond-anchor").before(t)}),"IntersectionObserver"in window?(t=new IntersectionObserver(function(e){e.forEach(e=>{e.isIntersecting&&(n(),t.unobserve(e.target))})})).observe(m):n())}();
     1!function(){var e;if(e=document.getElementById("wac-stub")){var t=e.dataset.postid;if(r(e,"click",".comment-reply-link",(function(e){var t,n,o,r,m;e.preventDefault(),t=e.target,o=(n=t).dataset.commentid,r=n.dataset.belowelement,m=document.getElementById("respond"),document.getElementById("comment_parent").value=o,document.getElementById(r).after(m),document.getElementById(r).scrollIntoView({behavior:"smooth"})})),r(e,"submit","#commentform",(function(e){var t,n,o,r;e.preventDefault(),t=e.target,n=t,o=new FormData(n),(r=document.createElement("div")).style="margin:1em",fetch(n.action,{method:n.method,redirect:"manual",body:o}).then((function(e){return e.status?e.OK?e.text().then((function(e){var t=(new DOMParser).parseFromString(e,"text/html").querySelector(".wp-die-message").innerText;return Promise.resolve({message:t,comment:""})})):Promise.reject():Promise.resolve({message:wac_env.pending_msg,comment:o.get("comment")})})).catch((function(e){return Promise.resolve({message:wac_env.error_msg,comment:""})})).then((function(e){r.innerHTML="<p><small>"+e.message+"</small></p>"+e.comment})).finally((function(){document.getElementById("respond").before(r),document.getElementById("commentform").reset()}))})),r(e,"reset","#commentform",(function(e){var t;t=document.getElementById("respond"),document.getElementById("comment_parent").value="",document.getElementById("respond-anchor").before(t)})),"IntersectionObserver"in window){var n=new IntersectionObserver((function(e){e.forEach((e=>{e.isIntersecting&&(o(),n.unobserve(e.target))}))}));n.observe(e)}else o()}function o(){fetch(wac_env.ajaxurl+"?action=wac_load_comments&postID="+t).then((function(e){return e.ok&&e.text()})).then((function(t){e.innerHTML=t;var n=document.getElementById("respond"),o=document.getElementById("commentform").querySelector(".form-submit"),r=document.createElement("input");r.type="reset",r.value="Cancel";var m=document.createElement("div");m.id="respond-anchor",n.after(m),o.append(r)}))}function r(e,t,n,o){e.addEventListener(t,(function(e){e.target.matches(n)&&o(e)}))}}();
  • ajax-comments-refueled/trunk/plugin.php

    r2610646 r2789926  
    55Plugin URI: https://planetjon.ca/projects/ajax-comments/
    66Description: Make your comment system fly with a full AJAX implementation of your existing template.
    7 Version: 1.1.1
     7Version: 1.2.0
    88Requires at least: 4.6
    9 Tested up to: 5.8
     9Tested up to: 6.0.2
    1010Requires PHP: 5.4
    1111Author: Jonathan Weatherhead
     
    1919use \WP_Query;
    2020
    21 add_action( 'wp_enqueue_scripts', function() {
    22     if( is_singular() ) {
    23         wp_dequeue_script( 'reply-comments' );
    24         wp_enqueue_script( 'wac-script', plugins_url( 'scripts.min.js', __FILE__ ), [], false, true );
    25         wp_localize_script( 'wac-script', 'wac_env', [
    26             'ajaxurl' => admin_url( 'admin-ajax.php' ),
    27             'pending_msg' => __( '&#x29D6; Thanks! Your comment will be available shortly. If you dont see it immediately, it is pending moderation.', 'wp-ajax-comments' ),
    28             'error_msg' => __( '&#x274C; Something went wrong. Please try again later.', 'wp-ajax-comments' )
     21add_action('wp_enqueue_scripts', function () {
     22    if (is_singular()) {
     23        wp_dequeue_script('reply-comments');
     24        wp_enqueue_script('wac-script', plugins_url('scripts.min.js', __FILE__), [], false, true);
     25        wp_localize_script('wac-script', 'wac_env', [
     26            'ajaxurl' => admin_url('admin-ajax.php'),
     27            'pending_msg' => __('Your comment is awaiting moderation.'), // WP string
     28            'error_msg' => __('Something went wrong. Please try again later.', 'wp-ajax-comments')
    2929        ]);
    3030    }
    31 }, 11 );
     31}, 11);
    3232
    33 add_filter( 'comments_template', function( $theme_template ) {
    34     return !wp_doing_ajax() ? plugin_dir_path( __FILE__ ) . 'comments.php' : $theme_template;
    35 } );
     33add_filter('comments_template', function ($theme_template) {
     34    return !wp_doing_ajax() ? plugin_dir_path(__FILE__) . 'comments.php' : $theme_template;
     35});
    3636
    37 add_action( 'wp_ajax_wac_load_comments', __NAMESPACE__ . '\wac_load_comments' );
    38 add_action( 'wp_ajax_nopriv_wac_load_comments', __NAMESPACE__ . '\wac_load_comments' );
     37add_action('wp_ajax_wac_load_comments', __NAMESPACE__ . '\wac_load_comments');
     38add_action('wp_ajax_nopriv_wac_load_comments', __NAMESPACE__ . '\wac_load_comments');
    3939
    40 function wac_load_comments() {
     40function wac_load_comments()
     41{
    4142    global $post;
    4243
    43     if( !headers_sent() ) {
    44         header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
    45         status_header( 200 );
     44    if (!headers_sent()) {
     45        header('Content-Type: text/html; charset=' . get_option('blog_charset'));
     46        status_header(200);
    4647    }
    4748
    48     $postID = filter_input( INPUT_GET, 'postID', FILTER_VALIDATE_INT );
    49     $posts = new WP_Query( [ 'p' => $postID ] );
     49    $postID = filter_input(INPUT_GET, 'postID', FILTER_VALIDATE_INT);
     50    $posts = new WP_Query(['p' => $postID]);
    5051    $GLOBALS['wp_query'] = $posts;
    5152
    52     if( have_posts() ) {
    53         while( have_posts() ) {
     53    if (have_posts()) {
     54        while (have_posts()) {
    5455            the_post();
    5556            comments_template();
  • ajax-comments-refueled/trunk/readme.txt

    r2610645 r2789926  
    33Tags: comments, ajax
    44Donate link: https://planetjon.ca
     5Stable tag: 1.2.0
    56Requires at least: 4.6
    6 Tested up to: 5.8
     7Tested up to: 6.0.2
    78Requires PHP: 5.4
    8 Stable tag: 1.1.1
    99License: GPL2
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1313
    1414== Description ==
    15 AJAX Comments Refueled allows for pages to be cached without needing to invalidate on new comments,
    16 and is useful for upgrading the default comment system to not rely on page refreshes for replying to comments.
     15AJAX Comments Refueled allows for pages to be cached without needing to invalidate on new comments, and is useful for upgrading the default comment system to not rely on page refreshes for replying to comments.
    1716
    1817This plugin is a zero-dependency implementation. This means that it does not require jQuery.
     18
     19Image by [Gerd Altmann](https://pixabay.com/users/geralt-9301/?utm_source=link-attribution) from Pixabay.
    1920
    2021== How To Use ==
     
    2324== Installation ==
    2425Simply upload the zip using the Add New Plugin feature within WordPress admin as per usual.
     26
     27== Changelog ==
     28
     29= 1.2 =
     30* WordPress error messages are now shown to the user instead of the generic error message.
  • ajax-comments-refueled/trunk/scripts.js

    r2610642 r2789926  
    1 ;(function() {
    2 var commentsBlock
     1(function () {
     2  var commentsBlock;
    33
    4 if(!(commentsBlock = document.getElementById('wac-stub'))) {
    5     return
    6 }
     4  if (!(commentsBlock = document.getElementById("wac-stub"))) {
     5    return;
     6  }
    77
    8 var postID = commentsBlock.dataset.postid
     8  var postID = commentsBlock.dataset.postid;
    99
    10 delegateEvent(commentsBlock, 'click', '.comment-reply-link', function(e) {
    11     e.preventDefault()
    12     replyToComment(e.target)
    13 })
     10  delegateEvent(commentsBlock, "click", ".comment-reply-link", function (e) {
     11    e.preventDefault();
     12    replyToComment(e.target);
     13  });
    1414
    15 delegateEvent(commentsBlock, 'submit', '#commentform', function(e) {
    16     e.preventDefault()
    17     submitComment(e.target)
    18 })
     15  delegateEvent(commentsBlock, "submit", "#commentform", function (e) {
     16    e.preventDefault();
     17    submitComment(e.target);
     18  });
    1919
    20 delegateEvent(commentsBlock, 'reset', '#commentform', function(e) {
    21     resetResponder()
    22 })
     20  delegateEvent(commentsBlock, "reset", "#commentform", function (e) {
     21    resetResponder();
     22  });
    2323
    24 if('IntersectionObserver' in window) {
    25     var commentsObserver = new IntersectionObserver(function(changes) {
    26         changes.forEach(change => {
    27             if(change.isIntersecting) {
    28                 loadComments()
    29                 commentsObserver.unobserve(change.target)
    30             }
    31         })
    32     })
     24  if ("IntersectionObserver" in window) {
     25    var commentsObserver = new IntersectionObserver(function (changes) {
     26      changes.forEach((change) => {
     27        if (change.isIntersecting) {
     28          loadComments();
     29          commentsObserver.unobserve(change.target);
     30        }
     31      });
     32    });
    3333
    34     commentsObserver.observe(commentsBlock)
    35 }
    36 else {
    37     loadComments()
    38 }
     34    commentsObserver.observe(commentsBlock);
     35  } else {
     36    loadComments();
     37  }
    3938
    40 function replyToComment(target) {
    41     var link = target
    42     var commentID = link.dataset.commentid
    43     var commentBlockID = link.dataset.belowelement
    44     var respond = document.getElementById('respond')
     39  function replyToComment(target) {
     40    var link = target;
     41    var commentID = link.dataset.commentid;
     42    var commentBlockID = link.dataset.belowelement;
     43    var respond = document.getElementById("respond");
    4544
    46     document.getElementById('comment_parent').value = commentID
    47     document.getElementById(commentBlockID).after(respond)
    48     document.getElementById(commentBlockID).scrollIntoView({behavior: 'smooth'})
    49 }
     45    document.getElementById("comment_parent").value = commentID;
     46    document.getElementById(commentBlockID).after(respond);
     47    document
     48      .getElementById(commentBlockID)
     49      .scrollIntoView({ behavior: "smooth" });
     50  }
    5051
    51 function submitComment(target) {
    52     var form = target
    53     var payload = new FormData(form)
    54     var placeholder = document.createElement('div')
    55     placeholder.style = 'margin:1em'
     52  function submitComment(target) {
     53    var form = target;
     54    var payload = new FormData(form);
     55    var placeholder = document.createElement("div");
     56    placeholder.style = "margin:1em";
    5657
    57     fetch(form.action, {
    58         method: form.method,
    59         redirect: 'manual',
    60         body: payload
    61     })
    62     .then(function(response) {
    63         if(!response.status) {
    64             placeholder.innerHTML = '<p><small>' + wac_env.pending_msg + '</small></p>'
    65             placeholder.innerHTML += payload.get('comment')
    66         }
    67         else {
    68             return Promise.reject()
    69         }
    70     })
    71     .catch(function() {
    72         placeholder.innerHTML = wac_env.error_msg
    73     })
    74     .finally(function() {
    75         var responder = document.getElementById('respond')
    76         responder.before(placeholder)
    77         document.getElementById('commentform').reset()
    78     })
    79 }
     58    fetch(form.action, {
     59      method: form.method,
     60      redirect: "manual",
     61      body: payload,
     62    })
     63      .then(function (response) {
     64        if (!response.status) {
     65          return Promise.resolve({
     66            message: wac_env.pending_msg,
     67            comment: payload.get("comment"),
     68          });
     69        } else if (response.OK) {
     70          return response.text().then(function (html) {
     71            var message = new DOMParser()
     72              .parseFromString(html, "text/html")
     73              .querySelector(".wp-die-message").innerText;
     74            return Promise.resolve({ message: message, comment: "" });
     75          });
     76        } else {
     77          return Promise.reject();
     78        }
     79      })
     80      .catch(function (e) {
     81        return Promise.resolve({ message: wac_env.error_msg, comment: "" });
     82      })
     83      .then(function (bundle) {
     84        placeholder.innerHTML =
     85          "<p><small>" + bundle.message + "</small></p>" + bundle.comment;
     86      })
     87      .finally(function () {
     88        var responder = document.getElementById("respond");
     89        responder.before(placeholder);
     90        document.getElementById("commentform").reset();
     91      });
     92  }
    8093
    81 function resetResponder() {
    82     var respond = document.getElementById('respond')
    83     document.getElementById('comment_parent').value = ''
    84     document.getElementById('respond-anchor').before(respond)
    85 }
     94  function resetResponder() {
     95    var respond = document.getElementById("respond");
     96    document.getElementById("comment_parent").value = "";
     97    document.getElementById("respond-anchor").before(respond);
     98  }
    8699
    87 function loadComments() {
    88     fetch(wac_env.ajaxurl + '?action=wac_load_comments&postID=' + postID)
    89         .then(function(response) {
    90             return response.ok && response.text();
    91         })
    92         .then(function(comments) {
    93             commentsBlock.innerHTML = comments
     100  function loadComments() {
     101    fetch(wac_env.ajaxurl + "?action=wac_load_comments&postID=" + postID)
     102      .then(function (response) {
     103        return response.ok && response.text();
     104      })
     105      .then(function (comments) {
     106        commentsBlock.innerHTML = comments;
    94107
    95             var respond = document.getElementById('respond')
    96             var commentForm = document.getElementById('commentform')
    97             var formActions = commentForm.querySelector('.form-submit')
     108        var respond = document.getElementById("respond");
     109        var commentForm = document.getElementById("commentform");
     110        var formActions = commentForm.querySelector(".form-submit");
    98111
    99             var resetButton = document.createElement('input')
    100             resetButton.type = 'reset'
    101             resetButton.value = 'Cancel'
     112        var resetButton = document.createElement("input");
     113        resetButton.type = "reset";
     114        resetButton.value = "Cancel";
    102115
    103             var respondAnchor = document.createElement('div')
    104             respondAnchor.id = 'respond-anchor'
    105             respond.after(respondAnchor)
    106             formActions.append(resetButton)
    107         })
    108 }
     116        var respondAnchor = document.createElement("div");
     117        respondAnchor.id = "respond-anchor";
     118        respond.after(respondAnchor);
     119        formActions.append(resetButton);
     120      });
     121  }
    109122
    110 function delegateEvent(delegate, event, target, handler) {
    111     delegate.addEventListener(event, function(e) {
    112         e.target.matches(target) && handler(e)
    113     })
    114 }
    115 })()
     123  function delegateEvent(delegate, event, target, handler) {
     124    delegate.addEventListener(event, function (e) {
     125      e.target.matches(target) && handler(e);
     126    });
     127  }
     128})();
  • ajax-comments-refueled/trunk/scripts.min.js

    r2610642 r2789926  
    1 !function(){var m,e,t;function n(){fetch(wac_env.ajaxurl+"?action=wac_load_comments&postID="+e).then(function(e){return e.ok&&e.text()}).then(function(e){m.innerHTML=e;var t=document.getElementById("respond"),n=document.getElementById("commentform").querySelector(".form-submit"),o=document.createElement("input");o.type="reset",o.value="Cancel";e=document.createElement("div");e.id="respond-anchor",t.after(e),n.append(o)})}function o(e,t,n,o){e.addEventListener(t,function(e){e.target.matches(n)&&o(e)})}(m=document.getElementById("wac-stub"))&&(e=m.dataset.postid,o(m,"click",".comment-reply-link",function(e){var t,n;e.preventDefault(),t=e.target,n=t.dataset.commentid,e=t.dataset.belowelement,t=document.getElementById("respond"),document.getElementById("comment_parent").value=n,document.getElementById(e).after(t),document.getElementById(e).scrollIntoView({behavior:"smooth"})}),o(m,"submit","#commentform",function(e){var t,n;e.preventDefault(),e=e.target,t=new FormData(e),(n=document.createElement("div")).style="margin:1em",fetch(e.action,{method:e.method,redirect:"manual",body:t}).then(function(e){return e.status?Promise.reject():(n.innerHTML="<p><small>"+wac_env.pending_msg+"</small></p>",void(n.innerHTML+=t.get("comment")))}).catch(function(){n.innerHTML=wac_env.error_msg}).finally(function(){document.getElementById("respond").before(n),document.getElementById("commentform").reset()})}),o(m,"reset","#commentform",function(e){var t;t=document.getElementById("respond"),document.getElementById("comment_parent").value="",document.getElementById("respond-anchor").before(t)}),"IntersectionObserver"in window?(t=new IntersectionObserver(function(e){e.forEach(e=>{e.isIntersecting&&(n(),t.unobserve(e.target))})})).observe(m):n())}();
     1!function(){var e;if(e=document.getElementById("wac-stub")){var t=e.dataset.postid;if(r(e,"click",".comment-reply-link",(function(e){var t,n,o,r,m;e.preventDefault(),t=e.target,o=(n=t).dataset.commentid,r=n.dataset.belowelement,m=document.getElementById("respond"),document.getElementById("comment_parent").value=o,document.getElementById(r).after(m),document.getElementById(r).scrollIntoView({behavior:"smooth"})})),r(e,"submit","#commentform",(function(e){var t,n,o,r;e.preventDefault(),t=e.target,n=t,o=new FormData(n),(r=document.createElement("div")).style="margin:1em",fetch(n.action,{method:n.method,redirect:"manual",body:o}).then((function(e){return e.status?e.OK?e.text().then((function(e){var t=(new DOMParser).parseFromString(e,"text/html").querySelector(".wp-die-message").innerText;return Promise.resolve({message:t,comment:""})})):Promise.reject():Promise.resolve({message:wac_env.pending_msg,comment:o.get("comment")})})).catch((function(e){return Promise.resolve({message:wac_env.error_msg,comment:""})})).then((function(e){r.innerHTML="<p><small>"+e.message+"</small></p>"+e.comment})).finally((function(){document.getElementById("respond").before(r),document.getElementById("commentform").reset()}))})),r(e,"reset","#commentform",(function(e){var t;t=document.getElementById("respond"),document.getElementById("comment_parent").value="",document.getElementById("respond-anchor").before(t)})),"IntersectionObserver"in window){var n=new IntersectionObserver((function(e){e.forEach((e=>{e.isIntersecting&&(o(),n.unobserve(e.target))}))}));n.observe(e)}else o()}function o(){fetch(wac_env.ajaxurl+"?action=wac_load_comments&postID="+t).then((function(e){return e.ok&&e.text()})).then((function(t){e.innerHTML=t;var n=document.getElementById("respond"),o=document.getElementById("commentform").querySelector(".form-submit"),r=document.createElement("input");r.type="reset",r.value="Cancel";var m=document.createElement("div");m.id="respond-anchor",n.after(m),o.append(r)}))}function r(e,t,n,o){e.addEventListener(t,(function(e){e.target.matches(n)&&o(e)}))}}();
Note: See TracChangeset for help on using the changeset viewer.