PluginProbe
Essential Form – The lightest plugin for contact forms, ultra lightweight and no spam / 0.0.4
Essential Form – The lightest plugin for contact forms, ultra lightweight and no spam v0.0.4
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.4, at essential-form.php

252 lines 13.9 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.4
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.4' );
24 define( 'ESSENTIAL_FORM_PLUGIN_DIR', untrailingslashit( dirname( __FILE__ ) ) );
25 define( 'ESSENTIAL_FORM_PLUGIN_URL', untrailingslashit( plugins_url( '', __FILE__ ) ) );
26
27 $keys = essential_form_get_keys();
28 if( $keys && is_array( $keys ) ){
29 define( 'ESSENTIAL_FORM_AJAX_KEY','essential_form_'.sanitize_key( $keys[0] ) );
30 }
31
32 if( wp_doing_ajax() && defined( 'ESSENTIAL_FORM_AJAX_KEY' ) ){
33 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' ) ) ) ){
34 require_once ESSENTIAL_FORM_PLUGIN_DIR.'/inc/ef-ajax.php';
35 }
36 }
37 elseif( is_admin() ){
38 require_once ESSENTIAL_FORM_PLUGIN_DIR.'/admin/ef-admin.php';
39 }
40
41 //Actions triggered after plugin activation
42 register_activation_hook( __FILE__,function( $networkwide ){
43 require ESSENTIAL_FORM_PLUGIN_DIR.'/plugin-activation.php';
44 } );
45
46 //Get options in case of single or multisite installation.
47 function essential_form_get_option( $option ){
48 if( !is_multisite() ){
49 return get_option( $option );
50 }
51 else{
52 return get_blog_option( get_current_blog_id(),$option );
53 }
54 }
55
56 //Update options in case of single or multisite installation.
57 function essential_form_update_option( $option,$newvalue,$autoload = false ){
58 if( !is_multisite() ){
59 return update_option( $option,$newvalue,$autoload );
60 }
61 else{
62 return update_blog_option( get_current_blog_id(),$option,$newvalue );
63 }
64 }
65
66 //Filtered settings
67 function essential_form_filtered_settings(){
68 $settings = array();
69 $default_settings = essential_form_default_settings();
70 $filtered = apply_filters( 'essential_form_settings',$default_settings );
71 foreach( $default_settings as $key => $value ){
72 $settings[$key] = isset( $filtered[$key] ) ? $filtered[$key] : $default_settings[$key];
73 }
74 return $settings;
75 }
76
77 //Default settings
78 function essential_form_default_settings(){
79 $privacy_page = (int) get_site_option( 'wp_page_for_privacy_policy' );
80 if( function_exists( 'icl_object_id' ) ){
81 $privacy_page_lang = icl_object_id( $privacy_page,'page', false,ICL_LANGUAGE_CODE );
82 $privacy_page = $privacy_page_lang ? $privacy_page_lang : $privacy_page;
83 }
84 $url = get_permalink( $privacy_page );
85 $url = $url ? $url : '#';
86 return array(
87 'email_from' => get_bloginfo( 'admin_email' ),
88 'email_to' => get_bloginfo( 'admin_email' ),
89 'email_subject' => sprintf( esc_html__( 'Message from %s','essential-form' ),get_bloginfo( 'name' ) ),
90 'label_name' => __( 'Name','essential-form' ),
91 'label_email' => __( 'Email','essential-form' ),
92 'label_message' => __( 'Message','essential-form' ),
93 'button_text' => __( 'Send','essential-form' ),
94 'agreement_text' => sprintf( __( 'By submitting this form I agree with the %sprivacy policy%s','essential-form' ),'<a href="'.esc_url( $url ).'" target="_blank">','</a>' ),
95 'success_message' => __( 'Form submitted successfully! Thank you for your message!','essential-form' ),
96 'name_missing_error' => __( 'Name is a required field!','essential-form' ),
97 'email_missing_error' => __( 'Email is a required field!','essential-form' ),
98 'email_not_valid_error' => __( 'Email not valid!','essential-form' ),
99 'message_missing_error' => __( 'Message is a required field!','essential-form' ),
100 'message_too_long_error' => __( 'This message is too long! Please, write not more than 50000 characters.','essential-form' ),
101 'missing_agreement_error' => __( 'You have to agree with our privacy policy to submit the form.','essential-form' )
102 );
103 }
104
105 add_shortcode( 'essential_form',function( $atts ){
106 //Form shortcode
107 static $s = 0;
108 $opts = essential_form_get_option( 'essential_form' );
109 if( isset( $opts['activation_keys'] ) ){
110 global $post;
111 ++$s;
112 $settings = essential_form_filtered_settings();
113 extract( $settings );
114 $label_name = isset( $atts['label_name'] ) ? sanitize_text_field( $atts['label_name'] ) : apply_filters( 'essential_form_label_name',$label_name );
115 $label_email = isset( $atts['label_email'] ) ? sanitize_text_field( $atts['label_email'] ) : apply_filters( 'essential_form_label_email',$label_email );
116 $label_message = isset( $atts['label_message'] ) ? sanitize_text_field( $atts['label_message'] ) : apply_filters( 'essential_form_label_message',$label_message );
117 $button_text = isset( $atts['button_text'] ) ? sanitize_text_field( $atts['button_text'] ) : apply_filters( 'essential_form_button_text',$button_text );
118 $agreement_text = isset( $atts['agreement_text'] ) ? wp_kses( $atts['agreement_text'],array( 'a' => array( 'href' => array(),'target' => array() ) ) ) : apply_filters( 'essential_form_agreement_text',$agreement_text );
119 $success_message = isset( $atts['success_message'] ) ? sanitize_text_field( $atts['success_message'] ) : apply_filters( 'essential_form_success_message',$success_message );
120 $keys = essential_form_get_keys();
121 $output = '<style id="'.esc_attr( $keys[0] ).'-'.$s.'-css">';
122 $output .= '.'.esc_attr( $keys[0] ).'-wrp .ef-error{border:1px solid red;padding:10px;margin-bottom:16px}';
123 $output .= '.'.esc_attr( $keys[0] ).'-wrp .ef-success{border:1px solid green;padding:10px;margin-bottom:16px}';
124 $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}';
125 $output .= '</style>';
126 $output .= '<div id="fw-'.esc_attr( $keys[0] ).'-'.$s.'" class="'.esc_attr( $keys[0] ).'-wrp ef-ctrl-wrp">';
127 $output .= '<div id="'.esc_attr( $keys[7] ).'-'.$s.'" style="display:none" class="ef-success">'.esc_html( $success_message ).'</div>';
128 $output .= '<div id="'.esc_attr( $keys[8] ).'-'.$s.'" style="display:none" class="ef-error"></div>';
129 $output .= '<form id="'.esc_attr( $keys[0] ).'-'.$s.'" class="'.esc_attr( $keys[0] ).'">';
130 $output .= '<div class="ef-ctrl-wrp">';
131 $output .= '<div><label for="'.esc_attr( $keys[1] ).'-'.$s.'">'.esc_html( $label_name ).'</label></div>';
132 $output .= '<div><input type="text" id="'.esc_attr( $keys[1] ).'-'.$s.'" class="'.esc_attr( $keys[1] ).'" required /></div>';
133 $output .= '</div>';
134 $output .= '<div class="ef-ctrl-wrp">';
135 $output .= '<div><label for="'.esc_attr( $keys[2] ).'-'.$s.'">'.esc_html( $label_email ).'</label></div>';
136 $output .= '<div><input type="email" id="'.esc_attr( $keys[2] ).'-'.$s.'" class="'.esc_attr( $keys[2] ).'" required /></div>';
137 $output .= '</div>';
138 $output .= '<div class="ef-ctrl-wrp">';
139 $output .= '<div><label for="'.esc_attr( $keys[3] ).'-'.$s.'">'.esc_html( $label_message ).'</label></div>';
140 $output .= '<div><textarea id="'.esc_attr( $keys[3] ).'-'.$s.'" class="'.esc_attr( $keys[3] ).'" required></textarea></div>';
141 $output .= '</div>';
142 $output .= '<div><input type="checkbox" id="'.esc_attr( $keys[4] ).'-'.$s.'" class="'.esc_attr( $keys[4] ).'" required value="on" /> '.wp_kses( $agreement_text ,array( 'a' => array( 'href' => array(),'target' => array() ) ) ).'</div>';
143 $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>';
144 $output .= '<input type="hidden" value="'.esc_attr( implode( ',',$keys ) ).'" id="'.esc_attr( $keys[5] ).'-'.$s.'" />';
145 $output .= '<input type="hidden" value="" id="'.esc_attr( $keys[6] ).'-'.$s.'" />';
146 $output .= '</form>';
147 $output .= '<div id="'.esc_attr( $keys[8] ).'-'.$s.'" style="margin-bottom:16px"></div>';
148 $output .= '</div>';
149 if( 1 === $s ){
150 $output .= '<script id="'.esc_attr( $keys[0] ).'-'.$s.'-js" type="text/javascript">';
151 $output .= 'essential_form_random_key = Math.floor(Math.random() * 99999999999999);';
152 $output .= 'function essential_form_'.sanitize_key( $keys[0] ).'_init(){';
153 $output .= 'var r = new XMLHttpRequest(),f=new FormData();';
154 $output .= 'r.open("POST","'.esc_js( admin_url( 'admin-ajax.php' ) ).'?action='.ESSENTIAL_FORM_AJAX_KEY.'_get_key",true);';
155 $output .= 'r.onload = function(e){';
156 $output .= 'if(this.readyState === 4 && "" !== e.target.responseText){';
157 $output .= 'document.getElementById("'.esc_attr( $keys[6] ).'-'.$s.'").value = e.target.responseText;';
158 $output .= '}';
159 $output .= '};';
160 $output .= 'f.append("random_key",essential_form_random_key);';
161 $output .= 'r.send(f);';
162 $output .= '}';
163 $output .= 'essential_form_'.sanitize_key( $keys[0] ).'_init();';
164 $output .= 'function essential_form_'.sanitize_key( $keys[0] ).'(n){';
165 $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);';
166 $output .= 'fe.className += " ef-progress";';
167 $output .= 'succ.style.display="none";';
168 $output .= 'err.style.display="none";';
169 $output .= 'req.onload = function(e){';
170 $output .= 'if(this.readyState === 4 && "" !== e.target.responseText){';
171 $output .= 'if("1" === e.target.responseText){';
172 $output .= 'succ.style.display="block";';
173 $output .= 'err.style.display="none";';
174 $output .= 'document.getElementById("'.esc_attr( $keys[0] ).'-'.$s.'").style.visibility="hidden";';
175 $output .= '}else{';
176 $output .= 'err.style.display="block";';
177 $output .= 'err.innerHTML=e.target.responseText;';
178 $output .= '}';
179 $output .= '}';
180 $output .= 'fe.className = fe.className.replace(" ef-progress","");';
181 $output .= 'return false;';
182 $output .= '};';
183 $f = 1;
184 foreach( essential_form_fields_array() as $fkey ){
185 if( 'agreement' !== $fkey ){
186 $output .= 'fd.append("'.esc_js( esc_attr( $fkey ) ).'",document.getElementById("'.esc_js( esc_attr( $keys[$f] ) ).'-" + n).value);';
187 }
188 else{
189 $output .= 'fd.append("'.esc_js( esc_attr( $fkey ) ).'",document.getElementById("'.esc_js( esc_attr( $keys[$f] ) ).'-" + n).checked);';
190 }
191 ++$f;
192 }
193 global $post;
194 if( $post && is_object( $post ) && isset( $post->ID ) ){
195 $output .= 'fd.append("post_id",'.absint( $post->ID ).');';
196 }
197 $output .= 'fd.append("random_key",essential_form_random_key);';
198 $output .= 'req.open("POST","'.esc_js( admin_url( 'admin-ajax.php' ) ).'?action='.ESSENTIAL_FORM_AJAX_KEY.'",true);';
199 $output .= 'req.send(fd);';
200 $output .= 'return false;';
201 $output .= '}';
202 $output .= '</script>';
203 }
204 return $output;
205 }
206 } );
207
208 //Return array of fields
209 function essential_form_fields_array(){
210 return array(
211 'name',
212 'email',
213 'message',
214 'agreement',
215 'keys',
216 'temp_key'
217 );
218 }
219
220 //Get keys for the anti-spam system
221 function essential_form_get_keys(){
222 $keys = false;
223 $opts = essential_form_get_option( 'essential_form' );
224 if( $opts && isset( $opts['activation_keys'] ) ){
225 $keys = $opts['activation_keys'];
226 foreach( $keys as &$key ){
227 $key = 'f'.sanitize_text_field( substr( md5( $key ),0,8 ) );
228 }
229 }
230 return $keys;
231 }
232
233 add_filter( 'plugin_action_links_'.untrailingslashit( plugin_basename( __FILE__ ) ),function( $links ) {
234 //It action links in the plugins page
235 $links[] = sprintf( 'Shortcode: %s','<span class="ef-shortcode" style="color:#000">[essential_form]</span>' );
236 $links[] = '<a href="https://wordpress.org/plugins/essential-form/" target="_blank" rel="noopener">'.esc_html__( 'Description','essential-form' ).'</a>';
237 return $links;
238 } );
239
240 add_action( 'init',function(){
241 //It loads plugin translation files
242 load_plugin_textdomain( 'essential-form',false,ESSENTIAL_FORM_PLUGIN_DIR.'/languages/' );
243 } );
244
245 add_filter( 'load_textdomain_mofile',function( $mofile,$domain ){
246 //Filter function to read plugin translation files
247 if ( 'essential-form' === $domain ) {
248 $loc = function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
249 $mofile = ESSENTIAL_FORM_PLUGIN_DIR . '/languages/essential-form-' . $loc . '.mo';
250 }
251 return $mofile;
252 },10,2 );