class-simple-job-board-privacy-exporter.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. if (!defined('ABSPATH')) {
  3. exit;
  4. } // Exit if accessed directly
  5. /**
  6. * Simple_Job_Board_Privacy_Exporter Class
  7. *
  8. * @link https://wordpress.org/plugins/simple-job-board
  9. *
  10. * @since 2.6.0
  11. *
  12. * @package Simple_Job_Board
  13. * @subpackage Simple_Job_Board/includes
  14. * @author PressTigers <support@presstigers.com>
  15. */
  16. class Simple_Job_Board_Privacy_Exporter {
  17. /**
  18. * Initialize the class and set its properties.
  19. *
  20. * @since 2.6.0
  21. */
  22. public function __construct() {
  23. // Hook - Register SJB Exporter Implementation to WP Core Exporter
  24. add_filter('wp_privacy_personal_data_exporters', array($this, 'register_exporter'));
  25. // Add applicant resumes to exported zip file.
  26. add_action('wp_privacy_personal_data_export_file_created', array($this, 'add_resume_to_zip'), 999, 4);
  27. }
  28. /**
  29. * Integrate applicant data eraser implementation in WP core eraser.
  30. *
  31. * @since 2.6.0
  32. *
  33. * @param array $erasers List of eraser callbacks.
  34. * @return array
  35. */
  36. public function register_exporter($exporter = array()) {
  37. $erasers['sjb-application-exporter'] = array(
  38. 'exporter_friendly_name' => __('Application Exporter'),
  39. 'callback' => array($this, 'applicant_data_exporter'),
  40. );
  41. return $erasers;
  42. }
  43. /**
  44. * Export applicant's data.
  45. *
  46. * @since 2.6.0
  47. */
  48. public function applicant_data_exporter($email_address, $page) {
  49. global $wpdb;
  50. $done = FALSE;
  51. $page = (int) $page;
  52. $user = get_user_by('email', $email_address); // Check if user has an ID in the DB to load stored personal data.
  53. $data_to_export = array();
  54. // Get applicants assoicated with requested user email
  55. $applicants = $wpdb->get_results($wpdb->prepare("
  56. SELECT p.ID FROM {$wpdb->prefix}posts AS p INNER JOIN {$wpdb->prefix}postmeta AS pt ON p.ID = pt.post_id WHERE pt.meta_value = %s AND p.post_type = %s AND p.post_status = %s", $email_address, 'jobpost_applicants', 'publish'));
  57. if (!empty($applicants)):
  58. foreach ($applicants as $applicant) {
  59. $data_to_export[] = array(
  60. 'group_id' => 'applicant_details',
  61. 'group_label' => __('Applicant Details', 'simple-job-board'),
  62. 'item_id' => 'applicant-' . intval($applicant->ID),
  63. 'data' => $this->get_applicant_personal_data(intval($applicant->ID)),
  64. );
  65. }
  66. endif;
  67. return array(
  68. 'data' => $data_to_export,
  69. 'done' => TRUE,
  70. );
  71. }
  72. /**
  73. * Get applicant details against his email ID.
  74. *
  75. * @since 2.6.0
  76. *
  77. * @param int $applicant_id Applicant ID
  78. * @return array $data_to_export Data export array.
  79. */
  80. public function get_applicant_personal_data($applicant_id) {
  81. $keys = get_post_custom_keys($applicant_id);
  82. if (!empty($keys)):
  83. $data_to_export[] = array(
  84. 'name' => __('Job Applied for', 'simple-job-board'),
  85. 'value' => get_the_title($applicant_id),
  86. );
  87. foreach ($keys as $key):
  88. if (substr($key, 0, 7) == 'jobapp_') {
  89. $count = 0;
  90. if (!is_serialized(get_post_meta($applicant_id, $key, TRUE))) {
  91. $data_to_export[] = array(
  92. 'name' => ucwords(str_replace('_', ' ', substr($key, 7))),
  93. 'value' => get_post_meta($applicant_id, $key, TRUE),
  94. );
  95. } else {
  96. $values = unserialize(get_post_meta($applicant_id, $key, TRUE));
  97. if (is_array($values)) {
  98. foreach ($values as $val):
  99. $val = $val;
  100. if ($count > 1) {
  101. $val.= ',&nbsp';
  102. }
  103. $count--;
  104. endforeach;
  105. $data_to_export[] = array(
  106. 'name' => ucwords(str_replace('_', ' ', substr($key, 7))),
  107. 'value' => $val,
  108. );
  109. } else {
  110. $data_to_export[] = array(
  111. 'name' => ucwords(str_replace('_', ' ', substr($key, 7))),
  112. 'value' => get_post_meta($applicant_id, $key, TRUE),
  113. );
  114. }
  115. }
  116. }
  117. $count++;
  118. endforeach;
  119. endif;
  120. return $data_to_export;
  121. }
  122. /**
  123. * Add resume to exported zip.
  124. *
  125. * @since 2.6.0
  126. *
  127. * $archive_pathname string Export file path.
  128. * $archive_url string Export file url.
  129. * $html_report_pathname string HTML file path.
  130. * $request_id int Export request id.
  131. */
  132. function add_resume_to_zip($archive_pathname, $archive_url, $html_report_pathname, $request_id) {
  133. global $wpdb;
  134. // Get the request data.
  135. $user = wp_get_user_request_data($request_id);
  136. $email_address = sanitize_email($user->email);
  137. $applicants = $wpdb->get_results($wpdb->prepare('
  138. SELECT p.ID FROM wp_posts AS p INNER JOIN wp_postmeta AS pt ON p.ID = pt.post_id WHERE pt.meta_value = %s AND post_type = %s', $email_address, 'jobpost_applicants'));
  139. $zip = new ZipArchive;
  140. if (!empty($applicants)):
  141. foreach ($applicants as $applicant) {
  142. if (get_post_meta($applicant->ID, 'resume_path', TRUE)) {
  143. $html_report_pathname = get_post_meta($applicant->ID, 'resume_path', TRUE);
  144. // Resume Name
  145. $filename = basename($html_report_pathname);
  146. if (TRUE === $zip->open($archive_pathname, ZipArchive::CREATE)) {
  147. if (!$zip->addFile($html_report_pathname, 'resume/' . $filename)) {
  148. $error = __('Unable to add data to export file.');
  149. }
  150. }
  151. }
  152. // Fetch multiple Attachment add-on data
  153. if ($files = get_post_meta($applicant->ID, 'attachments_meta', TRUE)) {
  154. $count = count($files['file_name']);
  155. for ($i = 0; $i < $count; $i++) {
  156. if ('' != $files['file_name'][$i]) {
  157. $file_path = $files['base_dir'] . '/' . esc_attr($files['file_name'][$i]);
  158. if (TRUE === $zip->open($archive_pathname, ZipArchive::CREATE)) {
  159. if (!$zip->addFile($file_path, 'resume/' . $files['file_name'][$i])) {
  160. $error = __('Unable to add data to export file.');
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. $zip->close();
  168. endif;
  169. }
  170. }
  171. new Simple_Job_Board_Privacy_Exporter();