-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcontact-form.php
336 lines (284 loc) · 11.7 KB
/
contact-form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/*
Plugin Name: Best Contact Form
Plugin URI: https://wedevs.com/wp-user-frontend-pro/
Description: Contact form plugin for WordPress
Version: 0.1
Author: weDevs
Author URI: https://wedevs.com/
License: GPL2
TextDomain: wpuf-contact-form
*/
/**
* Copyright (c) 2017 weDevs LLC (email: [email protected]). All rights reserved.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* **********************************************************************
*/
// don't call the file directly
if ( !defined( 'ABSPATH' ) ) exit;
/**
* WPUF_Contact_Form class
*
* @class WPUF_Contact_Form The class that holds the entire WPUF_Contact_Form plugin
*/
class WPUF_Contact_Form {
/**
* Constructor for the WPUF_Contact_Form class
*
* Sets up all the appropriate hooks and actions
* within our plugin.
*
* @uses register_activation_hook()
* @uses register_deactivation_hook()
* @uses is_admin()
* @uses add_action()
*/
public function __construct() {
$this->define_constants();
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
// Localize our plugin
add_action( 'init', array( $this, 'localization_setup' ) );
add_action( 'plugins_loaded', array( $this, 'init_plugin' ) );
// install the core
add_action( 'wp_ajax_wpuf_cf_install_wpuf', array( $this, 'install_wp_user_frontend' ) );
}
/**
* Initializes the WPUF_Contact_Form() class
*
* Checks for an existing WPUF_Contact_Form() instance
* and if it doesn't find one, creates it.
*/
public static function init() {
static $instance = false;
if ( ! $instance ) {
$instance = new WPUF_Contact_Form();
}
return $instance;
}
public function init_plugin() {
global $wpdb;
// bail out early if the core isn't installed
if ( ! class_exists( 'WP_User_Frontend' ) ) {
add_action( 'admin_notices', array( $this, 'core_activation_notice' ) );
return;
}
$wpdb->wpuf_cf_entries = $wpdb->prefix . 'wpuf_cf_entries';
$wpdb->wpuf_cf_entrymeta = $wpdb->prefix . 'wpuf_cf_entrymeta';
// seems like we have the core, we shall pass!!!
$this->includes();
$this->init_classes();
}
/**
* Placeholder for activation function
*
* Nothing being called here yet.
*/
public function activate() {
global $wpdb;
$collate = '';
if ( $wpdb->has_cap( 'collation' ) ) {
if ( ! empty($wpdb->charset ) ) {
$collate .= "DEFAULT CHARACTER SET $wpdb->charset";
}
if ( ! empty($wpdb->collate ) ) {
$collate .= " COLLATE $wpdb->collate";
}
}
$table_schema = array(
"CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}wpuf_cf_entries` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`form_id` bigint(20) unsigned DEFAULT NULL,
`user_id` bigint(20) unsigned DEFAULT NULL,
`user_ip` int(11) unsigned DEFAULT NULL,
`user_device` varchar(50) DEFAULT NULL,
`referer` varchar(255) DEFAULT NULL,
`status` varchar(10) DEFAULT 'publish',
`created_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `form_id` (`form_id`)
) $collate;",
"CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}wpuf_cf_entrymeta` (
`meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`wpuf_cf_entry_id` bigint(20) unsigned DEFAULT NULL,
`meta_key` varchar(255) DEFAULT NULL,
`meta_value` longtext,
PRIMARY KEY (`meta_id`),
KEY `meta_key` (`meta_key`),
KEY `entry_id` (`wpuf_cf_entry_id`)
) $collate;",
);
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
foreach ( $table_schema as $table ) {
dbDelta( $table );
}
update_option( 'wpuf_cf_version', WPUF_CONTACT_FORM_VERSION );
}
/**
* Placeholder for deactivation function
*
* Nothing being called here yet.
*/
public function deactivate() {
}
/**
* The prompt to install the core plugin
*
* @return void
*/
public function core_activation_notice() {
?>
<div class="updated" id="wpuf-contact-form-installer-notice" style="padding: 1em; position: relative;">
<h2><?php _e( 'Your Contact Form is almost ready!', 'best-contact-form' ); ?></h2>
<?php
$plugin_file = basename( dirname( __FILE__ ) ) . '/contact-form.php';
$core_plugin_file = 'wp-user-frontend/wpuf.php';
?>
<a href="<?php echo wp_nonce_url( 'plugins.php?action=deactivate&plugin=' . $plugin_file . '&plugin_status=all&paged=1&s=', 'deactivate-plugin_' . $plugin_file ); ?>" class="notice-dismiss" style="text-decoration: none;" title="<?php _e( 'Dismiss this notice', 'best-contact-form' ); ?>"></a>
<?php if ( file_exists( WP_PLUGIN_DIR . '/' . $core_plugin_file ) && is_plugin_inactive( 'wpuf-user-frontend' ) ): ?>
<p><?php _e( 'You just need to activate the Core Plugin to make it functional.', 'best-contact-form' ); ?></p>
<p>
<a class="button button-primary" href="<?php echo wp_nonce_url( 'plugins.php?action=activate&plugin=' . $core_plugin_file . '&plugin_status=all&paged=1&s=', 'activate-plugin_' . $core_plugin_file ); ?>" title="<?php _e( 'Activate this plugin', 'best-contact-form' ); ?>"><?php _e( 'Activate', 'best-contact-form' ); ?></a>
</p>
<?php else: ?>
<p><?php echo sprintf( __( "You just need to install the %sCore Plugin%s to make it functional.", "wpuf-contact-form" ), '<a target="_blank" href="https://wordpress.org/plugins/wp-user-frontend/">', '</a>' ); ?></p>
<p>
<button id="wpuf-contact-form-installer" class="button"><?php _e( 'Install Now', 'best-contact-form' ); ?></button>
</p>
<?php endif; ?>
</div>
<script type="text/javascript">
(function ($) {
var wrapper = $('#wpuf-contact-form-installer-notice');
wrapper.on('click', '#wpuf-contact-form-installer', function (e) {
var self = $(this);
e.preventDefault();
self.addClass('install-now updating-message');
self.text('<?php echo esc_js( 'Installing...', 'best-contact-form' ); ?>');
var data = {
action: 'wpuf_cf_install_wpuf',
_wpnonce: '<?php echo wp_create_nonce('wpuf-installer-nonce'); ?>'
};
$.post(ajaxurl, data, function (response) {
if (response.success) {
self.attr('disabled', 'disabled');
self.removeClass('install-now updating-message');
self.text('<?php echo esc_js( 'Installed', 'best-contact-form' ); ?>');
window.location.href = '<?php echo admin_url( 'admin.php?page=wpuf-contact-forms' ); ?>';
}
});
});
})(jQuery);
</script>
<?php
}
/**
* Initialize plugin for localization
*
* @uses load_plugin_textdomain()
*/
public function localization_setup() {
load_plugin_textdomain( 'best-contact-form', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Define the constants
*
* @return void
*/
private function define_constants() {
define( 'WPUF_CONTACT_FORM_VERSION', '1.0' );
define( 'WPUF_CONTACT_FORM_FILE', __FILE__ );
define( 'WPUF_CONTACT_FORM_ROOT', dirname( __FILE__ ) );
define( 'WPUF_CONTACT_FORM_INCLUDES', WPUF_CONTACT_FORM_ROOT . '/includes' );
define( 'WPUF_CONTACT_FORM_ROOT_URI', plugins_url( '', __FILE__ ) );
define( 'WPUF_CONTACT_FORM_ASSET_URI', WPUF_CONTACT_FORM_ROOT_URI . '/assets' );
}
/**
* Include the required classes
*
* @return void
*/
public function includes() {
if ( is_admin() ) {
require_once WPUF_CONTACT_FORM_INCLUDES . '/admin/admin.php';
require_once WPUF_CONTACT_FORM_INCLUDES . '/admin/class-contact-form-builder.php';
require_once WPUF_CONTACT_FORM_INCLUDES . '/admin/class-form-template.php';
} else {
require_once WPUF_CONTACT_FORM_INCLUDES . '/class-frontend-form.php';
}
require_once WPUF_CONTACT_FORM_INCLUDES . '/class-ajax.php';
require_once WPUF_CONTACT_FORM_INCLUDES . '/class-notification.php';
require_once WPUF_CONTACT_FORM_INCLUDES . '/functions.php';
}
/**
* Instantiate the required classes
*
* @return void
*/
public function init_classes() {
if ( is_admin() ) {
new WPUF_Contact_Form_Admin();
new WPUF_Contact_Form_Builder();
new WPUF_Contact_Form_Template();
} else {
new WPUF_Contact_Form_Frontend();
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
new WPUF_Contact_Form_Ajax();
}
}
/**
* Install the WP User Frontend plugin via ajax
*
* @return void
*/
public function install_wp_user_frontend() {
if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'wpuf-installer-nonce' ) ) {
wp_send_json_error( __( 'Error: Nonce verification failed', 'wpuf-pro' ) );
}
include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
$plugin = 'wp-user-frontend';
$api = plugins_api( 'plugin_information', array( 'slug' => $plugin, 'fields' => array( 'sections' => false ) ) );
$upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() );
$result = $upgrader->install( $api->download_link );
if ( is_wp_error( $result ) ) {
wp_send_json_error( $result );
}
$result = activate_plugin( 'wp-user-frontend/wpuf.php' );
if ( is_wp_error( $result ) ) {
wp_send_json_error( $result );
}
wp_send_json_success();
}
} // WPUF_Contact_Form
/**
* Initialize the plugin
*
* @return \WPUF_Contact_Form
*/
function wpuf_contact_form() {
return WPUF_Contact_Form::init();
}
// kick-off
wpuf_contact_form();