| 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', 'etimeclockwp' ), 'etimeclockwp_callback_general', 'etimeclockwp_users','normal'); |
| 9 |
add_meta_box('meta-box-id-order', __( 'Save', 'etimeclockwp' ), '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; }</style>"; |
| 21 |
|
| 22 |
// Use nonce for verification |
| 23 |
echo '<input type="hidden" name="etimeclockwp_MetaNonce" value="', wp_create_nonce(basename(__FILE__)), '" />'; |
| 24 |
|
| 25 |
echo '<input type="hidden" name="etimeclockwp_submit" value="1" />'; |
| 26 |
|
| 27 |
echo '<table class="form-table"><tr>'; |
| 28 |
|
| 29 |
$etimeclockwp_name = sanitize_text_field(get_post_meta($post->ID,'etimeclockwp_name', true)); |
| 30 |
echo "<td class='etimeclockwp_cell_width_product'>"; echo __('Full Name','etimeclockwp'); echo ": </td><td><input size='40' type='text' name='etimeclockwp_name' value='$etimeclockwp_name'> ("; echo __( 'Required' ); echo ")</td>"; |
| 31 |
|
| 32 |
echo "</tr><tr>"; |
| 33 |
|
| 34 |
$etimeclockwp_id = sanitize_text_field(get_post_meta($post->ID,'etimeclockwp_id', true)); |
| 35 |
echo "<td class='etimeclockwp_cell_width_product'>"; echo __('User ID','etimeclockwp'); echo ": </td><td><input size='40' type='text' name='etimeclockwp_id' value='$etimeclockwp_id'> ("; echo __( 'Required. The User ID should not contains spaces. Example: JSmith87', 'etimeclockwp' ); echo ")</td>"; |
| 36 |
|
| 37 |
echo "</tr><tr>"; |
| 38 |
|
| 39 |
$etimeclockwp_pwd = sanitize_text_field(get_post_meta($post->ID,'etimeclockwp_pwd', true)); |
| 40 |
echo "<td class='etimeclockwp_cell_width_product'>"; echo __('Password','etimeclockwp'); echo ": </td><td><input size='40' type='text' name='etimeclockwp_pwd' value='$etimeclockwp_pwd'> ("; echo __( 'Required', 'etimeclockwp' ); echo ")</td>"; |
| 41 |
|
| 42 |
echo '</tr></table>'; |
| 43 |
|
| 44 |
echo "</div>"; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
// save metabox |
| 49 |
function etimeclockwp_callback_save($post) { |
| 50 |
global $meta_box, $post; |
| 51 |
|
| 52 |
$etimeclockwp_status = $post->post_status; |
| 53 |
|
| 54 |
// Use nonce for verification |
| 55 |
echo '<input type="hidden" name="etimeclockwp_MetaNonce" value="', wp_create_nonce(basename(__FILE__)), '" />'; |
| 56 |
|
| 57 |
echo '<input type="hidden" name="etimeclockwp_submit" value="1" />'; |
| 58 |
|
| 59 |
echo '<table><tr>'; |
| 60 |
|
| 61 |
echo "</td><td align='right'><input id='publish' class='button-primary' type='submit' value='"; echo __('Save','etimeclockwp'); echo "' accesskey='p' tabindex='5' name='save'></td></tr><tr>"; |
| 62 |
|
| 63 |
echo '</tr></table>'; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
// save |
| 68 |
function etimeclockwp_save_meta_box_button($post_id) { |
| 69 |
|
| 70 |
if (isset($_POST['etimeclockwp_submit']) && $_POST['etimeclockwp_submit'] == "1") { |
| 71 |
|
| 72 |
// verify nonce |
| 73 |
if (!wp_verify_nonce($_POST['etimeclockwp_MetaNonce'], basename(__FILE__))) { |
| 74 |
return $post_id; |
| 75 |
} |
| 76 |
|
| 77 |
// check autosave |
| 78 |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
| 79 |
return $post_id; |
| 80 |
} |
| 81 |
|
| 82 |
// update values |
| 83 |
|
| 84 |
// name |
| 85 |
update_post_meta($post_id,'etimeclockwp_name',sanitize_text_field($_POST['etimeclockwp_name'])); |
| 86 |
|
| 87 |
// user id |
| 88 |
update_post_meta($post_id,'etimeclockwp_id',sanitize_text_field($_POST['etimeclockwp_id'])); |
| 89 |
|
| 90 |
// password |
| 91 |
update_post_meta($post_id,'etimeclockwp_pwd',sanitize_text_field($_POST['etimeclockwp_pwd'])); |
| 92 |
|
| 93 |
// wordpress account |
| 94 |
update_post_meta($post_id,'etimeclockwp_wp_account',sanitize_text_field($_POST['etimeclockwp_wp_account'])); |
| 95 |
|
| 96 |
|
| 97 |
// to avoid infinite loop |
| 98 |
remove_action('save_post','etimeclockwp_save_meta_box_button'); |
| 99 |
|
| 100 |
|
| 101 |
$action_array = array( |
| 102 |
'ID' => $post_id, |
| 103 |
'post_status' => 'publish', |
| 104 |
); |
| 105 |
|
| 106 |
$result = wp_update_post($action_array); |
| 107 |
|
| 108 |
// save the meta id key as a user meta value, this makes seeing if the user has a time clock account more more efficient |
| 109 |
$user = get_user_by('slug',sanitize_text_field($_POST['etimeclockwp_wp_account'])); |
| 110 |
update_user_meta($user->ID, 'etimeclockwp_meta_id' , $result); |
| 111 |
|
| 112 |
} |
| 113 |
} |
| 114 |
add_action( 'save_post', 'etimeclockwp_save_meta_box_button' ); |
| 115 |
|
| 116 |
|
| 117 |
// title |
| 118 |
function etimeclockwp_modify_title_question( $data , $postarr ) { |
| 119 |
|
| 120 |
if ($data['post_type'] == 'etimeclockwp_users') { |
| 121 |
if (isset($_POST['etimeclockwp_name'])) { |
| 122 |
$data['post_title'] = sanitize_text_field($_POST['etimeclockwp_name']); |
| 123 |
} |
| 124 |
} |
| 125 |
return $data; |
| 126 |
} |
| 127 |
add_filter('wp_insert_post_data','etimeclockwp_modify_title_question','99',2); |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
// user table - fill table columns with data |
| 133 |
function etimeclockwp_manage_button_columns( $column, $post_id ) { |
| 134 |
global $post; |
| 135 |
|
| 136 |
switch( $column ) { |
| 137 |
|
| 138 |
case 'user_id' : |
| 139 |
$etimeclockwp_id = sanitize_text_field(get_post_meta($post->ID,'etimeclockwp_id', true)); |
| 140 |
echo $etimeclockwp_id; |
| 141 |
break; |
| 142 |
|
| 143 |
default : |
| 144 |
break; |
| 145 |
} |
| 146 |
} |
| 147 |
add_action( 'manage_etimeclockwp_users_posts_custom_column', 'etimeclockwp_manage_button_columns', 10, 2 ); |
| 148 |
|
| 149 |
|
| 150 |
// titles for admin button table |
| 151 |
function etimeclockwp_users_columns($columns) { |
| 152 |
|
| 153 |
$columns = array( |
| 154 |
'cb' => '<input type="checkbox" />', |
| 155 |
'title' => __( 'User Name','etimeclockwp'), |
| 156 |
'user_id' => __( 'User ID','etimeclockwp'), |
| 157 |
); |
| 158 |
|
| 159 |
return $columns; |
| 160 |
} |
| 161 |
add_filter('manage_edit-etimeclockwp_users_columns','etimeclockwp_users_columns'); |
| 162 |
|
| 163 |
|
| 164 |
// users table - remove quick links |
| 165 |
function etimeclockwp_quick_links($actions, $post) { |
| 166 |
|
| 167 |
if ($post->post_type =="etimeclockwp_users") { |
| 168 |
unset($actions['inline hide-if-no-js']); |
| 169 |
unset($actions['edit']); |
| 170 |
unset($actions['trash']); |
| 171 |
unset($actions['view']); |
| 172 |
} |
| 173 |
return $actions; |
| 174 |
} |
| 175 |
add_filter('post_row_actions','etimeclockwp_quick_links', 10, 2); |
| 176 |
|
| 177 |
|
| 178 |
// users table - update message |
| 179 |
add_filter('post_updated_messages', 'codex_etimeclockwp_users_updated_messages'); |
| 180 |
function codex_etimeclockwp_users_updated_messages( $messages ) { |
| 181 |
global $post, $post_ID; |
| 182 |
|
| 183 |
$messages['etimeclockwp_users'] = array( |
| 184 |
1 => __('User Information updated.'), |
| 185 |
); |
| 186 |
|
| 187 |
return $messages; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
// users table - remove subsubsub menu links |
| 192 |
function etimeclockwp_users_table_sub_menu_links( $views ) { |
| 193 |
|
| 194 |
unset($views['private']); |
| 195 |
unset($views['mine']); |
| 196 |
unset($views['publish']); |
| 197 |
|
| 198 |
return $views; |
| 199 |
} |
| 200 |
add_filter('views_edit-etimeclockwp_users','etimeclockwp_users_table_sub_menu_links'); |