Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Ajax Send Message
- Version: 0.1
- License: GPLv2
- */
- /**
- * Ajax Send Message class
- */
- class Ajax_send_message {
- /**
- * Constructor
- * @return void
- */
- public function __construct() {
- add_filter( 'the_content', array( $this, 'message_form' ), 10, 1 );
- add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) );
- add_action( 'wp_ajax_nopriv_send_message', array( $this, 'send_message' ) );
- add_action( 'wp_ajax_send_message', array( $this, 'send_message' ) );
- }
- /**
- * Adds the message form to the end of posts content
- * @param string $content post content
- * @return string content with form
- */
- public function message_form( $content ) {
- // display only on posts
- if ( ! is_single() ) {
- return $content;
- }
- $nonce = wp_create_nonce( 'ajax_send_message' );
- global $current_user;
- $content .= '<div class="send-a-message-box">';
- if( !is_user_logged_in() ){
- $content .= '<input type="text" class="send-a-message-display-name" placeholder="' . __( 'Enter Your name', 'ajaxsendmessage' ) . '">';
- } else{
- $content .= '<div>Podpis: '.$current_user->display_name.' <a href="'.wp_logout_url(get_permalink()).'">'. __('Logout').'</a></div>';
- }
- $content .= '<textarea class="send-a-message-msg" placeholder="' . __( 'Enter Your message', 'ajaxsendmessage' ) . '"></textarea>
- <button class="send-message" data-nonce="' . $nonce . '">' .
- __( 'Send message', 'ajaxsendmessage' ) .
- '</button>
- <p class="send-a-message-response"></p>
- </div>';
- return $content;
- }
- /**
- * Enqueue plugin scripts
- * @return void
- */
- function scripts() {
- wp_enqueue_style( 'send-a-message', plugin_dir_url( __FILE__ ) . 'css/style.css' );
- wp_enqueue_script( 'send-a-message', plugin_dir_url( __FILE__ ) . 'js/scripts.js', array( 'jquery' ), null, true );
- // set variables for script
- wp_localize_script( 'send-a-message', 'settings', array(
- 'ajaxurl' => admin_url( 'admin-ajax.php' ),
- 'send_label' => __( 'Send message', 'ajaxsendmessage' ),
- 'error' => __( 'Sorry, something goes wrong. Please try again', 'ajaxsendmessage' )
- ) );
- }
- /**
- * Sends message to database
- * @return void
- */
- function send_message() {
- $data = $_POST;
- // check the nonce
- if ( check_ajax_referer( 'ajax_send_message', 'nonce', false ) == false ) {
- wp_send_json_error();
- }
- $insert = array(
- 'msg_date' => date('Y-m-d H:i:s'),
- 'msg_body' => sanitize_text_field( $data['msg'] ),
- 'msg_status' => 0,
- 'ip' => $_SERVER['REMOTE_ADDR'],
- );
- if( !is_user_logged_in() ) {
- $insert['member_name'] = sanitize_text_field( $data['user_name'] );
- $insert['member_id']=55;
- } else {
- global $current_user;
- $insert['member_name'] = $current_user->display_name;
- $insert['member_id'] = $current_user->ID;
- }
- global $wpdb;
- $table = $wpdb->prefix .'asm_msgs';
- $result = $wpdb->insert( $table, $insert);
- if ( $result ) {
- wp_send_json_success( __( 'Thanks for your message!', 'ajaxsendmessage' ) );
- }
- else {
- wp_send_json_error($wpdb->last_error,'ajaxsendmessage');
- }
- }
- }
- new Ajax_send_message();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement