| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The installer class |
| 5 |
* |
| 6 |
* @since 1.1.0 |
| 7 |
*/ |
| 8 |
class WeForms_Installer { |
| 9 |
|
| 10 |
/** |
| 11 |
* The installer class |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public function install() { |
| 16 |
|
| 17 |
$this->create_tables(); |
| 18 |
$this->maybe_set_default_settings(); |
| 19 |
$this->create_default_form(); |
| 20 |
|
| 21 |
$installed = get_option( 'weforms_installed' ); |
| 22 |
|
| 23 |
if ( ! $installed ) { |
| 24 |
update_option( 'weforms_installed', time() ); |
| 25 |
} |
| 26 |
|
| 27 |
set_transient( 'weforms_activation_redirect', true, 30 ); |
| 28 |
update_option( 'weforms_version', WEFORMS_VERSION ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Create the table schema |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function create_tables() { |
| 37 |
global $wpdb; |
| 38 |
|
| 39 |
$collate = ''; |
| 40 |
|
| 41 |
if ( $wpdb->has_cap( 'collation' ) ) { |
| 42 |
if ( ! empty($wpdb->charset ) ) { |
| 43 |
$collate .= "DEFAULT CHARACTER SET $wpdb->charset"; |
| 44 |
} |
| 45 |
|
| 46 |
if ( ! empty($wpdb->collate ) ) { |
| 47 |
$collate .= " COLLATE $wpdb->collate"; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
$table_schema = array( |
| 52 |
"CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}weforms_entries` ( |
| 53 |
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 54 |
`form_id` bigint(20) unsigned DEFAULT NULL, |
| 55 |
`user_id` bigint(20) unsigned DEFAULT NULL, |
| 56 |
`user_ip` int(11) unsigned DEFAULT NULL, |
| 57 |
`user_device` varchar(50) DEFAULT NULL, |
| 58 |
`referer` varchar(255) DEFAULT NULL, |
| 59 |
`status` varchar(10) DEFAULT 'publish', |
| 60 |
`created_at` datetime DEFAULT NULL, |
| 61 |
PRIMARY KEY (`id`), |
| 62 |
KEY `form_id` (`form_id`) |
| 63 |
) $collate;", |
| 64 |
|
| 65 |
"CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}weforms_entrymeta` ( |
| 66 |
`meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 67 |
`weforms_entry_id` bigint(20) unsigned DEFAULT NULL, |
| 68 |
`meta_key` varchar(255) DEFAULT NULL, |
| 69 |
`meta_value` longtext, |
| 70 |
PRIMARY KEY (`meta_id`), |
| 71 |
KEY `meta_key` (`meta_key`), |
| 72 |
KEY `entry_id` (`weforms_entry_id`) |
| 73 |
) $collate;", |
| 74 |
); |
| 75 |
|
| 76 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 77 |
foreach ( $table_schema as $table ) { |
| 78 |
dbDelta( $table ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Set the required default settings key if not present |
| 84 |
* |
| 85 |
* This is required for setting up the component settings data |
| 86 |
* |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function maybe_set_default_settings() { |
| 90 |
$requires_update = false; |
| 91 |
$settings = get_option( 'weforms_settings', array() ); |
| 92 |
$additional_keys = array( |
| 93 |
'email_gateway' => 'wordpress', |
| 94 |
'credit' => false, |
| 95 |
'recaptcha' => array( 'key' => '', 'secret' => '' ) |
| 96 |
); |
| 97 |
|
| 98 |
foreach ($additional_keys as $key => $value) { |
| 99 |
if ( ! isset( $settings[ $key ] ) ) { |
| 100 |
$settings[ $key ] = $value; |
| 101 |
|
| 102 |
$requires_update = true; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if ( $requires_update ) { |
| 107 |
update_option( 'weforms_settings', $settings ); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Create a default form |
| 113 |
* |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
public function create_default_form() { |
| 117 |
$version = get_option( 'weforms_version' ); |
| 118 |
|
| 119 |
// seems like it's already installed |
| 120 |
if ( $version ) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
weforms()->templates->create( 'contact' ); |
| 125 |
} |
| 126 |
} |