| @@ -1,147 +1,147 @@ | ||
| 1 | -<?php | |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | - exit; // Exit if accessed directly | |
| 4 | -} | |
| 5 | - | |
| 6 | -if ( ! class_exists( 'WPPM_profile_update' ) ) : | |
| 7 | - | |
| 8 | - final class WPPM_profile_update { | |
| 9 | - | |
| 10 | - // constructor | |
| 11 | - public function __construct() { | |
| 12 | - $this->setup_actions(); | |
| 13 | - } | |
| 14 | - | |
| 15 | - /** | |
| 16 | - * Setup the admin hooks, actions and filters | |
| 17 | - * | |
| 18 | - * @return void | |
| 19 | - */ | |
| 20 | - function setup_actions() { | |
| 21 | - // Bail if in network admin | |
| 22 | - if ( is_network_admin() ) { | |
| 23 | - return; | |
| 24 | - } | |
| 25 | - | |
| 26 | - // User profile edit/display actions | |
| 27 | - add_action( 'edit_user_profile', array($this, 'profile' ) ); | |
| 28 | - add_action( 'show_user_profile', array( $this, 'profile' ) ); | |
| 29 | - add_action( 'profile_update', array( $this, 'profile_update' ), 10, 2 ); | |
| 30 | - } | |
| 31 | - | |
| 32 | - function profile($profile_user){ | |
| 33 | - if ( ! current_user_can( 'edit_user', $profile_user->ID ) || !current_user_can( 'manage_options') ) { | |
| 34 | - return; | |
| 35 | - } ?> | |
| 36 | - <h3><?php esc_html_e( 'Taskbuilder', 'taskbuilder' ); ?></h3> | |
| 37 | - <?php | |
| 38 | - $this->capbility_form( $profile_user ); | |
| 39 | - | |
| 40 | - do_action( 'wppm_user_profile', $profile_user ); | |
| 41 | - | |
| 42 | - wp_nonce_field( 'wppm_nonce', 'wppm_profile_nonce' ); | |
| 43 | - | |
| 44 | - } | |
| 45 | - protected function capbility_form( $user ) { | |
| 46 | - global $wppmfunction, $wpdb; | |
| 47 | - if ( user_can( $user->ID, 'manage_options' ) ) { | |
| 48 | - return; | |
| 49 | - } | |
| 50 | - | |
| 51 | - if ( $user->ID == get_current_user_id() ) { | |
| 52 | - return; | |
| 53 | - } | |
| 54 | - | |
| 55 | - $meta_value = get_user_meta( $user->ID, 'wppm_capability', true ); | |
| 56 | - ?> | |
| 57 | - <table class="form-table"> | |
| 58 | - <tbody> | |
| 59 | - <tr> | |
| 60 | - <th><?php esc_html_e( 'Capability', 'taskbuilder' ); ?></th> | |
| 61 | - | |
| 62 | - <td> | |
| 63 | - <fieldset> | |
| 64 | - <select name="wppm_capability"> | |
| 65 | - <option value=""> | |
| 66 | - <?php echo esc_html_e( '— No capability for this user —', 'taskbuilder' ); ?> | |
| 67 | - </option> | |
| 68 | - <?php | |
| 69 | - foreach ( $wppmfunction->wppm_user_role() as $cap_key => $label ) : ?> | |
| 70 | - <option value="<?php echo esc_attr( $cap_key ); ?>" | |
| 71 | - <?php selected( $meta_value, $cap_key ); ?>> | |
| 72 | - <?php echo esc_html( $label['label'] ); ?> | |
| 73 | - </option> | |
| 74 | - | |
| 75 | - <?php endforeach; ?> | |
| 76 | - ?> | |
| 77 | - </select> | |
| 78 | - </fieldset> | |
| 79 | - </td> | |
| 80 | - </tr> | |
| 81 | - </tbody> | |
| 82 | - </table> | |
| 83 | - <?php | |
| 84 | - } | |
| 85 | - | |
| 86 | - function profile_update( $user_id, $prev_data ) { | |
| 87 | - global $wppmfunction,$wpdb; | |
| 88 | - if ( | |
| 89 | - ! isset( $_POST['wppm_profile_nonce'] ) | |
| 90 | - || | |
| 91 | - ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wppm_profile_nonce'] ) ), 'wppm_nonce' ) | |
| 92 | - ) { | |
| 93 | - return; | |
| 94 | - } | |
| 95 | - | |
| 96 | - $cap_key = empty( $_POST['wppm_capability'] ) ? '' : sanitize_text_field( wp_unslash( $_POST['wppm_capability'] ) ); | |
| 97 | - | |
| 98 | - if ( !current_user_can( 'manage_options' ) ) { | |
| 99 | - return; | |
| 100 | - } | |
| 101 | - | |
| 102 | - $user_id = empty( $user_id ) ? 0 : absint( $user_id ); | |
| 103 | - | |
| 104 | - $this->update_user_capability( $user_id, $cap_key ); | |
| 105 | - | |
| 106 | - do_action( 'wppm_update_profile', $user_id, $prev_data ); | |
| 107 | - } | |
| 108 | - | |
| 109 | - function update_user_capability( $user_id, $cap_key ) { | |
| 110 | - global $wppmfunction,$wpdb; | |
| 111 | - if ( empty( $cap_key ) ) { | |
| 112 | - update_user_meta( $user_id, 'wppm_capability', '' ); | |
| 113 | - $this->remove_capability( $user_id ); | |
| 114 | - return; | |
| 115 | - } | |
| 116 | - | |
| 117 | - update_user_meta( $user_id, 'wppm_capability', $cap_key ); | |
| 118 | - | |
| 119 | - $this->remove_capability( $user_id,$cap_key ); | |
| 120 | - $this->add_capability( $user_id, $cap_key ); | |
| 121 | - | |
| 122 | - } | |
| 123 | - | |
| 124 | - function remove_capability( $user_id ) { | |
| 125 | - global $wppmfunction,$wpdb; | |
| 126 | - $user = get_user_by( 'id', $user_id ); | |
| 127 | - | |
| 128 | - foreach ( $wppmfunction->wppm_user_role() as $meta_key => $label ) { | |
| 129 | - $user->remove_cap( $meta_key ); | |
| 130 | - } | |
| 131 | - } | |
| 132 | - | |
| 133 | - function add_capability( $user_id, $cap_key ) { | |
| 134 | - global $wppmfunction,$wpdb; | |
| 135 | - $user = get_user_by( 'id', $user_id ); | |
| 136 | - $user_cap_slug = $wppmfunction->wppm_user_role(); | |
| 137 | - foreach($user_cap_slug as $key=>$val){ | |
| 138 | - if($cap_key== $key){ | |
| 139 | - $user->add_cap($key ); | |
| 140 | - } | |
| 141 | - } | |
| 142 | - $user->add_cap( $cap_key ); | |
| 143 | - } | |
| 144 | - } | |
| 145 | -endif; | |
| 146 | - | |
| 1 | +<?php | |
| 2 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | + exit; // Exit if accessed directly | |
| 4 | +} | |
| 5 | + | |
| 6 | +if ( ! class_exists( 'WPPM_profile_update' ) ) : | |
| 7 | + | |
| 8 | + final class WPPM_profile_update { | |
| 9 | + | |
| 10 | + // constructor | |
| 11 | + public function __construct() { | |
| 12 | + $this->setup_actions(); | |
| 13 | + } | |
| 14 | + | |
| 15 | + /** | |
| 16 | + * Setup the admin hooks, actions and filters | |
| 17 | + * | |
| 18 | + * @return void | |
| 19 | + */ | |
| 20 | + function setup_actions() { | |
| 21 | + // Bail if in network admin | |
| 22 | + if ( is_network_admin() ) { | |
| 23 | + return; | |
| 24 | + } | |
| 25 | + | |
| 26 | + // User profile edit/display actions | |
| 27 | + add_action( 'edit_user_profile', array($this, 'profile' ) ); | |
| 28 | + add_action( 'show_user_profile', array( $this, 'profile' ) ); | |
| 29 | + add_action( 'profile_update', array( $this, 'profile_update' ), 10, 2 ); | |
| 30 | + } | |
| 31 | + | |
| 32 | + function profile($profile_user){ | |
| 33 | + if ( ! current_user_can( 'edit_user', $profile_user->ID ) || !current_user_can( 'manage_options') ) { | |
| 34 | + return; | |
| 35 | + } ?> | |
| 36 | + <h3><?php esc_html_e( 'Taskbuilder', 'taskbuilder' ); ?></h3> | |
| 37 | + <?php | |
| 38 | + $this->capbility_form( $profile_user ); | |
| 39 | + | |
| 40 | + do_action( 'wppm_user_profile', $profile_user ); | |
| 41 | + | |
| 42 | + wp_nonce_field( 'wppm_nonce', 'wppm_profile_nonce' ); | |
| 43 | + | |
| 44 | + } | |
| 45 | + protected function capbility_form( $user ) { | |
| 46 | + global $wppmfunction, $wpdb; | |
| 47 | + if ( user_can( $user->ID, 'manage_options' ) ) { | |
| 48 | + return; | |
| 49 | + } | |
| 50 | + | |
| 51 | + if ( $user->ID == get_current_user_id() ) { | |
| 52 | + return; | |
| 53 | + } | |
| 54 | + | |
| 55 | + $meta_value = get_user_meta( $user->ID, 'wppm_capability', true ); | |
| 56 | + ?> | |
| 57 | + <table class="form-table"> | |
| 58 | + <tbody> | |
| 59 | + <tr> | |
| 60 | + <th><?php esc_html_e( 'Capability', 'taskbuilder' ); ?></th> | |
| 61 | + | |
| 62 | + <td> | |
| 63 | + <fieldset> | |
| 64 | + <select name="wppm_capability"> | |
| 65 | + <option value=""> | |
| 66 | + <?php echo esc_html_e( '— No capability for this user —', 'taskbuilder' ); ?> | |
| 67 | + </option> | |
| 68 | + <?php | |
| 69 | + foreach ( $wppmfunction->wppm_user_role() as $cap_key => $label ) : ?> | |
| 70 | + <option value="<?php echo esc_attr( $cap_key ); ?>" | |
| 71 | + <?php selected( $meta_value, $cap_key ); ?>> | |
| 72 | + <?php echo esc_html( $label['label'] ); ?> | |
| 73 | + </option> | |
| 74 | + | |
| 75 | + <?php endforeach; ?> | |
| 76 | + ?> | |
| 77 | + </select> | |
| 78 | + </fieldset> | |
| 79 | + </td> | |
| 80 | + </tr> | |
| 81 | + </tbody> | |
| 82 | + </table> | |
| 83 | + <?php | |
| 84 | + } | |
| 85 | + | |
| 86 | + function profile_update( $user_id, $prev_data ) { | |
| 87 | + global $wppmfunction,$wpdb; | |
| 88 | + if ( | |
| 89 | + ! isset( $_POST['wppm_profile_nonce'] ) | |
| 90 | + || | |
| 91 | + ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wppm_profile_nonce'] ) ), 'wppm_nonce' ) | |
| 92 | + ) { | |
| 93 | + return; | |
| 94 | + } | |
| 95 | + | |
| 96 | + $cap_key = empty( $_POST['wppm_capability'] ) ? '' : sanitize_text_field( wp_unslash( $_POST['wppm_capability'] ) ); | |
| 97 | + | |
| 98 | + if ( !current_user_can( 'manage_options' ) ) { | |
| 99 | + return; | |
| 100 | + } | |
| 101 | + | |
| 102 | + $user_id = empty( $user_id ) ? 0 : absint( $user_id ); | |
| 103 | + | |
| 104 | + $this->update_user_capability( $user_id, $cap_key ); | |
| 105 | + | |
| 106 | + do_action( 'wppm_update_profile', $user_id, $prev_data ); | |
| 107 | + } | |
| 108 | + | |
| 109 | + function update_user_capability( $user_id, $cap_key ) { | |
| 110 | + global $wppmfunction,$wpdb; | |
| 111 | + if ( empty( $cap_key ) ) { | |
| 112 | + update_user_meta( $user_id, 'wppm_capability', '' ); | |
| 113 | + $this->remove_capability( $user_id ); | |
| 114 | + return; | |
| 115 | + } | |
| 116 | + | |
| 117 | + update_user_meta( $user_id, 'wppm_capability', $cap_key ); | |
| 118 | + | |
| 119 | + $this->remove_capability( $user_id,$cap_key ); | |
| 120 | + $this->add_capability( $user_id, $cap_key ); | |
| 121 | + | |
| 122 | + } | |
| 123 | + | |
| 124 | + function remove_capability( $user_id ) { | |
| 125 | + global $wppmfunction,$wpdb; | |
| 126 | + $user = get_user_by( 'id', $user_id ); | |
| 127 | + | |
| 128 | + foreach ( $wppmfunction->wppm_user_role() as $meta_key => $label ) { | |
| 129 | + $user->remove_cap( $meta_key ); | |
| 130 | + } | |
| 131 | + } | |
| 132 | + | |
| 133 | + function add_capability( $user_id, $cap_key ) { | |
| 134 | + global $wppmfunction,$wpdb; | |
| 135 | + $user = get_user_by( 'id', $user_id ); | |
| 136 | + $user_cap_slug = $wppmfunction->wppm_user_role(); | |
| 137 | + foreach($user_cap_slug as $key=>$val){ | |
| 138 | + if($cap_key== $key){ | |
| 139 | + $user->add_cap($key ); | |
| 140 | + } | |
| 141 | + } | |
| 142 | + $user->add_cap( $cap_key ); | |
| 143 | + } | |
| 144 | + } | |
| 145 | +endif; | |
| 146 | + | |
| 147 | 147 | new WPPM_profile_update(); |