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

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