geminilabs

[site-reviews] insert review into the page after submission

Jun 29th, 2021 (edited)
258
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Runs after a review is submitted
  3.  */
  4. add_filter('site-reviews/enqueue/public/inline-script/after', function () {
  5.     return "GLSR.Event.on('site-reviews/form/handle', (data) => {
  6.        let request = {
  7.            body: 'action=get_review&id=' + data.review.ID,
  8.            headers: { 'Content-Type': 'application/x-www-form-urlencoded;' },
  9.            method: 'POST',
  10.        };
  11.        fetch(GLSR.ajaxurl, request)
  12.            .then(response => response.json())
  13.            .then(response => {
  14.                if (response.success) {
  15.                    const reviewHtml = response.data;
  16.                    // @todo insert the reviewHtml string into your page
  17.                }
  18.            })
  19.            .catch(error => console.error(error))
  20.    })";
  21. });
  22.  
  23. /**
  24.  * Get the review HTML with AJAX
  25.  */
  26. function glsr_ajax_get_review() {
  27.     $review = glsr_get_review(filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT));
  28.     if ($review->isValid()) {
  29.         wp_send_json_success((string) $review);
  30.     }
  31.     wp_send_json_error();
  32. }
  33. add_action('wp_ajax_get_review', 'glsr_ajax_get_review');
  34. add_action('wp_ajax_nopriv_get_review', 'glsr_ajax_get_review');
  35.  
RAW Paste Data Copied