/app/model/import/class-ms-model-import-membership.php

  1. <?php 
  2. /** 
  3. * Imports data from WPMU DEV Membership plugin. 
  4. * 
  5. * @since 1.0.0 
  6. * @package Membership2 
  7. * @subpackage Model 
  8. */ 
  9. class MS_Model_Import_Membership extends MS_Model_Import { 
  10.  
  11. /** 
  12. * Identifier for this Import source 
  13. * 
  14. * @since 1.0.0 
  15. */ 
  16. const KEY = 'membership'; 
  17.  
  18. /** 
  19. * Stores the result of present() call 
  20. * 
  21. * @since 1.0.0 
  22. * 
  23. * @var bool 
  24. */ 
  25. static protected $is_present = null; 
  26.  
  27. /** 
  28. * The import data object 
  29. * 
  30. * @since 1.0.0 
  31. * 
  32. * @var array 
  33. */ 
  34. protected $data = array(); 
  35.  
  36. /** 
  37. * Checks if the user did import data from this source before. 
  38. * 
  39. * This information is not entirely reliable, since data could have been 
  40. * deleted again after import. 
  41. * 
  42. * @since 1.0.0 
  43. * @return bool 
  44. */ 
  45. static public function did_import() { 
  46. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  47. return ! empty( $settings->import[ self::KEY ] ); 
  48.  
  49. /** 
  50. * This function parses the Import source (i.e. an file-upload) and returns 
  51. * true in case the source data is valid. When returning true then the 
  52. * $source property of the model is set to the sanitized import source data. 
  53. * 
  54. * @since 1.0.0 
  55. * 
  56. * @return bool 
  57. */ 
  58. public function prepare() { 
  59. self::_message( 'preview', false ); 
  60.  
  61. $this->prepare_import_struct(); 
  62. $this->data = $this->validate_object( $this->data ); 
  63.  
  64. if ( empty( $this->data ) ) { 
  65. self::_message( 'error', __( 'Hmmm, we could not import the Membership data...', 'membership2' ) ); 
  66. return false; 
  67.  
  68. $this->source = $this->data; 
  69. return true; 
  70.  
  71. /** 
  72. * Returns true if the specific import-source is present and can be used 
  73. * for import. 
  74. * 
  75. * @since 1.0.0 
  76. * @return bool 
  77. */ 
  78. static public function present() { 
  79. if ( null === self::$is_present ) { 
  80. self::$is_present = false; 
  81.  
  82. // Check for one core table of the plugin. 
  83. global $wpdb; 
  84. $rule_table = $wpdb->prefix . 'm_membership_rules'; 
  85.  
  86. $sql = 'SHOW TABLES LIKE %s;'; 
  87. $sql = $wpdb->prepare( $sql, $rule_table ); 
  88. self::$is_present = $wpdb->get_var( $sql ) == $rule_table; 
  89.  
  90. return self::$is_present; 
  91.  
  92. /** 
  93. * Returns a valid import object that contains the Membership details 
  94. * 
  95. * @since 1.0.0 
  96. * @return object 
  97. */ 
  98. protected function prepare_import_struct() { 
  99. $this->data = (object) array(); 
  100.  
  101. $this->data->source_key = self::KEY; 
  102. $this->data->source = sprintf( 
  103. '%s (%s)',  
  104. 'Membership Premium',  
  105. 'WPMUDEV' 
  106. ); 
  107. $this->data->plugin_version = '3.5.x'; 
  108. $this->data->export_time = date( 'Y-m-d H:i' ); 
  109. $this->data->notes = array( 
  110. __( 'Exported data:', 'membership2' ),  
  111. __( '- Subscription Plans (without level rules)', 'membership2' ),  
  112. __( '- Members', 'membership2' ),  
  113. __( '- Registrations (link between Members and Subscription Plans)', 'membership2' ),  
  114. __( '- Transactions', 'membership2' ),  
  115. __( 'Each Subscription-Level is imported as a individual Membership.', 'membership2' ),  
  116. __( 'Transactions are converted to invoices. Data like tax-rate or applied coupons are not available.', 'membership2' ),  
  117. __( 'Please note that we cannot import recurring 2Checkout subscriptions to Membership2!', 'membership2' ),  
  118. ); 
  119.  
  120. $this->data->memberships = array(); 
  121. $this->data->members = array(); 
  122. $this->data->settings = array(); 
  123.  
  124. $this->add_memberships(); 
  125. $this->add_members(); 
  126.  
  127. return $this->data; 
  128.  
  129. /** 
  130. * Generates a list of all default membership objects that can be imported. 
  131. * The Membership2 base membership is not included! 
  132. * 
  133. * @since 1.0.0 
  134. */ 
  135. protected function add_memberships() { 
  136. global $wpdb; 
  137.  
  138. /** 
  139. * Notes: 
  140. * Child memberships are not possible 
  141. * Trial period is not possible 
  142. */ 
  143.  
  144. $sql = " 
  145. SELECT DISTINCT 
  146. subsc.id * 1000 + suble.level_id AS id,  
  147. TRIM( CONCAT( subsc.sub_name, ': ', memle.level_title ) ) AS `name`,  
  148. subsc.sub_description AS `description`,  
  149. 'simple' AS `type`,  
  150. subsc.sub_active AS `active`,  
  151. IF ( subsc.sub_public = 0, 1, 0 ) AS `private`,  
  152. IF ( suble.level_price = 0, 1, 0 ) AS `free`,  
  153. '' AS `dripped`,  
  154. '' AS `special`,  
  155. suble.level_price AS `price`,  
  156. 0 AS `trial`,  
  157. CASE suble.sub_type 
  158. WHEN 'indefinite' THEN 'permanent' 
  159. WHEN 'finite' THEN 'finite' 
  160. WHEN 'serial' THEN 'recurring' 
  161. ELSE 'permanent' 
  162. END AS `pay_type`,  
  163. suble.level_period AS `period_unit`,  
  164. suble.level_period_unit AS `period_type` 
  165. FROM `{$wpdb->prefix}m_subscriptions` subsc 
  166. INNER JOIN `{$wpdb->prefix}m_subscriptions_levels` suble ON suble.sub_id = subsc.id 
  167. INNER JOIN `{$wpdb->prefix}m_membership_levels` memle ON memle.id = suble.level_id 
  168. "; 
  169. $res = $wpdb->get_results( $sql ); 
  170.  
  171. foreach ( $res as $mem ) { 
  172. $this->data->memberships[] = $mem; 
  173.  
  174. /** 
  175. * Generates a list of all members that have a membership 
  176. * 
  177. * @since 1.0.0 
  178. */ 
  179. protected function add_members() { 
  180. global $wpdb; 
  181.  
  182. $sql = " 
  183. SELECT DISTINCT 
  184. wpuser.id AS `id`,  
  185. wpuser.user_email AS `email`,  
  186. wpuser.user_login AS `username` 
  187. FROM {$wpdb->prefix}m_membership_relationships member 
  188. INNER JOIN {$wpdb->users} wpuser ON wpuser.id = member.user_id 
  189. "; 
  190. $res = $wpdb->get_results( $sql ); 
  191.  
  192. foreach ( $res as $mem ) { 
  193. $this->data->members[$mem->id] = $mem; 
  194. $this->add_subscriptions( $mem->id ); 
  195.  
  196. /** 
  197. * Generates a list of subscriptions for the specified member 
  198. * 
  199. * @since 1.0.0 
  200. * @param int $member_id The member-ID 
  201. */ 
  202. protected function add_subscriptions( $member_id ) { 
  203. global $wpdb; 
  204.  
  205. $sql = " 
  206. SELECT DISTINCT 
  207. CONCAT( member.user_id * 10, member.sub_id * 1000 + member.level_id ) AS `id`,  
  208. member.sub_id AS `sub_id`,  
  209. member.level_id AS `level_id`,  
  210. member.sub_id * 1000 + member.level_id AS membership,  
  211. CASE 
  212. WHEN member.expirydate >= CURRENT_DATE() THEN 'active' 
  213. ELSE 'expired' 
  214. END AS `status`,  
  215. CASE member.usinggateway 
  216. WHEN 'paypalsolo' THEN 'paypalsingle' 
  217. WHEN 'paypalexpress' THEN 'paypalstandard' 
  218. WHEN 'twocheckout' THEN 'manual' 
  219. WHEN 'freesubscriptions' THEN 'free' 
  220. WHEN 'authorizenetarb' THEN 'authorize' 
  221. WHEN 'authorizenetaim' THEN 'authorize' 
  222. WHEN 'authorize' THEN 'authorize' 
  223. ELSE 'admin' 
  224. END AS `gateway`,  
  225. DATE_FORMAT( member.startdate, '%%Y-%%m-%%d' ) AS `start`,  
  226. DATE_FORMAT( member.expirydate, '%%Y-%%m-%%d' ) AS `end` 
  227. FROM {$wpdb->prefix}m_membership_relationships member 
  228. WHERE member.user_id = %s 
  229. "; 
  230. $sql = $wpdb->prepare( $sql, $member_id ); 
  231. $res = $wpdb->get_results( $sql ); 
  232.  
  233. $this->data->members[$member_id]->subscriptions = array(); 
  234. foreach ( $res as $sub ) { 
  235. $this->data->members[$member_id]->subscriptions[$sub->id] = $sub; 
  236. $this->add_invoices( $member_id, $sub->sub_id, $sub->id ); 
  237.  
  238. /** 
  239. * Generates a list of invoices for the specified subscription 
  240. * 
  241. * @since 1.0.0 
  242. * @param int $member_id The member-ID 
  243. * @param int $subscription The subscription plan-ID 
  244. * @param int $exp_id The export ID of the subscription object 
  245. */ 
  246. protected function add_invoices( $member_id, $subscription, $exp_id ) { 
  247. global $wpdb; 
  248.  
  249. $sql = " 
  250. SELECT DISTINCT 
  251. CONCAT( member.user_id * 10, member.sub_id * 1000 + member.level_id ) AS `subscription_id`,  
  252. inv.transaction_id AS `id`,  
  253. CONCAT( member.user_id, '-', LPAD( inv.transaction_id, 3, '0') ) AS `invoice_number`,  
  254. inv.transaction_paypal_ID AS `external_id`,  
  255. CASE inv.transaction_gateway 
  256. WHEN 'paypalsolo' THEN 'paypalsingle' 
  257. WHEN 'paypalexpress' THEN 'paypalstandard' 
  258. WHEN 'twocheckout' THEN 'manual' 
  259. WHEN 'freesubscriptions' THEN 'free' 
  260. WHEN 'authorizenetarb' THEN 'authorize' 
  261. WHEN 'authorizenetaim' THEN 'authorize' 
  262. WHEN 'authorize' THEN 'authorize' 
  263. ELSE 'admin' 
  264. END AS `gateway`,  
  265. CASE inv.transaction_status 
  266. WHEN 'Partially-Refunded' THEN 'failed' 
  267. WHEN 'Refunded' THEN 'failed' 
  268. WHEN 'Reversed' THEN 'failed' 
  269. WHEN 'Pending' THEN 'pending' 
  270. WHEN 'In-Progress' THEN 'pending' 
  271. WHEN 'Denied' THEN 'denied' 
  272. WHEN 'Completed' THEN 'paid' 
  273. WHEN 'Processed' THEN 'paid' 
  274. ELSE 'paid' 
  275. END AS `status`,  
  276. inv.transaction_currency AS `currency`,  
  277. inv.transaction_total_amount / 100 AS `amount`,  
  278. inv.transaction_total_amount / 100 AS `total`,  
  279. FROM_UNIXTIME( inv.transaction_stamp, '%%Y-%%m-%%d' ) AS `due`,  
  280. inv.transaction_note AS `notes` 
  281. FROM {$wpdb->prefix}m_membership_relationships member 
  282. INNER JOIN {$wpdb->prefix}m_subscription_transaction inv ON inv.transaction_user_id = member.user_id AND inv.transaction_subscription_ID = member.sub_id 
  283. WHERE 
  284. member.user_id = %s 
  285. AND member.sub_id = %s 
  286. "; 
  287. $sql = $wpdb->prepare( $sql, $member_id, $subscription ); 
  288. $res = $wpdb->get_results( $sql ); 
  289.  
  290. $this->data->members[$member_id]->subscriptions[$exp_id]->invoices = array(); 
  291. foreach ( $res as $inv ) { 
  292. $this->data->members[$member_id]->subscriptions[$exp_id]->invoices[] = $inv; 
  293.  
  294. /** 
  295. * Adds activation details for a single add-on to the import object 
  296. * 
  297. * @since 1.0.0 
  298. * @param string $name The add-on name 
  299. */ 
  300. protected function activate_addon( $name ) { 
  301. $this->data->settings['addons'] = lib3()->array->get( $this->data->settings['addons'] ); 
  302. $this->data->settings['addons'][$name] = true; 
  303.  
.