From e7039f1e8139807bddcf8e4db1722ee1536b4612 Mon Sep 17 00:00:00 2001
From: Kevin Pliester <5339107+pixelbart@users.noreply.github.com>
Date: Fri, 30 Jul 2021 16:10:32 +0200
Subject: [PATCH] 4.4.43
---
core/assets/js/helpful.js | 276 +++++++++++++++++---------------
core/modules/class-frontend.php | 58 ++++++-
helpful.php | 2 +-
readme.txt | 2 +-
4 files changed, 207 insertions(+), 131 deletions(-)
diff --git a/core/assets/js/helpful.js b/core/assets/js/helpful.js
index d10ab44..ba9ebec 100644
--- a/core/assets/js/helpful.js
+++ b/core/assets/js/helpful.js
@@ -1,126 +1,152 @@
-(function ($) {
-
- "use strict";
-
- const HelpfulPlugin = {
- el: ".helpful",
- vote: "helpful_save_vote",
- feedback: "helpful_save_feedback",
- helpful: helpful,
- initPlugin: function () {
- const self = this;
-
- if (self.el.length < 1) {
- return;
- }
-
- $(document).on("click", ".helpful .helpful-controls button", function (e) {
- e.preventDefault();
-
- var currentButton = $(this);
- var currentForm = $(currentButton).closest(".helpful");
- var ajaxData = {};
-
- $.extend(ajaxData, helpful.ajax_data);
- $.extend(ajaxData, $(currentButton).data());
- ajaxData.action = self.vote;
-
- var request = self.ajaxRequest(ajaxData);
-
- request.done(function (response) {
- $(currentForm).find(".helpful-header").remove();
- $(currentForm).find(".helpful-controls").remove();
- $(currentForm).find(".helpful-footer").remove();
- $(currentForm).find(".helpful-content").html(response);
- self.feedbackForm(currentForm);
- });
- });
-
- $.each($(".helpful"), function () {
- var currentContainer = $(this);
-
- if ($(currentContainer).is(".helpful-prevent-form")) {
- self.feedbackForm(currentContainer);
- }
-
- if ($(currentContainer).find(".helpful-toggle-feedback").length) {
- $(currentContainer).find(".helpful-toggle-feedback").click(function (e) {
- e.preventDefault();
- $(this).parent().find("div").removeAttr("hidden");
- $(this).remove();
- });
- }
- });
- },
- feedbackForm: function (currentForm) {
- var self = this;
-
- $(currentForm).find(".helpful-cancel").unbind().click(function (e) {
- e.preventDefault();
-
- var ajaxData = {
- "action": "helpful_save_feedback",
- "cancel": 1,
- "type": $(currentForm).find("[name='type']").val(),
- "_wpnonce": $(currentForm).find("[name='_wpnonce']").val(),
- "post_id": $(currentForm).find("[name='post_id']").val(),
- };
-
- var request = self.ajaxRequest(ajaxData);
-
- request.done(function (response) {
- $(currentForm).find(".helpful-content").html(response);
- });
- });
-
- $(currentForm).on("submit", ".helpful-feedback-form", function (e) {
- e.preventDefault();
-
- var formData = $(this).serializeArray();
- var ajaxData = {};
-
- $.each(formData, function (i, field) {
- ajaxData[field.name] = field.value;
- });
-
- $(currentForm).remove(".danger");
-
- let key;
- let required = [];
-
- $(currentForm).find("[required]").each(function () {
- if (!$.trim(this.value).length) {
- $(this).after("" + helpful.translations.fieldIsRequired + "");
- required.push(key);
- }
- });
-
- if (required.length) {
- return;
- }
-
- var request = self.ajaxRequest(ajaxData);
-
- request.done(function (response) {
- $(currentForm).find(".helpful-content").html(response);
- });
- });
- },
- ajaxRequest: function (data) {
- if (typeof this.helpful.ajax_session !== "undefined") {
- data.session = this.helpful.ajax_session;
- }
-
- return $.ajax({
- url: this.helpful.ajax_url,
- data: data,
- method: "POST",
- });
- },
- };
-
- $(function () {
- HelpfulPlugin.initPlugin();
- });
-
+(function($) {
+
+ "use strict";
+
+ const HelpfulPlugin = {
+ el: ".helpful",
+ vote: "helpful_save_vote",
+ feedback: "helpful_save_feedback",
+ helpful: helpful,
+ initPlugin: function() {
+ const self = this;
+
+ if (window.performance) {
+ if (2 === performance.navigation.type) {
+ self.hasUserVoted();
+ }
+ }
+
+ if (self.el.length < 1) {
+ return;
+ }
+
+ $(document).on("click", ".helpful .helpful-controls button", function(e) {
+ e.preventDefault();
+
+ var currentButton = $(this);
+ var currentForm = $(currentButton).closest(".helpful");
+ var ajaxData = {};
+
+ $.extend(ajaxData, helpful.ajax_data);
+ $.extend(ajaxData, $(currentButton).data());
+ ajaxData.action = self.vote;
+
+ var request = self.ajaxRequest(ajaxData);
+
+ request.done(function(response) {
+ $(currentForm).find(".helpful-header").remove();
+ $(currentForm).find(".helpful-controls").remove();
+ $(currentForm).find(".helpful-footer").remove();
+ $(currentForm).find(".helpful-content").html(response);
+ self.feedbackForm(currentForm);
+ });
+ });
+
+ $.each($(".helpful"), function() {
+ var currentContainer = $(this);
+
+ if ($(currentContainer).is(".helpful-prevent-form")) {
+ self.feedbackForm(currentContainer);
+ }
+
+ if ($(currentContainer).find(".helpful-toggle-feedback").length) {
+ $(currentContainer).find(".helpful-toggle-feedback").click(function(e) {
+ e.preventDefault();
+ $(this).parent().find("div").removeAttr("hidden");
+ $(this).remove();
+ });
+ }
+ });
+ },
+ feedbackForm: function(currentForm) {
+ var self = this;
+
+ $(currentForm).find(".helpful-cancel").unbind().click(function(e) {
+ e.preventDefault();
+
+ var ajaxData = {
+ "action": "helpful_save_feedback",
+ "cancel": 1,
+ "type": $(currentForm).find("[name='type']").val(),
+ "_wpnonce": $(currentForm).find("[name='_wpnonce']").val(),
+ "post_id": $(currentForm).find("[name='post_id']").val(),
+ };
+
+ var request = self.ajaxRequest(ajaxData);
+
+ request.done(function(response) {
+ $(currentForm).find(".helpful-content").html(response);
+ });
+ });
+
+ $(currentForm).on("submit", ".helpful-feedback-form", function(e) {
+ e.preventDefault();
+
+ var formData = $(this).serializeArray();
+ var ajaxData = {};
+
+ $.each(formData, function(i, field) {
+ ajaxData[field.name] = field.value;
+ });
+
+ $(currentForm).remove(".danger");
+
+ let key;
+ let required = [];
+
+ $(currentForm).find("[required]").each(function() {
+ if (!$.trim(this.value).length) {
+ $(this).after("" + helpful.translations.fieldIsRequired + "");
+ required.push(key);
+ }
+ });
+
+ if (required.length) {
+ return;
+ }
+
+ var request = self.ajaxRequest(ajaxData);
+
+ request.done(function(response) {
+ $(currentForm).find(".helpful-content").html(response);
+ });
+ });
+ },
+ hasUserVoted: function() {
+ const self = this;
+
+ let ajaxData = this.helpful.user_voted;
+
+ $(self.el).hide();
+
+ let request = self.ajaxRequest(ajaxData);
+
+ request.done(function(response) {
+ if (response.success) {
+ $(self.el).remove();
+ } else {
+ $(self.el).show();
+ }
+ });
+
+ return;
+ },
+ ajaxRequest: function(data) {
+ if (typeof this.helpful.ajax_session !== "undefined") {
+ data.session = this.helpful.ajax_session;
+ }
+
+ return $.ajax({
+ url: this.helpful.ajax_url,
+ data: data,
+ method: "POST",
+ cache: false,
+ });
+ },
+ };
+
+ $(function() {
+ HelpfulPlugin.initPlugin();
+ });
+
})(jQuery);
\ No newline at end of file
diff --git a/core/modules/class-frontend.php b/core/modules/class-frontend.php
index 3ebb082..00e8c7a 100644
--- a/core/modules/class-frontend.php
+++ b/core/modules/class-frontend.php
@@ -58,6 +58,9 @@ public function __construct()
add_shortcode('helpful-feedback', [ & $this, 'helpful_feedback']);
add_filter('init', [ & $this, 'filter_nonces']);
+
+ add_action('wp_ajax_helpful_has_user_voted', [ & $this, 'has_user_voted']);
+ add_action('wp_ajax_nopriv_helpful_has_user_voted', [ & $this, 'has_user_voted']);
}
/**
@@ -117,6 +120,7 @@ public function default_themes($themes)
/**
* Enqueue styles and scripts
*
+ * @global $post
* @return void
*/
public function enqueue_scripts()
@@ -146,17 +150,27 @@ public function enqueue_scripts()
$file = Helper::plugins_url('core/assets/js/helpful.js');
wp_enqueue_script('helpful', $file, ['jquery'], $plugin['Version'], true);
- $user = Helpers\User::get_user();
- $nonce = wp_create_nonce('helpful_frontend_nonce');
+ global $post;
+
+ $post_id = (isset($post->ID)) ? $post->ID : 0;
+ $user_id = Helpers\User::get_user();
+
$vars = [
'ajax_url' => admin_url('admin-ajax.php'),
'ajax_data' => [
- 'user_id' => $user,
- '_wpnonce' => $nonce,
+ 'user_id' => $user_id,
+ '_wpnonce' => wp_create_nonce('helpful_frontend_nonce'),
],
'translations' => [
'fieldIsRequired' => __('This field is required.', 'helpful'),
],
+ 'user_voted' => [
+ 'user_id' => $user_id,
+ 'post_id' => $post_id,
+ 'action' => 'helpful_has_user_voted',
+ '_wpnonce' => wp_create_nonce('helpful_has_user_voted'),
+ ],
+ 'post_id' => $post_id,
];
if (isset($_SESSION)) {
@@ -598,4 +612,40 @@ public function helpful_feedback($atts)
return '';
}
+
+ /**
+ * @return void
+ */
+ public function has_user_voted()
+ {
+ check_ajax_referer('helpful_has_user_voted');
+
+ $errors = [];
+ $post_id = null;
+ $user_id = null;
+
+ if (isset($_REQUEST['post_id']) && is_numeric($_REQUEST['post_id'])) {
+ $post_id = intval($_REQUEST['post_id']);
+ } else {
+ $errors[] = _x('No post has been found.', 'ajax error message', 'helpful');
+ }
+
+ if (isset($_REQUEST['user_id']) && '' !== trim($_REQUEST['user_id'])) {
+ $user_id = sanitize_text_field(wp_unslash($_REQUEST['user_id']));
+ } else {
+ $errors[] = _x('No user has been found.', 'ajax error message', 'helpful');
+ }
+
+ if (!empty($errors)) {
+ wp_send_json_error($errors);
+ }
+
+ $status = Helpers\User::check_user($user_id, $post_id);
+
+ if (!$status) {
+ $status = 0;
+ }
+
+ wp_send_json_success($status);
+ }
}
diff --git a/helpful.php b/helpful.php
index 80ffe53..14a37bd 100644
--- a/helpful.php
+++ b/helpful.php
@@ -2,7 +2,7 @@
/**
* Plugin Name: Helpful
* Description: Add a fancy feedback form under your posts or post-types and ask your visitors a question. Give them the abbility to vote with yes or no.
- * Version: 4.4.42
+ * Version: 4.4.43
* Author: Pixelbart
* Author URI: https://pixelbart.de
* Text Domain: helpful
diff --git a/readme.txt b/readme.txt
index b735da3..929b940 100644
--- a/readme.txt
+++ b/readme.txt
@@ -5,7 +5,7 @@ Tags: helpful, poll, feedback, reviews, vote, review, voting
Requires at least: 4.6
Tested up to: 5.8
Requires PHP: 5.6.20
-Stable tag: 4.4.42
+Stable tag: 4.4.43
License: MIT License
License URI: https://opensource.org/licenses/MIT