function init_woocommerce_product_reviews_pro()
{
    /**
     * # WooCommerce Product Reviews Pro Main Plugin Class
     *
     * ## Plugin Overview
     *
     * ## Features
     *
     * ## Frontend Considerations
     *
     * ## Admin Considerations
     *
     * ## Database
     *
     * @since 1.0.0
     */
    class WC_Product_Reviews_Pro extends SV_WC_Plugin
    {
        /** plugin version number */
        const VERSION = '1.0.6';
        /** @var WC_Product_Reviews_Pro single instance of this plugin */
        protected static $instance;
        /** plugin id */
        const PLUGIN_ID = 'product_reviews_pro';
        /** plugin meta prefix */
        const PLUGIN_PREFIX = 'wc_product_reviews_pro_';
        /** plugin text domain */
        const TEXT_DOMAIN = 'woocommerce-product-reviews-pro';
        /** @var \WC_Product_Reviews_Pro_Admin instance */
        public $admin;
        /** @var \WC_Product_Reviews_Pro_Frontend instance */
        public $frontend;
        /** @var \WC_Product_Reviews_Pro_AJAX instance */
        public $ajax;
        /**
         * Initializes the plugin
         *
         * @since 1.0.0
         * @return \WC_Product_Reviews_Pro
         */
        public function __construct()
        {
            parent::__construct(self::PLUGIN_ID, self::VERSION, self::TEXT_DOMAIN);
            // Admin
            if (is_admin() && !defined('DOING_AJAX')) {
                // delay standard install so we can use get_comments()
                remove_action('wp_loaded', array($this, 'do_install'));
                add_action('admin_init', array($this, 'do_install'));
            }
            // Include required files
            $this->includes();
            add_action('wp_update_comment_count', array($this, 'clear_transients'));
            add_filter('pre_option_woocommerce_enable_review_rating', array($this, 'filter_enable_review_rating'));
            // Points and Rewards compatability - ensure only allowed contibutions are counted towards earned review points
            add_filter('wc_points_rewards_review_post_comments_args', array($this, 'points_rewards_review_get_comments_args'), 10, 2);
            add_filter('wc_points_rewards_review_approve_comments_args', array($this, 'points_rewards_review_get_comments_args'), 10, 2);
            // Points and Rewards compatability - ensure points are only added for allowed contribution types
            add_filter('wc_points_rewards_pro_post_add_product_review_points', array($this, 'points_rewards_review_add_product_review_points'), 10, 2);
            add_filter('wc_points_rewards_pro_approve_add_product_review_points', array($this, 'points_rewards_review_add_product_review_points'), 10, 2);
        }
        /**
         * Include required files
         *
         * @since 1.0.0
         */
        public function includes()
        {
            require_once 'includes/class-wc-product-reviews-pro-review-qualifiers.php';
            $this->review_qualifiers = new WC_Product_Reviews_Pro_Review_Qualifiers();
            require_once 'includes/class-wc-product-reviews-pro-contribution-factory.php';
            $this->contribution_factory = new WC_Product_Reviews_Pro_Contribution_Factory();
            require_once 'includes/class-wc-product-reviews-pro-contribution-type.php';
            require_once 'includes/abstract-wc-contribution.php';
            require_once 'includes/class-wc-contribution-review.php';
            require_once 'includes/class-wc-contribution-question.php';
            require_once 'includes/class-wc-contribution-video.php';
            require_once 'includes/class-wc-contribution-photo.php';
            require_once 'includes/class-wc-contribution-comment.php';
            require_once 'includes/wc-product-reviews-pro-contribution-functions.php';
            if (!is_admin() || defined('DOING_AJAX')) {
                $this->frontend_includes();
            }
            if (is_admin() && !defined('DOING_AJAX')) {
                $this->admin_includes();
            }
            require_once 'includes/class-wc-product-reviews-pro-ajax.php';
            $this->ajax = new WC_Product_Reviews_Pro_AJAX();
        }
        /**
         * Include required frontend files
         *
         * @since 1.0.0
         */
        private function frontend_includes()
        {
            require_once 'includes/wc-product-reviews-pro-template-functions.php';
            require_once 'includes/frontend/class-wc-product-reviews-pro-frontend.php';
            $this->frontend = new WC_Product_Reviews_Pro_Frontend();
        }
        /**
         * Include required admin files
         *
         * @since 1.0.0
         */
        private function admin_includes()
        {
            require_once 'lib/class-wc-reviews.php';
            require_once 'includes/admin/class-wc-product-reviews-pro-admin.php';
            $this->admin = new WC_Product_Reviews_Pro_Admin();
        }
        /**
         * Load plugin text domain.
         *
         * @since 1.0.0
         * @see SV_WC_Plugin::load_translation()
         */
        public function load_translation()
        {
            load_plugin_textdomain('woocommerce-product-reviews-pro', false, dirname(plugin_basename($this->get_file())) . '/i18n/languages');
        }
        /**
         * Get contribution types
         *
         * @since 1.0.0
         * @return array of contribution type names, ie ['review', 'question', 'video', 'photo', 'contribution_comment']
         */
        public function get_contribution_types()
        {
            /**
             * Filter the contribution types
             *
             * @since 1.0.0
             * @param array $contribution_types The contribution types.
             */
            return apply_filters('wc_product_reviews_pro_contribution_types', array('review', 'question', 'video', 'photo', 'contribution_comment'));
        }
        /**
         * Get enabled contribution types
         *
         * @since 1.0.0
         * @return array of contribution type names, ie ['review', 'question', 'video', 'photo', 'contribution_comment']
         */
        public function get_enabled_contribution_types()
        {
            if ('all' == get_option('wc_product_reviews_pro_enabled_contribution_types')) {
                return $this->get_contribution_types();
            } else {
                return (array) get_option('wc_product_reviews_pro_specific_enabled_contribution_types');
            }
        }
        /**
         * Filter the woocommerce_enable_review_rating option
         *
         * Checks if reviews have been enabled. If disabled, returns 'no'.
         *
         * @since 1.0.0
         * @param  string $enabled
         * @return string 'no' if reviews are disabled, pass-thru otherwise
         */
        public function filter_enable_review_rating($enabled)
        {
            return !in_array('review', $this->get_enabled_contribution_types()) ? 'no' : $enabled;
        }
        /**
         * Filter the get_comments arguments when a comment is posted or approved
         *
         * This ensures that only allowed contrubution types are counted towards
         * previously awarded points
         *
         * @since 1.0.6
         * @param array $args The get_comments array of arguments
         * @return array
         */
        public function points_rewards_review_get_comments_args($args)
        {
            /**
             * Filter the array of contibution types which should award points
             * Note: This filter should only be used if running WP 4.1+ only
             * {@link https://core.trac.wordpress.org/ticket/21571}
             *
             * @since 1.0.6
             * @param array $contribution_types The array of contibution types which
             */
            $comment_types = apply_filters('wc_product_reviews_pro_review_points_contribution_types', array('review'));
            // backwards compatability with < WP 4.1 as the `type` argument must be a sting in older versions
            if (count($comment_types) <= 1) {
                $comment_types = explode('', $comment_types);
            }
            return array_merge($args, array('type' => $comment_types));
        }
        /**
         * Filter if points should be added for a particular comment id on posting
         * or approving a review.
         *
         * This ensures that points are only rewarded for the review contibution type
         * but allows users to filter the types if needed.
         *
         * @since 1.0.6
         * @param bool $add_points True if points should be awarded for this contribution (default), false otherwise
         * @param int $comment_id The comment ID
         * @return bool True if points should be awarded for this contribution, false otherwise
         */
        public function points_rewards_review_add_product_review_points($add_points, $comment_id)
        {
            $comment = get_comment($comment_id);
            // bail if there is an issue with retreiving the comment object
            if (!$comment) {
                return $add_points;
            }
            /**
             * Filter the array of contibution types which should award points
             * Note: This filter should only be used if running WP 4.1+ only
             * {@link https://core.trac.wordpress.org/ticket/21571}
             *
             * @since 1.0.6
             * @param array $contribution_types The array of contibution types which
             */
            $comment_types = apply_filters('wc_product_reviews_pro_review_points_contribution_types', array('review'));
            return in_array($comment->comment_type, $comment_types);
        }
        /** Admin methods ******************************************************/
        /**
         * Render a notice for the user to read the docs before adding add-ons
         *
         * @since 1.0.0
         * @see SV_WC_Plugin::add_admin_notices()
         */
        public function add_admin_notices()
        {
            // show any dependency notices
            parent::add_admin_notices();
            $this->get_admin_notice_handler()->add_admin_notice(sprintf(__('Thanks for installing Product Reviews Pro! Before getting started, please take a moment to %sread the documentation%s :) ', self::TEXT_DOMAIN), '<a href="http://docs.woothemes.com/document/woocommerce-product-reviews-pro/" target="_blank">', '</a>'), 'read-the-docs-notice', array('always_show_on_settings' => false, 'notice_class' => 'updated'));
        }
        /**
         * Clear transients for a review.
         *
         * TODO: Once https://github.com/woothemes/woocommerce/pull/6284 makes it to the core, we can remove this
         *
         * @param mixed $post_id the post identifier
         */
        public function clear_transients($post_id)
        {
            $post_id = absint($post_id);
            delete_transient('wc_rating_count_' . $post_id . '_1');
            delete_transient('wc_rating_count_' . $post_id . '_2');
            delete_transient('wc_rating_count_' . $post_id . '_3');
            delete_transient('wc_rating_count_' . $post_id . '_4');
            delete_transient('wc_rating_count_' . $post_id . '_5');
        }
        /** Helper methods ******************************************************/
        /**
         * Main Product Reviews Pro Instance, ensures only one instance is/can be loaded
         *
         * @since 1.0.0
         * @see wc_product_reviews_pro()
         * @return WC_Product_Reviews_Pro
         */
        public static function instance()
        {
            if (is_null(self::$instance)) {
                self::$instance = new self();
            }
            return self::$instance;
        }
        /**
         * Returns the plugin name, localized
         *
         * @since 1.0.0
         * @see SV_WC_Plugin::get_plugin_name()
         * @return string the plugin name
         */
        public function get_plugin_name()
        {
            return __('WooCommerce Product Reviews Pro', $this->text_domain);
        }
        /**
         * Returns __FILE__
         *
         * @since 1.0.0
         * @see SV_WC_Plugin::get_file()
         * @return string the full path and filename of the plugin file
         */
        protected function get_file()
        {
            return __FILE__;
        }
        /**
         * Gets the URL to the settings page
         *
         * @since 1.0.0
         * @see SV_WC_Plugin::is_plugin_settings()
         * @param string $_ unused
         * @return string URL to the settings page
         */
        public function get_settings_url($_ = '')
        {
            return admin_url('admin.php?page=wc-settings&tab=products');
        }
        /**
         * Returns true if on the gateway settings page
         *
         * @since 1.0.0
         * @see SV_WC_Plugin::is_plugin_settings()
         * @return boolean true if on the settings page
         */
        public function is_plugin_settings()
        {
            return isset($_GET['page']) && 'reviews' == $_GET['page'];
        }
        /** Lifecycle methods ******************************************************/
        /**
         * Installation routine
         *
         * @since 1.0.0
         * @see SV_WC_Plugin::install()
         */
        protected function install()
        {
            global $wpdb;
            // Default settings
            update_option('wc_product_reviews_pro_enabled_contribution_types', 'all');
            update_option('wc_product_reviews_pro_contributions_orderby', 'most_helpful');
            update_option('wc_product_reviews_pro_contribution_moderation', get_option('comment_moderation') ? 'yes' : 'no');
            // Set comment_type to 'review' on all comments that have a product as
            // their parent and no type set.  Page through comments in blocks to
            // avoid out of memory errors
            $offset = (int) get_option('wc_product_reviews_pro_install_offset', 0);
            $records_per_page = 500;
            do {
                $record_ids = get_comments(array('post_type' => 'product', 'type' => '', 'fields' => 'ids', 'offset' => $offset, 'number' => $records_per_page));
                // some sort of bad database error: deactivate the plugin and display an error
                if (is_wp_error($record_ids)) {
                    require_once ABSPATH . 'wp-admin/includes/plugin.php';
                    deactivate_plugins('woocommerce-product-reviews-pro/woocommerce-product-reviews-pro.php');
                    wp_die(sprintf(__('Error activating and installing %s: %s', self::TEXT_DOMAIN), $this->get_plugin_name(), '<ul><li>' . implode('</li><li>', $record_ids->get_error_messages()) . '</li></ul>') . '<a href="' . admin_url('plugins.php') . '">' . __('&laquo; Go Back', self::TEXT_DOMAIN) . '</a>');
                }
                if (is_array($record_ids)) {
                    foreach ($record_ids as $id) {
                        $wpdb->query("UPDATE {$wpdb->comments} SET comment_type = 'review' WHERE comment_type = '' AND comment_ID = {$id}");
                    }
                }
                // increment offset
                $offset += $records_per_page;
                // and keep track of how far we made it in case we hit a script timeout
                update_option('wc_product_reviews_pro_install_offset', $offset);
            } while (count($record_ids) == $records_per_page);
            // while full set of results returned  (meaning there may be more results still to retrieve)
        }
    }
    /**
     * Returns the One True Instance of Product Reviews Pro
     *
     * @since 1.0.0
     * @return WC_Product_Reviews_Pro
     */
    function wc_product_reviews_pro()
    {
        return WC_Product_Reviews_Pro::instance();
    }
    // fire it up!
    wc_product_reviews_pro();
}
/**
 * Main function for returning contributions, uses the WC_Product_Reviews_Pro_Contribution_Factory class.
 *
 * @since 1.0.0
 * @param mixed $the_contribution Comment object or comment ID of the contribution.
 * @param array $args (default: array()) Contains all arguments to be used to get this contribution.
 * @return WC_Contribution
 */
