s2class_upgrade

The Subscribe2 s2class upgrade class.

Defined (1)

The class is defined in the following location(s).

/classes/class-s2-upgrade.php  
  1. class s2class_upgrade { 
  2. function upgrade_core() { 
  3. // let's take the time to double check data for registered users 
  4. global $mysubscribe2; 
  5. if ( version_compare($mysubscribe2->wp_release, '3.5', '<') ) { 
  6. global $wpdb; 
  7. $users = $wpdb->get_col($wpdb->prepare("SELECT ID from $wpdb->users WHERE ID NOT IN (SELECT user_id FROM $wpdb->usermeta WHERE meta_key=%s)", $mysubscribe2->get_usermeta_keyname('s2_format'))); 
  8. if ( !empty($users) ) { 
  9. foreach ($users as $user_ID) { 
  10. $mysubscribe2->register($user_ID); 
  11. } else { 
  12. $args = array( 
  13. 'meta_query' => array( 
  14. array('key' => $mysubscribe2->get_usermeta_keyname('s2_format'), 'compare' => 'NOT EXISTS') 
  15. ); 
  16.  
  17. $user_query = new WP_User_Query( $args ); 
  18. $users = $user_query->get_results(); 
  19. if ( !empty($users) ) { 
  20. foreach ($users as $user) { 
  21. $mysubscribe2->register($user->ID); 
  22. // let's make sure that the 's2_authors' key exists on every site for all Registered Users too 
  23. $this->upgrade70(); 
  24. } // end upgrade_core() 
  25.  
  26. function upgrade23() { 
  27. global $mysubscribe2, $wpdb; 
  28.  
  29. // include upgrade-functions for maybe_add_column; 
  30. if ( !function_exists('maybe_add_column') ) { 
  31. require_once(ABSPATH . 'wp-admin/install-helper.php'); 
  32. $date = date('Y-m-d'); 
  33. maybe_add_column($mysubscribe2->public, 'date', "ALTER TABLE $mysubscribe2->public ADD date DATE DEFAULT '$date' NOT NULL AFTER active"); 
  34.  
  35. // update the options table to serialized format 
  36. $old_options = $wpdb->get_col("SELECT option_name from $wpdb->options where option_name LIKE 's2%' AND option_name != 's2_future_posts'"); 
  37.  
  38. if ( !empty($old_options) ) { 
  39. foreach ( $old_options as $option ) { 
  40. $value = get_option($option); 
  41. $option_array = substr($option, 3); 
  42. $mysubscribe2->subscribe2_options[$option_array] = $value; 
  43. delete_option($option); 
  44. } // end upgrade23() 
  45.  
  46. function upgrade51() { 
  47. global $mysubscribe2; 
  48.  
  49. // include upgrade-functions for maybe_add_column; 
  50. if ( !function_exists('maybe_add_column') ) { 
  51. require_once(ABSPATH . 'wp-admin/install-helper.php'); 
  52. maybe_add_column($mysubscribe2->public, 'ip', "ALTER TABLE $mysubscribe2->public ADD ip char(64) DEFAULT 'admin' NOT NULL AFTER date"); 
  53. } // end upgrade51() 
  54.  
  55. function upgrade56() { 
  56. global $mysubscribe2; 
  57. // correct autoformat to upgrade from pre 5.6 
  58. if ( $mysubscribe2->subscribe2_options['autoformat'] == 'text' ) { 
  59. $mysubscribe2->subscribe2_options['autoformat'] = 'excerpt'; 
  60. if ( $mysubscribe2->subscribe2_options['autoformat'] == 'full' ) { 
  61. $mysubscribe2->subscribe2_options['autoformat'] = 'post'; 
  62. } // end upgrade56() 
  63.  
  64. function upgrade59() { 
  65. global $mysubscribe2, $wpdb; 
  66. // ensure existing public subscriber emails are all sanitized 
  67. $confirmed = $mysubscribe2->get_public(); 
  68. $unconfirmed = $mysubscribe2->get_public(0); 
  69. $public_subscribers = array_merge((array)$confirmed, (array)$unconfirmed); 
  70.  
  71. foreach ( $public_subscribers as $email ) { 
  72. $new_email = $mysubscribe2->sanitize_email($email); 
  73. if ( $email !== $new_email ) { 
  74. $wpdb->get_results($wpdb->prepare("UPDATE $mysubscribe2->public SET email=%s WHERE CAST(email as binary)=%s", $new_email, $email)); 
  75. } // end upgrade59() 
  76.  
  77. function upgrade64() { 
  78. global $mysubscribe2; 
  79. // change old CAPITALISED keywords to those in {PARENTHESES}; since version 6.4 
  80. $keywords = array('BLOGNAME', 'BLOGLINK', 'TITLE', 'POST', 'POSTTIME', 'TABLE', 'TABLELINKS', 'PERMALINK', 'TINYLINK', 'DATE', 'TIME', 'MYNAME', 'EMAIL', 'AUTHORNAME', 'LINK', 'CATS', 'TAGS', 'COUNT', 'ACTION'); 
  81. $keyword = implode('|', $keywords); 
  82. $regex = '/(?<!\{)\b('.$keyword.')\b(?!\{)/xm'; 
  83. $replace = '{\1}'; 
  84. $mysubscribe2->subscribe2_options['mailtext'] = preg_replace($regex, $replace, $mysubscribe2->subscribe2_options['mailtext']); 
  85. $mysubscribe2->subscribe2_options['notification_subject'] = preg_replace($regex, $replace, $mysubscribe2->subscribe2_options['notification_subject']); 
  86. $mysubscribe2->subscribe2_options['confirm_email'] = preg_replace($regex, $replace, $mysubscribe2->subscribe2_options['confirm_email']); 
  87. $mysubscribe2->subscribe2_options['confirm_subject'] = preg_replace($regex, $replace, $mysubscribe2->subscribe2_options['confirm_subject']); 
  88. $mysubscribe2->subscribe2_options['remind_email'] = preg_replace($regex, $replace, $mysubscribe2->subscribe2_options['remind_email']); 
  89. $mysubscribe2->subscribe2_options['remind_subject'] = preg_replace($regex, $replace, $mysubscribe2->subscribe2_options['remind_subject']); 
  90.  
  91. if ( version_compare($mysubscribe2->wp_release, '3.5', '<') ) { 
  92. $users = $mysubscribe2->get_all_registered('ID'); 
  93. foreach ( $users as $user_ID ) { 
  94. $check_format = get_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_format'), true); 
  95. // if user is already registered update format remove 's2_excerpt' field and update 's2_format' 
  96. if ( 'html' == $check_format ) { 
  97. delete_user_meta($user_ID, 's2_excerpt'); 
  98. } elseif ( 'text' == $check_format ) { 
  99. update_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_format'), get_user_meta($user_ID, 's2_excerpt')); 
  100. delete_user_meta($user_ID, 's2_excerpt'); 
  101. $subscribed = get_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed'), true); 
  102. if ( strstr($subscribed, '-1') ) { 
  103. // make sure we remove '-1' from any settings 
  104. $old_cats = explode(', ', $subscribed); 
  105. $pos = array_search('-1', $old_cats); 
  106. unset($old_cats[$pos]); 
  107. $cats = implode(', ', $old_cats); 
  108. update_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed'), $cats); 
  109. } else { 
  110. $args = array( 
  111. 'relation' => 'AND',  
  112. 'meta_query' => array( 
  113. array('key' => $mysubscribe2->get_usermeta_keyname('s2_format'),  
  114. 'value' => 'html') 
  115. ),  
  116. 'meta_query' => array( 
  117. array('key' => 's2_excerpt',  
  118. 'compare' => 'EXISTS') 
  119. ); 
  120.  
  121. $user_query = new WP_User_Query( $args ); 
  122. $users = $user_query->get_results(); 
  123. if ( !empty($users) ) { 
  124. foreach ($users as $user) { 
  125. delete_user_meta($user->ID, 's2_excerpt'); 
  126.  
  127. $args = array( 
  128. 'relation' => 'AND',  
  129. 'meta_query' => array( 
  130. array('key' => $mysubscribe2->get_usermeta_keyname('s2_format'),  
  131. 'value' => 'text') 
  132. ),  
  133. 'meta_query' => array( 
  134. array('key' => 's2_excerpt',  
  135. 'compare' => 'EXISTS') 
  136. ); 
  137.  
  138. $user_query = new WP_User_Query( $args ); 
  139. $users = $user_query->get_results(); 
  140. if ( !empty($users) ) { 
  141. foreach ($users as $user) { 
  142. update_user_meta($user->ID, $mysubscribe2->get_usermeta_keyname('s2_format'), get_user_meta($user->ID, 's2_excerpt')); 
  143. delete_user_meta($user->ID, 's2_excerpt'); 
  144.  
  145. $args = array( 
  146. 'meta_query' => array( 
  147. array('key' => $mysubscribe2->get_usermeta_keyname('s2_subscribed'),  
  148. 'value' => '-1',  
  149. 'compare' => 'LIKE') 
  150. ); 
  151.  
  152. $user_query = new WP_User_Query( $args ); 
  153. $users = $user_query->get_results(); 
  154. if ( !empty($users) ) { 
  155. foreach ($users as $user) { 
  156. $subscribed = get_user_meta($user->ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed'), true); 
  157. $old_cats = explode(', ', $subscribed); 
  158. $pos = array_search('-1', $old_cats); 
  159. unset($old_cats[$pos]); 
  160. $cats = implode(', ', $old_cats); 
  161. update_user_meta($user->ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed'), $cats); 
  162.  
  163. // upgrade old wpmu user meta data to new 
  164. if ( $mysubscribe2->s2_mu === true ) { 
  165. global $s2class_multisite, $wpdb; 
  166. $s2class_multisite->namechange_subscribe2_widget(); 
  167. // loop through all users 
  168. foreach ( $users as $user_ID ) { 
  169. // get categories which the user is subscribed to (old ones) 
  170. $categories = get_user_meta($user_ID, 's2_subscribed', true); 
  171. $categories = explode(', ', $categories); 
  172. $format = get_user_meta($user_ID, 's2_format', true); 
  173. $autosub = get_user_meta($user_ID, 's2_autosub', true); 
  174.  
  175. // load blogs of user (only if we need them) 
  176. $blogs = array(); 
  177. if ( count($categories) > 0 && !in_array('-1', $categories) ) { 
  178. $blogs = get_blogs_of_user($user_ID, true); 
  179.  
  180. foreach ( $blogs as $blog ) { 
  181. switch_to_blog($blog->userblog_id); 
  182.  
  183. $blog_categories = (array)$wpdb->get_col("SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = 'category'"); 
  184. $subscribed_categories = array_intersect($categories, $blog_categories); 
  185. if ( !empty($subscribed_categories) ) { 
  186. foreach ( $subscribed_categories as $subscribed_category ) { 
  187. update_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_cat') . $subscribed_category, $subscribed_category); 
  188. update_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed'), implode(', ', $subscribed_categories)); 
  189. if ( !empty($format) ) { 
  190. update_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_format'), $format); 
  191. if ( !empty($autosub) ) { 
  192. update_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_autosub'), $autosub); 
  193. restore_current_blog(); 
  194.  
  195. // delete old user meta keys 
  196. delete_user_meta($user_ID, 's2_subscribed'); 
  197. delete_user_meta($user_ID, 's2_format'); 
  198. delete_user_meta($user_ID, 's2_autosub'); 
  199. foreach ( $categories as $cat ) { 
  200. delete_user_meta($user_ID, 's2_cat' . $cat); 
  201. } // end upgrade64() 
  202.  
  203. function upgrade70() { 
  204. global $mysubscribe2; 
  205. if ( version_compare($mysubscribe2->wp_release, '3.5', '<') ) { 
  206. $users = $wpdb->get_col($wpdb->prepare("SELECT ID from $wpdb->users WHERE ID NOT IN (SELECT user_id from $wpdb->usermeta WHERE meta_key=%s", $mysubscribe2->get_usermeta_keyname('s2_authors'))); 
  207. foreach ( $users as $user_ID ) { 
  208. $check_authors = get_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_authors'), true); 
  209. if ( empty($check_authors) ) { 
  210. update_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_authors'), ''); 
  211. } else { 
  212. $args = array( 
  213. 'meta_query' => array( 
  214. array('key' => $mysubscribe2->get_usermeta_keyname('s2_authors'), 'compare' => 'NOT EXISTS') 
  215. ); 
  216.  
  217. $user_query = new WP_User_Query( $args ); 
  218. $users = $user_query->get_results(); 
  219. if ( !empty($users) ) { 
  220. foreach ($users as $user) { 
  221. update_user_meta($user->ID, $mysubscribe2->get_usermeta_keyname('s2_authors'), ''); 
  222. } // end upgrade70() 
  223.  
  224. function upgrade85() { 
  225. global $mysubscribe2, $wpdb; 
  226.  
  227. // include upgrade-functions for maybe_add_column; 
  228. if ( !function_exists('maybe_add_column') ) { 
  229. require_once(ABSPATH . 'wp-admin/install-helper.php'); 
  230. maybe_add_column($mysubscribe2->public, 'time', "ALTER TABLE $mysubscribe2->public ADD time TIME DEFAULT '00:00:00' NOT NULL AFTER date"); 
  231.  
  232. // update postmeta field to a protected name, from version 8.5 
  233. $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_s2mail' WHERE meta_key = 's2mail'" ); 
  234. } // end upgrade85() 
  235.  
  236. function upgrade86() { 
  237. global $mysubscribe2, $wpdb; 
  238.  
  239. // include upgrade-functions for maybe_add_column; 
  240. if ( !function_exists('maybe_add_column') ) { 
  241. require_once(ABSPATH . 'wp-admin/install-helper.php'); 
  242. maybe_add_column($mysubscribe2->public, 'conf_date', "ALTER TABLE $mysubscribe2->public ADD conf_date DATE AFTER ip"); 
  243. maybe_add_column($mysubscribe2->public, 'conf_time', "ALTER TABLE $mysubscribe2->public ADD conf_time TIME AFTER conf_date"); 
  244. maybe_add_column($mysubscribe2->public, 'conf_ip', "ALTER TABLE $mysubscribe2->public ADD conf_ip char(64) AFTER conf_time"); 
  245.  
  246. // remove unnecessary table data 
  247. $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key = 's2_cat'" ); 
  248.  
  249. $sql = "SELECT ID FROM $wpdb->users INNER JOIN $wpdb->usermeta ON ( $wpdb->users.ID = $wpdb->usermeta.user_id) WHERE ( $wpdb->usermeta.meta_key = '" . $mysubscribe2->get_usermeta_keyname('s2_subscribed') . "' AND $wpdb->usermeta.meta_value LIKE ', %' )"; 
  250. $users = $wpdb->get_results($sql); 
  251. foreach ( $users as $user ) { 
  252. // make sure we remove leading ', ' from this setting 
  253. $subscribed = get_user_meta($user->ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed'), true); 
  254. $old_cats = explode(', ', $subscribed); 
  255. unset($old_cats[0]); 
  256. $cats = implode(', ', $old_cats); 
  257. update_user_meta($user->ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed'), $cats); 
  258. } // end upgrade86() 
  259.  
  260. function upgrade88() { 
  261. // to ensure compulsory category collects all users we need there to be s2_subscribed meta-keys for all users 
  262. global $mysubscribe2; 
  263.  
  264. if ( version_compare($mysubscribe2->wp_release, '3.5', '<') ) { 
  265. $all_registered = $mysubscribe2->get_all_registered('ID'); 
  266. if ( !empty($all_registered) ) { 
  267. foreach ( $all_registered as $user_ID ) { 
  268. $check_subscribed = get_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed'), true); 
  269. if ( empty($check_subscribed) ) { 
  270. update_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed'), ''); 
  271. } else { 
  272. $args = array( 
  273. 'meta_query' => array( 
  274. array('key' => $mysubscribe2->get_usermeta_keyname('s2_subscribed'), 'compare' => 'NOT EXISTS') 
  275. ); 
  276.  
  277. $user_query = new WP_User_Query( $args ); 
  278. $users = $user_query->get_results(); 
  279. if ( !empty($users) ) { 
  280. foreach ($users as $user) { 
  281. update_user_meta($user->ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed'), ''); 
  282.  
  283. // check the time column again as the upgrade86() function contained a bug 
  284. // include upgrade-functions for maybe_add_column; 
  285. if ( !function_exists('maybe_add_column') ) { 
  286. require_once(ABSPATH . 'wp-admin/install-helper.php'); 
  287. maybe_add_column($mysubscribe2->public, 'time', "ALTER TABLE $mysubscribe2->public ADD time TIME DEFAULT '00:00:00' NOT NULL AFTER date"); 
  288. } // end upgrade88() 
  289.  
  290. function upgrade100() { 
  291. global $mysubscribe2; 
  292. if ( $mysubscribe2->subscribe2_options['email_freq'] != 'never' ) { 
  293. $mysubscribe2->subscribe2_options['last_s2cron'] = ''; 
  294. unset($mysubscribe2->subscribe2_options['previous_s2cron']); 
  295. } // end upgrade100()