class-simple-job-board-meta-box-job-features.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php if (!defined('ABSPATH')) { exit; } // Exit if accessed directly
  2. /**
  3. * Simple_Job_Board_Meta_box_Job_Features Class
  4. *
  5. * This meta box is designed to create job features and list the user defined job features.
  6. *
  7. * @link https://wordpress.org/plugins/simple-job-board
  8. * @since 2.2.3
  9. * @since 2.3.2 Added Job Features Labels' Editing Feature
  10. * @since 2.4.0 Improved Sanitization & Escaping of Job Features' Inputs & Outputs
  11. * @since 2.4.5 Fixed the job application form builder issue with multilingual characters.
  12. *
  13. * @package Simple_Job_Board
  14. * @subpackage Simple_Job_Board/admin/partials/meta-boxes
  15. * @author PressTigers <support@presstigers.com>
  16. */
  17. class Simple_Job_Board_Meta_Box_Job_Features {
  18. /**
  19. * Meta box for Job Features.
  20. *
  21. * @since 2.2.3
  22. */
  23. public static function sjb_meta_box_output($post) {
  24. global $jobfields;
  25. // Add a nonce field so we can check for it later.
  26. wp_nonce_field('sjb_jobpost_meta_box', 'jobpost_meta_box_nonce');
  27. /*
  28. * Use get_post_meta() to retrieve an existing value
  29. * from the database and use the value for the form.
  30. */
  31. ?>
  32. <div class="job_features meta_option_panel jobpost_fields">
  33. <ul id="job_features">
  34. <?php
  35. $keys = get_post_custom_keys($post->ID);
  36. //getting setting page saved options
  37. $settings_options = unserialize(get_option('jobfeature_settings_options'));
  38. //check Array differnce when $keys is not NULL
  39. if (NULL == $keys) {
  40. //"Add New" job check
  41. $removed_options = $settings_options;
  42. } elseif (NULL == $settings_options) {
  43. $removed_options = '';
  44. } else {
  45. //Remove the same option from post meta and options
  46. $removed_options = array_diff_key($settings_options, get_post_meta($post->ID));
  47. }
  48. if (NULL != $keys):
  49. foreach ($keys as $key):
  50. if (substr($key, 0, 11) == 'jobfeature_') {
  51. $val = get_post_meta($post->ID, $key, TRUE);
  52. if (is_serialized($val)) {
  53. $val = unserialize( $val );
  54. $val = ( is_array( $val ) ) ? array_map('esc_attr', $val ) : esc_attr( $val );
  55. }
  56. /**
  57. * New Label Index Insertion:
  58. *
  59. * - Addition of new index "label"
  60. * - Data Legacy Checking
  61. */
  62. $label = isset($val['label']) ? $val['label'] : __(ucwords(str_replace('_', ' ', substr($key, 11))), 'simple-job-board');
  63. $value = isset($val['value']) ? $val['value'] : $val;
  64. echo '<li><label class="sjb-editable-label">'
  65. . $label
  66. . '</label>'
  67. . '<input type="hidden" name="' . $key . '[label]" value="' . $label . '" />';
  68. // Setting options meta Fileds button to Empty
  69. $button = '<div class="button removeField">' . esc_html__('Delete', 'simple-job-board') . '</div>';
  70. echo '<input type="text" id="' . $key . '" name="' . $key . '[value]" value="' . $value . '" /> &nbsp; ' . $button . '</li>';
  71. }
  72. endforeach;
  73. endif;
  74. // Adding setting page features to jobpost
  75. if (NULL != $removed_options):
  76. if ( !isset( $_GET['action'] ) ):
  77. foreach ($removed_options as $key => $val):
  78. if (substr($key, 0, 11) == 'jobfeature_') {
  79. $val = ( is_array( $val ) )? array_map('esc_attr', $val ) : esc_attr( $val );
  80. /**
  81. * New Label Index Insertion:
  82. *
  83. * - Addition of new index "label"
  84. * - Data Legacy Checking
  85. */
  86. $label = isset($val['label']) ? $val['label'] : __(ucwords(str_replace('_', ' ', substr($key, 11))), 'simple-job-board');
  87. $value = isset($val['value']) ? $val['value'] : $val;
  88. // Convert Empty Value Parameter to NULL
  89. if ('empty' === $value) {
  90. $value = '';
  91. }
  92. echo '<li><label class="sjb-editable-label">'
  93. . $label
  94. . '</label>'
  95. . '<input type="hidden" name="' . $key . '[label]" value="' . $label . '" />';
  96. echo '<input type="text" id="' . $key . '" name="' . $key . '[value]" value="' . $value . '" /> &nbsp; <div class="button removeField">' . esc_html__('Delete', 'simple-job-board') . '</div></li>';
  97. }
  98. endforeach;
  99. endif;
  100. endif;
  101. ?>
  102. </ul>
  103. </div>
  104. <div class="clearfix clear"></div>
  105. <table id="jobfeatures_form" class="alignleft">
  106. <thead>
  107. <tr>
  108. <th><label for="jobFeature"><?php echo esc_html__('Feature', 'simple-job-board'); ?></label></th>
  109. <th><label for="jobFeatureVal"><?php echo esc_html__('Value', 'simple-job-board'); ?></label></th>
  110. <th></th>
  111. </tr>
  112. </thead>
  113. <tbody>
  114. <tr>
  115. <td id="jobFeature"><input type="text" id="jobfeature_name" /></td>
  116. <td><input type="text" id="jobfeature_value" /></td>
  117. <td><div class="button" id="addFeature"><?php echo esc_html__('Add Field', 'simple-job-board'); ?></div></td>
  118. </tr>
  119. </tbody>
  120. </table>
  121. <div class="clearfix clear"></div>
  122. <?php
  123. }
  124. /**
  125. * Save job features meta box.
  126. *
  127. * @since 2.2.3
  128. *
  129. * @param int $post_id Post id
  130. * @return void
  131. */
  132. public static function sjb_save_jobpost_meta($post_id) {
  133. // Delete previous stored fields.
  134. $old_keys = get_post_custom_keys($post_id);
  135. foreach ($old_keys as $key => $val):
  136. if ( substr($val, 0, 11) == 'jobfeature_' ) {
  137. delete_post_meta($post_id, $val); //Remove meta from the db.
  138. }
  139. endforeach;
  140. $POST_data = filter_input_array( INPUT_POST );
  141. // Add new value.
  142. foreach( $POST_data as $key => $val ):
  143. if ( substr($key, 0, 11 ) == 'jobfeature_' ) { // Make sure that it is set.
  144. $key = preg_replace('/[^\p{L} 0-9]/u', '_', $key);
  145. $data = serialize( array_map('sanitize_text_field', filter_input( INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ) ) );
  146. update_post_meta( $post_id, $key, $data ); // Add new value.
  147. }
  148. endforeach;
  149. }
  150. }