class-simple-job-board-notifications.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php if (!defined('ABSPATH')) { exit; } // Exit if accessed directly
  2. /**
  3. * Simple_Job_Board_Notifications Class
  4. *
  5. * This class is used to notify Admin, HR and Applicant on application submission.
  6. *
  7. * @link https://wordpress.org/plugins/simple-job-board
  8. * @since 1.0.0
  9. * @since 2.2.2 Added "sjb_notification_template" filter.
  10. * @since 2.2.3 Added "sjb_applicant_details_notification" filter.
  11. * @since 2.3.0 Revised the Admin, HR and Applicant notification templates.
  12. * @since 2.4.0 Added Email Reply-to & From Parameters, & Revised Inputs & Outputs, Sanitization & Escaping
  13. * @since 2.4.5 Added $post_id in HR & Admin Email-to filters' parameters.
  14. * - Breakdown the email notification templates into their respective functions.
  15. * - Introduced "sjb_admin_email_template" filter.
  16. * - Introduced "sjb_hr_email_template" filter.
  17. * - Introduced "sjb_applicant_email_template" filter.
  18. *
  19. * @package Simple_Job_Board
  20. * @subpackage Simple_Job_Board/includes
  21. * @author PressTigers <support@presstigers.com>
  22. */
  23. class Simple_Job_Board_Notifications {
  24. /**
  25. * Admin Notification
  26. *
  27. * @since 1.0.0
  28. *
  29. * @param $post_id Post ID
  30. * @return void
  31. */
  32. public static function admin_notification($post_id) {
  33. // Applied job title
  34. $job_title = get_the_title($post_id);
  35. $applicant_post_keys = get_post_custom_keys($post_id);
  36. // Applicant Email
  37. $applicant_email = self::applicant_details('email', $post_id);
  38. // Applicant Name
  39. $applicant_name = self::applicant_details('name', $post_id);
  40. // Admin Email Address
  41. $admin_email = ( FALSE !== get_option( 'settings_admin_email' ) ) ? get_option( 'settings_admin_email' ) : get_option( 'admin_email' );
  42. $to = apply_filters('sjb_admin_notification_to', esc_attr( $admin_email ) , $post_id);
  43. $subject = apply_filters('sjb_admin_notification_sbj', sprintf(esc_html__('Applicant Resume Received %s ', 'simple-job-board'), html_entity_decode( $job_title )), $job_title, $post_id);
  44. // Email Header: Reply-to & From Parameters
  45. $headers[] = 'From: ' . get_bloginfo('name') . ' <' . esc_attr( $admin_email ) . '>';
  46. if (!empty($applicant_name) && !empty($applicant_email)) {
  47. $headers[] = 'Reply-To: ' . $applicant_name . ' <' . $applicant_email . '>';
  48. }
  49. $headers[] = 'Content-Type: text/html; charset=UTF-8';
  50. $message = self::job_notification_templates($post_id, 'Admin');
  51. $attachment = apply_filters('sjb_admin_notification_attachment', '', $post_id);
  52. wp_mail($to, $subject, $message, $headers, $attachment);
  53. }
  54. /**
  55. * HR Notification
  56. *
  57. * @since 1.0.0
  58. *
  59. * @param $post_id Post ID
  60. * @return void
  61. */
  62. public static function hr_notification( $post_id ) {
  63. // Applied job title
  64. $job_title = get_the_title($post_id);
  65. // Applicant Email
  66. $applicant_email = self::applicant_details('mail', $post_id);
  67. // Applicant Name
  68. $applicant_name = self::applicant_details('name', $post_id);
  69. $to = apply_filters('sjb_hr_notification_to', get_option('settings_hr_email'), $post_id);
  70. $subject = apply_filters('sjb_hr_notification_sbj', sprintf(esc_html__('Applicant Resume Received %s ', 'simple-job-board'), html_entity_decode( $job_title )), $job_title, $post_id);
  71. $message = self::job_notification_templates($post_id, 'HR');
  72. // Admin Email
  73. $admin_email = ( FALSE !== get_option( 'settings_admin_email' ) ) ? get_option( 'settings_admin_email' ) : get_option( 'admin_email' );
  74. // Email Header: Reply-to & From Parameters
  75. $headers[] = 'From: ' . get_bloginfo('name') . ' <' . esc_attr($admin_email) . '>';
  76. if (!empty($applicant_name) && !empty($applicant_email)) {
  77. $headers[] = 'Reply-To: ' . $applicant_name . ' <' . $applicant_email . '>';
  78. }
  79. $headers[] = 'Content-Type: text/html; charset=UTF-8';
  80. $attachment = apply_filters('sjb_hr_notification_attachment', '', $post_id);
  81. if ('' != $to)
  82. wp_mail($to, $subject, $message, $headers, $attachment);
  83. }
  84. /**
  85. * Applicant Notification
  86. *
  87. * @since 1.0.0
  88. *
  89. * @param $post_id Post ID
  90. * @return void
  91. */
  92. public static function applicant_notification($post_id) {
  93. // Applied job title
  94. $job_title = get_the_title($post_id);
  95. // Applicant Email
  96. $applicant_email = self::applicant_details('email', $post_id);
  97. $subject = apply_filters('sjb_applicant_notification_sbj', sprintf(esc_html__('Your Resume Received for Job %s ', 'simple-job-board'), html_entity_decode( $job_title )), $job_title, $post_id);
  98. $message = self::job_notification_templates($post_id, 'applicant');
  99. // Admin Email
  100. $admin_email = ( FALSE !== get_option( 'settings_admin_email' ) ) ? get_option( 'settings_admin_email' ) : get_option( 'admin_email' );
  101. // Get the site domain and get rid of www.
  102. $sitename = strtolower($_SERVER['SERVER_NAME']);
  103. if (substr($sitename, 0, 4) == 'www.') {
  104. $sitename = substr($sitename, 4);
  105. }
  106. // Email Header: Reply-to & From Parameters
  107. $from_email = 'noreply@' . $sitename;
  108. $headers[] = 'From: ' . get_bloginfo('name') . ' <' . esc_attr( $admin_email ) . '>';
  109. $headers[] = 'Reply-To: ' . get_bloginfo('name') . '<' . $from_email . '>';
  110. $headers[] = 'Content-Type: text/html; charset=UTF-8';
  111. // Validate Applicant Email
  112. if ( isset( $applicant_email ) && is_email( $applicant_email ) )
  113. wp_mail( $applicant_email, $subject, $message, $headers );
  114. }
  115. /**
  116. * Email Template
  117. *
  118. * @since 1.0.0
  119. *
  120. * @param int $post_id Post ID
  121. * @param string $notification_receiver Notification Receiver (Admin or HR or || Applicant)
  122. * @return string $message Email Template
  123. */
  124. public static function job_notification_templates($post_id, $notification_receiver) {
  125. $message = self::email_start_template($notification_receiver, $post_id);
  126. if ('HR' === $notification_receiver) {
  127. $message .= self::hr_email_template($post_id, $notification_receiver );
  128. } elseif ('Admin' === $notification_receiver) {
  129. $message .= self::admin_email_template( $post_id, $notification_receiver );
  130. } else {
  131. $message .= self::applicant_email_template( $post_id, $notification_receiver );
  132. }
  133. $message .= self::email_end_template( $post_id, $notification_receiver );
  134. /**
  135. * Hook -> Notification Message.
  136. *
  137. * @since 2.2.0
  138. * @since 2.2.3 Added $post_id and $notification_receiver parameters in filter.
  139. *
  140. * @param string $message Email Template
  141. * @param int $post_id Post Id
  142. * @param string $notification_receiver Notification Receiver
  143. */
  144. return apply_filters('sjb_notification_template', $message, $post_id, $notification_receiver);
  145. }
  146. /**
  147. * Email Start Template
  148. *
  149. * @since 2.4.5
  150. *
  151. * @param int $post_id Post ID
  152. * @param string $notification_receiver Notification Receiver (Admin or HR or || Applicant)
  153. * @return string $message Email Start Template
  154. */
  155. public static function email_start_template( $post_id, $notification_receiver ) {
  156. $header_title = ( 'applicant' != $notification_receiver ) ? esc_html__('Job Application', 'simple-job-board') : esc_html__('Job Application Acknowledgement', 'simple-job-board');
  157. $message = '<div style="width:700px; margin:0 auto; border: 1px solid #95B3D7;font-family:Arial;">'
  158. . '<div style="border: 1px solid #95B3D7; background-color:#95B3D7;">'
  159. . ' <h2 style="text-align:center;">' . $header_title . '</h2>'
  160. . ' </div>'
  161. . '<div style="margin:10px;">'
  162. . '<p>' . date("Y/m/d") . '</p>'
  163. . '<p>';
  164. /**
  165. * Modify Email Start Template
  166. *
  167. * @since 2.4.5
  168. *
  169. * @param string $message Email Start Template
  170. * @param int $post_id Post ID
  171. * @param string $notification_receiver Notification Receiver (Admin or HR or || Applicant) *
  172. */
  173. return apply_filters('sjb_email_start_template', $message, $notification_receiver, $post_id);
  174. }
  175. /**
  176. * Email End Template
  177. *
  178. * @since 2.4.5
  179. */
  180. public static function email_end_template( $post_id, $notification_receiver ) {
  181. $message = '</div>'
  182. . '</div>';
  183. /**
  184. * Modify Email End Template
  185. *
  186. * @since 2.4.5
  187. *
  188. * @param string $message Email End Template
  189. * @param int $post_id Post ID
  190. * @param string $notification_receiver Notification Receiver (Admin or HR or || Applicant) *
  191. */
  192. return apply_filters('sjb_email_end_template', $message, $post_id, $notification_receiver);
  193. }
  194. /**
  195. * Admin Email Template
  196. *
  197. * @since 2.4.5
  198. *
  199. * @param int $post_id Post ID
  200. * @param string $notification_receiver Notification Receiver (Admin or HR or || Applicant)
  201. * @return string $message Admin Email Template
  202. */
  203. public static function admin_email_template( $post_id, $notification_receiver ) {
  204. // Applied Job Title
  205. $job_title = get_the_title($post_id);
  206. // Applicant Name
  207. $applicant_name = self::applicant_details('name', $post_id);
  208. $message = esc_html__('Hi', 'simple-job-board') . ' ' . $notification_receiver . ',</p>';
  209. $message .= '<p>' . esc_html__('I am applying for the job post', 'simple-job-board') . ' <b>' . esc_attr($job_title) . '</b> ' . esc_html__('with interest. I have attached my resume with the job application. I have also filled out the required details.', 'simple-job-board') . '</p>';
  210. /**
  211. * Hook -> Applicant details.
  212. *
  213. * Add applicant's details in notification template.
  214. *
  215. * @since 2.2.3
  216. *
  217. * @param int $post_id Post Id
  218. * @param string $notification_receiver Notification Receiver
  219. * @return string $message Message Template
  220. */
  221. $message = apply_filters('sjb_applicant_details_notification', $message, $post_id, $notification_receiver);
  222. $message .= '<p>' . esc_html__('I look forward to hearing from you.', 'simple-job-board') . '</p>'
  223. . esc_html__('Warm Regards,', 'simple-job-board') . '<br>';
  224. if ( NULL != $applicant_name ):
  225. $message.= $applicant_name . '';
  226. endif;
  227. /**
  228. * Modify Admin Email Template
  229. *
  230. * @since 2.4.5
  231. *
  232. * @param string $message Admin Email Template
  233. * @param int $post_id Post Id
  234. * @param string $notification_receiver Notification Receiver
  235. */
  236. return apply_filters('sjb_admin_email_template', $message, $post_id, $notification_receiver);
  237. }
  238. /**
  239. * HR Email Template
  240. *
  241. * @since 2.4.5
  242. *
  243. * @param int $post_id Post ID
  244. * @param string $notification_receiver Notification Receiver (Admin or HR or || Applicant)
  245. * @return string $message HR Email Template
  246. */
  247. public static function hr_email_template($post_id, $notification_receiver) {
  248. // Site URL
  249. $site_url = get_option('home');
  250. // Applied Job Title
  251. $job_title = get_the_title($post_id);
  252. // Applicant Name
  253. $applicant_name = self::applicant_details('name', $post_id);
  254. $message = esc_html__('Dear', 'simple-job-board') . ' ' . $notification_receiver . ',';
  255. $message .= '</p>'
  256. . '<p>';
  257. if (NULL != $applicant_name):
  258. $message.= '<b>' . $applicant_name . '</b> ';
  259. else:
  260. $message.= esc_html__('Applicant', 'simple-job-board') . ' ';
  261. endif;
  262. $message .= esc_html__('has applied against your job opening', 'simple-job-board') . ' <b>' . esc_attr($job_title) . '</b> ' . esc_html__('at', 'simple-job-board') . ' <a href="' . esc_url($site_url) . '">' . get_bloginfo('name') . '</a> '
  263. . esc_html__("Please login to your account to download the CV or check from the applicant's list from the dashboard.", "simple-job-board") . '</p>';
  264. /**
  265. * Hook -> Applicant details.
  266. *
  267. * Add applicant's details in notification template.
  268. *
  269. * @since 2.2.3
  270. *
  271. * @param int $post_id Post Id
  272. * @param string $notification_receiver Notification Receiver
  273. * @return string $message Message Template
  274. */
  275. $message = apply_filters('sjb_applicant_details_notification', $message, $post_id, $notification_receiver);
  276. $message .= '<br>' . esc_html__('Best Regards,', 'simple-job-board') . '<br>'
  277. . esc_html__('Admin', 'simple-job-board') . '<br>';
  278. /**
  279. * Modify HR Email Template
  280. *
  281. * @since 2.4.5
  282. *
  283. * @param string $message HR Email Template
  284. * @param int $post_id Post Id
  285. * @param string $notification_receiver Notification Receiver
  286. */
  287. return apply_filters( 'sjb_hr_email_template', $message, $post_id, $notification_receiver );
  288. }
  289. /**
  290. * Applicant Email Template
  291. *
  292. * @since 2.4.5
  293. *
  294. * @param int $post_id Post ID
  295. * @param string $notification_receiver Notification Receiver (Admin or HR or || Applicant)
  296. * @return string $message Applicant Email Template
  297. */
  298. public static function applicant_email_template($post_id, $notification_receiver ) {
  299. // Site URL
  300. $site_url = get_option('home');
  301. // Applied Job Title
  302. $job_title = get_the_title($post_id);
  303. $applicant_name = self::applicant_details( 'name', $post_id );
  304. // Applicant Email Template.
  305. $message = esc_html__('Hi', 'simple-job-board');
  306. if (NULL != $applicant_name):
  307. $message .= ' ' . $applicant_name . ',';
  308. else:
  309. $message .= ' ' . esc_html__('Applicant', 'simple-job-board') . ',';
  310. endif;
  311. $message .= '<p>' . esc_html__('Your application for the position of', 'simple-job-board') . '<b> ' . esc_attr($job_title) . '</b> ' . esc_html__('at', 'simple-job-board') . ' <a href="' . esc_url($site_url) . '">' . get_bloginfo('name') . '</a> ' . esc_html__('has been successfully submitted. You will hear back from', 'simple-job-board') . ' <a href="' . esc_url($site_url) . '">' . get_bloginfo('name') . '</a> ' . esc_html__('based on their evaluation of your CV.', 'simple-job-board') . '</p>'
  312. . '<p>' . esc_html__('Good Luck!', 'simple-job-board') . '</p>'
  313. . esc_html__('Best Regards,', 'simple-job-board') . '<br>'
  314. . esc_html__('Admin', 'simple-job-board');
  315. /**
  316. * Modify Applicant Email Template
  317. *
  318. * @since 2.4.5
  319. *
  320. * @param string $message Applicant Email Template
  321. * @param int $post_id Post Id
  322. * @param string $notification_receiver Notification Receiver
  323. */
  324. return apply_filters( 'sjb_applicant_email_template', $message, $post_id, $notification_receiver );
  325. }
  326. /**
  327. * Applicant Details
  328. *
  329. * @since 2.4.5
  330. *
  331. * @param string $paramter
  332. * @param int $post_id Post Id
  333. * @return string $applicant_details Applicant Details
  334. */
  335. public static function applicant_details($paramter, $post_id) {
  336. $applicant_post_keys = get_post_custom_keys($post_id);
  337. $applicant_details = '';
  338. // Search Applicant Name
  339. if (NULL != $applicant_post_keys):
  340. foreach ($applicant_post_keys as $key) {
  341. if ('jobapp_' === substr($key, 0, 7)) {
  342. $place = strpos($key, $paramter);
  343. if (!empty($place)) {
  344. $applicant_details = get_post_meta($post_id, $key, TRUE);
  345. break;
  346. }
  347. }
  348. }
  349. endif;
  350. return $applicant_details;
  351. }
  352. }
  353. new Simple_Job_Board_Notifications();