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

283 lines 16.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 Plugin URI: https://josemortellaro.com/essential-form/
8 Domain Path: /languages/
9 Text Domain: essential-form
10 Version: 1.0.2
11 */
12 /* This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20 */
21 defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
22
23 // Definitions.
24 define( 'ESSENTIAL_FORM_VERSION','1.0.2' );
25 define( 'ESSENTIAL_FORM_PLUGIN_DIR', untrailingslashit( dirname( __FILE__ ) ) );
26 define( 'ESSENTIAL_FORM_PLUGIN_URL', untrailingslashit( plugins_url( '', __FILE__ ) ) );
27 define( 'ESSENTIAL_FORM_BASE_NAME', untrailingslashit( plugin_basename( __FILE__ ) ) );
28
29 $keys = essential_form_get_keys();
30 if( $keys && is_array( $keys ) ){
31 define( 'ESSENTIAL_FORM_AJAX_KEY','essential_form_'.sanitize_key( $keys[0] ) );
32 }
33
34 if( wp_doing_ajax() && defined( 'ESSENTIAL_FORM_AJAX_KEY' ) ){
35 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' ) ) ) ){
36 require_once ESSENTIAL_FORM_PLUGIN_DIR.'/inc/ef-ajax.php';
37 }
38 }
39 elseif( is_admin() ){
40 require_once ESSENTIAL_FORM_PLUGIN_DIR.'/admin/ef-admin.php';
41 }
42
43 // Actions triggered after plugin activation.
44 register_activation_hook( __FILE__,function( $networkwide ){
45 require ESSENTIAL_FORM_PLUGIN_DIR.'/plugin-activation.php';
46 } );
47
48 // Get options in case of single or multisite installation.
49 function essential_form_get_option( $option ){
50 if( !is_multisite() ){
51 return get_option( $option );
52 }
53 else{
54 return get_blog_option( get_current_blog_id(),$option );
55 }
56 }
57
58 // Update options in case of single or multisite installation.
59 function essential_form_update_option( $option,$newvalue,$autoload = false ){
60 if( !is_multisite() ){
61 return update_option( $option,$newvalue,$autoload );
62 }
63 else{
64 return update_blog_option( get_current_blog_id(),$option,$newvalue );
65 }
66 }
67
68 // Filtered settings.
69 function essential_form_filtered_settings(){
70 $settings = array();
71 $default_settings = essential_form_default_settings();
72 $filtered = apply_filters( 'essential_form_settings',$default_settings );
73 foreach( $default_settings as $key => $value ){
74 $settings[$key] = isset( $filtered[$key] ) ? $filtered[$key] : $default_settings[$key];
75 }
76 return $settings;
77 }
78
79 // Default settings.
80 function essential_form_default_settings(){
81 $privacy_page = (int) get_option( 'wp_page_for_privacy_policy' );
82 if( function_exists( 'icl_object_id' ) ){
83 $privacy_page_lang = icl_object_id( $privacy_page,'page', false,ICL_LANGUAGE_CODE );
84 $privacy_page = $privacy_page_lang ? $privacy_page_lang : $privacy_page;
85 }
86 $url = get_permalink( $privacy_page );
87 $url = $url ? $url : '#';
88 return array(
89 'email_from' => get_bloginfo( 'admin_email' ),
90 'email_to' => get_bloginfo( 'admin_email' ),
91 'email_subject' => sprintf( esc_html__( 'Message from %s','essential-form' ),get_bloginfo( 'name' ) ),
92 'label_name' => __( 'Name','essential-form' ),
93 'label_email' => __( 'Email','essential-form' ),
94 'label_message' => __( 'Message','essential-form' ),
95 'button_text' => __( 'Send','essential-form' ),
96 'agreement_text' => sprintf( __( 'By submitting this form I agree with the %sprivacy policy%s','essential-form' ),'<a href="'.esc_url( $url ).'" target="_blank">','</a>' ),
97 'success_message' => __( 'Form submitted successfully! Thank you for your message!','essential-form' ),
98 'name_missing_error' => __( 'Name is a required field!','essential-form' ),
99 'email_missing_error' => __( 'Email is a required field!','essential-form' ),
100 'email_not_valid_error' => __( 'Email not valid!','essential-form' ),
101 'message_missing_error' => __( 'Message is a required field!','essential-form' ),
102 'message_too_long_error' => __( 'This message is too long! Please, write not more than 50000 characters.','essential-form' ),
103 'missing_agreement_error' => __( 'You have to agree with our privacy policy to submit the form.','essential-form' )
104 );
105 }
106
107 add_shortcode( 'essential_form',function( $atts ){
108 //Form shortcode.
109 static $s = 0;
110 $opts = essential_form_get_option( 'essential_form' );
111 if( isset( $opts['activation_keys'] ) ){
112 do_action( 'essential_form_init' );
113 global $post;
114 ++$s;
115 $settings = essential_form_filtered_settings();
116 extract( $settings );
117 $label_name = isset( $atts['label_name'] ) ? sanitize_text_field( $atts['label_name'] ) : apply_filters( 'essential_form_label_name',$label_name );
118 $label_email = isset( $atts['label_email'] ) ? sanitize_text_field( $atts['label_email'] ) : apply_filters( 'essential_form_label_email',$label_email );
119 $label_message = isset( $atts['label_message'] ) ? sanitize_text_field( $atts['label_message'] ) : apply_filters( 'essential_form_label_message',$label_message );
120 $button_text = isset( $atts['button_text'] ) ? sanitize_text_field( $atts['button_text'] ) : apply_filters( 'essential_form_button_text',$button_text );
121 $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 );
122 $success_message = isset( $atts['success_message'] ) ? sanitize_text_field( $atts['success_message'] ) : apply_filters( 'essential_form_success_message',$success_message );
123 $keys = essential_form_get_keys();
124 $output = '<style id="'.esc_attr( $keys[0] ).'-'.$s.'-css">';
125 $output .= '.'.esc_attr( $keys[0] ).'-wrp .ef-error{border:1px solid red;padding:10px;margin-bottom:16px}';
126 $output .= '.'.esc_attr( $keys[0] ).'-wrp .ef-success{border:1px solid green;padding:10px;margin-bottom:16px}';
127 $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}';
128 $output .= '</style>';
129 $output .= '<div id="fw-'.esc_attr( $keys[0] ).'-'.$s.'" class="'.esc_attr( $keys[0] ).'-wrp ef-ctrl-wrp">';
130 $output .= '<div id="'.esc_attr( $keys[7] ).'-'.$s.'" style="display:none" class="ef-success">'.esc_html( $success_message ).'</div>';
131 $output .= '<div id="'.esc_attr( $keys[8] ).'-'.$s.'" style="display:none" class="ef-error"></div>';
132 $output .= '<form id="'.esc_attr( $keys[0] ).'-'.$s.'" class="'.esc_attr( $keys[0] ).'">';
133 $output = apply_filters( 'essential_form_before_name', $output, $post, $settings, $atts );
134 $output .= '<div class="ef-ctrl-wrp">';
135 $output .= '<div><label for="'.esc_attr( $keys[1] ).'-'.$s.'">'.esc_html( $label_name ).'</label></div>';
136 $output .= '<div><input type="text" id="'.esc_attr( $keys[1] ).'-'.$s.'" class="'.esc_attr( $keys[1] ).'"' . ( apply_filters( 'essential_form_name_required', true ) ? ' required' : '' ) . ' /></div>';
137 $output .= '</div>';
138 $output = apply_filters( 'essential_form_after_name', $output, $post, $settings, $atts );
139 $output .= '<div class="ef-ctrl-wrp">';
140 $output .= '<div><label for="'.esc_attr( $keys[2] ).'-'.$s.'">'.esc_html( $label_email ).'</label></div>';
141 $output .= '<div><input type="email" id="'.esc_attr( $keys[2] ).'-'.$s.'" class="'.esc_attr( $keys[2] ).'"' . ( apply_filters( 'essential_form_email_required', true ) ? ' required' : '' ) . ' /></div>';
142 $output .= '</div>';
143 $output = apply_filters( 'essential_form_after_email', $output, $post, $settings, $atts );
144 $output .= '<div class="ef-ctrl-wrp">';
145 $output .= '<div><label for="'.esc_attr( $keys[3] ).'-'.$s.'">'.esc_html( $label_message ).'</label></div>';
146 $output .= '<div><textarea id="'.esc_attr( $keys[3] ).'-'.$s.'" class="'.esc_attr( $keys[3] ).'"' . ( apply_filters( 'essential_form_message_required', true ) ? ' required' : '' ) . '></textarea></div>';
147 $output .= '</div>';
148 $output = apply_filters( 'essential_form_after_message', $output, $post, $settings, $atts );
149 $checkbox_required = '';
150 $checkbox_wrapper_style = ' style="display:none"';
151 if( ! defined( 'ESSENTIAL_FORM_ASK_FOR_AGREEMENT' ) || ESSENTIAL_FORM_ASK_FOR_AGREEMENT ) {
152 $checkbox_required = ' ' . apply_filters( 'essential_form_agreement_checkbox_required', 'required' );
153 $checkbox_wrapper_style = '';
154 }
155 $checkbox_input_style = ' required' === $checkbox_required ? '' : ' style="display:none"';
156 $lable_open = $label_close = '';
157 if( apply_filters( 'essential_form_agreement_clickable', ! defined( 'ESSENTIAL_FORM_AGREEMENT_CLICKABLE' ) || ESSENTIAL_FORM_AGREEMENT_CLICKABLE ) ) {
158 $label_open = '<label for="'.esc_attr( $keys[4] ).'-'.$s.'">';
159 $label_close = '</label>';
160 }
161 $output .= '<div' . $checkbox_wrapper_style . '> ' . $label_open . '<input' . $checkbox_input_style . ' type="checkbox" id="'.esc_attr( $keys[4] ).'-'.$s.'" class="'.esc_attr( $keys[4] ).'"' . $checkbox_required . ' value="on" /> '.wp_kses( $agreement_text ,array( 'a' => array( 'href' => array(),'target' => array() ) ) ) . $label_close . '</div>';
162 $output = apply_filters( 'essential_form_after_checkbox', $output, $post, $settings, $atts );
163 $output .= '<div><button id="'.esc_attr( $keys[3] ).'-'.$s.'" type="submit" class="button" style="';
164 $output .= apply_filters( 'essential_form_submit_style', 'background-size:22px 22px !important;background-repeat:no-repeat !important;background-image:url('.includes_url( '/images/spinner.gif' ).') !important' );
165 $output .= '" onclick="essential_form_'.sanitize_key( $keys[0] ).'('.$s.');return false;">'.esc_html( $button_text ).'</div>';
166 $output = apply_filters( 'essential_form_after_submit', $output, $post, $settings, $atts );
167 $output .= '<input type="hidden" value="'.esc_attr( implode( ',',$keys ) ).'" id="'.esc_attr( $keys[5] ).'-'.$s.'" />';
168 $output .= '<input type="hidden" value="" id="'.esc_attr( $keys[6] ).'-'.$s.'" />';
169 $output .= '</form>';
170 $output .= '<div id="'.esc_attr( $keys[8] ).'-'.$s.'" style="margin-bottom:16px"></div>';
171 $output .= '</div>';
172 if( 1 === $s ){
173 $output .= '<script id="'.esc_attr( $keys[0] ).'-'.$s.'-js" type="text/javascript">';
174 $output .= 'essential_form_random_key = Math.floor(Math.random() * 99999999999999);';
175 $output .= 'function essential_form_'.sanitize_key( $keys[0] ).'_init(){';
176 $output .= 'var r = new XMLHttpRequest(),f=new FormData();';
177 $output .= 'r.open("POST","'.esc_js( admin_url( 'admin-ajax.php' ) ).'?action='.ESSENTIAL_FORM_AJAX_KEY.'_get_key",true);';
178 $output .= 'r.onload = function(e){';
179 $output .= 'if(this.readyState === 4 && "" !== e.target.responseText){';
180 $output .= 'document.getElementById("'.esc_attr( $keys[6] ).'-'.$s.'").value = e.target.responseText;';
181 $output .= '}';
182 $output .= '};';
183 $output .= 'f.append("random_key",essential_form_random_key);';
184 $output .= 'r.send(f);';
185 $output .= '}';
186 $output .= 'essential_form_'.sanitize_key( $keys[0] ).'_init();';
187 $output .= 'function essential_form_'.sanitize_key( $keys[0] ).'(n){';
188 $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);';
189 $output .= 'fe.className += " ef-progress";';
190 $output .= 'succ.style.display="none";';
191 $output .= 'err.style.display="none";';
192 $output .= 'req.onload = function(e){';
193 $output .= 'if(this.readyState === 4 && "" !== e.target.responseText){';
194 $output .= 'if("1" === e.target.responseText){';
195 $output .= 'succ.style.display="block";';
196 $output .= 'err.style.display="none";';
197 $output .= 'document.getElementById("'.esc_attr( $keys[0] ).'-'.$s.'").style.visibility="hidden";';
198 $output .= '}else{';
199 $output .= 'err.style.display="block";';
200 $output .= 'err.innerHTML=e.target.responseText;';
201 $output .= '}';
202 $output .= '}';
203 $output .= 'fe.className = fe.className.replace(" ef-progress","");';
204 $output .= 'return false;';
205 $output .= '};';
206 $f = 1;
207 foreach( essential_form_fields_array() as $fkey ){
208 if( 'agreement' === $fkey ){
209 $output .= 'fd.append("'.esc_js( esc_attr( $fkey ) ).'",document.getElementById("'.esc_js( esc_attr( $keys[$f] ) ).'-" + n).checked);';
210 }
211 elseif( false === strpos( $fkey, 'custom_' ) ){
212 $output .= 'fd.append("'.esc_js( esc_attr( $fkey ) ).'",document.getElementById("'.esc_js( esc_attr( $keys[$f] ) ).'-" + n).value);';
213 }
214 ++$f;
215 }
216 $output .= apply_filters( 'essential_form_append_custom_field', '' );
217 global $post;
218 if( $post && is_object( $post ) && isset( $post->ID ) ){
219 $output .= 'fd.append("post_id",'.absint( $post->ID ).');';
220 }
221 $output .= 'fd.append("random_key",essential_form_random_key);';
222 $output .= 'req.open("POST","'.esc_js( admin_url( 'admin-ajax.php' ) ).'?action='.ESSENTIAL_FORM_AJAX_KEY.'",true);';
223 $output .= 'req.send(fd);';
224 $output .= 'return false;';
225 $output .= '}';
226 $output .= '</script>';
227 }
228 return $output;
229 }
230 } );
231
232 // Return array of fields.
233 function essential_form_fields_array(){
234 return apply_filters( 'essential_form_fields_array', array(
235 'name',
236 'email',
237 'message',
238 'agreement',
239 'keys',
240 'temp_key'
241 ) );
242 }
243
244 // Get keys for the anti-spam system.
245 function essential_form_get_keys(){
246 $keys = false;
247 $opts = essential_form_get_option( 'essential_form' );
248 if( $opts && isset( $opts['activation_keys'] ) ){
249 $keys = $opts['activation_keys'];
250 foreach( $keys as &$key ){
251 $key = 'f'.sanitize_text_field( substr( md5( $key ),0,8 ) );
252 }
253 }
254 return $keys;
255 }
256
257 add_filter( 'plugin_action_links_'.untrailingslashit( plugin_basename( __FILE__ ) ),function( $links ) {
258 // It action links in the plugins page.
259 $links[] = sprintf( 'Shortcode: %s','<span class="ef-shortcode" style="color:#000">[essential_form]</span>' );
260 $links[] = '<a href="https://wordpress.org/plugins/essential-form/" target="_blank" rel="noopener">'.esc_html__( 'Description','essential-form' ).'</a>';
261 return $links;
262 } );
263
264 add_action( 'init',function(){
265 // It loads plugin translation files.
266 load_plugin_textdomain( 'essential-form',false,ESSENTIAL_FORM_PLUGIN_DIR.'/languages/' );
267 } );
268
269 add_filter( 'load_textdomain_mofile',function( $mofile,$domain ){
270 // Filter function to read plugin translation files.
271 if ( 'essential-form' === $domain ) {
272 $loc = function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
273 $mofile = defined( 'WP_LANG_DIR' ) && WP_LANG_DIR && file_exists( WP_LANG_DIR . '/plugins/essential-form-' . $loc . '.mo' ) ? WP_LANG_DIR . '/plugins/essential-form-' . $loc . '.mo' : ESSENTIAL_FORM_PLUGIN_DIR . '/languages/essential-form-' . $loc . '.mo';
274 }
275 return $mofile;
276 },10,2 );
277
278
279 // Helper function to add custom field.
280 function essential_form_add_field( $slug, $type, $title, $required = false, $near_field = 'message', $error_message = false, $field_class = '' ) {
281 require_once ESSENTIAL_FORM_PLUGIN_DIR . '/classes/ef-class-add-fields-factory.php';
282 $new_field = new Essential_Form_Fields_Factory( $slug, $type, $title, $required, $near_field, $error_message, $field_class );
283 }