From d0c8c528ac3395458f951dff3534271ec78268b1 Mon Sep 17 00:00:00 2001 From: Kevin Pliester Date: Mon, 7 Sep 2020 18:06:25 +0200 Subject: [PATCH] 4.3.0 --- core/assets/js/frontend.js | 8 +- core/assets/js/helpful.js | 56 ++++++--- core/classes/class-helpful-frontend.php | 93 ++------------- .../classes/class-helpful-helper-feedback.php | 110 +++++++++++++++++- core/classes/class-helpful-shortcodes.php | 18 +++ core/classes/class-helpful-tabs-feedback.php | 2 + core/tabs/tab-feedback.php | 30 ++++- helpful.php | 2 +- readme.txt | 4 +- wpml-config.xml | 1 + 10 files changed, 220 insertions(+), 104 deletions(-) diff --git a/core/assets/js/frontend.js b/core/assets/js/frontend.js index cf6f5d5..dbac99d 100644 --- a/core/assets/js/frontend.js +++ b/core/assets/js/frontend.js @@ -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); }); } @@ -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, diff --git a/core/assets/js/helpful.js b/core/assets/js/helpful.js index 13a8d51..7fd2068 100644 --- a/core/assets/js/helpful.js +++ b/core/assets/js/helpful.js @@ -6,6 +6,7 @@ el: ".helpful", vote: "helpful_save_vote", feedback: "helpful_save_feedback", + helpful: helpful, initPlugin: function () { const self = this; @@ -13,9 +14,6 @@ $(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'); @@ -25,7 +23,9 @@ $.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(); @@ -33,35 +33,61 @@ 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", }); diff --git a/core/classes/class-helpful-frontend.php b/core/classes/class-helpful-frontend.php index 97c4a07..ee8e2a0 100644 --- a/core/classes/class-helpful-frontend.php +++ b/core/classes/class-helpful-frontend.php @@ -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 ); } @@ -165,6 +169,8 @@ 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'] ); @@ -172,10 +178,10 @@ public function save_vote() 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 ) ); } } @@ -192,6 +198,8 @@ public function save_feedback() { check_ajax_referer( 'helpful_feedback_nonce' ); + do_action( 'helpful_ajax_save_feedback' ); + /** * Simple Spam Protection */ @@ -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 '
'; - - printf( '', Helpful_Helper_Values::getUser() ); - printf( '', 'helpful_save_feedback' ); - printf( '', $post_id ); - printf( '', $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 ''; - } - - wp_nonce_field( 'helpful_feedback_nonce' ); - - if ( '' !== $custom_template ) { - include $custom_template; - } else { - include $default_template; - } - - echo '
'; - - do_action( 'helpful_after_feedback_form' ); - - $content = ob_get_contents(); - ob_end_clean(); - - return $content; - } } diff --git a/core/classes/class-helpful-helper-feedback.php b/core/classes/class-helpful-helper-feedback.php index 02138bb..a79fd15 100644 --- a/core/classes/class-helpful-helper-feedback.php +++ b/core/classes/class-helpful-helper-feedback.php @@ -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() ) { @@ -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 '
'; + + printf( '', $user_id ); + printf( '', 'helpful_save_feedback' ); + printf( '', $post_id ); + printf( '', $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 ''; + } + + wp_nonce_field( 'helpful_feedback_nonce' ); + + if ( '' !== $custom_template ) { + include $custom_template; + } else { + include $default_template; + } + + echo '
'; + + do_action( 'helpful_after_feedback_form' ); + + $content = ob_get_contents(); + ob_end_clean(); + + if ( false !== $show_feedback ) { + $content = '
'; + } + + return $content; + } } diff --git a/core/classes/class-helpful-shortcodes.php b/core/classes/class-helpful-shortcodes.php index 12662c1..9f16d1b 100644 --- a/core/classes/class-helpful-shortcodes.php +++ b/core/classes/class-helpful-shortcodes.php @@ -90,6 +90,7 @@ public function add_to_content( $content ) } $helpful = Helpful_Helper_Values::getDefaults(); + $exists = false; $hidden = false; $class = ''; @@ -98,11 +99,17 @@ public function add_to_content( $content ) return __return_empty_string(); } + $exists = true; $hidden = true; $class = 'helpful-exists'; $helpful['content'] = $helpful['exists_text']; } + if ( false !== $exists && get_option( 'helpful_feedback_after_vote' ) ) { + $content .= Helpful_Helper_Feedback::after_vote( $helpful['post_id'], true ); + return $content; + } + $helpful['content'] = do_shortcode( $helpful['content'] ); ob_start(); @@ -156,6 +163,7 @@ public function shortcode_helpful( $atts, $content = '' ) $helpful = shortcode_atts( $defaults, $atts ); $helpful = apply_filters( 'helpful_shortcode_atts', $helpful ); + $exists = false; $hidden = false; $class = ''; @@ -164,6 +172,7 @@ public function shortcode_helpful( $atts, $content = '' ) return __return_empty_string(); } + $exists = true; $hidden = true; $class = 'helpful-exists'; $helpful['content'] = $helpful['exists_text']; @@ -177,17 +186,26 @@ public function shortcode_helpful( $atts, $content = '' ) } } + if ( false !== $exists && get_option( 'helpful_feedback_after_vote' ) ) { + $content .= Helpful_Helper_Feedback::after_vote( $helpful['post_id'], true ); + return $content; + } + ob_start(); $default_template = HELPFUL_PATH . 'templates/helpful.php'; $custom_template = locate_template( 'helpful/helpful.php' ); + do_action( 'helpful_before' ); + if ( '' !== $custom_template ) { include $custom_template; } else { include $default_template; } + do_action( 'helpful_after' ); + $content .= ob_get_contents(); ob_end_clean(); diff --git a/core/classes/class-helpful-tabs-feedback.php b/core/classes/class-helpful-tabs-feedback.php index 79bd063..db1f872 100644 --- a/core/classes/class-helpful-tabs-feedback.php +++ b/core/classes/class-helpful-tabs-feedback.php @@ -118,6 +118,8 @@ public function register_settings() 'helpful_feedback_subject', 'helpful_feedback_email_content', 'helpful_feedback_message_spam', + 'helpful_feedback_after_vote', + 'helpful_feedback_message_voted', ]; $fields = apply_filters( 'helpful_feedback_fields', $fields ); diff --git a/core/tabs/tab-feedback.php b/core/tabs/tab-feedback.php index 9dcc3c6..576d2c7 100644 --- a/core/tabs/tab-feedback.php +++ b/core/tabs/tab-feedback.php @@ -106,7 +106,7 @@

-
+

@@ -165,6 +165,34 @@
+
+ + + +
+ +

+ +
+ +
+ +
+ + +

+
+ +
+
+