MS_Model_Import_Membership

Imports data from WPMU DEV Membership plugin.

Defined (1)

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

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