MS_Controller_Import

Class that handles Import/Export functions.

Defined (1)

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

/app/controller/class-ms-controller-import.php  
  1. class MS_Controller_Import extends MS_Controller { 
  2.  
  3. // Action definitions. 
  4. const ACTION_EXPORT = 'export'; 
  5. const ACTION_PREVIEW = 'preview'; 
  6.  
  7. // Ajax action: Import data. 
  8. const AJAX_ACTION_IMPORT = 'ms_import'; 
  9.  
  10. // Ajax action: Save an automatic transaction matching (Billings page). 
  11. const AJAX_ACTION_MATCH = 'ms_save_matching'; 
  12.  
  13. // Ajax action: Retry to process a single transaction (Billings page). 
  14. const AJAX_ACTION_RETRY = 'transaction_retry'; 
  15.  
  16. /** 
  17. * Prepare the Import manager. 
  18. * @since 1.0.0 
  19. */ 
  20. public function __construct() { 
  21. parent::__construct(); 
  22.  
  23. $this->add_ajax_action( 
  24. self::AJAX_ACTION_IMPORT,  
  25. 'ajax_action_import' 
  26. ); 
  27.  
  28. $this->add_ajax_action( 
  29. self::AJAX_ACTION_MATCH,  
  30. 'ajax_action_match' 
  31. ); 
  32.  
  33. $this->add_ajax_action( 
  34. self::AJAX_ACTION_RETRY,  
  35. 'ajax_action_retry' 
  36. ); 
  37.  
  38. /** 
  39. * Initialize the admin-side functions. 
  40. * @since 1.0.0 
  41. */ 
  42. public function admin_init() { 
  43. $tab_key = 'import'; // should be unique plugin-wide value of `&tab=`. 
  44.  
  45. $this->run_action( 
  46. 'ms_controller_settings_enqueue_scripts_' . $tab_key,  
  47. 'enqueue_scripts' 
  48. ); 
  49.  
  50. /** 
  51. * Handles the matching of transaction details with a membership. 
  52. * Expected output: 
  53. * OK:Message to display 
  54. * ERR 
  55. * @since 1.0.1.2 
  56. */ 
  57. public function ajax_action_match() { 
  58. $res = 'ERR'; 
  59.  
  60. if ( ! $this->is_admin_user() ) { 
  61. return; 
  62.  
  63. $fields_match = array( 'match_with', 'source', 'source_id' ); 
  64.  
  65. // Save details of a single invoice. 
  66. if ( $this->verify_nonce() 
  67. && self::validate_required( $fields_match ) 
  68. ) { 
  69. $source = $_POST['source']; 
  70. $source_id = $_POST['source_id']; 
  71. $match_id = $_POST['match_with']; 
  72.  
  73. if ( MS_Model_Import::match_with_source( $match_id, $source_id, $source ) ) { 
  74. $res = 'OK:' . __( 'Matching details saved. Future transactions are automatically processed from now on!', 'membership2' ); 
  75.  
  76. echo $res; 
  77. exit; 
  78.  
  79. /** 
  80. * Retries to process a single error-state transaction. 
  81. * Expected output: 
  82. * OK 
  83. * ERR 
  84. * @since 1.0.1.2 
  85. */ 
  86. public function ajax_action_retry() { 
  87. $res = 'ERR'; 
  88.  
  89. if ( ! $this->is_admin_user() ) { 
  90. return; 
  91.  
  92. $fields_retry = array( 'id' ); 
  93.  
  94. // Save details of a single invoice. 
  95. if ( $this->verify_nonce() 
  96. && self::validate_required( $fields_retry ) 
  97. ) { 
  98. $log_id = intval( $_POST['id'] ); 
  99.  
  100. if ( MS_Model_Import::retry_to_process( $log_id ) ) { 
  101. $res = 'OK'; 
  102.  
  103. $log = MS_Factory::load( 'MS_Model_Transactionlog', $log_id ); 
  104. $res .= ':' . $log->description; 
  105.  
  106. echo $res; 
  107. exit; 
  108.  
  109. /** 
  110. * Handles an import batch that is sent via ajax. 
  111. * One batch includes multiple import commands that are to be processed in 
  112. * the specified order. 
  113. * Expected output: 
  114. * OK:<number of successful commands> 
  115. * ERR 
  116. * @since 1.0.0 
  117. */ 
  118. public function ajax_action_import() { 
  119. $res = 'ERR'; 
  120. $success = 0; 
  121.  
  122. if ( ! isset( $_POST['items'] ) || ! isset( $_POST['source'] ) ) { 
  123. echo $res; 
  124. exit; 
  125.  
  126. $batch = $_POST['items']; 
  127. $source = $_POST['source']; 
  128.  
  129. $res = 'OK'; 
  130. foreach ( $batch as $item ) { 
  131. if ( $this->process_item( $item, $source ) ) { 
  132. $success += 1; 
  133.  
  134. echo $res . ':' . $success; 
  135. exit; 
  136.  
  137. /** 
  138. * Processes a single import command. 
  139. * @since 1.0.0 
  140. * @param array $item The import command. 
  141. */ 
  142. protected function process_item( $item, $source ) { 
  143. $res = false; 
  144.  
  145. lib3()->array->equip( $item, 'task', 'data' ); 
  146. $task = $item['task']; 
  147. $data = $item['data']; 
  148. $model = MS_Factory::create( 'MS_Model_Import' ); 
  149. $model->source_key = $source; 
  150.  
  151. // Set MS_STOP_EMAILS modifier to suppress any outgoing emails. 
  152. MS_Plugin::set_modifier( 'MS_STOP_EMAILS', true ); 
  153.  
  154. // Possible tasks are defined in ms-view-settings-import.js 
  155. switch ( $task ) { 
  156. case 'start': 
  157. lib3()->array->equip( $item, 'clear' ); 
  158. $clear = lib3()->is_true( $item['clear'] ); 
  159. $model->start( $clear ); 
  160. $res = true; 
  161. break; 
  162.  
  163. case 'import-membership': 
  164. // Function expects an object, not an array! 
  165. $data = (object) $data; 
  166. $model->import_membership( $data ); 
  167. $res = true; 
  168. break; 
  169.  
  170. case 'import-member': 
  171. // Function expects an object, not an array! 
  172. $data = (object) $data; 
  173. $model->import_member( $data ); 
  174. $res = true; 
  175. break; 
  176.  
  177. case 'import-settings': 
  178. lib3()->array->equip( $item, 'setting', 'value' ); 
  179. $setting = $item['setting']; 
  180. $value = $item['value']; 
  181. $model->import_setting( $setting, $value ); 
  182. $res = true; 
  183. break; 
  184.  
  185. case 'done': 
  186. $model->done(); 
  187. $res = true; 
  188. break; 
  189.  
  190. /** 
  191. * After the import action was complated notify other objects and 
  192. * add-ons. 
  193. * @since 1.0.0 
  194. */ 
  195. do_action( 'ms_import_action_' . $task, $item ); 
  196.  
  197. return $res; 
  198.  
  199. /** 
  200. * Main entry point: Processes the import/export action. 
  201. * This function is called by the settings-controller whenever the 
  202. * Import/Export tab provides a correct nonce. We will first find out which 
  203. * action to execute and then handle all the details... 
  204. * @since 1.0.0 
  205. */ 
  206. public function process() { 
  207. lib3()->array->equip_post( 'action', 'import_source' ); 
  208. $action = $_POST['action']; 
  209.  
  210. if ( isset( $_POST['submit'] ) ) { 
  211. $action = $_POST['submit']; 
  212.  
  213. switch ( $action ) { 
  214. case self::ACTION_EXPORT: 
  215. $handler = MS_Factory::create( 'MS_Model_Import_Export' ); 
  216. $handler->process(); 
  217. break; 
  218.  
  219. case self::ACTION_PREVIEW: 
  220. $view = MS_Factory::create( 'MS_View_Settings_Import' ); 
  221. $model_name = 'MS_Model_Import_' . $_POST['import_source']; 
  222. $model = null; 
  223.  
  224. try { 
  225. $model = MS_Factory::create( $model_name ); 
  226. } catch( Exception $ex ) { 
  227. self::_message( 
  228. 'error',  
  229. __( 'Coming soon: This import source is not supported yet...', 'membership2' ) 
  230. ); 
  231.  
  232. if ( is_a( $model, 'MS_Model_Import' ) ) { 
  233. if ( $model->prepare() ) { 
  234. $data = array( 
  235. 'model' => $model,  
  236. ); 
  237.  
  238. $view->data = apply_filters( 
  239. 'ms_view_import_data',  
  240. $data 
  241. ); 
  242.  
  243. self::_message( 
  244. 'preview',  
  245. apply_filters( 
  246. 'ms_view_import_preview',  
  247. $view->to_html() 
  248. ); 
  249. break; 
  250.  
  251. /** 
  252. * Enqueue admin scripts in the settings screen. 
  253. * @since 1.0.0 
  254. */ 
  255. public function enqueue_scripts() { 
  256. $data = array( 
  257. 'ms_init' => array( 'view_settings_import' ),  
  258. 'lang' => array( 
  259. 'progress_title' => __( 'Importing data...', 'membership2' ),  
  260. 'close_progress' => __( 'Okay', 'membership2' ),  
  261. 'import_done' => __( 'All done!', 'membership2' ),  
  262. 'task_start' => __( 'Preparing...', 'membership2' ),  
  263. 'task_done' => __( 'Cleaning up...', 'membership2' ),  
  264. 'task_import_member' => __( 'Importing Member', 'membership2' ),  
  265. 'task_import_membership' => __( 'Importing Membership', 'membership2' ),  
  266. 'task_import_settings' => __( 'Importing Settings', 'membership2' ),  
  267. ),  
  268. ); 
  269.  
  270. lib3()->ui->data( 'ms_data', $data ); 
  271. wp_enqueue_script( 'ms-admin' ); 
  272.