Skip to content

Commit

Permalink
4.4.43
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelbart committed Jul 30, 2021
1 parent 446f5d0 commit e7039f1
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 131 deletions.
276 changes: 151 additions & 125 deletions core/assets/js/helpful.js
Original file line number Diff line number Diff line change
@@ -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("<req class=\"danger\">" + helpful.translations.fieldIsRequired + "</req>");
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("<req class=\"danger\">" + helpful.translations.fieldIsRequired + "</req>");
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);
58 changes: 54 additions & 4 deletions core/modules/class-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

/**
Expand Down Expand Up @@ -117,6 +120,7 @@ public function default_themes($themes)
/**
* Enqueue styles and scripts
*
* @global $post
* @return void
*/
public function enqueue_scripts()
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion helpful.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit e7039f1

Please sign in to comment.