Guest User

AV - Vote Button (ajax)

a guest
Jun 17th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.25 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: AV - Vote for Standalone Document
  4. Plugin URI: http://pippinsplugins.com/write-a-love-it-plugin-with-ajax/
  5. Description: AJAX voting on posts/pages
  6. Version: 1.0
  7. Author: Alex Vallejo
  8. Author URI:
  9. */
  10.  
  11. if(!defined('DOC_VOTE_BASE_DIR')){
  12.     define('DOC_VOTE_BASE_DIR', dirname(__FILE__));
  13. }
  14. if(!defined('DOC_VOTE_BASE_URL')){
  15.     define('DOC_VOTE_BASE_URL',plugin_dir_url(__FILE__));
  16. }
  17.  
  18. function vote_standalone_doc_table(){
  19. global $wpdb;
  20. $sql = "CREATE TABLE IF NOT EXISTS wp_vote_standalone_doc (
  21.                                              ID INT UNSIGNED NOT NULL AUTO_INCREMENT  PRIMARY KEY,
  22.                                              doc_id SMALLINT NOT NULL,
  23.                                              votes SMALLINT NOT NULL,
  24.                                              drilldown SMALLINT NOT NULL
  25.                                              )";
  26. require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  27. dbDelta($sql);
  28. }
  29. register_activation_hook( __FILE__, 'vote_standalone_doc_table' );
  30.  
  31. class Vote_Standalone_Doc{
  32.    
  33.     function vote_load_js(){
  34.  
  35.             wp_enqueue_script('av-vote', DOC_VOTE_BASE_URL . 'js/av_vote.js', array( 'jquery' ));
  36.             wp_localize_script('av-vote', 'av_vote_vars',
  37.                                array(
  38.                                      'ajaxurl' => admin_url( 'admin-ajax.php' ),
  39.                                      'nonce' => wp_create_nonce('av-vote-nonce'),
  40.                                      'thanks' => 'Thanks for your feedback.',
  41.                                      'error_message' => 'Sorry, there was a problem processing your request.'
  42.                                      )
  43.                                );
  44.             wp_register_style('vote_standalone_doc_survey', DOC_VOTE_BASE_URL . 'includes/style.css');
  45.             wp_enqueue_style('vote_standalone_doc_survey', DOC_VOTE_BASE_URL . 'includes/style.css');
  46.     }
  47.    
  48.        
  49.     function __construct(){
  50.     $this->hooks();
  51.     }
  52.     function hooks(){
  53.         //add_action('wp_enqueue_scripts', 'vote_load_js');
  54.         add_action('wp_head',array($this,'vote_load_js'));
  55.         add_action( 'wp_ajax_register_vote', array($this,'handle_vote') );
  56.     }
  57.  
  58.     function av_vsd_display_link(){
  59.         global $wpdb;
  60.         //Show Vote option only if Embeds_On_Page == 0
  61.         global $post;
  62.            
  63.             $doc_id = $post->ID;
  64.            
  65.             $vote_count = $wpdb->get_results($wpdb->prepare("SELECT votes FROM wp_vote_standalone_doc WHERE doc_id = %d", $doc_id));
  66.             if(is_array($vote_count)){$vote_count = intval($vote_count[0]->votes);}
  67.           if(is_int($vote_count)){
  68.                 if($vote_count == 1){$vote_or_votes = 'Vote';}else{$vote_or_votes = 'Votes';} // Display "vote" or "votes"
  69.                 if(isset($doc_id)){
  70.                     $html = '<div class="questions"><span id="question1"><a href="#" class="vote_for_doc" data-doc_id="'.$doc_id.'">
  71.                     I would like to have this document integrated into CT2</a></span>
  72.                    
  73.                     <span id="question2" class="drilldown_text">I would like the document to have in-line links to further information on various topics: <a href="#" class="drilldown" data-drilldown=1>Yes, show me drilldown content</a><a href="#" class="drilldown" data-drilldown=0>No thanks</a></span>
  74.                    
  75.                     <span id="status">(<span class="vote-count">' . $vote_count . '</span> '.$vote_or_votes.' for ' . get_the_title($doc_id) . ')</span></div>';
  76.  
  77.                 }else{ // get_the_title failed
  78.                     $html = 'The title for this document was not found.';
  79.                 }
  80.                    
  81.           }else{
  82.               if(is_string($vote_count)){
  83.                   $html = '$vote_count is a string in the database<p>' . print_r($vote_count);
  84.               }if(is_array($vote_count)){
  85.                     $html = '$vote_count is an array in the database<p>' . print_r($vote_count);
  86.               }else{
  87.                   $html = '$vote_count is not a string or an integer<p>' . var_dump($vote_count);
  88.               }
  89.           }
  90.         //}
  91.         return $html;
  92.     }  
  93.     function handle_vote(){
  94.         global $wpdb;
  95.            
  96.             /*if( !is_user_logged_in() )
  97.             return false;*/
  98.            
  99.             if( !wp_verify_nonce( $_POST['nonce'], 'av-vote-nonce' ))
  100.             die( 'Go away!');
  101.            
  102.             $get_old_votes = $wpdb->get_results($wpdb->prepare("SELECT votes FROM wp_vote_standalone_doc WHERE doc_id = %d", $_POST['doc_id']));
  103.             if(is_array($get_old_votes)){$get_old_votes = $get_old_votes[0]->votes;}
  104.            
  105.             $old_drilldown = $wpdb->get_results($wpdb->prepare("SELECT drilldown FROM wp_vote_standalone_doc WHERE doc_id = %d", $_POST['doc_id']));
  106.             if(is_array($get_old_votes)){$get_old_votes = $get_old_votes[0]->votes;}
  107.            
  108.             if($get_old_votes){
  109.                 $new_votes = $get_old_votes + 1;
  110.                 $new_drilldown = intval($_POST['drilldown']) + intval($old_drilldown);
  111.                 if(is_int($new_votes)){
  112.                    
  113.                 $update_vote = $wpdb->update( 'wp_vote_standalone_doc',
  114.                                              array( 'votes' => $new_votes,
  115.                                                    'drilldown' => $new_drilldown
  116.                                                    ),
  117.                                              array( 'doc_id' => $_POST['doc_id']
  118.                                                      ),
  119.                                              array( '%d', '%d' ),
  120.                                              array( '%d' )
  121.                                              );
  122.                
  123.                 if(!$update_vote){
  124.                     echo "Vote not recorded" . $wpdb->print_error();
  125.                 }else{
  126.                     echo "Vote recorded";
  127.                 }
  128.                 }
  129.                 //$wpdb->print_error();
  130.             }else{ // New row entry
  131.                 $insert_row = $wpdb->insert( 'wp_vote_standalone_doc',
  132.                                             array( 'doc_id' => $_POST['doc_id'],
  133.                                                     'drilldown' => $_POST['drilldown'],
  134.                                                     'votes' => 1),
  135.                                             array(  '%d','%d','%d' )
  136.                                             );
  137.            
  138.                 //$wpdb->print_error();
  139.                 if(!$insert_row){
  140.                     echo "Vote not recorded";
  141.                 }else{
  142.                     echo "Vote recorded";
  143.                 }
  144.             }
  145.         //}
  146.         die();
  147.     }
  148.  
  149. } // END CLASS MY_LIKE_BUTTON
  150.  
  151. $vote_button = new Vote_Standalone_Doc;
  152. add_shortcode( 'vote', array('Vote_Standalone_Doc', 'av_vsd_display_link') );
  153. ?>
Advertisement
Add Comment
Please, Sign In to add comment