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