WYSIJA_help_update

The MailPoet Newsletters WYSIJA help update class.

Defined (1)

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

/helpers/update.php  
  1. class WYSIJA_help_update extends WYSIJA_object { 
  2.  
  3. function __construct() { 
  4. $this->modelWysija = new WYSIJA_model(); 
  5.  
  6. //IMPORTANT when making db updated or running update processes, add the version and in the big switch below in the runUpdate() method 
  7. $this->updates = array( 
  8. '1.1',  
  9. '2.0', '2.1', '2.1.6', '2.1.7', '2.1.8',  
  10. '2.2', '2.2.1',  
  11. '2.3.3', '2.3.4',  
  12. '2.4', '2.4.1', '2.4.3', '2.4.4',  
  13. '2.5', '2.5.2', '2.5.5', '2.5.9.6', '2.5.9.7',  
  14. '2.6', '2.6.0.8', '2.6.15' 
  15. ); 
  16.  
  17. function runUpdate( $version ) { 
  18. //run all the updates missing since the db-version 
  19. //foreach ... $this->updateVersion($version); 
  20. switch($version) { 
  21. case '1.1': 
  22. //add column namekey to 
  23. $model_config=WYSIJA::get('config', 'model'); 
  24. if(!$this->modelWysija->query("SHOW COLUMNS FROM `[wysija]list` LIKE 'namekey';")) { 
  25. $querys[]='ALTER TABLE `[wysija]list` ADD `namekey` VARCHAR( 255 ) NULL;'; 
  26.  
  27. $querys[]="UPDATE `[wysija]list` SET `namekey` = 'users' WHERE `list_id` =".$model_config->getValue('importwp_list_id').";"; 
  28. $errors=$this->runUpdateQueries($querys); 
  29.  
  30. $importHelp=WYSIJA::get('plugins_import', 'helper'); 
  31. $importHelp->testPlugins(); 
  32.  
  33. // move data 
  34. $installHelper = WYSIJA::get('install', 'helper'); 
  35. $installHelper->moveData('dividers'); 
  36. $installHelper->moveData('bookmarks'); 
  37. $installHelper->moveData('themes'); 
  38.  
  39. if($errors) { 
  40. $this->error(implode($errors, "\n")); 
  41. return false; 
  42. return true; 
  43. break; 
  44. case '2.0': 
  45. //add column namekey to 
  46. $model_config=WYSIJA::get('config', 'model'); 
  47. if(!$this->modelWysija->query("SHOW COLUMNS FROM `[wysija]email` LIKE 'modified_at';")) { 
  48. $querys[]="ALTER TABLE `[wysija]email` ADD `modified_at` INT UNSIGNED NOT NULL DEFAULT '0';"; 
  49. if(!$model_config->getValue('update_error_20')) { 
  50. $querys[]="UPDATE `[wysija]email` SET `modified_at` = `sent_at` WHERE `sent_at`>=0;"; 
  51. $querys[]="UPDATE `[wysija]email` SET `modified_at` = `created_at` WHERE `modified_at`='0';"; 
  52. $querys[]="UPDATE `[wysija]email` SET `status` = '99' WHERE `status` ='1';";//change sending status from 1 to 99 
  53.  
  54.  
  55. $errors=$this->runUpdateQueries($querys); 
  56.  
  57. if($errors) { 
  58. $model_config->save(array('update_error_20'=>true)); 
  59. $this->error(implode($errors, "\n")); 
  60. return false; 
  61. return true; 
  62. break; 
  63. case '2.1': 
  64. $model_config=WYSIJA::get('config', 'model'); 
  65. if(!$model_config->getValue('update_error_21')) { 
  66. $modelEmails=WYSIJA::get('email', 'model'); 
  67. $modelEmails->reset(); 
  68. $emailsLoaded=$modelEmails->get(array('subject', 'email_id'), array('status'=>2, 'type'=>1)); 
  69.  
  70. //set the default new role caps for super admin and admin 
  71. $wp_tools_helper = WYSIJA::get('wp_tools', 'helper'); 
  72. $wp_tools_helper->set_default_rolecaps(); 
  73.  
  74. //based on the config values for role_campaign and role_subscribers 
  75. //let's give the each core roles the right capability 
  76.  
  77. $minimumroles=array('role_campaign'=>'wysija_newsletters', 'role_subscribers'=>'wysija_subscribers'); 
  78.  
  79. foreach($minimumroles as $rolename=>$capability) { 
  80. $rolesetting=$model_config->getValue($rolename); 
  81. switch($rolesetting) { 
  82. case 'switch_themes': 
  83. $keyrole=1; 
  84. break; 
  85. case 'moderate_comments': 
  86. $keyrole=3; 
  87. break; 
  88. case 'upload_files': 
  89. $keyrole=4; 
  90. break; 
  91. case 'edit_posts': 
  92. $keyrole=5; 
  93. break; 
  94. case 'read': 
  95. $keyrole=6; 
  96. break; 
  97. default: 
  98. $keyrole=false; 
  99.  
  100. if(!$keyrole) { 
  101. //add the setting to a custom role 
  102. $role = get_role($rolesetting); 
  103. //added for invalid roles ... 
  104. if($role) { 
  105. $role->add_cap( $capability ); 
  106. }else{ 
  107. //get all the minimum roles getting that capability 
  108. $editable_roles=$wp_tools_helper->wp_get_roles(); 
  109. $startcount=1; 
  110. if(!isset($editable_roles[$startcount])) $startcount++; 
  111. for($i = $startcount; $i <= $keyrole; $i++) { 
  112. $rolename=$editable_roles[$i]; 
  113. //add the setting to a custom role 
  114. $role = get_role($rolename['key']); 
  115. $role->add_cap( $capability ); 
  116. $helper_toolbox = WYSIJA::get('toolbox', 'helper'); 
  117. $model_config->save(array('dkim_domain'=>$helper_toolbox->_make_domain_name())); 
  118.  
  119. if(!$this->modelWysija->query("SHOW COLUMNS FROM `[wysija]list` LIKE 'is_public';")) { 
  120. $querys[]="ALTER TABLE `[wysija]list` ADD `is_public` TINYINT UNSIGNED NOT NULL DEFAULT 0;"; 
  121. $errors=$this->runUpdateQueries($querys); 
  122. if($errors) { 
  123. $model_config->save(array('update_error_21'=>true)); 
  124. $this->error(implode($errors, "\n")); 
  125. return false; 
  126. return true; 
  127. break; 
  128. case '2.1.6': 
  129. $querys[]="UPDATE `[wysija]user_list` as A inner join `[wysija]user` as B on (A.user_id= B.user_id) set A.sub_date= B.created_at where A.sub_date=0 and A.unsub_date=0 and B.status>-1;"; 
  130. $errors=$this->runUpdateQueries($querys); 
  131.  
  132. if($errors) { 
  133. $this->error(implode($errors, "\n")); 
  134. return false; 
  135. return true; 
  136. break; 
  137. case '2.1.7': 
  138. $querys[]='UPDATE `[wysija]user_list` as A inner join `[wysija]user` as B on (A.user_id= B.user_id) set A.sub_date= '.time().' where A.sub_date=0 and B.status>-1;'; 
  139. $errors=$this->runUpdateQueries($querys); 
  140.  
  141. if($errors) { 
  142. $this->error(implode($errors, "\n")); 
  143. return false; 
  144. return true; 
  145. break; 
  146. case '2.1.8': 
  147. $mConfig=WYSIJA::get('config', 'model'); 
  148.  
  149. $querys[]='UPDATE `[wysija]user_list` as A set A.sub_date= '.time().' where A.list_id='.$mConfig->getValue('importwp_list_id').';'; 
  150. $errors=$this->runUpdateQueries($querys); 
  151.  
  152. if($errors) { 
  153. $this->error(implode($errors, "\n")); 
  154. return false; 
  155. return true; 
  156. break; 
  157. case '2.2': 
  158. $mConfig=WYSIJA::get('config', 'model'); 
  159.  
  160. //let's rename the Synched WordPress list into WordPress Users 
  161. $mList=WYSIJA::get('list', 'model'); 
  162. $mList->update(array('name'=>'WordPress Users'), array('list_id'=>$mConfig->getValue('importwp_list_id'), 'namekey'=>'users')); 
  163.  
  164. //remove subscribers that should not be in the WordPress Users list 
  165. $querys[]='DELETE FROM `[wysija]user_list` WHERE `list_id` = '.$mConfig->getValue('importwp_list_id').' AND `user_id` in ( SELECT user_id FROM `[wysija]user` where wpuser_id=0 );'; 
  166. $errors=$this->runUpdateQueries($querys); 
  167.  
  168. if($errors) { 
  169. $this->error(implode($errors, "\n")); 
  170. return false; 
  171.  
  172. return true; 
  173. break; 
  174. case '2.2.1': 
  175. $helperU=WYSIJA::get('user', 'helper'); 
  176. $helperU->cleanWordpressUsersList(); 
  177.  
  178. return true; 
  179. break; 
  180. case '2.3.3': 
  181. update_option('wysija_log', ''); 
  182.  
  183. return true; 
  184. break; 
  185.  
  186.  
  187. case '2.3.4': 
  188. $model_config=WYSIJA::get('config', 'model'); 
  189.  
  190. $dbl_optin=(int)$model_config->getValue('confirm_dbleoptin'); 
  191. //count issue for people who went through all of the versions and now are left with some users in a limbo 
  192. $querys[]='UPDATE `[wysija]user_list` as A inner join `[wysija]user` as B on (A.user_id = B.user_id) set A.sub_date= '.time().' where A.sub_date=0 and A.unsub_date=0 and B.status>='.$dbl_optin.';'; 
  193. $errors=$this->runUpdateQueries($querys); 
  194.  
  195. if($errors) { 
  196. $this->error(implode($errors, "\n")); 
  197. return false; 
  198. return true; 
  199. break; 
  200.  
  201. case '2.4': 
  202. $queries = array(); 
  203. $queries[] = 'CREATE TABLE IF NOT EXISTS `[wysija]form` ('. 
  204. '`form_id` INT unsigned AUTO_INCREMENT NOT NULL, '. 
  205. '`name` tinytext COLLATE utf8_bin, '. 
  206. '`data` longtext COLLATE utf8_bin, '. 
  207. '`styles` longtext COLLATE utf8_bin, '. 
  208. '`subscribed` int(10) unsigned NOT NULL DEFAULT "0", '. 
  209. 'PRIMARY KEY (`form_id`)'. 
  210. ') /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci*/'; 
  211.  
  212. $errors = $this->runUpdateQueries($queries); 
  213.  
  214. if($errors) { 
  215. $this->error(implode($errors, "\n")); 
  216. return false; 
  217. } else { 
  218. // the table should be created now. let's make sure: 
  219. if((bool)$this->modelWysija->query('SHOW TABLES LIKE "[wysija]form";') === false) { 
  220. return false; 
  221. } else { 
  222. // the form table has been successfully created, let's convert all previously added widgets 
  223. $widgets_converted = $this->convert_widgets_to_forms(); 
  224. if($widgets_converted === 0) { 
  225. $helper_install = WYSIJA::get('install', 'helper'); 
  226. $helper_install->create_default_subscription_form(); 
  227.  
  228. return true; 
  229. break; 
  230.  
  231. case '2.4.1': 
  232. $model_email=WYSIJA::get('email', 'model'); 
  233. $model_email->setConditions(array('type'=>'2')); 
  234. $emails = $model_email->getRows(array('email_id', 'params')); 
  235.  
  236. // we don't want parent post notification to send emails 
  237. foreach($emails as $email) { 
  238. $model_email->getParams($email); 
  239. if(isset($email['params']) && $email['params']['autonl']['event']=='new-articles') { 
  240. $model_queue=WYSIJA::get('queue', 'model'); 
  241. $model_queue->delete(array('email_id'=>$email['email_id'])); 
  242. return true; 
  243.  
  244. break; 
  245.  
  246. case '2.4.3': 
  247. // convert data for previously saved forms 
  248. /** 
  249. * 1. get all forms and loop through each form 
  250. * 2. check data['settings']['success_message'] is base64 valid -> decode it 
  251. * 3. loop through data['body'], if type="text" -> is base64 valid -> decode it 
  252. * 4. save form 
  253. */ 
  254. $model_forms = WYSIJA::get('forms', 'model'); 
  255. $forms = $model_forms->getRows(); 
  256. if(is_array($forms) && count($forms) > 0) { 
  257.  
  258. foreach ($forms as $i => $form) { 
  259. $requires_update = false; 
  260.  
  261. // decode form data 
  262. $data = unserialize(base64_decode($form['data'])); 
  263.  
  264. // convert success_message if necessary 
  265. if(strlen($data['settings']['success_message']) % 4 === 0 && preg_match('%^[a-zA-Z0-9/+]*={0, 2}$%', $data['settings']['success_message'])) { 
  266. // this is a potential base64 string so decode it 
  267. $data['settings']['success_message'] = base64_decode($data['settings']['success_message']); 
  268.  
  269. $requires_update = true; 
  270.  
  271. // loop through each block 
  272. foreach ($data['body'] as $j => $block) { 
  273. // in case we find a text block 
  274. if($block['type'] === 'text') { 
  275. // convert text if necessary 
  276. if(strlen($block['params']['text']) % 4 === 0 && preg_match('%^[a-zA-Z0-9/+]*={0, 2}$%', $block['params']['text'])) { 
  277.  
  278. // this is a potential base64 string so decode it 
  279. $data['body'][$j]['params']['text'] = base64_decode($block['params']['text']); 
  280. $requires_update = true; 
  281.  
  282. // if the form requires update, let's do it 
  283. if($requires_update === true) { 
  284. $model_forms->reset(); 
  285. $model_forms->update(array('data' => base64_encode(serialize($data))), array('form_id' => (int)$form['form_id'])); 
  286.  
  287. return true; 
  288. break; 
  289. case '2.4.4': 
  290. // we now have steps in the installation process to make sure we don't rerun two processes 
  291. WYSIJA::update_option('installation_step', '16'); 
  292. return true; 
  293. break; 
  294.  
  295. case '2.5': 
  296. // set the default new role caps for super admin and admin 
  297. $wp_tools_helper = WYSIJA::get('wp_tools', 'helper'); 
  298. $wp_tools_helper->set_default_rolecaps(); 
  299.  
  300. // get the main site bounce settings and save them to the global ms settings 
  301. if(is_multisite()) { 
  302. $main_blog_id=1; 
  303. switch_to_blog( $main_blog_id ); 
  304. $main_site_encoded_option = unserialize(base64_decode(get_option( 'wysija' ))); 
  305. restore_current_blog(); 
  306.  
  307. $data_bounce=array(); 
  308. foreach($main_site_encoded_option as $key => $val) { 
  309. if((strpos($key, 'bounce_')!==false || strpos($key, 'bouncing_')!==false) && !empty($val)) $data_bounce['ms_'.$key]=$val; 
  310.  
  311. $data_saved_ms_before= unserialize(base64_decode(get_site_option('ms_wysija'))); 
  312.  
  313. // we don't want to run that multiple times 
  314. if(empty($data_saved_ms_before['ms_bounce_host'])) { 
  315. if(!empty($data_saved_ms_before)) $data_bounce=array_merge($data_saved_ms_before, $data_bounce); 
  316. update_site_option('ms_wysija', base64_encode(serialize($data_bounce))); 
  317.  
  318.  
  319. // Sending method: remove "once a day" & "twice daily", fallback on "every 2 hours" 
  320. $model_config = WYSIJA::get('config', 'model'); 
  321. $removed_sending_methods = array('twicedaily', 'daily'); 
  322. $target_sending_method = 'two_hours'; 
  323. if(in_array($model_config->getValue('sending_emails_each'), $removed_sending_methods)) 
  324. $model_config->save(array('sending_emails_each'=>$target_sending_method)); 
  325. if(in_array($model_config->getValue('ms_sending_emails_each'), $removed_sending_methods)) 
  326. $model_config->save(array('ms_sending_emails_each'=>$target_sending_method)); 
  327.  
  328. return true; 
  329. break; 
  330.  
  331. case '2.5.2': 
  332. $queries = array(); 
  333. $queries[] = 'UPDATE `[wysija]user_list` AS A JOIN `[wysija]user` AS B ON A.user_id = B.user_id SET A.unsub_date = 0, A.sub_date = '.time().' WHERE STATUS = 1 AND sub_date =0'; 
  334. $errors = $this->runUpdateQueries( $queries ); 
  335.  
  336. if ( $errors ) { 
  337. $this->error( implode( $errors, "\n" ) ); 
  338. return false; 
  339. return true; 
  340. break; 
  341.  
  342. case '2.5.5': 
  343. $model_email = WYSIJA::get( 'email', 'model', false, 'wysija-newsletters', false ); 
  344. $model_config = WYSIJA::get( 'config', 'model' ); 
  345.  
  346. $model_email->update( 
  347. array( 
  348. 'replyto_name' => $model_config->getValue( 'replyto_name' ),  
  349. 'replyto_email' => $model_config->getValue( 'replyto_email' ),  
  350. ),  
  351. array( 
  352. 'email_id' => $model_config->getValue( 'confirm_email_id' ),  
  353. ); 
  354. return true; 
  355. break; 
  356.  
  357. case '2.5.9.6': 
  358. $alter_queries = array(); 
  359. $alter_queries[] = 'ALTER TABLE [wysija]user ADD COLUMN `domain` VARCHAR(255);'; 
  360. $errors = $this->run_update_queries( $alter_queries ); 
  361.  
  362. if ( $this->does_column_exist( 'domain', '[wysija]user' ) ) { 
  363. $queries = array(); 
  364. $queries[] = "UPDATE [wysija]user SET `domain` = SUBSTRING(`email`, LOCATE('@', `email`)+1);"; 
  365. $errors = $this->run_update_queries( $queries ); 
  366. if ( $errors ) { 
  367. $this->error( implode( $errors, "\n" ) ); 
  368. return false; 
  369.  
  370. } else { 
  371. if(!empty($errors)) { 
  372. $this->error( implode( $errors, "\n" ) ); 
  373. return false; 
  374.  
  375. return true; 
  376. break; 
  377.  
  378. case '2.5.9.7': 
  379. $queries = array(); 
  380.  
  381. //add column namekey to 
  382. $model_config = WYSIJA::get( 'config', 'model' ); 
  383. $columns_to_add_to_user_table = array( 
  384. 'confirmed_ip' => 'VARCHAR(100) NOT NULL DEFAULT 0',  
  385. 'confirmed_at' => 'INT unsigned NULL',  
  386. 'last_opened' => 'INT unsigned NULL',  
  387. 'last_clicked' => 'INT unsigned NULL',  
  388. ); 
  389.  
  390. foreach ( $columns_to_add_to_user_table as $column_to_add => $sql_definition ) { 
  391. if ( ! $this->modelWysija->query( "SHOW COLUMNS FROM `[wysija]user` LIKE '{$column_to_add}';" ) ) { 
  392. $queries[] = "ALTER TABLE `[wysija]user` ADD `{$column_to_add}` {$sql_definition};"; 
  393.  
  394. // Custom Fields main table. 
  395. $queries[] = 'CREATE TABLE IF NOT EXISTS `[wysija]custom_field` ('. 
  396. '`id` mediumint(9) NOT NULL AUTO_INCREMENT, '. 
  397. '`name` tinytext NOT NULL, '. 
  398. '`type` tinytext NOT NULL, '. 
  399. '`required` tinyint(1) DEFAULT "0" NOT NULL, '. 
  400. '`settings` text DEFAULT NULL, '. 
  401. 'PRIMARY KEY (`id`)'. 
  402. ') /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci*/'; 
  403.  
  404. $errors = $this->runUpdateQueries( $queries ); 
  405.  
  406. if ( $errors ) { 
  407. $this->error( implode( $errors, "\n" ) ); 
  408. return false; 
  409. // update the new page_selection options with the default MailPoet's page 
  410. $default_page_id = $model_config->getValue( 'confirm_email_link' ); 
  411. $model_config->save( 
  412. array( 
  413. 'confirmation_page' => $default_page_id,  
  414. 'unsubscribe_page' => $default_page_id,  
  415. 'subscriptions_page' => $default_page_id,  
  416. ); 
  417.  
  418. return true; 
  419. break; 
  420.  
  421. case '2.6': 
  422. case '2.6.0.8': 
  423. $queries = array(); 
  424.  
  425. // Correct links which are missing 'h' 
  426. $queries[] = "UPDATE `[wysija]url` SET `url` = CONCAT('h', `url`) WHERE `url` LIKE 'ttp://%'"; 
  427. $errors = $this->run_update_queries( $queries ); 
  428.  
  429. if ( $errors ) { 
  430. $this->error( implode( $errors, "\n" ) ); 
  431. return false; 
  432.  
  433. return true; 
  434. break; 
  435.  
  436. case '2.6.15': 
  437. global $wpdb; 
  438. $sql = "SHOW INDEX FROM [wysija]user_list WHERE Key_name = 'user_id'"; 
  439. $result_index = $wpdb->get_results(str_replace('[wysija]', $this->modelWysija->getPrefix(), $sql)); 
  440.  
  441. $queries = array(); 
  442. //create an INDEX only if it doesn't exist already 
  443. if( empty( $result_index ) ) { 
  444. $queries[] = 'ALTER TABLE [wysija]user_list ADD INDEX `user_id` ( `user_id` ) '; 
  445. $errors = $this->run_update_queries( $queries ); 
  446.  
  447. if( !empty($errors) ) { 
  448. $this->error( implode( $errors, "\n" ) ); 
  449. return true; 
  450. break; 
  451.  
  452. default: 
  453. return false; 
  454. return false; 
  455.  
  456.  
  457. /** 
  458. * @return type 
  459. */ 
  460. function check() { 
  461.  
  462. //we can go there if this is not a new installation and it's an admin 
  463. if(WYSIJA::current_user_can('switch_themes') ) { 
  464.  
  465. // check that the redirection is only processed from wysija's interfaces 
  466. if(isset($_REQUEST['page']) && in_array($_REQUEST['page'], array('wysija_config', 'wysija_campaigns', 'wysija_subscribers'))) { 
  467.  
  468. // we are earlier than 1.1 or earlier than the current file version so we can run what's needed to reach the right version number 
  469. $config=WYSIJA::get('config', 'model'); 
  470.  
  471. if(!$config->getValue('wysija_db_version') || version_compare($config->getValue('wysija_db_version'), WYSIJA::get_version()) < 0) { 
  472. $this->update(WYSIJA::get_version()); 
  473. // once the update procedure is done we can redirect to the what's new/fixed page 
  474. $noredirect=false; 
  475.  
  476. //get the right option name based on the type of site we're in 
  477. $whats_new_option='wysija_whats_new'; 
  478. $is_multisite=is_multisite(); 
  479. $is_network_admin=WYSIJA::current_user_can('manage_network'); 
  480.  
  481. // check that in case of a multisite configuration only the network admin sees that update page 
  482. if($is_multisite) { 
  483. if($is_network_admin) { 
  484. $whats_new_option='ms_wysija_whats_new'; 
  485. }else { 
  486. return; 
  487.  
  488. // a whats_new option is set and it is less than the current version so that means we need to display it 
  489. if((!$config->getValue($whats_new_option) || version_compare($config->getValue($whats_new_option), WYSIJA::get_version()) < 0)) { 
  490.  
  491. //if there is an action set and it's one of those then we just don't redirect 
  492. if(isset($_REQUEST['action']) && in_array($_REQUEST['action'], array('whats_new', 'welcome_new', 'activate-plugin'))) $noredirect=true; 
  493.  
  494. if(!$noredirect) { 
  495. $timeInstalled=$config->getValue('installed_time')+3600; 
  496. //if it is a fresh install then it redirects to the welcome screen otherwise to the update one 
  497. if(time()>$timeInstalled) { 
  498. WYSIJA::redirect('admin.php?page=wysija_campaigns&action=whats_new'); 
  499. }else{ 
  500. WYSIJA::redirect('admin.php?page=wysija_campaigns&action=welcome_new'); 
  501.  
  502.  
  503.  
  504. /** 
  505. * this is a central function which will run every update until the current version 
  506. * @param string $version_current // not used since we use the wysija_db_version 
  507. * @return boolean 
  508. */ 
  509. function update($version_current) { 
  510. $config=WYSIJA::get('config', 'model'); 
  511. $config->getValue('wysija_db_version'); 
  512.  
  513. foreach($this->updates as $version) { 
  514. if(version_compare($config->getValue('wysija_db_version'), $version) < 0) { 
  515. if(!$this->runUpdate($version)) { 
  516. $this->error(sprintf(__('Update procedure to MailPoet version "%1$s" failed!', WYSIJA), $version), true); 
  517. return false; 
  518. }else{ 
  519. $config->save(array('wysija_db_version' => $version)); 
  520.  
  521.  
  522. /** 
  523. * return the failed queries 
  524. * @param type $queries 
  525. * @return type 
  526. * @deprecated since version 2.6 
  527. */ 
  528. function runUpdateQueries($queries) { 
  529. return $this->run_update_queries($queries); 
  530.  
  531. /** 
  532. * run a list of specified queries and return a list of failed queries 
  533. * @param array $queries 
  534. * <pre> 
  535. * array( 
  536. * 0 => full_query_1,  
  537. * 1 => full_query_2,  
  538. * ... 
  539. * n => full_query_n 
  540. * ) 
  541. * @return false or array 
  542. * <pre> 
  543. * array( 
  544. * 0 => full_query_1,  
  545. * 1 => full_query_2,  
  546. * ... 
  547. * n => full_query_n 
  548. * ) 
  549. */ 
  550. function run_update_queries($queries) { 
  551. $failed = array(); 
  552.  
  553. //we use mysql query instead of wordpress query to make sure we don't miss the sql errors 
  554. global $wpdb; 
  555. foreach($queries as $query) { 
  556. $query=str_replace('[wysija]', $this->modelWysija->getPrefix(), $query); 
  557. $last_error = $wpdb->last_error; 
  558. $wpdb->query($query); 
  559.  
  560. if( (empty($wpdb->result) || !$wpdb->result) && !empty( $wpdb->last_error ) && $last_error != $wpdb->last_error ) { 
  561. $failed[]= $wpdb->last_error." ($query)"; 
  562. if($failed) return $failed; 
  563. else return false; 
  564.  
  565. /** 
  566. * Detects if a column already exsits in a table 
  567. * @global wpdb $wpdb 
  568. * @param string $column_name name of the column 
  569. * @param string $table_name name of a table which the column should belong to 
  570. * @return boolean 
  571. */ 
  572. protected function does_column_exist($column_name, $table_name) { 
  573. global $wpdb; 
  574. $sql = "SHOW COLUMNS FROM `$table_name` LIKE '$column_name';"; 
  575.  
  576. $results = $wpdb->get_results(str_replace('[wysija]', $this->modelWysija->getPrefix(), $sql)); 
  577.  
  578. return (!empty($results)); 
  579.  
  580.  
  581. /** 
  582. * Version 2.4 introduces the Form Editor 
  583. * This function converts all previously saved subscription widgets into forms 
  584. */ 
  585. function convert_widget_to_form($values = array()) { 
  586. // $values needs to be an array 
  587. if(!is_array($values)) return false; 
  588.  
  589. // make sure we don't convert wysija forms into wysija forms.... that would be silly isn't it? 
  590. if(isset($values['form']) && (int)$values['form'] > 0) return false; 
  591.  
  592. $settings = $body = array(); 
  593.  
  594. // SETTINGS 
  595. // specify who selects the list (user | admin) 
  596. if($values['autoregister'] === 'not_auto_register') { 
  597. $settings['lists_selected_by'] = 'admin'; 
  598. } else { 
  599. // the user will select his own list so let's add the list selection 
  600. $settings['lists_selected_by'] = 'user'; 
  601.  
  602. // lists 
  603. $settings['lists'] = $values['lists']; 
  604.  
  605. // success message 
  606. $settings['on_success'] = 'message'; 
  607. $settings['success_message'] = $values['success']; 
  608.  
  609. // are the labels in or out? 
  610. if($values['labelswithin'] === 'labels_within') { 
  611. $label_within = true; 
  612. } else { 
  613. $label_within = false; 
  614.  
  615. // The order of the fields were: firstname, lastname, email, list_selection, submit 
  616. $blocks = array(); 
  617.  
  618. // INSTRUCTIONS 
  619. if(isset($values['instruction']) && strlen(trim($values['instruction'])) > 0) { 
  620. $blocks[] = array( 
  621. 'params' => array( 
  622. 'text' => base64_encode($values['instruction']),  
  623. ),  
  624. 'type' => 'text',  
  625. 'field' => 'text',  
  626. 'name' => __('Random text or instructions', WYSIJA) 
  627. ); 
  628.  
  629. // CUSTOM FIELDS (firstname, lastname, email) 
  630. $has_email_field = false; 
  631. foreach($values['customfields'] as $field => $params) { 
  632. switch($field) { 
  633. case 'firstname': 
  634. $name = __('First name', WYSIJA); 
  635. break; 
  636. case 'lastname': 
  637. $name = __('Last name', WYSIJA); 
  638. break; 
  639. case 'email': 
  640. $has_email_field = true; 
  641. $name = __('Email', WYSIJA); 
  642. break; 
  643.  
  644.  
  645. $blocks[] = array( 
  646. 'name' => $name,  
  647. 'type' => 'input',  
  648. 'field' => $field,  
  649. 'params' => array( 
  650. 'label' => $params['label'],  
  651. 'required' => 1,  
  652. 'label_within' => (int)$label_within 
  653. ); 
  654.  
  655. // make really sure we have an email field 
  656. if($has_email_field === false) { 
  657. $blocks[] = array( 
  658. 'name' => __('Email', WYSIJA),  
  659. 'type' => 'input',  
  660. 'field' => 'email',  
  661. 'params' => array( 
  662. 'label' => __('Email', WYSIJA),  
  663. 'required' => 1,  
  664. 'label_within' => (int)$label_within 
  665. ); 
  666.  
  667. // LIST SELECTION (only if the user can pick his own lists) 
  668. if($settings['lists_selected_by'] === 'user') { 
  669.  
  670. $list_values = array(); 
  671.  
  672. foreach($settings['lists'] as $list_id) { 
  673. $list_values[] = array( 
  674. 'list_id' => $list_id,  
  675. 'is_checked' => 1 
  676. ); 
  677. $blocks[] = array( 
  678. 'name' => __('List selection', WYSIJA),  
  679. 'type' => 'list',  
  680. 'field' => 'list',  
  681. 'params' => array( 
  682. 'label' => __('Select list(s):', WYSIJA),  
  683. 'values' => $list_values 
  684. ); 
  685.  
  686. // ADD SUBMIT BUTTON 
  687. $submit_label = __('Subscribe!', WYSIJA); 
  688. if(isset($values['submit']) && strlen(trim($values['submit'])) > 0) { 
  689. $submit_label = $values['submit']; 
  690. $blocks[] = array( 
  691. 'name' => __('Submit', WYSIJA),  
  692. 'type' => 'submit',  
  693. 'field' => 'submit',  
  694. 'params' => array( 
  695. 'label' => $submit_label 
  696. ); 
  697.  
  698. // Format body based on blocks 
  699. for($i = 0, $count = count($blocks); $i < $count; $i++) { 
  700. $body['block-'.($i + 1)] = array_merge($blocks[$i], array('position' => ($i + 1))); 
  701.  
  702. // set default form name 
  703. $form_name = __('New Form', WYSIJA); 
  704.  
  705. // check if title exists and override default form name 
  706. if(isset($values['title']) && strlen(trim($values['title'])) > 0) { 
  707. $form_name = $values['title']; 
  708.  
  709. // form engine helper 
  710. $helper_form_engine = WYSIJA::get('form_engine', 'helper'); 
  711.  
  712. // insert form into db 
  713. $model_forms = WYSIJA::get('forms', 'model'); 
  714. $model_forms->reset(); 
  715. // get form id back because it's required to generate the html form 
  716. $form_id = $model_forms->insert(array('name' => $form_name)); 
  717.  
  718. if((int)$form_id > 0) { 
  719. $model_forms->reset(); 
  720. // set form engine data 
  721. $helper_form_engine->set_data(array( 
  722. 'form_id' => (int)$form_id,  
  723. 'settings' => $settings,  
  724. 'body' => $body 
  725. )); 
  726.  
  727. // update form in database 
  728. $model_forms->update(array('data' => $helper_form_engine->get_encoded('data')), array('form_id' => $form_id)); 
  729.  
  730. return $form_id; 
  731. } else { 
  732. return false; 
  733.  
  734. /** 
  735. * very important function to convert subscription widget to our new forms in 2.4 
  736. * @return int 
  737. */ 
  738. function convert_widgets_to_forms() { 
  739. $widgets_converted = 0; 
  740.  
  741. // get all wysija widgets 
  742. $widgets = get_option('widget_wysija'); 
  743.  
  744. foreach($widgets as $key => &$values) { 
  745. $form_id = $this->convert_widget_to_form($values); 
  746. if($form_id!==false) { 
  747. $values['default_form'] = $form_id; 
  748. $widgets_converted++; 
  749.  
  750. update_option('widget_wysija', $widgets); 
  751. return $widgets_converted; 
  752.  
  753. /** 
  754. * Deprecated Methods 
  755. */ 
  756. function checkForNewVersion() { 
  757. /** 
  758. $current = get_site_transient( 'update_plugins' ); 
  759.   
  760. if ( !isset( $current->response[ $file ] ) ) 
  761. return false; 
  762.   
  763. $r = $current->response[ $file ]; 
  764. $default_headers = array( 
  765. 'Name' => 'Plugin Name',  
  766. 'PluginURI' => 'Plugin URI',  
  767. 'Version' => 'Version',  
  768. 'Description' => 'Description',  
  769. 'Author' => 'Author',  
  770. 'AuthorURI' => 'Author URI',  
  771. 'TextDomain' => 'Text Domain',  
  772. 'DomainPath' => 'Domain Path',  
  773. 'Network' => 'Network',  
  774. ); 
  775.   
  776. $plugin_data = get_file_data( WP_PLUGIN_DIR . DS.$file, $default_headers, 'plugin' ); 
  777.   
  778. $plugins_allowedtags = array('a' => array('href' => array(), 'title' => array()), 'abbr' => array('title' => array()), 'acronym' => array('title' => array()), 'code' => array(), 'em' => array(), 'strong' => array()); 
  779. $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags ); 
  780.   
  781. $details_url = self_admin_url((is_multisite()? 'network/' : '' ).'plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '§ion=changelog&TB_iframe=true&width=600&height=400'); 
  782.   
  783. if(((is_multisite() && current_user_can('manage_network') ) || current_user_can('update_plugins') ) && !empty($r->package) ) { 
  784. $this->notice( 
  785. sprintf( 
  786. __('Hey! %1$s has an update (version %4$s), <a href="%5$s">click here to update</a>.', WYSIJA) 
  787. , '<strong>'.$plugin_name.'</strong>',  
  788. esc_url($details_url),  
  789. esc_attr($plugin_name),  
  790. $r->new_version,  
  791. wp_nonce_url( self_admin_url('update.php?action=upgrade-plugin&plugin=') . $file, 'upgrade-plugin_' . $file) ), true, true); 
  792. */ 
  793.  
  794. /** 
  795. * in some cases scenario our update helper can't be run simply because a version is missing 
  796. */ 
  797. function repair_settings() { 
  798. static $is_repairing = FALSE; 
  799.  
  800. if($is_repairing === FALSE) { 
  801. $is_repairing = TRUE; 
  802.  
  803. // set installed as true 
  804. $values['installed'] = true; 
  805. // set installed_time: minus 7200 on it so that we don't display the welcome page again on the 
  806. // view condition in the check() function above WYSIJA::redirect('admin.php?page=wysija_campaigns&action=welcome_new'); 
  807. $values['installed_time'] = time() - 7200; 
  808.  
  809. // find our current db version 
  810. $values['wysija_db_version'] = $this->_find_db_version(); 
  811.  
  812. // save the missing settings to repair the installation 
  813. $model_config = WYSIJA::get('config', 'model'); 
  814. $model_config->save($values); 
  815.  
  816.  
  817.  
  818. /** 
  819. * find out what is the db version based on the existing columns of some tables 
  820. */ 
  821. private function _find_db_version() { 
  822. $model_wysija = new WYSIJA_model(); 
  823.  
  824. // test against 2.0 and set it to 1.1 if true 
  825. $test = $model_wysija->query('get_res', "SHOW COLUMNS FROM `[wysija]email` like 'modified_at';" ); 
  826. if(empty($test)) { 
  827. return '1.1'; 
  828. // test against 2.4 and set it to 2.3.4 if true 
  829. $test = $model_wysija->query('get_res', "SHOW COLUMNS FROM `[wysija]form`;" ); 
  830. if(empty($test)) { 
  831. return '2.3.4'; 
  832.  
  833. // test against 2.5.9.6 and set it to 2.5.5 if true 
  834. $test = $model_wysija->query('get_res', "SHOW COLUMNS FROM `[wysija]user` like 'domain';" ); 
  835. if(empty($test)) { 
  836. return '2.5.5'; 
  837. // test against 2.5.9.7 and set it to 2.5.9.6 if true 
  838. $test = $model_wysija->query('get_res', "SHOW COLUMNS FROM `[wysija]user` like 'last_opened';" ); 
  839. if(empty($test)) { 
  840. return '2.5.9.6'; 
  841.  
  842. return WYSIJA::get_version();