Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelbart committed Dec 17, 2020
1 parent 49e359f commit 8788d19
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 124 deletions.
234 changes: 126 additions & 108 deletions core/assets/js/helpful.js
Original file line number Diff line number Diff line change
@@ -1,110 +1,128 @@
(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;
});

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();
});


"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 () {
let currentField = $(this);

if (!$(currentField).val().trim.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();
});

})(jQuery);
4 changes: 4 additions & 0 deletions core/helpers/class-feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@ public static function after_vote( $post_id, $show_feedback = false )
$hide_feedback = get_post_meta( $post_id, 'helpful_hide_feedback_on_post', true );
$hide_feedback = ( 'on' === $hide_feedback ) ? true : false;

if( Helper::is_feedback_disabled() ) {
$hide_feedback = true;
}

$user_id = User::get_user();
$type = User::get_user_vote_status( $user_id, $post_id );

Expand Down
11 changes: 8 additions & 3 deletions core/modules/class-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ public function enqueue_scripts()
'user_id' => $user,
'_wpnonce' => $nonce,
],
'translations' => [
'fieldIsRequired' => __( 'This field is required.', 'helpful' ),
],
];

if ( isset( $_SESSION ) ) {
Expand Down Expand Up @@ -457,9 +460,11 @@ public function helpful( $atts, $content = '' )
}

if ( false !== $exists && 'on' === get_option( 'helpful_feedback_after_vote' ) ) {
$shortcode = Helpers\Feedback::after_vote( $helpful['post_id'], true );
$shortcode = Helpers\Values::convert_tags( $shortcode, $helpful['post_id'] );
return $shortcode;
if ( ! Helper::is_feedback_disabled() ) {
$shortcode = Helpers\Feedback::after_vote( $helpful['post_id'], true );
$shortcode = Helpers\Values::convert_tags( $shortcode, $helpful['post_id'] );
return $shortcode;
}
}

if ( get_post_meta( $helpful['post_id'], 'helpful_heading', true ) ) {
Expand Down
3 changes: 1 addition & 2 deletions core/modules/class-metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ public function save_data( $post_id )

foreach ( $metas as $meta ) :
if ( isset( $_POST[ $meta ] ) && '' !== trim( $_POST[ $meta ] ) ) {
$value = sanitize_text_field( wp_unslash( $_POST[ $meta ] ) );
update_post_meta( $post_id, $meta, $value );
update_post_meta( $post_id, $meta, $_POST[ $meta ] );
} else {
delete_post_meta( $post_id, $meta );
}
Expand Down
10 changes: 7 additions & 3 deletions core/modules/class-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,30 @@ public function widget_setup()
public function widget_callback()
{
$links = [
sprintf(
'settings' => sprintf(
'<a href="%s" title="%s">%s</a>',
admin_url( 'admin.php?page=helpful&tab=texts' ),
__( 'Settings', 'helpful' ),
'<span class="dashicons dashicons-admin-settings"></span>'
),
sprintf(
'feedback' => sprintf(
'<a href="%s" title="%s">%s</a>',
admin_url( 'admin.php?page=helpful_feedback' ),
__( 'Feedback', 'helpful' ),
'<span class="dashicons dashicons-testimonial"></span>'
),
sprintf(
'stats' => sprintf(
'<a href="%s" title="%s">%s</a>',
admin_url( 'admin.php?page=helpful' ),
__( 'Statistics', 'helpful' ),
'<span class="dashicons dashicons-chart-area"></span>'
),
];

if ( Helper::is_feedback_disabled() ) {
unset( $links['feedback'] );
}

$years = Helpers\Stats::get_years();

$this->render_template( $links, $years );
Expand Down
6 changes: 3 additions & 3 deletions templates/admin-metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<label for="helpful_heading"><?php _ex( 'Headline', 'meta box label', 'helpful' ); ?></label>
</th>
<td>
<input type="text" class="widefat" name="helpful_heading" id="helpful_heading" value="<?php echo $helpful_headline; ?>"><br>
<textarea class="widefat" name="helpful_heading" id="helpful_heading"><?php echo $helpful_heading; ?></textarea><br>
<span class="description">You can also use HTML.</span>
</td>
</tr>
Expand Down Expand Up @@ -120,7 +120,7 @@
<label for="helpful_pro"><?php _ex( 'Button (pro)', 'meta box label', 'helpful' ); ?></label>
</th>
<td>
<input type="text" class="widefat" name="helpful_pro" id="helpful_pro" value="<?php echo $helpful_pro; ?>"><br>
<textarea class="widefat" name="helpful_pro" id="helpful_pro"><?php echo $helpful_pro; ?></textarea><br>
<span class="description">You can also use HTML.</span>
</td>
</tr>
Expand All @@ -129,7 +129,7 @@
<label for="helpful_contra"><?php _ex( 'Button (contra)', 'meta box label', 'helpful' ); ?></label>
</th>
<td>
<input type="text" class="widefat" name="helpful_contra" id="helpful_contra" value="<?php echo $helpful_contra; ?>"><br>
<textarea class="widefat" name="helpful_contra" id="helpful_contra"><?php echo $helpful_contra; ?></textarea><br>
<span class="description">You can also use HTML.</span>
</td>
</tr>
Expand Down
10 changes: 5 additions & 5 deletions templates/admin-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

<div class="helpful-widget-panels">

<?php if ( isset( $widget_stats['most_helpful'] ) ) : ?>
<?php if ( isset( $widget_stats['most_helpful'] ) && ! empty( $widget_stats['most_helpful'] ) ) : ?>
<div class="helpful-widget-panel">

<button type="button">
Expand All @@ -78,7 +78,7 @@
</div>
<?php endif; ?>

<?php if ( isset( $widget_stats['least_helpful'] ) ) : ?>
<?php if ( isset( $widget_stats['least_helpful'] ) && ! empty( $widget_stats['least_helpful'] ) ) : ?>
<div class="helpful-widget-panel">

<button type="button">
Expand All @@ -99,7 +99,7 @@
</div>
<?php endif; ?>

<?php if ( isset( $widget_stats['recently_pro'] ) ) : ?>
<?php if ( isset( $widget_stats['recently_pro'] ) && ! empty( $widget_stats['recently_pro'] ) ) : ?>
<div class="helpful-widget-panel">

<button type="button">
Expand All @@ -119,7 +119,7 @@
</div>
<?php endif; ?>

<?php if ( isset( $widget_stats['recently_contra'] ) ) : ?>
<?php if ( isset( $widget_stats['recently_contra'] ) && ! empty( $widget_stats['recently_contra'] ) ) : ?>
<div class="helpful-widget-panel">

<button type="button">
Expand All @@ -139,7 +139,7 @@
</div>
<?php endif; ?>

<?php if ( isset( $widget_stats['feedback_items'] ) ) : ?>
<?php if ( isset( $widget_stats['feedback_items'] ) && ! empty( $widget_stats['feedback_items'] ) ) : ?>
<div class="helpful-widget-panel">

<button type="button">
Expand Down

0 comments on commit 8788d19

Please sign in to comment.