Skip to content

Commit

Permalink
4.4.35
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelbart committed Jun 25, 2021
1 parent de5b464 commit 841845f
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 133 deletions.
286 changes: 156 additions & 130 deletions core/assets/js/admin-feedback.js
Original file line number Diff line number Diff line change
@@ -1,132 +1,158 @@
(function ($) {

"use strict";

const HelpfulAdminFeedback = {
loader: "<div class=\"helpful_loader\"><i class=\"dashicons dashicons-update\"></i></div>",
initClass: function () {
this.resetFeedback();
this.deleteFeedbackItem();
this.changeFeedbackFilter();
this.exportFeedback();
},
resetFeedback: function () {
const self = this;
const filter_form = $(".helpful-admin-filter");

if ($("[name='post_id']").length) {
$(".helpful-reset").show();
}

$(".helpful-reset").on("click", function (e) {
e.preventDefault();

$(this).hide();
$("[name='post_id']").remove();

let ajax_data = $(filter_form).serializeArray();

self.getFeedackItems(ajax_data);
});
},
getFeedackItems: function (ajax_data) {
const self = this;
const filter_form = $(".helpful-admin-filter");
const container = $(".helpful-admin-feedback");

let request;
let data;

$(container).html(self.loader);

request = self.ajaxRequest(ajax_data);

request.done(function (response) {
$(container).html(response);

$(container).find("[data-page]").unbind().click(function (e) {
let page = $(this).data("page");
$(filter_form).find("[name='paginate']").val(page);
$(filter_form).change();
});
});
},
changeFeedbackFilter: function () {
const self = this;
const filter_form = $(".helpful-admin-filter");

let ajax_data = $(filter_form).serializeArray();
self.getFeedackItems(ajax_data);

$(filter_form).find("[name='filter']").on("change", function () {
$(filter_form).find("[name='paginate']").val(1);
});

$(filter_form).on("change", function (e) {
e.preventDefault();
let ajax_data = $(this).serializeArray();
self.getFeedackItems(ajax_data);
return false;
});
},
deleteFeedbackItem: function () {
const self = this;

let request;
let data;
let button;

$(document).on("click", ".helpful-delete-item", function (e) {

button = $(this);

data = {
action: "helpful_remove_feedback",
_wpnonce: helpful_admin_feedback.nonce,
feedback_id: $(button).data("id"),
};

request = self.ajaxRequest(data);
$(button).closest("article").fadeOut();
});
},
exportFeedback: function () {
const self = this;

if ($(".helpful-export").length < 1) {
return;
}

$(".helpful-export").unbind("click").on("click", function (e) {
e.preventDefault();

let current_button = $(this);
let ajax_data = {
action: "helpful_export_feedback",
_wpnonce: helpful_admin_feedback.nonce,
type: $(current_button).data("type"),
};

let request = self.ajaxRequest(ajax_data);

request.done(function (response) {
if ("success" === response.status) {
window.location.href = response.file;
}
});
});
},
ajaxRequest: function (data) {
return $.ajax({
url: helpful_admin_feedback.ajax_url,
data: data,
method: "POST",
});
},
};

$(function () {
HelpfulAdminFeedback.initClass();
});
(function($) {

"use strict";

const HelpfulAdminFeedback = {
loader: "<div class=\"helpful_loader\"><i class=\"dashicons dashicons-update\"></i></div>",
initClass: function() {
this.resetFeedback();
this.deleteFeedbackItem();
this.changeFeedbackFilter();
this.exportFeedback();
this.deleteFeedback();
},
resetFeedback: function() {
const self = this;
const filter_form = $(".helpful-admin-filter");

if ($("[name='post_id']").length) {
$(".helpful-reset").show();
}

$(".helpful-reset").on("click", function(e) {
e.preventDefault();

$(this).hide();
$("[name='post_id']").remove();

let ajax_data = $(filter_form).serializeArray();

self.getFeedackItems(ajax_data);
});
},
getFeedackItems: function(ajax_data) {
const self = this;
const filter_form = $(".helpful-admin-filter");
const container = $(".helpful-admin-feedback");

let request;
let data;

$(container).html(self.loader);

request = self.ajaxRequest(ajax_data);

request.done(function(response) {
$(container).html(response);

$(container).find("[data-page]").unbind().click(function(e) {
let page = $(this).data("page");
$(filter_form).find("[name='paginate']").val(page);
$(filter_form).change();
});
});
},
changeFeedbackFilter: function() {
const self = this;
const filter_form = $(".helpful-admin-filter");

let ajax_data = $(filter_form).serializeArray();
self.getFeedackItems(ajax_data);

$(filter_form).find("[name='filter']").on("change", function() {
$(filter_form).find("[name='paginate']").val(1);
});

$(filter_form).on("change", function(e) {
e.preventDefault();
let ajax_data = $(this).serializeArray();
self.getFeedackItems(ajax_data);
return false;
});
},
deleteFeedbackItem: function() {
const self = this;

let request;
let data;
let button;

$(document).on("click", ".helpful-delete-item", function(e) {

button = $(this);

data = {
action: "helpful_remove_feedback",
_wpnonce: helpful_admin_feedback.nonce,
feedback_id: $(button).data("id"),
};

request = self.ajaxRequest(data);
$(button).closest("article").fadeOut();
});
},
exportFeedback: function() {
const self = this;

if ($(".helpful-export").length < 1) {
return;
}

$(".helpful-export").unbind("click").on("click", function(e) {
e.preventDefault();

let current_button = $(this);
let ajax_data = {
action: "helpful_export_feedback",
_wpnonce: helpful_admin_feedback.nonce,
type: $(current_button).data("type"),
};

let request = self.ajaxRequest(ajax_data);

request.done(function(response) {
if ("success" === response.status) {
window.location.href = response.file;
}
});
});
},
deleteFeedback: function() {
const self = this;

if (!$(".helpful-delete-feedback").length) {
return;
}

$(".helpful-delete-feedback").on("click", function(e) {
e.preventDefault();

if (confirm(helpful_admin_feedback.texts.delete_feedback)) {
let request = self.ajaxRequest({
action: "helpful_delete_all_feedback",
_wpnonce: helpful_admin_feedback.nonce,
});

request.done(function(response) {
if (!response.success) {
alert(response.data);
}
$(".helpful-admin-filter").change();
});
}
});
},
ajaxRequest: function(data) {
return $.ajax({
url: helpful_admin_feedback.ajax_url,
data: data,
method: "POST",
});
},
};

$(function() {
HelpfulAdminFeedback.initClass();
});

})(jQuery);
30 changes: 30 additions & 0 deletions core/modules/class-feedback-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function __construct()
add_action( 'wp_ajax_helpful_admin_feedback_items', [ &$this, 'ajax_get_feedback_items' ] );
add_action( 'wp_ajax_helpful_remove_feedback', [ &$this, 'ajax_delete_feedback_item' ] );
add_action( 'wp_ajax_helpful_export_feedback', [ &$this, 'ajax_export_feedback' ] );
add_action( 'wp_ajax_helpful_delete_all_feedback', [ & $this, 'ajax_delete_all_feedback']);
}

/**
Expand Down Expand Up @@ -113,6 +114,9 @@ public function enqueue_scripts( $hook_suffix )
$vars = [
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'helpful_admin_feedback_nonce' ),
'texts' => [
'delete_feedback' => esc_html__('Are you sure you want to delete all your feedback?', 'helpful'),
],
];

wp_localize_script( 'helpful-admin-feedback', 'helpful_admin_feedback', $vars );
Expand Down Expand Up @@ -323,4 +327,30 @@ public function ajax_export_feedback()

wp_send_json( $response );
}

/**
* Empties the feedback table and optimizes it afterwards.
*
* @return void
*/
public function ajax_delete_all_feedback()
{
check_ajax_referer( 'helpful_admin_feedback_nonce' );

global $wpdb;
$table_name = $wpdb->prefix . 'helpful_feedback';

$wpdb->query("TRUNCATE TABLE $table_name");
$wpdb->query("OPTIMIZE TABLE $table_name");

$rows = $wpdb->get_var("SELECT count(*) FROM $table_name");

if (!$rows) {
wp_send_json_success(_x('Your feedback has been deleted.', 'success message', 'helpful'));
}

$message = _x('Your feedback could not be deleted. Try again or report the error in the WordPress Support Forum: %s', 'error message', 'helpful');
$message = sprintf($message, 'https://wordpress.org/support/plugin/helpful/');
wp_send_json_error($message);
}
}
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.34
* Version: 4.4.35
* 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.7
Requires PHP: 5.6.20
Stable tag: 4.4.34
Stable tag: 4.4.35
License: MIT License
License URI: https://opensource.org/licenses/MIT

Expand Down
6 changes: 5 additions & 1 deletion templates/admin-feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@
<option value="contra"><?php esc_html_e( 'Contra', 'helpful' ); ?></option>
</select>

<button type="button" class="button default helpful-reset" style="margin-right: auto; margin-left: 5px; display: none;">
<button type="button" class="button default helpful-reset" style="margin-right: 15px; margin-left: 5px; display: none;">
<?php echo esc_html_x( 'Reset filter', 'button text', 'helpful' ); ?>
</button>

<button type="button" class="button danger helpful-delete-feedback" style="margin-right: auto; margin-left: 5px;">
<?php _ex( 'Delete all', 'admin delete all feedback button', 'helpful'); ?>
</button>

<button type="button" class="button default helpful-export" data-type="feedback">
<?php echo esc_html_x( 'Export', 'export button text', 'helpful' ); ?>
Expand Down

0 comments on commit 841845f

Please sign in to comment.