PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.0.0-beta.3
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.0.0-beta.3
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / weforms.php

weforms.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.0.0-beta.3, at weforms.php

510 lines 16.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: weForms
4 * Description: The best contact form plugin for WordPress
5 * Plugin URI: https://wedevs.com/weforms/
6 * Author: weDevs
7 * Author URI: https://wedevs.com/
8 * Version: 1.0.0-beta.3
9 * License: GPL2 or later
10 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
11 * Text Domain: weforms
12 * Domain Path: /languages
13 */
14
15 /**
16 * Copyright (c) 2017 weDevs LLC (email: info@wedevs.com). All rights reserved.
17 *
18 * Released under the GPL license
19 * http://www.opensource.org/licenses/gpl-license.php
20 *
21 * This is an add-on for WordPress
22 * http://wordpress.org/
23 *
24 * **********************************************************************
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 * **********************************************************************
39 */
40
41 // don't call the file directly
42 if ( !defined( 'ABSPATH' ) ) exit;
43
44 /**
45 * WeForms class
46 *
47 * @class WeForms The class that holds the entire WeForms plugin
48 */
49 final class WeForms {
50
51 /**
52 * Plugin version
53 *
54 * @var string
55 */
56 public $version = '1.0.0-beta.3';
57
58 /**
59 * Constructor for the WeForms class
60 *
61 * Sets up all the appropriate hooks and actions
62 * within our plugin.
63 *
64 * @uses register_activation_hook()
65 * @uses register_deactivation_hook()
66 * @uses is_admin()
67 * @uses add_action()
68 */
69 public function __construct() {
70 $this->define_constants();
71
72 register_activation_hook( __FILE__, array( $this, 'activate' ) );
73 register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
74
75 add_action( 'init', array( $this, 'maybe_requires_core' ) );
76 add_action( 'wpuf_loaded', array( $this, 'init_plugin' ) );
77 }
78
79 /**
80 * Define the constants
81 *
82 * @return void
83 */
84 private function define_constants() {
85 define( 'WEFORMS_VERSION', '1.0' );
86 define( 'WEFORMS_FILE', __FILE__ );
87 define( 'WEFORMS_ROOT', dirname( __FILE__ ) );
88 define( 'WEFORMS_INCLUDES', WEFORMS_ROOT . '/includes' );
89 define( 'WEFORMS_ROOT_URI', plugins_url( '', __FILE__ ) );
90 define( 'WEFORMS_ASSET_URI', WEFORMS_ROOT_URI . '/assets' );
91 }
92
93 /**
94 * Load the plugin after WP User Frontend is loaded
95 *
96 * @return void
97 */
98 public function init_plugin() {
99 $this->includes();
100 $this->init_hooks();
101 }
102
103 /**
104 * Initializes the WeForms() class
105 *
106 * Checks for an existing WeForms() instance
107 * and if it doesn't find one, creates it.
108 */
109 public static function init() {
110 static $instance = false;
111
112 if ( ! $instance ) {
113 $instance = new WeForms();
114 }
115
116 return $instance;
117 }
118
119 /**
120 * Placeholder for activation function
121 *
122 * Nothing being called here yet.
123 */
124 public function activate() {
125 global $wpdb;
126
127 $collate = '';
128
129 if ( $wpdb->has_cap( 'collation' ) ) {
130 if ( ! empty($wpdb->charset ) ) {
131 $collate .= "DEFAULT CHARACTER SET $wpdb->charset";
132 }
133
134 if ( ! empty($wpdb->collate ) ) {
135 $collate .= " COLLATE $wpdb->collate";
136 }
137 }
138
139 $table_schema = array(
140 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}weforms_entries` (
141 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
142 `form_id` bigint(20) unsigned DEFAULT NULL,
143 `user_id` bigint(20) unsigned DEFAULT NULL,
144 `user_ip` int(11) unsigned DEFAULT NULL,
145 `user_device` varchar(50) DEFAULT NULL,
146 `referer` varchar(255) DEFAULT NULL,
147 `status` varchar(10) DEFAULT 'publish',
148 `created_at` datetime DEFAULT NULL,
149 PRIMARY KEY (`id`),
150 KEY `form_id` (`form_id`)
151 ) $collate;",
152
153 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}weforms_entrymeta` (
154 `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
155 `weforms_entry_id` bigint(20) unsigned DEFAULT NULL,
156 `meta_key` varchar(255) DEFAULT NULL,
157 `meta_value` longtext,
158 PRIMARY KEY (`meta_id`),
159 KEY `meta_key` (`meta_key`),
160 KEY `entry_id` (`weforms_entry_id`)
161 ) $collate;",
162 );
163
164 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
165 foreach ( $table_schema as $table ) {
166 dbDelta( $table );
167 }
168
169 $this->maybe_set_default_settings();
170 $this->create_default_form();
171
172 update_option( 'weforms_installed', time() );
173 update_option( 'weforms_version', WEFORMS_VERSION );
174 }
175
176 /**
177 * Placeholder for deactivation function
178 *
179 * Nothing being called here yet.
180 */
181 public function deactivate() {
182
183 }
184
185 /**
186 * Include the required classes
187 *
188 * @return void
189 */
190 public function includes() {
191
192 if ( is_admin() ) {
193 require_once WEFORMS_INCLUDES . '/admin/class-admin.php';
194 require_once WEFORMS_INCLUDES . '/admin/class-cf7.php';
195 require_once WEFORMS_INCLUDES . '/admin/class-form-template.php';
196 require_once WEFORMS_INCLUDES . '/admin/class-pro-integrations.php';
197 } else {
198 require_once WPUF_ROOT . '/class/render-form.php';
199 require_once WEFORMS_INCLUDES . '/class-frontend-form.php';
200 }
201
202 require_once WEFORMS_INCLUDES . '/class-ajax.php';
203 require_once WEFORMS_INCLUDES . '/class-notification.php';
204 require_once WEFORMS_INCLUDES . '/functions.php';
205 require_once WEFORMS_INCLUDES . '/functions-template-contact-form.php';
206 }
207
208 /**
209 * Initialize the hooks
210 *
211 * @return void
212 */
213 public function init_hooks() {
214 // Localize our plugin
215 add_action( 'init', array( $this, 'localization_setup' ) );
216
217 // initialize the classes
218 add_action( 'init', array( $this, 'init_classes' ) );
219 add_action( 'init', array( $this, 'wpdb_table_shortcuts' ), 0 );
220
221 add_filter( 'wpuf_integrations', array( $this, 'register_default_integrations' ) );
222
223 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
224 }
225
226 /**
227 * Set WPDB table shortcut names
228 *
229 * @return void
230 */
231 public function wpdb_table_shortcuts() {
232 global $wpdb;
233
234 $wpdb->weforms_entries = $wpdb->prefix . 'weforms_entries';
235 $wpdb->weforms_entrymeta = $wpdb->prefix . 'weforms_entrymeta';
236 }
237
238 /**
239 * Check if the core WP User Frontend is installed
240 *
241 * @return boolean
242 */
243 public function is_core_installed() {
244 return class_exists( 'WP_User_Frontend' );
245 }
246
247 /**
248 * If the core isn't installed
249 *
250 * @return void
251 */
252 public function maybe_requires_core() {
253 if ( $this->is_core_installed() ) {
254 return;
255 }
256
257 // show the notice
258 add_action( 'admin_notices', array( $this, 'core_activation_notice' ) );
259
260 // install the core
261 add_action( 'wp_ajax_wpuf_cf_install_wpuf', array( $this, 'install_wp_user_frontend' ) );
262 }
263
264 /**
265 * The prompt to install the core plugin
266 *
267 * @return void
268 */
269 public function core_activation_notice() {
270 ?>
271 <div class="updated" id="wpuf-contact-form-installer-notice" style="padding: 1em; position: relative;">
272 <h2><?php _e( 'weForms is almost ready!', 'weforms' ); ?></h2>
273
274 <?php
275 $plugin_file = basename( dirname( __FILE__ ) ) . '/contact-form.php';
276 $core_plugin_file = 'wp-user-frontend/wpuf.php';
277 ?>
278 <a href="<?php echo wp_nonce_url( 'plugins.php?action=deactivate&amp;plugin=' . $plugin_file . '&amp;plugin_status=all&amp;paged=1&amp;s=', 'deactivate-plugin_' . $plugin_file ); ?>" class="notice-dismiss" style="text-decoration: none;" title="<?php _e( 'Dismiss this notice', 'weforms' ); ?>"></a>
279
280 <?php if ( file_exists( WP_PLUGIN_DIR . '/' . $core_plugin_file ) && is_plugin_inactive( 'wpuf-user-frontend' ) ): ?>
281 <p><?php _e( 'You just need to activate the Core Plugin to make it functional.', 'weforms' ); ?></p>
282 <p>
283 <a class="button button-primary" href="<?php echo wp_nonce_url( 'plugins.php?action=activate&amp;plugin=' . $core_plugin_file . '&amp;plugin_status=all&amp;paged=1&amp;s=', 'activate-plugin_' . $core_plugin_file ); ?>" title="<?php _e( 'Activate this plugin', 'weforms' ); ?>"><?php _e( 'Activate', 'weforms' ); ?></a>
284 </p>
285 <?php else: ?>
286 <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>
287
288 <p>
289 <button id="wpuf-contact-form-installer" class="button"><?php _e( 'Install Now', 'weforms' ); ?></button>
290 </p>
291 <?php endif; ?>
292 </div>
293
294 <script type="text/javascript">
295 (function ($) {
296 var wrapper = $('#wpuf-contact-form-installer-notice');
297
298 wrapper.on('click', '#wpuf-contact-form-installer', function (e) {
299 var self = $(this);
300
301 e.preventDefault();
302 self.addClass('install-now updating-message');
303 self.text('<?php echo esc_js( 'Installing...', 'weforms' ); ?>');
304
305 var data = {
306 action: 'wpuf_cf_install_wpuf',
307 _wpnonce: '<?php echo wp_create_nonce('wpuf-installer-nonce'); ?>'
308 };
309
310 $.post(ajaxurl, data, function (response) {
311 if (response.success) {
312 self.attr('disabled', 'disabled');
313 self.removeClass('install-now updating-message');
314 self.text('<?php echo esc_js( 'Installed', 'weforms' ); ?>');
315
316 window.location.href = '<?php echo admin_url( 'admin.php?page=weforms' ); ?>';
317 }
318 });
319 });
320 })(jQuery);
321 </script>
322 <?php
323 }
324
325 /**
326 * Initialize plugin for localization
327 *
328 * @uses load_plugin_textdomain()
329 */
330 public function localization_setup() {
331 load_plugin_textdomain( 'weforms', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
332 }
333
334 /**
335 * Instantiate the required classes
336 *
337 * @return void
338 */
339 public function init_classes() {
340
341 if ( is_admin() ) {
342 new WeForms_Admin();
343 new WeForms_Form_Template();
344 new WeForms_Pro_Integrations();
345 new WeForms_CF7();
346 } else {
347 new WeForms_Frontend();
348 }
349
350 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
351 new WeForms_Ajax();
352 }
353 }
354
355 /**
356 * Install the WP User Frontend plugin via ajax
357 *
358 * @return void
359 */
360 public function install_wp_user_frontend() {
361
362 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'wpuf-installer-nonce' ) ) {
363 wp_send_json_error( __( 'Error: Nonce verification failed', 'weforms' ) );
364 }
365
366 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
367 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
368
369 $plugin = 'wp-user-frontend';
370 $api = plugins_api( 'plugin_information', array( 'slug' => $plugin, 'fields' => array( 'sections' => false ) ) );
371
372 $upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() );
373 $result = $upgrader->install( $api->download_link );
374
375 if ( is_wp_error( $result ) ) {
376 wp_send_json_error( $result );
377 }
378
379 $result = activate_plugin( 'wp-user-frontend/wpuf.php' );
380
381 if ( is_wp_error( $result ) ) {
382 wp_send_json_error( $result );
383 }
384
385 // hide wpuf page creation notice and tracking
386 update_option( '_wpuf_page_created', '1' );
387 update_option( 'wp-user-frontend_tracking_notice', 'hide' );
388
389 wp_send_json_success();
390 }
391
392 /**
393 * Plugin action links
394 *
395 * @param array $links
396 *
397 * @return array
398 */
399 function plugin_action_links( $links ) {
400
401 $links[] = '<a href="' . admin_url( 'admin.php?page=weforms' ) . '">' . __( 'Settings', 'weforms' ) . '</a>';
402
403 return $links;
404 }
405
406 /**
407 * Register default integrations
408 *
409 * @param array $integrations
410 *
411 * @return array
412 */
413 public function register_default_integrations( $integrations ) {
414 require_once WEFORMS_INCLUDES . '/integrations/slack/class-integration-slack.php';
415
416 $integrations = array_merge( $integrations, array( 'WeForms_Integration_Slack' ) );
417
418 return $integrations;
419 }
420
421 /**
422 * Set the required default settings key if not present
423 *
424 * This is required for setting up the component settings data
425 *
426 * @return void
427 */
428 public function maybe_set_default_settings() {
429 $requires_update = false;
430 $settings = get_option( 'weforms_settings', array() );
431 $additional_keys = array(
432 'email_gateway' => 'wordpress',
433 'recaptcha' => array( 'type' => 'v2', 'key' => '', 'secret' => '' )
434 );
435
436 foreach ($additional_keys as $key => $value) {
437 if ( ! isset( $settings[ $key ] ) ) {
438 $settings[ $key ] = $value;
439
440 $requires_update = true;
441 }
442 }
443
444 if ( $requires_update ) {
445 update_option( 'weforms_settings', $settings );
446 }
447 }
448
449 /**
450 * Create a default form
451 *
452 * @return void
453 */
454 public function create_default_form() {
455 $version = get_option( 'weforms_version' );
456
457 // seems like it's already installed
458 if ( $version ) {
459 return;
460 }
461
462 if ( ! function_exists( 'weforms_get_contactform_template_fields' ) ) {
463 require_once dirname( __FILE__ ) . '/includes/functions-template-contact-form.php';
464 }
465
466 $form_post_data = array(
467 'post_title' => __( 'Contact Form', 'weforms' ),
468 'post_type' => 'wpuf_contact_form',
469 'post_status' => 'publish',
470 'post_author' => get_current_user_id()
471 );
472
473 $form_id = wp_insert_post( $form_post_data );
474
475 if ( is_wp_error( $form_id ) ) {
476 return;
477 }
478
479 update_post_meta( $form_id, 'wpuf_form_settings', weforms_get_contactform_template_settings() );
480 update_post_meta( $form_id, 'notifications', weforms_get_contactform_template_notification() );
481
482 $form_fields = weforms_get_contactform_template_fields();
483
484 if ( $form_fields ) {
485 foreach ($form_fields as $menu_order => $field) {
486 wp_insert_post( array(
487 'post_type' => 'wpuf_input',
488 'post_status' => 'publish',
489 'post_content' => maybe_serialize( $field ),
490 'post_parent' => $form_id,
491 'menu_order' => $menu_order
492 ) );
493 }
494 }
495 }
496
497 } // WeForms
498
499 /**
500 * Initialize the plugin
501 *
502 * @return \WeForms
503 */
504 function weforms() {
505 return WeForms::init();
506 }
507
508 // kick-off
509 weforms();
510