class.swpm-installation.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * Description of BInstallation
  4. *
  5. * @author nur
  6. */
  7. class SwpmInstallation {
  8. /*
  9. * This function is capable of handing both single site or multi-site install and upgrade all in one.
  10. */
  11. static function run_safe_installer() {
  12. global $wpdb;
  13. //Do this if multi-site setup
  14. if (function_exists('is_multisite') && is_multisite()) {
  15. // check if it is a network activation - if so, run the activation function for each blog id
  16. if (isset($_GET['networkwide']) && ($_GET['networkwide'] == 1)) {
  17. $old_blog = $wpdb->blogid;
  18. // Get all blog ids
  19. $blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
  20. foreach ($blogids as $blog_id) {
  21. switch_to_blog($blog_id);
  22. SwpmInstallation::installer();
  23. SwpmInstallation::initdb();
  24. }
  25. switch_to_blog($old_blog);
  26. return;
  27. }
  28. }
  29. //Do this if single site standard install
  30. SwpmInstallation::installer();
  31. SwpmInstallation::initdb();
  32. }
  33. public static function installer() {
  34. global $wpdb;
  35. require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  36. $charset_collate = '';
  37. if (!empty($wpdb->charset)) {
  38. $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  39. } else {
  40. $charset_collate = "DEFAULT CHARSET=utf8";
  41. }
  42. if (!empty($wpdb->collate)) {
  43. $charset_collate .= " COLLATE $wpdb->collate";
  44. }
  45. $sql = "CREATE TABLE " . $wpdb->prefix . "swpm_members_tbl (
  46. member_id int(12) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  47. user_name varchar(255) NOT NULL,
  48. first_name varchar(64) DEFAULT '',
  49. last_name varchar(64) DEFAULT '',
  50. password varchar(255) NOT NULL,
  51. member_since date NOT NULL DEFAULT '0000-00-00',
  52. membership_level smallint(6) NOT NULL,
  53. more_membership_levels VARCHAR(100) DEFAULT NULL,
  54. account_state enum('active','inactive','activation_required','expired','pending','unsubscribed') DEFAULT 'pending',
  55. last_accessed datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  56. last_accessed_from_ip varchar(128) NOT NULL,
  57. email varchar(255) DEFAULT NULL,
  58. phone varchar(64) DEFAULT NULL,
  59. address_street varchar(255) DEFAULT NULL,
  60. address_city varchar(255) DEFAULT NULL,
  61. address_state varchar(255) DEFAULT NULL,
  62. address_zipcode varchar(255) DEFAULT NULL,
  63. home_page varchar(255) DEFAULT NULL,
  64. country varchar(255) DEFAULT NULL,
  65. gender enum('male','female','not specified') DEFAULT 'not specified',
  66. referrer varchar(255) DEFAULT NULL,
  67. extra_info text,
  68. reg_code varchar(255) DEFAULT NULL,
  69. subscription_starts date DEFAULT NULL,
  70. initial_membership_level smallint(6) DEFAULT NULL,
  71. txn_id varchar(255) DEFAULT '',
  72. subscr_id varchar(255) DEFAULT '',
  73. company_name varchar(255) DEFAULT '',
  74. notes text DEFAULT NULL,
  75. flags int(11) DEFAULT '0',
  76. profile_image varchar(255) DEFAULT '',
  77. birth_place varchar(255) DEFAULT NULL,
  78. birth_date date NOT NULL DEFAULT '0000-00-00',
  79. )" . $charset_collate . ";";
  80. dbDelta($sql);
  81. $sql = "CREATE TABLE " . $wpdb->prefix . "swpm_membership_tbl (
  82. id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  83. alias varchar(127) NOT NULL,
  84. role varchar(255) NOT NULL DEFAULT 'subscriber',
  85. permissions tinyint(4) NOT NULL DEFAULT '0',
  86. subscription_period varchar(11) NOT NULL DEFAULT '-1',
  87. subscription_duration_type tinyint NOT NULL default 0,
  88. subscription_unit VARCHAR(20) NULL,
  89. loginredirect_page text NULL,
  90. category_list longtext,
  91. page_list longtext,
  92. post_list longtext,
  93. comment_list longtext,
  94. attachment_list longtext,
  95. custom_post_list longtext,
  96. disable_bookmark_list longtext,
  97. options longtext,
  98. protect_older_posts tinyint(1) NOT NULL DEFAULT '0',
  99. campaign_name varchar(255) NOT NULL DEFAULT ''
  100. )" . $charset_collate . " AUTO_INCREMENT=1 ;";
  101. dbDelta($sql);
  102. $sql = "SELECT * FROM " . $wpdb->prefix . "swpm_membership_tbl WHERE id = 1";
  103. $results = $wpdb->get_row($sql);
  104. if (is_null($results)) {
  105. $sql = "INSERT INTO " . $wpdb->prefix . "swpm_membership_tbl (
  106. id ,
  107. alias ,
  108. role ,
  109. permissions ,
  110. subscription_period ,
  111. subscription_unit,
  112. loginredirect_page,
  113. category_list ,
  114. page_list ,
  115. post_list ,
  116. comment_list,
  117. disable_bookmark_list,
  118. options,
  119. campaign_name
  120. )VALUES (1 , 'Content Protection', 'administrator', '15', '0',NULL,NULL, NULL , NULL , NULL , NULL,NULL,NULL,'');";
  121. $wpdb->query($sql);
  122. }
  123. $sql = "UPDATE " . $wpdb->prefix . "swpm_membership_tbl SET subscription_duration_type = 1 WHERE subscription_unit='days' AND subscription_duration_type = 0";
  124. $wpdb->query($sql);
  125. $sql = "UPDATE " . $wpdb->prefix . "swpm_membership_tbl SET subscription_duration_type = 2 WHERE subscription_unit='weeks' AND subscription_duration_type = 0";
  126. $wpdb->query($sql);
  127. $sql = "UPDATE " . $wpdb->prefix . "swpm_membership_tbl SET subscription_duration_type = 3 WHERE subscription_unit='months' AND subscription_duration_type = 0";
  128. $wpdb->query($sql);
  129. $sql = "UPDATE " . $wpdb->prefix . "swpm_membership_tbl SET subscription_duration_type = 4 WHERE subscription_unit='years' AND subscription_duration_type = 0";
  130. $wpdb->query($sql);
  131. $sql = "CREATE TABLE " . $wpdb->prefix . "swpm_membership_meta_tbl (
  132. id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  133. level_id int(11) NOT NULL,
  134. meta_key varchar(255) NOT NULL,
  135. meta_label varchar(255) NULL,
  136. meta_value text,
  137. meta_type varchar(255) NOT NULL DEFAULT 'text',
  138. meta_default text,
  139. meta_context varchar(255) NOT NULL DEFAULT 'default',
  140. KEY level_id (level_id)
  141. )" . $charset_collate . " AUTO_INCREMENT=1;";
  142. dbDelta($sql);
  143. $sql = "CREATE TABLE " . $wpdb->prefix . "swpm_payments_tbl (
  144. id int(12) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  145. email varchar(255) DEFAULT NULL,
  146. first_name varchar(64) DEFAULT '',
  147. last_name varchar(64) DEFAULT '',
  148. member_id varchar(16) DEFAULT '',
  149. membership_level varchar(64) DEFAULT '',
  150. txn_date date NOT NULL default '0000-00-00',
  151. txn_id varchar(255) NOT NULL default '',
  152. subscr_id varchar(255) NOT NULL default '',
  153. reference varchar(255) NOT NULL default '',
  154. payment_amount varchar(32) NOT NULL default '',
  155. gateway varchar(32) DEFAULT '',
  156. status varchar(255) DEFAULT '',
  157. ip_address varchar(128) default ''
  158. )" . $charset_collate . ";";
  159. dbDelta($sql);
  160. //Save the current DB version
  161. update_option("swpm_db_version", SIMPLE_WP_MEMBERSHIP_DB_VER);
  162. }
  163. public static function initdb() {
  164. $settings = SwpmSettings::get_instance();
  165. $installed_version = $settings->get_value('swpm-active-version');
  166. //Set other default settings values
  167. $reg_prompt_email_subject = "Complete your registration";
  168. $reg_prompt_email_body = "Dear {first_name} {last_name}" .
  169. "\n\nThank you for joining us!" .
  170. "\n\nPlease complete your registration by visiting the following link:" .
  171. "\n\n{reg_link}" .
  172. "\n\nThank You";
  173. $reg_email_subject = "Your registration is complete";
  174. $reg_email_body = "Dear {first_name} {last_name}\n\n" .
  175. "Your registration is now complete!\n\n" .
  176. "Registration details:\n" .
  177. "Username: {user_name}\n" .
  178. "Password: {password}\n\n" .
  179. "Please login to the member area at the following URL:\n\n" .
  180. "{login_link}\n\n" .
  181. "Thank You";
  182. $reg_email_subject_admin = "Notification of New Member Registration";
  183. $reg_email_body_admin = "A new member has completed the registration.\n\n" .
  184. "Username: {user_name}\n" .
  185. "Email: {email}\n\n" .
  186. "Please login to the admin dashboard to view details of this user.\n\n" .
  187. "You can customize this email message from the Email Settings menu of the plugin.\n\n" .
  188. "Thank You";
  189. $upgrade_email_subject = "Subject for email sent after account upgrade";
  190. $upgrade_email_body = "Dear {first_name} {last_name}" .
  191. "\n\nYour Account Has Been Upgraded." .
  192. "\n\nThank You";
  193. $reset_email_subject = get_bloginfo('name') . ": New Password";
  194. $reset_email_body = "Dear {first_name} {last_name}" .
  195. "\n\nHere is your new password:" .
  196. "\n\nUsername: {user_name}" .
  197. "\nPassword: {password}" .
  198. "\n\nYou can change the password from the edit profile section of the site (after you log into the site)" .
  199. "\n\nThank You";
  200. $status_change_email_subject = "Account Updated!";
  201. $status_change_email_body = "Dear {first_name} {last_name}," .
  202. "\n\nYour account status has been updated!" .
  203. " Please login to the member area at the following URL:" .
  204. "\n\n {login_link}" .
  205. "\n\nThank You";
  206. $bulk_activate_email_subject = "Account Activated!";
  207. $bulk_activate_email_body = "Hi," .
  208. "\n\nYour account has been activated!" .
  209. "\n\nYou can now login to the member area." .
  210. "\n\nThank You";
  211. $email_activation_mail_subject = "Action Required to Activate Your Account";
  212. $email_activation_mail_body = "Dear {first_name}" .
  213. "\n\nThank you for registering. To activate your account, please click on the following link (this will confirm your email address):" .
  214. "\n\n{activation_link}" .
  215. "\n\nThank You";
  216. $curr_email_act_mail_subj = $settings->get_value('email-activation-mail-subject', false);
  217. if ($curr_email_act_mail_subj === false) {
  218. $settings->set_value('email-activation-mail-subject', stripslashes($email_activation_mail_subject));
  219. }
  220. $curr_email_act_mail_body = $settings->get_value('email-activation-mail-body', false);
  221. if ($curr_email_act_mail_body === false) {
  222. $settings->set_value('email-activation-mail-body', stripslashes($email_activation_mail_body));
  223. }
  224. if (empty($installed_version)) {
  225. //Do fresh install tasks
  226. //Create the mandatory pages (if they are not there)
  227. SwpmMiscUtils::create_mandatory_wp_pages();
  228. //End of page creation
  229. $example_from_address = 'hello@' . SwpmMiscUtils::get_home_url_without_http_and_www();
  230. $senders_email_address = get_bloginfo('name') . " <" . $example_from_address . ">";
  231. $settings->set_value('reg-complete-mail-subject', stripslashes($reg_email_subject))
  232. ->set_value('reg-complete-mail-body', stripslashes($reg_email_body))
  233. ->set_value('reg-prompt-complete-mail-subject', stripslashes($reg_prompt_email_subject))
  234. ->set_value('reg-prompt-complete-mail-body', stripslashes($reg_prompt_email_body))
  235. ->set_value('upgrade-complete-mail-subject', stripslashes($upgrade_email_subject))
  236. ->set_value('upgrade-complete-mail-body', stripslashes($upgrade_email_body))
  237. ->set_value('reset-mail-subject', stripslashes($reset_email_subject))
  238. ->set_value('reset-mail-body', stripslashes($reset_email_body))
  239. ->set_value('account-change-email-subject', stripslashes($status_change_email_subject))
  240. ->set_value('account-change-email-body', stripslashes($status_change_email_body))
  241. ->set_value('email-from', $senders_email_address);
  242. $settings->set_value('reg-complete-mail-subject-admin', stripslashes($reg_email_subject_admin));
  243. $settings->set_value('reg-complete-mail-body-admin', stripslashes($reg_email_body_admin));
  244. $settings->set_value('bulk-activate-notify-mail-subject', stripslashes($bulk_activate_email_subject));
  245. $settings->set_value('bulk-activate-notify-mail-body', stripslashes($bulk_activate_email_body));
  246. }
  247. if (version_compare($installed_version, SIMPLE_WP_MEMBERSHIP_VER) == -1) {
  248. //Do upgrade tasks
  249. }
  250. $settings->set_value('swpm-active-version', SIMPLE_WP_MEMBERSHIP_VER)->save(); //save everything.
  251. //Generate and save a swpm private key for this site
  252. $unique_id = uniqid('', true);
  253. add_option('swpm_private_key_one', $unique_id);
  254. }
  255. }