function wc_product_reviews_pro_get_contribution($the_contribution = false, $args = array())
{
    return wc_product_reviews_pro()->contribution_factory->wc_product_reviews_pro_get_contribution($the_contribution, $args);
}
/**
 * Display comment form for contribution comments
 *
 * @since 1.0.0
 */
?>

<div class="contribution-comment-form">

	<form action="<?php 
echo site_url('/wp-comments-post.php');
?>
" method="post" enctype="multipart/form-data" novalidate>

		<?php 
foreach (wc_product_reviews_pro()->frontend->get_contribution_fields('contribution_comment') as $key => $field) {
    ?>

			<?php 
    woocommerce_form_field($key, $field);
    ?>

		<?php 
}
?>

		<input type="hidden" name="comment_type" value="contribution_comment" />
		<input type="hidden" name="comment_post_ID" value="<?php 
the_ID();
?>
">
 /**
  * Replace Edit/Moderate Comment title/headline with Edit {$type}, when editing/moderating a contribution
  *
  * @param  string $translation Translated text.
  * @param  string $text        Text to translate.
  * @return string              Translated text.
  */
 public function filter_edit_comments_screen_translations($translation, $text)
 {
     $replace_texts = array('Edit Comment', 'Moderate Comment');
     // Bail out if not a text we should replace
     if (!in_array($text, $replace_texts)) {
         return $translation;
     }
     global $comment;
     // Try to get comment from query params
     if (!$comment && isset($_GET['action']) && 'editcomment' == $_GET['action'] && isset($_GET['c'])) {
         $comment_id = intval($_GET['c']);
         $comment = get_comment($comment_id);
     }
     // Bail out if no comment type is set
     if (!$comment || !$comment->comment_type) {
         return $translation;
     }
     $contribution_types = wc_product_reviews_pro()->get_contribution_types();
     // Only replace the translated text if we are editing a comment left on a product,
     // which effectively means it's a review
     if (in_array($comment->comment_type, $contribution_types)) {
         $contribution_type = wc_product_reviews_pro_get_contribution_type($comment->comment_type);
         switch ($text) {
             case 'Edit Comment':
                 $translation = $contribution_type->get_edit_text();
                 break;
             case 'Moderate Comment':
                 $translation = $contribution_type->get_moderate_text();
                 break;
         }
     }
     return $translation;
 }
 /**
  * Customize the review product tab
  *
  * Will replace the review tab title with a more generic
  * one if multiple contribution types are enabled, or
  * with a specific title, if only one type is enabled.
  *
  * @since 1.0.0
  * @param array $tabs
  * @return array
  */
 public function customize_review_tab($tabs)
 {
     global $post;
     if (isset($tabs['reviews'])) {
         $enabled_contribution_types = wc_product_reviews_pro()->get_enabled_contribution_types();
         // Do not take contribution_comments into account
         if (($key = array_search('contribution_comment', $enabled_contribution_types)) !== false) {
             unset($enabled_contribution_types[$key]);
         }
         // Hide reviews tab if none of the types are enabled
         if (empty($enabled_contribution_types)) {
             unset($tabs['reviews']);
         } elseif (count($enabled_contribution_types) == 1) {
             $type = $enabled_contribution_types[0];
             $contribution_type = wc_product_reviews_pro_get_contribution_type($type);
             $count = wc_product_reviews_pro_get_comments_number($post->ID, $type);
             $tabs['reviews']['title'] = $contribution_type->get_tab_title($count);
         } else {
             $count = wc_product_reviews_pro_get_comments_number($post->ID, $enabled_contribution_types);
             $contribution_type = wc_product_reviews_pro_get_contribution_type(null);
             $tabs['reviews']['title'] = $contribution_type->get_tab_title($count);
         }
     }
     return $tabs;
 }
 /**
  * Add contribution types as allowed comment types for avatars
  *
  * @since 1.0.0
  * @param array $allowed_types
  * @return array
  */
 public function add_contribution_avatar_types($allowed_types)
 {
     $contribution_types = array_keys(wc_product_reviews_pro()->get_contribution_types());
     return array_unique(array_merge($allowed_types, $contribution_types));
 }
