Advertisement
Guest User

wp-customer-reviews.php

a guest
Jan 30th, 2013
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 59.43 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Plugin Name: WP Customer Reviews
  4.  * Plugin URI: http://www.gowebsolutions.com/plugins/wp-customer-reviews/
  5.  * Description: WP Customer Reviews allows your customers and visitors to leave reviews or testimonials of your services. Reviews are Microformat enabled (hReview).
  6.  * Version: 2.4.5
  7.  * Revision Date: June 7, 2012
  8.  * Requires at least: WP 2.8.6
  9.  * Tested up to: WP 3.4
  10.  * Author: Go Web Solutions
  11.  * Author URI: http://www.gowebsolutions.com/
  12.  * License: GNU General Public License
  13.  *
  14.  * This program is free software; you can redistribute it and/or modify
  15.  * it under the terms of the GNU General Public License as published by
  16.  * the Free Software Foundation; either version 2 of the License, or
  17.  * (at your option) any later version.
  18.  *
  19.  * This program is distributed in the hope that it will be useful,
  20.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.  * GNU General Public License for more details.
  23.  *
  24.  * You should have received a copy of the GNU General Public License
  25.  * along with this program; if not, write to the Free Software
  26.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  27.  *
  28.  */
  29.  
  30. class WPCustomerReviews {
  31.  
  32.     var $dbtable = 'wpcreviews';
  33.     var $force_active_page = false;
  34.     var $got_aggregate = false;
  35.     var $options = array();
  36.     var $p = '';
  37.     var $page = 1;
  38.     var $plugin_version = '0.0.0';
  39.     var $shown_form = false;
  40.     var $shown_hcard = false;
  41.     var $status_msg = '';
  42.  
  43.     function WPCustomerReviews() {
  44.         global $wpdb;
  45.  
  46. global $show_only_one_time;
  47. $show_only_one_time=0;
  48.  
  49.         define('IN_WPCR', 1);
  50.        
  51.         /* uncomment the below block to display strict/notice errors */
  52.         /*
  53.         restore_error_handler();
  54.         error_reporting(E_ALL);
  55.         ini_set('error_reporting', E_ALL);
  56.         ini_set('html_errors',TRUE);
  57.         ini_set('display_errors',TRUE);
  58.         */
  59.  
  60.         $this->dbtable = $wpdb->prefix . $this->dbtable;
  61.         $this->plugin_version = $this->plugin_get_version();
  62.  
  63.         add_action('the_content', array(&$this, 'do_the_content'), 10); /* prio 10 prevents a conflict with some odd themes */
  64.         add_action('init', array(&$this, 'init')); /* init also tries to insert script/styles */
  65.         add_action('admin_init', array(&$this, 'admin_init'));
  66.                
  67.         /* try multiple methods of inserting our scripts and styles */
  68.         /*
  69.         add_action('wp_print_styles',array(&$this, 'add_style_script'));
  70.         add_action('wp_print_scripts',array(&$this, 'add_style_script'));
  71.         add_action('wp_head',array(&$this, 'add_style_script'), 0);
  72.         */
  73.        
  74.         add_action('template_redirect',array(&$this, 'template_redirect')); /* handle redirects and form posts, and add style/script if needed */
  75.        
  76.         add_action('admin_menu', array(&$this, 'addmenu'));
  77.         add_action('wp_ajax_update_field', array(&$this, 'admin_view_reviews')); /* special ajax stuff */
  78.         add_action('save_post', array(&$this, 'admin_save_post'), 10, 2); /* 2 arguments */
  79.        
  80.         add_filter('plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'plugin_settings_link'));
  81.     }
  82.  
  83.     /* keep out of admin file */
  84.     function plugin_settings_link($links) {
  85.         $url = get_admin_url().'options-general.php?page=wpcr_options';
  86.         $settings_link = '<a href="'.$url.'"><img src="' . $this->getpluginurl() . 'star.png" />&nbsp;Settings</a>';
  87.         array_unshift($links, $settings_link);
  88.         return $links;
  89.     }
  90.  
  91.     /* keep out of admin file */
  92.     function addmenu() {
  93.         add_options_page('Customer Reviews', '<img src="' . $this->getpluginurl() . 'star.png" />&nbsp;Customer Reviews', 'manage_options', 'wpcr_options', array(&$this, 'admin_options'));
  94.         add_menu_page('Customer Reviews', 'Customer Reviews', 'edit_others_posts', 'wpcr_view_reviews', array(&$this, 'admin_view_reviews'), $this->getpluginurl() . 'star.png', 50); /* 50 should be underneath comments */
  95.  
  96.         global $WPCustomerReviewsAdmin;
  97.         $this->include_admin(); /* include admin functions */
  98.         $WPCustomerReviewsAdmin->wpcr_add_meta_box();
  99.     }
  100.  
  101.     /* forward to admin file */
  102.     function admin_options() {
  103.         global $WPCustomerReviewsAdmin;
  104.         $this->include_admin(); /* include admin functions */
  105.         $WPCustomerReviewsAdmin->real_admin_options();
  106.     }
  107.  
  108.     /* forward to admin file */
  109.     function admin_save_post($post_id, $post) {
  110.         global $WPCustomerReviewsAdmin;
  111.         $this->include_admin(); /* include admin functions */
  112.         $WPCustomerReviewsAdmin->real_admin_save_post($post_id);
  113.     }
  114.  
  115.     /* forward to admin file */
  116.     function admin_view_reviews() {
  117.         global $WPCustomerReviewsAdmin;
  118.         $this->include_admin(); /* include admin functions */
  119.         $WPCustomerReviewsAdmin->real_admin_view_reviews();
  120.     }
  121.    
  122.     /* returns current plugin version */
  123.     function plugin_get_version() {
  124.         require_once( ABSPATH . 'wp-admin/includes/plugin.php');
  125.         $plugin_data = get_plugin_data( __FILE__ );
  126.         $plugin_version = $plugin_data['Version'];
  127.         return $plugin_version;
  128.     }
  129.    
  130.     function get_jumplink_for_review($review,$page) {
  131.         /* $page will be 1 for shortcode usage since it pulls most recent, which SHOULD all be on page 1 */
  132.         $link = get_permalink( $review->page_id );
  133.        
  134.         if (strpos($link,'?') === false) {
  135.             $link = trailingslashit($link) . "?wpcrp=$page#hreview-$review->id";
  136.         } else {
  137.             $link = $link . "&wpcrp=$page#hreview-$review->id";
  138.         }
  139.        
  140.         return $link;
  141.     }
  142.  
  143.     function get_options() {
  144.         $home_domain = @parse_url(get_home_url());
  145.         $home_domain = $home_domain['scheme'] . "://" . $home_domain['host'] . '/';
  146.  
  147.         $default_options = array(
  148.             'act_email' => '',
  149.             'act_uniq' => '',
  150.             'activate' => 0,
  151.             'ask_custom' => array(),
  152.             'ask_fields' => array('fname' => 1, 'femail' => 1, 'fwebsite' => 1, 'ftitle' => 1, 'fage' => 0, 'fgender' => 0),
  153.             'business_city' => '',
  154.             'business_country' => 'USA',
  155.             'business_email' => get_bloginfo('admin_email'),
  156.             'business_name' => get_bloginfo('name'),
  157.             'business_phone' => '',
  158.             'business_state' => '',
  159.             'business_street' => '',
  160.             'business_url' => $home_domain,
  161.             'business_zip' => '',
  162.             'dbversion' => 0,
  163.             'enable_posts_default' => 0,
  164.             'enable_pages_default' => 0,
  165.             'field_custom' => array(),
  166.             'form_location' => 0,
  167.             'goto_leave_text' => 'Click here to submit your review.',
  168.             'goto_show_button' => 1,
  169.             'hreview_type' => 'business',
  170.             'leave_text' => 'Submit your review',
  171.             'require_custom' => array(),
  172.             'require_fields' => array('fname' => 1, 'femail' => 1, 'fwebsite' => 0, 'ftitle' => 0, 'fage' => 0, 'fgender' => 0),
  173.             'reviews_per_page' => 10,
  174.             'show_custom' => array(),
  175.             'show_fields' => array('fname' => 1, 'femail' => 0, 'fwebsite' => 0, 'ftitle' => 1, 'fage' => 0, 'fgender' => 0),
  176.             'show_hcard' => 1,
  177.             'show_hcard_on' => 1,
  178.             'submit_button_text' => 'Submit your review',
  179.             'support_us' => 1,
  180.             'title_tag' => 'h2'
  181.         );
  182.        
  183.         $this->options = get_option('wpcr_options', $default_options);
  184.  
  185.         /* magically easy migrations to newer versions */
  186.         $has_new = false;
  187.         foreach ($default_options as $col => $def_val) {
  188.  
  189.             if (!isset($this->options[$col])) {
  190.                 $this->options[$col] = $def_val;
  191.                 $has_new = true;
  192.             }
  193.  
  194.             if (is_array($def_val)) {
  195.                 foreach ($def_val as $acol => $aval) {
  196.                     if (!isset($this->options[$col][$acol])) {
  197.                         $this->options[$col][$acol] = $aval;
  198.                         $has_new = true;
  199.                     }
  200.                 }
  201.             }
  202.         }
  203.  
  204.         if ($has_new) {
  205.             update_option('wpcr_options', $this->options);
  206.         }
  207.     }
  208.  
  209.     function make_p_obj() {
  210.         $this->p = new stdClass();
  211.  
  212.         foreach ($_GET as $c => $val) {
  213.             if (is_array($val)) {
  214.                 $this->p->$c = $val;
  215.             } else {
  216.                 $this->p->$c = trim(stripslashes($val));
  217.             }
  218.         }
  219.  
  220.         foreach ($_POST as $c => $val) {
  221.             if (is_array($val)) {
  222.                 $this->p->$c = $val;
  223.             } else {
  224.                 $this->p->$c = trim(stripslashes($val));
  225.             }
  226.         }
  227.     }
  228.  
  229.     function check_migrate() {
  230.         global $wpdb;
  231.         $migrated = false;
  232.  
  233.         /* remove me after official release */
  234.         $current_dbversion = intval(str_replace('.', '', $this->options['dbversion']));
  235.         $plugin_db_version = intval(str_replace('.', '', $this->plugin_version));
  236.  
  237.         if ($current_dbversion == $plugin_db_version) {
  238.             return false;
  239.         }
  240.        
  241.         global $WPCustomerReviewsAdmin;
  242.         $this->include_admin(); /* include admin functions */
  243.         $WPCustomerReviewsAdmin->createUpdateReviewtable(); /* creates AND updates table */
  244.  
  245.         /* initial installation */
  246.         if ($current_dbversion == 0) {
  247.             $this->options['dbversion'] = $plugin_db_version;
  248.             $current_dbversion = $plugin_db_version;
  249.             update_option('wpcr_options', $this->options);
  250.             return false;
  251.         }
  252.  
  253.         /* check for upgrades if needed */
  254.  
  255.         /* upgrade to 2.0.0 */
  256.         if ($current_dbversion < 200) {
  257.             /* add multiple page support to database */
  258.  
  259.             /* change all current reviews to use the selected page id */
  260.             $pageID = intval($this->options['selected_pageid']);
  261.             $wpdb->query("UPDATE `$this->dbtable` SET `page_id`=$pageID WHERE `page_id`=0");
  262.  
  263.             /* add new meta to existing selected page */
  264.             update_post_meta($pageID, 'wpcr_enable', 1);
  265.  
  266.             $this->options['dbversion'] = 200;
  267.             $current_dbversion = 200;
  268.             update_option('wpcr_options', $this->options);
  269.             $migrated = true;
  270.         }
  271.  
  272.         /* done with all migrations, push dbversion to current version */
  273.         if ($current_dbversion != $plugin_db_version || $migrated == true) {
  274.             $this->options['dbversion'] = $plugin_db_version;
  275.             $current_dbversion = $plugin_db_version;
  276.             update_option('wpcr_options', $this->options);
  277.  
  278.             global $WPCustomerReviewsAdmin;
  279.             $this->include_admin(); /* include admin functions */
  280.             $WPCustomerReviewsAdmin->notify_activate(3);
  281.             $WPCustomerReviewsAdmin->force_update_cache(); /* update any caches */
  282.  
  283.             return true;
  284.         }
  285.  
  286.         return false;
  287.     }
  288.    
  289.     function is_active_page() {
  290.         global $post;
  291.        
  292. global $show_only_one_time;
  293. if ( $show_only_one_time == 1 ) return false;
  294.  
  295.         $has_shortcode = $this->force_active_page;
  296.         if ( $has_shortcode !== false ) {
  297. $show_only_one_time++;
  298.             return 'shortcode';
  299.         }
  300.        
  301.         if ( !isset($post) || !isset($post->ID) || intval($post->ID) == 0 ) {
  302.             return false; /* we can only use the plugin if we have a valid post ID */
  303.         }
  304.        
  305.         if (!is_singular()) {
  306.             return false; /* not on a single post/page view */
  307.         }
  308.        
  309.         $wpcr_enabled_post = get_post_meta($post->ID, 'wpcr_enable', true);
  310.         if ( $wpcr_enabled_post ) {
  311. $show_only_one_time++;
  312.             return 'enabled';
  313.         }
  314.        
  315.         return false;
  316.     }
  317.    
  318.     function add_style_script() {
  319.         /* to prevent compatibility issues and for shortcodes, add to every page */
  320.         wp_enqueue_style('wp-customer-reviews');
  321.         wp_enqueue_script('wp-customer-reviews');
  322.     }
  323.    
  324.     function template_redirect() {
  325.    
  326.         /* do this in template_redirect so we can try to redirect cleanly */
  327.         global $post;
  328.         if (!isset($post) || !isset($post->ID)) {
  329.             $post = new stdClass();
  330.             $post->ID = 0;
  331.         }
  332.        
  333.         if (isset($_COOKIE['wpcr_status_msg'])) {
  334.             $this->status_msg = $_COOKIE['wpcr_status_msg'];
  335.             if ( !headers_sent() ) {
  336.                 setcookie('wpcr_status_msg', '', time() - 3600); /* delete the cookie */
  337.                 unset($_COOKIE['wpcr_status_msg']);
  338.             }
  339.         }
  340.        
  341.         $GET_P = "submitwpcr_$post->ID";
  342.  
  343.         if ($post->ID > 0 && isset($this->p->$GET_P) && $this->p->$GET_P == $this->options['submit_button_text'])
  344.         {
  345.             $msg = $this->add_review($post->ID);
  346.             $has_error = $msg[0];
  347.             $status_msg = $msg[1];
  348.             $url = get_permalink($post->ID);
  349.             $cookie = array('wpcr_status_msg' => $status_msg);
  350.             $this->wpcr_redirect($url, $cookie);
  351.         }
  352.     }
  353.    
  354.     function rand_string($length) {
  355.         $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  356.         $str = '';
  357.  
  358.         $size = strlen($chars);
  359.         for ($i = 0; $i < $length; $i++) {
  360.             $str .= $chars[rand(0, $size - 1)];
  361.         }
  362.  
  363.         return $str;
  364.     }
  365.  
  366.     function get_aggregate_reviews($pageID) {
  367.         if ($this->got_aggregate !== false) {
  368.             return $this->got_aggregate;
  369.         }
  370.  
  371.         global $wpdb;
  372.  
  373.         $pageID = intval($pageID);
  374.         $row = $wpdb->get_results("SELECT COUNT(*) AS `total`,AVG(review_rating) AS `aggregate_rating`,MAX(review_rating) AS `max_rating` FROM `$this->dbtable` WHERE `page_id`=$pageID AND `status`=1");
  375.  
  376.         /* make sure we have at least one review before continuing below */
  377.         if ($wpdb->num_rows == 0 || $row[0]->total == 0) {
  378.             $this->got_aggregate = array("aggregate" => 0, "max" => 0, "total" => 0, "text" => 'Reviews for my site');
  379.             return false;
  380.         }
  381.  
  382.         $aggregate_rating = $row[0]->aggregate_rating;
  383.         $max_rating = $row[0]->max_rating;
  384.         $total_reviews = $row[0]->total;
  385.  
  386.         $row = $wpdb->get_results("SELECT `review_text` FROM `$this->dbtable` WHERE `page_id`=$pageID AND `status`=1 ORDER BY `date_time` DESC LIMIT 1");
  387.         $sample_text = substr($row[0]->review_text, 0, 180);
  388.  
  389.         $this->got_aggregate = array("aggregate" => $aggregate_rating, "max" => $max_rating, "total" => $total_reviews, "text" => $sample_text);
  390.         return true;
  391.     }
  392.  
  393.     function get_reviews($postID, $startpage, $perpage, $status, $random) {
  394.         global $wpdb;
  395.  
  396.         $startpage = $startpage - 1; /* mysql starts at 0 instead of 1, so reduce them all by 1 */
  397.         if ($startpage < 0) { $startpage = 0; }
  398.  
  399.         $limit = 'LIMIT ' . $startpage * $perpage . ',' . $perpage;
  400.  
  401.         if ($status == -1) {
  402.             $qry_status = '1=1';
  403.         } else {
  404.             $qry_status = "`status`=$status";
  405.         }
  406.  
  407.         $postID = intval($postID);
  408.         if ($postID == -1) {
  409.             $and_post = '';
  410.         } else {
  411.             $and_post = "AND `page_id`=$postID";
  412.         }
  413.  
  414. if ( $random ) {
  415.         $reviews = $wpdb->get_results("SELECT
  416.            `id`,
  417.            `date_time`,
  418.            `reviewer_name`,
  419.            `reviewer_email`,
  420.            `review_title`,
  421.            `review_text`,
  422.            `review_response`,
  423.            `review_rating`,
  424.            `reviewer_url`,
  425.            `reviewer_ip`,
  426.            `status`,
  427.            `page_id`,
  428.            `custom_fields`
  429.            FROM `$this->dbtable` WHERE $qry_status $and_post ORDER BY RAND() DESC $limit
  430.            ");
  431. }
  432. else {
  433.         $reviews = $wpdb->get_results("SELECT
  434.            `id`,
  435.            `date_time`,
  436.            `reviewer_name`,
  437.            `reviewer_email`,
  438.            `review_title`,
  439.            `review_text`,
  440.            `review_response`,
  441.            `review_rating`,
  442.            `reviewer_url`,
  443.            `reviewer_ip`,
  444.            `status`,
  445.            `page_id`,
  446.            `custom_fields`
  447.            FROM `$this->dbtable` WHERE $qry_status $and_post ORDER BY `date_time` DESC $limit
  448.            ");
  449. }
  450.  
  451.         $total_reviews = $wpdb->get_results("SELECT COUNT(*) AS `total` FROM `$this->dbtable` WHERE $qry_status $and_post");
  452.         $total_reviews = $total_reviews[0]->total;
  453.  
  454.         return array($reviews, $total_reviews);
  455.     }
  456.  
  457.     function aggregate_footer() {
  458.        
  459.         $aggregate_footer_output = '';
  460.        
  461.         if ($this->options['show_hcard_on'] != 0 && $this->shown_hcard === false) {
  462.  
  463.             $this->shown_hcard = true;
  464.  
  465.             /* start - make sure we should continue */
  466.             $show = false;
  467.  
  468.             if ( $this->options['show_hcard_on'] == 1 ) {
  469.                 $show = true;
  470.             } else if ( $this->options['show_hcard_on'] == 2 && ( is_home() || is_front_page() ) ) {
  471.                 $show = true;
  472.             } else if ( $this->options['show_hcard_on'] == 3 && $this->is_active_page() ) {
  473.                 $show = true;
  474.             }
  475.             /* end - make sure we should continue */
  476.            
  477.             $div_id = "wpcr_hcard_h";
  478.             if ( $this->is_active_page() ) {
  479.                 if ( $this->options['show_hcard'] == 1 ) {
  480.                     $div_id = "wpcr_hcard_s";
  481.                 }
  482.             }
  483.  
  484.             if ($show) { /* we append like this to prevent newlines and wpautop issues */
  485.                
  486.                 $aggregate_footer_output = '<div id="' . $div_id . '" class="vcard">';
  487.                 $aggregate_footer_output .= '<a class="url fn org" href="' . $this->options['business_url'] . '">' . $this->options['business_name'] . '</a><br />';
  488.                
  489.                 if (
  490.                         $this->options['business_street'] != '' ||
  491.                         $this->options['business_city'] != '' ||
  492.                         $this->options['business_state'] != '' ||
  493.                         $this->options['business_zip'] != '' ||
  494.                         $this->options['business_country'] != ''
  495.                    )
  496.                 {
  497.                     $aggregate_footer_output .= '<span class="adr">';
  498.                     if ($this->options['business_street'] != '') {
  499.                         $aggregate_footer_output .= '<span class="street-address">' . $this->options['business_street'] . '</span>&nbsp;';
  500.                     }
  501.                     if ($this->options['business_city'] != '') {
  502.                         $aggregate_footer_output .='<span class="locality">' . $this->options['business_city'] . '</span>,&nbsp;';
  503.                     }
  504.                     if ($this->options['business_state'] != '') {
  505.                         $aggregate_footer_output .='<span class="region">' . $this->options['business_state'] . '</span>,&nbsp;';
  506.                     }
  507.                     if ($this->options['business_zip'] != '') {
  508.                         $aggregate_footer_output .='<span class="postal-code">' . $this->options['business_zip'] . '</span>&nbsp;';
  509.                     }
  510.                     if ($this->options['business_country'] != '') {
  511.                         $aggregate_footer_output .='<span class="country-name">' . $this->options['business_country'] . '</span>&nbsp;';
  512.                     }
  513.  
  514.                     $aggregate_footer_output .= '</span>';
  515.                 }
  516.  
  517.                 if ($this->options['business_email'] != '' && $this->options['business_phone'] != '') {
  518.                     $aggregate_footer_output .= '<br />';
  519.                 }
  520.  
  521.                 if ($this->options['business_email'] != '') {
  522.                     $aggregate_footer_output .= '<a class="email" href="mailto:' . $this->options['business_email'] . '">' . $this->options['business_email'] . '</a>';
  523.                 }
  524.                 if ($this->options['business_email'] != '' && $this->options['business_phone'] != '') {
  525.                     $aggregate_footer_output .= '&nbsp;&bull;&nbsp';
  526.                 }
  527.                 if ($this->options['business_phone'] != '') {
  528.                     $aggregate_footer_output .= '<span class="tel">' . $this->options['business_phone'] . '</span>';
  529.                 }
  530.  
  531.                 $aggregate_footer_output .= '</div>';
  532.             }
  533.         }
  534.  
  535.         return $aggregate_footer_output;
  536.     }
  537.  
  538.     function iso8601($time=false) {
  539.         if ($time === false)
  540.             $time = time();
  541.         $date = date('Y-m-d\TH:i:sO', $time);
  542.         return (substr($date, 0, strlen($date) - 2) . ':' . substr($date, -2));
  543.     }
  544.  
  545.     function pagination($total_results, $reviews_per_page) {
  546.         global $post; /* will exist if on a post */
  547.  
  548.         $out = '';
  549.         $uri = false;
  550.         $pretty = false;
  551.  
  552.         $range = 2;
  553.         $showitems = ($range * 2) + 1;
  554.  
  555.         $paged = $this->page;
  556.         if ($paged == 0) { $paged = 1; }
  557.        
  558.         if (!isset($this->p->review_status)) { $this->p->review_status = 0; }
  559.  
  560.         $pages = ceil($total_results / $reviews_per_page);
  561.  
  562.         if ($pages > 1) {
  563.             if (is_admin()) {
  564.                 $url = '?page=wpcr_view_reviews&amp;review_status=' . $this->p->review_status . '&amp;';
  565.             } else {
  566.                 $uri = trailingslashit(get_permalink($post->ID));
  567.                 if (strpos($uri, '?') === false) {
  568.                     $url = $uri . '?';
  569.                     $pretty = true;
  570.                 } /* page is using pretty permalinks */ else {
  571.                     $url = $uri . '&amp;';
  572.                     $pretty = false;
  573.                 } /* page is using get variables for pageid */
  574.             }
  575.  
  576.             $out .= '<div id="wpcr_pagination"><div id="wpcr_pagination_page">Page: </div>';
  577.  
  578.             if ($paged > 2 && $paged > $range + 1 && $showitems < $pages) {
  579.                 if ($uri && $pretty) {
  580.                     $url2 = $uri;
  581.                 } /* not in admin AND using pretty permalinks */ else {
  582.                     $url2 = $url;
  583.                 }
  584.                 $out .= '<a href="' . $url2 . '">&laquo;</a>';
  585.             }
  586.  
  587.             if ($paged > 1 && $showitems < $pages) {
  588.                 $out .= '<a href="' . $url . 'wpcrp=' . ($paged - 1) . '">&lsaquo;</a>';
  589.             }
  590.  
  591.             for ($i = 1; $i <= $pages; $i++) {
  592.                 if ($i == $paged) {
  593.                     $out .= '<span class="wpcr_current">' . $paged . '</span>';
  594.                 } else if (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems) {
  595.                     if ($i == 1) {
  596.                         if ($uri && $pretty) {
  597.                             $url2 = $uri;
  598.                         } /* not in admin AND using pretty permalinks */ else {
  599.                             $url2 = $url;
  600.                         }
  601.                         $out .= '<a href="' . $url2 . '" class="wpcr_inactive">' . $i . '</a>';
  602.                     } else {
  603.                         $out .= '<a href="' . $url . 'wpcrp=' . $i . '" class="wpcr_inactive">' . $i . '</a>';
  604.                     }
  605.                 }
  606.             }
  607.  
  608.             if ($paged < $pages && $showitems < $pages) {
  609.                 $out .= '<a href="' . $url . 'wpcrp=' . ($paged + 1) . '">&rsaquo;</a>';
  610.             }
  611.             if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages) {
  612.                 $out .= '<a href="' . $url . 'wpcrp=' . $pages . '">&raquo;</a>';
  613.             }
  614.             $out .= '</div>';
  615.             $out .= '<div class="wpcr_clear wpcr_pb5"></div>';
  616.  
  617.             return $out;
  618.         }
  619.     }
  620.        
  621.     function output_reviews_show($inside_div, $postid, $perpage, $max, $hide_custom = 0, $hide_response = 0, $snippet_length = 0, $show_morelink = '', $random = 0) {
  622.        
  623.         if ($max != -1) {
  624.             $thispage = 1;
  625.         } else {
  626.             $thispage = $this->page;
  627.         }
  628.                
  629.         $arr_Reviews = $this->get_reviews($postid, $thispage, $perpage, 1, $random);
  630.        
  631.         $reviews = $arr_Reviews[0];
  632.         $total_reviews = intval($arr_Reviews[1]);
  633.  
  634.         $reviews_content = '';
  635.         $hidesummary = '';
  636.         $title_tag = $this->options['title_tag'];
  637.  
  638.         /* trying to access a page that does not exists -- send to main page */
  639.         if ( isset($this->p->wpcrp) && $this->p->wpcrp != 1 && count($reviews) == 0 ) {
  640.             $url = get_permalink($postid);
  641.             $this->wpcr_redirect($url);
  642.         }
  643.        
  644.         if ($postid == 0) {
  645.             /* NOTE: if using shortcode to show reviews for all pages, could do weird things when using product type */
  646.             $postid = $reviews[0]->page_id;
  647.         }
  648.  
  649.         $meta_product_name = get_post_meta($postid, 'wpcr_product_name', true);
  650.         if (!$meta_product_name) {
  651.             $meta_product_name = get_the_title($postid);
  652.         }
  653.  
  654.         $meta_product_desc = get_post_meta($postid, 'wpcr_product_desc', true);
  655.         $meta_product_brand = get_post_meta($postid, 'wpcr_product_brand', true);
  656.         $meta_product_upc = get_post_meta($postid, 'wpcr_product_upc', true);
  657.         $meta_product_sku = get_post_meta($postid, 'wpcr_product_sku', true);
  658.         $meta_product_model = get_post_meta($postid, 'wpcr_product_model', true);
  659.  
  660.         if (!$inside_div) {
  661.             $reviews_content .= '<div id="wpcr_respond_1">';
  662.         }
  663.        
  664.         if (count($reviews) == 0) {
  665.             /* $reviews_content .= '<p>There are no reviews yet. Be the first to leave yours!</p>'; */
  666.         } else {
  667.  
  668.             $this->get_aggregate_reviews($postid);
  669.  
  670.             $summary = $this->got_aggregate["text"];
  671.             $best_score = number_format($this->got_aggregate["max"], 1);
  672.             $average_score = number_format($this->got_aggregate["aggregate"], 1);
  673.  
  674.             if ($this->options['hreview_type'] == 'product') {
  675.                 $reviews_content .= '
  676.                    <span class="item hproduct" id="hproduct-' . $postid . '">
  677.                        <span class="wpcr_hide">
  678.                            <span class="brand">' . $meta_product_brand . '</span>
  679.                            <span class="fn">' . $meta_product_name . '</span>
  680.                            <span class="description">' . $meta_product_desc . '</span>
  681.                            <span class="identifier">
  682.                                <span class="type">SKU</span>
  683.                                <span class="value">' . $meta_product_sku . '</span>
  684.                            </span>
  685.                            <span class="identifier">
  686.                                <span class="type">UPC</span>
  687.                                <span class="value">' . $meta_product_upc . '</span>
  688.                            </span>
  689.                            <span class="identifier">
  690.                                <span class="type">Model</span>
  691.                                <span class="value">' . $meta_product_model . '</span>
  692.                            </span>
  693.                        </span>
  694.                    ';
  695.             }
  696.  
  697.             foreach ($reviews as $review) {
  698.                
  699.                 if ($snippet_length > 0)
  700.                 {
  701.                     $review->review_text = $this->trim_text_to_word($review->review_text,$snippet_length);
  702.                 }
  703.                
  704.                 $review->review_text .= '<br />';
  705.  
  706.                 $hide_name = '';
  707.                 if ($this->options['show_fields']['fname'] == 0) {
  708.                     $review->reviewer_name = 'Anonymous';
  709.                     $hide_name = 'wpcr_hide';
  710.                 }
  711.                 if ($review->reviewer_name == '') {
  712.                     $review->reviewer_name = 'Anonymous';
  713.                 }
  714.  
  715.                 if ($this->options['show_fields']['fwebsite'] == 1 && $review->reviewer_url != '') {
  716.                     $review->review_text .= '<br /><small><a href="' . $review->reviewer_url . '">' . $review->reviewer_url . '</a></small>';
  717.                 }
  718.                 if ($this->options['show_fields']['femail'] == 1 && $review->reviewer_email != '') {
  719.                     $review->review_text .= '<br /><small>' . $review->reviewer_email . '</small>';
  720.                 }
  721.                 if ($this->options['show_fields']['ftitle'] == 1) {
  722.                     /* do nothing */
  723.                 } else {
  724.                     $review->review_title = substr($review->review_text, 0, 150);
  725.                     $hidesummary = 'wpcr_hide';
  726.                 }
  727.                
  728.                 if ($show_morelink != '') {
  729.                     $review->review_text .= " <a href='".$this->get_jumplink_for_review($review,1)."'>$show_morelink</a>";
  730.                 }
  731.                
  732.                 $review->review_text = nl2br($review->review_text);
  733.                 $review_response = '';
  734.                
  735.                 if ($hide_response == 0)
  736.                 {
  737.                     if (strlen($review->review_response) > 0) {
  738.                         $review_response = '<p class="response"><strong>Response:</strong> ' . nl2br($review->review_response) . '</p>';
  739.                     }
  740.                 }
  741.  
  742.                 $custom_shown = '';
  743.                 if ($hide_custom == 0)
  744.                 {
  745.                     $custom_fields_unserialized = @unserialize($review->custom_fields);
  746.                     if (!is_array($custom_fields_unserialized)) {
  747.                         $custom_fields_unserialized = array();
  748.                     }
  749.                    
  750.                     foreach ($this->options['field_custom'] as $i => $val) {  
  751.                         if ( isset($custom_fields_unserialized[$val]) ) {
  752.                             $show = $this->options['show_custom'][$i];                         
  753.                             if ($show == 1 && $custom_fields_unserialized[$val] != '') {
  754.                                 $custom_shown .= "<div class='wpcr_fl'>" . $val . ': ' . $custom_fields_unserialized[$val] . '&nbsp;&bull;&nbsp;</div>';
  755.                             }
  756.                         }
  757.                     }
  758.  
  759.                     $custom_shown = preg_replace("%&bull;&nbsp;</div>$%si","</div><div class='wpcr_clear'></div>",$custom_shown);
  760.                 }
  761.  
  762.                 $name_block = '' .
  763.                     '<div class="wpcr_fl wpcr_rname">' .
  764.                     date("M d, Y", strtotime($review->date_time)) . '&nbsp;' .
  765.                     '<span class="' . $hide_name . '">by</span>&nbsp;' .
  766.                     '<span class="reviewer vcard" id="hreview-wpcr-reviewer-' . $review->id . '">' .
  767.                     '<span class="fn ' . $hide_name . '">' . $review->reviewer_name . '</span>' .
  768.                     '</span>' .
  769.                     '<div class="wpcr_clear"></div>' .
  770.                     $custom_shown .
  771.                     '</div>';
  772.  
  773.                 if ($this->options['hreview_type'] == 'product') {
  774.                     $reviews_content .= '
  775.                        <div class="hreview" id="hreview-' . $review->id . '">
  776.                            <' . $title_tag . ' class="summary ' . $hidesummary . '">' . $review->review_title . '</' . $title_tag . '>
  777.                            <span class="item" id="hreview-wpcr-hproduct-for-' . $review->id . '" style="display:none;">
  778.                                <span class="fn">' . $meta_product_name . '</span>
  779.                            </span>
  780.                            <div class="wpcr_fl wpcr_sc">
  781.                                <abbr class="rating" title="' . $review->review_rating . '"></abbr>
  782.                                <div class="wpcr_rating">
  783.                                    ' . $this->output_rating($review->review_rating, false) . '
  784.                                 </div>                 
  785.                            </div>
  786.                            ' . $name_block . '
  787.                            <div class="wpcr_clear wpcr_spacing1"></div>
  788.                            <blockquote class="description"><p>' . $review->review_text . '</p></blockquote>
  789.                            ' . $review_response . '
  790.                            <span style="display:none;" class="type">product</span>
  791.                            <span style="display:none;" class="version">0.3</span>
  792.                        </div>
  793.                        <hr />';
  794.                 } else if ($this->options['hreview_type'] == 'business') {
  795.                     $reviews_content .= '
  796.                        <div class="hreview" id="hreview-' . $review->id . '">
  797.                            <' . $title_tag . ' class="summary ' . $hidesummary . '">' . $review->review_title . '</' . $title_tag . '>
  798.                            <div class="wpcr_fl wpcr_sc">
  799.                                <abbr class="rating" title="' . $review->review_rating . '"></abbr>
  800.                                <div class="wpcr_rating">
  801.                                    ' . $this->output_rating($review->review_rating, false) . '
  802.                                 </div>                 
  803.                            </div>
  804.                            ' . $name_block . '
  805.                            <div class="wpcr_clear wpcr_spacing1"></div>
  806.                            <span class="item vcard" id="hreview-wpcr-hcard-for-' . $review->id . '" style="display:none;">
  807.                                <a class="url fn org" href="' . $this->options['business_url'] . '">' . $this->options['business_name'] . '</a>
  808.                                <span class="tel">' . $this->options['business_phone'] . '</span>
  809.                                <span class="adr">
  810.                                    <span class="street-address">' . $this->options['business_street'] . '</span>
  811.                                    <span class="locality">' . $this->options['business_city'] . '</span>
  812.                                    <span class="region">' . $this->options['business_state'] . '</span>, <span class="postal-code">' . $this->options['business_zip'] . '</span>
  813.                                    <span class="country-name">' . $this->options['business_country'] . '</span>
  814.                                </span>
  815.                            </span>
  816.                            <blockquote class="description"><p>' . $review->review_text . '</p></blockquote>
  817.                            ' . $review_response . '
  818.                            <span style="display:none;" class="type">business</span>
  819.                            <span style="display:none;" class="version">0.3</span>
  820.                       </div>
  821.                       <hr />';
  822.                 }
  823.             }
  824.  
  825.             if ($this->options['hreview_type'] == 'product') {
  826.                 $reviews_content .= '
  827.                    <span class="hreview-aggregate haggregatereview" id="hreview-wpcr-aggregate">
  828.                       <span style="display:none;">
  829.                           <span class="rating">
  830.                             <span class="average">' . $average_score . '</span>
  831.                             <span class="best">' . $best_score . '</span>
  832.                           </span>  
  833.                           <span class="votes">' . $this->got_aggregate["total"] . '</span>
  834.                           <span class="count">' . $this->got_aggregate["total"] . '</span>
  835.                           <span class="summary">' . $summary . '</span>
  836.                           <span class="item" id="hreview-wpcr-vcard">
  837.                            <span class="fn">' . $meta_product_name . '</span>
  838.                           </span>
  839.                       </span>
  840.                    </span>';
  841.                 $reviews_content .= '</span>'; /* end hProduct */
  842.             } else if ($this->options['hreview_type'] == 'business') {
  843.                 $reviews_content .= '
  844.                    <span class="hreview-aggregate" id="hreview-wpcr-aggregate">
  845.                       <span style="display:none;">
  846.                            <span class="item vcard" id="hreview-wpcr-vcard">
  847.                                <a class="url fn org" href="' . $this->options['business_url'] . '">' . $this->options['business_name'] . '</a>
  848.                                <span class="tel">' . $this->options['business_phone'] . '</span>
  849.                                <span class="adr">
  850.                                    <span class="street-address">' . $this->options['business_street'] . '</span>
  851.                                    <span class="locality">' . $this->options['business_city'] . '</span>
  852.                                    <span class="region">' . $this->options['business_state'] . '</span>, <span class="postal-code">' . $this->options['business_zip'] . '</span>
  853.                                    <span class="country-name">' . $this->options['business_country'] . '</span>
  854.                                </span>
  855.                            </span>
  856.                           <span class="rating">
  857.                                 <span class="average">' . $average_score . '</span>
  858.                                 <span class="best">' . $best_score . '</span>
  859.                           </span>  
  860.                           <span class="votes">' . $this->got_aggregate["total"] . '</span>
  861.                           <span class="count">' . $this->got_aggregate["total"] . '</span>
  862.                           <span class="summary">' . $summary . '</span>
  863.                       </span>
  864.                    </span>
  865.                    ';
  866.             }
  867.         }
  868.        
  869.         if (!$inside_div) {
  870.             $reviews_content .= '</div>'; /* wpcr_respond_1 */
  871.         }
  872.        
  873.         return array($reviews_content, $total_reviews);
  874.     }
  875.    
  876.     /* trims text, but does not break up a word */
  877.     function trim_text_to_word($text,$len) {
  878.         if(strlen($text) > $len) {
  879.           $matches = array();
  880.           preg_match("/^(.{1,$len})[\s]/i", $text, $matches);
  881.           $text = $matches[0];
  882.         }
  883.         return $text.'... ';
  884.     }
  885.  
  886.     function do_the_content($original_content) {
  887.         global $post;
  888.        
  889.         $using_shortcode_insert = false;
  890.         if ($original_content == 'shortcode_insert') {
  891.             $original_content = '';
  892.             $using_shortcode_insert = true;
  893.         }
  894.        
  895.         $the_content = '';
  896.         $is_active_page = $this->is_active_page();
  897.        
  898.         /* return normal content if this is not an enabled page, or if this is a post not on single post view */
  899.         if (!$is_active_page) {
  900.             $the_content .= '<div id="wpcr_respond_1">';
  901.             $the_content .= $this->aggregate_footer(); /* check if we need to show something in the footer then */
  902.             $the_content .= '</div>';
  903.             return $original_content . $the_content;
  904.         }
  905.        
  906.         $the_content .= '<div id="wpcr_respond_1">'; /* start the div */
  907.         $inside_div = true;
  908.        
  909.         if ($this->options['form_location'] == 0) {
  910.             $the_content .= $this->show_reviews_form();
  911.         }
  912.  
  913.         $ret_Arr = $this->output_reviews_show( $inside_div, $post->ID, $this->options['reviews_per_page'], -1 );
  914.         $the_content .= $ret_Arr[0];
  915.         $total_reviews = $ret_Arr[1];
  916.        
  917.         $the_content .= $this->pagination($total_reviews, $this->options['reviews_per_page']);
  918.  
  919.         if ($this->options['form_location'] == 1) {
  920.             $the_content .= $this->show_reviews_form();
  921.         }
  922.  
  923.         if ($this->options['support_us'] == 1) {
  924.             $the_content .= '<div class="wpcr_clear wpcr_power">Powered by <strong><a href="http://www.gowebsolutions.com/plugins/wp-customer-reviews/">WP Customer Reviews</a></strong></div>';
  925.         }
  926.        
  927.         $the_content .= $this->aggregate_footer(); /* check if we need to show something in the footer also */
  928.        
  929.         $the_content .= '</div>'; /* wpcr_respond_1 */
  930.  
  931.         //$the_content = preg_replace('/\n\r|\r\n|\n|\r|\t|\s{2}/', '', $the_content); /* minify to prevent automatic line breaks */
  932.         $the_content = preg_replace('/\n\r|\r\n|\n|\r|\t/', '', $the_content); /* minify to prevent automatic line breaks, not removing double spaces */
  933.  
  934.         return $original_content . $the_content;
  935.     }
  936.  
  937.     function output_rating($rating, $enable_hover) {
  938.         $out = '';
  939.  
  940.         $rating_width = 20 * $rating; /* 20% for each star if having 5 stars */
  941.  
  942.         $out .= '<div class="sp_rating">';
  943.  
  944.         if ($enable_hover) {
  945.             $out .= '<div class="status"><div class="score"><a class="score1">1</a><a class="score2">2</a><a class="score3">3</a><a class="score4">4</a><a class="score5">5</a></div></div>';
  946.         }
  947.  
  948.         $out .= '<div class="base"><div class="average" style="width:' . $rating_width . '%"></div></div>';
  949.         $out .= '</div>';
  950.  
  951.         return $out;
  952.     }
  953.  
  954.     function show_reviews_form() {
  955.         global $post, $current_user;
  956.  
  957.         $fields = '';
  958.         $out = '';
  959.         $req_js = "<script type='text/javascript'>";
  960.  
  961.         if ( isset($_COOKIE['wpcr_status_msg']) ) {
  962.             $this->status_msg = $_COOKIE['wpcr_status_msg'];
  963.         }
  964.        
  965.         if ($this->status_msg != '') {
  966.             $req_js .= "wpcr_del_cookie('wpcr_status_msg');";
  967.         }
  968.  
  969.         /* a silly and crazy but effective antispam measure.. bots wont have a clue */
  970.         $rand_prefixes = array();
  971.         for ($i = 0; $i < 15; $i++) {
  972.             $rand_prefixes[] = $this->rand_string(mt_rand(1, 8));
  973.         }
  974.        
  975.         if (!isset($this->p->fname)) { $this->p->fname = ''; }
  976.         if (!isset($this->p->femail)) { $this->p->femail = ''; }
  977.         if (!isset($this->p->fwebsite)) { $this->p->fwebsite = ''; }
  978.         if (!isset($this->p->ftitle)) { $this->p->ftitle = ''; }
  979.         if (!isset($this->p->ftext)) { $this->p->ftext = ''; }
  980.  
  981.         if ($this->options['ask_fields']['fname'] == 1) {
  982.             if ($this->options['require_fields']['fname'] == 1) {
  983.                 $req = '*';
  984.             } else {
  985.                 $req = '';
  986.             }
  987.             $fields .= '<tr><td><label for="' . $rand_prefixes[0] . '-fname" class="comment-field">Name: ' . $req . '</label></td><td><input class="text-input" type="text" id="' . $rand_prefixes[0] . '-fname" name="' . $rand_prefixes[0] . '-fname" value="' . $this->p->fname . '" /></td></tr>';
  988.         }
  989.         if ($this->options['ask_fields']['femail'] == 1) {
  990.             if ($this->options['require_fields']['femail'] == 1) {
  991.                 $req = '*';
  992.             } else {
  993.                 $req = '';
  994.             }
  995.             $fields .= '<tr><td><label for="' . $rand_prefixes[1] . '-femail" class="comment-field">Email: ' . $req . '</label></td><td><input class="text-input" type="text" id="' . $rand_prefixes[1] . '-femail" name="' . $rand_prefixes[1] . '-femail" value="' . $this->p->femail . '" /></td></tr>';
  996.         }
  997.         if ($this->options['ask_fields']['fwebsite'] == 1) {
  998.             if ($this->options['require_fields']['fwebsite'] == 1) {
  999.                 $req = '*';
  1000.             } else {
  1001.                 $req = '';
  1002.             }
  1003.             $fields .= '<tr><td><label for="' . $rand_prefixes[2] . '-fwebsite" class="comment-field">Website: ' . $req . '</label></td><td><input class="text-input" type="text" id="' . $rand_prefixes[2] . '-fwebsite" name="' . $rand_prefixes[2] . '-fwebsite" value="' . $this->p->fwebsite . '" /></td></tr>';
  1004.         }
  1005.         if ($this->options['ask_fields']['ftitle'] == 1) {
  1006.             if ($this->options['require_fields']['ftitle'] == 1) {
  1007.                 $req = '*';
  1008.             } else {
  1009.                 $req = '';
  1010.             }
  1011.             $fields .= '<tr><td><label for="' . $rand_prefixes[3] . '-ftitle" class="comment-field">Review Title: ' . $req . '</label></td><td><input class="text-input" type="text" id="' . $rand_prefixes[3] . '-ftitle" name="' . $rand_prefixes[3] . '-ftitle" maxlength="150" value="' . $this->p->ftitle . '" /></td></tr>';
  1012.         }
  1013.  
  1014.         $custom_fields = array(); /* used for insert as well */
  1015.         $custom_count = count($this->options['field_custom']); /* used for insert as well */
  1016.         for ($i = 0; $i < $custom_count; $i++) {
  1017.             $custom_fields[$i] = $this->options['field_custom'][$i];
  1018.         }
  1019.  
  1020.         foreach ($this->options['ask_custom'] as $i => $val) {
  1021.             if ( isset($this->options['ask_custom'][$i]) ) {
  1022.                 if ($val == 1) {
  1023.                     if ($this->options['require_custom'][$i] == 1) {
  1024.                         $req = '*';
  1025.                     } else {
  1026.                         $req = '';
  1027.                     }
  1028.  
  1029.                     $custom_i = "custom_$i";
  1030.                     if (!isset($this->p->$custom_i)) { $this->p->$custom_i = ''; }
  1031.                     $fields .= '<tr><td><label for="custom_' . $i . '" class="comment-field">' . $custom_fields[$i] . ': ' . $req . '</label></td><td><input class="text-input" type="text" id="custom_' . $i . '" name="custom_' . $i . '" maxlength="150" value="' . $this->p->$custom_i . '" /></td></tr>';
  1032.                 }
  1033.             }
  1034.         }
  1035.  
  1036.         $some_required = '';
  1037.        
  1038.         foreach ($this->options['require_fields'] as $col => $val) {
  1039.             if ($val == 1) {
  1040.                 $col = str_replace("'","\'",$col);
  1041.                 $req_js .= "wpcr_req.push('$col');";
  1042.                 $some_required = '<small>* Required Field</small>';
  1043.             }
  1044.         }
  1045.  
  1046.         foreach ($this->options['require_custom'] as $i => $val) {
  1047.             if ($val == 1) {
  1048.                 $req_js .= "wpcr_req.push('custom_$i');";
  1049.                 $some_required = '<small>* Required Field</small>';
  1050.             }
  1051.         }
  1052.        
  1053.         $req_js .= "</script>\n";
  1054.        
  1055.         if ($this->options['goto_show_button'] == 1) {
  1056.             $button_html = '<div class="wpcr_status_msg">' . $this->status_msg . '</div>'; /* show errors or thank you message here */
  1057.             $button_html .= '<p><a id="wpcr_button_1" href="javascript:void(0);">' . $this->options['goto_leave_text'] . '</a></p><hr />';
  1058.             $out .= $button_html;
  1059.         }
  1060.  
  1061.         /* different output variables make it easier to debug this section */
  1062.         $out .= '<div id="wpcr_respond_2">' . $req_js . '
  1063.                    <form class="wpcrcform" id="wpcr_commentform" method="post" action="javascript:void(0);">
  1064.                        <div id="wpcr_div_2">
  1065.                            <input type="hidden" id="frating" name="frating" />
  1066.                            <table id="wpcr_table_2">
  1067.                                <tbody>
  1068.                                    <tr><td colspan="2"><div id="wpcr_postcomment">' . $this->options["leave_text"] . '</div></td></tr>
  1069.                                    ' . $fields;
  1070.  
  1071.         $out2 = '  
  1072.            <tr>
  1073.                <td><label class="comment-field">Rating:</label></td>
  1074.                <td><div class="wpcr_rating">' . $this->output_rating(0, true) . '</div></td>
  1075.            </tr>';
  1076.  
  1077.         $out3 = '
  1078.                            <tr><td colspan="2"><label for="' . $rand_prefixes[5] . '-ftext" class="comment-field">Review:</label></td></tr>
  1079.                            <tr><td colspan="2"><textarea id="' . $rand_prefixes[5] . '-ftext" name="' . $rand_prefixes[5] . '-ftext" rows="8" cols="50">' . $this->p->ftext . '</textarea></td></tr>
  1080.                            <tr>
  1081.                                <td colspan="2" id="wpcr_check_confirm">
  1082.                                    ' . $some_required . '
  1083.                                    <div class="wpcr_clear"></div>    
  1084.                                    <input type="checkbox" name="' . $rand_prefixes[6] . '-fconfirm1" id="fconfirm1" value="1" />
  1085.                                    <div class="wpcr_fl"><input type="checkbox" name="' . $rand_prefixes[7] . '-fconfirm2" id="fconfirm2" value="1" /></div><div class="wpcr_fl" style="margin:-2px 0px 0px 5px"><label for="fconfirm2">Check this box to confirm you are human.</label></div>
  1086.                                    <div class="wpcr_clear"></div>
  1087.                                    <input type="checkbox" name="' . $rand_prefixes[8] . '-fconfirm3" id="fconfirm3" value="1" />
  1088.                                </td>
  1089.                            </tr>
  1090.                            <tr><td colspan="2"><input id="wpcr_submit_btn" name="submitwpcr_' . $post->ID . '" type="submit" value="' . $this->options['submit_button_text'] . '" /></td></tr>
  1091.                        </tbody>
  1092.                    </table>
  1093.                </div>
  1094.            </form>';
  1095.  
  1096.         $out4 = '<hr /></div>';
  1097.         $out4 .= '<div class="wpcr_clear wpcr_pb5"></div>';
  1098.  
  1099.         return $out . $out2 . $out3 . $out4;
  1100.     }
  1101.  
  1102.     function add_review($pageID) {
  1103.         global $wpdb;
  1104.  
  1105.         /* begin - some antispam magic */
  1106.         $this->newp = new stdClass();
  1107.  
  1108.         foreach ($this->p as $col => $val) {
  1109.             $pos = strpos($col, '-');
  1110.             if ($pos !== false) {
  1111.                 $col = substr($col, $pos + 1); /* off by one */
  1112.             }
  1113.             $this->newp->$col = $val;
  1114.         }
  1115.  
  1116.         $this->p = $this->newp;
  1117.         unset($this->newp);
  1118.         /* end - some antispam magic */
  1119.  
  1120.         /* some sanitation */
  1121.         $date_time = date('Y-m-d H:i:s');
  1122.         $ip = $_SERVER['REMOTE_ADDR'];
  1123.        
  1124.         if (!isset($this->p->fname)) { $this->p->fname = ''; }
  1125.         if (!isset($this->p->femail)) { $this->p->femail = ''; }
  1126.         if (!isset($this->p->fwebsite)) { $this->p->fwebsite = ''; }
  1127.         if (!isset($this->p->ftitle)) { $this->p->ftitle = ''; }
  1128.         if (!isset($this->p->ftext)) { $this->p->ftext = ''; }
  1129.         if (!isset($this->p->femail)) { $this->p->femail = ''; }
  1130.         if (!isset($this->p->fwebsite)) { $this->p->fwebsite = ''; }
  1131.         if (!isset($this->p->frating)) { $this->p->frating = 0; } /* default to 0 */
  1132.         if (!isset($this->p->fconfirm1)) { $this->p->fconfirm1 = 0; } /* default to 0 */
  1133.         if (!isset($this->p->fconfirm2)) { $this->p->fconfirm2 = 0; } /* default to 0 */
  1134.         if (!isset($this->p->fconfirm3)) { $this->p->fconfirm3 = 0; } /* default to 0 */
  1135.        
  1136.         $this->p->fname = trim(strip_tags($this->p->fname));
  1137.         $this->p->femail = trim(strip_tags($this->p->femail));
  1138.         $this->p->ftitle = trim(strip_tags($this->p->ftitle));
  1139.         $this->p->ftext = trim(strip_tags($this->p->ftext));
  1140.         $this->p->frating = intval($this->p->frating);
  1141.  
  1142.         /* begin - server-side validation */
  1143.         $errors = '';
  1144.  
  1145.         foreach ($this->options['require_fields'] as $col => $val) {
  1146.             if ($val == 1) {
  1147.                 if (!isset($this->p->$col) || $this->p->$col == '') {
  1148.                     $nice_name = ucfirst(substr($col, 1));
  1149.                     $errors .= 'You must include your ' . $nice_name . '.<br />';
  1150.                 }
  1151.             }
  1152.         }
  1153.  
  1154.         $custom_fields = array(); /* used for insert as well */
  1155.         $custom_count = count($this->options['field_custom']); /* used for insert as well */
  1156.         for ($i = 0; $i < $custom_count; $i++) {
  1157.             $custom_fields[$i] = $this->options['field_custom'][$i];
  1158.         }
  1159.  
  1160.         foreach ($this->options['require_custom'] as $i => $val) {
  1161.             if ($val == 1) {
  1162.                 $custom_i = "custom_$i";
  1163.                 if (!isset($this->p->$custom_i) || $this->p->$custom_i == '') {
  1164.                     $nice_name = $custom_fields[$i];
  1165.                     $errors .= 'You must include your ' . $nice_name . '.<br />';
  1166.                 }
  1167.             }
  1168.         }
  1169.        
  1170.         /* only do regex matching if not blank */
  1171.         if ($this->p->femail != '' && $this->options['ask_fields']['femail'] == 1) {
  1172.             if (!preg_match('/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/', $this->p->femail)) {
  1173.                 $errors .= 'The email address provided is not valid.<br />';
  1174.             }
  1175.         }
  1176.  
  1177.         /* only do regex matching if not blank */
  1178.         if ($this->p->fwebsite != '' && $this->options['ask_fields']['fwebsite'] == 1) {
  1179.             if (!preg_match('/^\S+:\/\/\S+\.\S+.+$/', $this->p->fwebsite)) {
  1180.                 $errors .= 'The website provided is not valid. Be sure to include http://<br />';
  1181.             }
  1182.         }
  1183.  
  1184.         if (intval($this->p->fconfirm1) == 1 || intval($this->p->fconfirm3) == 1) {
  1185.             $errors .= 'You have triggered our anti-spam system. Please try again. Code 001.<br />';
  1186.         }
  1187.  
  1188.         if (intval($this->p->fconfirm2) != 1) {
  1189.             $errors .= 'You have triggered our anti-spam system. Please try again. Code 002<br />';
  1190.         }
  1191.  
  1192.         if ($this->p->frating < 1 || $this->p->frating > 5) {
  1193.             $errors .= 'You have triggered our anti-spam system. Please try again. Code 003<br />';
  1194.         }
  1195.  
  1196.         if (strlen(trim($this->p->ftext)) < 30) {
  1197.             $errors .= 'You must include a review. Please make reviews at least a couple of sentences.<br />';
  1198.         }
  1199.  
  1200.         /* returns true for errors */
  1201.         if ($errors) {
  1202.             return array(true, "<div>$errors</div>");
  1203.         }
  1204.         /* end - server-side validation */
  1205.  
  1206.         $custom_insert = array();      
  1207.         for ($i = 0; $i < $custom_count; $i++) {       
  1208.             if ($this->options['ask_custom'][$i] == 1) {
  1209.                 $name = $custom_fields[$i];
  1210.                 $custom_i = "custom_$i";               
  1211.                 if ( isset($this->p->$custom_i) ) {
  1212.                     $custom_insert[$name] = ucfirst($this->p->$custom_i);
  1213.                 }
  1214.             }
  1215.         }
  1216.         $custom_insert = serialize($custom_insert);
  1217.  
  1218.         $query = $wpdb->prepare("INSERT INTO `$this->dbtable`
  1219.                (`date_time`, `reviewer_name`, `reviewer_email`, `reviewer_ip`, `review_title`, `review_text`, `status`, `review_rating`, `reviewer_url`, `custom_fields`, `page_id`)
  1220.                VALUES (%s, %s, %s, %s, %s, %s, %d, %d, %s, %s, %d)", $date_time, $this->p->fname, $this->p->femail, $ip, $this->p->ftitle, $this->p->ftext, 0, $this->p->frating, $this->p->fwebsite, $custom_insert, $pageID);
  1221.  
  1222.         $wpdb->query($query);
  1223.  
  1224.         $admin_link = get_admin_url().'admin.php?page=wpcr_view_reviews';
  1225.         $admin_link = "Link to admin approval page: $admin_link";
  1226.  
  1227.         @wp_mail(get_bloginfo('admin_email'), "WP Customer Reviews: New Review Posted on " . date('m/d/Y h:i'), "A new review has been posted for " . $this->options['business_name'] . " via WP Customer Reviews. \n\nYou will need to login to the admin area and approve this review before it will appear on your site.\n\n{$admin_link}");
  1228.  
  1229.         /* returns false for no error */
  1230.         return array(false, '<div>Thank you for your comments.</div>');
  1231.     }
  1232.  
  1233.     function deactivate() {
  1234.         /* do not fire on upgrading plugin or upgrading WP - only on true manual deactivation */
  1235.         if (isset($this->p->action) && $this->p->action == 'deactivate') {
  1236.             $this->options['activate'] = 0;
  1237.             update_option('wpcr_options', $this->options);
  1238.             global $WPCustomerReviewsAdmin;
  1239.             $this->include_admin(); /* include admin functions */
  1240.             $WPCustomerReviewsAdmin->notify_activate(2);
  1241.         }
  1242.     }
  1243.  
  1244.     function wpcr_redirect($url, $cookie = array()) {
  1245.        
  1246.         $headers_sent = headers_sent();
  1247.        
  1248.         if ($headers_sent == true) {
  1249.             /* use JS redirect and add cookie before redirect */
  1250.             /* we do not html comment script blocks here - to prevent any issues with other plugins adding content to newlines, etc */
  1251.             $out = "<html><head><title>Redirecting...</title></head><body><div style='clear:both;text-align:center;padding:10px;'>" .
  1252.                     "Processing... Please wait..." .
  1253.                     "<script type='text/javascript'>";
  1254.             foreach ($cookie as $col => $val) {
  1255.                 $val = preg_replace("/\r?\n/", "\\n", addslashes($val));
  1256.                 $out .= "document.cookie=\"$col=$val\";";
  1257.             }
  1258.             $out .= "window.location='$url';";
  1259.             $out .= "</script>";
  1260.             $out .= "</div></body></html>";
  1261.             echo $out;
  1262.         } else {
  1263.             foreach ($cookie as $col => $val) {
  1264.                 setcookie($col, $val); /* add cookie via headers */
  1265.             }
  1266.             ob_end_clean();
  1267.             wp_redirect($url); /* nice redirect */
  1268.         }
  1269.        
  1270.         exit();
  1271.     }
  1272.  
  1273.     function init() { /* used for admin_init also */
  1274.         $this->make_p_obj(); /* make P variables object */
  1275.         $this->get_options(); /* populate the options array */
  1276.         $this->check_migrate(); /* call on every instance to see if we have upgraded in any way */
  1277.  
  1278.         if ( !isset($this->p->wpcrp) ) { $this->p->wpcrp = 1; }
  1279.        
  1280.         $this->page = intval($this->p->wpcrp);
  1281.         if ($this->page < 1) { $this->page = 1; }
  1282.        
  1283.         add_shortcode( 'WPCR_INSERT', array(&$this, 'shortcode_wpcr_insert') );
  1284.         add_shortcode( 'WPCR_SHOW', array(&$this, 'shortcode_wpcr_show') );
  1285.        
  1286.         wp_register_style('wp-customer-reviews', $this->getpluginurl() . 'wp-customer-reviews.css', array(), $this->plugin_version);
  1287.         wp_register_script('wp-customer-reviews', $this->getpluginurl() . 'wp-customer-reviews.js', array('jquery'), $this->plugin_version);
  1288.         /* add style and script here if needed for some theme compatibility */
  1289.         $this->add_style_script();
  1290.     }
  1291.    
  1292.     function shortcode_wpcr_insert() {
  1293.         $this->force_active_page = 1;
  1294.         return $this->do_the_content('shortcode_insert');        
  1295.     }
  1296.    
  1297.     function shortcode_wpcr_show($atts) {
  1298.         $this->force_active_page = 1;
  1299.        
  1300.         extract( shortcode_atts( array('postid' => 'all','num' => '3','hidecustom' => '0','hideresponse' => '0', 'snippet' => '0','more' => '', 'random' => '0'), $atts ) );
  1301.        
  1302.         if (strtolower($postid) == 'all') { $postid = -1; /* -1 queries all reviews */ }
  1303.         $postid = intval($postid);
  1304.         $num = intval($num);
  1305.         $hidecustom = intval($hidecustom);
  1306.         $hideresponse = intval($hideresponse);
  1307.         $snippet = intval($snippet);
  1308.         $random = intval($random);
  1309.         $more = $more;
  1310.        
  1311.         if ($postid < -1) { $postid = -1; }
  1312.         if ($num < 1) { $num = 3; }
  1313.         if ($hidecustom < 0 || $hidecustom > 1) { $hidecustom = 0; }
  1314.         if ($hideresponse < 0 || $hideresponse > 1) { $hideresponse = 0; }
  1315.         if ($snippet < 0) { $snippet = 0; }
  1316.        
  1317.         $inside_div = false;
  1318.        
  1319.         $ret_Arr = $this->output_reviews_show( $inside_div, $postid, $num, $num, $hidecustom, $hideresponse, $snippet, $more, $random );
  1320.         return $ret_Arr[0];
  1321.     }
  1322.  
  1323.     function activate() {
  1324.         register_setting('wpcr_gotosettings', 'wpcr_gotosettings');
  1325.         add_option('wpcr_gotosettings', true); /* used for redirecting to settings page upon initial activation */
  1326.     }
  1327.  
  1328.     function include_admin() {
  1329.         global $WPCustomerReviewsAdmin;
  1330.         require_once($this->getplugindir() . 'wp-customer-reviews-admin.php'); /* include admin functions */
  1331.     }
  1332.  
  1333.     function admin_init() {
  1334.         global $WPCustomerReviewsAdmin;
  1335.         $this->include_admin(); /* include admin functions */
  1336.         $WPCustomerReviewsAdmin->real_admin_init();
  1337.     }
  1338.  
  1339.     function getpluginurl() {
  1340.         return trailingslashit(plugins_url(basename(dirname(__FILE__))));
  1341.     }
  1342.  
  1343.     function getplugindir() {
  1344.         return trailingslashit(WP_PLUGIN_DIR . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)));
  1345.     }
  1346.  
  1347. }
  1348.  
  1349. if (!defined('IN_WPCR')) {
  1350.     global $WPCustomerReviews;
  1351.     $WPCustomerReviews = new WPCustomerReviews();
  1352.     register_activation_hook(__FILE__, array(&$WPCustomerReviews, 'activate'));
  1353.     register_deactivation_hook(__FILE__, array(&$WPCustomerReviews, 'deactivate'));
  1354. }
  1355. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement