/app/view/settings/page/class-ms-view-settings-page-communications.php

  1. <?php 
  2.  
  3. class MS_View_Settings_Page_Communications extends MS_View_Settings_Edit { 
  4.  
  5. /** 
  6. * Return the HTML form. 
  7. * 
  8. * @since 1.0.0 
  9. * @return string 
  10. */ 
  11. public function to_html() { 
  12. $comm = $this->data['comm']; 
  13. $fields = $this->get_fields(); 
  14.  
  15. $this->add_action( 'admin_footer', 'wp_footer' ); 
  16. $title = __( 'Automated Email Responses', 'membership2' ); 
  17.  
  18. if ( isset( $this->data['membership'] ) ) { 
  19. $membership = $this->data['membership']; 
  20. } else { 
  21. $membership = false; 
  22.  
  23. if ( $membership instanceof MS_Model_Membership ) { 
  24. $settings_url = MS_Controller_Plugin::get_admin_url( 
  25. 'settings',  
  26. array( 'tab' => MS_Controller_Settings::TAB_EMAILS ) 
  27. ); 
  28. $desc = sprintf( 
  29. __( 'Here you can override %sdefault messages%s for this membership.', 'membership2' ),  
  30. '<a href="' . $settings_url . '">',  
  31. '</a>' 
  32. ); 
  33. } else { 
  34. $desc = ''; 
  35.  
  36. ob_start(); 
  37.  
  38. MS_Helper_Html::settings_tab_header( 
  39. array( 'title' => $title, 'desc' => $desc ) 
  40. ); 
  41. ?> 
  42.  
  43. <form id="ms-comm-type-form" action="" method="post"> 
  44. <?php 
  45. MS_Helper_Html::html_element( $fields['load_action'] ); 
  46. MS_Helper_Html::html_element( $fields['load_nonce'] ); 
  47. MS_Helper_Html::html_element( $fields['comm_type'] ); 
  48. MS_Helper_Html::html_element( $fields['switch_comm_type'] ); 
  49. ?> 
  50. </form> 
  51.  
  52. <?php 
  53. MS_Helper_Html::html_separator(); 
  54. if ( ! empty( $fields['override'] ) ) { 
  55. MS_Helper_Html::html_element( $fields['override'] ); 
  56. ?> 
  57.  
  58. <form action="" method="post" class="ms-editor-form"> 
  59. <?php 
  60. if ( ! empty( $fields['membership_id'] ) ) { 
  61. MS_Helper_Html::html_separator(); 
  62. MS_Helper_Html::html_element( $fields['membership_id'] ); 
  63. MS_Helper_Html::html_element( $fields['action'] ); 
  64. MS_Helper_Html::html_element( $fields['nonce'] ); 
  65. MS_Helper_Html::html_element( $fields['type'] ); 
  66.  
  67. if ( is_a( $comm, 'MS_Model_Communication' ) ) { 
  68. printf( 
  69. '<h3>%1$s %2$s: %3$s</h3><div class="ms-description" style="margin-bottom:20px;">%4$s</div>',  
  70. $comm->get_title(),  
  71. __( 'Message', 'membership2' ),  
  72. MS_Helper_Html::html_element( $fields['enabled'], true ),  
  73. $comm->get_description() 
  74. ); 
  75.  
  76. if ( $comm->period_enabled ) { 
  77. echo '<div class="ms-period-wrapper clear">'; 
  78. $fields['period_unit'] = $comm->set_period_name( $fields['period_unit'] ); 
  79. MS_Helper_Html::html_element( $fields['period_unit'] ); 
  80. MS_Helper_Html::html_element( $fields['period_type'] ); 
  81. echo '</div>'; 
  82.  
  83. MS_Helper_Html::html_element( $fields['subject'] ); 
  84. MS_Helper_Html::html_element( $fields['email_body'] ); 
  85.  
  86. MS_Helper_Html::html_element( $fields['cc_enabled'] ); 
  87. echo '   '; 
  88. MS_Helper_Html::html_element( $fields['cc_email'] ); 
  89. MS_Helper_Html::html_separator(); 
  90. MS_Helper_Html::html_element( $fields['save_email'] ); 
  91. ?> 
  92. </form> 
  93. <?php 
  94.  
  95. return ob_get_clean(); 
  96.  
  97. /** 
  98. * Add short JS values in page footer. 
  99. * 
  100. * @since 1.0.0 
  101. */ 
  102. public function wp_footer() { 
  103. $comm = $this->data['comm']; 
  104. $vars = $comm->comm_vars; 
  105. $vars = lib3()->array->get( $vars ); 
  106.  
  107. /** 
  108. * Print JS details for the custom TinyMCE "Insert Variable" button 
  109. * 
  110. * @see class-ms-controller-settings.php (function add_mce_buttons) 
  111. * @see ms-view-settings-automated-msg.js 
  112. */ 
  113. $var_button = array( 
  114. 'title' => __( 'Insert Membership Variables', 'membership2' ),  
  115. 'items' => $vars,  
  116. ); 
  117.  
  118. printf( 
  119. '<script>window.ms_data.var_button = %1$s;window.ms_data.lang_confirm = %2$s</script>',  
  120. json_encode( $var_button ),  
  121. json_encode( 
  122. __( 'You have made changes that are not saved yet. Do you want to discard those changes?', 'membership2' ) 
  123. ); 
  124.  
  125. /** 
  126. * Prepare the fields that are displayed in the form. 
  127. * 
  128. * @since 1.0.1.0 
  129. * @return array 
  130. */ 
  131. protected function get_fields() { 
  132. $comm = $this->data['comm']; 
  133. $membership = false; 
  134. $membership_id = 0; 
  135.  
  136. if ( isset( $this->data['membership'] ) ) { 
  137. $membership = $this->data['membership']; 
  138.  
  139. if ( $membership instanceof MS_Model_Membership ) { 
  140. $membership_id = $membership->id; 
  141. } else { 
  142. $membership = false; 
  143.  
  144. lib3()->array->equip( 
  145. $comm,  
  146. 'type',  
  147. 'enabled',  
  148. 'period',  
  149. 'subject',  
  150. 'description',  
  151. 'cc_enabled',  
  152. 'cc_email' 
  153. ); 
  154.  
  155. $action = MS_Controller_Communication::AJAX_ACTION_UPDATE_COMM; 
  156. $nonce = wp_create_nonce( $action ); 
  157. $comm_titles = MS_Model_Communication::get_communication_type_titles( 
  158. $membership 
  159. ); 
  160.  
  161. $key_active = __( 'Send Email', 'membership2' ); 
  162. $key_inactive = __( 'No Email', 'membership2' ); 
  163. $key_skip = __( 'Use default template', 'membership2' ); 
  164. $titles = array( 
  165. $key_active => array(),  
  166. $key_inactive => array(),  
  167. $key_skip => array(),  
  168. ); 
  169. foreach ( $comm_titles as $type => $title ) { 
  170. $tmp_comm = MS_Model_Communication::get_communication( 
  171. $type,  
  172. $membership,  
  173. true 
  174. ); 
  175.  
  176. if ( $membership && ! $tmp_comm->override ) { 
  177. $titles[$key_skip][$type] = $title; 
  178. } elseif ( $tmp_comm->enabled ) { 
  179. $titles[$key_active][$type] = $title; 
  180. } else { 
  181. $titles[$key_inactive][$type] = $title; 
  182.  
  183. $fields = array( 
  184. 'comm_type' => array( 
  185. 'id' => 'comm_type',  
  186. 'type' => MS_Helper_Html::INPUT_TYPE_SELECT,  
  187. 'value' => $comm->type,  
  188. 'field_options' => $titles,  
  189. ),  
  190.  
  191. 'switch_comm_type' => array( 
  192. 'id' => 'switch_comm_type',  
  193. 'type' => MS_Helper_Html::INPUT_TYPE_BUTTON,  
  194. 'value' => __( 'Load Template', 'membership2' ),  
  195. ),  
  196.  
  197. 'override' => array( 
  198. 'id' => 'override',  
  199. 'type' => MS_Helper_Html::INPUT_TYPE_RADIO_SLIDER,  
  200. 'value' => $comm->override,  
  201. 'before' => __( 'Use default template', 'membership2' ),  
  202. 'after' => __( 'Define custom template', 'membership2' ),  
  203. 'wrapper_class' => 'ms-block ms-tcenter',  
  204. 'class' => 'override-slider',  
  205. 'ajax_data' => array( 
  206. 'type' => $comm->type,  
  207. 'field' => 'override',  
  208. 'action' => $action,  
  209. '_wpnonce' => $nonce,  
  210. 'membership_id' => $membership_id,  
  211. ),  
  212. ),  
  213.  
  214. 'membership_id' => array( 
  215. 'id' => 'membership_id',  
  216. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  217. 'value' => $membership_id,  
  218. ),  
  219.  
  220. 'type' => array( 
  221. 'id' => 'type',  
  222. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  223. 'value' => $comm->type,  
  224. ),  
  225.  
  226. 'enabled' => array( 
  227. 'id' => 'enabled',  
  228. 'type' => MS_Helper_Html::INPUT_TYPE_RADIO_SLIDER,  
  229. 'value' => $comm->enabled,  
  230. 'class' => 'state-slider',  
  231. 'before' => ' <i class="wpmui-fa wpmui-fa-ban"></i>',  
  232. 'after' => '<i class="wpmui-fa wpmui-fa-envelope"></i> ',  
  233. 'ajax_data' => array( 
  234. 'type' => $comm->type,  
  235. 'field' => 'enabled',  
  236. 'action' => $action,  
  237. '_wpnonce' => $nonce,  
  238. 'membership_id' => $membership_id,  
  239. ),  
  240. ),  
  241.  
  242. // Note: title/desc is overwritten by MS_Model_Communication (below) 
  243. 'period_unit' => array( 
  244. 'id' => 'period_unit',  
  245. 'type' => MS_Helper_Html::INPUT_TYPE_TEXT,  
  246. 'title' => __( 'Period after/before', 'membership2' ),  
  247. 'value' => $comm->period['period_unit'],  
  248. ),  
  249.  
  250. 'period_type' => array( 
  251. 'id' => 'period_type',  
  252. 'type' => MS_Helper_Html::INPUT_TYPE_SELECT,  
  253. 'value' => $comm->period['period_type'],  
  254. 'field_options' => MS_Helper_Period::get_period_types( 'plural' ),  
  255. ),  
  256.  
  257. 'subject' => array( 
  258. 'id' => 'subject',  
  259. 'type' => MS_Helper_Html::INPUT_TYPE_TEXT,  
  260. 'title' => apply_filters( 
  261. 'ms_translation_flag',  
  262. __( 'Message Subject', 'membership2' ),  
  263. 'communication-subject' 
  264. ),  
  265. 'value' => $comm->subject,  
  266. 'class' => 'ms-comm-subject widefat',  
  267. ),  
  268.  
  269. 'email_body' => array( 
  270. 'id' => 'email_body',  
  271. 'type' => MS_Helper_Html::INPUT_TYPE_WP_EDITOR,  
  272. 'title' => apply_filters( 
  273. 'ms_translation_flag',  
  274. '',  
  275. 'communication-body' 
  276. ),  
  277. 'value' => $comm->description,  
  278. 'field_options' => array( 
  279. 'media_buttons' => false,  
  280. 'editor_class' => 'wpmui-ajax-update',  
  281. ),  
  282. ),  
  283.  
  284. 'cc_enabled' => array( 
  285. 'id' => 'cc_enabled',  
  286. 'type' => MS_Helper_Html::INPUT_TYPE_CHECKBOX,  
  287. 'title' => __( 'Send copy to Administrator', 'membership2' ),  
  288. 'value' => $comm->cc_enabled,  
  289. 'class' => 'ms-inline-block',  
  290. ),  
  291.  
  292. 'cc_email' => array( 
  293. 'id' => 'cc_email',  
  294. 'type' => MS_Helper_Html::INPUT_TYPE_SELECT,  
  295. 'value' => $comm->cc_email,  
  296. 'field_options' => MS_Model_Member::get_admin_user_emails(),  
  297. ),  
  298.  
  299. 'save_email' => array( 
  300. 'id' => 'save_email',  
  301. 'value' => __( 'Save Changes', 'membership2' ),  
  302. 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT,  
  303. ),  
  304.  
  305. 'action' => array( 
  306. 'id' => 'action',  
  307. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  308. 'value' => 'save_comm',  
  309. ),  
  310.  
  311. 'nonce' => array( 
  312. 'id' => '_wpnonce',  
  313. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  314. 'value' => wp_create_nonce( 'save_comm' ),  
  315. ),  
  316.  
  317. 'load_action' => array( 
  318. 'id' => 'load_action',  
  319. 'name' => 'action',  
  320. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  321. 'value' => 'load_action',  
  322. ),  
  323.  
  324. 'load_nonce' => array( 
  325. 'id' => '_wpnonce1',  
  326. 'name' => '_wpnonce',  
  327. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  328. 'value' => wp_create_nonce( 'load_action' ),  
  329. ),  
  330. ); 
  331.  
  332. if ( ! ( $membership instanceof MS_Model_Membership ) ) { 
  333. unset( $fields['override'] ); 
  334. unset( $fields['membership_id'] ); 
  335.  
  336. return apply_filters( 
  337. 'ms_view_settings_prepare_email_fields',  
  338. $fields 
  339. ); 
.