Last active
September 15, 2017 04:39
-
-
Save keks55/1caa68e72f7e69f3bb911cde3fd90305 to your computer and use it in GitHub Desktop.
WP- Ajax Message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Ajax message | |
Plugin URI: http://keksus.com/wordpress-plugins/ae.html | |
Description: Send message to email with ajax form | |
Version: 0.0.1 | |
Author: Keksus | |
Author URI: http://keksus.com/ | |
Text Domain: ae | |
Domain Path: /languages/ | |
License: GPLv3 | |
Copyright 2017-2018 Keksus.com (email : [email protected]) | |
*/ | |
// this is an include only WP file | |
if (!defined('ABSPATH')){ | |
die; | |
} | |
class Ajax_Message { | |
protected $option_name = 'ajax-message'; | |
public function __construct(){ | |
register_activation_hook(__FILE__, array($this, 'activate')); | |
register_deactivation_hook(__FILE__, array($this, 'deactivate')); | |
add_action('admin_notices', array($this, 'plugin_admin_notices')); | |
add_action('wp_enqueue_scripts', array($this,'scripts_frontend')); | |
add_action('admin_enqueue_scripts', array($this,'scripts_admin')); | |
add_action( 'current_screen', array($this, 'this_screen')); | |
add_action('wp_ajax_ae_action', array($this, 'ae_action_callback')); | |
add_action('wp_ajax_nopriv_ae_action', array($this,'ae_action_callback')); | |
add_shortcode('ae_message', array($this, 'ae_message_shortcode')); | |
add_action('admin_init', array($this, 'plugin_settings')); | |
add_action('admin_menu', array($this, 'admin_page_settings')); | |
add_action( 'wp_head', array($this, 'style_to_header' )); | |
add_action('init', array($this,'ae_session')); | |
} | |
function activate(){ | |
$this->data = array( | |
'email' => get_option('admin_email'), | |
'name' => 'Name:', | |
'message' => 'Message:', | |
'submit' => 'Send message', | |
'width' => '100%', | |
'btn_color' => '#000', | |
'btn_text_color' => '#fff', | |
'subject' => 'Ajax Message', | |
'success' => 'Thank you, message sent', | |
'error' => 'Error: Message not sent', | |
'textarea' => '', | |
'captcha_on' => '1', | |
'captcha_digits' => '3' | |
); | |
update_option($this->option_name, $this->data); | |
$this->notices = get_option('plugin_admin_notices', array()); | |
$this->notices = array( | |
'content' => 'Thank you for installing this plugin! Plugin settings are available on the page | |
<a href="' . admin_url('options-general.php?page=ae_options_group'). '">Settings - Ajax Message</a>' | |
); | |
update_option('plugin_admin_notices', $this->notices); | |
} | |
function deactivate(){ | |
delete_option($this->option_name); | |
delete_option('plugin_admin_notices'); | |
} | |
function plugin_admin_notices() { | |
if ($this->notices = get_option('plugin_admin_notices')) { | |
foreach ($this->notices as $notice) { | |
echo "<div class='updated'><p>$notice</p></div>"; | |
} | |
delete_option('plugin_admin_notices'); | |
} | |
} | |
function scripts_frontend(){ | |
wp_enqueue_style('frontend-css', plugins_url('css/frontend.css',__FILE__ )); | |
wp_enqueue_style('admin-icons', plugins_url('css/ionicons.min.css',__FILE__ )); | |
wp_enqueue_script('frontend-ajax', plugins_url('js/frontend.js',__FILE__ ), array(jquery)); | |
wp_localize_script('frontend-ajax', 'ajax', | |
array( | |
'url' => admin_url('admin-ajax.php'), | |
'nonce' => wp_create_nonce('helloworld') | |
) | |
); | |
} | |
function scripts_admin(){ | |
$current_screen = get_current_screen(); | |
if($current_screen->id === "settings_page_ae_options_group" ) { | |
wp_enqueue_style('admin-css', plugins_url('css/admin.css',__FILE__ )); | |
wp_enqueue_style('admin-icons', plugins_url('css/ionicons.min.css',__FILE__ )); | |
wp_enqueue_script('admin-js', plugins_url('js/admin.js',__FILE__ ), array(jquery)); | |
wp_localize_script('admin-js', 'ajax', | |
array( | |
'url' => admin_url('admin-ajax.php') | |
//'nonce' => wp_create_nonce('helloadmin') | |
) | |
); | |
} | |
} | |
// show plugin footer text | |
function this_screen(){ | |
$current_screen = get_current_screen(); | |
if($current_screen->id === "settings_page_ae_options_group" ) { | |
add_filter('update_footer', 'right_admin_footer_text_output', 11); | |
function right_admin_footer_text_output($text){ | |
$text = current_time('Y-m-d'); | |
return $text; | |
} | |
add_filter('admin_footer_text', 'left_admin_footer_text_output'); | |
function left_admin_footer_text_output($text){ | |
$text = 'Thank you for installing this plugin! Created by <a class="created" href="http://keksus.com">Keksus</a>'; | |
return $text; | |
} | |
} | |
} | |
function style_to_header(){ | |
$options = get_option($this->option_name); | |
?><style type="text/css"><?php | |
echo "\n". $options['textarea']. "\n"; | |
?></style><?php | |
} | |
// frontend html form | |
function ae_form(){ | |
// send ajax script in plugin.js file | |
$options = get_option($this->option_name); | |
?> | |
<div class='clear'> | |
<div class='q12' style='width:<?php echo _e($options['width'], 'ae');?>'> | |
<form id='#ae' class='ajax-form' method='POST' action=''> | |
<label for='text_field'><?php echo _e($options['name'], 'ae');?></label> | |
<input id='name' type='text' class='txt' name='name'> | |
<label for='text_area'><?php echo _e($options['message'], 'ae');?></label> | |
<textarea id='message' rows='5' name='message'></textarea> | |
<?php if( $options['captcha_on'] == '1' ): ?> | |
<span class='captcha '><?php echo _e('Enter code:', 'ae'); ?></span> | |
<?php echo $this->ae_session(); ?> | |
<input id='captcha' type='text' class='txt' name='captcha'> | |
<?php endif; ?> | |
<div class="message-btn"> | |
<div> | |
<input type='submit' name='submit' class='ajax-button' style=' | |
background:<?php echo _e($options['btn_color'], 'ae'); ?>; | |
color: <?php echo _e($options['btn_text_color'], 'ae'); ?>' | |
value='<?php echo _e($options['submit'], 'ae');?>'/> | |
</div> | |
<div> | |
<span id='load'><div id='loading'>LOADING!</div></span> | |
</div> | |
</div> | |
<?php wp_nonce_field('helloworld'); ?> | |
</form> | |
<div id='response'></div> | |
</div> | |
</div> | |
<?php | |
} | |
// validate and sent message | |
function ae_action_callback(){ | |
$options = get_option($this->option_name); | |
check_ajax_referer('helloworld'); | |
if( defined('DOING_AJAX') && DOING_AJAX ){ | |
$name = sanitize_text_field($_POST['name']); | |
$message = nl2br( esc_textarea($_POST['message'])); | |
// validate data | |
if( '' == $name ){ | |
echo '<div class="alert-error"><span>'. __('Name required', 'ae') . '</span>'; | |
echo '<div class="alert-close"><i class="ion-android-close"></i></div>'; | |
} | |
elseif( '' == $message ){ | |
echo '<div class="alert-error"><span>'. __('Message required', 'ae') . '</span>'; | |
echo '<div class="alert-close"><i class="ion-android-close"></i></div>'; | |
} | |
elseif( isset($_SESSION['captcha']) ){ | |
if( $_POST['captcha'] == $_SESSION['captcha'] ){ | |
$this->ae_mail(); | |
} | |
else{ | |
echo '<div class="alert-error"><span>'. __('Wrong captcha', 'ae') . '</span>'; | |
echo '<div class="alert-close"><i class="ion-android-close"></i></div>'; | |
} | |
} | |
else{ | |
$this->ae_mail(); | |
} | |
//echo wp_json_encode($_POST); | |
wp_die(); | |
} | |
} | |
function ae_mail(){ | |
$options = get_option($this->option_name); | |
$this->to_email = $options['email']; | |
$this->subject = $options['subject']; | |
$this->url = get_option('siteurl'); | |
$this->from_email = get_option('blogname'); | |
$this->body = "<p><b>From:</b> $this->url</p>"; | |
$this->body .= "<p><b>Name:</b> $name</p>"; | |
$this->body .= "<p><b>Email:</b> $this->from_email</p>"; | |
$this->body .= "<p><b>Subject:</b> $this->subject</p>"; | |
$this->body .= "<p><b>Message:</b></p><p> $message</p>"; | |
// test send mail | |
/*echo '<div class="alert-success"><span>'. $this->body .'</span>'; | |
echo '<div class="alert-close"><i class="ion-android-close"></i></div>';*/ | |
$this->headers = 'Content-type: text/html; charset=utf-8' . "\r\n"; | |
//$this->headers .= "From: $name <[email protected]>" . "\r\n"; | |
if( wp_mail($this->to_email, $this->subject, $this->body, $this->headers )){ | |
echo '<div class="alert-success"><span>'. __($options['success'], 'ae') .'</span>'; | |
echo '<div class="alert-close"><i class="ion-android-close"></i></div>'; | |
} | |
else{ | |
echo '<div class="alert-error"><span>'. __($options['error'], 'ae') .'</span>'; | |
echo '<div class="alert-close"><i class="ion-android-close"></i></div>'; | |
} | |
} | |
function ae_session(){ | |
$options = get_option($this->option_name); | |
if( $options['captcha_on'] == '1' ){ | |
if(headers_sent()){ | |
session_start(); | |
$digits = $options['captcha_digits']; | |
$_SESSION['captcha'] = rand(pow(10, $digits-1), pow(10, $digits)-1); | |
$captcha = $_SESSION['captcha']; | |
return $captcha; | |
} | |
} | |
else{ | |
session_destroy(); | |
} | |
} | |
// shortcode button [ae_message] | |
function ae_message_shortcode(){ | |
return $this->ae_form(); | |
} | |
// register plugin settings | |
function plugin_settings(){ | |
register_setting('ae_options_group', $this->option_name, array($this, 'sanitize_callback')); | |
} | |
// settings page for admin dashboard | |
function admin_page_settings(){ | |
add_options_page('Ajax Message settings', 'Ajax Message', 'manage_options', 'ae_options_group', array($this, 'options_output')); | |
} | |
function options_output(){ | |
$options = get_option($this->option_name); | |
$checked = ( is_array($options) && $options['captcha_on'] == '1' ) ? 'checked="checked"' : ''; | |
?> | |
<div class='options'> | |
<h1><?php _e('Ajax Message', 'ae'); ?></h1> | |
<form class='ajax-form-admin' method='POST' action="<?php echo admin_url('options.php'); ?>"> | |
<?php settings_fields('ae_options_group'); ?> | |
<div class='q12'> | |
<div class='tabs'> | |
<ul class='tab-links'> | |
<li class='active'><a href='#tab1'><?php _e('Settings', 'ae'); ?></a></li> | |
<li><a href='#tab2'><?php _e('Readme', 'ae'); ?></a></li> | |
</ul> | |
</div> | |
<div class='tab-content active'> | |
<div id='tab1' class='tab active' > | |
<div class='clear'> | |
<div class='q6'> | |
<div class='post'> | |
<h2><?php _e('Destination', 'ae'); ?></h2> | |
<?php //print_r($options) ?> | |
<label for='text_field'><?php _e('Email address (*): ', 'ae'); ?></label> | |
<input type='text' class='txt email' name='<?php echo $this->option_name?>[email]' value='<?php echo $options['email']; ?>'> | |
<h2><?php _e('Form field text', 'ae'); ?></h2> | |
<?php //print_r($options) ?> | |
<label for='text_field'><?php _e('Name field:', 'ae'); ?></label> | |
<input type='text' class='txt name' name='<?php echo $this->option_name?>[name]' value='<?php echo $options['name']; ?>'> | |
<label for='text_field'><?php _e('Message field:', 'ae'); ?></label> | |
<input type='text' class='txt message' name='<?php echo $this->option_name?>[message]' value='<?php echo $options['message']; ?>'> | |
<label for='text_field'><?php _e('Submit button (*):', 'ae'); ?></label> | |
<input type='text' class='txt submit' name='<?php echo $this->option_name?>[submit]' value='<?php echo $options['submit']; ?>'> | |
<h2><?php _e('Form styles', 'ae'); ?></h2> | |
<label for='text_field'><?php _e('Form width (pixels or percents):', 'ae'); ?></label> | |
<input type='text' class='txt' name='<?php echo $this->option_name?>[width]' value='<?php echo $options['width']; ?>'> | |
<label for='text_field'><?php _e('Submit button color:', 'ae'); ?></label> | |
<input type='text' class='txt' name='<?php echo $this->option_name?>[btn_color]' value='<?php echo $options['btn_color']; ?>'> | |
<label for='text_field'><?php _e('Submit button text color:', 'ae'); ?></label> | |
<input type='text' class='txt subtext' name='<?php echo $this->option_name?>[btn_text_color]' value='<?php echo $options['btn_text_color']; ?>'> | |
<h2><?php _e('Form reply messages', 'ae'); ?></h2> | |
<label for='text_field'><?php _e('Success message (*):', 'ae'); ?></label> | |
<input type='text' class='txt success' name='<?php echo $this->option_name?>[success]' value='<?php echo $options['success']; ?>'> | |
<label for='text_field'><?php _e('Error message (*):', 'ae'); ?></label> | |
<input type='text' class='txt error' name='<?php echo $this->option_name?>[error]' value='<?php echo $options['error']; ?>'> | |
</div><!-- end post --> | |
</div> | |
<div class='q6'> | |
<div class='post'> | |
<h2><?php _e('Captcha options', 'ae'); ?></h2> | |
<label for='text_field'><?php _e('Captcha number of digits:', 'ae'); ?></label> | |
<input type='text' class='txt captchadig' name='<?php echo $this->option_name?>[captcha_digits]' value='<?php echo $options['captcha_digits']; ?>'> | |
<label for='text_field'><?php _e('Show/hide captcha:', 'ae'); ?></label> | |
<input type="checkbox" name="<?php echo $this->option_name?>[captcha_on]" value="1"<?php echo $checked; ?> /> | |
<h2><?php _e('Custom CSS', 'ae'); ?></h2> | |
<label for='text_field'><?php _e('CSS code: ', 'ae'); ?></label> | |
<textarea spellcheck="false" id='textarea' class='txt textarea' name='<?php echo $this->option_name?>[textarea]' rows="5"><?php echo $options['textarea']; ?> </textarea> | |
</div> | |
</div> | |
</div> | |
</div> | |
<div id='tab2' class='tab'> | |
<div class='clear'> | |
<div class='q10'> | |
<div class='post'> | |
<h2><?php _e('How to:', 'ae'); ?></h2> | |
<p>1. If you want to use Ajax message form on the page or post, add this shortcode inside the text editor:</p> | |
<pre>[ae_message]</pre> | |
<p>Also you can use the code written above in the widget "Ajax message".</p> | |
<p>2. If you want to use Ajax message form in the theme code, add this code to your template:</p> | |
<pre><?php echo "<>"; | |
echo "?php>" ."do_shortcode('[ae_message]');" . "?>"; ?></pre> | |
<h2><?php _e('License:', 'ae'); ?></h2> | |
<p><?php _e( 'This theme is licensed under the <a target="_blank" href="https://www.gnu.org/licenses/gpl-3.0.html">GPL v3 license</a> This means you can use it for anything you like as long as it remains GPL v3.', 'ae' ); ?></p> | |
<h2><?php _e('Credits:', 'ae'); ?></h2> | |
<p><?php _e( 'This plugin was created by <a target="_blank" href="http://keksus.com/">Keksus.com</a>', 'ae' ); ?></p> | |
<p><?php _e( 'A back-link to our website is very much appreciated or you can follow us via our social media!', 'ae' ); ?></p> | |
<p class='links'> | |
<a target="_blank" href="https://twitter.com/keks5588" class="button button-secondary">Twitter</a> | |
<a target="_blank" href="https://www.facebook.com/keks5588" class="button button-secondary">Facebook</a> | |
<a target="_blank" href="https://plus.google.com/110925729980114845157" class="button button-secondary">Google +</a> | |
<a href="https://vk.com/keks5588" class="button button-secondary">VK</a> | |
</p> | |
<h2><?php _e('Donation:', 'ae'); ?></h2> | |
<p><?php _e( 'If you would like this plugin, you can donate any amount for other plugins development.', 'colored' ); ?></p> | |
<p class='links'><a href="http://keksus.com/donate.html" target="_blank" class="button button-secondary"> | |
<?php _e( 'Donate', 'colored' ); ?> | |
</a> | |
</p> | |
</div><!-- end post --> | |
</div> | |
</div> | |
</div> | |
</div> | |
<div id='response' class='clear'></div> | |
<div class='buttons clear'> | |
<input type='submit' class='button-primary' value='<?php _e('Save Changes') ?>' /> | |
<span id='load'><div id='loading'>LOADING!</div></span> | |
</div> | |
</form> | |
</div> | |
<?php | |
} | |
// clean options | |
function sanitize_callback($options){ | |
$type = 'updated'; | |
if( empty($options['email'])){ | |
$type = 'error'; | |
$message = __('Empty email field!', 'ae'); | |
} | |
elseif( !is_email($options['email'])){ | |
$type = 'error'; | |
$message = __('Not valid email!', 'ae'); | |
} | |
elseif( empty($options['submit'])){ | |
$type = 'error'; | |
$message = __('Empty submit button field!', 'ae'); | |
} | |
elseif( empty($options['success'])){ | |
$type = 'error'; | |
$message = __('Empty success message field!', 'ae'); | |
} | |
elseif( empty($options['error'])){ | |
$type = 'error'; | |
$message = __('Empty error message field!', 'ae'); | |
} | |
else{ | |
$message = __('Settings Saved', 'ae'); | |
} | |
add_settings_error($this->option_name, 'settings_updated', $message, $type ); | |
foreach( $options as $name => $val ){ | |
$val = sanitize_text_field($val); | |
} | |
return $options; //die(print_r($options )); | |
} | |
} //end class | |
new Ajax_Message(); | |
// Widget | |
function ajax_message_widget() { | |
register_widget( 'Ajax_Message_Widget' ); | |
} | |
add_action( 'widgets_init', 'ajax_message_widget' ); | |
class Ajax_Message_Widget extends WP_Widget { | |
public function __construct(){ | |
$widget_ops = array( | |
'classname' => 'ajax_message_widget', | |
'description' => __( 'Shortcode or HTML or Plain Text.', 'ae' ) | |
); | |
parent::__construct( 'ae', __( 'Ajax Message', 'ae' ), $widget_ops ); | |
} | |
public function widget($args, $instance ){ | |
$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base ); | |
echo $args['before_widget']; | |
if ($title ) { | |
echo $args['before_title'] . $title . $args['after_title']; | |
} | |
?> | |
<div class="textwidget"> | |
<?php | |
do_shortcode( apply_filters( 'widget_text', empty($instance['text']) ? '' : $instance['text'], $instance )); | |
?> | |
</div> | |
<?php | |
echo $args['after_widget']; | |
} | |
public function update($new_instance, $old_instance ){ | |
$instance = $old_instance; | |
$instance['title'] = strip_tags($new_instance['title']); | |
if ( current_user_can( 'unfiltered_html' )) { | |
$instance['text'] = $new_instance['text']; | |
} else { | |
$instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text'])) ); // wp_filter_post_kses() expects slashed | |
} | |
$instance['filter'] = !empty($new_instance['filter']); | |
return $instance; | |
} | |
public function form($instance ) { | |
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' )); | |
?> | |
<p><label for="<?php echo esc_attr($this->get_field_id( 'title' )); ?>"><?php esc_html_e( 'Title:', 'ae' ); ?></label> | |
<input class="widefat" id="<?php echo esc_attr($this->get_field_id( 'title' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'title' )); ?>" type="text" value="<?php echo esc_attr($instance['title']); ?>" /></p> | |
<p><label for="<?php echo esc_attr($this->get_field_id( 'text' )); ?>"><?php esc_html_e( 'Content:', 'ae' ); ?></label> | |
<textarea class="widefat" rows="16" cols="20" id="<?php echo esc_attr($this->get_field_id( 'text' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'text' )); ?>"><?php echo esc_textarea($instance['text']) ?></textarea></p> | |
<p><input id="<?php echo esc_attr($this->get_field_id( 'filter' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'filter' )); ?>" type="checkbox" <?php checked( isset($instance['filter']) ? $instance['filter'] : 0 ); ?> /> <label for="<?php echo esc_attr($this->get_field_id( 'filter' )); ?>"><?php esc_html_e( 'Automatically add paragraphs', 'ae' ); ?></label></p> | |
<?php | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment