Skip to content

Commit

Permalink
4.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelbart committed Sep 7, 2020
1 parent c1a88fd commit d0c8c52
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 104 deletions.
8 changes: 7 additions & 1 deletion core/assets/js/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@
ajaxData["_ajax_nonce"] = $(currentContainer).data("nonce");

if( ajaxData["post_content"].length > 0 ) {
self.ajaxRequest(ajaxData).done(function(response) {
var request = self.ajaxRequest(ajaxData);

request.done(function (response) {
$(parentElement).html(response);
});
}
Expand All @@ -112,6 +114,10 @@
}
},
ajaxRequest: function(data) {
if (typeof helpful.ajax_session !== 'undefined') {
data.session = helpful.ajax_session;
}

return $.ajax({
url : helpful.ajax_url,
data : data,
Expand Down
56 changes: 41 additions & 15 deletions core/assets/js/helpful.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
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();
if (e.target !== e.currentTarget) {
// return;
}

var currentButton = $(this);
var currentForm = $(currentButton).closest('.helpful');
Expand All @@ -25,43 +23,71 @@
$.extend(ajaxData, $(currentButton).data());
ajaxData.action = self.vote;

self.ajaxRequest(ajaxData).done(function (response) {
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 current_container = $(this);
if ($(current_container).is('.helpful-prevent-form')) {
console.log(current_container);
self.feedbackForm($(current_container));
}
});
},
feedbackForm: function (currentForm) {
var self = this;

$(currentForm).find('.helpful-cancel').unbind().click(function (e) {
e.preventDefault();
var ajaxData = [
{ name: 'action', value: 'helpful_save_feedback' },
{ name: 'cancel', value: 1 },
{ name: 'type', value: $(currentForm).find('[name="type"]').val() },
{ name: '_wpnonce', value: $(currentForm).find('[name="_wpnonce"]').val() },
];

self.ajaxRequest(ajaxData).done(function (response) {

var ajaxData = {
action: 'helpful_save_feedback',
cancel: 1,
type: $(currentForm).find('[name="type"]').val(),
'_wpnonce': $(currentForm).find('[name="_wpnonce"]').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 ajaxData = $(this).serializeArray();
self.ajaxRequest(ajaxData).done(function (response) {

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

console.log('ajaxRequest', data);

return $.ajax({
url: helpful.ajax_url,
url: this.helpful.ajax_url,
data: data,
method: "POST",
});
Expand Down
93 changes: 10 additions & 83 deletions core/classes/class-helpful-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ public function enqueue_scripts()
],
];

if ( isset( $_SESSION ) ) {
$vars['ajax_session'] = apply_filters( 'helpful_ajax_session', $_SESSION );
}

wp_localize_script( 'helpful', 'helpful', $vars );
}

Expand All @@ -165,17 +169,19 @@ public function save_vote()
{
check_ajax_referer( 'helpful_frontend_nonce' );

do_action( 'helpful_ajax_save_vote' );

$user_id = sanitize_text_field( $_POST['user_id'] );
$post_id = intval( $_POST['post'] );
$value = sanitize_text_field( $_POST['value'] );

if ( ! Helpful_Helper_Values::checkUser( $user_id, $post_id ) ) {
if ( 'pro' === $value ) {
Helpful_Helper_Values::insertPro( $user_id, $post_id );
$response = do_shortcode( $this->after_vote( $value, $post_id ) );
$response = do_shortcode( Helpful_Helper_Feedback::after_vote( $post_id ) );
} else {
Helpful_Helper_Values::insertContra( $user_id, $post_id );
$response = do_shortcode( $this->after_vote( $value, $post_id ) );
$response = do_shortcode( Helpful_Helper_Feedback::after_vote( $post_id ) );
}
}

Expand All @@ -192,6 +198,8 @@ public function save_feedback()
{
check_ajax_referer( 'helpful_feedback_nonce' );

do_action( 'helpful_ajax_save_feedback' );

/**
* Simple Spam Protection
*/
Expand Down Expand Up @@ -226,85 +234,4 @@ public function save_feedback()

wp_die();
}

/**
* Render after messages or feedback form, after vote.
* Checks if custom template exists.
*
* @param string $type feedback type pro or contra.
* @param integer $post_id post id.
*
* @return string
*/
public function after_vote( $type, $post_id )
{
$feedback_text = esc_html_x(
'Thank you very much. Please write us your opinion, so that we can improve ourselves.',
'form user note',
'helpful'
);

$hide_feedback = get_post_meta( $post_id, 'helpful_hide_feedback_on_post', true );
$hide_feedback = ( 'on' === $hide_feedback ) ? true : false;

if ( 'pro' === $type ) {
$feedback_text = get_option( 'helpful_feedback_message_pro' );

if ( ! get_option( 'helpful_feedback_after_pro' ) || false !== $hide_feedback ) {
return do_shortcode( get_option( 'helpful_after_pro' ) );
}
}

if ( 'contra' === $type ) {
$feedback_text = get_option( 'helpful_feedback_message_contra' );

if ( ! get_option( 'helpful_feedback_after_contra' ) || false !== $hide_feedback ) {
return do_shortcode( get_option( 'helpful_after_contra' ) );
}
}

ob_start();

$default_template = HELPFUL_PATH . 'templates/feedback.php';
$custom_template = locate_template( 'helpful/feedback.php' );

do_action( 'helpful_before_feedback_form' );

echo '<form class="helpful-feedback-form">';

printf( '<input type="hidden" name="user_id" value="%s">', Helpful_Helper_Values::getUser() );
printf( '<input type="hidden" name="action" value="%s">', 'helpful_save_feedback' );
printf( '<input type="hidden" name="post_id" value="%s">', $post_id );
printf( '<input type="hidden" name="type" value="%s">', $type );

/**
* Simple Spam Protection
*/
$spam_protection = apply_filters( 'helpful_simple_spam_protection', true );

if ( ! is_bool( $spam_protection ) ) {
$spam_protection = true;
}

if ( true === $spam_protection ) {
echo '<input type="text" name="website" id="website" style="display:none;">';
}

wp_nonce_field( 'helpful_feedback_nonce' );

if ( '' !== $custom_template ) {
include $custom_template;
} else {
include $default_template;
}

echo '</form>';

do_action( 'helpful_after_feedback_form' );

$content = ob_get_contents();
ob_end_clean();

return $content;
}
}
110 changes: 109 additions & 1 deletion core/classes/class-helpful-helper-feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ public static function insertFeedback()
$fields[ $key ] = sanitize_text_field( $value );
}

$fields = apply_filters( 'helpful_feedback_submit_fields', $fields );
$session = [];

if ( isset( $_REQUEST['session'] ) ) {
$session = $_REQUEST['session'];
}

$fields = apply_filters( 'helpful_feedback_submit_fields', $fields, $session );
}

if ( is_user_logged_in() ) {
Expand Down Expand Up @@ -334,4 +340,106 @@ public static function get_feedback_count( $post_id = null )

return $wpdb->get_var( $wpdb->prepare( $sql, $post_id ) );
}



/**
* Render after messages or feedback form, after vote.
* Checks if custom template exists.
*
* @param integer $post_id post id.
* @param bool $show_feedback show feedback form anyway.
*
* @return string
*/
public static function after_vote( $post_id, $show_feedback = false )
{
$feedback_text = esc_html_x(
'Thank you very much. Please write us your opinion, so that we can improve ourselves.',
'form user note',
'helpful'
);

$hide_feedback = get_post_meta( $post_id, 'helpful_hide_feedback_on_post', true );
$hide_feedback = ( 'on' === $hide_feedback ) ? true : false;

$user_id = Helpful_Helper_Values::getUser();
$type = Helpful_Helper_Values::get_user_vote_status( $user_id, $post_id );

if ( 'pro' === $type ) {
$feedback_text = get_option( 'helpful_feedback_message_pro' );

if ( true !== $show_feedback ) {
if ( ! get_option( 'helpful_feedback_after_pro' ) || false !== $hide_feedback ) {
return do_shortcode( get_option( 'helpful_after_pro' ) );
}
}
}

if ( 'contra' === $type ) {
$feedback_text = get_option( 'helpful_feedback_message_contra' );

if ( true !== $show_feedback ) {
if ( ! get_option( 'helpful_feedback_after_contra' ) || false !== $hide_feedback ) {
return do_shortcode( get_option( 'helpful_after_contra' ) );
}
}
}

if ( false !== $show_feedback ) {
$feedback_text = get_option( 'helpful_feedback_message_voted' );
}

if ( '' === trim( $feedback_text ) ) {
$feedback_text = false;
}

ob_start();

$default_template = HELPFUL_PATH . 'templates/feedback.php';
$custom_template = locate_template( 'helpful/feedback.php' );

do_action( 'helpful_before_feedback_form' );

echo '<form class="helpful-feedback-form">';

printf( '<input type="hidden" name="user_id" value="%s">', $user_id );
printf( '<input type="hidden" name="action" value="%s">', 'helpful_save_feedback' );
printf( '<input type="hidden" name="post_id" value="%s">', $post_id );
printf( '<input type="hidden" name="type" value="%s">', $type );

/**
* Simple Spam Protection
*/
$spam_protection = apply_filters( 'helpful_simple_spam_protection', true );

if ( ! is_bool( $spam_protection ) ) {
$spam_protection = true;
}

if ( true === $spam_protection ) {
echo '<input type="text" name="website" id="website" style="display:none;">';
}

wp_nonce_field( 'helpful_feedback_nonce' );

if ( '' !== $custom_template ) {
include $custom_template;
} else {
include $default_template;
}

echo '</form>';

do_action( 'helpful_after_feedback_form' );

$content = ob_get_contents();
ob_end_clean();

if ( false !== $show_feedback ) {
$content = '<div class="helpful helpful-prevent-form"><div class="helpful-content" role="alert">' . $content . '</div></div>';
}

return $content;
}
}
Loading

0 comments on commit d0c8c52

Please sign in to comment.