MS_Model_Import_Export

Class that handles Export functions.

Defined (1)

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

/app/model/import/class-ms-model-import-export.php  
  1. class MS_Model_Import_Export extends MS_Model { 
  2.  
  3. /** 
  4. * Identifier for this Import source 
  5. * @since 1.0.0 
  6. */ 
  7. const KEY = 'membership2'; 
  8.  
  9. /** 
  10. * Checks if the user did import data from this source before. 
  11. * This information is not entirely reliable, since data could have been 
  12. * deleted again after import. 
  13. * @since 1.0.0 
  14. * @return bool 
  15. */ 
  16. static public function did_import() { 
  17. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  18. return ! empty( $settings->import[ self::KEY ] ); 
  19.  
  20. /** 
  21. * Main entry point: Handles the export action. 
  22. * This task will exit the current request as the result will be a download 
  23. * and no HTML page that is displayed. 
  24. * @since 1.0.0 
  25. */ 
  26. public function process() { 
  27. $data = (object) array(); 
  28. $data->source_key = self::KEY; 
  29. $data->source = 'Membership2'; 
  30. $data->plugin_version = MS_PLUGIN_VERSION; 
  31. $data->export_time = date( 'Y-m-d H:i' ); 
  32. $data->notes = array( 
  33. __( 'Exported data:', 'membership2' ),  
  34. __( '- Memberships (without protection rules)', 'membership2' ),  
  35. __( '- Members (including Stripe/Authorize payment settings)', 'membership2' ),  
  36. __( '- Subscriptions (link between Members and Memberships)', 'membership2' ),  
  37. __( '- Invoices', 'membership2' ),  
  38. ); 
  39.  
  40. $data->memberships = array(); 
  41.  
  42. // Export the base membership (i.e. the Membership2 settings) 
  43. $membership = MS_Model_Membership::get_base(); 
  44. $data->memberships[] = $this->export_membership( $membership->id ); 
  45.  
  46. // Export all memberships. 
  47. $memberships = MS_Model_Membership::get_memberships( array( 'post_parent' => 0 ) ); 
  48. foreach ( $memberships as $membership ) { 
  49. $data->memberships[] = $this->export_membership( $membership->id ); 
  50.  
  51. // Export the members. 
  52. $members = MS_Model_Member::get_members(); 
  53. $data->members = array(); 
  54. foreach ( $members as $member ) { 
  55. if ( ! $member->is_member ) { continue; } 
  56. $data->members[] = $this->export_member( $member->id ); 
  57.  
  58. // Export plugin settings. 
  59. $obj = array(); 
  60. $data->settings = $this->export_settings(); 
  61.  
  62. // Export Coupons. 
  63. $data->coupons = array(); 
  64.  
  65. lib3()->net->file_download( json_encode( $data ), 'membership2-export.json' ); 
  66.  
  67. /** 
  68. * Export specific data. 
  69. * @since 1.0.0 
  70. * @param int $membership_id 
  71. * @return object Export data 
  72. */ 
  73. protected function export_membership( $membership_id ) { 
  74. $src = MS_Factory::load( 'MS_Model_Membership', $membership_id ); 
  75.  
  76. $obj = (object) array(); 
  77. $obj->id = $this->exp_id( 'membership', $src->id ); 
  78. $obj->name = $src->name; 
  79. $obj->description = $src->description; 
  80. $obj->type = $src->type; 
  81. $obj->active = (bool) $src->active; 
  82. $obj->private = (bool) $src->private; 
  83. $obj->free = (bool) $src->is_free; 
  84.  
  85. if ( ! $obj->free ) { 
  86. $obj->price = $src->price; 
  87. $obj->trial = (bool) $src->trial_period_enabled; 
  88.  
  89. switch ( $src->pay_type ) { 
  90. case MS_Model_Membership::PAYMENT_TYPE_FINITE: 
  91. $obj->pay_type = 'finite'; 
  92. $obj->period_unit = $src->period['period_unit']; 
  93. $obj->period_type = $src->period['period_type']; 
  94. break; 
  95.  
  96. case MS_Model_Membership::PAYMENT_TYPE_DATE_RANGE: 
  97. $obj->pay_type = 'date'; 
  98. $obj->period_start = $src->period_date_start; 
  99. $obj->period_end = $src->period_date_end; 
  100. break; 
  101.  
  102. case MS_Model_Membership::PAYMENT_TYPE_RECURRING: 
  103. $obj->pay_type = 'recurring'; 
  104. $obj->period_unit = $src->pay_cycle_period['period_unit']; 
  105. $obj->period_type = $src->pay_cycle_period['period_type']; 
  106. $obj->period_repetition = $src->pay_cycle_repetition; 
  107. break; 
  108.  
  109. default: 
  110. $obj->pay_type = 'permanent'; 
  111. break; 
  112.  
  113. if ( $obj->trial ) { 
  114. $obj->trial_price = $src->trial_price; 
  115. $obj->trial_period_unit = $src->trial_period['period_unit']; 
  116. $obj->trial_period_type = $src->trial_period['period_type']; 
  117.  
  118. return $obj; 
  119.  
  120. /** 
  121. * Export specific data. 
  122. * @since 1.0.0 
  123. * @param int $member_id 
  124. * @return object Export data 
  125. */ 
  126. protected function export_member( $member_id ) { 
  127. $src = MS_Factory::load( 'MS_Model_Member', $member_id ); 
  128.  
  129. $obj = (object) array(); 
  130. $obj->id = $this->exp_id( 'user', $src->username ); 
  131. $obj->email = $src->email; 
  132. $obj->username = $src->username; 
  133.  
  134. $gw_stripe = MS_Gateway_Stripe::ID; 
  135. $gw_auth = MS_Gateway_Authorize::ID; 
  136. $obj->payment = array( 
  137. // Stripe. 
  138. 'stripe_card_exp' => $src->get_gateway_profile( $gw_stripe, 'card_exp' ),  
  139. 'stripe_card_num' => $src->get_gateway_profile( $gw_stripe, 'card_num' ),  
  140. 'stripe_customer' => $src->get_gateway_profile( $gw_stripe, 'customer_id' ),  
  141.  
  142. // Authorize. 
  143. 'authorize_card_exp' => $src->get_gateway_profile( $gw_auth, 'card_exp' ),  
  144. 'authorize_card_num' => $src->get_gateway_profile( $gw_auth, 'card_num' ),  
  145. 'authorize_cim_profile' => $src->get_gateway_profile( $gw_auth, 'cim_profile_id' ),  
  146. 'authorize_cim_payment_profile' => $src->get_gateway_profile( $gw_auth, 'cim_payment_profile_id' ),  
  147. ); 
  148.  
  149. $obj->subscriptions = array(); 
  150. foreach ( $src->subscriptions as $registration ) { 
  151. $obj->subscriptions[] = $this->export_relationship( $registration ); 
  152.  
  153. return $obj; 
  154.  
  155. /** 
  156. * Export specific data. 
  157. * @since 1.0.0 
  158. * @param MS_Model_Relationship $src 
  159. * @return object Export data 
  160. */ 
  161. protected function export_relationship( $src ) { 
  162. $obj = (object) array(); 
  163. $obj->id = $this->exp_id( 'relationship', $src->id ); 
  164. $obj->membership = $this->exp_id( 'membership', $src->membership_id ); 
  165. $obj->status = $src->status; 
  166. $obj->gateway = $src->gateway_id; 
  167. $obj->start = $src->start_date; 
  168. $obj->end = $src->expire_date; 
  169.  
  170. $obj->trial_finished = $src->trial_period_completed; 
  171. if ( ! $obj->trial_finished ) { 
  172. $obj->trial_end = $src->trial_expire_date; 
  173.  
  174. $obj->invoices = array(); 
  175. $invoices = $src->get_invoices(); 
  176. foreach ( $invoices as $invoice ) { 
  177. $obj->invoices[] = $this->export_invoice( $invoice ); 
  178.  
  179. return $obj; 
  180.  
  181. /** 
  182. * Export specific data. 
  183. * @since 1.0.0 
  184. * @param MS_Model_Invoice $src 
  185. * @return object Export data 
  186. */ 
  187. protected function export_invoice( $src ) { 
  188. $obj = (object) array(); 
  189. $obj->id = $this->exp_id( 'invoice', $src->id ); 
  190. $obj->invoice_number = $src->invoice_number; 
  191. $obj->external_id = $src->external_id; 
  192. $obj->gateway = $src->gateway_id; 
  193. $obj->status = $src->status; 
  194.  
  195. $obj->coupon = $this->exp_id( 'coupon', $src->coupon_id ); 
  196. $obj->currency = $src->currency; 
  197. $obj->amount = $src->amount; 
  198. $obj->discount = $src->discount; 
  199. $obj->discount2 = $src->pro_rate; 
  200. $obj->total = $src->total; 
  201.  
  202. $obj->for_trial = (bool) $src->trial_period; 
  203. $obj->due = $src->due_date; 
  204. $obj->notes = $src->notes; 
  205.  
  206. return $obj; 
  207.  
  208. /** 
  209. * Export specific data. 
  210. * @since 1.0.0 
  211. * @param int $coupon_id 
  212. * @return object Export data 
  213. */ 
  214. protected function export_coupon( $coupon_id ) { 
  215. $obj = (object) array(); 
  216.  
  217. return $obj; 
  218.  
  219. /** 
  220. * Export specific data. 
  221. * @since 1.0.0 
  222. * @return object Export data 
  223. */ 
  224. protected function export_settings() { 
  225. $src = MS_Factory::load( 'MS_Model_settings' ); 
  226.  
  227. $obj = (object) array(); 
  228. $obj->enabled = $src->plugin_enabled; 
  229. $obj->hide_toolbar = $src->hide_admin_bar; 
  230. $obj->currency = $src->currency; 
  231. $obj->invoice_sender = $src->invoice_sender_name; 
  232.  
  233. return $obj; 
  234.  
  235. /** 
  236. * Returns a static export-ID for the given type. 
  237. * The export-ID will be same during this request but may change in the next 
  238. * export. This ID ensures that all links inside the export file are valid 
  239. * but that we do not use actual WordPress IDs. 
  240. * @since 1.0.0 
  241. * @param string $type Type 
  242. * @param int $internal_id WordPress ID 
  243. * @return int Export-ID 
  244. */ 
  245. protected function exp_id( $type, $internal_id ) { 
  246. static $Counter = 10000; 
  247. static $Ids = array(); 
  248.  
  249. $Ids[$type] = lib3()->array->get( $Ids[$type] ); 
  250. if ( ! isset( $Ids[$type][$internal_id] ) ) { 
  251. $Ids[$type][$internal_id] = $Counter; 
  252. $Counter += 1; 
  253.  
  254. return $Ids[$type][$internal_id]; 
  255.