Advertisement
Guest User

AV - Vote

a guest
Jun 17th, 2012
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         wp_enqueue_script('av-vote', DOC_VOTE_BASE_URL . 'js/av_vote.js', array( 'jquery' ));
  35.         wp_localize_script('av-vote', 'av_vote_vars',
  36.             array(
  37.                 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  38.                 'nonce' => wp_create_nonce('av-vote-nonce'),
  39.                 'thanks' => 'Thanks for your feedback.',
  40.                 'error_message' => 'Sorry, there was a problem processing your request.'
  41.             )
  42.         );
  43.         wp_register_style('vote_standalone_doc_survey', DOC_VOTE_BASE_URL . 'includes/style.css');
  44.         wp_enqueue_style('vote_standalone_doc_survey', DOC_VOTE_BASE_URL . 'includes/style.css');
  45.     }
  46.    
  47.        
  48.     function __construct(){
  49.     $this->hooks();
  50.     }
  51.     function hooks(){
  52.         //add_action('wp_enqueue_scripts', 'vote_load_js');
  53.         add_action('wp_head',array($this,'vote_load_js'));
  54.         add_action( 'wp_ajax_register_vote', array($this,'handle_vote') );
  55.     }
  56.  
  57.     function av_vsd_display_link(){
  58.         global $wpdb;
  59.         //Show Vote option only if Embeds_On_Page == 0
  60.         global $post;
  61.            
  62.         $doc_id = $post->ID;
  63.            
  64.         $vote_count = $wpdb->get_results($wpdb->prepare("SELECT votes FROM wp_vote_standalone_doc WHERE doc_id = %d", $doc_id));
  65.         if(is_array($vote_count)){$vote_count = intval($vote_count[0]->votes);}
  66.         if(is_int($vote_count)){
  67.             if($vote_count == 1){$vote_or_votes = 'Vote';}else{$vote_or_votes = 'Votes';} // Display "vote" or "votes"
  68.             if(isset($doc_id)){
  69.                 $html = '<div class="questions"><span id="question1"><a href="#" class="vote_for_doc" data-doc_id="'.$doc_id.'">
  70.                 I would like to have this document integrated into CT2</a></span>
  71.                    
  72.                 <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>
  73.                    
  74.                 <span id="status">(<span class="vote-count">' . $vote_count . '</span> '.$vote_or_votes.' for ' . get_the_title($doc_id) . ')</span></div>';
  75.  
  76.                 }else{ // get_the_title failed
  77.                     $html = 'The title for this document was not found.';
  78.                 }
  79.                    
  80.           }else{
  81.               if(is_string($vote_count)){
  82.                   $html = '$vote_count is a string in the database<p>' . print_r($vote_count);
  83.               }if(is_array($vote_count)){
  84.                     $html = '$vote_count is an array in the database<p>' . print_r($vote_count);
  85.               }else{
  86.                   $html = '$vote_count is not a string or an integer<p>' . var_dump($vote_count);
  87.               }
  88.           }
  89.         //}
  90.         return $html;
  91.     }  
  92.     function handle_vote(){
  93.         global $wpdb;
  94.            
  95.             /*if( !is_user_logged_in() )
  96.             return false;*/
  97.            
  98.             if( !wp_verify_nonce( $_POST['nonce'], 'av-vote-nonce' ))
  99.             die( 'Go away!');
  100.            
  101.             $get_old_votes = $wpdb->get_results($wpdb->prepare("SELECT votes FROM wp_vote_standalone_doc WHERE doc_id = %d", $_POST['doc_id']));
  102.             if(is_array($get_old_votes)){$get_old_votes = $get_old_votes[0]->votes;}
  103.            
  104.             $old_drilldown = $wpdb->get_results($wpdb->prepare("SELECT drilldown FROM wp_vote_standalone_doc WHERE doc_id = %d", $_POST['doc_id']));
  105.             if(is_array($get_old_votes)){$get_old_votes = $get_old_votes[0]->votes;}
  106.            
  107.             if($get_old_votes){
  108.                 $new_votes = $get_old_votes + 1;
  109.                 $new_drilldown = intval($_POST['drilldown']) + intval($old_drilldown);
  110.                 if(is_int($new_votes)){
  111.                    
  112.                 $update_vote = $wpdb->update( 'wp_vote_standalone_doc',
  113.                     array( 'votes' => $new_votes,
  114.                         'drilldown' => $new_drilldown),
  115.                         array( 'doc_id' => $_POST['doc_id']),
  116.                         array( '%d', '%d' ),
  117.                         array( '%d' )
  118.                     );
  119.                
  120.                 if(!$update_vote){
  121.                     echo "Vote not recorded" . $wpdb->print_error();
  122.                 }else{
  123.                     echo "Vote recorded";
  124.                 }
  125.                 }
  126.                 //$wpdb->print_error();
  127.             }else{ // New row entry
  128.                 $insert_row = $wpdb->insert( 'wp_vote_standalone_doc',
  129.                     array( 'doc_id' => $_POST['doc_id'],
  130.                     'drilldown' => $_POST['drilldown'],
  131.                     'votes' => 1),
  132.                     array(  '%d','%d','%d' )
  133.                 );
  134.            
  135.                 //$wpdb->print_error();
  136.                 if(!$insert_row){
  137.                     echo "Vote not recorded";
  138.                 }else{
  139.                     echo "Vote recorded";
  140.                 }
  141.             }
  142.         //}
  143.         die();
  144.     }
  145.  
  146. } // END CLASS MY_LIKE_BUTTON
  147.  
  148. $vote_button = new Vote_Standalone_Doc;
  149. add_shortcode( 'vote', array('Vote_Standalone_Doc', 'av_vsd_display_link') );
  150. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement