PluginProbe
Essential Form – The lightest plugin for contact forms, ultra lightweight and no spam / 0.0.1
Essential Form – The lightest plugin for contact forms, ultra lightweight and no spam v0.0.1
trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.6.RC-1 0.0.7 0.0.8 0.0.9 1.0.0 1.0.1 1.0.2
essential-form / essential-form.php

essential-form.php in Essential Form – The lightest plugin for contact forms, ultra lightweight and no spam 0.0.1, at essential-form.php

226 lines 12.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Essential Form
4 Description: The lightest contact form for WordPress. So essential you'll either love it or hate it. Ultra lightweight and no spam.
5 Author: Jose Mortellaro
6 Author URI: https://josemortellaro.com/
7 Domain Path: /languages/
8 Text Domain: essential-form
9 Version: 0.0.1
10 */
11 /* This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19 */
20 defined( 'ABSPATH' ) || exit; // Exit if accessed directly
21
22 //Definitions
23 define( 'ESSENTIAL_FORM_VERSION','0.0.1' );
24 define( 'ESSENTIAL_FORM_PLUGIN_DIR', untrailingslashit( dirname( __FILE__ ) ) );
25 define( 'ESSENTIAL_FORM_PLUGIN_URL', untrailingslashit( plugins_url( '', __FILE__ ) ) );
26 $keys = essential_form_get_keys();
27 define( 'ESSENTIAL_FORM_AJAX_KEY','essential_form_'.sanitize_key( $keys[0] ) );
28
29 if( wp_doing_ajax() ){
30 if( isset( $_REQUEST['action'] ) && in_array( sanitize_text_field( $_REQUEST['action'] ),apply_filters( 'essential_form_allowed_actions',array( 'essential_form_'.sanitize_key( $keys[0] ),'essential_form_'.sanitize_key( $keys[0] ).'_get_key' ) ) ) ){
31 require_once ESSENTIAL_FORM_PLUGIN_DIR.'/inc/ef-ajax.php';
32 }
33 }
34 elseif( is_admin() ){
35 require_once ESSENTIAL_FORM_PLUGIN_DIR.'/admin/ef-admin.php';
36 }
37
38 //Actions triggered after plugin activation
39 register_activation_hook( __FILE__,function( $networkwide ){
40 require ESSENTIAL_FORM_PLUGIN_DIR.'/plugin-activation.php';
41 } );
42
43 //Get options in case of single or multisite installation.
44 function essential_form_get_option( $option ){
45 if( !is_multisite() ){
46 return get_option( $option );
47 }
48 else{
49 return get_blog_option( get_current_blog_id(),$option );
50 }
51 }
52
53 //Update options in case of single or multisite installation.
54 function essential_form_update_option( $option,$newvalue,$autoload = false ){
55 if( !is_multisite() ){
56 return update_option( $option,$newvalue,$autoload );
57 }
58 else{
59 return update_blog_option( get_current_blog_id(),$option,$newvalue );
60 }
61 }
62
63 //Filtered settings
64 function essential_form_filtered_settings(){
65 $settings = array();
66 $default_settings = essential_form_default_settings();
67 $filtered = apply_filters( 'essential_form_settings',$default_settings );
68 foreach( $default_settings as $key => $value ){
69 $settings[$key] = isset( $filtered[$key] ) ? $filtered[$key] : $default_settings[$key];
70 }
71 return $settings;
72 }
73
74 //Default settings
75 function essential_form_default_settings(){
76 return array(
77 'email_from' => get_bloginfo( 'admin_email' ),
78 'email_to' => get_bloginfo( 'admin_email' ),
79 'email_subject' => sprintf( esc_html__( 'Message from %s','essential-form' ),get_bloginfo( 'name' ) ),
80 'label_name' => __( 'Name','essential-form' ),
81 'label_email' => __( 'Email','essential-form' ),
82 'label_message' => __( 'Message','essential-form' ),
83 'button_text' => __( 'Send','essential-form' ),
84 'agreement_text' => __( 'By submitting this form I agree with the privacy policy','essential-form' ),
85 'success_message' => __( 'Form submitted successfully! Thank you for your message!','essential-form' ),
86 'name_missing_error' => __( 'Name is a required field!','essential-form' ),
87 'email_missing_error' => __( 'Email is a required field!','essential-form' ),
88 'email_not_valid_error' => __( 'Email not valid!','essential-form' ),
89 'message_missing_error' => __( 'Message is a required field!','essential-form' ),
90 'message_too_long_error' => __( 'This message is too long! Please, write not more than 50000 characters.','essential-form' ),
91 'missing_agreement_error' => __( 'You have to agree with our privacy policy to submit the form.','essential-form' )
92 );
93 }
94
95 add_shortcode( 'essential_form',function( $atts ){
96 //Form shortcode
97 static $s = 0;
98 $opts = essential_form_get_option( 'essential_form' );
99 if( isset( $opts['activation_keys'] ) ){
100 global $post;
101 ++$s;
102 $settings = essential_form_filtered_settings();
103 extract( $settings );
104 $label_name = isset( $atts['label_name'] ) ? sanitize_text_field( $atts['label_name'] ) : apply_filters( 'essential_form_label_name',$label_name );
105 $label_email = isset( $atts['label_email'] ) ? sanitize_text_field( $atts['label_email'] ) : apply_filters( 'essential_form_label_email',$label_email );
106 $label_message = isset( $atts['label_message'] ) ? sanitize_text_field( $atts['label_message'] ) : apply_filters( 'essential_form_label_message',$label_message );
107 $button_text = isset( $atts['button_text'] ) ? sanitize_text_field( $atts['button_text'] ) : apply_filters( 'essential_form_button_text',$button_text );
108 $agreement_text = isset( $atts['agreement_text'] ) ? sanitize_text_field( $atts['agreement_text'] ) : apply_filters( 'essential_form_agreement_text',$agreement_text );
109 $success_message = isset( $atts['success_message'] ) ? sanitize_text_field( $atts['success_message'] ) : apply_filters( 'essential_form_success_message',$success_message );
110 $keys = essential_form_get_keys();
111 $output = '<style id="'.esc_attr( $keys[0] ).'-'.$s.'-css">';
112 $output .= '.'.esc_attr( $keys[0] ).'-wrp .ef-error{border:1px solid red;padding:10px;margin-bottom:16px}';
113 $output .= '.'.esc_attr( $keys[0] ).'-wrp .ef-success{border:1px solid green;padding:10px;margin-bottom:16px}';
114 $output .= '.'.esc_attr( $keys[0] ).' input:not([type=checkbox]),.'.esc_attr( $keys[0] ).' textarea{width:100%}.'.esc_attr( $keys[0] ).' textarea{min-height:150px}.'.esc_attr( $keys[0] ).'>div{margin-bottom:16px}.'.esc_attr( $keys[0] ).' button{background-position:-999999px -9999999px !important}.'.esc_attr( $keys[0] ).'.ef-progress button{background-position:center !important;pointer-events:none !important}';
115 $output .= '</style>';
116 $output .= '<div id="fw-'.esc_attr( $keys[0] ).'-'.$s.'" class="'.esc_attr( $keys[0] ).'-wrp ef-ctrl-wrp">';
117 $output .= '<div id="'.esc_attr( $keys[7] ).'-'.$s.'" style="display:none" class="ef-success">'.esc_html( $success_message ).'</div>';
118 $output .= '<div id="'.esc_attr( $keys[8] ).'-'.$s.'" style="display:none" class="ef-error"></div>';
119 $output .= '<form id="'.esc_attr( $keys[0] ).'-'.$s.'" class="'.esc_attr( $keys[0] ).'">';
120 $output .= '<div class="ef-ctrl-wrp">';
121 $output .= '<div><label for="'.esc_attr( $keys[1] ).'-'.$s.'">'.esc_html( $label_name ).'</label></div>';
122 $output .= '<div><input type="text" id="'.esc_attr( $keys[1] ).'-'.$s.'" class="'.esc_attr( $keys[1] ).'" required /></div>';
123 $output .= '</div>';
124 $output .= '<div class="ef-ctrl-wrp">';
125 $output .= '<div><label for="'.esc_attr( $keys[2] ).'-'.$s.'">'.esc_html( $label_email ).'</label></div>';
126 $output .= '<div><input type="email" id="'.esc_attr( $keys[2] ).'-'.$s.'" class="'.esc_attr( $keys[2] ).'" required /></div>';
127 $output .= '</div>';
128 $output .= '<div class="ef-ctrl-wrp">';
129 $output .= '<div><label for="'.esc_attr( $keys[3] ).'-'.$s.'">'.esc_html( $label_message ).'</label></div>';
130 $output .= '<div><textarea id="'.esc_attr( $keys[3] ).'-'.$s.'" class="'.esc_attr( $keys[3] ).'" required></textarea></div>';
131 $output .= '</div>';
132 $output .= '<div><input type="checkbox" id="'.esc_attr( $keys[4] ).'-'.$s.'" class="'.esc_attr( $keys[4] ).'" required value="on" /> '.esc_html( $agreement_text ).'</div>';
133 $output .= '<div><button id="'.esc_attr( $keys[3] ).'-'.$s.'" type="submit" class="button" style="background-size:22px 22px !important;background-repeat:no-repeat !important;background-image:url('.includes_url( '/images/spinner.gif' ).') !important" onclick="essential_form_'.sanitize_key( $keys[0] ).'('.$s.');return false;">'.esc_html( $button_text ).'</div>';
134 $output .= '<input type="hidden" value="'.esc_attr( implode( ',',$keys ) ).'" id="'.esc_attr( $keys[5] ).'-'.$s.'" />';
135 $output .= '<input type="hidden" value="" id="'.esc_attr( $keys[6] ).'-'.$s.'" />';
136 $output .= '</form>';
137 $output .= '<div id="'.esc_attr( $keys[8] ).'-'.$s.'" style="margin-bottom:16px"></div>';
138 $output .= '</div>';
139 if( 1 === $s ){
140 $output .= '<script id="'.esc_attr( $keys[0] ).'-'.$s.'-js" type="text/javascript">';
141 $output .= 'essential_form_random_key = Math.floor(Math.random() * 99999999999999);';
142 $output .= 'function essential_form_'.sanitize_key( $keys[0] ).'_init(){';
143 $output .= 'var r = new XMLHttpRequest(),f=new FormData();';
144 $output .= 'r.open("POST","'.esc_js( admin_url( 'admin-ajax.php' ) ).'?action='.ESSENTIAL_FORM_AJAX_KEY.'_get_key",true);';
145 $output .= 'r.onload = function(e){';
146 $output .= 'if(this.readyState === 4 && "" !== e.target.responseText){';
147 $output .= 'document.getElementById("'.esc_attr( $keys[6] ).'-'.$s.'").value = e.target.responseText;';
148 $output .= '}';
149 $output .= '};';
150 $output .= 'f.append("random_key",essential_form_random_key);';
151 $output .= 'r.send(f);';
152 $output .= '}';
153 $output .= 'essential_form_'.sanitize_key( $keys[0] ).'_init();';
154 $output .= 'function essential_form_'.sanitize_key( $keys[0] ).'(n){';
155 $output .= 'var req = new XMLHttpRequest(),fd=new FormData(),succ=document.getElementById("'.esc_attr( $keys[7] ).'-'.$s.'"),err=document.getElementById("'.esc_attr( $keys[8] ).'-'.$s.'"),fe=document.getElementById("'.esc_attr( $keys[0] ).'-" + n);';
156 $output .= 'fe.className += " ef-progress";';
157 $output .= 'succ.style.display="none";';
158 $output .= 'err.style.display="none";';
159 $output .= 'req.onload = function(e){';
160 $output .= 'if(this.readyState === 4 && "" !== e.target.responseText){';
161 $output .= 'if("1" === e.target.responseText){';
162 $output .= 'succ.style.display="block";';
163 $output .= 'err.style.display="none";';
164 $output .= 'document.getElementById("'.esc_attr( $keys[0] ).'-'.$s.'").style.visibility="hidden";';
165 $output .= '}else{';
166 $output .= 'err.style.display="block";';
167 $output .= 'err.innerHTML=e.target.responseText;';
168 $output .= '}';
169 $output .= '}';
170 $output .= 'fe.className = fe.className.replace(" ef-progress","");';
171 $output .= 'return false;';
172 $output .= '};';
173 $f = 1;
174 foreach( essential_form_fields_array() as $fkey ){
175 if( 'agreement' !== $fkey ){
176 $output .= 'fd.append("'.esc_js( esc_attr( $fkey ) ).'",document.getElementById("'.esc_js( esc_attr( $keys[$f] ) ).'-" + n).value);';
177 }
178 else{
179 $output .= 'fd.append("'.esc_js( esc_attr( $fkey ) ).'",document.getElementById("'.esc_js( esc_attr( $keys[$f] ) ).'-" + n).checked);';
180 }
181 ++$f;
182 }
183 global $post;
184 if( $post && is_object( $post ) && isset( $post->ID ) ){
185 $output .= 'fd.append("post_id",'.absint( $post->ID ).');';
186 }
187 $output .= 'fd.append("random_key",essential_form_random_key);';
188 $output .= 'req.open("POST","'.esc_js( admin_url( 'admin-ajax.php' ) ).'?action='.ESSENTIAL_FORM_AJAX_KEY.'",true);';
189 $output .= 'req.send(fd);';
190 $output .= 'return false;';
191 $output .= '}';
192 $output .= '</script>';
193 }
194 return $output;
195 }
196 } );
197
198 //Return array of fields
199 function essential_form_fields_array(){
200 return array(
201 'name',
202 'email',
203 'message',
204 'agreement',
205 'keys',
206 'temp_key'
207 );
208 }
209
210 //Get keys for the anti-spam system
211 function essential_form_get_keys(){
212 $opts = essential_form_get_option( 'essential_form' );
213 $keys = $opts['activation_keys'];
214 foreach( $keys as &$key ){
215 $key = 'f'.sanitize_text_field( substr( md5( $key ),0,8 ) );
216 }
217 return $keys;
218 }
219
220 add_filter( 'plugin_action_links_'.untrailingslashit( plugin_basename( __FILE__ ) ),function( $links ) {
221 //It action links in the plugins page
222 $links[] = sprintf( 'Shortcode: %s','<span class="ef-shortcode" style="color:#000">[essential_form]</span>' );
223 $links[] = '<a href="https://wordpress.org/plugins/essential-form/" target="_blank" rel="noopener">'.esc_html__( 'Description','essential-form' ).'</a>';
224 return $links;
225 } );
226