/blog/wp-content/plugins/thank-me-later/upgrade/Upgrade2_0_0_2.php

https://github.com/BranchesWebDesign/development · PHP · 191 lines · 147 code · 27 blank · 17 comment · 18 complexity · 547395a2e7701a33f34ba9355d2bb738 MD5 · raw file

  1. <?php
  2. /**
  3. * Upgrade to version 2.0.0.2 (also 2.0)
  4. */
  5. class Bbpp_ThankMeLater_Upgrade2_0_0_2 {
  6. public function ishere() {
  7. if (get_option("_tml2.0_installed_version") == "2.0") {
  8. return TRUE;
  9. }
  10. if (get_option("_tml2.0_installed_version") == "2.0.0.1") {
  11. return TRUE;
  12. }
  13. if (get_option("_tml2.0_installed_version") == "2.0.0.2") {
  14. return TRUE;
  15. }
  16. }
  17. /**
  18. * Upgrade from 1.5.3.1
  19. */
  20. public function up() {
  21. global $wpdb;
  22. // load 1.5.3.1 options
  23. $old_opts = get_option("_tml_options");
  24. if ($old_opts !== FALSE && !is_array($old_opts)) {
  25. $old_opts = unserialize($old_opts);
  26. }
  27. // reformat the restriction arrays
  28. $tag_slugs = $old_opts["restrict_tagsArr"];
  29. if (!is_array($tag_slugs)) {
  30. $tag_slugs = array();
  31. }
  32. $tag_slugs = array_flip($tag_slugs);
  33. $cat_slugs = $old_opts["restrict_categoriesArr"];
  34. if (!is_array($cat_slugs)) {
  35. $cat_slugs = array();
  36. }
  37. $cat_slugs = array_flip($cat_slugs);
  38. $opts = array(
  39. "max_messages" => $old_opts["maxSend"],
  40. "comment_gap" => $old_opts["sendGap"],
  41. "messages" => array(array(
  42. "from_name" => $old_opts["message_from"],
  43. "from_email" => $old_opts["message_fromemail"],
  44. "message_subject" => $old_opts["message_subject"],
  45. "use_html" => ($old_opts["html"] == "true") ? 1 : 0,
  46. "nl2br" => 0,
  47. "message_body" => $old_opts["message_message"],
  48. "send_after_use_default" => 0,
  49. "send_after" => $old_opts["sendafter"],
  50. "send_after_plus_minus" => $old_opts["giveortake"],
  51. "restrict_by_tags_use_default" => 0,
  52. "restrict_by_tags_type" => $old_opts["restrict_tagsType"],
  53. "restrict_by_tags_slugs" => $tag_slugs,
  54. "restrict_by_cats_use_default" => 0,
  55. "restrict_by_cats_type" => $old_opts["restrict_categoriesType"],
  56. "restrict_by_cats_slugs" => $cat_slugs,
  57. "restrict_by_users_use_default" => 1,
  58. "restrict_by_users" => 0,
  59. "restrict_by_users_type" => "logged-in",
  60. "prob" => 1,
  61. "uid" => 0
  62. ))
  63. );
  64. $message = $opts["messages"][0];
  65. foreach ($opts as $name => $val) {
  66. $autoload = "no";
  67. $opt_name = "_tml2.0_" . $name;
  68. $opt_val = (is_object($val) || is_array($val)) ? serialize($val) : $val;
  69. if (get_option($opt_name) !== false) {
  70. update_option($opt_name, $opt_val);
  71. } else {
  72. add_option($opt_name, $opt_val, "", $autoload);
  73. }
  74. }
  75. // remove the 1.5.3.1 options
  76. delete_option("_tml_options");
  77. delete_option("_tml_installed");
  78. delete_option("_tml_promote");
  79. // create the database tables
  80. $wpdb->query("CREATE TABLE `{$wpdb->prefix}tml_emails` (
  81. `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  82. `email` VARCHAR(255) NOT NULL UNIQUE,
  83. `subscribed` BOOLEAN,
  84. PRIMARY KEY (`ID`)
  85. )");
  86. $wpdb->query("CREATE TABLE `{$wpdb->prefix}tml_queue` (
  87. `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  88. `email_ID` INT UNSIGNED NOT NULL,
  89. `comment_ID` INT UNSIGNED NOT NULL UNIQUE,
  90. `send_time` INT UNSIGNED NOT NULL ,
  91. `message_uid` INT UNSIGNED NOT NULL,
  92. PRIMARY KEY ( `ID` ) ,
  93. INDEX ( `send_time` )
  94. )");
  95. $wpdb->query("CREATE TABLE `{$wpdb->prefix}tml_history` (
  96. `comment_ID` INT UNSIGNED NOT NULL,
  97. `email_ID` INT UNSIGNED NOT NULL,
  98. `time` INT UNSIGNED NOT NULL ,
  99. `send_time` INT UNSIGNED NOT NULL,
  100. `message_uid` INT UNSIGNED NOT NULL,
  101. `use_as_unique` BOOLEAN,
  102. PRIMARY KEY ( `comment_ID` ),
  103. INDEX ( `email_ID` )
  104. )");
  105. // copy data from 1.5.3.1 into 2.0
  106. $i = 0;
  107. $rpq = 100;
  108. while ($results_set = $wpdb->get_results("SELECT * FROM `{$wpdb->prefix}thankmelater_sent` LIMIT {$i}, {$rpq}")) {
  109. foreach ($results_set as $row) { // for each e-mail
  110. $wpdb->insert($wpdb->prefix . "tml_emails", array(
  111. "email" => $row->email,
  112. "subscribed" => true
  113. ), array("%s", "%b"));
  114. $email_id = $wpdb->insert_id;
  115. ### Messages in the queue ###
  116. $queued_items = $wpdb->get_results($wpdb->prepare("SELECT * FROM `{$wpdb->prefix}thankmelater` WHERE email_ID = %d", $row->id));
  117. foreach ($queued_items as $item) {
  118. $comment = get_comment($item->comment_ID, OBJECT); // comment object
  119. if ($comment->comment_type != "") { // is it /really/ a comment?
  120. return;
  121. }
  122. $status = wp_get_comment_status($item->comment_ID);
  123. if (in_array($status, array("hold", "spam", "delete"))) {
  124. continue;
  125. }
  126. $send_time = (int)(time() + $message["send_after"] - $message["send_after_plus_minus"] + mt_rand(0, $message["send_after_plus_minus"]*2));
  127. $wpdb->insert(
  128. $wpdb->prefix . "tml_queue",
  129. array(
  130. "email_ID" => $email_id,
  131. "comment_ID" => $item->comment_ID,
  132. "send_time" => $send_time,
  133. "message_uid" => $message["uid"]
  134. ),
  135. array("%d", "%d", "%d", "%d")
  136. );
  137. $wpdb->insert(
  138. $wpdb->prefix . "tml_history",
  139. array(
  140. "comment_ID" => $item->comment_ID,
  141. "email_ID" => $email_id,
  142. "time" => time(),
  143. "send_time" => $send_time,
  144. "message_uid" => $message["uid"],
  145. "use_as_unique"=> true,
  146. ),
  147. array("%d", "%d", "%d", "%d", "%d", "%b")
  148. );
  149. }
  150. }
  151. $i += $rpq;
  152. }
  153. // drop the 1.5.3 tables
  154. $wpdb->query("DROP TABLE IF EXISTS `{$wpdb->prefix}thankmelater`");
  155. $wpdb->query("DROP TABLE IF EXISTS `{$wpdb->prefix}thankmelater_sent`");
  156. // indicate we are at 2.0.0.2:
  157. update_option("_tml2.0_installed_version", "2.0.0.2");
  158. }
  159. /**
  160. * Downgrade to 1.4.1
  161. */
  162. public function down() {
  163. }
  164. }