| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
|
| 6 |
// register meta box |
| 7 |
function etimeclockwp_users_register_meta_boxes() { |
| 8 |
add_meta_box( 'meta-box-id-general', __( 'User Information', 'time-clock' ), 'etimeclockwp_callback_general', 'etimeclockwp_users','normal'); |
| 9 |
add_meta_box('meta-box-id-order', __( 'Save', 'time-clock' ), 'etimeclockwp_callback_save', 'etimeclockwp_users','side'); |
| 10 |
} |
| 11 |
add_action( 'add_meta_boxes', 'etimeclockwp_users_register_meta_boxes' ); |
| 12 |
|
| 13 |
|
| 14 |
// callback general |
| 15 |
function etimeclockwp_callback_general($post) { |
| 16 |
global $meta_box, $post; |
| 17 |
|
| 18 |
echo "<div class='etimeclockwp_meta_box'>"; |
| 19 |
|
| 20 |
echo "<style>#post-body-content { margin-bottom: 0px; } |
| 21 |
.etimeclockwp-field-error { color: #cc0000; font-weight: bold; display: none; } |
| 22 |
input.etimeclockwp-error { border-color: #cc0000; } |
| 23 |
</style>"; |
| 24 |
|
| 25 |
// Use nonce for verification |
| 26 |
echo '<input type="hidden" name="etimeclockwp_MetaNonce" value="', wp_create_nonce(basename(__FILE__)), '" />'; |
| 27 |
|
| 28 |
echo '<input type="hidden" name="etimeclockwp_submit" value="1" />'; |
| 29 |
|
| 30 |
echo '<table class="form-table"><tr>'; |
| 31 |
|
| 32 |
$etimeclockwp_name = sanitize_text_field(get_post_meta($post->ID,'etimeclockwp_name', true)); |
| 33 |
echo "<td class='etimeclockwp_cell_width_product'>"; echo __('Full Name','time-clock'); echo ": </td><td><input size='40' type='text' name='etimeclockwp_name' id='etimeclockwp_name' value='$etimeclockwp_name' required> ("; echo __( 'Required', 'time-clock' ); echo ")</td>"; |
| 34 |
echo "<td><span class='etimeclockwp-field-error etimeclockwp-name-error'>" . __('Full Name is required', 'time-clock') . "</span></td>"; |
| 35 |
|
| 36 |
echo "</tr><tr>"; |
| 37 |
|
| 38 |
$etimeclockwp_id = sanitize_text_field(get_post_meta($post->ID,'etimeclockwp_id', true)); |
| 39 |
echo "<td class='etimeclockwp_cell_width_product'>"; echo __('User ID','time-clock'); echo ": </td><td><input size='40' type='text' name='etimeclockwp_id' id='etimeclockwp_id' value='$etimeclockwp_id' required> ("; echo __( 'Required. The User ID should not contains spaces. Example: JSmith87', 'time-clock' ); echo ")</td>"; |
| 40 |
echo "<td><span class='etimeclockwp-field-error etimeclockwp-id-error'>" . __('User ID is required and should not contain spaces', 'time-clock') . "</span></td>"; |
| 41 |
|
| 42 |
echo "</tr><tr>"; |
| 43 |
|
| 44 |
$etimeclockwp_pwd = sanitize_text_field(get_post_meta($post->ID,'etimeclockwp_pwd', true)); |
| 45 |
echo "<td class='etimeclockwp_cell_width_product'>"; echo __('Password','time-clock'); echo ": </td><td><input size='40' type='text' name='etimeclockwp_pwd' id='etimeclockwp_pwd' value='$etimeclockwp_pwd' required> ("; echo __( 'Required', 'time-clock' ); echo ")</td>"; |
| 46 |
echo "<td><span class='etimeclockwp-field-error etimeclockwp-pwd-error'>" . __('Password is required', 'time-clock') . "</span></td>"; |
| 47 |
|
| 48 |
echo '</tr></table>'; |
| 49 |
|
| 50 |
echo "</div>"; |
| 51 |
|
| 52 |
// Add client-side validation script |
| 53 |
echo '<script type="text/javascript"> |
| 54 |
jQuery(document).ready(function($) { |
| 55 |
// Form validation on submit |
| 56 |
$("#post").on("submit", function(e) { |
| 57 |
let hasErrors = false; |
| 58 |
|
| 59 |
// Reset error states |
| 60 |
$(".etimeclockwp-field-error").hide(); |
| 61 |
$("input").removeClass("etimeclockwp-error"); |
| 62 |
|
| 63 |
// Validate Full Name |
| 64 |
if ($("#etimeclockwp_name").val() === "") { |
| 65 |
$("#etimeclockwp_name").addClass("etimeclockwp-error"); |
| 66 |
$(".etimeclockwp-name-error").show(); |
| 67 |
hasErrors = true; |
| 68 |
} |
| 69 |
|
| 70 |
// Validate User ID |
| 71 |
const userId = $("#etimeclockwp_id").val(); |
| 72 |
if (userId === "" || userId.indexOf(" ") >= 0) { |
| 73 |
$("#etimeclockwp_id").addClass("etimeclockwp-error"); |
| 74 |
$(".etimeclockwp-id-error").show(); |
| 75 |
hasErrors = true; |
| 76 |
} |
| 77 |
|
| 78 |
// Validate Password |
| 79 |
if ($("#etimeclockwp_pwd").val() === "") { |
| 80 |
$("#etimeclockwp_pwd").addClass("etimeclockwp-error"); |
| 81 |
$(".etimeclockwp-pwd-error").show(); |
| 82 |
hasErrors = true; |
| 83 |
} |
| 84 |
|
| 85 |
// Prevent form submission if there are errors |
| 86 |
if (hasErrors) { |
| 87 |
e.preventDefault(); |
| 88 |
// Scroll to top where errors are visible |
| 89 |
$("html, body").animate({ scrollTop: 0 }, "fast"); |
| 90 |
return false; |
| 91 |
} |
| 92 |
}); |
| 93 |
}); |
| 94 |
</script>'; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
// save metabox |
| 99 |
function etimeclockwp_callback_save($post) { |
| 100 |
global $meta_box, $post; |
| 101 |
|
| 102 |
$etimeclockwp_status = $post->post_status; |
| 103 |
|
| 104 |
// Use nonce for verification |
| 105 |
echo '<input type="hidden" name="etimeclockwp_MetaNonce" value="', wp_create_nonce(basename(__FILE__)), '" />'; |
| 106 |
|
| 107 |
echo '<input type="hidden" name="etimeclockwp_submit" value="1" />'; |
| 108 |
|
| 109 |
echo '<table><tr>'; |
| 110 |
|
| 111 |
echo "</td><td align='right'><input id='publish' class='button-primary' type='submit' value='"; echo __('Save','time-clock'); echo "' accesskey='p' tabindex='5' name='save'></td></tr><tr>"; |
| 112 |
|
| 113 |
echo '</tr></table>'; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
// save |
| 118 |
function etimeclockwp_save_meta_box_button($post_id) { |
| 119 |
|
| 120 |
if (isset($_POST['etimeclockwp_submit']) && $_POST['etimeclockwp_submit'] == "1") { |
| 121 |
|
| 122 |
// verify nonce |
| 123 |
if (!wp_verify_nonce($_POST['etimeclockwp_MetaNonce'], basename(__FILE__))) { |
| 124 |
return $post_id; |
| 125 |
} |
| 126 |
|
| 127 |
// check autosave |
| 128 |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
| 129 |
return $post_id; |
| 130 |
} |
| 131 |
|
| 132 |
// Server-side validation as a backup |
| 133 |
if (empty($_POST['etimeclockwp_name']) || empty($_POST['etimeclockwp_id']) || empty($_POST['etimeclockwp_pwd'])) { |
| 134 |
return $post_id; |
| 135 |
} |
| 136 |
|
| 137 |
// update values |
| 138 |
|
| 139 |
// name |
| 140 |
update_post_meta($post_id,'etimeclockwp_name',sanitize_text_field($_POST['etimeclockwp_name'])); |
| 141 |
|
| 142 |
// user id |
| 143 |
update_post_meta($post_id,'etimeclockwp_id',sanitize_text_field($_POST['etimeclockwp_id'])); |
| 144 |
|
| 145 |
// password |
| 146 |
update_post_meta($post_id,'etimeclockwp_pwd',sanitize_text_field($_POST['etimeclockwp_pwd'])); |
| 147 |
|
| 148 |
// wordpress account |
| 149 |
if (isset($_POST['etimeclockwp_wp_account'])) { |
| 150 |
update_post_meta($post_id,'etimeclockwp_wp_account',sanitize_text_field($_POST['etimeclockwp_wp_account'])); |
| 151 |
|
| 152 |
// save the meta id key as a user meta value, this makes seeing if the user has a time clock account more more efficient |
| 153 |
$user = get_user_by('slug',sanitize_text_field($_POST['etimeclockwp_wp_account'])); |
| 154 |
if ($user) { |
| 155 |
update_user_meta($user->ID, 'etimeclockwp_meta_id', $post_id); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// to avoid infinite loop |
| 160 |
remove_action('save_post','etimeclockwp_save_meta_box_button'); |
| 161 |
|
| 162 |
|
| 163 |
$action_array = array( |
| 164 |
'ID' => $post_id, |
| 165 |
'post_status' => 'publish', |
| 166 |
); |
| 167 |
|
| 168 |
$result = wp_update_post($action_array); |
| 169 |
|
| 170 |
} |
| 171 |
} |
| 172 |
add_action( 'save_post', 'etimeclockwp_save_meta_box_button' ); |
| 173 |
|
| 174 |
|
| 175 |
// title |
| 176 |
function etimeclockwp_modify_title_question( $data , $postarr ) { |
| 177 |
|
| 178 |
if ($data['post_type'] == 'etimeclockwp_users') { |
| 179 |
if (isset($_POST['etimeclockwp_name'])) { |
| 180 |
$data['post_title'] = sanitize_text_field($_POST['etimeclockwp_name']); |
| 181 |
} |
| 182 |
} |
| 183 |
return $data; |
| 184 |
} |
| 185 |
add_filter('wp_insert_post_data','etimeclockwp_modify_title_question','99',2); |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
// user table - fill table columns with data |
| 191 |
function etimeclockwp_manage_button_columns( $column, $post_id ) { |
| 192 |
global $post; |
| 193 |
|
| 194 |
switch( $column ) { |
| 195 |
|
| 196 |
case 'user_id' : |
| 197 |
$etimeclockwp_id = sanitize_text_field(get_post_meta($post->ID,'etimeclockwp_id', true)); |
| 198 |
echo $etimeclockwp_id; |
| 199 |
break; |
| 200 |
|
| 201 |
default : |
| 202 |
break; |
| 203 |
} |
| 204 |
} |
| 205 |
add_action( 'manage_etimeclockwp_users_posts_custom_column', 'etimeclockwp_manage_button_columns', 10, 2 ); |
| 206 |
|
| 207 |
|
| 208 |
// titles for admin button table |
| 209 |
function etimeclockwp_users_columns($columns) { |
| 210 |
|
| 211 |
$columns = array( |
| 212 |
'cb' => '<input type="checkbox" />', |
| 213 |
'title' => __( 'User Name','time-clock'), |
| 214 |
'user_id' => __( 'User ID','time-clock'), |
| 215 |
); |
| 216 |
|
| 217 |
return $columns; |
| 218 |
} |
| 219 |
add_filter('manage_edit-etimeclockwp_users_columns','etimeclockwp_users_columns'); |
| 220 |
|
| 221 |
|
| 222 |
// users table - remove quick links |
| 223 |
function etimeclockwp_quick_links($actions, $post) { |
| 224 |
|
| 225 |
if ($post->post_type =="etimeclockwp_users") { |
| 226 |
unset($actions['inline hide-if-no-js']); |
| 227 |
unset($actions['edit']); |
| 228 |
unset($actions['trash']); |
| 229 |
unset($actions['view']); |
| 230 |
} |
| 231 |
return $actions; |
| 232 |
} |
| 233 |
add_filter('post_row_actions','etimeclockwp_quick_links', 10, 2); |
| 234 |
|
| 235 |
|
| 236 |
// users table - update message |
| 237 |
add_filter('post_updated_messages', 'codex_etimeclockwp_users_updated_messages'); |
| 238 |
function codex_etimeclockwp_users_updated_messages( $messages ) { |
| 239 |
global $post, $post_ID; |
| 240 |
|
| 241 |
$messages['etimeclockwp_users'] = array( |
| 242 |
1 => __('User Information updated.', 'time-clock'), |
| 243 |
); |
| 244 |
|
| 245 |
return $messages; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
// users table - remove subsubsub menu links |
| 250 |
function etimeclockwp_users_table_sub_menu_links( $views ) { |
| 251 |
|
| 252 |
unset($views['private']); |
| 253 |
unset($views['mine']); |
| 254 |
unset($views['publish']); |
| 255 |
|
| 256 |
return $views; |
| 257 |
} |
| 258 |
add_filter('views_edit-etimeclockwp_users','etimeclockwp_users_table_sub_menu_links'); |