class-wc-settings-emails.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * WooCommerce Email Settings
  4. *
  5. * @package WooCommerce/Admin
  6. * @version 2.1.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. if ( class_exists( 'WC_Settings_Emails', false ) ) {
  10. return new WC_Settings_Emails();
  11. }
  12. /**
  13. * WC_Settings_Emails.
  14. */
  15. class WC_Settings_Emails extends WC_Settings_Page {
  16. /**
  17. * Constructor.
  18. */
  19. public function __construct() {
  20. $this->id = 'email';
  21. $this->label = __( 'Emails', 'woocommerce' );
  22. add_action( 'woocommerce_admin_field_email_notification', array( $this, 'email_notification_setting' ) );
  23. parent::__construct();
  24. }
  25. /**
  26. * Get sections.
  27. *
  28. * @return array
  29. */
  30. public function get_sections() {
  31. $sections = array(
  32. '' => __( 'Email options', 'woocommerce' ),
  33. );
  34. return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
  35. }
  36. /**
  37. * Get settings array.
  38. *
  39. * @return array
  40. */
  41. public function get_settings() {
  42. $settings = apply_filters(
  43. 'woocommerce_email_settings', array(
  44. array(
  45. 'title' => __( 'Email notifications', 'woocommerce' ),
  46. 'desc' => __( 'Email notifications sent from WooCommerce are listed below. Click on an email to configure it.', 'woocommerce' ),
  47. 'type' => 'title',
  48. 'id' => 'email_notification_settings',
  49. ),
  50. array( 'type' => 'email_notification' ),
  51. array(
  52. 'type' => 'sectionend',
  53. 'id' => 'email_notification_settings',
  54. ),
  55. array(
  56. 'type' => 'sectionend',
  57. 'id' => 'email_recipient_options',
  58. ),
  59. array(
  60. 'title' => __( 'Email sender options', 'woocommerce' ),
  61. 'type' => 'title',
  62. 'desc' => '',
  63. 'id' => 'email_options',
  64. ),
  65. array(
  66. 'title' => __( '"From" name', 'woocommerce' ),
  67. 'desc' => __( 'How the sender name appears in outgoing WooCommerce emails.', 'woocommerce' ),
  68. 'id' => 'woocommerce_email_from_name',
  69. 'type' => 'text',
  70. 'css' => 'min-width:300px;',
  71. 'default' => esc_attr( get_bloginfo( 'name', 'display' ) ),
  72. 'autoload' => false,
  73. 'desc_tip' => true,
  74. ),
  75. array(
  76. 'title' => __( '"From" address', 'woocommerce' ),
  77. 'desc' => __( 'How the sender email appears in outgoing WooCommerce emails.', 'woocommerce' ),
  78. 'id' => 'woocommerce_email_from_address',
  79. 'type' => 'email',
  80. 'custom_attributes' => array(
  81. 'multiple' => 'multiple',
  82. ),
  83. 'css' => 'min-width:300px;',
  84. 'default' => get_option( 'admin_email' ),
  85. 'autoload' => false,
  86. 'desc_tip' => true,
  87. ),
  88. array(
  89. 'type' => 'sectionend',
  90. 'id' => 'email_options',
  91. ),
  92. array(
  93. 'title' => __( 'Email template', 'woocommerce' ),
  94. 'type' => 'title',
  95. 'desc' => sprintf( __( 'This section lets you customize the WooCommerce emails. <a href="%s" target="_blank">Click here to preview your email template</a>.', 'woocommerce' ), wp_nonce_url( admin_url( '?preview_woocommerce_mail=true' ), 'preview-mail' ) ),
  96. 'id' => 'email_template_options',
  97. ),
  98. array(
  99. 'title' => __( 'Header image', 'woocommerce' ),
  100. 'desc' => __( 'URL to an image you want to show in the email header. Upload images using the media uploader (Admin > Media).', 'woocommerce' ),
  101. 'id' => 'woocommerce_email_header_image',
  102. 'type' => 'text',
  103. 'css' => 'min-width:300px;',
  104. 'placeholder' => __( 'N/A', 'woocommerce' ),
  105. 'default' => '',
  106. 'autoload' => false,
  107. 'desc_tip' => true,
  108. ),
  109. array(
  110. 'title' => __( 'Footer text', 'woocommerce' ),
  111. 'desc' => __( 'The text to appear in the footer of WooCommerce emails.', 'woocommerce' ) . ' ' . sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '{site_title}' ),
  112. 'id' => 'woocommerce_email_footer_text',
  113. 'css' => 'width:300px; height: 75px;',
  114. 'placeholder' => __( 'N/A', 'woocommerce' ),
  115. 'type' => 'textarea',
  116. 'default' => '{site_title}',
  117. 'autoload' => false,
  118. 'desc_tip' => true,
  119. ),
  120. array(
  121. 'title' => __( 'Base color', 'woocommerce' ),
  122. /* translators: %s: default color */
  123. 'desc' => sprintf( __( 'The base color for WooCommerce email templates. Default %s.', 'woocommerce' ), '<code>#96588a</code>' ),
  124. 'id' => 'woocommerce_email_base_color',
  125. 'type' => 'color',
  126. 'css' => 'width:6em;',
  127. 'default' => '#96588a',
  128. 'autoload' => false,
  129. 'desc_tip' => true,
  130. ),
  131. array(
  132. 'title' => __( 'Background color', 'woocommerce' ),
  133. /* translators: %s: default color */
  134. 'desc' => sprintf( __( 'The background color for WooCommerce email templates. Default %s.', 'woocommerce' ), '<code>#f7f7f7</code>' ),
  135. 'id' => 'woocommerce_email_background_color',
  136. 'type' => 'color',
  137. 'css' => 'width:6em;',
  138. 'default' => '#f7f7f7',
  139. 'autoload' => false,
  140. 'desc_tip' => true,
  141. ),
  142. array(
  143. 'title' => __( 'Body background color', 'woocommerce' ),
  144. /* translators: %s: default color */
  145. 'desc' => sprintf( __( 'The main body background color. Default %s.', 'woocommerce' ), '<code>#ffffff</code>' ),
  146. 'id' => 'woocommerce_email_body_background_color',
  147. 'type' => 'color',
  148. 'css' => 'width:6em;',
  149. 'default' => '#ffffff',
  150. 'autoload' => false,
  151. 'desc_tip' => true,
  152. ),
  153. array(
  154. 'title' => __( 'Body text color', 'woocommerce' ),
  155. /* translators: %s: default color */
  156. 'desc' => sprintf( __( 'The main body text color. Default %s.', 'woocommerce' ), '<code>#3c3c3c</code>' ),
  157. 'id' => 'woocommerce_email_text_color',
  158. 'type' => 'color',
  159. 'css' => 'width:6em;',
  160. 'default' => '#3c3c3c',
  161. 'autoload' => false,
  162. 'desc_tip' => true,
  163. ),
  164. array(
  165. 'type' => 'sectionend',
  166. 'id' => 'email_template_options',
  167. ),
  168. )
  169. );
  170. return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings );
  171. }
  172. /**
  173. * Output the settings.
  174. */
  175. public function output() {
  176. global $current_section;
  177. // Define emails that can be customised here.
  178. $mailer = WC()->mailer();
  179. $email_templates = $mailer->get_emails();
  180. if ( $current_section ) {
  181. foreach ( $email_templates as $email_key => $email ) {
  182. if ( strtolower( $email_key ) === $current_section ) {
  183. $email->admin_options();
  184. break;
  185. }
  186. }
  187. } else {
  188. $settings = $this->get_settings();
  189. WC_Admin_Settings::output_fields( $settings );
  190. }
  191. }
  192. /**
  193. * Save settings.
  194. */
  195. public function save() {
  196. global $current_section;
  197. if ( ! $current_section ) {
  198. WC_Admin_Settings::save_fields( $this->get_settings() );
  199. } else {
  200. $wc_emails = WC_Emails::instance();
  201. if ( in_array( $current_section, array_map( 'sanitize_title', array_keys( $wc_emails->get_emails() ) ), true ) ) {
  202. foreach ( $wc_emails->get_emails() as $email_id => $email ) {
  203. if ( sanitize_title( $email_id ) === $current_section ) {
  204. do_action( 'woocommerce_update_options_' . $this->id . '_' . $email->id );
  205. }
  206. }
  207. } else {
  208. do_action( 'woocommerce_update_options_' . $this->id . '_' . $current_section );
  209. }
  210. }
  211. }
  212. /**
  213. * Output email notification settings.
  214. */
  215. public function email_notification_setting() {
  216. // Define emails that can be customised here.
  217. $mailer = WC()->mailer();
  218. $email_templates = $mailer->get_emails();
  219. ?>
  220. <tr valign="top">
  221. <td class="wc_emails_wrapper" colspan="2">
  222. <table class="wc_emails widefat" cellspacing="0">
  223. <thead>
  224. <tr>
  225. <?php
  226. $columns = apply_filters(
  227. 'woocommerce_email_setting_columns', array(
  228. 'status' => '',
  229. 'name' => __( 'Email', 'woocommerce' ),
  230. 'email_type' => __( 'Content type', 'woocommerce' ),
  231. 'recipient' => __( 'Recipient(s)', 'woocommerce' ),
  232. 'actions' => '',
  233. )
  234. );
  235. foreach ( $columns as $key => $column ) {
  236. echo '<th class="wc-email-settings-table-' . esc_attr( $key ) . '">' . esc_html( $column ) . '</th>';
  237. }
  238. ?>
  239. </tr>
  240. </thead>
  241. <tbody>
  242. <?php
  243. foreach ( $email_templates as $email_key => $email ) {
  244. echo '<tr>';
  245. foreach ( $columns as $key => $column ) {
  246. switch ( $key ) {
  247. case 'name':
  248. echo '<td class="wc-email-settings-table-' . esc_attr( $key ) . '">
  249. <a href="' . admin_url( 'admin.php?page=wc-settings&tab=email&section=' . strtolower( $email_key ) ) . '">' . $email->get_title() . '</a>
  250. ' . wc_help_tip( $email->get_description() ) . '
  251. </td>';
  252. break;
  253. case 'recipient':
  254. echo '<td class="wc-email-settings-table-' . esc_attr( $key ) . '">
  255. ' . esc_html( $email->is_customer_email() ? __( 'Customer', 'woocommerce' ) : $email->get_recipient() ) . '
  256. </td>';
  257. break;
  258. case 'status':
  259. echo '<td class="wc-email-settings-table-' . esc_attr( $key ) . '">';
  260. if ( $email->is_manual() ) {
  261. echo '<span class="status-manual tips" data-tip="' . esc_attr__( 'Manually sent', 'woocommerce' ) . '">' . esc_html__( 'Manual', 'woocommerce' ) . '</span>';
  262. } elseif ( $email->is_enabled() ) {
  263. echo '<span class="status-enabled tips" data-tip="' . esc_attr__( 'Enabled', 'woocommerce' ) . '">' . esc_html__( 'Yes', 'woocommerce' ) . '</span>';
  264. } else {
  265. echo '<span class="status-disabled tips" data-tip="' . esc_attr__( 'Disabled', 'woocommerce' ) . '">-</span>';
  266. }
  267. echo '</td>';
  268. break;
  269. case 'email_type':
  270. echo '<td class="wc-email-settings-table-' . esc_attr( $key ) . '">
  271. ' . esc_html( $email->get_content_type() ) . '
  272. </td>';
  273. break;
  274. case 'actions':
  275. echo '<td class="wc-email-settings-table-' . esc_attr( $key ) . '">
  276. <a class="button alignright" href="' . admin_url( 'admin.php?page=wc-settings&tab=email&section=' . strtolower( $email_key ) ) . '">' . esc_html__( 'Manage', 'woocommerce' ) . '</a>
  277. </td>';
  278. break;
  279. default:
  280. do_action( 'woocommerce_email_setting_column_' . $key, $email );
  281. break;
  282. }
  283. }
  284. echo '</tr>';
  285. }
  286. ?>
  287. </tbody>
  288. </table>
  289. </td>
  290. </tr>
  291. <?php
  292. }
  293. }
  294. return new WC_Settings_Emails();