Example #7
0
 * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
 */
if (!defined('ABSPATH')) {
    exit;
}
// Exit if accessed directly
/**
 * Display single product reviews (comments)
 *
 * @since 1.0.0
 */
global $product;
if (!comments_open()) {
    return;
}
$contribution_types = wc_product_reviews_pro()->get_enabled_contribution_types();
$ratings = array(5, 4, 3, 2, 1);
$total_rating_count = $product->get_rating_count();
?>
<div id="reviews">

	<h2 class="contributions-title"><?php 
_e('Share your thoughts!', WC_Product_Reviews_Pro::TEXT_DOMAIN);
?>
</h2>

	<?php 
// Product ratings
?>
	<?php 
if ('yes' == get_option('woocommerce_enable_review_rating') && $product->get_rating_count()) {
Example #8
0
    return $count;
}
add_filter('loop_shop_per_page', 'dl_sort_by_page');
add_action('woocommerce_before_shop_loop', 'woocommerce_catalog_page_ordering', 40);
// change pagination .prev & .next labels
add_filter('woocommerce_pagination_args', 'custom_override_pagination_args');
function custom_override_pagination_args($args)
{
    $args['prev_text'] = __('Previous Page');
    $args['next_text'] = __('Next Page');
    return $args;
}
// change place for "my contributions"
if (function_exists('wc_product_reviews_pro') && wc_product_reviews_pro()) {
    remove_action('woocommerce_before_my_account', array(wc_product_reviews_pro()->frontend, 'render_my_account_contributions'), 11);
    add_action('woocommerce_after_my_account', array(wc_product_reviews_pro()->frontend, 'render_my_account_contributions'), 5);
}
// change place for wishlists
add_filter('woocommerce_wishlists_account_location', 'change_woocommerce_wishlists_account_location', 10, 1);
function change_woocommerce_wishlists_account_location($location)
{
    return 'before';
}
// Remove quantity in all product type
function wc_remove_all_quantity_fields($return, $product)
{
    return true;
}
add_filter('woocommerce_is_sold_individually', 'wc_remove_all_quantity_fields', 10, 2);
// change place for price
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10, 2);