Advertisement
geminilabs

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

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