init_vars(); $this->hooks(); } /** * Initialize main settings values. * * @since 9.1.3 * * @return void */ public function init_vars() { $postfix = '_advanced'; $settings = (array)get_option('xstore_sales_booster_settings', array()); $default = array( 'rating_summary_position' => 'comments_start', 'rating_summary' => 'on', 'pros_cons' => false, 'likes' => 'on', 'reset_likes' => false, 'verified_owner_badge' => 'on', 'rating_criteria' => false, 'rating_criteria_required' => false, 'criteria' => '', 'criteria_ready' => [], 'circle_avatars' => false, 'date_format' => 'ago', 'date_format_custom' => wc_date_format() ); $local_settings = $default; if (count($settings) && isset($settings[self::$option_name . $postfix])) { $local_settings = wp_parse_args( $settings[ self::$option_name . $postfix ], $default ); } $force_check_switchers = array( 'rating_summary', 'pros_cons', 'likes', 'reset_likes', 'verified_owner_badge', 'rating_criteria', 'rating_criteria_required', 'circle_avatars' ); foreach ($force_check_switchers as $switcher) { $local_settings[$switcher] = $local_settings[$switcher] == 'on'; } if ($local_settings['criteria'] ) { $criteria_unique_slugs = array(); $criteria = explode(',', $local_settings['criteria']); if (count($criteria)) { foreach ($criteria as $criteria_key) { if ('' == $criteria_key || '' == $local_settings[$criteria_key . '_slug']) continue; if ( in_array($local_settings[$criteria_key . '_slug'], $criteria_unique_slugs)) continue; $criteria_ready = array( 'slug' => $local_settings[$criteria_key . '_slug'], 'name' => $local_settings[$criteria_key . '_name'], ); if (array_filter($criteria_ready)) { $local_settings['criteria_ready'][] = $criteria_ready; $criteria_unique_slugs[] = $criteria_ready['slug']; } } // empty all values if (!array_filter($local_settings['criteria_ready'])) $local_settings['criteria_ready'] = array(); } } $this->settings = $local_settings; } /** * Hooks for creating/saving/removing data in comment form. * * @since 9.1.3 * * @return void */ public function hooks() { add_action('wp_enqueue_scripts', array($this, 'load_styles'), 40); // change the position of star rating in review comment remove_action('woocommerce_review_before_comment_meta', 'woocommerce_review_display_rating', 10); add_action('woocommerce_review_before_comment_text', 'woocommerce_review_display_rating', 5); if (in_array($this->settings['date_format'], array('ago', 'custom'))) { add_action('woocommerce_review_meta', array($this, 'set_comment_date_format_custom'), 1); add_action('woocommerce_review_meta', array($this, 'reset_comment_date_format_custom'), 100); } // pros and cons if ( $this->settings['pros_cons'] ) { add_action('comment_post', array($this, 'save_comment_pros_cons')); add_filter('comment_text', array($this, 'render_pros_cons'), 30, 2); add_filter('woocommerce_product_review_comment_form_args', array($this, 'add_pros_cons_fields')); } if ( $this->settings['rating_summary'] ) { add_filter('comment_post_redirect', array($this, 'redirect_after_comment')); $summary_hook = str_replace( array('above_all', 'comments_start', 'review_start'), array('etheme_before_product_comments_wrapper', 'etheme_before_product_comments', 'etheme_before_product_review_form'), $this->settings['rating_summary_position'] ); add_action($summary_hook, array($this, 'render_rating_summary')); } if ( $this->settings['verified_owner_badge'] ) { add_filter( 'comment_author', array( $this, 'add_author_icon' ), 10, 2 ); } if ( $this->settings['rating_criteria'] && count($this->settings['criteria_ready']) ) { remove_action( 'woocommerce_review_before_comment_text', 'woocommerce_review_display_rating', 5 ); add_action( 'woocommerce_review_before_comment_text', array( $this, 'render_criteria_star_rating' ), 7 ); add_filter('etheme_product_review_rating_html', array($this, 'add_criteria_ratings'), 10); add_filter('cr_review_form_before_comment', array($this, 'add_criteria_ratings'), 10); add_action( 'comment_post', array( $this, 'save_comment_rating_criteria' ) ); add_filter('etheme_et_js_config', function ($config) { if ( get_query_var('is_single_product', false) || get_query_var('is_single_product_shortcode', false) ) { if ( !array_key_exists('sales_booster_reviews_advanced', $config) ) $config['sales_booster_reviews_advanced'] = array(); $config['sales_booster_reviews_advanced'] = array_merge(array( 'criteria_list' => wp_json_encode($this->settings['criteria_ready']), 'criteria_required' => $this->settings['rating_criteria_required'] ), $config['sales_booster_reviews_advanced']); } return $config; }); } if ( $this->settings['circle_avatars'] ) { add_action('woocommerce_review_before', array($this, 'set_avatar_circle'), 1); add_action('woocommerce_review_before', array($this, 'reset_avatar_circle'), 100); } if ( $this->settings['likes'] ) { add_action( 'comment_post', array( $this, 'save_comment_likes' ) ); add_action( 'woocommerce_review_after_comment_text', array( $this, 'render_likes' ) ); add_filter('etheme_et_js_config', function ($config) { if ( get_query_var('is_single_product', false) || get_query_var('is_single_product_shortcode', false) ) { $config['is_loggedIn'] = get_query_var( 'et_is-loggedin', false); $config['woocommerceSettings'] = array_merge($config['woocommerceSettings'], array( 'myaccount_url' => $config['woocommerceSettings']['is_woocommerce'] ? esc_url(get_permalink(get_option('woocommerce_myaccount_page_id'))) : '', 'voted_text' => esc_html__('Voted', 'xstore') )); if ( !array_key_exists('sales_booster_reviews_advanced', $config) ) $config['sales_booster_reviews_advanced'] = array(); $config['sales_booster_reviews_advanced'] = array_merge(array( 'cheating_likes' => esc_html__('Cheating huh?', 'xstore'), 'reset_likes' => $this->settings['reset_likes'] ), $config['sales_booster_reviews_advanced']); } return $config; }); } } public function load_styles() { if ( get_query_var('is_single_product', false) || get_query_var('is_single_product_shortcode', false) ) { wp_enqueue_style('etheme-sale-booster-reviews-advanced'); if ( $this->settings['likes'] ) wp_enqueue_script( 'et_reviews_likes' ); if ( $this->settings['rating_criteria'] && count($this->settings['criteria_ready']) ) wp_enqueue_script( 'et_reviews_criteria' ); } } /** * Converting data to human-readable version (X days ago) * @param $date * @param $format * @param $comment * @return string */ public function human_time_ago($date, $format, $comment) { return sprintf(_x( '%s ago', '%s = human-readable time difference', 'xstore' ), human_time_diff( get_comment_time( 'U', false, true, $comment->comment_ID ), current_time( 'timestamp' ) ) ); } /** * Set the date format to custom one if the options are set. * @param $format * @return mixed */ public function custom_date_format($format) { if ( empty($this->settings['date_format_custom']) ) return $format; return $this->settings['date_format_custom']; } /** * Setter of custom date format */ public function set_comment_date_format_custom() { if ( $this->settings['date_format'] == 'ago' ) add_filter('get_comment_date', array($this, 'human_time_ago'), 10, 3); else add_filter('woocommerce_date_format', array($this, 'custom_date_format'), 10, 1); } /** * Resetter of custom date format */ public function reset_comment_date_format_custom() { if ( $this->settings['date_format'] == 'ago' ) remove_filter('get_comment_date', array($this, 'human_time_ago'), 10, 3); else remove_filter('woocommerce_date_format', array($this, 'custom_date_format'), 10, 1); } /** * Render rating summary. * * @return string|void */ public function render_rating_summary() { if ( ! function_exists( 'wc_review_ratings_enabled' ) || ! wc_review_ratings_enabled() ) { return ''; } global $product; $rating_count = $product->get_rating_count(); if ( $rating_count < 1 ) return; $rating_item = array( 5 => $product->get_rating_count(5), 4 => $product->get_rating_count(4), 3 => $product->get_rating_count(3), 2 => $product->get_rating_count(2), 1 => $product->get_rating_count(1) ); $average_rating = $product->get_average_rating(); ?>
settings['pros_cons'] ) { return $comment_form; } $comment_form['comment_field'] .= ''; $comment_form['comment_field'] .= ''; return $comment_form; } /** * Render pros & cons for each comment if one's has such data set on posting. * @param $comment_content * @param $comment * @return string */ public function render_pros_cons( $comment_content, $comment ) { if ( ! $this->settings['pros_cons'] || ( ! wp_doing_ajax() && ( is_admin() || ! is_singular( 'product' ) ) ) ) { return $comment_content; } $pros_list = array_filter( get_comment_meta( $comment->comment_ID, 'et_pros' ) ); $cons_list = array_filter( get_comment_meta( $comment->comment_ID, 'et_cons' ) ); if ( empty( $pros_list ) && empty( $cons_list ) ) return $comment_content; ob_start(); ?>