diff --git a/core/assets/js/admin-feedback.js b/core/assets/js/admin-feedback.js index 796cb87..a430e27 100644 --- a/core/assets/js/admin-feedback.js +++ b/core/assets/js/admin-feedback.js @@ -1,132 +1,158 @@ -(function ($) { - - "use strict"; - - const HelpfulAdminFeedback = { - loader: "
", - 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: "
", + 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); \ No newline at end of file diff --git a/core/modules/class-feedback-admin.php b/core/modules/class-feedback-admin.php index 8140fff..0f48e1a 100644 --- a/core/modules/class-feedback-admin.php +++ b/core/modules/class-feedback-admin.php @@ -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']); } /** @@ -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 ); @@ -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); + } } \ No newline at end of file diff --git a/helpful.php b/helpful.php index 38243dc..f254f9b 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.34 + * Version: 4.4.35 * Author: Pixelbart * Author URI: https://pixelbart.de * Text Domain: helpful diff --git a/readme.txt b/readme.txt index a067a2b..6203a33 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.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 diff --git a/templates/admin-feedback.php b/templates/admin-feedback.php index 6b9cc7e..f808009 100644 --- a/templates/admin-feedback.php +++ b/templates/admin-feedback.php @@ -39,9 +39,13 @@ - + +