Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2017
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: Ajax Send Message
  4. Version: 0.1
  5. License: GPLv2
  6. */
  7.  
  8. /**
  9.  * Ajax Send Message class
  10.  */
  11. class Ajax_send_message {
  12.  
  13.     /**
  14.      * Constructor
  15.      * @return void
  16.      */
  17.     public function __construct() {
  18.  
  19.         add_filter( 'the_content', array( $this, 'message_form' ), 10, 1 );
  20.        
  21.         add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) );
  22.  
  23.         add_action( 'wp_ajax_nopriv_send_message', array( $this, 'send_message' ) );
  24.         add_action( 'wp_ajax_send_message', array( $this, 'send_message' ) );
  25.        
  26.     }
  27.  
  28.     /**
  29.      * Adds the message form to the end of posts content
  30.      * @param  string $content post content
  31.      * @return string content with form
  32.      */
  33.     public function message_form( $content ) {
  34.  
  35.         // display only on posts
  36.         if ( ! is_single() ) {
  37.             return $content;
  38.         }
  39.  
  40.         $nonce = wp_create_nonce( 'ajax_send_message' );
  41.     global $current_user;
  42.         $content .= '<div class="send-a-message-box">';
  43.     if( !is_user_logged_in() ){
  44.       $content .= '<input type="text" class="send-a-message-display-name" placeholder="' . __( 'Enter Your name', 'ajaxsendmessage' ) . '">';
  45.     } else{
  46.       $content .= '<div>Podpis: '.$current_user->display_name.' <a href="'.wp_logout_url(get_permalink()).'">'. __('Logout').'</a></div>';
  47.     }
  48.        
  49.         $content .= '<textarea class="send-a-message-msg" placeholder="' . __( 'Enter Your message', 'ajaxsendmessage' ) . '"></textarea>
  50.            <button class="send-message" data-nonce="' . $nonce . '">' .
  51.                             __( 'Send message', 'ajaxsendmessage' ) .
  52.                         '</button>
  53.                         <p class="send-a-message-response"></p>
  54.                     </div>';
  55.  
  56.         return $content;
  57.  
  58.     }
  59.  
  60.     /**
  61.      * Enqueue plugin scripts
  62.      * @return void
  63.      */
  64.     function scripts() {
  65.  
  66.         wp_enqueue_style( 'send-a-message', plugin_dir_url( __FILE__ ) . 'css/style.css' );
  67.  
  68.         wp_enqueue_script( 'send-a-message', plugin_dir_url( __FILE__ ) . 'js/scripts.js', array( 'jquery' ), null, true );
  69.  
  70.         // set variables for script
  71.         wp_localize_script( 'send-a-message', 'settings', array(
  72.             'ajaxurl'    => admin_url( 'admin-ajax.php' ),
  73.             'send_label' => __( 'Send message', 'ajaxsendmessage' ),
  74.             'error'      => __( 'Sorry, something goes wrong. Please try again', 'ajaxsendmessage' )
  75.         ) );
  76.  
  77.     }
  78.     /**
  79.      * Sends message to database
  80.      * @return void
  81.      */
  82.     function send_message() {
  83.    
  84.         $data = $_POST;
  85.  
  86.         // check the nonce
  87.         if ( check_ajax_referer( 'ajax_send_message', 'nonce', false ) == false ) {
  88.         wp_send_json_error();
  89.       }
  90.  
  91.     $insert = array(
  92.       'msg_date' => date('Y-m-d H:i:s'),
  93.       'msg_body' => sanitize_text_field( $data['msg'] ),
  94.       'msg_status' => 0,
  95.       'ip' => $_SERVER['REMOTE_ADDR'],
  96. );
  97.  
  98.   if( !is_user_logged_in() ) {
  99.     $insert['member_name'] = sanitize_text_field( $data['user_name'] );
  100.     $insert['member_id']=55;
  101.   } else {
  102.     global $current_user;
  103.     $insert['member_name'] = $current_user->display_name;
  104.     $insert['member_id'] = $current_user->ID;
  105.   }
  106.  
  107.     global $wpdb;
  108.         $table = $wpdb->prefix .'asm_msgs';
  109.  
  110.        
  111.     $result = $wpdb->insert( $table, $insert);
  112.  
  113.         if ( $result ) {
  114.             wp_send_json_success( __( 'Thanks for your message!', 'ajaxsendmessage' ) );
  115.         }
  116.    else {
  117.         wp_send_json_error($wpdb->last_error,'ajaxsendmessage');
  118. }
  119.  
  120.     }
  121.  
  122. }
  123.  
  124. new Ajax_send_message();
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement