Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: AV - Vote for Standalone Document
- Plugin URI: http://pippinsplugins.com/write-a-love-it-plugin-with-ajax/
- Description: AJAX voting on posts/pages
- Version: 1.0
- Author: Alex Vallejo
- Author URI:
- */
- if(!defined('DOC_VOTE_BASE_DIR')){
- define('DOC_VOTE_BASE_DIR', dirname(__FILE__));
- }
- if(!defined('DOC_VOTE_BASE_URL')){
- define('DOC_VOTE_BASE_URL',plugin_dir_url(__FILE__));
- }
- function vote_standalone_doc_table(){
- global $wpdb;
- $sql = "CREATE TABLE IF NOT EXISTS wp_vote_standalone_doc (
- ID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
- doc_id SMALLINT NOT NULL,
- votes SMALLINT NOT NULL,
- drilldown SMALLINT NOT NULL
- )";
- require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
- dbDelta($sql);
- }
- register_activation_hook( __FILE__, 'vote_standalone_doc_table' );
- class Vote_Standalone_Doc{
- function vote_load_js(){
- wp_enqueue_script('av-vote', DOC_VOTE_BASE_URL . 'js/av_vote.js', array( 'jquery' ));
- wp_localize_script('av-vote', 'av_vote_vars',
- array(
- 'ajaxurl' => admin_url( 'admin-ajax.php' ),
- 'nonce' => wp_create_nonce('av-vote-nonce'),
- 'thanks' => 'Thanks for your feedback.',
- 'error_message' => 'Sorry, there was a problem processing your request.'
- )
- );
- wp_register_style('vote_standalone_doc_survey', DOC_VOTE_BASE_URL . 'includes/style.css');
- wp_enqueue_style('vote_standalone_doc_survey', DOC_VOTE_BASE_URL . 'includes/style.css');
- }
- function __construct(){
- $this->hooks();
- }
- function hooks(){
- //add_action('wp_enqueue_scripts', 'vote_load_js');
- add_action('wp_head',array($this,'vote_load_js'));
- add_action( 'wp_ajax_register_vote', array($this,'handle_vote') );
- }
- function av_vsd_display_link(){
- global $wpdb;
- //Show Vote option only if Embeds_On_Page == 0
- global $post;
- $doc_id = $post->ID;
- $vote_count = $wpdb->get_results($wpdb->prepare("SELECT votes FROM wp_vote_standalone_doc WHERE doc_id = %d", $doc_id));
- if(is_array($vote_count)){$vote_count = intval($vote_count[0]->votes);}
- if(is_int($vote_count)){
- if($vote_count == 1){$vote_or_votes = 'Vote';}else{$vote_or_votes = 'Votes';} // Display "vote" or "votes"
- if(isset($doc_id)){
- $html = '<div class="questions"><span id="question1"><a href="#" class="vote_for_doc" data-doc_id="'.$doc_id.'">
- I would like to have this document integrated into CT2</a></span>
- <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>
- <span id="status">(<span class="vote-count">' . $vote_count . '</span> '.$vote_or_votes.' for ' . get_the_title($doc_id) . ')</span></div>';
- }else{ // get_the_title failed
- $html = 'The title for this document was not found.';
- }
- }else{
- if(is_string($vote_count)){
- $html = '$vote_count is a string in the database<p>' . print_r($vote_count);
- }if(is_array($vote_count)){
- $html = '$vote_count is an array in the database<p>' . print_r($vote_count);
- }else{
- $html = '$vote_count is not a string or an integer<p>' . var_dump($vote_count);
- }
- }
- //}
- return $html;
- }
- function handle_vote(){
- global $wpdb;
- /*if( !is_user_logged_in() )
- return false;*/
- if( !wp_verify_nonce( $_POST['nonce'], 'av-vote-nonce' ))
- die( 'Go away!');
- $get_old_votes = $wpdb->get_results($wpdb->prepare("SELECT votes FROM wp_vote_standalone_doc WHERE doc_id = %d", $_POST['doc_id']));
- if(is_array($get_old_votes)){$get_old_votes = $get_old_votes[0]->votes;}
- $old_drilldown = $wpdb->get_results($wpdb->prepare("SELECT drilldown FROM wp_vote_standalone_doc WHERE doc_id = %d", $_POST['doc_id']));
- if(is_array($get_old_votes)){$get_old_votes = $get_old_votes[0]->votes;}
- if($get_old_votes){
- $new_votes = $get_old_votes + 1;
- $new_drilldown = intval($_POST['drilldown']) + intval($old_drilldown);
- if(is_int($new_votes)){
- $update_vote = $wpdb->update( 'wp_vote_standalone_doc',
- array( 'votes' => $new_votes,
- 'drilldown' => $new_drilldown),
- array( 'doc_id' => $_POST['doc_id']),
- array( '%d', '%d' ),
- array( '%d' )
- );
- if(!$update_vote){
- echo "Vote not recorded" . $wpdb->print_error();
- }else{
- echo "Vote recorded";
- }
- }
- //$wpdb->print_error();
- }else{ // New row entry
- $insert_row = $wpdb->insert( 'wp_vote_standalone_doc',
- array( 'doc_id' => $_POST['doc_id'],
- 'drilldown' => $_POST['drilldown'],
- 'votes' => 1),
- array( '%d','%d','%d' )
- );
- //$wpdb->print_error();
- if(!$insert_row){
- echo "Vote not recorded";
- }else{
- echo "Vote recorded";
- }
- }
- //}
- die();
- }
- } // END CLASS MY_LIKE_BUTTON
- $vote_button = new Vote_Standalone_Doc;
- add_shortcode( 'vote', array('Vote_Standalone_Doc', 'av_vsd_display_link') );
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement