WYSIJA_help_plugins_import

The MailPoet Newsletters WYSIJA help plugins import class.

Defined (1)

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

/helpers/plugins_import.php  
  1. class WYSIJA_help_plugins_import extends WYSIJA_object{ 
  2.  
  3. function __construct() { 
  4. parent::__construct(); 
  5.  
  6. //all the basic information concerning each plugin to make the match works 
  7. function getPluginsInfo($table=false) { 
  8. $pluginsTest=array( 
  9. 'newsletter'=>array( 
  10. "name"=>__("Newsletter Pro (by Satollo)", WYSIJA),  
  11. "pk"=>"id",  
  12. "matches"=>array("name"=>"firstname", "email"=>"email", "surname"=>"lastname", "ip"=>"ip"),  
  13. "where"=>array("status"=>"C") 
  14. ),  
  15. 'easymail_subscribers'=>array( 
  16. "name"=>"ALO EasyMail",  
  17. "pk"=>"id",  
  18. "matches"=>array("name"=>"firstname", "email"=>"email"),  
  19. "where"=>array("active"=>1),  
  20. "whereunconfirmed"=>array("active"=>0) 
  21. ),  
  22. 'meenewsusers'=>array( 
  23. "name"=>"Meenews",  
  24. "pk"=>"id",  
  25. "matches"=>array("name"=>"firstname", "email"=>"email"),  
  26. "where"=>array("state"=>2),  
  27. "whereunconfirmed"=>array("state"=>1),  
  28. 'list'=>array( 
  29. 'list'=> array( 
  30. 'table'=>'meenewscategories',  
  31. 'matches'=>array( 
  32. 'categoria'=>'name' 
  33. ),  
  34. 'pk'=>'id',  
  35. ),  
  36. 'user_list'=>array( 
  37. 'table'=>'meenewsusers',  
  38. 'matches'=>array( 
  39. 'id_categoria'=>'list_id',  
  40. 'subscriber_id'=>'id' 
  41. ),  
  42.  
  43. 'mailpress_users'=>array( 
  44. "name"=>"MailPress",  
  45. "pk"=>"id",  
  46. "matches"=>array("name"=>"firstname", "email"=>"email", "laststatus_IP"=>"ip"),  
  47. "where"=>array("status"=>"active"),  
  48. "whereunconfirmed"=>array("status"=>"waiting") 
  49. ),  
  50.  
  51. 'subscribe2'=>array( 
  52. "name"=>"Subscribe2",  
  53. "pk"=>"id",  
  54. "matches"=>array("email"=>"email", "ip"=>"ip"),  
  55. "where"=>array("active"=>1),  
  56. "whereunconfirmed"=>array("active"=>0) 
  57. ),  
  58.  
  59. 'eemail_newsletter_sub'=>array( 
  60. "name"=>"Email newsletter",  
  61. "pk"=>"eemail_id_sub",  
  62. "matches"=>array("eemail_email_sub"=>"email", "eemail_name_sub"=>"firstname"),  
  63. "where"=>array("eemail_status_sub"=>"YES") 
  64. ),  
  65. 'gsom_subscribers'=>array( 
  66. "name"=>"G-Lock Double Opt-in Manager",  
  67. "pk"=>"intId",  
  68. "matches"=>array("varEmail"=>"email", "gsom_fname_field"=>"firstname", "Last_Name1"=>"lastname", "varIP"=>"ip"),  
  69. "where"=>array("intStatus"=>1),  
  70. "whereunconfirmed"=>array("intStatus"=>0) 
  71. ),  
  72.  
  73. 'wpr_subscribers'=>array( 
  74. "name"=>"WP Autoresponder",  
  75. "pk"=>"id",  
  76. "matches"=>array("name"=>"firstname", "email"=>"email"),  
  77. "where"=>array("active"=>1, "confirmed"=>1),  
  78. "whereunconfirmed"=>array("active"=>1, "confirmed"=>0),  
  79. 'list'=>array( 
  80. 'list'=> array( 
  81. 'table'=>'wpr_newsletters',  
  82. 'matches'=>array( 
  83. 'name'=>'name' 
  84. ),  
  85. 'pk'=>'id',  
  86. ),  
  87. 'user_list'=>array( 
  88. 'table'=>'wpr_subscribers',  
  89. 'matches'=>array( 
  90. 'nid'=>'list_id',  
  91. 'id'=>'user_id' 
  92. ),  
  93.  
  94. 'wpmlsubscribers'=>array( 
  95. "name"=>__("Newsletters (by Tribulant)"),  
  96. "pk"=>"id",  
  97. "matches"=>array("email"=>"email", "ip_address"=>"ip"),  
  98. "where"=>array("wpmlsubscriberslists.active"=>"Y"),  
  99. 'list'=>array( 
  100. 'list'=> array( 
  101. 'table'=>'wpmlmailinglists',  
  102. 'matches'=>array( 
  103. 'title'=>'name' 
  104. ),  
  105. 'pk'=>'id',  
  106. ),  
  107. 'user_list'=>array( 
  108. 'table'=>'wpmlsubscriberslists',  
  109. 'matches'=>array( 
  110. 'list_id'=>'list_id',  
  111. 'subscriber_id'=>'user_id' 
  112. ),  
  113. 'nl_email'=>array( 
  114. "name"=>"Sendit",  
  115. "pk"=>"id_email",  
  116. "matches"=>array("email"=>"email", "contactname"=>"firstname"),  
  117. "where"=>array("accepted"=>"y"),  
  118. "whereunconfirmed"=>array("accepted"=>"n"),  
  119. 'list'=>array( 
  120. 'list'=> array( 
  121. 'table'=>'nl_liste',  
  122. 'matches'=>array( 
  123. 'nomelista'=>'name' 
  124. ),  
  125. 'pk'=>'id_lista',  
  126. ),  
  127. 'user_list'=>array( 
  128. 'table'=>'nl_email',  
  129. 'matches'=>array( 
  130. 'id_lista'=>'list_id',  
  131. 'id_email'=>'user_id' 
  132. ); 
  133.  
  134. if($table) { 
  135. if(!isset($pluginsTest[$table])) return false; 
  136. return $pluginsTest[$table]; 
  137. return $pluginsTest; 
  138.  
  139. function reverseMatches($matches) { 
  140. $matchesrev=array(); 
  141. foreach($matches as $key => $val) { 
  142. $matchesrev[$val]=$key; 
  143. return $matchesrev; 
  144.  
  145. /** 
  146. * this function is run only once at install to test which plugins are possible to import 
  147. * @global type $wpdb 
  148. */ 
  149. function testPlugins() { 
  150. $modelWysija=new WYSIJA_model(); 
  151. $possibleImport=array(); 
  152. foreach($this->getPluginsInfo() as $tableName =>$pluginInfos) { 
  153. /**if the plugin's subscribers table exists*/ 
  154. $result=$modelWysija->query("SHOW TABLES like '".$modelWysija->wpprefix.$tableName."';"); 
  155. if($result) { 
  156. /**select total of users*/ 
  157. $where=$this->generateWhere($pluginInfos['where']); 
  158. $result=$modelWysija->query("get_row", "SELECT COUNT(`".$pluginInfos['pk']."`) as total FROM `".$modelWysija->wpprefix.$tableName."` ".$where." ;", ARRAY_A); 
  159. $pluginInfosSave=array(); 
  160. if((int)$result['total']>0) { 
  161. /** there is a possible import to do */ 
  162. $pluginInfosSave['total']=(int)$result['total']; 
  163.  
  164. /**if the plugin's lists table exists*/ 
  165. if(isset($pluginInfos['list'])) { 
  166. $resultlist=$modelWysija->query("SHOW TABLES like '".$modelWysija->wpprefix.$pluginInfos['list']['list']['table']."';"); 
  167. if($resultlist) { 
  168. /**select total of users*/ 
  169. /**$where=$this->generateWhere($pluginInfos);*/ 
  170. $where=""; 
  171. $queryLists="SELECT COUNT(`".$pluginInfos['list']['list']['pk']."`) as total FROM `".$modelWysija->wpprefix.$pluginInfos['list']['list']['table']."` ".$where." ;"; 
  172. $resultlist=$modelWysija->query("get_row", $queryLists, ARRAY_A); 
  173.  
  174. if((int)$resultlist['total']>0) { 
  175. /** there is a possible import to do */ 
  176. $pluginInfosSave['total_lists']=(int)$resultlist['total']; 
  177.  
  178.  
  179.  
  180. if(!isset($pluginInfosSave['total_lists']))$pluginInfosSave['total_lists']=1; 
  181. $possibleImport[$tableName]=$pluginInfosSave; 
  182.  
  183.  
  184. /** if we found some plugins to import from we just save their details in the config */ 
  185. if($possibleImport) { 
  186. $modelConfig=WYSIJA::get("config", "model"); 
  187. $modelConfig->save(array("pluginsImportableEgg"=>$possibleImport)); 
  188.  
  189.  
  190.  
  191. function import($table_name, $connection_info, $is_synch_wp=false, $is_main_site=true, $is_synch=false) { 
  192. // careful WordPress global 
  193. global $wpdb; 
  194. // insert the list corresponding to that import 
  195. $model=WYSIJA::get('list', 'model'); 
  196.  
  197. // only we insert the default list the first time we pass here for real import 
  198. if(!$is_synch) { 
  199. if($is_synch_wp) $list_name=__('WordPress Users', WYSIJA); 
  200. else $list_name=sprintf(__('%1$s\'s import list', WYSIJA), $connection_info['name']); 
  201. $descriptionList=sprintf(__('The list created automatically on import of the plugin\'s subscribers : "%1$s', WYSIJA), $connection_info['name']); 
  202. // the list for all the users 
  203. $defaultListId=$model->insert(array( 
  204. 'name'=>$list_name,  
  205. 'description'=>$descriptionList,  
  206. 'is_enabled'=>0,  
  207. 'namekey'=>$table_name)); 
  208. }else $defaultListId=$is_synch['wysija_list_main_id']; 
  209.  
  210.  
  211. $mktime=time(); 
  212. $mktimeConfirmed=$mktime+1; 
  213.  
  214. if(strpos($table_name, 'query-')!==false) { 
  215. $lowertbname=str_replace('-', '_', $table_name); 
  216.  
  217. //TODO maybe we don't need to create a list that we sync but simply need a way to make a filter 
  218. $matches=apply_filters('wysija_fields_'.$lowertbname); 
  219. $query_select=apply_filters('wysija_select_'.$lowertbname); 
  220.  
  221. $query_select=str_replace('[created_at]', $mktime, $query_select); 
  222. $fields='(`'.implode('`, `', $matches).'`, `created_at` )'; 
  223. $query="INSERT IGNORE INTO `[wysija]user` $fields $query_select"; 
  224. }else{ 
  225. // automated part using the import array with the DB structure 
  226. // prepare the table transfer query 
  227. $colsPlugin=array_keys($connection_info['matches']); 
  228.  
  229. $extracols=$extravals=''; 
  230.  
  231. if(isset($connection_info['matchesvar'])) { 
  232. $extracols=', `'.implode('`, `', array_keys($connection_info['matchesvar'])).'`'; 
  233. $extravals=', '.implode(', ', $connection_info['matchesvar']); 
  234.  
  235. // import/synch the unconfirmed users from an extension 
  236. if(isset($connection_info['whereunconfirmed'])) { 
  237. $fields='(`'.implode('`, `', $connection_info['matches']).'`, `created_at` '.$extracols.' )'; 
  238. $values='`'.implode('`, `', $colsPlugin).'`, '.$mktime.$extravals; 
  239.  
  240. // query to save the external plugins unconfirmed subscribers into wysija subsribers 
  241. $where=$this->generateWhere($connection_info['whereunconfirmed']); 
  242.  
  243. $query="INSERT IGNORE INTO `[wysija]user` $fields SELECT $values FROM ".$model->wpprefix.$table_name.$where; 
  244. $model->query($query); 
  245.  
  246.  
  247. $fields='(`'.implode('`, `', $connection_info['matches']).'`, `created_at` '.$extracols.' )'; 
  248. $values='`'.implode('`, `', $colsPlugin).'`, '.$mktimeConfirmed.$extravals; 
  249.  
  250. // import/synch the confirmed users from wp or an extension 
  251. if($table_name == 'users') { 
  252. // query to save the wordpress users into wysija subsribers 
  253.  
  254. $query_select = "SELECT u1.ID, u1.user_email, m1.meta_value as first_name, m2.meta_value as last_name, ".$mktimeConfirmed.$extravals." 
  255. FROM ".$wpdb->base_prefix.$table_name." AS u1 
  256. JOIN ".$wpdb->base_prefix."usermeta AS m1 ON ( m1.user_id = u1.ID 
  257. AND m1.meta_key = 'first_name' ) 
  258. JOIN ".$wpdb->base_prefix."usermeta AS m2 ON ( m2.user_id = u1.ID 
  259. AND m2.meta_key = 'last_name' ) "; 
  260.  
  261. // in case of a multisite by default we want to import the wp users from the site on which we are running that script 
  262. if(!$is_main_site) { 
  263. $query_select.=' JOIN '.$wpdb->base_prefix.'usermeta as m3 ON ( u1.ID = m3.user_id AND m3.meta_key = \''.$model->wpprefix.'capabilities\' )'; 
  264.  
  265.  
  266.  
  267. $query="INSERT IGNORE INTO `[wysija]user` $fields ".$query_select; 
  268.  
  269. }else { 
  270. /** query to save the external plugins subscribers into wysija subsribers*/ 
  271. $where=$this->generateWhere($connection_info['where']); 
  272.  
  273. $query="INSERT IGNORE INTO `[wysija]user` $fields SELECT $values FROM ".$model->wpprefix.$table_name.$where; 
  274.  
  275.  
  276.  
  277. $model->query($query); 
  278.  
  279. //update status to subscribed for the confirmed users 
  280. $modelU=WYSIJA::get('user', 'model'); 
  281. $modelU->update(array('status'=>1), array('created_at'=>$mktimeConfirmed)); 
  282.  
  283.  
  284. /** query to save the fresshly inserted subscribers into wysija new imported list*/ 
  285. $this->insertUserList($defaultListId, $mktime, $mktimeConfirmed); 
  286.  
  287. $query="SELECT COUNT(user_id) as total FROM ".$model->getPrefix()."user WHERE created_at IN ('".$mktime."', '".$mktimeConfirmed."')"; 
  288. $result=$wpdb->get_row($query, ARRAY_A); 
  289.  
  290. /** insert all the lists from plugin if there are any table defined */ 
  291. if(isset($connection_info['list'])) { 
  292. $listmatchesrev=$this->reverseMatches($connection_info['list']['list']['matches']); 
  293. /**selec the lists*/ 
  294. $selectListsKeep="SELECT `".$listmatchesrev['name']."`, `".$connection_info['list']['list']['pk']."` FROM ".$model->wpprefix.$connection_info['list']['list']['table']; 
  295. $resultslists=$model->query("get_res", $selectListsKeep); 
  296.  
  297. if($resultslists) { 
  298. $userlistmatchesrev=$this->reverseMatches($connection_info['list']['user_list']['matches']); 
  299. foreach($resultslists as $listresult) { 
  300. /**insert the lists*/ 
  301. if(!$is_synch) { 
  302. $list_name=sprintf(__('"%2$s" imported from %1$s', WYSIJA), $connection_info["name"], $listresult[$listmatchesrev['name']]); 
  303. $descriptionList=sprintf(__('The list existed in "%1$s" and has been imported automatically.', WYSIJA), $connection_info["name"]); 
  304. $listidimported=$model->insert(array( 
  305. "name"=>$list_name,  
  306. "description"=>$descriptionList,  
  307. "is_enabled"=>0,  
  308. "namekey"=>$table_name."-listimported-".$listresult[$connection_info['list']['list']['pk']])); 
  309. }else { 
  310. $model->reset(); 
  311. $datalist=$model->getOne(false, array("namekey"=>$table_name."-listimported-".$listresult[$connection_info['list']['list']['pk']])); 
  312. $listidimported=$datalist['list_id']; 
  313.  
  314. /** insert the user lists */ 
  315. $query_select_join=' INNER JOIN '.$model->wpprefix.$table_name.' ON ( [wysija]user.email = '.$model->wpprefix.$table_name.'.'.$connection_info['matches']['email'].' )'; 
  316. if($connection_info['list']['user_list']['table']!=$table_name) $query_select_join.=' INNER JOIN '.$model->wpprefix.$connection_info['list']['user_list']['table'].' ON ( '.$model->wpprefix.$connection_info['list']['user_list']['table'].'.'.$userlistmatchesrev['user_id'].' = '.$model->wpprefix.$table_name.'.'.$connection_info['pk'].' )'; 
  317. $query_select_join.=" WHERE ".$model->wpprefix.$connection_info['list']['user_list']['table'].".".$userlistmatchesrev['list_id']."='".$listresult[$connection_info['list']['list']['pk']]."' "; 
  318.  
  319. $selectuserCreated="SELECT `[wysija]user`.`user_id`, ".$listidimported.", ".time()." FROM [wysija]user ".$query_select_join; 
  320. $query="INSERT IGNORE INTO `[wysija]user_list` (`user_id`, `list_id`, `sub_date`) ".$selectuserCreated; 
  321. $model->query($query); 
  322.  
  323. // since 2.6, we add a new column (`domain`), based on user email's address 
  324. $this->_generate_user_domain(); 
  325.  
  326. $helperU=WYSIJA::get('user', 'helper'); 
  327. $helperU->refreshUsers(); 
  328. if(!$is_synch) { 
  329. //$this->wp_notice(sprintf(_ds_fs('%1$s users from %2$s have been imported into the new list %3$s', WYSIJA), "<strong>".$result['total']."</strong>", "<strong>".$plugInfo['name']."</strong>", "<strong>".$listname."</strong>")); 
  330. }else{ 
  331. //$this->notice(__("List has been synched successfully.", WYSIJA)); 
  332.  
  333. return $defaultListId; 
  334.  
  335. /** 
  336. * since 2.6 
  337. * Generate domain of users based on their email address 
  338. */ 
  339. protected function _generate_user_domain() { 
  340. $query = "UPDATE [wysija]user SET `domain` = SUBSTRING(`email`, LOCATE('@', `email`)+1);"; 
  341. $user_model = WYSIJA::get('user', 'model'); 
  342. $user_model->query($query); 
  343.  
  344. function insertUserList($defaultListId, $mktime, $mktimeConfirmed, $querySelect=false) { 
  345. $model=WYSIJA::get('list', 'model'); 
  346. if(!$querySelect) $querySelect='SELECT `user_id`, '.$defaultListId.', '.time()." FROM [wysija]user WHERE created_at IN ('".$mktime."', '".$mktimeConfirmed."')"; 
  347. $query='INSERT IGNORE INTO `[wysija]user_list` (`user_id`, `list_id`, `sub_date`) '.$querySelect; 
  348. return $model->query($query); 
  349.  
  350. function generateWhere($plugInfoWhere) { 
  351. $where=' as B WHERE'; 
  352. $i=0; 
  353. foreach($plugInfoWhere as $keyy => $vale) { 
  354. if($i>0)$where.=' AND '; 
  355. if($keyy=='wpmlsubscriberslists.active') { 
  356. $model=WYSIJA::get('list', 'model'); 
  357. $innerjoin=' INNER JOIN '.$model->wpprefix.'wpmlsubscriberslists as B ON ( '.$model->wpprefix.'wpmlsubscribers.id = B.subscriber_id )'; 
  358. $where=$innerjoin." WHERE B.active='".$vale."' "; 
  359. }else{ 
  360. $where.=' B.'.$keyy."='".$vale."' "; 
  361. $i++; 
  362. return $where; 
  363.  
  364. function importWP() { 
  365.  
  366. // this parameter is needed on import to make sure that we don't import all of the network users in one random 
  367. $is_main_site = true; 
  368. if (is_multisite()) { 
  369. // Carefull WordPress global 
  370. global $wpdb; 
  371. if ($wpdb->prefix != $wpdb->base_prefix) { 
  372. $is_main_site = false; 
  373.  
  374. $connection_info = array( 
  375. 'name' => 'WordPress',  
  376. 'pk' => 'ID',  
  377. 'matches' => array( 
  378. 'ID' => 'wpuser_id',  
  379. 'user_email' => 'email',  
  380. 'first_name' => 'firstname',  
  381. 'last_name' => 'lastname'),  
  382. 'matchesvar' => array( 
  383. 'status' => 1) 
  384. ); 
  385.  
  386. $table_name = 'users'; 
  387.  
  388. return $this->import($table_name, $connection_info, true, $is_main_site); 
  389.  
  390.