MS_Controller_Communication

Controller for Automated Communications.

Defined (1)

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

/app/controller/class-ms-controller-communication.php  
  1. class MS_Controller_Communication extends MS_Controller { 
  2.  
  3. /** 
  4. * Ajax action name. 
  5. * @since 1.0.0 
  6. * @var string The ajax action name. 
  7. */ 
  8. const AJAX_ACTION_UPDATE_COMM = 'update_comm'; 
  9.  
  10. /** 
  11. * Save communication form. 
  12. * @since 1.0.1.0 
  13. * @var string 
  14. */ 
  15. const ACTION_SAVE_COMM = 'save_comm'; 
  16.  
  17. /** 
  18. * Prepare Membership settings manager. 
  19. * @since 1.0.0 
  20. */ 
  21. public function __construct() { 
  22. parent::__construct(); 
  23.  
  24. do_action( 'ms_controller_communication_before', $this ); 
  25.  
  26. $this->add_ajax_action( 
  27. self::AJAX_ACTION_UPDATE_COMM,  
  28. 'ajax_action_update_communication' 
  29. ); 
  30.  
  31. $this->add_action( 
  32. 'ms_controller_membership_setup_completed',  
  33. 'auto_setup_communications' 
  34. ); 
  35.  
  36. $this->add_action( 
  37. 'ms_model_event',  
  38. 'process_event',  
  39. 10, 2 
  40. ); 
  41.  
  42. $this->add_action( 
  43. 'ms_cron_process_communications',  
  44. 'process_queue' 
  45. ); 
  46.  
  47. do_action( 'ms_controller_communication_after', $this ); 
  48.  
  49. /** 
  50. * Initialize the admin-side functions. 
  51. * @since 1.0.0 
  52. */ 
  53. public function admin_init() { 
  54. $tab = ''; 
  55. if ( isset( $_GET['tab'] ) ) { 
  56. $tab = $_GET['tab']; 
  57.  
  58. /** 
  59. * Both in the Settings page and in the Membership Edit page the 
  60. * communication tab has the same name ('emails'), so we can use this 
  61. * info to better set-up our action hook. 
  62. */ 
  63. if ( MS_Controller_Settings::TAB_EMAILS == $tab ) { 
  64. $this->run_action( 
  65. 'init',  
  66. 'admin_manager' 
  67. ); 
  68.  
  69. // Add custom buttons to the MCE editor (insert variable). 
  70. $this->run_action( 
  71. 'admin_head',  
  72. 'add_mce_buttons' 
  73. ); 
  74.  
  75. /** 
  76. * Manages communication actions. 
  77. * Verifies GET and POST requests to manage settings. 
  78. * @since 1.0.1.0 
  79. */ 
  80. public function admin_manager() { 
  81. $msg = 0; 
  82. $redirect = false; 
  83.  
  84. if ( $this->is_admin_user() && $this->verify_nonce() ) { 
  85. /** 
  86. * After verifying permissions this filters can be used by Add-ons 
  87. * to process their own settings. 
  88. * @since 1.0.1.0 
  89. */ 
  90. do_action( 'ms_admin_communication_manager' ); 
  91.  
  92. $fields = array( 'type', 'subject', 'email_body' ); 
  93.  
  94. if ( self::validate_required( array( 'comm_type' ) ) 
  95. && MS_Model_Communication::is_valid_communication_type( $_POST['comm_type'] ) 
  96. ) { 
  97. // Load comm type from user select. 
  98. $redirect = esc_url_raw( 
  99. remove_query_arg( 
  100. 'msg',  
  101. add_query_arg( 'comm_type', $_POST['comm_type'] ) 
  102. ); 
  103. } elseif ( isset( $_POST['save_email'] ) 
  104. && self::validate_required( $fields ) 
  105. ) { 
  106. // Save email template form. 
  107. $default_type = MS_Model_Communication::COMM_TYPE_REGISTRATION; 
  108. if ( ! empty( $_REQUEST['membership_id'] ) ) { 
  109. $membership_id = intval( $_REQUEST['membership_id'] ); 
  110. $comm_types = array_keys( 
  111. MS_Model_Communication::get_communication_type_titles( 
  112. $membership_id 
  113. ); 
  114. $default_type = reset( $comm_types ); 
  115.  
  116. if ( ! empty( $_POST['type'] ) 
  117. && MS_Model_Communication::is_valid_communication_type( $_POST['type'] ) 
  118. ) { 
  119. $type = $_POST['type']; 
  120. } else { 
  121. $type = $default_type; 
  122.  
  123. $msg = $this->save_communication( $type, $_POST ); 
  124. $redirect = esc_url_raw( 
  125. add_query_arg( 
  126. array( 
  127. 'comm_type' => urlencode( $_POST['type'] ),  
  128. 'msg' => $msg,  
  129. ); 
  130.  
  131. if ( $redirect ) { 
  132. wp_safe_redirect( $redirect ); 
  133. exit(); 
  134.  
  135. /** 
  136. * Handles an event and process the correct communication if required. 
  137. * @since 1.0.1.0 
  138. * @param MS_Model_Event $event The event that is processed. 
  139. * @param mixed $data The data passed to the event handler. 
  140. */ 
  141. public function process_event( $event, $data ) { 
  142. if ( $data instanceof MS_Model_Relationship ) { 
  143. $subscription = $data; 
  144. $membership = $data->get_membership(); 
  145. } elseif ( $data instanceof MS_Model_Membership ) { 
  146. $subscription = false; 
  147. $membership = $data; 
  148. } else { 
  149. $subscription = false; 
  150. $membership = false; 
  151.  
  152. $enqueue = array(); 
  153. $process = array(); 
  154.  
  155. switch ( $event->type ) { 
  156. case MS_Model_Event::TYPE_MS_CANCELED: 
  157. $enqueue[] = MS_Model_Communication::COMM_TYPE_CANCELLED; 
  158. break; 
  159.  
  160. case MS_Model_Event::TYPE_CREDIT_CARD_EXPIRE: 
  161. $enqueue[] = MS_Model_Communication::COMM_TYPE_CREDIT_CARD_EXPIRE; 
  162. break; 
  163.  
  164. case MS_Model_Event::TYPE_PAYMENT_FAILED: 
  165. $enqueue[] = MS_Model_Communication::COMM_TYPE_FAILED_PAYMENT; 
  166. break; 
  167.  
  168. case MS_Model_Event::TYPE_MS_DEACTIVATED: 
  169. $enqueue[] = MS_Model_Communication::COMM_TYPE_FINISHED; 
  170. break; 
  171.  
  172. case MS_Model_Event::TYPE_UPDATED_INFO: 
  173. $enqueue[] = MS_Model_Communication::COMM_TYPE_INFO_UPDATE; 
  174. break; 
  175.  
  176. case MS_Model_Event::TYPE_PAID: 
  177. $enqueue[] = MS_Model_Communication::COMM_TYPE_INVOICE; 
  178. break; 
  179.  
  180. case MS_Model_Event::TYPE_MS_SIGNED_UP: 
  181. $process[] = MS_Model_Communication::COMM_TYPE_REGISTRATION_FREE; 
  182. $process[] = MS_Model_Communication::COMM_TYPE_REGISTRATION; 
  183. break; 
  184.  
  185. case MS_Model_Event::TYPE_MS_RENEWED: 
  186. $process[] = MS_Model_Communication::COMM_TYPE_RENEWED; 
  187. break; 
  188.  
  189. case MS_Model_Event::TYPE_MS_REGISTERED: 
  190. $process[] = MS_Model_Communication::COMM_TYPE_SIGNUP; 
  191. break; 
  192.  
  193. case MS_Model_Event::TYPE_MS_RESETPASSWORD: 
  194. $process[] = MS_Model_Communication::COMM_TYPE_RESETPASSWORD; 
  195. break; 
  196.  
  197. case MS_Model_Event::TYPE_MS_MOVED: 
  198. break; 
  199. case MS_Model_Event::TYPE_MS_EXPIRED: 
  200. break; 
  201. case MS_Model_Event::TYPE_MS_TRIAL_EXPIRED: 
  202. break; 
  203. case MS_Model_Event::TYPE_MS_DROPPED: 
  204. break; 
  205. case MS_Model_Event::TYPE_MS_BEFORE_FINISHES: 
  206. break; 
  207. case MS_Model_Event::TYPE_MS_AFTER_FINISHES: 
  208. break; 
  209. case MS_Model_Event::TYPE_MS_BEFORE_TRIAL_FINISHES: 
  210. break; 
  211. case MS_Model_Event::TYPE_MS_TRIAL_FINISHED: 
  212. break; 
  213. case MS_Model_Event::TYPE_PAYMENT_PENDING: 
  214. break; 
  215. case MS_Model_Event::TYPE_PAYMENT_DENIED: 
  216. break; 
  217. case MS_Model_Event::TYPE_PAYMENT_BEFORE_DUE: 
  218. break; 
  219. case MS_Model_Event::TYPE_PAYMENT_AFTER_DUE: 
  220. break; 
  221.  
  222. foreach ( $enqueue as $type ) { 
  223. $comm = MS_Model_Communication::get_communication( $type, $membership ); 
  224. if ( ! $comm ) { continue; } 
  225. $comm->enqueue_messages( $event, $data ); 
  226.  
  227. foreach ( $process as $type ) { 
  228. $comm = MS_Model_Communication::get_communication( $type, $membership ); 
  229. if ( ! $comm ) { continue; } 
  230. $comm->process_communication( $event, $data ); 
  231.  
  232. /** 
  233. * Send enqueued emails now. 
  234. * @since 1.0.1.0 
  235. * @internal Cron handler 
  236. * @see filter ms_cron_process_communications 
  237. */ 
  238. public function process_queue() { 
  239. $comms = MS_Model_Communication::get_communications( null ); 
  240.  
  241. foreach ( $comms as $comm ) { 
  242. $comm->process_queue(); 
  243.  
  244. /** 
  245. * Handle Ajax update comm field action. 
  246. * Related Action Hooks: 
  247. * - wp_ajax_update_comm 
  248. * @since 1.0.0 
  249. */ 
  250. public function ajax_action_update_communication() { 
  251. do_action( 
  252. 'ms_controller_communication_ajax_action_update_communication_before',  
  253. $this 
  254. ); 
  255.  
  256. $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED; 
  257.  
  258. $isset = array( 'type', 'field', 'value' ); 
  259. if ( $this->verify_nonce() 
  260. && self::validate_required( $isset, 'POST', false ) 
  261. && $this->is_admin_user() 
  262. ) { 
  263. lib3()->array->strip_slashes( $_POST, 'value' ); 
  264.  
  265. $membership_id = null; 
  266. if ( isset( $_POST['membership_id'] ) ) { 
  267. $membership_id = intval( $_POST['membership_id'] ); 
  268. $type = $_POST['type']; 
  269. $field = $_POST['field']; 
  270. $value = $_POST['value']; 
  271.  
  272. $comm = MS_Model_Communication::get_communication( 
  273. $type,  
  274. $membership_id,  
  275. true 
  276. ); 
  277.  
  278. $comm->$field = $value; 
  279. $comm->save(); 
  280. $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED; 
  281.  
  282. do_action( 
  283. 'ms_controller_communication_ajax_action_update_communication_after',  
  284. $this 
  285. ); 
  286.  
  287. echo apply_filters( 
  288. 'ms_controller_commnucation_ajax_action_update_communication_msg',  
  289. $msg,  
  290. $this 
  291. ); 
  292. exit; 
  293.  
  294. /** 
  295. * Auto setup communications. 
  296. * Fires after a membership setup is completed. 
  297. * Related Action Hooks: 
  298. * - ms_controller_membership_setup_completed 
  299. * @since 1.0.0 
  300. * @param MS_Model_Membership $membership 
  301. */ 
  302. public function auto_setup_communications( $membership ) { 
  303. /** 
  304. * Note: We intentionally set the parameter to 0. This 
  305. * function should set up default messages when first membership is 
  306. * created. It should not override default messages with membership- 
  307. * specific ones. 
  308. */ 
  309. $comms = MS_Model_Communication::get_communications( 0 ); 
  310.  
  311. foreach ( $comms as $comm ) { 
  312. $comm->enabled = true; 
  313. $comm->save(); 
  314.  
  315. do_action( 
  316. 'ms_controller_communication_auto_setup_communications_after',  
  317. $membership,  
  318. $this 
  319. ); 
  320.  
  321. /** 
  322. * Handle saving of Communication settings. 
  323. * @since 1.0.0 
  324. * @param mixed[] $fields The data to process. 
  325. */ 
  326. public function save_communication( $type, $fields ) { 
  327. $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED; 
  328.  
  329. if ( ! $this->is_admin_user() ) { 
  330. return $msg; 
  331.  
  332. $membership_id = null; 
  333. if ( isset( $_POST['membership_id'] ) ) { 
  334. $membership_id = intval( $_POST['membership_id'] ); 
  335.  
  336. $comm = MS_Model_Communication::get_communication( 
  337. $type,  
  338. $membership_id,  
  339. true 
  340. ); 
  341.  
  342. if ( ! empty( $fields ) ) { 
  343. lib3()->array->equip( 
  344. $fields,  
  345. 'enabled',  
  346. 'subject',  
  347. 'email_body',  
  348. 'period_unit',  
  349. 'period_type',  
  350. 'cc_enabled',  
  351. 'cc_email' 
  352. ); 
  353.  
  354. $comm->enabled = lib3()->is_true( $fields['enabled'] ); 
  355. $comm->subject = $fields['subject']; 
  356. $comm->message = $fields['email_body']; 
  357. $comm->period = array( 
  358. 'period_unit' => $fields['period_unit'],  
  359. 'period_type' => $fields['period_type'],  
  360. ); 
  361. $comm->cc_enabled = ! empty( $fields['cc_enabled'] ); 
  362. $comm->cc_email = $fields['cc_email']; 
  363.  
  364. $comm->save(); 
  365. $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED; 
  366.  
  367. return apply_filters( 
  368. 'ms_controller_communication_save',  
  369. $msg,  
  370. $type,  
  371. $fields,  
  372. $this 
  373. ); 
  374.  
  375. /** 
  376. * Prepare WordPress to add our custom TinyMCE button to the WYSIWYG editor. 
  377. * @since 1.0.1.0 
  378. * @see class-ms-view-settings-edit.php (function render_tab_messages_automated) 
  379. * @see ms-view-settings-automated-msg.js 
  380. */ 
  381. static public function add_mce_buttons() { 
  382. // Check user permissions. 
  383. if ( ! current_user_can( 'edit_posts' ) 
  384. && ! current_user_can( 'edit_pages' ) 
  385. ) { 
  386. return; 
  387.  
  388. // Check if WYSIWYG is enabled. 
  389. if ( 'true' != get_user_option( 'rich_editing' ) ) { 
  390. return; 
  391.  
  392. add_filter( 
  393. 'mce_external_plugins',  
  394. array( __CLASS__, 'add_variables_plugin' ) 
  395. ); 
  396. add_filter( 
  397. 'mce_buttons',  
  398. array( __CLASS__, 'register_variables_button' ) 
  399. ); 
  400.  
  401. /** 
  402. * Associate a javascript file with the new TinyMCE button. 
  403. * Hooks Filters: 
  404. * - mce_external_plugins 
  405. * @since 1.0.0 
  406. * @param array $plugin_array List of default TinyMCE plugin scripts. 
  407. * @return array Updated list of TinyMCE plugin scripts. 
  408. */ 
  409. public static function add_variables_plugin( $plugin_array ) { 
  410. $plugin_url = MS_Plugin::instance()->url; 
  411.  
  412. // This is a dummy reference (ms-admin.js is always loaded)! 
  413. // Actually this line would not be needed, but WordPress will not show 
  414. // our button when this is missing... 
  415. $plugin_array['ms_variable'] = $plugin_url . 'app/assets/js/ms-admin.js'; 
  416.  
  417. return $plugin_array; 
  418.  
  419. /** 
  420. * Register new "Insert variables" button in the editor. 
  421. * Hooks Filters: 
  422. * - mce_buttons 
  423. * @since 1.0.0 
  424. * @param array $buttons List of default TinyMCE buttons. 
  425. * @return array Updated list of TinyMCE buttons. 
  426. */ 
  427. public static function register_variables_button( $buttons ) { 
  428. array_push( $buttons, 'ms_variable' ); 
  429. return $buttons; 
  430.