define_constants(); register_activation_hook( __FILE__, array( $this, 'activate' ) ); register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) ); add_action( 'init', array( $this, 'maybe_requires_core' ) ); add_action( 'wpuf_loaded', array( $this, 'init_plugin' ) ); } /** * Define the constants * * @return void */ private function define_constants() { define( 'WEFORMS_VERSION', '1.0' ); define( 'WEFORMS_FILE', __FILE__ ); define( 'WEFORMS_ROOT', dirname( __FILE__ ) ); define( 'WEFORMS_INCLUDES', WEFORMS_ROOT . '/includes' ); define( 'WEFORMS_ROOT_URI', plugins_url( '', __FILE__ ) ); define( 'WEFORMS_ASSET_URI', WEFORMS_ROOT_URI . '/assets' ); } /** * Load the plugin after WP User Frontend is loaded * * @return void */ public function init_plugin() { $this->includes(); $this->init_hooks(); do_action( 'weforms_loaded' ); } /** * Initializes the WeForms() class * * Checks for an existing WeForms() instance * and if it doesn't find one, creates it. */ public static function init() { static $instance = false; if ( ! $instance ) { $instance = new WeForms(); } return $instance; } /** * 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}weforms_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}weforms_entrymeta` ( `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `weforms_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` (`weforms_entry_id`) ) $collate;", ); require_once ABSPATH . 'wp-admin/includes/upgrade.php'; foreach ( $table_schema as $table ) { dbDelta( $table ); } $this->maybe_set_default_settings(); $this->create_default_form(); update_option( 'weforms_installed', time() ); update_option( 'weforms_version', WEFORMS_VERSION ); } /** * Placeholder for deactivation function * * Nothing being called here yet. */ public function deactivate() { } /** * Include the required classes * * @return void */ public function includes() { if ( is_admin() ) { require_once WEFORMS_INCLUDES . '/admin/class-admin.php'; require_once WEFORMS_INCLUDES . '/admin/class-cf7.php'; require_once WEFORMS_INCLUDES . '/admin/class-form-template.php'; require_once WEFORMS_INCLUDES . '/admin/class-pro-integrations.php'; } else { require_once WPUF_ROOT . '/class/render-form.php'; require_once WEFORMS_INCLUDES . '/class-frontend-form.php'; } require_once WEFORMS_INCLUDES . '/class-emailer.php'; require_once WEFORMS_INCLUDES . '/class-ajax.php'; require_once WEFORMS_INCLUDES . '/class-notification.php'; require_once WEFORMS_INCLUDES . '/functions.php'; require_once WEFORMS_INCLUDES . '/functions-template-contact-form.php'; } /** * Initialize the hooks * * @return void */ public function init_hooks() { // Localize our plugin add_action( 'init', array( $this, 'localization_setup' ) ); // initialize the classes add_action( 'init', array( $this, 'init_classes' ) ); add_action( 'init', array( $this, 'wpdb_table_shortcuts' ), 0 ); add_filter( 'wpuf_integrations', array( $this, 'register_default_integrations' ) ); add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) ); } /** * Set WPDB table shortcut names * * @return void */ public function wpdb_table_shortcuts() { global $wpdb; $wpdb->weforms_entries = $wpdb->prefix . 'weforms_entries'; $wpdb->weforms_entrymeta = $wpdb->prefix . 'weforms_entrymeta'; } /** * Check if the core WP User Frontend is installed * * @return boolean */ public function is_core_installed() { return class_exists( 'WP_User_Frontend' ); } /** * If the core isn't installed * * @return void */ public function maybe_requires_core() { if ( $this->is_core_installed() ) { return; } // show the notice add_action( 'admin_notices', array( $this, 'core_activation_notice' ) ); // install the core add_action( 'wp_ajax_wpuf_cf_install_wpuf', array( $this, 'install_wp_user_frontend' ) ); } /** * The prompt to install the core plugin * * @return void */ public function core_activation_notice() { ?>
emailer = new WeForms_Emailer(); if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { new WeForms_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', 'weforms' ) ); } 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 ); } // hide wpuf page creation notice and tracking update_option( '_wpuf_page_created', '1' ); update_option( 'wp-user-frontend_tracking_notice', 'hide' ); wp_send_json_success(); } /** * Plugin action links * * @param array $links * * @return array */ function plugin_action_links( $links ) { $links[] = '' . __( 'Settings', 'weforms' ) . ''; return $links; } /** * Register default integrations * * @param array $integrations * * @return array */ public function register_default_integrations( $integrations ) { require_once WEFORMS_INCLUDES . '/integrations/slack/class-integration-slack.php'; $integrations = array_merge( $integrations, array( 'WeForms_Integration_Slack' ) ); return $integrations; } /** * Set the required default settings key if not present * * This is required for setting up the component settings data * * @return void */ public function maybe_set_default_settings() { $requires_update = false; $settings = get_option( 'weforms_settings', array() ); $additional_keys = array( 'email_gateway' => 'wordpress', 'recaptcha' => array( 'type' => 'v2', 'key' => '', 'secret' => '' ) ); foreach ($additional_keys as $key => $value) { if ( ! isset( $settings[ $key ] ) ) { $settings[ $key ] = $value; $requires_update = true; } } if ( $requires_update ) { update_option( 'weforms_settings', $settings ); } } /** * Create a default form * * @return void */ public function create_default_form() { $version = get_option( 'weforms_version' ); // seems like it's already installed if ( $version ) { return; } if ( ! function_exists( 'weforms_get_contactform_template_fields' ) ) { require_once dirname( __FILE__ ) . '/includes/functions-template-contact-form.php'; } $form_post_data = array( 'post_title' => __( 'Contact Form', 'weforms' ), 'post_type' => 'wpuf_contact_form', 'post_status' => 'publish', 'post_author' => get_current_user_id() ); $form_id = wp_insert_post( $form_post_data ); if ( is_wp_error( $form_id ) ) { return; } update_post_meta( $form_id, 'wpuf_form_settings', weforms_get_contactform_template_settings() ); update_post_meta( $form_id, 'notifications', weforms_get_contactform_template_notification() ); $form_fields = weforms_get_contactform_template_fields(); if ( $form_fields ) { foreach ($form_fields as $menu_order => $field) { wp_insert_post( array( 'post_type' => 'wpuf_input', 'post_status' => 'publish', 'post_content' => maybe_serialize( $field ), 'post_parent' => $form_id, 'menu_order' => $menu_order ) ); } } } } // WeForms /** * Initialize the plugin * * @return \WeForms */ function weforms() { return WeForms::init(); } // kick-off weforms();